70 if (
xcsf->ea->pred_reset) {
166 return fmax(acc, DBL_EPSILON);
184 const double *y,
const int set_num,
const bool cur)
242 const bool print_act,
const bool print_pred)
245 printf(
"%s\n", json_str);
367 if (cm || pm || am) {
386 if (cc || pc || ac) {
427 s += fwrite(&c->
err,
sizeof(
double), 1, fp);
428 s += fwrite(&c->
fit,
sizeof(
double), 1, fp);
429 s += fwrite(&c->
num,
sizeof(
int), 1, fp);
430 s += fwrite(&c->
exp,
sizeof(
int), 1, fp);
431 s += fwrite(&c->
size,
sizeof(
double), 1, fp);
432 s += fwrite(&c->
time,
sizeof(
int), 1, fp);
433 s += fwrite(&c->
m,
sizeof(
bool), 1, fp);
434 s += fwrite(&c->
age,
sizeof(
int), 1, fp);
435 s += fwrite(&c->
mtotal,
sizeof(
int), 1, fp);
437 s += fwrite(&c->
action,
sizeof(
int), 1, fp);
455 s += fread(&c->
err,
sizeof(
double), 1, fp);
456 s += fread(&c->
fit,
sizeof(
double), 1, fp);
457 s += fread(&c->
num,
sizeof(
int), 1, fp);
458 s += fread(&c->
exp,
sizeof(
int), 1, fp);
459 s += fread(&c->
size,
sizeof(
double), 1, fp);
460 s += fread(&c->
time,
sizeof(
int), 1, fp);
461 s += fread(&c->
m,
sizeof(
bool), 1, fp);
462 s += fread(&c->
age,
sizeof(
int), 1, fp);
463 s += fread(&c->
mtotal,
sizeof(
int), 1, fp);
466 s += fread(&c->
action,
sizeof(
int), 1, fp);
487 const bool return_cond,
const bool return_act,
488 const bool return_pred)
490 cJSON *json = cJSON_CreateObject();
491 cJSON_AddNumberToObject(json,
"error", c->
err);
492 cJSON_AddNumberToObject(json,
"fitness", c->
fit);
493 cJSON_AddNumberToObject(json,
"accuracy",
cl_acc(
xcsf, c));
494 cJSON_AddNumberToObject(json,
"set_size", c->
size);
495 cJSON_AddNumberToObject(json,
"numerosity", c->
num);
496 cJSON_AddNumberToObject(json,
"experience", c->
exp);
497 cJSON_AddNumberToObject(json,
"time", c->
time);
498 cJSON_AddNumberToObject(json,
"samples_seen", c->
age);
499 cJSON_AddNumberToObject(json,
"samples_matched", c->
mtotal);
500 cJSON_AddBoolToObject(json,
"current_match", c->
m);
501 cJSON_AddNumberToObject(json,
"current_action", c->
action);
503 cJSON_AddItemToObject(json,
"current_prediction", p);
504 char *json_str = NULL;
507 cJSON *condition = cJSON_Parse(json_str);
508 cJSON_AddItemToObject(json,
"condition", condition);
511 if (return_act &&
xcsf->n_actions > 1) {
513 cJSON *action = cJSON_Parse(json_str);
514 cJSON_AddItemToObject(json,
"action", action);
519 cJSON *prediction = cJSON_Parse(json_str);
520 cJSON_AddItemToObject(json,
"prediction", prediction);
523 char *
string = cJSON_Print(json);
536 const cJSON *item = cJSON_GetObjectItem(json,
"error");
537 if (item != NULL && cJSON_IsNumber(item)) {
538 c->
err = item->valuedouble;
540 item = cJSON_GetObjectItem(json,
"fitness");
541 if (item != NULL && cJSON_IsNumber(item)) {
542 c->
fit = item->valuedouble;
544 item = cJSON_GetObjectItem(json,
"set_size");
545 if (item != NULL && cJSON_IsNumber(item)) {
546 c->
size = item->valuedouble;
548 item = cJSON_GetObjectItem(json,
"numerosity");
549 if (item != NULL && cJSON_IsNumber(item)) {
550 c->
num = item->valueint;
552 item = cJSON_GetObjectItem(json,
"experience");
553 if (item != NULL && cJSON_IsNumber(item)) {
554 c->
exp = item->valueint;
556 item = cJSON_GetObjectItem(json,
"time");
557 if (item != NULL && cJSON_IsNumber(item)) {
558 c->
time = item->valueint;
560 item = cJSON_GetObjectItem(json,
"samples_seen");
561 if (item != NULL && cJSON_IsNumber(item)) {
562 c->
age = item->valueint;
564 item = cJSON_GetObjectItem(json,
"samples_matched");
565 if (item != NULL && cJSON_IsNumber(item)) {
566 c->
mtotal = item->valueint;
579 const cJSON *item = cJSON_GetObjectItem(json,
"current_match");
580 if (item != NULL && cJSON_IsBool(item)) {
581 c->
m =
true ? item->type == cJSON_True :
false;
583 item = cJSON_GetObjectItem(json,
"current_action");
584 if (item != NULL && cJSON_IsNumber(item)) {
585 c->
action = item->valueint;
587 item = cJSON_GetObjectItem(json,
"current_prediction");
588 if (item != NULL && cJSON_IsArray(item)) {
589 if (cJSON_GetArraySize(item) !=
xcsf->y_dim) {
590 printf(
"Import error: current prediction length mismatch\n");
593 for (
int i = 0; i <
xcsf->y_dim; ++i) {
594 const cJSON *item_i = cJSON_GetArrayItem(item, i);
614 const cJSON *item = cJSON_GetObjectItem(json,
"action");
618 item = cJSON_GetObjectItem(json,
"prediction");
622 item = cJSON_GetObjectItem(json,
"condition");
void action_set(const struct XCSF *xcsf, struct Cl *c)
Sets a classifier's action functions to the implementations.
Interface for classifier actions.
static void act_init(const struct XCSF *xcsf, struct Cl *c)
Initialises a classifier's action.
static bool act_crossover(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Performs classifier action crossover.
static void act_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Copies the action from one classifier to another.
static char * act_json_export(const struct XCSF *xcsf, const struct Cl *c)
Returns a json formatted string representation of an action .
static bool act_mutate(const struct XCSF *xcsf, const struct Cl *c)
Performs classifier action mutation.
static void act_cover(const struct XCSF *xcsf, const struct Cl *c, const double *x, const int action)
Generates an action that matches the specified value.
static size_t act_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Reads the action from a file.
static size_t act_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Writes the action to a file.
static int act_compute(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Computes the current classifier action using the input.
static bool act_general(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Returns whether the action of classifier c1 is more general than c2.
static void act_free(const struct XCSF *xcsf, const struct Cl *c)
Frees the memory used by the classifier action.
static void act_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Creates an action from a cJSON object.
static void act_update(const struct XCSF *xcsf, const struct Cl *c, const double *x, const double *y)
Updates the classifier's action.
double cl_pred_size(const struct XCSF *xcsf, const struct Cl *c)
Returns the size of a classifier's prediction.
double cl_mfrac(const struct XCSF *xcsf, const struct Cl *c)
Returns the fraction of observed inputs matched by a classifier.
void cl_init(const struct XCSF *xcsf, struct Cl *c, const double size, const int time)
Initialises a new classifier - but not condition, action, prediction.
bool cl_m(const struct XCSF *xcsf, const struct Cl *c)
Returns whether a classifier matched the most recent input.
double cl_cond_size(const struct XCSF *xcsf, const struct Cl *c)
Returns the size of a classifier's condition.
void cl_rand(const struct XCSF *xcsf, struct Cl *c)
Initialises random actions, conditions and predictions.
void cl_update(const struct XCSF *xcsf, struct Cl *c, const double *x, const double *y, const int set_num, const bool cur)
Updates a classifier's experience, error, and set size.
void cl_update_fit(const struct XCSF *xcsf, struct Cl *c, const double acc_sum, const double acc)
Updates the fitness of a classifier.
int cl_action(const struct XCSF *xcsf, struct Cl *c, const double *x)
Computes the current classifier action using the input.
const double * cl_predict(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Computes the current classifier prediction using the input.
char * cl_json_export(const struct XCSF *xcsf, const struct Cl *c, const bool return_cond, const bool return_act, const bool return_pred)
Returns a json formatted string representation of a classifier.
bool cl_general(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Returns whether classifier c1 is more general than c2.
void cl_init_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Initialises and creates a copy of one classifier from another.
bool cl_match(const struct XCSF *xcsf, struct Cl *c, const double *x)
Calculates whether a classifier matches an input.
bool cl_mutate(const struct XCSF *xcsf, const struct Cl *c)
Performs classifier mutation.
size_t cl_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Writes a classifier to a file.
void cl_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Copies condition, action, and prediction structures.
size_t cl_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Reads a classifier from a file.
bool cl_crossover(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Performs classifier crossover.
void cl_cover(const struct XCSF *xcsf, struct Cl *c, const double *x, const int action)
Covers the condition and action for a classifier.
static void cl_json_import_current(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Sets a classifier's current properties from a cJSON object.
void cl_free(const struct XCSF *xcsf, struct Cl *c)
Frees the memory used by a classifier.
double cl_acc(const struct XCSF *xcsf, const struct Cl *c)
Returns the accuracy of a classifier.
void cl_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Creates a classifier from a cJSON object.
double cl_del_vote(const struct XCSF *xcsf, const struct Cl *c, const double avg_fit)
Returns the deletion vote of a classifier.
static void cl_json_import_properties(struct Cl *c, const cJSON *json)
Sets a classifier's properties from a cJSON object.
bool cl_subsumer(const struct XCSF *xcsf, const struct Cl *c)
Returns whether a classifier is a potential subsumer.
void cl_print(const struct XCSF *xcsf, const struct Cl *c, const bool print_cond, const bool print_act, const bool print_pred)
Prints a classifier.
Functions operating on classifiers.
void condition_set(const struct XCSF *xcsf, struct Cl *c)
Sets a classifier's condition functions to the implementations.
Interface for classifier conditions.
static void cond_free(const struct XCSF *xcsf, const struct Cl *c)
Frees the memory used by the classifier condition.
static bool cond_mutate(const struct XCSF *xcsf, const struct Cl *c)
Performs classifier condition mutation.
static bool cond_crossover(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Performs classifier condition crossover.
static bool cond_general(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Returns whether classifier c1 has a condition more general than c2.
static size_t cond_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Writes the condition to a file.
static void cond_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Creates a condition from a cJSON object.
static void cond_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Copies the condition from one classifier to another.
static double cond_size(const struct XCSF *xcsf, const struct Cl *c)
Returns the size of the classifier condition.
static void cond_cover(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Generates a condition that matches the current input.
static void cond_init(const struct XCSF *xcsf, struct Cl *c)
Initialises a classifier's condition.
static bool cond_match(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Calculates whether the condition matches the input.
static size_t cond_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Reads the condition from a file.
static char * cond_json_export(const struct XCSF *xcsf, const struct Cl *c)
Returns a json formatted string representation of a condition.
static void cond_update(const struct XCSF *xcsf, const struct Cl *c, const double *x, const double *y)
Updates the classifier's condition.
Evolutionary algorithm functions.
Loss functions for calculating prediction error.
void prediction_set(const struct XCSF *xcsf, struct Cl *c)
Sets a classifier's prediction functions to the implementations.
Interface for classifier predictions.
static double pred_size(const struct XCSF *xcsf, const struct Cl *c)
Returns the size of the classifier prediction.
static char * pred_json_export(const struct XCSF *xcsf, const struct Cl *c)
Returns a json formatted string representation of a prediction.
static void pred_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Creates a prediction from a cJSON object.
static bool pred_mutate(const struct XCSF *xcsf, const struct Cl *c)
Performs classifier prediction mutation.
static void pred_compute(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Computes the current classifier prediction using the input.
static size_t pred_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Writes the prediction to a file.
static void pred_update(const struct XCSF *xcsf, const struct Cl *c, const double *x, const double *y)
Updates the classifier's prediction.
static void pred_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Copies the prediction from one classifier to another.
static bool pred_crossover(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Performs classifier prediction crossover.
static void pred_init(const struct XCSF *xcsf, struct Cl *c)
Initialises a classifier's prediction.
static size_t pred_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Reads the prediction from a file.
static void pred_free(const struct XCSF *xcsf, const struct Cl *c)
Frees the memory used by the classifier prediction.
Classifier data structure.
int mtotal
Total number of times actually matched an input.
struct PredVtbl const * pred_vptr
Functions acting on predictions.
int time
Time EA last executed in a participating set.
struct ActVtbl const * act_vptr
Functions acting on actions.
int action
Current classifier action.
bool m
Whether the classifier matches current input.
int age
Total number of times match testing been performed.
double size
Average participated set size.
struct CondVtbl const * cond_vptr
Functions acting on conditions.
double * prediction
Current classifier prediction.
Utility functions for random number handling, etc.