28 #define MAX_COVER (1000000)
38 struct Clist **delprev)
40 struct Clist *prev = NULL;
42 while (iter != NULL) {
66 struct Clist **delprev)
69 double total_vote = 0;
71 while (iter != NULL) {
76 const int n_spins = (
xcsf->COMPACTION &&
xcsf->error <
xcsf->E0) ? 2 : 1;
77 for (
int i = 0; i < n_spins; ++i) {
79 iter =
xcsf->pset.list;
80 struct Clist *prev = NULL;
91 if (*del == NULL || s > delsize) {
106 struct Clist *del = NULL;
107 struct Clist *delprev = NULL;
118 if (del->
cl->
num == 0) {
121 if (delprev == NULL) {
139 memset(act_covered, 0,
sizeof(
bool) *
xcsf->n_actions);
140 const struct Clist *iter =
xcsf->mset.list;
141 while (iter != NULL) {
142 act_covered[iter->
cl->
action] =
true;
145 for (
int i = 0; i <
xcsf->n_actions; ++i) {
146 if (!act_covered[i]) {
162 bool *act_covered = malloc(
sizeof(
bool) *
xcsf->n_actions);
166 for (
int i = 0; i <
xcsf->n_actions; ++i) {
167 if (!act_covered[i]) {
169 struct Cl *
new = malloc(
sizeof(
struct Cl));
177 const int prev_psize =
xcsf->pset.size;
181 if (prev_psize >
xcsf->pset.size) {
182 const int prev_msize =
xcsf->mset.size;
186 if (prev_msize >
xcsf->mset.size) {
192 printf(
"Error: max covering attempts (%d) exceeded\n",
MAX_COVER);
208 double accs[set->
size];
211 for (
int i = 0; iter != NULL && i < set->
size; ++i) {
213 acc_sum += accs[i] * iter->
cl->
num;
218 for (
int i = 0; iter != NULL && i < set->
size; ++i) {
235 while (iter != NULL) {
236 struct Cl *c = iter->
cl;
244 bool subsumed =
false;
246 while (iter != NULL) {
247 struct Cl *c = iter->
cl;
273 while (iter != NULL) {
283 FILE *f = fopen(
xcsf->population_file,
"rt");
285 printf(
"Error opening JSON file: %s\n",
xcsf->population_file);
288 fseek(f, 0, SEEK_END);
289 const long len = 1 + ftell(f);
290 fseek(f, 0, SEEK_SET);
291 char *json_buffer = malloc(
sizeof(
char) * len);
292 const size_t s = fread(json_buffer,
sizeof(
char), len - 1, f);
294 json_buffer[len - 1] =
'\0';
296 printf(
"Error opening JSON file: %s\n",
xcsf->population_file);
310 if (strncmp(
xcsf->population_file,
"\0", 1) != 0) {
313 if (
xcsf->POP_INIT) {
314 while (
xcsf->pset.num <
xcsf->POP_SIZE) {
315 struct Cl *
new = malloc(
sizeof(
struct Cl));
342 while (
xcsf->pset.num >
xcsf->POP_SIZE) {
358 #ifdef PARALLEL_MATCH
362 for (
int i = 0; iter != NULL && i <
xcsf->pset.size; ++i) {
367 #pragma omp parallel for
368 for (
int i = 0; i <
xcsf->pset.size; ++i) {
373 for (
int i = 0; i <
xcsf->pset.size; ++i) {
381 while (iter != NULL) {
390 if (cover && (
xcsf->n_actions > 1 ||
xcsf->mset.size < 1)) {
406 const struct Clist *iter =
xcsf->mset.list;
407 while (iter != NULL) {
425 if (set->
list == NULL) {
426 set->
list = malloc(
sizeof(
struct Clist));
430 struct Clist *
new = malloc(
sizeof(
struct Clist));
432 new->next = set->
list;
449 const double *y,
const bool cur)
451 #ifdef PARALLEL_UPDATE
454 for (
int i = 0; iter != NULL && i < set->
size; ++i) {
458 #pragma omp parallel for
459 for (
int i = 0; i < set->
size; ++i) {
464 while (iter != NULL) {
470 if (
xcsf->SET_SUBSUMPTION) {
484 struct Clist *prev = NULL;
486 while (iter != NULL) {
487 if (iter->
cl == NULL || iter->
cl->
num == 0) {
516 const bool print_cond,
const bool print_act,
const bool print_pred)
520 printf(
"%s\n", json_str);
533 while (iter != NULL) {
549 while (iter != NULL) {
550 sum += iter->
cl->
fit;
575 while (iter != NULL) {
593 while (iter != NULL) {
613 s += fwrite(&
xcsf->pset.size,
sizeof(
int), 1, fp);
614 s += fwrite(&
xcsf->pset.num,
sizeof(
int), 1, fp);
615 const struct Clist *iter =
xcsf->pset.list;
616 while (iter != NULL) {
631 struct Clist *prev = NULL;
633 while (current != NULL) {
635 current->
next = prev;
639 xcsf->pset.list = prev;
654 s += fread(&size,
sizeof(
int), 1, fp);
655 s += fread(&num,
sizeof(
int), 1, fp);
657 for (
int i = 0; i < size; ++i) {
658 struct Cl *c = malloc(
sizeof(
struct Cl));
678 while (iter != NULL) {
698 while (iter != NULL) {
717 const struct Clist *iter =
xcsf->pset.list;
718 while (iter != NULL) {
719 const double e = iter->
cl->
err;
720 if (e < xcsf->E0 && iter->
cl->
exp *
xcsf->BETA > 1) {
730 double error = DBL_MAX;
731 iter =
xcsf->pset.list;
732 while (iter != NULL) {
733 const double e = iter->
cl->
err;
734 if (e < error && iter->
cl->
exp *
xcsf->BETA > 1) {
755 const bool return_cond,
const bool return_act,
756 const bool return_pred)
758 cJSON *json = cJSON_CreateObject();
759 cJSON *classifiers = cJSON_AddArrayToObject(json,
"classifiers");
761 while (iter != NULL) {
764 cJSON *classifier = cJSON_Parse(str);
765 cJSON_AddItemToArray(classifiers, classifier);
769 char *
string = cJSON_Print(json);
782 struct Cl *
new = malloc(
sizeof(
struct Cl));
798 cJSON *json = cJSON_Parse(json_str);
800 if (json->child != NULL && cJSON_IsArray(json->child)) {
801 cJSON *tail = json->child->child;
803 while (tail->next != NULL) {
806 while (tail != NULL) {
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.
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.
bool cl_match(const struct XCSF *xcsf, struct Cl *c, const double *x)
Calculates whether a classifier matches an input.
size_t cl_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Writes a classifier to a file.
size_t cl_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Reads a classifier from a file.
void cl_cover(const struct XCSF *xcsf, struct Cl *c, const double *x, const int action)
Covers the condition and action for a classifier.
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.
bool cl_subsumer(const struct XCSF *xcsf, const struct Cl *c)
Returns whether a classifier is a potential subsumer.
Functions operating on classifiers.
double clset_mean_pred_size(const struct XCSF *xcsf, const struct Set *set)
Calculates the mean prediction size of classifiers in the set.
void clset_pset_init(struct XCSF *xcsf)
Initialises a new population of random classifiers.
static void clset_pset_del(struct XCSF *xcsf)
Deletes a single classifier from the population set.
void clset_kill(const struct XCSF *xcsf, struct Set *set)
Frees the set and the classifiers.
void clset_update(struct XCSF *xcsf, struct Set *set, const double *x, const double *y, const bool cur)
Provides reinforcement to the set and performs set subsumption.
double clset_total_fit(const struct Set *set)
Calculates the total fitness of classifiers in the set.
double clset_mean_time(const struct Set *set)
Calculates the mean time stamp of classifiers in the set.
void clset_validate(struct Set *set)
Removes classifiers with 0 numerosity from the set.
static void clset_subsumption(struct XCSF *xcsf, struct Set *set)
Performs set subsumption.
void clset_pset_enforce_limit(struct XCSF *xcsf)
Enforces the maximum population size limit.
void clset_add(struct Set *set, struct Cl *c)
Adds a classifier to the set.
void clset_action(struct XCSF *xcsf, const int action)
Constructs the action set from the match set.
void clset_print(const struct XCSF *xcsf, const struct Set *set, const bool print_cond, const bool print_act, const bool print_pred)
Prints the classifiers in the set.
static void clset_pset_roulette(const struct XCSF *xcsf, struct Clist **del, struct Clist **delprev)
Selects a classifier from the population for deletion via roulette.
static void clset_pset_never_match(const struct XCSF *xcsf, struct Clist **del, struct Clist **delprev)
Finds a rule in the population that never matches an input.
double clset_mean_cond_size(const struct XCSF *xcsf, const struct Set *set)
Calculates the mean condition size of classifiers in the set.
void clset_json_insert(struct XCSF *xcsf, const char *json_str)
Creates classifiers from JSON and inserts into the population.
size_t clset_pset_save(const struct XCSF *xcsf, FILE *fp)
Writes the population set to a file.
void clset_init(struct Set *set)
Initialises a new set.
#define MAX_COVER
Maximum number of covering attempts.
static void clset_cover(struct XCSF *xcsf, const double *x)
Ensures all possible actions are covered by the match set.
static void clset_update_fit(const struct XCSF *xcsf, const struct Set *set)
Updates the fitness of classifiers in the set.
void clset_set_times(const struct XCSF *xcsf, const struct Set *set)
Sets the time stamps for classifiers in the set.
static void clset_load_pop_file(struct XCSF *xcsf)
double clset_mfrac(const struct XCSF *xcsf)
Returns the fraction of inputs matched by the most general rule with error below E0....
static double clset_total_time(const struct Set *set)
Calculates the total time stamps of classifiers in the set.
void clset_match(struct XCSF *xcsf, const double *x, const bool cover)
Constructs the match set - forward propagates conditions and actions.
void clset_free(struct Set *set)
Frees the set, but not the classifiers.
static void clset_pset_reverse(struct XCSF *xcsf)
Reverses the order of the population classifier list.
static bool clset_action_coverage(const struct XCSF *xcsf, bool *act_covered)
Checks whether each action is covered by the match set.
void clset_json_insert_cl(struct XCSF *xcsf, const cJSON *json)
Creates a classifier from cJSON and inserts in the population set.
char * clset_json_export(const struct XCSF *xcsf, const struct Set *set, const bool return_cond, const bool return_act, const bool return_pred)
Returns a json formatted string representation of a classifier set.
size_t clset_pset_load(struct XCSF *xcsf, FILE *fp)
Reads the population set from a file.
Functions operating on sets of classifiers.
Classifier data structure.
int mtotal
Total number of times actually matched an input.
int time
Time EA last executed in a participating set.
int action
Current classifier action.
int age
Total number of times match testing been performed.
struct Clist * next
Pointer to the next list element.
struct Cl * cl
Pointer to classifier data structure.
int size
Number of macro-classifiers.
struct Clist * list
Linked list of classifiers.
int num
The total numerosity of classifiers.
double rand_uniform(const double min, const double max)
Returns a uniform random float [min,max].
void utils_json_parse_check(const cJSON *json)
Checks whether JSON parsed correctly.
Utility functions for random number handling, etc.