XCSF 1.4.8
XCSF learning classifier system
Loading...
Searching...
No Matches
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
60struct 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
73void
74condition_set(const struct XCSF *xcsf, struct Cl *c);
75
76const char *
77condition_type_as_string(const int type);
78
79int
80condition_type_as_int(const char *type);
81
82void
84
85void
87
88char *
89cond_param_json_import(struct XCSF *xcsf, cJSON *json);
90
91char *
92cond_param_json_export(const struct XCSF *xcsf);
93
94size_t
95cond_param_save(const struct XCSF *xcsf, FILE *fp);
96
97size_t
98cond_param_load(struct XCSF *xcsf, FILE *fp);
99
104struct 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
137static inline size_t
138cond_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
150static inline size_t
151cond_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
152{
153 return (*c->cond_vptr->cond_impl_load)(xcsf, c, fp);
154}
155
162static inline double
163cond_size(const struct XCSF *xcsf, const struct Cl *c)
164{
165 return (*c->cond_vptr->cond_impl_size)(xcsf, c);
166}
167
175static inline void
176cond_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
189static inline bool
190cond_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
203static inline bool
204cond_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
216static inline bool
217cond_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
228static inline bool
229cond_mutate(const struct XCSF *xcsf, const struct Cl *c)
230{
231 return (*c->cond_vptr->cond_impl_mutate)(xcsf, c);
232}
233
240static inline void
241cond_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
252static inline void
253cond_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
263static inline void
264cond_free(const struct XCSF *xcsf, const struct Cl *c)
265{
266 (*c->cond_vptr->cond_impl_free)(xcsf, c);
267}
268
274static inline void
275cond_init(const struct XCSF *xcsf, struct Cl *c)
276{
277 (*c->cond_vptr->cond_impl_init)(xcsf, c);
278}
279
285static inline void
286cond_print(const struct XCSF *xcsf, const struct Cl *c)
287{
288 (*c->cond_vptr->cond_impl_print)(xcsf, c);
289}
290
297static inline char *
298cond_json_export(const struct XCSF *xcsf, const struct Cl *c)
299{
300 return (*c->cond_vptr->cond_impl_json_export)(xcsf, c);
301}
302
309static inline void
310cond_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
329void
330cond_param_set_eta(struct XCSF *xcsf, const double a);
331
332void
333cond_param_set_min(struct XCSF *xcsf, const double a);
334
335void
336cond_param_set_max(struct XCSF *xcsf, const double a);
337
338void
339cond_param_set_p_dontcare(struct XCSF *xcsf, const double a);
340
341void
342cond_param_set_spread_min(struct XCSF *xcsf, const double a);
343
344void
345cond_param_set_bits(struct XCSF *xcsf, const int a);
346
347int
348cond_param_set_type_string(struct XCSF *xcsf, const char *a);
349
350void
351cond_param_set_type(struct XCSF *xcsf, const int a);
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
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_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
char * cond_param_json_export(const struct XCSF *xcsf)
Returns a json formatted string of the condition parameters.
Definition condition.c:232
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
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
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 size_t cond_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Reads the condition from a file.
Definition condition.h:151
void cond_param_free(struct XCSF *xcsf)
Frees condition parameters.
Definition condition.c:365
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
char * cond_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the condition parameters from a cJSON object.
Definition condition.c:281
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
char *(* cond_impl_json_export)(const struct XCSF *xcsf, const struct Cl *c)
Definition condition.h:125
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
XCSF data structure.
Definition xcsf.h:85
XCSF data structures.