40 const struct Cl *c2p,
struct Cl *c1,
struct Cl *c2,
44 c1->
err =
xcsf->ea->err_reduc * ((c1p->
err + c2p->
err) * 0.5);
68 const struct Set *set)
84 for (
struct Clist *iter = set->
list; iter != NULL; iter = iter->
next) {
86 candidates[choices] = iter;
114 struct Cl *c1,
const bool cmod,
const bool mmod)
116 if (!cmod && !mmod) {
120 }
else if (
xcsf->ea->subsumption) {
136 const double fit_sum)
141 double sum = iter->
cl->
fit;
144 sum += iter->
cl->
fit;
158 struct Cl *winner = NULL;
159 while (winner == NULL) {
161 while (iter != NULL) {
163 (winner == NULL || iter->
cl->
fit > winner->
fit)) {
207 struct Cl *c1p = NULL;
208 struct Cl *c2p = NULL;
211 for (
int i = 0; i * 2 <
xcsf->ea->lambda; ++i) {
213 struct Cl *c1 = malloc(
sizeof(
struct Cl));
214 struct Cl *c2 = malloc(
sizeof(
struct Cl));
258 cJSON *json = cJSON_CreateObject();
259 cJSON_AddStringToObject(json,
"select_type",
262 cJSON_AddNumberToObject(json,
"select_size",
xcsf->ea->select_size);
264 cJSON_AddNumberToObject(json,
"theta_ea",
xcsf->ea->theta);
265 cJSON_AddNumberToObject(json,
"lambda",
xcsf->ea->lambda);
266 cJSON_AddNumberToObject(json,
"p_crossover",
xcsf->ea->p_crossover);
267 cJSON_AddNumberToObject(json,
"err_reduc",
xcsf->ea->err_reduc);
268 cJSON_AddNumberToObject(json,
"fit_reduc",
xcsf->ea->fit_reduc);
269 cJSON_AddBoolToObject(json,
"subsumption",
xcsf->ea->subsumption);
270 cJSON_AddBoolToObject(json,
"pred_reset",
xcsf->ea->pred_reset);
271 char *
string = cJSON_Print(json);
284 for (cJSON *iter = json; iter != NULL; iter = iter->next) {
285 if (strncmp(iter->string,
"select_type\0", 12) == 0 &&
286 cJSON_IsString(iter)) {
289 printf(
"Invalid EA SELECT_TYPE: %s\n", iter->valuestring);
293 }
else if (strncmp(iter->string,
"select_size\0", 12) == 0 &&
294 cJSON_IsNumber(iter)) {
296 }
else if (strncmp(iter->string,
"theta_ea\0", 9) == 0 &&
297 cJSON_IsNumber(iter)) {
299 }
else if (strncmp(iter->string,
"lambda\0", 7) == 0 &&
300 cJSON_IsNumber(iter)) {
302 }
else if (strncmp(iter->string,
"p_crossover\0", 12) == 0 &&
303 cJSON_IsNumber(iter)) {
305 }
else if (strncmp(iter->string,
"err_reduc\0", 10) == 0 &&
306 cJSON_IsNumber(iter)) {
308 }
else if (strncmp(iter->string,
"fit_reduc\0", 10) == 0 &&
309 cJSON_IsNumber(iter)) {
311 }
else if (strncmp(iter->string,
"subsumption\0", 12) == 0 &&
312 cJSON_IsBool(iter)) {
313 const bool sub =
true ? iter->type == cJSON_True :
false;
315 }
else if (strncmp(iter->string,
"pred_reset\0", 11) == 0 &&
316 cJSON_IsBool(iter)) {
317 const bool reset =
true ? iter->type == cJSON_True :
false;
320 printf(
"Error importing EA parameter %s\n", iter->string);
336 s += fwrite(&
xcsf->ea->select_type,
sizeof(
int), 1, fp);
337 s += fwrite(&
xcsf->ea->select_size,
sizeof(
double), 1, fp);
338 s += fwrite(&
xcsf->ea->theta,
sizeof(
double), 1, fp);
339 s += fwrite(&
xcsf->ea->lambda,
sizeof(
int), 1, fp);
340 s += fwrite(&
xcsf->ea->p_crossover,
sizeof(
double), 1, fp);
341 s += fwrite(&
xcsf->ea->err_reduc,
sizeof(
double), 1, fp);
342 s += fwrite(&
xcsf->ea->fit_reduc,
sizeof(
double), 1, fp);
343 s += fwrite(&
xcsf->ea->subsumption,
sizeof(
bool), 1, fp);
344 s += fwrite(&
xcsf->ea->pred_reset,
sizeof(
bool), 1, fp);
358 s += fread(&
xcsf->ea->select_type,
sizeof(
int), 1, fp);
359 s += fread(&
xcsf->ea->select_size,
sizeof(
double), 1, fp);
360 s += fread(&
xcsf->ea->theta,
sizeof(
double), 1, fp);
361 s += fread(&
xcsf->ea->lambda,
sizeof(
int), 1, fp);
362 s += fread(&
xcsf->ea->p_crossover,
sizeof(
double), 1, fp);
363 s += fread(&
xcsf->ea->err_reduc,
sizeof(
double), 1, fp);
364 s += fread(&
xcsf->ea->fit_reduc,
sizeof(
double), 1, fp);
365 s += fread(&
xcsf->ea->subsumption,
sizeof(
bool), 1, fp);
366 s += fread(&
xcsf->ea->pred_reset,
sizeof(
bool), 1, fp);
384 printf(
"ea_type_as_string(): invalid type: %d\n", type);
410 if (a < 0 || a > 1) {
411 return "Invalid EA SELECT_SIZE. Range: [0,1]";
413 xcsf->ea->select_size = a;
421 return "EA THETA must be >= 0";
430 if (a < 0 || a > 1) {
431 return "Invalid EA P_CROSSOVER. Range: [0,1]";
433 xcsf->ea->p_crossover = a;
441 return "EA LAMBDA must be >= 2";
443 xcsf->ea->lambda = a;
450 if (a < 0 || a > 1) {
451 return "Invalid EA ERR_REDUC. Range: [0,1]";
453 xcsf->ea->err_reduc = a;
460 if (a < 0 || a > 1) {
461 return "Invalid EA FIT_REDUC. Range: [0,1]";
463 xcsf->ea->fit_reduc = a;
470 xcsf->ea->subsumption = a;
477 xcsf->ea->pred_reset = a;
485 xcsf->ea->select_type = a;
496 xcsf->ea->select_type = type;
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_general(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Returns whether classifier c1 is more general than c2.
bool cl_mutate(const struct XCSF *xcsf, const struct Cl *c)
Performs classifier mutation.
void cl_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Copies condition, action, and prediction structures.
bool cl_crossover(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Performs classifier crossover.
void cl_free(const struct XCSF *xcsf, struct Cl *c)
Frees the memory used by 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_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_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_set_times(const struct XCSF *xcsf, const struct Set *set)
Sets the time stamps for classifiers in the set.
Functions operating on sets of classifiers.
const char * ea_param_set_subsumption(struct XCSF *xcsf, const bool a)
static struct Cl * ea_select_tournament(const struct XCSF *xcsf, const struct Set *set)
Selects a classifier from the set via tournament.
void ea(struct XCSF *xcsf, const struct Set *set)
Executes the evolutionary algorithm (EA).
static void ea_add(struct XCSF *xcsf, const struct Set *set, struct Cl *c1p, struct Cl *c2p, struct Cl *c1, const bool cmod, const bool mmod)
Adds offspring to the population.
void ea_param_defaults(struct XCSF *xcsf)
Initialises default evolutionary algorithm parameters.
const char * ea_param_set_lambda(struct XCSF *xcsf, const int a)
const char * ea_param_set_theta(struct XCSF *xcsf, const double a)
const char * ea_type_as_string(const int type)
Returns a string representation of an EA select type from an integer.
const char * ea_param_set_err_reduc(struct XCSF *xcsf, const double a)
char * ea_param_json_export(const struct XCSF *xcsf)
Returns a json formatted string representation of the EA parameters.
const char * ea_param_set_fit_reduc(struct XCSF *xcsf, const double a)
static void ea_select(const struct XCSF *xcsf, const struct Set *set, struct Cl **c1p, struct Cl **c2p)
Selects two parents.
const char * ea_param_set_pred_reset(struct XCSF *xcsf, const bool a)
size_t ea_param_load(struct XCSF *xcsf, FILE *fp)
Loads evolutionary algorithm parameters.
int ea_type_as_int(const char *type)
Returns the integer representation of an EA selection type.
const char * ea_param_set_p_crossover(struct XCSF *xcsf, const double a)
static void ea_init_offspring(const struct XCSF *xcsf, const struct Cl *c1p, const struct Cl *c2p, struct Cl *c1, struct Cl *c2, const bool cmod)
Initialises offspring error and fitness.
int ea_param_set_type_string(struct XCSF *xcsf, const char *a)
static void ea_subsume(struct XCSF *xcsf, struct Cl *c, struct Cl *c1p, struct Cl *c2p, const struct Set *set)
Performs evolutionary algorithm subsumption.
size_t ea_param_save(const struct XCSF *xcsf, FILE *fp)
Saves evolutionary algorithm parameters.
int ea_param_set_select_type(struct XCSF *xcsf, const int a)
void ea_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the EA parameters from a cJSON object.
static struct Cl * ea_select_rw(const struct XCSF *xcsf, const struct Set *set, const double fit_sum)
Selects a classifier from the set via roulette wheel.
const char * ea_param_set_select_size(struct XCSF *xcsf, const double a)
Evolutionary algorithm functions.
#define EA_SELECT_TOURNAMENT
Tournament parental selection.
#define EA_STRING_TOURNAMENT
Tournament.
#define EA_SELECT_OPTIONS
Valid EA types.
#define EA_STRING_ROULETTE
Roulette.
#define EA_SELECT_INVALID
Error code for invalid selection.
#define EA_SELECT_ROULETTE
Roulette wheel parental selection.
Classifier data structure.
int time
Time EA last executed in a participating set.
double size
Average participated set size.
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 rand_uniform_int(const int min, const int max)
Returns a uniform random integer [min,max] not inclusive of max.
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 catch_error(const char *ret)
Catches parameter value errors.