XCSF  1.4.7
XCSF learning classifier system
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 "cond_dgp.h"
25 #include "cond_dummy.h"
26 #include "cond_ellipsoid.h"
27 #include "cond_gp.h"
28 #include "cond_neural.h"
29 #include "cond_rectangle.h"
30 #include "cond_ternary.h"
31 #include "rule_dgp.h"
32 #include "rule_neural.h"
33 #include "utils.h"
34 
40 void
41 condition_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;
54  case COND_TYPE_NEURAL:
56  break;
57  case COND_TYPE_GP:
58  c->cond_vptr = &cond_gp_vtbl;
59  break;
60  case COND_TYPE_DGP:
62  break;
63  case COND_TYPE_TERNARY:
65  break;
66  case RULE_TYPE_DGP:
69  break;
70  case RULE_TYPE_NEURAL:
73  break;
74  default:
75  printf("Invalid condition type specified: %d\n", xcsf->cond->type);
76  exit(EXIT_FAILURE);
77  }
78 }
79 
85 const char *
86 condition_type_as_string(const int type)
87 {
88  switch (type) {
89  case COND_TYPE_DUMMY:
90  return COND_STRING_DUMMY;
97  case COND_TYPE_NEURAL:
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;
103  case COND_TYPE_TERNARY:
104  return COND_STRING_TERNARY;
105  case RULE_TYPE_DGP:
106  return COND_STRING_RULE_DGP;
107  case RULE_TYPE_NEURAL:
109  case RULE_TYPE_NETWORK:
111  default:
112  printf("condition_type_as_string(): invalid type: %d\n", type);
113  exit(EXIT_FAILURE);
114  }
115 }
116 
122 int
123 condition_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 
165 void
167 {
177 }
178 
184 static char *
186 {
187  const struct ArgsCond *cond = xcsf->cond;
188  cJSON *json = cJSON_CreateObject();
189  cJSON_AddNumberToObject(json, "eta", cond->eta);
190  cJSON_AddNumberToObject(json, "min", cond->min);
191  cJSON_AddNumberToObject(json, "max", cond->max);
192  cJSON_AddNumberToObject(json, "spread_min", cond->spread_min);
193  char *string = cJSON_Print(json);
194  cJSON_Delete(json);
195  return string;
196 }
197 
204 static char *
205 cond_param_json_import_csr(struct XCSF *xcsf, cJSON *json)
206 {
207  for (cJSON *iter = json; iter != NULL; iter = iter->next) {
208  if (strncmp(iter->string, "eta\0", 4) == 0 && cJSON_IsNumber(iter)) {
209  cond_param_set_eta(xcsf, iter->valuedouble);
210  } else if (strncmp(iter->string, "min\0", 4) == 0 &&
211  cJSON_IsNumber(iter)) {
212  cond_param_set_min(xcsf, iter->valuedouble);
213  } else if (strncmp(iter->string, "max\0", 4) == 0 &&
214  cJSON_IsNumber(iter)) {
215  cond_param_set_max(xcsf, iter->valuedouble);
216  } else if (strncmp(iter->string, "spread_min\0", 11) == 0 &&
217  cJSON_IsNumber(iter)) {
218  cond_param_set_spread_min(xcsf, iter->valuedouble);
219  } else {
220  return iter->string;
221  }
222  }
223  return NULL;
224 }
225 
231 char *
233 {
234  const struct ArgsCond *cond = xcsf->cond;
235  cJSON *json = cJSON_CreateObject();
236  cJSON_AddStringToObject(json, "type", condition_type_as_string(cond->type));
237  char *json_str = NULL;
238  switch (cond->type) {
239  case COND_TYPE_TERNARY:
241  break;
245  json_str = cond_param_json_export_csr(xcsf);
246  break;
247  case COND_TYPE_GP:
248  json_str = cond_gp_param_json_export(xcsf);
249  break;
250  case COND_TYPE_DGP:
251  case RULE_TYPE_DGP:
252  json_str = cond_dgp_param_json_export(xcsf);
253  break;
254  case COND_TYPE_NEURAL:
255  case RULE_TYPE_NEURAL:
256  case RULE_TYPE_NETWORK:
257  json_str = layer_args_json_export(xcsf->cond->largs);
258  break;
259  default:
260  break;
261  }
262  if (json_str != NULL) {
263  cJSON *params = cJSON_Parse(json_str);
264  if (params != NULL) {
265  cJSON_AddItemToObject(json, "args", params);
266  }
267  free(json_str);
268  }
269  char *string = cJSON_Print(json);
270  cJSON_Delete(json);
271  return string;
272 }
273 
280 char *
281 cond_param_json_import(struct XCSF *xcsf, cJSON *json)
282 {
283  char *ret = NULL;
284  switch (xcsf->cond->type) {
285  case COND_TYPE_TERNARY:
286  ret = cond_ternary_param_json_import(xcsf, json->child);
287  break;
291  ret = cond_param_json_import_csr(xcsf, json->child);
292  break;
293  case COND_TYPE_GP:
294  ret = cond_gp_param_json_import(xcsf, json->child);
295  break;
296  case COND_TYPE_DGP:
297  case RULE_TYPE_DGP:
298  ret = cond_dgp_param_json_import(xcsf, json->child);
299  break;
300  case COND_TYPE_NEURAL:
301  case RULE_TYPE_NEURAL:
302  case RULE_TYPE_NETWORK:
303  ret = cond_neural_param_json_import(xcsf, json->child);
304  break;
305  default:
306  printf("cond_param_json_import(): unknown type.\n");
307  exit(EXIT_FAILURE);
308  }
309  return ret;
310 }
311 
318 size_t
319 cond_param_save(const struct XCSF *xcsf, FILE *fp)
320 {
321  const struct ArgsCond *cond = xcsf->cond;
322  size_t s = 0;
323  s += fwrite(&cond->type, sizeof(int), 1, fp);
324  s += fwrite(&cond->eta, sizeof(double), 1, fp);
325  s += fwrite(&cond->min, sizeof(double), 1, fp);
326  s += fwrite(&cond->max, sizeof(double), 1, fp);
327  s += fwrite(&cond->spread_min, sizeof(double), 1, fp);
328  s += fwrite(&cond->p_dontcare, sizeof(double), 1, fp);
329  s += fwrite(&cond->bits, sizeof(int), 1, fp);
330  s += graph_args_save(cond->dargs, fp);
331  s += tree_args_save(cond->targs, fp);
332  s += layer_args_save(cond->largs, fp);
333  return s;
334 }
335 
342 size_t
343 cond_param_load(struct XCSF *xcsf, FILE *fp)
344 {
345  struct ArgsCond *cond = xcsf->cond;
346  size_t s = 0;
347  s += fread(&cond->type, sizeof(int), 1, fp);
348  s += fread(&cond->eta, sizeof(double), 1, fp);
349  s += fread(&cond->min, sizeof(double), 1, fp);
350  s += fread(&cond->max, sizeof(double), 1, fp);
351  s += fread(&cond->spread_min, sizeof(double), 1, fp);
352  s += fread(&cond->p_dontcare, sizeof(double), 1, fp);
353  s += fread(&cond->bits, sizeof(int), 1, fp);
354  s += graph_args_load(cond->dargs, fp);
355  s += tree_args_load(cond->targs, fp);
356  s += layer_args_load(&cond->largs, fp);
357  return s;
358 }
359 
364 void
366 {
367  tree_args_free(xcsf->cond->targs);
368  free(xcsf->cond->targs);
369  free(xcsf->cond->dargs);
370  xcsf->cond->targs = NULL;
371  xcsf->cond->dargs = NULL;
372  layer_args_free(&xcsf->cond->largs);
373 }
374 
375 /* parameter setters */
376 
377 void
378 cond_param_set_eta(struct XCSF *xcsf, const double a)
379 {
380  if (a < 0) {
381  printf("Warning: tried to set COND ETA too small\n");
382  xcsf->cond->eta = 0;
383  } else if (a > 1) {
384  printf("Warning: tried to set COND ETA too large\n");
385  xcsf->cond->eta = 1;
386  } else {
387  xcsf->cond->eta = a;
388  }
389 }
390 
391 void
392 cond_param_set_min(struct XCSF *xcsf, const double a)
393 {
394  xcsf->cond->min = a;
395 }
396 
397 void
398 cond_param_set_max(struct XCSF *xcsf, const double a)
399 {
400  xcsf->cond->max = a;
401 }
402 
403 void
404 cond_param_set_p_dontcare(struct XCSF *xcsf, const double a)
405 {
406  if (a < 0) {
407  printf("Warning: tried to set COND P_DONTCARE too small\n");
408  xcsf->cond->p_dontcare = 0;
409  } else if (a > 1) {
410  printf("Warning: tried to set COND P_DONTCARE too large\n");
411  xcsf->cond->p_dontcare = 1;
412  } else {
413  xcsf->cond->p_dontcare = a;
414  }
415 }
416 
417 void
418 cond_param_set_spread_min(struct XCSF *xcsf, const double a)
419 {
420  if (a < 0) {
421  printf("Warning: tried to set COND SPREAD_MIN too small\n");
422  xcsf->cond->spread_min = 0;
423  } else {
424  xcsf->cond->spread_min = a;
425  }
426 }
427 
428 void
429 cond_param_set_bits(struct XCSF *xcsf, const int a)
430 {
431  if (a < 1) {
432  printf("Warning: tried to set COND BITS too small\n");
433  xcsf->cond->bits = 1;
434  } else {
435  xcsf->cond->bits = a;
436  }
437 }
438 
439 int
440 cond_param_set_type_string(struct XCSF *xcsf, const char *a)
441 {
442  const int type = condition_type_as_int(a);
443  if (type != COND_TYPE_INVALID) {
444  xcsf->cond->type = type;
445  }
446  return type;
447 }
448 
449 void
450 cond_param_set_type(struct XCSF *xcsf, const int a)
451 {
452  if (a < 0) {
453  printf("Warning: tried to set COND TYPE too small\n");
454  xcsf->cond->type = 0;
455  } else {
456  xcsf->cond->type = a;
457  }
458 }
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
char * cond_dgp_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the DGP parameters from a cJSON object.
Definition: cond_dgp.c:290
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_import(struct XCSF *xcsf, cJSON *json)
Sets the tree GP parameters from a cJSON object.
Definition: cond_gp.c:274
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
Tree GP condition functions.
static struct CondVtbl const cond_gp_vtbl
Tree GP condition implemented functions.
Definition: cond_gp.h:97
char * cond_neural_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the neural network parameters from a cJSON object.
Definition: cond_neural.c:353
void cond_neural_param_defaults(struct XCSF *xcsf)
Initialises default neural condition parameters.
Definition: cond_neural.c:383
Multi-layer perceptron neural network condition functions.
static struct CondVtbl const cond_neural_vtbl
Multi-layer perceptron neural network condition implemented functions.
Definition: cond_neural.h:107
Hyperrectangle condition functions.
static struct CondVtbl const cond_rectangle_vtbl
Hyperrectangle condition implemented functions.
char * cond_ternary_param_json_export(const struct XCSF *xcsf)
Returns a json formatted string of the ternary parameters.
Definition: cond_ternary.c:387
char * cond_ternary_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the ternary parameters from a cJSON object.
Definition: cond_ternary.c:405
void cond_ternary_param_defaults(struct XCSF *xcsf)
Initialises default ternary condition parameters.
Definition: cond_ternary.c:426
Ternary condition functions.
static struct CondVtbl const cond_ternary_vtbl
Ternary condition implemented functions.
Definition: cond_ternary.h:103
char * cond_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the condition parameters from a cJSON object.
Definition: condition.c:281
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:450
static char * cond_param_json_export_csr(const struct XCSF *xcsf)
Returns a json formatted string of the center-spread parameters.
Definition: condition.c:185
void cond_param_set_spread_min(struct XCSF *xcsf, const double a)
Definition: condition.c:418
size_t cond_param_load(struct XCSF *xcsf, FILE *fp)
Loads condition parameters.
Definition: condition.c:343
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:429
static char * cond_param_json_import_csr(struct XCSF *xcsf, cJSON *json)
Sets the center-spread parameters from a cJSON object.
Definition: condition.c:205
void cond_param_set_min(struct XCSF *xcsf, const double a)
Definition: condition.c:392
size_t cond_param_save(const struct XCSF *xcsf, FILE *fp)
Saves condition parameters.
Definition: condition.c:319
void cond_param_set_p_dontcare(struct XCSF *xcsf, const double a)
Definition: condition.c:404
void cond_param_set_max(struct XCSF *xcsf, const double a)
Definition: condition.c:398
int cond_param_set_type_string(struct XCSF *xcsf, const char *a)
Definition: condition.c:440
char * cond_param_json_export(const struct XCSF *xcsf)
Returns a json formatted string of the condition parameters.
Definition: condition.c:232
void cond_param_defaults(struct XCSF *xcsf)
Initialises default condition parameters.
Definition: condition.c:166
void cond_param_free(struct XCSF *xcsf)
Frees condition parameters.
Definition: condition.c:365
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_set_eta(struct XCSF *xcsf, const double a)
Definition: condition.c:378
#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
Definition: __init__.py:1
char * layer_args_json_export(struct ArgsLayer *args)
Returns a json formatted string of the neural layer parameters.
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.
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.
Definition: rule_neural.h:156
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 min
Minimum value expected from inputs.
Definition: condition.h:64
double eta
Gradient descent rate.
Definition: condition.h:62
double p_dontcare
Don't care probability.
Definition: condition.h:65
struct ArgsDGP * dargs
DGP parameters.
Definition: condition.h:69
struct ArgsGPTree * targs
Tree GP parameters.
Definition: condition.h:70
int type
Classifier condition type: hyperrectangles, etc.
Definition: condition.h:61
int bits
Bits per float to binarise inputs.
Definition: condition.h:67
double spread_min
Minimum initial spread.
Definition: condition.h:66
struct ArgsLayer * largs
Linked-list of layer parameters.
Definition: condition.h:68
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
Utility functions for random number handling, etc.