XCSF  1.4.7
XCSF learning classifier system
condition.h
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 #pragma once
25 
26 #include "xcsf.h"
27 
28 #define COND_TYPE_INVALID (-1)
29 #define COND_TYPE_DUMMY (0)
30 #define COND_TYPE_HYPERRECTANGLE_CSR (1)
31 #define COND_TYPE_HYPERRECTANGLE_UBR (2)
32 #define COND_TYPE_HYPERELLIPSOID (3)
33 #define COND_TYPE_NEURAL (4)
34 #define COND_TYPE_GP (5)
35 #define COND_TYPE_DGP (6)
36 #define COND_TYPE_TERNARY (7)
37 #define RULE_TYPE_DGP (11)
38 #define RULE_TYPE_NEURAL (12)
39 #define RULE_TYPE_NETWORK (13)
40 
41 #define COND_STRING_DUMMY ("dummy\0")
42 #define COND_STRING_HYPERRECTANGLE_CSR ("hyperrectangle_csr\0")
43 #define COND_STRING_HYPERRECTANGLE_UBR ("hyperrectangle_ubr\0")
44 #define COND_STRING_HYPERELLIPSOID ("hyperellipsoid\0")
45 #define COND_STRING_NEURAL ("neural\0")
46 #define COND_STRING_GP ("tree_gp\0")
47 #define COND_STRING_DGP ("dgp\0")
48 #define COND_STRING_TERNARY ("ternary\0")
49 #define COND_STRING_RULE_DGP ("rule_dgp\0")
50 #define COND_STRING_RULE_NEURAL ("rule_neural\0")
51 #define COND_STRING_RULE_NETWORK ("rule_network\0")
52 
53 #define COND_TYPE_OPTIONS \
54  "dummy, hyperrectangle_csr, hyperrectangle_ubr, hyperellipsoid, neural, " \
55  "tree_gp, dgp, ternary, rule_dgp, rule_neural, rule_network"
56 
60 struct ArgsCond {
61  int type;
62  double eta;
63  double max;
64  double min;
65  double p_dontcare;
66  double spread_min;
67  int bits;
68  struct ArgsLayer *largs;
69  struct ArgsDGP *dargs;
70  struct ArgsGPTree *targs;
71 };
72 
73 void
74 condition_set(const struct XCSF *xcsf, struct Cl *c);
75 
76 const char *
77 condition_type_as_string(const int type);
78 
79 int
80 condition_type_as_int(const char *type);
81 
82 void
84 
85 void
86 cond_param_free(struct XCSF *xcsf);
87 
88 char *
89 cond_param_json_import(struct XCSF *xcsf, cJSON *json);
90 
91 char *
92 cond_param_json_export(const struct XCSF *xcsf);
93 
94 size_t
95 cond_param_save(const struct XCSF *xcsf, FILE *fp);
96 
97 size_t
98 cond_param_load(struct XCSF *xcsf, FILE *fp);
99 
104 struct CondVtbl {
105  bool (*cond_impl_crossover)(const struct XCSF *xcsf, const struct Cl *c1,
106  const struct Cl *c2);
107  bool (*cond_impl_general)(const struct XCSF *xcsf, const struct Cl *c1,
108  const struct Cl *c2);
109  bool (*cond_impl_match)(const struct XCSF *xcsf, const struct Cl *c,
110  const double *x);
111  bool (*cond_impl_mutate)(const struct XCSF *xcsf, const struct Cl *c);
112  void (*cond_impl_copy)(const struct XCSF *xcsf, struct Cl *dest,
113  const struct Cl *src);
114  void (*cond_impl_cover)(const struct XCSF *xcsf, const struct Cl *c,
115  const double *x);
116  void (*cond_impl_free)(const struct XCSF *xcsf, const struct Cl *c);
117  void (*cond_impl_init)(const struct XCSF *xcsf, struct Cl *c);
118  void (*cond_impl_print)(const struct XCSF *xcsf, const struct Cl *c);
119  void (*cond_impl_update)(const struct XCSF *xcsf, const struct Cl *c,
120  const double *x, const double *y);
121  double (*cond_impl_size)(const struct XCSF *xcsf, const struct Cl *c);
122  size_t (*cond_impl_save)(const struct XCSF *xcsf, const struct Cl *c,
123  FILE *fp);
124  size_t (*cond_impl_load)(const struct XCSF *xcsf, struct Cl *c, FILE *fp);
125  char *(*cond_impl_json_export)(const struct XCSF *xcsf, const struct Cl *c);
126  void (*cond_impl_json_import)(const struct XCSF *xcsf, struct Cl *c,
127  const cJSON *json);
128 };
129 
137 static inline size_t
138 cond_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
139 {
140  return (*c->cond_vptr->cond_impl_save)(xcsf, c, fp);
141 }
142 
150 static inline size_t
151 cond_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
152 {
153  return (*c->cond_vptr->cond_impl_load)(xcsf, c, fp);
154 }
155 
162 static inline double
163 cond_size(const struct XCSF *xcsf, const struct Cl *c)
164 {
165  return (*c->cond_vptr->cond_impl_size)(xcsf, c);
166 }
167 
175 static inline void
176 cond_update(const struct XCSF *xcsf, const struct Cl *c, const double *x,
177  const double *y)
178 {
179  (*c->cond_vptr->cond_impl_update)(xcsf, c, x, y);
180 }
181 
189 static inline bool
190 cond_crossover(const struct XCSF *xcsf, const struct Cl *c1,
191  const struct Cl *c2)
192 {
193  return (*c1->cond_vptr->cond_impl_crossover)(xcsf, c1, c2);
194 }
195 
203 static inline bool
204 cond_general(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
205 {
206  return (*c1->cond_vptr->cond_impl_general)(xcsf, c1, c2);
207 }
208 
216 static inline bool
217 cond_match(const struct XCSF *xcsf, const struct Cl *c, const double *x)
218 {
219  return (*c->cond_vptr->cond_impl_match)(xcsf, c, x);
220 }
221 
228 static inline bool
229 cond_mutate(const struct XCSF *xcsf, const struct Cl *c)
230 {
231  return (*c->cond_vptr->cond_impl_mutate)(xcsf, c);
232 }
233 
240 static inline void
241 cond_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
242 {
243  (*src->cond_vptr->cond_impl_copy)(xcsf, dest, src);
244 }
245 
252 static inline void
253 cond_cover(const struct XCSF *xcsf, const struct Cl *c, const double *x)
254 {
255  (*c->cond_vptr->cond_impl_cover)(xcsf, c, x);
256 }
257 
263 static inline void
264 cond_free(const struct XCSF *xcsf, const struct Cl *c)
265 {
266  (*c->cond_vptr->cond_impl_free)(xcsf, c);
267 }
268 
274 static inline void
275 cond_init(const struct XCSF *xcsf, struct Cl *c)
276 {
277  (*c->cond_vptr->cond_impl_init)(xcsf, c);
278 }
279 
285 static inline void
286 cond_print(const struct XCSF *xcsf, const struct Cl *c)
287 {
288  (*c->cond_vptr->cond_impl_print)(xcsf, c);
289 }
290 
297 static inline char *
298 cond_json_export(const struct XCSF *xcsf, const struct Cl *c)
299 {
300  return (*c->cond_vptr->cond_impl_json_export)(xcsf, c);
301 }
302 
309 static inline void
310 cond_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
311 {
312  const cJSON *item = cJSON_GetObjectItem(json, "type");
313  if (item == NULL || !cJSON_IsString(item)) {
314  printf("cond_json_import(): missing type\n");
315  exit(EXIT_FAILURE);
316  }
317  const char *type = item->valuestring;
318  if (condition_type_as_int(type) != xcsf->cond->type) {
319  printf("cond_json_import(): mismatched type\n");
320  printf("XCSF type = %s, but imported type = %s\n",
321  condition_type_as_string(xcsf->cond->type), type);
322  exit(EXIT_FAILURE);
323  }
324  (*c->cond_vptr->cond_impl_json_import)(xcsf, c, json);
325 }
326 
327 /* parameter setters */
328 
329 void
330 cond_param_set_eta(struct XCSF *xcsf, const double a);
331 
332 void
333 cond_param_set_min(struct XCSF *xcsf, const double a);
334 
335 void
336 cond_param_set_max(struct XCSF *xcsf, const double a);
337 
338 void
339 cond_param_set_p_dontcare(struct XCSF *xcsf, const double a);
340 
341 void
342 cond_param_set_spread_min(struct XCSF *xcsf, const double a);
343 
344 void
345 cond_param_set_bits(struct XCSF *xcsf, const int a);
346 
347 int
348 cond_param_set_type_string(struct XCSF *xcsf, const char *a);
349 
350 void
351 cond_param_set_type(struct XCSF *xcsf, const int a);
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 void cond_print(const struct XCSF *xcsf, const struct Cl *c)
Prints the classifier condition.
Definition: condition.h:286
static void cond_free(const struct XCSF *xcsf, const struct Cl *c)
Frees the memory used by the classifier condition.
Definition: condition.h:264
static bool cond_mutate(const struct XCSF *xcsf, const struct Cl *c)
Performs classifier condition mutation.
Definition: condition.h:229
void cond_param_set_spread_min(struct XCSF *xcsf, const double a)
Definition: condition.c:418
static bool cond_crossover(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Performs classifier condition crossover.
Definition: condition.h:190
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
static bool cond_general(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Returns whether classifier c1 has a condition more general than c2.
Definition: condition.h:204
void cond_param_set_bits(struct XCSF *xcsf, const int a)
Definition: condition.c:429
static size_t cond_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Writes the condition to a file.
Definition: condition.h:138
void cond_param_set_min(struct XCSF *xcsf, const double a)
Definition: condition.c:392
static void cond_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Creates a condition from a cJSON object.
Definition: condition.h:310
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
static void cond_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Copies the condition from one classifier to another.
Definition: condition.h:241
int cond_param_set_type_string(struct XCSF *xcsf, const char *a)
Definition: condition.c:440
static double cond_size(const struct XCSF *xcsf, const struct Cl *c)
Returns the size of the classifier condition.
Definition: condition.h:163
static void cond_cover(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Generates a condition that matches the current input.
Definition: condition.h:253
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
static void cond_init(const struct XCSF *xcsf, struct Cl *c)
Initialises a classifier's condition.
Definition: condition.h:275
static bool cond_match(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Calculates whether the condition matches the input.
Definition: condition.h:217
static size_t cond_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Reads the condition from a file.
Definition: condition.h:151
static char * cond_json_export(const struct XCSF *xcsf, const struct Cl *c)
Returns a json formatted string representation of a condition.
Definition: condition.h:298
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
static void cond_update(const struct XCSF *xcsf, const struct Cl *c, const double *x, const double *y)
Updates the classifier's condition.
Definition: condition.h:176
void cond_param_set_eta(struct XCSF *xcsf, const double a)
Definition: condition.c:378
Definition: __init__.py:1
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
Parameters for initialising DGP graphs.
Definition: dgp.h:31
Parameters for initialising GP trees.
Definition: gp.h:31
Parameters for initialising a neural network layer.
Classifier data structure.
Definition: xcsf.h:45
struct CondVtbl const * cond_vptr
Functions acting on conditions.
Definition: xcsf.h:46
Condition interface data structure.
Definition: condition.h:104
void(* cond_impl_copy)(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Definition: condition.h:112
void(* cond_impl_init)(const struct XCSF *xcsf, struct Cl *c)
Definition: condition.h:117
void(* cond_impl_print)(const struct XCSF *xcsf, const struct Cl *c)
Definition: condition.h:118
void(* cond_impl_json_import)(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Definition: condition.h:126
double(* cond_impl_size)(const struct XCSF *xcsf, const struct Cl *c)
Definition: condition.h:121
void(* cond_impl_free)(const struct XCSF *xcsf, const struct Cl *c)
Definition: condition.h:116
void(* cond_impl_update)(const struct XCSF *xcsf, const struct Cl *c, const double *x, const double *y)
Definition: condition.h:119
bool(* cond_impl_crossover)(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Definition: condition.h:105
bool(* cond_impl_general)(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Definition: condition.h:107
bool(* cond_impl_mutate)(const struct XCSF *xcsf, const struct Cl *c)
Definition: condition.h:111
size_t(* cond_impl_load)(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Definition: condition.h:124
void(* cond_impl_cover)(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Definition: condition.h:114
size_t(* cond_impl_save)(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Definition: condition.h:122
bool(* cond_impl_match)(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Definition: condition.h:109
char *(* cond_impl_json_export)(const struct XCSF *xcsf, const struct Cl *c)
Definition: condition.h:125
XCSF data structure.
Definition: xcsf.h:85
XCSF data structures.