45 new->b1 = malloc(
sizeof(
double) *
xcsf->x_dim);
46 new->b2 = malloc(
sizeof(
double) *
xcsf->x_dim);
47 const double spread_max = fabs(
xcsf->cond->max -
xcsf->cond->min);
48 for (
int i = 0; i <
xcsf->x_dim; ++i) {
54 for (
int i = 0; i <
xcsf->x_dim; ++i) {
58 new->mu = malloc(
sizeof(
double) *
N_MU);
59 if (
xcsf->cond->sam) {
62 new->mu[0] =
xcsf->cond->p_mu;
63 new->mu[1] =
xcsf->cond->mu;
96 new->b1 = malloc(
sizeof(
double) *
xcsf->x_dim);
97 new->b2 = malloc(
sizeof(
double) *
xcsf->x_dim);
98 new->mu = malloc(
sizeof(
double) *
N_MU);
99 memcpy(new->b1, src_cond->
b1,
sizeof(
double) *
xcsf->x_dim);
100 memcpy(new->b2, src_cond->
b2,
sizeof(
double) *
xcsf->x_dim);
101 memcpy(new->mu, src_cond->
mu,
sizeof(
double) *
N_MU);
116 const double spread_max = fabs(
xcsf->cond->max -
xcsf->cond->min);
118 for (
int i = 0; i <
xcsf->x_dim; ++i) {
123 for (
int i = 0; i <
xcsf->x_dim; ++i) {
126 cond->
b1[i] = x[i] - (r1 * 0.5);
127 cond->
b2[i] = x[i] + (r2 * 0.5);
143 const double *x,
const double *y)
146 if (
xcsf->cond->eta > 0) {
149 for (
int i = 0; i <
xcsf->x_dim; ++i) {
150 cond->
b1[i] +=
xcsf->cond->eta * (x[i] - cond->
b1[i]);
153 for (
int i = 0; i <
xcsf->x_dim; ++i) {
154 const double center = (cond->
b1[i] + cond->
b2[i]) * 0.5;
155 const double update = (x[i] - center) * 0.5 *
xcsf->cond->eta;
156 cond->
b1[i] += update;
157 cond->
b2[i] += update;
176 for (
int i = 0; i <
xcsf->x_dim; ++i) {
177 const double lb = cond->
b1[i] - cond->
b2[i];
178 const double ub = cond->
b1[i] + cond->
b2[i];
179 if (x[i] < lb || x[i] > ub) {
184 for (
int i = 0; i <
xcsf->x_dim; ++i) {
185 const double lb = fmin(cond->
b1[i], cond->
b2[i]);
186 const double ub = fmax(cond->
b1[i], cond->
b2[i]);
187 if (x[i] < lb || x[i] > ub) {
208 bool changed =
false;
210 for (
int i = 0; i <
xcsf->x_dim; ++i) {
212 const double tmp = cond1->
b1[i];
213 cond1->
b1[i] = cond2->
b1[i];
218 const double tmp = cond1->
b2[i];
219 cond1->
b2[i] = cond2->
b2[i];
237 bool changed =
false;
239 double *
b1 = cond->
b1;
240 double *
b2 = cond->
b2;
242 if (
xcsf->cond->sam) {
246 for (
int i = 0; i <
xcsf->x_dim; ++i) {
260 b2[i] = fmax(DBL_EPSILON,
b2[i]);
287 for (
int i = 0; i <
xcsf->x_dim; ++i) {
288 const double l1 = cond1->
b1[i] - cond1->
b2[i];
289 const double l2 = cond2->
b1[i] - cond2->
b2[i];
290 const double u1 = cond1->
b1[i] + cond1->
b2[i];
291 const double u2 = cond2->
b1[i] + cond2->
b2[i];
292 if (l1 > l2 || u1 < u2) {
297 for (
int i = 0; i <
xcsf->x_dim; ++i) {
298 const double l1 = fmin(cond1->
b1[i], cond1->
b2[i]);
299 const double l2 = fmin(cond2->
b1[i], cond2->
b2[i]);
300 const double u1 = fmax(cond1->
b1[i], cond1->
b2[i]);
301 const double u2 = fmax(cond2->
b1[i], cond2->
b2[i]);
302 if (l1 > l2 || u1 < u2) {
319 printf(
"%s\n", json_str);
348 s += fwrite(cond->
b1,
sizeof(
double),
xcsf->x_dim, fp);
349 s += fwrite(cond->
b2,
sizeof(
double),
xcsf->x_dim, fp);
350 s += fwrite(cond->
mu,
sizeof(
double),
N_MU, fp);
366 new->b1 = malloc(
sizeof(
double) *
xcsf->x_dim);
367 new->b2 = malloc(
sizeof(
double) *
xcsf->x_dim);
368 new->mu = malloc(
sizeof(
double) *
N_MU);
369 s += fread(new->b1,
sizeof(
double),
xcsf->x_dim, fp);
370 s += fread(new->b2,
sizeof(
double),
xcsf->x_dim, fp);
371 s += fread(new->mu,
sizeof(
double),
N_MU, fp);
386 cJSON *json = cJSON_CreateObject();
387 cJSON *
b1 = cJSON_CreateDoubleArray(cond->
b1,
xcsf->x_dim);
388 cJSON *
b2 = cJSON_CreateDoubleArray(cond->
b2,
xcsf->x_dim);
389 cJSON *mutation = cJSON_CreateDoubleArray(cond->
mu,
N_MU);
391 cJSON_AddStringToObject(json,
"type",
"hyperrectangle_csr");
392 cJSON_AddItemToObject(json,
"center",
b1);
393 cJSON_AddItemToObject(json,
"spread",
b2);
395 cJSON_AddStringToObject(json,
"type",
"hyperrectangle_ubr");
396 cJSON_AddItemToObject(json,
"bound1",
b1);
397 cJSON_AddItemToObject(json,
"bound2",
b2);
399 cJSON_AddItemToObject(json,
"mutation", mutation);
400 char *
string = cJSON_Print(json);
420 const char *b1_name = csr ?
"center" :
"bound1";
421 const char *b2_name = csr ?
"spread" :
"bound2";
422 const cJSON *item = cJSON_GetObjectItem(json, b1_name);
423 if (item != NULL && cJSON_IsArray(item)) {
424 if (cJSON_GetArraySize(item) ==
xcsf->x_dim) {
425 for (
int i = 0; i <
xcsf->x_dim; ++i) {
426 const cJSON *item_i = cJSON_GetArrayItem(item, i);
427 cond->
b1[i] = item_i->valuedouble;
430 printf(
"Import error: %s length mismatch\n", b1_name);
434 item = cJSON_GetObjectItem(json, b2_name);
435 if (item != NULL && cJSON_IsArray(item)) {
436 if (cJSON_GetArraySize(item) ==
xcsf->x_dim) {
437 for (
int i = 0; i <
xcsf->x_dim; ++i) {
438 const cJSON *item_i = cJSON_GetArrayItem(item, i);
439 cond->
b2[i] = item_i->valuedouble;
442 printf(
"Import error: %s length mismatch\n", b2_name);
void cond_rectangle_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Copies a hyperrectangle condition from one classifier to another.
void cond_rectangle_free(const struct XCSF *xcsf, const struct Cl *c)
Frees the memory used by a hyperrectangle condition.
char * cond_rectangle_json_export(const struct XCSF *xcsf, const struct Cl *c)
Returns a json formatted string representation of a hyperrectangle.
bool cond_rectangle_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_rectangle_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Creates a hyperrectangle from a cJSON object.
static const int MU_TYPE[2]
Self-adaptation method for mutating hyperrectangles.
void cond_rectangle_print(const struct XCSF *xcsf, const struct Cl *c)
Prints a hyperrectangle condition.
void cond_rectangle_update(const struct XCSF *xcsf, const struct Cl *c, const double *x, const double *y)
Updates a hyperrectangle, sliding the centers towards the mean input.
#define N_MU
Number of hyperrectangle mutation rates.
void cond_rectangle_init(const struct XCSF *xcsf, struct Cl *c)
Creates and initialises a hyperrectangle condition.
size_t cond_rectangle_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Writes a hyperrectangle condition to a file.
bool cond_rectangle_mutate(const struct XCSF *xcsf, const struct Cl *c)
Mutates a hyperrectangle condition with the self-adaptive rate.
bool cond_rectangle_crossover(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Performs uniform crossover with two hyperrectangle conditions.
double cond_rectangle_size(const struct XCSF *xcsf, const struct Cl *c)
Returns the size of a hyperrectangle condition.
size_t cond_rectangle_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Reads a hyperrectangle condition from a file.
void cond_rectangle_cover(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Generates a hyperrectangle that matches the current input.
bool cond_rectangle_match(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Calculates whether a hyperrectangle condition matches an input.
Hyperrectangle condition functions.
#define COND_TYPE_HYPERRECTANGLE_CSR
Condition type CSR hyperrectangle.
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.
Classifier data structure.
void * cond
Condition structure.
Hyperrectangle condition data structure.
double * b2
Spreads for CSR, second bound for UBR.
double * mu
Mutation rates.
double * b1
Centers for CSR, first bound for UBR.
double rand_normal(const double mu, const double sigma)
Returns a random Gaussian with specified mean and standard deviation.
double rand_uniform(const double min, const double max)
Returns a uniform random float [min,max].
Utility functions for random number handling, etc.
static double clamp(const double a, const double min, const double max)
Returns a float clamped within the specified range.