51 for (
int i = 0; i <
xcsf->x_dim; ++i) {
52 const double d = (x[i] - cond->
center[i]) / cond->
spread[i];
68 new->center = malloc(
sizeof(
double) *
xcsf->x_dim);
69 new->spread = malloc(
sizeof(
double) *
xcsf->x_dim);
70 new->mu = malloc(
sizeof(
double) *
N_MU);
71 const double spread_max = fabs(
xcsf->cond->max -
xcsf->cond->min);
72 for (
int i = 0; i <
xcsf->x_dim; ++i) {
76 if (
xcsf->cond->sam) {
79 new->mu[0] =
xcsf->cond->p_mu;
80 new->mu[1] =
xcsf->cond->mu;
109 const struct Cl *src)
113 new->center = malloc(
sizeof(
double) *
xcsf->x_dim);
114 new->spread = malloc(
sizeof(
double) *
xcsf->x_dim);
115 new->mu = malloc(
sizeof(
double) *
N_MU);
116 memcpy(new->center, src_cond->
center,
sizeof(
double) *
xcsf->x_dim);
117 memcpy(new->spread, src_cond->
spread,
sizeof(
double) *
xcsf->x_dim);
118 memcpy(new->mu, src_cond->
mu,
sizeof(
double) *
N_MU);
133 const double spread_max = fabs(
xcsf->cond->max -
xcsf->cond->min);
134 for (
int i = 0; i <
xcsf->x_dim; ++i) {
149 const double *x,
const double *y)
152 if (
xcsf->cond->eta > 0) {
154 for (
int i = 0; i <
xcsf->x_dim; ++i) {
187 bool changed =
false;
189 for (
int i = 0; i <
xcsf->x_dim; ++i) {
191 const double tmp = cond1->
center[i];
197 const double tmp = cond1->
spread[i];
216 bool changed =
false;
221 if (
xcsf->cond->sam) {
225 for (
int i = 0; i <
xcsf->x_dim; ++i) {
258 double *temp = malloc(
sizeof(
double) *
xcsf->x_dim);
259 memcpy(temp, cond2->
center,
sizeof(
double) *
xcsf->x_dim);
260 for (
int i = 0; i <
xcsf->x_dim; ++i) {
263 temp[i] += cond2->
spread[i];
268 temp[i] -= 2 * cond2->
spread[i];
273 temp[i] = cond2->
center[i];
289 printf(
"%s\n", json_str);
318 s += fwrite(cond->
center,
sizeof(
double),
xcsf->x_dim, fp);
319 s += fwrite(cond->
spread,
sizeof(
double),
xcsf->x_dim, fp);
320 s += fwrite(cond->
mu,
sizeof(
double),
N_MU, fp);
336 new->center = malloc(
sizeof(
double) *
xcsf->x_dim);
337 new->spread = malloc(
sizeof(
double) *
xcsf->x_dim);
338 new->mu = malloc(
sizeof(
double) *
N_MU);
339 s += fread(new->center,
sizeof(
double),
xcsf->x_dim, fp);
340 s += fread(new->spread,
sizeof(
double),
xcsf->x_dim, fp);
341 s += fread(new->mu,
sizeof(
double),
N_MU, fp);
356 cJSON *json = cJSON_CreateObject();
357 cJSON_AddStringToObject(json,
"type",
"hyperellipsoid");
360 cJSON *mutation = cJSON_CreateDoubleArray(cond->
mu,
N_MU);
361 cJSON_AddItemToObject(json,
"center",
center);
362 cJSON_AddItemToObject(json,
"spread",
spread);
363 cJSON_AddItemToObject(json,
"mutation", mutation);
364 char *
string = cJSON_Print(json);
380 const cJSON *item = cJSON_GetObjectItem(json,
"center");
381 if (item != NULL && cJSON_IsArray(item)) {
382 if (cJSON_GetArraySize(item) ==
xcsf->x_dim) {
383 for (
int i = 0; i <
xcsf->x_dim; ++i) {
384 const cJSON *item_i = cJSON_GetArrayItem(item, i);
385 cond->
center[i] = item_i->valuedouble;
388 printf(
"Import error: center length mismatch\n");
392 item = cJSON_GetObjectItem(json,
"spread");
393 if (item != NULL && cJSON_IsArray(item)) {
394 if (cJSON_GetArraySize(item) ==
xcsf->x_dim) {
395 for (
int i = 0; i <
xcsf->x_dim; ++i) {
396 const cJSON *item_i = cJSON_GetArrayItem(item, i);
397 cond->
spread[i] = item_i->valuedouble;
400 printf(
"Import error: spread length mismatch\n");
void cond_ellipsoid_free(const struct XCSF *xcsf, const struct Cl *c)
Frees the memory used by a hyperellipsoid condition.
size_t cond_ellipsoid_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Reads a hyperellipsoid condition from a file.
void cond_ellipsoid_print(const struct XCSF *xcsf, const struct Cl *c)
Prints a hyperellipsoid condition.
size_t cond_ellipsoid_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Writes a hyperellipsoid condition to a file.
char * cond_ellipsoid_json_export(const struct XCSF *xcsf, const struct Cl *c)
Returns a json formatted string representation of a hyperellipsoid.
static const int MU_TYPE[2]
Self-adaptation method for mutating hyperellipsoids.
bool cond_ellipsoid_match(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Calculates whether a hyperellipsoid condition matches an input.
void cond_ellipsoid_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Creates a hyperellipsoid from a cJSON object.
#define N_MU
Number of hyperellipsoid mutation rates.
static double cond_ellipsoid_dist(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Returns the relative distance to a hyperellipsoid.
double cond_ellipsoid_size(const struct XCSF *xcsf, const struct Cl *c)
Returns the size of a hyperellipsoid condition.
void cond_ellipsoid_update(const struct XCSF *xcsf, const struct Cl *c, const double *x, const double *y)
Updates a hyperellipsoid, sliding the centers towards the mean input.
bool cond_ellipsoid_crossover(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Performs uniform crossover with two hyperellipsoid conditions.
bool cond_ellipsoid_mutate(const struct XCSF *xcsf, const struct Cl *c)
Mutates a hyperellipsoid condition with the self-adaptive rate.
bool cond_ellipsoid_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_ellipsoid_init(const struct XCSF *xcsf, struct Cl *c)
Creates and initialises a hyperellipsoid condition.
void cond_ellipsoid_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Copies a hyperellipsoid condition from one classifier to another.
void cond_ellipsoid_cover(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Generates a hyperellipsoid that matches the current input.
Hyperellipsoid condition functions.
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.
Hyperellipsoid condition data structure.
double * mu
Mutation rates.
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.