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);
91 new->b1 = malloc(
sizeof(
double) *
xcsf->x_dim);
92 new->b2 = malloc(
sizeof(
double) *
xcsf->x_dim);
93 new->mu = malloc(
sizeof(
double) *
N_MU);
94 memcpy(new->b1, src_cond->
b1,
sizeof(
double) *
xcsf->x_dim);
95 memcpy(new->b2, src_cond->
b2,
sizeof(
double) *
xcsf->x_dim);
96 memcpy(new->mu, src_cond->
mu,
sizeof(
double) *
N_MU);
111 const double spread_max = fabs(
xcsf->cond->max -
xcsf->cond->min);
113 for (
int i = 0; i <
xcsf->x_dim; ++i) {
118 for (
int i = 0; i <
xcsf->x_dim; ++i) {
121 cond->
b1[i] = x[i] - (r1 * 0.5);
122 cond->
b2[i] = x[i] + (r2 * 0.5);
136 const double *x,
const double *y)
140 xcsf->cond->eta > 0) {
142 for (
int i = 0; i <
xcsf->x_dim; ++i) {
143 cond->
b1[i] +=
xcsf->cond->eta * (x[i] - cond->
b1[i]);
161 for (
int i = 0; i <
xcsf->x_dim; ++i) {
162 const double lb = cond->
b1[i] - cond->
b2[i];
163 const double ub = cond->
b1[i] + cond->
b2[i];
164 if (x[i] < lb || x[i] > ub) {
169 for (
int i = 0; i <
xcsf->x_dim; ++i) {
170 const double lb = fmin(cond->
b1[i], cond->
b2[i]);
171 const double ub = fmax(cond->
b1[i], cond->
b2[i]);
172 if (x[i] < lb || x[i] > ub) {
193 bool changed =
false;
195 for (
int i = 0; i <
xcsf->x_dim; ++i) {
197 const double tmp = cond1->
b1[i];
198 cond1->
b1[i] = cond2->
b1[i];
203 const double tmp = cond1->
b2[i];
204 cond1->
b2[i] = cond2->
b2[i];
222 bool changed =
false;
224 double *
b1 = cond->
b1;
225 double *
b2 = cond->
b2;
227 for (
int i = 0; i <
xcsf->x_dim; ++i) {
237 b2[i] = fmax(DBL_EPSILON,
b2[i]);
262 for (
int i = 0; i <
xcsf->x_dim; ++i) {
263 const double l1 = cond1->
b1[i] - cond1->
b2[i];
264 const double l2 = cond2->
b1[i] - cond2->
b2[i];
265 const double u1 = cond1->
b1[i] + cond1->
b2[i];
266 const double u2 = cond2->
b1[i] + cond2->
b2[i];
267 if (l1 > l2 || u1 < u2) {
272 for (
int i = 0; i <
xcsf->x_dim; ++i) {
273 const double l1 = fmin(cond1->
b1[i], cond1->
b2[i]);
274 const double l2 = fmin(cond2->
b1[i], cond2->
b2[i]);
275 const double u1 = fmax(cond1->
b1[i], cond1->
b2[i]);
276 const double u2 = fmax(cond2->
b1[i], cond2->
b2[i]);
277 if (l1 > l2 || u1 < u2) {
294 printf(
"%s\n", json_str);
323 s += fwrite(cond->
b1,
sizeof(
double),
xcsf->x_dim, fp);
324 s += fwrite(cond->
b2,
sizeof(
double),
xcsf->x_dim, fp);
325 s += fwrite(cond->
mu,
sizeof(
double),
N_MU, fp);
341 new->b1 = malloc(
sizeof(
double) *
xcsf->x_dim);
342 new->b2 = malloc(
sizeof(
double) *
xcsf->x_dim);
343 new->mu = malloc(
sizeof(
double) *
N_MU);
344 s += fread(new->b1,
sizeof(
double),
xcsf->x_dim, fp);
345 s += fread(new->b2,
sizeof(
double),
xcsf->x_dim, fp);
346 s += fread(new->mu,
sizeof(
double),
N_MU, fp);
361 cJSON *json = cJSON_CreateObject();
362 cJSON *
b1 = cJSON_CreateDoubleArray(cond->
b1,
xcsf->x_dim);
363 cJSON *
b2 = cJSON_CreateDoubleArray(cond->
b2,
xcsf->x_dim);
364 cJSON *mutation = cJSON_CreateDoubleArray(cond->
mu,
N_MU);
366 cJSON_AddStringToObject(json,
"type",
"hyperrectangle_csr");
367 cJSON_AddItemToObject(json,
"center",
b1);
368 cJSON_AddItemToObject(json,
"spread",
b2);
370 cJSON_AddStringToObject(json,
"type",
"hyperrectangle_ubr");
371 cJSON_AddItemToObject(json,
"bound1",
b1);
372 cJSON_AddItemToObject(json,
"bound2",
b2);
374 cJSON_AddItemToObject(json,
"mutation", mutation);
375 char *
string = cJSON_Print(json);
395 const char *b1_name = csr ?
"center" :
"bound1";
396 const char *b2_name = csr ?
"spread" :
"bound2";
397 const cJSON *item = cJSON_GetObjectItem(json, b1_name);
398 if (item != NULL && cJSON_IsArray(item)) {
399 if (cJSON_GetArraySize(item) ==
xcsf->x_dim) {
400 for (
int i = 0; i <
xcsf->x_dim; ++i) {
401 const cJSON *item_i = cJSON_GetArrayItem(item, i);
402 cond->
b1[i] = item_i->valuedouble;
405 printf(
"Import error: %s length mismatch\n", b1_name);
409 item = cJSON_GetObjectItem(json, b2_name);
410 if (item != NULL && cJSON_IsArray(item)) {
411 if (cJSON_GetArraySize(item) ==
xcsf->x_dim) {
412 for (
int i = 0; i <
xcsf->x_dim; ++i) {
413 const cJSON *item_i = cJSON_GetArrayItem(item, i);
414 cond->
b2[i] = item_i->valuedouble;
417 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.
static const int MU_TYPE[(1)]
Self-adaptation method for mutating hyperrectangles.
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.
char * cond_rectangle_json_export(const struct XCSF *xcsf, const struct Cl *c)
Returns a json formatted string representation of a hyperrectangle.
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.