XCSF 1.5.1
XCSF learning classifier system
Loading...
Searching...
No Matches
condition.c
Go to the documentation of this file.
1/*
2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation, either version 3 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 */
15
24#include "condition.h"
25#include "cond_dgp.h"
26#include "cond_dummy.h"
27#include "cond_ellipsoid.h"
28#include "cond_gp.h"
29#include "cond_neural.h"
30#include "cond_rectangle.h"
31#include "cond_ternary.h"
32#include "rule_dgp.h"
33#include "rule_neural.h"
34
40void
41condition_set(const struct XCSF *xcsf, struct Cl *c)
42{
43 switch (xcsf->cond->type) {
44 case COND_TYPE_DUMMY:
46 break;
50 break;
53 break;
56 break;
57 case COND_TYPE_GP:
59 break;
60 case COND_TYPE_DGP:
62 break;
65 break;
66 case RULE_TYPE_DGP:
69 break;
73 break;
74 default:
75 printf("Invalid condition type specified: %d\n", xcsf->cond->type);
76 exit(EXIT_FAILURE);
77 }
78}
79
85const char *
87{
88 switch (type) {
89 case COND_TYPE_DUMMY:
90 return COND_STRING_DUMMY;
98 return COND_STRING_NEURAL;
99 case COND_TYPE_GP:
100 return COND_STRING_GP;
101 case COND_TYPE_DGP:
102 return COND_STRING_DGP;
104 return COND_STRING_TERNARY;
105 case RULE_TYPE_DGP:
107 case RULE_TYPE_NEURAL:
111 default:
112 printf("condition_type_as_string(): invalid type: %d\n", type);
113 exit(EXIT_FAILURE);
114 }
115}
116
122int
123condition_type_as_int(const char *type)
124{
125 if (strncmp(type, COND_STRING_DUMMY, 6) == 0) {
126 return COND_TYPE_DUMMY;
127 }
128 if (strncmp(type, COND_STRING_HYPERRECTANGLE_CSR, 19) == 0) {
130 }
131 if (strncmp(type, COND_STRING_HYPERRECTANGLE_UBR, 19) == 0) {
133 }
134 if (strncmp(type, COND_STRING_HYPERELLIPSOID, 15) == 0) {
136 }
137 if (strncmp(type, COND_STRING_NEURAL, 7) == 0) {
138 return COND_TYPE_NEURAL;
139 }
140 if (strncmp(type, COND_STRING_GP, 8) == 0) {
141 return COND_TYPE_GP;
142 }
143 if (strncmp(type, COND_STRING_DGP, 4) == 0) {
144 return COND_TYPE_DGP;
145 }
146 if (strncmp(type, COND_STRING_TERNARY, 8) == 0) {
147 return COND_TYPE_TERNARY;
148 }
149 if (strncmp(type, COND_STRING_RULE_DGP, 9) == 0) {
150 return RULE_TYPE_DGP;
151 }
152 if (strncmp(type, COND_STRING_RULE_NEURAL, 12) == 0) {
153 return RULE_TYPE_NEURAL;
154 }
155 if (strncmp(type, COND_STRING_RULE_NETWORK, 13) == 0) {
156 return RULE_TYPE_NETWORK;
157 }
158 return COND_TYPE_INVALID;
159}
160
165void
181
187static char *
189{
190 const struct ArgsCond *cond = xcsf->cond;
191 cJSON *json = cJSON_CreateObject();
192 cJSON_AddNumberToObject(json, "eta", cond->eta);
193 cJSON_AddNumberToObject(json, "min", cond->min);
194 cJSON_AddNumberToObject(json, "max", cond->max);
195 cJSON_AddNumberToObject(json, "spread_min", cond->spread_min);
196 cJSON_AddNumberToObject(json, "p_mu", cond->p_mu);
197 cJSON_AddNumberToObject(json, "mu", cond->mu);
198 cJSON_AddBoolToObject(json, "sam", cond->sam);
199 char *string = cJSON_Print(json);
200 cJSON_Delete(json);
201 return string;
202}
203
210static char *
212{
213 for (cJSON *iter = json; iter != NULL; iter = iter->next) {
214 if (strncmp(iter->string, "eta\0", 4) == 0 && cJSON_IsNumber(iter)) {
215 cond_param_set_eta(xcsf, iter->valuedouble);
216 } else if (strncmp(iter->string, "min\0", 4) == 0 &&
217 cJSON_IsNumber(iter)) {
218 cond_param_set_min(xcsf, iter->valuedouble);
219 } else if (strncmp(iter->string, "max\0", 4) == 0 &&
220 cJSON_IsNumber(iter)) {
221 cond_param_set_max(xcsf, iter->valuedouble);
222 } else if (strncmp(iter->string, "spread_min\0", 11) == 0 &&
223 cJSON_IsNumber(iter)) {
224 cond_param_set_spread_min(xcsf, iter->valuedouble);
225 } else if (strncmp(iter->string, "p_mu\0", 5) == 0 &&
226 cJSON_IsNumber(iter)) {
227 cond_param_set_p_mu(xcsf, iter->valuedouble);
228 } else if (strncmp(iter->string, "mu\0", 3) == 0 &&
229 cJSON_IsNumber(iter)) {
230 cond_param_set_mu(xcsf, iter->valuedouble);
231 } else if (strncmp(iter->string, "sam\0", 4) == 0 &&
232 cJSON_IsBool(iter)) {
233 const bool sam = (iter->type == cJSON_True);
235 } else {
236 return iter->string;
237 }
238 }
239 return NULL;
240}
241
247char *
249{
250 const struct ArgsCond *cond = xcsf->cond;
251 cJSON *json = cJSON_CreateObject();
252 cJSON_AddStringToObject(json, "type", condition_type_as_string(cond->type));
253 char *json_str = NULL;
254 switch (cond->type) {
257 break;
262 break;
263 case COND_TYPE_GP:
265 break;
266 case COND_TYPE_DGP:
267 case RULE_TYPE_DGP:
269 break;
270 case COND_TYPE_NEURAL:
271 case RULE_TYPE_NEURAL:
273 json_str = layer_args_json_export(xcsf->cond->largs);
274 break;
275 default:
276 break;
277 }
278 if (json_str != NULL) {
279 cJSON *params = cJSON_Parse(json_str);
280 if (params != NULL) {
281 cJSON_AddItemToObject(json, "args", params);
282 }
283 free(json_str);
284 }
285 char *string = cJSON_Print(json);
286 cJSON_Delete(json);
287 return string;
288}
289
296char *
297cond_param_json_import(struct XCSF *xcsf, cJSON *json)
298{
299 char *ret = NULL;
300 switch (xcsf->cond->type) {
302 ret = cond_ternary_param_json_import(xcsf, json->child);
303 break;
307 ret = cond_param_json_import_csr(xcsf, json->child);
308 break;
309 case COND_TYPE_GP:
310 ret = cond_gp_param_json_import(xcsf, json->child);
311 break;
312 case COND_TYPE_DGP:
313 case RULE_TYPE_DGP:
314 ret = cond_dgp_param_json_import(xcsf, json->child);
315 break;
316 case COND_TYPE_NEURAL:
317 case RULE_TYPE_NEURAL:
319 ret = cond_neural_param_json_import(xcsf, json->child);
320 break;
321 default:
322 printf("cond_param_json_import(): unknown type.\n");
323 exit(EXIT_FAILURE);
324 }
325 return ret;
326}
327
334size_t
335cond_param_save(const struct XCSF *xcsf, FILE *fp)
336{
337 const struct ArgsCond *cond = xcsf->cond;
338 size_t s = 0;
339 s += fwrite(&cond->type, sizeof(int), 1, fp);
340 s += fwrite(&cond->eta, sizeof(double), 1, fp);
341 s += fwrite(&cond->min, sizeof(double), 1, fp);
342 s += fwrite(&cond->max, sizeof(double), 1, fp);
343 s += fwrite(&cond->spread_min, sizeof(double), 1, fp);
344 s += fwrite(&cond->p_dontcare, sizeof(double), 1, fp);
345 s += fwrite(&cond->p_mu, sizeof(double), 1, fp);
346 s += fwrite(&cond->mu, sizeof(double), 1, fp);
347 s += fwrite(&cond->sam, sizeof(bool), 1, fp);
348 s += fwrite(&cond->bits, sizeof(int), 1, fp);
349 s += graph_args_save(cond->dargs, fp);
350 s += tree_args_save(cond->targs, fp);
351 s += layer_args_save(cond->largs, fp);
352 return s;
353}
354
361size_t
362cond_param_load(struct XCSF *xcsf, FILE *fp)
363{
364 struct ArgsCond *cond = xcsf->cond;
365 size_t s = 0;
366 s += fread(&cond->type, sizeof(int), 1, fp);
367 s += fread(&cond->eta, sizeof(double), 1, fp);
368 s += fread(&cond->min, sizeof(double), 1, fp);
369 s += fread(&cond->max, sizeof(double), 1, fp);
370 s += fread(&cond->spread_min, sizeof(double), 1, fp);
371 s += fread(&cond->p_dontcare, sizeof(double), 1, fp);
372 s += fread(&cond->p_mu, sizeof(double), 1, fp);
373 s += fread(&cond->mu, sizeof(double), 1, fp);
374 s += fread(&cond->sam, sizeof(bool), 1, fp);
375 s += fread(&cond->bits, sizeof(int), 1, fp);
376 s += graph_args_load(cond->dargs, fp);
377 s += tree_args_load(cond->targs, fp);
378 s += layer_args_load(&cond->largs, fp);
379 return s;
380}
381
386void
388{
389 tree_args_free(xcsf->cond->targs);
390 free(xcsf->cond->targs);
391 free(xcsf->cond->dargs);
392 xcsf->cond->targs = NULL;
393 xcsf->cond->dargs = NULL;
394 layer_args_free(&xcsf->cond->largs);
395}
396
397/* parameter setters */
398
399void
400cond_param_set_eta(struct XCSF *xcsf, const double a)
401{
402 if (a < 0) {
403 printf("Warning: tried to set COND ETA too small\n");
404 xcsf->cond->eta = 0;
405 } else if (a > 1) {
406 printf("Warning: tried to set COND ETA too large\n");
407 xcsf->cond->eta = 1;
408 } else {
409 xcsf->cond->eta = a;
410 }
411}
412
413void
414cond_param_set_min(struct XCSF *xcsf, const double a)
415{
416 xcsf->cond->min = a;
417}
418
419void
420cond_param_set_max(struct XCSF *xcsf, const double a)
421{
422 xcsf->cond->max = a;
423}
424
425void
426cond_param_set_p_dontcare(struct XCSF *xcsf, const double a)
427{
428 if (a < 0) {
429 printf("Warning: tried to set COND P_DONTCARE too small\n");
430 xcsf->cond->p_dontcare = 0;
431 } else if (a > 1) {
432 printf("Warning: tried to set COND P_DONTCARE too large\n");
433 xcsf->cond->p_dontcare = 1;
434 } else {
435 xcsf->cond->p_dontcare = a;
436 }
437}
438
439void
440cond_param_set_mu(struct XCSF *xcsf, const double a)
441{
442 if (a < 0) {
443 printf("Warning: tried to set COND MU too small\n");
444 xcsf->cond->mu = 0;
445 } else if (a > 1) {
446 printf("Warning: tried to set COND MU too large\n");
447 xcsf->cond->mu = 1;
448 } else {
449 xcsf->cond->mu = a;
450 }
451}
452
453void
454cond_param_set_p_mu(struct XCSF *xcsf, const double a)
455{
456 if (a < 0) {
457 printf("Warning: tried to set COND P_MU too small\n");
458 xcsf->cond->p_mu = 0;
459 } else if (a > 1) {
460 printf("Warning: tried to set COND P_MU too large\n");
461 xcsf->cond->p_mu = 1;
462 } else {
463 xcsf->cond->p_mu = a;
464 }
465}
466
467void
468cond_param_set_sam(struct XCSF *xcsf, const bool a)
469{
470 xcsf->cond->sam = a;
471}
472
473void
474cond_param_set_spread_min(struct XCSF *xcsf, const double a)
475{
476 if (a < 0) {
477 printf("Warning: tried to set COND SPREAD_MIN too small\n");
478 xcsf->cond->spread_min = 0;
479 } else {
480 xcsf->cond->spread_min = a;
481 }
482}
483
484void
485cond_param_set_bits(struct XCSF *xcsf, const int a)
486{
487 if (a < 1) {
488 printf("Warning: tried to set COND BITS too small\n");
489 xcsf->cond->bits = 1;
490 } else {
491 xcsf->cond->bits = a;
492 }
493}
494
495int
496cond_param_set_type_string(struct XCSF *xcsf, const char *a)
497{
498 const int type = condition_type_as_int(a);
499 if (type != COND_TYPE_INVALID) {
500 xcsf->cond->type = type;
501 }
502 return type;
503}
504
505void
506cond_param_set_type(struct XCSF *xcsf, const int a)
507{
508 if (a < 0) {
509 printf("Warning: tried to set COND TYPE too small\n");
510 xcsf->cond->type = 0;
511 } else {
512 xcsf->cond->type = a;
513 }
514}
char * cond_dgp_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the DGP parameters from a cJSON object.
Definition cond_dgp.c:290
char * cond_dgp_param_json_export(const struct XCSF *xcsf)
Returns a json formatted string of the DGP parameters.
Definition cond_dgp.c:278
void cond_dgp_param_defaults(struct XCSF *xcsf)
Initialises default DGP condition parameters.
Definition cond_dgp.c:300
Dynamical GP graph condition functions.
static struct CondVtbl const cond_dgp_vtbl
Dynamical GP graph condition implemented functions.
Definition cond_dgp.h:97
Always-matching dummy condition functions.
static struct CondVtbl const cond_dummy_vtbl
Dummy condition implemented functions.
Definition cond_dummy.h:81
Hyperellipsoid condition functions.
static struct CondVtbl const cond_ellipsoid_vtbl
Hyperellipsoid condition implemented functions.
void cond_gp_param_defaults(struct XCSF *xcsf)
Initialises default tree GP condition parameters.
Definition cond_gp.c:286
char * cond_gp_param_json_export(const struct XCSF *xcsf)
Returns a json formatted string of the tree GP parameters.
Definition cond_gp.c:262
char * cond_gp_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the tree GP parameters from a cJSON object.
Definition cond_gp.c:274
Tree GP condition functions.
static struct CondVtbl const cond_gp_vtbl
Tree GP condition implemented functions.
Definition cond_gp.h:97
void cond_neural_param_defaults(struct XCSF *xcsf)
Initialises default neural condition parameters.
char * cond_neural_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the neural network parameters from a cJSON object.
Multi-layer perceptron neural network condition functions.
static struct CondVtbl const cond_neural_vtbl
Multi-layer perceptron neural network condition implemented functions.
Hyperrectangle condition functions.
static struct CondVtbl const cond_rectangle_vtbl
Hyperrectangle condition implemented functions.
char * cond_ternary_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the ternary parameters from a cJSON object.
char * cond_ternary_param_json_export(const struct XCSF *xcsf)
Returns a json formatted string of the ternary parameters.
void cond_ternary_param_defaults(struct XCSF *xcsf)
Initialises default ternary condition parameters.
Ternary condition functions.
static struct CondVtbl const cond_ternary_vtbl
Ternary condition implemented functions.
void condition_set(const struct XCSF *xcsf, struct Cl *c)
Sets a classifier's condition functions to the implementations.
Definition condition.c:41
void cond_param_set_type(struct XCSF *xcsf, const int a)
Definition condition.c:506
static char * cond_param_json_import_csr(struct XCSF *xcsf, cJSON *json)
Sets the center-spread parameters from a cJSON object.
Definition condition.c:211
void cond_param_set_spread_min(struct XCSF *xcsf, const double a)
Definition condition.c:474
size_t cond_param_load(struct XCSF *xcsf, FILE *fp)
Loads condition parameters.
Definition condition.c:362
void cond_param_set_sam(struct XCSF *xcsf, const bool a)
Definition condition.c:468
int condition_type_as_int(const char *type)
Returns the integer representation of a condition type given a name.
Definition condition.c:123
void cond_param_set_bits(struct XCSF *xcsf, const int a)
Definition condition.c:485
void cond_param_set_min(struct XCSF *xcsf, const double a)
Definition condition.c:414
size_t cond_param_save(const struct XCSF *xcsf, FILE *fp)
Saves condition parameters.
Definition condition.c:335
void cond_param_set_p_dontcare(struct XCSF *xcsf, const double a)
Definition condition.c:426
void cond_param_set_max(struct XCSF *xcsf, const double a)
Definition condition.c:420
void cond_param_set_p_mu(struct XCSF *xcsf, const double a)
Definition condition.c:454
static char * cond_param_json_export_csr(const struct XCSF *xcsf)
Returns a json formatted string of the center-spread parameters.
Definition condition.c:188
void cond_param_set_mu(struct XCSF *xcsf, const double a)
Definition condition.c:440
int cond_param_set_type_string(struct XCSF *xcsf, const char *a)
Definition condition.c:496
char * cond_param_json_export(const struct XCSF *xcsf)
Returns a json formatted string of the condition parameters.
Definition condition.c:248
void cond_param_defaults(struct XCSF *xcsf)
Initialises default condition parameters.
Definition condition.c:166
const char * condition_type_as_string(const int type)
Returns a string representation of a condition type from an integer.
Definition condition.c:86
void cond_param_free(struct XCSF *xcsf)
Frees condition parameters.
Definition condition.c:387
void cond_param_set_eta(struct XCSF *xcsf, const double a)
Definition condition.c:400
char * cond_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the condition parameters from a cJSON object.
Definition condition.c:297
Interface for classifier conditions.
#define COND_STRING_NEURAL
Neural.
Definition condition.h:45
#define COND_STRING_RULE_NETWORK
Rule network.
Definition condition.h:51
#define COND_STRING_HYPERRECTANGLE_CSR
CSR.
Definition condition.h:42
#define COND_TYPE_TERNARY
Condition type ternary.
Definition condition.h:36
#define COND_TYPE_DGP
Condition type DGP.
Definition condition.h:35
#define COND_TYPE_INVALID
Error code for invalid condition.
Definition condition.h:28
#define COND_STRING_RULE_NEURAL
Rule neural.
Definition condition.h:50
#define COND_TYPE_HYPERRECTANGLE_UBR
Condition type UBR hyperrectangle.
Definition condition.h:31
#define COND_TYPE_HYPERELLIPSOID
Condition type hyperellipsoid.
Definition condition.h:32
#define COND_TYPE_DUMMY
Condition type dummy.
Definition condition.h:29
#define RULE_TYPE_NEURAL
Condition type and action type neural.
Definition condition.h:38
#define RULE_TYPE_DGP
Condition type and action type DGP.
Definition condition.h:37
#define COND_STRING_TERNARY
Ternary.
Definition condition.h:48
#define COND_STRING_GP
Tree GP.
Definition condition.h:46
#define COND_STRING_RULE_DGP
Rule DGP.
Definition condition.h:49
#define RULE_TYPE_NETWORK
Condition type and prediction type neural.
Definition condition.h:39
#define COND_TYPE_HYPERRECTANGLE_CSR
Condition type CSR hyperrectangle.
Definition condition.h:30
#define COND_STRING_DUMMY
Dummy.
Definition condition.h:41
#define COND_STRING_HYPERELLIPSOID
Hyperellipsoid.
Definition condition.h:44
#define COND_STRING_DGP
DGP.
Definition condition.h:47
#define COND_TYPE_GP
Condition type tree GP.
Definition condition.h:34
#define COND_STRING_HYPERRECTANGLE_UBR
UBR.
Definition condition.h:43
#define COND_TYPE_NEURAL
Condition type neural network.
Definition condition.h:33
size_t graph_args_save(const struct ArgsDGP *args, FILE *fp)
Saves DGP parameters.
Definition dgp.c:687
size_t graph_args_load(struct ArgsDGP *args, FILE *fp)
Loads DGP parameters.
Definition dgp.c:705
void tree_args_free(struct ArgsGPTree *args)
Frees memory used by GP tree parameters.
Definition gp.c:438
size_t tree_args_load(struct ArgsGPTree *args, FILE *fp)
Loads Tree GP parameters.
Definition gp.c:521
size_t tree_args_save(const struct ArgsGPTree *args, FILE *fp)
Saves Tree GP parameters.
Definition gp.c:501
size_t layer_args_load(struct ArgsLayer **largs, FILE *fp)
Loads neural network layer parameters.
void layer_args_free(struct ArgsLayer **largs)
Frees memory used by a list of layer parameters and points to NULL.
size_t layer_args_save(const struct ArgsLayer *args, FILE *fp)
Saves neural network layer parameters.
char * layer_args_json_export(struct ArgsLayer *args)
Returns a json formatted string of the neural layer parameters.
Dynamical GP graph rule (condition + action) functions.
static struct ActVtbl const rule_dgp_act_vtbl
Dynamical GP rule action implemented functions.
Definition rule_dgp.h:157
static struct CondVtbl const rule_dgp_cond_vtbl
Dynamical GP rule condition implemented functions.
Definition rule_dgp.h:94
Neural network rule (condition + action) functions.
static struct ActVtbl const rule_neural_act_vtbl
Neural network rule action implemented functions.
static struct CondVtbl const rule_neural_cond_vtbl
Neural network rule condition implemented functions.
Definition rule_neural.h:93
Parameters for initialising and operating conditions.
Definition condition.h:60
double p_mu
Probability of mutation occurring.
Definition condition.h:67
double min
Minimum value expected from inputs.
Definition condition.h:64
double eta
Gradient descent rate.
Definition condition.h:62
double mu
Mutation rate.
Definition condition.h:68
double p_dontcare
Don't care probability.
Definition condition.h:65
struct ArgsDGP * dargs
DGP parameters.
Definition condition.h:72
struct ArgsGPTree * targs
Tree GP parameters.
Definition condition.h:73
int type
Classifier condition type: hyperrectangles, etc.
Definition condition.h:61
bool sam
Whether to self-adapt p_mu and mu.
Definition condition.h:69
int bits
Bits per float to binarise inputs.
Definition condition.h:70
double spread_min
Minimum initial spread.
Definition condition.h:66
struct ArgsLayer * largs
Linked-list of layer parameters.
Definition condition.h:71
double max
Maximum value expected from inputs.
Definition condition.h:63
Classifier data structure.
Definition xcsf.h:45
struct ActVtbl const * act_vptr
Functions acting on actions.
Definition xcsf.h:48
struct CondVtbl const * cond_vptr
Functions acting on conditions.
Definition xcsf.h:46
XCSF data structure.
Definition xcsf.h:85