30 #define DONT_CARE ('#')
47 for (
int i = 0; i < cond->
length; ++i) {
67 new->length =
xcsf->x_dim *
xcsf->cond->bits;
68 new->string = malloc(
sizeof(
char) * new->length);
69 new->tmp_input = malloc(
sizeof(
char) *
xcsf->cond->bits);
70 new->mu = malloc(
sizeof(
double) *
N_MU);
100 const struct Cl *src)
104 new->length = src_cond->
length;
105 new->string = malloc(
sizeof(
char) * src_cond->
length);
106 new->tmp_input = malloc(
sizeof(
char) *
xcsf->cond->bits);
107 new->mu = malloc(
sizeof(
double) *
N_MU);
108 memcpy(new->string, src_cond->
string,
sizeof(
char) * src_cond->
length);
109 memcpy(new->mu, src_cond->
mu,
sizeof(
double) *
N_MU);
123 const int bits =
xcsf->cond->bits;
124 for (
int i = 0; i <
xcsf->x_dim; ++i) {
126 for (
int j = 0; j < bits; ++j) {
145 const double *x,
const double *y)
164 const int bits =
xcsf->cond->bits;
165 for (
int i = 0; i <
xcsf->x_dim; ++i) {
167 for (
int j = 0; j < bits; ++j) {
168 const char s = cond->
string[i * bits + j];
190 bool changed =
false;
192 for (
int i = 0; i < cond1->
length; ++i) {
194 const char tmp = cond1->
string[i];
216 bool changed =
false;
217 for (
int i = 0; i < cond->
length; ++i) {
244 bool general =
false;
245 for (
int i = 0; i < cond1->
length; ++i) {
266 printf(
"%s\n", json_str);
297 s += fwrite(&cond->
length,
sizeof(
int), 1, fp);
298 s += fwrite(cond->
string,
sizeof(
char), cond->
length, fp);
299 s += fwrite(cond->
mu,
sizeof(
double),
N_MU, fp);
316 s += fread(&new->length,
sizeof(
int), 1, fp);
317 if (new->length < 1) {
318 printf(
"cond_ternary_load(): read error\n");
322 new->string = malloc(
sizeof(
char) * new->length);
323 s += fread(new->string,
sizeof(
char), new->length, fp);
324 new->tmp_input = malloc(
sizeof(
char) *
xcsf->cond->bits);
325 new->mu = malloc(
sizeof(
double) *
N_MU);
326 s += fread(new->mu,
sizeof(
double),
N_MU, fp);
342 cJSON *json = cJSON_CreateObject();
343 cJSON_AddStringToObject(json,
"type",
"ternary");
344 char buff[cond->
length + 1];
345 memcpy(buff, cond->
string,
sizeof(
char) * cond->
length);
346 buff[cond->
length] =
'\0';
347 cJSON_AddStringToObject(json,
"string", buff);
348 cJSON *mutation = cJSON_CreateDoubleArray(cond->
mu,
N_MU);
349 cJSON_AddItemToObject(json,
"mutation", mutation);
350 char *
string = cJSON_Print(json);
367 const cJSON *item = cJSON_GetObjectItem(json,
"string");
368 if (item != NULL && cJSON_IsString(item)) {
369 for (
int i = 0; i < cond->
length; ++i) {
370 const char bit = item->valuestring[i];
372 printf(
"Import error: string terminated early\n");
390 cJSON *json = cJSON_CreateObject();
391 cJSON_AddNumberToObject(json,
"p_dontcare", cond->
p_dontcare);
392 cJSON_AddNumberToObject(json,
"bits", cond->
bits);
393 char *
string = cJSON_Print(json);
407 for (cJSON *iter = json; iter != NULL; iter = iter->next) {
408 if (strncmp(iter->string,
"p_dontcare\0", 11) == 0 &&
409 cJSON_IsNumber(iter)) {
411 }
else if (strncmp(iter->string,
"bits\0", 5) == 0 &&
412 cJSON_IsNumber(iter)) {
double cond_ternary_size(const struct XCSF *xcsf, const struct Cl *c)
Returns the size of a ternary condition.
static const int MU_TYPE[(1)]
Self-adaptation method for mutating ternary conditions.
void cond_ternary_init(const struct XCSF *xcsf, struct Cl *c)
Creates and initialises a ternary bitstring condition.
bool cond_ternary_match(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Calculates whether a ternary condition matches an input.
char * cond_ternary_param_json_export(const struct XCSF *xcsf)
Returns a json formatted string of the ternary parameters.
void cond_ternary_update(const struct XCSF *xcsf, const struct Cl *c, const double *x, const double *y)
Dummy update function.
bool cond_ternary_general(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Returns whether classifier c1 has a condition more general than c2.
void cond_ternary_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Copies a ternary condition from one classifier to another.
char * cond_ternary_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the ternary parameters from a cJSON object.
void cond_ternary_cover(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Generates a ternary condition that matches the current input.
bool cond_ternary_mutate(const struct XCSF *xcsf, const struct Cl *c)
Mutates a ternary condition.
#define N_MU
Number of ternary mutation rates.
void cond_ternary_print(const struct XCSF *xcsf, const struct Cl *c)
Prints a ternary condition.
void cond_ternary_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Creates a ternary condition from a cJSON object.
void cond_ternary_free(const struct XCSF *xcsf, const struct Cl *c)
Frees the memory used by a ternary condition.
bool cond_ternary_crossover(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Performs uniform crossover with two ternary conditions.
size_t cond_ternary_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Writes a ternary condition to a file.
char * cond_ternary_json_export(const struct XCSF *xcsf, const struct Cl *c)
Returns a json formatted string representation of a ternary condition.
void cond_ternary_param_defaults(struct XCSF *xcsf)
Initialises default ternary condition parameters.
static void cond_ternary_rand(const struct XCSF *xcsf, const struct Cl *c)
Randomises a ternary condition.
size_t cond_ternary_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Reads a ternary condition from a file.
#define DONT_CARE
Don't care symbol.
Ternary condition functions.
void cond_param_set_bits(struct XCSF *xcsf, const int a)
void cond_param_set_p_dontcare(struct XCSF *xcsf, const double a)
Evolutionary algorithm functions.
void sam_json_import(double *mu, const int N, const cJSON *json)
Initialises a mutation vector from a cJSON object.
void sam_init(double *mu, const int N, const int *type)
Initialises self-adaptive mutation rates.
void sam_adapt(double *mu, const int N, const int *type)
Self-adapts mutation rates.
Self-adaptive mutation functions.
#define SAM_LOG_NORMAL
Log normal self-adaptation.
Parameters for initialising and operating conditions.
double p_dontcare
Don't care probability.
int bits
Bits per float to binarise inputs.
Classifier data structure.
void * cond
Condition structure.
Ternary condition data structure.
int length
Length of the bitstring.
char * string
Ternary bitstring.
double * mu
Mutation rates.
char * tmp_input
Temporary storage for float conversion.
double rand_uniform(const double min, const double max)
Returns a uniform random float [min,max].
Utility functions for random number handling, etc.
static void float_to_binary(const double f, char *binary, const int bits)
Generates a binary string from a float.