XCSF  1.4.7
XCSF learning classifier system
clset.c
Go to the documentation of this file.
1 /*
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  */
15 
24 #include "clset.h"
25 #include "cl.h"
26 #include "utils.h"
27 
28 #define MAX_COVER (1000000)
29 
36 static void
37 clset_pset_never_match(const struct XCSF *xcsf, struct Clist **del,
38  struct Clist **delprev)
39 {
40  struct Clist *prev = NULL;
41  struct Clist *iter = xcsf->pset.list;
42  while (iter != NULL) {
43  if (iter->cl->mtotal == 0 && iter->cl->age > xcsf->M_PROBATION) {
44  *del = iter;
45  *delprev = prev;
46  break;
47  }
48  prev = iter;
49  iter = iter->next;
50  }
51 }
52 
64 static void
65 clset_pset_roulette(const struct XCSF *xcsf, struct Clist **del,
66  struct Clist **delprev)
67 {
68  const double avg_fit = clset_total_fit(&xcsf->pset) / xcsf->pset.num;
69  double total_vote = 0;
70  struct Clist *iter = xcsf->pset.list;
71  while (iter != NULL) {
72  total_vote += cl_del_vote(xcsf, iter->cl, avg_fit);
73  iter = iter->next;
74  }
75  double delsize = 0;
76  const int n_spins = (xcsf->COMPACTION && xcsf->error < xcsf->E0) ? 2 : 1;
77  for (int i = 0; i < n_spins; ++i) {
78  // perform a single roulette spin with the deletion vote
79  iter = xcsf->pset.list;
80  struct Clist *prev = NULL;
81  const double p = rand_uniform(0, total_vote);
82  double sum = cl_del_vote(xcsf, iter->cl, avg_fit);
83  while (p > sum) {
84  prev = iter;
85  iter = iter->next;
86  sum += cl_del_vote(xcsf, iter->cl, avg_fit);
87  }
88  // select the rule for deletion if it is the largest sized winner
89  const double s =
90  cl_cond_size(xcsf, iter->cl) + cl_pred_size(xcsf, iter->cl);
91  if (*del == NULL || s > delsize) {
92  *del = iter;
93  *delprev = prev;
94  delsize = s;
95  }
96  }
97 }
98 
103 static void
105 {
106  struct Clist *del = NULL;
107  struct Clist *delprev = NULL;
108  // select any rules that never match
109  clset_pset_never_match(xcsf, &del, &delprev);
110  // if none found, select a rule using roulette wheel
111  if (del == NULL) {
112  clset_pset_roulette(xcsf, &del, &delprev);
113  }
114  // decrement numerosity
115  --(del->cl->num);
116  --(xcsf->pset.num);
117  // remove macro-classifiers as necessary
118  if (del->cl->num == 0) {
119  clset_add(&xcsf->kset, del->cl);
120  --(xcsf->pset.size);
121  if (delprev == NULL) {
122  xcsf->pset.list = del->next;
123  } else {
124  delprev->next = del->next;
125  }
126  free(del);
127  }
128 }
129 
136 static bool
137 clset_action_coverage(const struct XCSF *xcsf, bool *act_covered)
138 {
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;
143  iter = iter->next;
144  }
145  for (int i = 0; i < xcsf->n_actions; ++i) {
146  if (!act_covered[i]) {
147  return false;
148  }
149  }
150  return true;
151 }
152 
158 static void
159 clset_cover(struct XCSF *xcsf, const double *x)
160 {
161  int attempts = 0;
162  bool *act_covered = malloc(sizeof(bool) * xcsf->n_actions);
163  bool covered = clset_action_coverage(xcsf, act_covered);
164  while (!covered) {
165  covered = true;
166  for (int i = 0; i < xcsf->n_actions; ++i) {
167  if (!act_covered[i]) {
168  // create a new classifier with matching condition and action
169  struct Cl *new = malloc(sizeof(struct Cl));
170  cl_init(xcsf, new, (xcsf->mset.num) + 1, xcsf->time);
171  cl_cover(xcsf, new, x, i);
172  clset_add(&xcsf->pset, new);
173  clset_add(&xcsf->mset, new);
174  }
175  }
176  // enforce population size
177  const int prev_psize = xcsf->pset.size;
179  // if a macro classifier was deleted,
180  // remove any deleted rules from the match set
181  if (prev_psize > xcsf->pset.size) {
182  const int prev_msize = xcsf->mset.size;
183  clset_validate(&xcsf->mset);
184  // if the deleted classifier was in the match set,
185  // check if an action is now not covered
186  if (prev_msize > xcsf->mset.size) {
187  covered = clset_action_coverage(xcsf, act_covered);
188  }
189  }
190  ++attempts;
191  if (attempts > MAX_COVER) {
192  printf("Error: max covering attempts (%d) exceeded\n", MAX_COVER);
193  exit(EXIT_FAILURE);
194  }
195  }
196  free(act_covered);
197 }
198 
204 static void
205 clset_update_fit(const struct XCSF *xcsf, const struct Set *set)
206 {
207  double acc_sum = 0;
208  double accs[set->size];
209  // calculate accuracies
210  const struct Clist *iter = set->list;
211  for (int i = 0; iter != NULL && i < set->size; ++i) {
212  accs[i] = cl_acc(xcsf, iter->cl);
213  acc_sum += accs[i] * iter->cl->num;
214  iter = iter->next;
215  }
216  // update fitnesses
217  iter = set->list;
218  for (int i = 0; iter != NULL && i < set->size; ++i) {
219  cl_update_fit(xcsf, iter->cl, acc_sum, accs[i]);
220  iter = iter->next;
221  }
222 }
223 
229 static void
230 clset_subsumption(struct XCSF *xcsf, struct Set *set)
231 {
232  // find the most general subsumer in the set
233  struct Cl *s = NULL;
234  const struct Clist *iter = set->list;
235  while (iter != NULL) {
236  struct Cl *c = iter->cl;
237  if (cl_subsumer(xcsf, c) && (s == NULL || cl_general(xcsf, c, s))) {
238  s = c;
239  }
240  iter = iter->next;
241  }
242  // subsume the more specific classifiers in the set
243  if (s != NULL) {
244  bool subsumed = false;
245  iter = set->list;
246  while (iter != NULL) {
247  struct Cl *c = iter->cl;
248  if (c != NULL && s != c && cl_general(xcsf, s, c)) {
249  s->num += c->num;
250  c->num = 0;
251  clset_add(&xcsf->kset, c);
252  subsumed = true;
253  }
254  iter = iter->next;
255  }
256  if (subsumed) {
257  clset_validate(set);
258  clset_validate(&xcsf->pset);
259  }
260  }
261 }
262 
268 static double
269 clset_total_time(const struct Set *set)
270 {
271  double sum = 0;
272  const struct Clist *iter = set->list;
273  while (iter != NULL) {
274  sum += iter->cl->time * iter->cl->num;
275  iter = iter->next;
276  }
277  return sum;
278 }
279 
280 static void
282 {
283  FILE *f = fopen(xcsf->population_file, "rt");
284  if (f == NULL) {
285  printf("Error opening JSON file: %s\n", xcsf->population_file);
286  exit(EXIT_FAILURE);
287  }
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);
293  fclose(f);
294  json_buffer[len - 1] = '\0';
295  if (s == 0) {
296  printf("Error opening JSON file: %s\n", xcsf->population_file);
297  exit(EXIT_FAILURE);
298  }
299  clset_json_insert(xcsf, json_buffer);
300  free(json_buffer);
301 }
302 
307 void
309 {
310  if (strncmp(xcsf->population_file, "\0", 1) != 0) {
312  }
313  if (xcsf->POP_INIT) {
314  while (xcsf->pset.num < xcsf->POP_SIZE) {
315  struct Cl *new = malloc(sizeof(struct Cl));
316  cl_init(xcsf, new, xcsf->POP_SIZE, 0);
317  cl_rand(xcsf, new);
318  clset_add(&xcsf->pset, new);
319  }
320  }
321 }
322 
327 void
328 clset_init(struct Set *set)
329 {
330  set->list = NULL;
331  set->size = 0;
332  set->num = 0;
333 }
334 
339 void
341 {
342  while (xcsf->pset.num > xcsf->POP_SIZE) {
344  }
345 }
346 
355 void
356 clset_match(struct XCSF *xcsf, const double *x, const bool cover)
357 {
358 #ifdef PARALLEL_MATCH
359  // prepare for parallel processing of matching conditions
360  struct Clist *blist[xcsf->pset.size];
361  struct Clist *iter = xcsf->pset.list;
362  for (int i = 0; iter != NULL && i < xcsf->pset.size; ++i) {
363  blist[i] = iter;
364  iter = iter->next;
365  }
366  // process conditions and actions setting m flags in parallel
367  #pragma omp parallel for
368  for (int i = 0; i < xcsf->pset.size; ++i) {
369  cl_match(xcsf, blist[i]->cl, x);
370  cl_action(xcsf, blist[i]->cl, x);
371  }
372  // build match set list in series
373  for (int i = 0; i < xcsf->pset.size; ++i) {
374  if (cl_m(xcsf, blist[i]->cl)) {
375  clset_add(&xcsf->mset, blist[i]->cl);
376  }
377  }
378 #else
379  // process conditions and actions and build match set list in series
380  struct Clist *iter = xcsf->pset.list;
381  while (iter != NULL) {
382  if (cl_match(xcsf, iter->cl, x)) {
383  clset_add(&xcsf->mset, iter->cl);
384  cl_action(xcsf, iter->cl, x);
385  }
386  iter = iter->next;
387  }
388 #endif
389  // perform covering if all actions are not represented
390  if (cover && (xcsf->n_actions > 1 || xcsf->mset.size < 1)) {
391  clset_cover(xcsf, x);
392  }
393  // update statistics
394  xcsf->mset_size += (xcsf->mset.size - xcsf->mset_size) * xcsf->BETA;
395  xcsf->mfrac += (clset_mfrac(xcsf) - xcsf->mfrac) * xcsf->BETA;
396 }
397 
403 void
404 clset_action(struct XCSF *xcsf, const int action)
405 {
406  const struct Clist *iter = xcsf->mset.list;
407  while (iter != NULL) {
408  if (iter->cl->action == action) {
409  clset_add(&xcsf->aset, iter->cl);
410  }
411  iter = iter->next;
412  }
413  // update statistics
414  xcsf->aset_size += (xcsf->aset.size - xcsf->aset_size) * xcsf->BETA;
415 }
416 
422 void
423 clset_add(struct Set *set, struct Cl *c)
424 {
425  if (set->list == NULL) {
426  set->list = malloc(sizeof(struct Clist));
427  set->list->cl = c;
428  set->list->next = NULL;
429  } else {
430  struct Clist *new = malloc(sizeof(struct Clist));
431  new->cl = c;
432  new->next = set->list;
433  set->list = new;
434  }
435  ++(set->size);
436  set->num += c->num;
437 }
438 
447 void
448 clset_update(struct XCSF *xcsf, struct Set *set, const double *x,
449  const double *y, const bool cur)
450 {
451 #ifdef PARALLEL_UPDATE
452  struct Clist *blist[set->size];
453  struct Clist *iter = set->list;
454  for (int i = 0; iter != NULL && i < set->size; ++i) {
455  blist[i] = iter;
456  iter = iter->next;
457  }
458  #pragma omp parallel for
459  for (int i = 0; i < set->size; ++i) {
460  cl_update(xcsf, blist[i]->cl, x, y, set->num, cur);
461  }
462 #else
463  struct Clist *iter = set->list;
464  while (iter != NULL) {
465  cl_update(xcsf, iter->cl, x, y, set->num, cur);
466  iter = iter->next;
467  }
468 #endif
469  clset_update_fit(xcsf, set);
470  if (xcsf->SET_SUBSUMPTION) {
471  clset_subsumption(xcsf, set);
472  }
473 }
474 
479 void
480 clset_validate(struct Set *set)
481 {
482  set->size = 0;
483  set->num = 0;
484  struct Clist *prev = NULL;
485  struct Clist *iter = set->list;
486  while (iter != NULL) {
487  if (iter->cl == NULL || iter->cl->num == 0) {
488  if (prev == NULL) {
489  set->list = iter->next;
490  free(iter);
491  iter = set->list;
492  } else {
493  prev->next = iter->next;
494  free(iter);
495  iter = prev->next;
496  }
497  } else {
498  ++(set->size);
499  set->num += iter->cl->num;
500  prev = iter;
501  iter = iter->next;
502  }
503  }
504 }
505 
514 void
515 clset_print(const struct XCSF *xcsf, const struct Set *set,
516  const bool print_cond, const bool print_act, const bool print_pred)
517 {
518  char *json_str =
519  clset_json_export(xcsf, set, print_cond, print_act, print_pred);
520  printf("%s\n", json_str);
521  free(json_str);
522 }
523 
529 void
530 clset_set_times(const struct XCSF *xcsf, const struct Set *set)
531 {
532  const struct Clist *iter = set->list;
533  while (iter != NULL) {
534  iter->cl->time = xcsf->time;
535  iter = iter->next;
536  }
537 }
538 
544 double
545 clset_total_fit(const struct Set *set)
546 {
547  double sum = 0;
548  const struct Clist *iter = set->list;
549  while (iter != NULL) {
550  sum += iter->cl->fit;
551  iter = iter->next;
552  }
553  return sum;
554 }
555 
561 double
562 clset_mean_time(const struct Set *set)
563 {
564  return clset_total_time(set) / set->num;
565 }
566 
571 void
572 clset_free(struct Set *set)
573 {
574  struct Clist *iter = set->list;
575  while (iter != NULL) {
576  set->list = iter->next;
577  free(iter);
578  iter = set->list;
579  }
580  set->size = 0;
581  set->num = 0;
582 }
583 
589 void
590 clset_kill(const struct XCSF *xcsf, struct Set *set)
591 {
592  struct Clist *iter = set->list;
593  while (iter != NULL) {
594  cl_free(xcsf, iter->cl);
595  set->list = iter->next;
596  free(iter);
597  iter = set->list;
598  }
599  set->size = 0;
600  set->num = 0;
601 }
602 
609 size_t
610 clset_pset_save(const struct XCSF *xcsf, FILE *fp)
611 {
612  size_t s = 0;
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) {
617  s += cl_save(xcsf, iter->cl, fp);
618  iter = iter->next;
619  }
620  return s;
621 }
622 
627 static void
629 {
630  struct Clist *current = xcsf->pset.list;
631  struct Clist *prev = NULL;
632  struct Clist *next = NULL;
633  while (current != NULL) {
634  next = current->next;
635  current->next = prev;
636  prev = current;
637  current = next;
638  }
639  xcsf->pset.list = prev;
640 }
641 
648 size_t
649 clset_pset_load(struct XCSF *xcsf, FILE *fp)
650 {
651  size_t s = 0;
652  int size = 0;
653  int num = 0;
654  s += fread(&size, sizeof(int), 1, fp);
655  s += fread(&num, sizeof(int), 1, fp);
656  clset_init(&xcsf->pset);
657  for (int i = 0; i < size; ++i) {
658  struct Cl *c = malloc(sizeof(struct Cl));
659  s += cl_load(xcsf, c, fp);
660  clset_add(&xcsf->pset, c);
661  }
662  clset_pset_reverse(xcsf); // reverse population list for consistency
663  return s;
664 }
665 
672 double
673 clset_mean_cond_size(const struct XCSF *xcsf, const struct Set *set)
674 {
675  double sum = 0;
676  int cnt = 0;
677  const struct Clist *iter = set->list;
678  while (iter != NULL) {
679  sum += cl_cond_size(xcsf, iter->cl);
680  ++cnt;
681  iter = iter->next;
682  }
683  return sum / cnt;
684 }
685 
692 double
693 clset_mean_pred_size(const struct XCSF *xcsf, const struct Set *set)
694 {
695  double sum = 0;
696  int cnt = 0;
697  const struct Clist *iter = set->list;
698  while (iter != NULL) {
699  sum += cl_pred_size(xcsf, iter->cl);
700  ++cnt;
701  iter = iter->next;
702  }
703  return sum / cnt;
704 }
705 
712 double
713 clset_mfrac(const struct XCSF *xcsf)
714 {
715  double mfrac = 0;
716  // most general rule below E0
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) {
721  const double m = cl_mfrac(xcsf, iter->cl);
722  if (m > mfrac) {
723  mfrac = m;
724  }
725  }
726  iter = iter->next;
727  }
728  // lowest error rule
729  if (mfrac == 0) {
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) {
735  mfrac = cl_mfrac(xcsf, iter->cl);
736  error = e;
737  }
738  iter = iter->next;
739  }
740  }
741  return mfrac;
742 }
743 
753 char *
754 clset_json_export(const struct XCSF *xcsf, const struct Set *set,
755  const bool return_cond, const bool return_act,
756  const bool return_pred)
757 {
758  cJSON *json = cJSON_CreateObject();
759  cJSON *classifiers = cJSON_AddArrayToObject(json, "classifiers");
760  const struct Clist *iter = set->list;
761  while (iter != NULL) {
762  char *str = cl_json_export(xcsf, iter->cl, return_cond, return_act,
763  return_pred);
764  cJSON *classifier = cJSON_Parse(str);
765  cJSON_AddItemToArray(classifiers, classifier);
766  free(str);
767  iter = iter->next;
768  }
769  char *string = cJSON_Print(json);
770  cJSON_Delete(json);
771  return string;
772 }
773 
779 void
780 clset_json_insert_cl(struct XCSF *xcsf, const cJSON *json)
781 {
782  struct Cl *new = malloc(sizeof(struct Cl));
783  cl_json_import(xcsf, new, json);
784  clset_add(&xcsf->pset, new);
785  clset_init(&xcsf->kset);
787  clset_kill(xcsf, &xcsf->kset);
788 }
789 
795 void
796 clset_json_insert(struct XCSF *xcsf, const char *json_str)
797 {
798  cJSON *json = cJSON_Parse(json_str);
800  if (json->child != NULL && cJSON_IsArray(json->child)) {
801  cJSON *tail = json->child->child; // insert inverted for consistency
802  tail->prev = NULL; // this should have been set by cJSON!
803  while (tail->next != NULL) {
804  tail = tail->next;
805  }
806  while (tail != NULL) {
807  clset_json_insert_cl(xcsf, tail);
808  tail = tail->prev;
809  }
810  }
811  cJSON_Delete(json);
812 }
double cl_pred_size(const struct XCSF *xcsf, const struct Cl *c)
Returns the size of a classifier's prediction.
Definition: cl.c:411
double cl_mfrac(const struct XCSF *xcsf, const struct Cl *c)
Returns the fraction of observed inputs matched by a classifier.
Definition: cl.c:274
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.
Definition: cl.c:40
bool cl_m(const struct XCSF *xcsf, const struct Cl *c)
Returns whether a classifier matched the most recent input.
Definition: cl.c:290
double cl_cond_size(const struct XCSF *xcsf, const struct Cl *c)
Returns the size of a classifier's condition.
Definition: cl.c:399
void cl_rand(const struct XCSF *xcsf, struct Cl *c)
Initialises random actions, conditions and predictions.
Definition: cl.c:129
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.
Definition: cl.c:183
void cl_update_fit(const struct XCSF *xcsf, struct Cl *c, const double acc_sum, const double acc)
Updates the fitness of a classifier.
Definition: cl.c:211
int cl_action(const struct XCSF *xcsf, struct Cl *c, const double *x)
Computes the current classifier action using the input.
Definition: cl.c:304
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.
Definition: cl.c:486
bool cl_general(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Returns whether classifier c1 is more general than c2.
Definition: cl.c:347
bool cl_match(const struct XCSF *xcsf, struct Cl *c, const double *x)
Calculates whether a classifier matches an input.
Definition: cl.c:257
size_t cl_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Writes a classifier to a file.
Definition: cl.c:424
size_t cl_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Reads a classifier from a file.
Definition: cl.c:452
void cl_cover(const struct XCSF *xcsf, struct Cl *c, const double *x, const int action)
Covers the condition and action for a classifier.
Definition: cl.c:113
void cl_free(const struct XCSF *xcsf, struct Cl *c)
Frees the memory used by a classifier.
Definition: cl.c:223
double cl_acc(const struct XCSF *xcsf, const struct Cl *c)
Returns the accuracy of a classifier.
Definition: cl.c:162
void cl_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Creates a classifier from a cJSON object.
Definition: cl.c:608
double cl_del_vote(const struct XCSF *xcsf, const struct Cl *c, const double avg_fit)
Returns the deletion vote of a classifier.
Definition: cl.c:147
bool cl_subsumer(const struct XCSF *xcsf, const struct Cl *c)
Returns whether a classifier is a potential subsumer.
Definition: cl.c:331
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.
Definition: clset.c:693
void clset_pset_init(struct XCSF *xcsf)
Initialises a new population of random classifiers.
Definition: clset.c:308
static void clset_pset_del(struct XCSF *xcsf)
Deletes a single classifier from the population set.
Definition: clset.c:104
void clset_kill(const struct XCSF *xcsf, struct Set *set)
Frees the set and the classifiers.
Definition: clset.c:590
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.
Definition: clset.c:448
double clset_total_fit(const struct Set *set)
Calculates the total fitness of classifiers in the set.
Definition: clset.c:545
double clset_mean_time(const struct Set *set)
Calculates the mean time stamp of classifiers in the set.
Definition: clset.c:562
void clset_validate(struct Set *set)
Removes classifiers with 0 numerosity from the set.
Definition: clset.c:480
static void clset_subsumption(struct XCSF *xcsf, struct Set *set)
Performs set subsumption.
Definition: clset.c:230
void clset_pset_enforce_limit(struct XCSF *xcsf)
Enforces the maximum population size limit.
Definition: clset.c:340
void clset_add(struct Set *set, struct Cl *c)
Adds a classifier to the set.
Definition: clset.c:423
void clset_action(struct XCSF *xcsf, const int action)
Constructs the action set from the match set.
Definition: clset.c:404
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.
Definition: clset.c:515
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.
Definition: clset.c:65
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.
Definition: clset.c:37
double clset_mean_cond_size(const struct XCSF *xcsf, const struct Set *set)
Calculates the mean condition size of classifiers in the set.
Definition: clset.c:673
void clset_json_insert(struct XCSF *xcsf, const char *json_str)
Creates classifiers from JSON and inserts into the population.
Definition: clset.c:796
size_t clset_pset_save(const struct XCSF *xcsf, FILE *fp)
Writes the population set to a file.
Definition: clset.c:610
void clset_init(struct Set *set)
Initialises a new set.
Definition: clset.c:328
#define MAX_COVER
Maximum number of covering attempts.
Definition: clset.c:28
static void clset_cover(struct XCSF *xcsf, const double *x)
Ensures all possible actions are covered by the match set.
Definition: clset.c:159
static void clset_update_fit(const struct XCSF *xcsf, const struct Set *set)
Updates the fitness of classifiers in the set.
Definition: clset.c:205
void clset_set_times(const struct XCSF *xcsf, const struct Set *set)
Sets the time stamps for classifiers in the set.
Definition: clset.c:530
static void clset_load_pop_file(struct XCSF *xcsf)
Definition: clset.c:281
double clset_mfrac(const struct XCSF *xcsf)
Returns the fraction of inputs matched by the most general rule with error below E0....
Definition: clset.c:713
static double clset_total_time(const struct Set *set)
Calculates the total time stamps of classifiers in the set.
Definition: clset.c:269
void clset_match(struct XCSF *xcsf, const double *x, const bool cover)
Constructs the match set - forward propagates conditions and actions.
Definition: clset.c:356
void clset_free(struct Set *set)
Frees the set, but not the classifiers.
Definition: clset.c:572
static void clset_pset_reverse(struct XCSF *xcsf)
Reverses the order of the population classifier list.
Definition: clset.c:628
static bool clset_action_coverage(const struct XCSF *xcsf, bool *act_covered)
Checks whether each action is covered by the match set.
Definition: clset.c:137
void clset_json_insert_cl(struct XCSF *xcsf, const cJSON *json)
Creates a classifier from cJSON and inserts in the population set.
Definition: clset.c:780
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.
Definition: clset.c:754
size_t clset_pset_load(struct XCSF *xcsf, FILE *fp)
Reads the population set from a file.
Definition: clset.c:649
Functions operating on sets of classifiers.
Definition: __init__.py:1
Classifier data structure.
Definition: xcsf.h:45
int mtotal
Total number of times actually matched an input.
Definition: xcsf.h:62
int time
Time EA last executed in a participating set.
Definition: xcsf.h:57
double err
Error.
Definition: xcsf.h:52
int action
Current classifier action.
Definition: xcsf.h:60
int age
Total number of times match testing been performed.
Definition: xcsf.h:61
int num
Numerosity.
Definition: xcsf.h:54
int exp
Experience.
Definition: xcsf.h:55
double fit
Fitness.
Definition: xcsf.h:53
Classifier linked list.
Definition: xcsf.h:68
struct Clist * next
Pointer to the next list element.
Definition: xcsf.h:70
struct Cl * cl
Pointer to classifier data structure.
Definition: xcsf.h:69
Classifier set.
Definition: xcsf.h:76
int size
Number of macro-classifiers.
Definition: xcsf.h:78
struct Clist * list
Linked list of classifiers.
Definition: xcsf.h:77
int num
The total numerosity of classifiers.
Definition: xcsf.h:79
XCSF data structure.
Definition: xcsf.h:85
double rand_uniform(const double min, const double max)
Returns a uniform random float [min,max].
Definition: utils.c:62
void utils_json_parse_check(const cJSON *json)
Checks whether JSON parsed correctly.
Definition: utils.c:109
Utility functions for random number handling, etc.