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) {
104 const struct Cl *src)
108 new->center = malloc(
sizeof(
double) *
xcsf->x_dim);
109 new->spread = malloc(
sizeof(
double) *
xcsf->x_dim);
110 new->mu = malloc(
sizeof(
double) *
N_MU);
111 memcpy(new->center, src_cond->
center,
sizeof(
double) *
xcsf->x_dim);
112 memcpy(new->spread, src_cond->
spread,
sizeof(
double) *
xcsf->x_dim);
113 memcpy(new->mu, src_cond->
mu,
sizeof(
double) *
N_MU);
128 const double spread_max = fabs(
xcsf->cond->max -
xcsf->cond->min);
129 for (
int i = 0; i <
xcsf->x_dim; ++i) {
144 const double *x,
const double *y)
147 if (
xcsf->cond->eta > 0) {
149 for (
int i = 0; i <
xcsf->x_dim; ++i) {
182 bool changed =
false;
184 for (
int i = 0; i <
xcsf->x_dim; ++i) {
186 const double tmp = cond1->
center[i];
192 const double tmp = cond1->
spread[i];
211 bool changed =
false;
216 for (
int i = 0; i <
xcsf->x_dim; ++i) {
246 double *temp = malloc(
sizeof(
double) *
xcsf->x_dim);
247 memcpy(temp, cond2->
center,
sizeof(
double) *
xcsf->x_dim);
248 for (
int i = 0; i <
xcsf->x_dim; ++i) {
251 temp[i] += cond2->
spread[i];
256 temp[i] -= 2 * cond2->
spread[i];
261 temp[i] = cond2->
center[i];
277 printf(
"%s\n", json_str);
306 s += fwrite(cond->
center,
sizeof(
double),
xcsf->x_dim, fp);
307 s += fwrite(cond->
spread,
sizeof(
double),
xcsf->x_dim, fp);
308 s += fwrite(cond->
mu,
sizeof(
double),
N_MU, fp);
324 new->center = malloc(
sizeof(
double) *
xcsf->x_dim);
325 new->spread = malloc(
sizeof(
double) *
xcsf->x_dim);
326 new->mu = malloc(
sizeof(
double) *
N_MU);
327 s += fread(new->center,
sizeof(
double),
xcsf->x_dim, fp);
328 s += fread(new->spread,
sizeof(
double),
xcsf->x_dim, fp);
329 s += fread(new->mu,
sizeof(
double),
N_MU, fp);
344 cJSON *json = cJSON_CreateObject();
345 cJSON_AddStringToObject(json,
"type",
"hyperellipsoid");
348 cJSON *mutation = cJSON_CreateDoubleArray(cond->
mu,
N_MU);
349 cJSON_AddItemToObject(json,
"center",
center);
350 cJSON_AddItemToObject(json,
"spread",
spread);
351 cJSON_AddItemToObject(json,
"mutation", mutation);
352 char *
string = cJSON_Print(json);
368 const cJSON *item = cJSON_GetObjectItem(json,
"center");
369 if (item != NULL && cJSON_IsArray(item)) {
370 if (cJSON_GetArraySize(item) ==
xcsf->x_dim) {
371 for (
int i = 0; i <
xcsf->x_dim; ++i) {
372 const cJSON *item_i = cJSON_GetArrayItem(item, i);
373 cond->
center[i] = item_i->valuedouble;
376 printf(
"Import error: center length mismatch\n");
380 item = cJSON_GetObjectItem(json,
"spread");
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->
spread[i] = item_i->valuedouble;
388 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.
static const int MU_TYPE[(1)]
Self-adaptation method for mutating hyperellipsoids.
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.
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.
char * cond_ellipsoid_json_export(const struct XCSF *xcsf, const struct Cl *c)
Returns a json formatted string representation of a hyperellipsoid.
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.