XCSF 1.5.1
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 double p_mu;
68 double mu;
69 bool sam;
70 int bits;
71 struct ArgsLayer *largs;
72 struct ArgsDGP *dargs;
73 struct ArgsGPTree *targs;
74};
75
76void
77condition_set(const struct XCSF *xcsf, struct Cl *c);
78
79const char *
80condition_type_as_string(const int type);
81
82int
83condition_type_as_int(const char *type);
84
85void
87
88void
90
91char *
92cond_param_json_import(struct XCSF *xcsf, cJSON *json);
93
94char *
95cond_param_json_export(const struct XCSF *xcsf);
96
97size_t
98cond_param_save(const struct XCSF *xcsf, FILE *fp);
99
100size_t
101cond_param_load(struct XCSF *xcsf, FILE *fp);
102
107struct CondVtbl {
108 bool (*cond_impl_crossover)(const struct XCSF *xcsf, const struct Cl *c1,
109 const struct Cl *c2);
110 bool (*cond_impl_general)(const struct XCSF *xcsf, const struct Cl *c1,
111 const struct Cl *c2);
112 bool (*cond_impl_match)(const struct XCSF *xcsf, const struct Cl *c,
113 const double *x);
114 bool (*cond_impl_mutate)(const struct XCSF *xcsf, const struct Cl *c);
115 void (*cond_impl_copy)(const struct XCSF *xcsf, struct Cl *dest,
116 const struct Cl *src);
117 void (*cond_impl_cover)(const struct XCSF *xcsf, const struct Cl *c,
118 const double *x);
119 void (*cond_impl_free)(const struct XCSF *xcsf, const struct Cl *c);
120 void (*cond_impl_init)(const struct XCSF *xcsf, struct Cl *c);
121 void (*cond_impl_print)(const struct XCSF *xcsf, const struct Cl *c);
122 void (*cond_impl_update)(const struct XCSF *xcsf, const struct Cl *c,
123 const double *x, const double *y);
124 double (*cond_impl_size)(const struct XCSF *xcsf, const struct Cl *c);
125 size_t (*cond_impl_save)(const struct XCSF *xcsf, const struct Cl *c,
126 FILE *fp);
127 size_t (*cond_impl_load)(const struct XCSF *xcsf, struct Cl *c, FILE *fp);
128 char *(*cond_impl_json_export)(const struct XCSF *xcsf, const struct Cl *c);
129 void (*cond_impl_json_import)(const struct XCSF *xcsf, struct Cl *c,
130 const cJSON *json);
131};
132
140static inline size_t
141cond_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
142{
143 return (*c->cond_vptr->cond_impl_save)(xcsf, c, fp);
144}
145
153static inline size_t
154cond_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
155{
156 return (*c->cond_vptr->cond_impl_load)(xcsf, c, fp);
157}
158
165static inline double
166cond_size(const struct XCSF *xcsf, const struct Cl *c)
167{
168 return (*c->cond_vptr->cond_impl_size)(xcsf, c);
169}
170
178static inline void
179cond_update(const struct XCSF *xcsf, const struct Cl *c, const double *x,
180 const double *y)
181{
182 (*c->cond_vptr->cond_impl_update)(xcsf, c, x, y);
183}
184
192static inline bool
193cond_crossover(const struct XCSF *xcsf, const struct Cl *c1,
194 const struct Cl *c2)
195{
196 return (*c1->cond_vptr->cond_impl_crossover)(xcsf, c1, c2);
197}
198
206static inline bool
207cond_general(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
208{
209 return (*c1->cond_vptr->cond_impl_general)(xcsf, c1, c2);
210}
211
219static inline bool
220cond_match(const struct XCSF *xcsf, const struct Cl *c, const double *x)
221{
222 return (*c->cond_vptr->cond_impl_match)(xcsf, c, x);
223}
224
231static inline bool
232cond_mutate(const struct XCSF *xcsf, const struct Cl *c)
233{
234 return (*c->cond_vptr->cond_impl_mutate)(xcsf, c);
235}
236
243static inline void
244cond_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
245{
246 (*src->cond_vptr->cond_impl_copy)(xcsf, dest, src);
247}
248
255static inline void
256cond_cover(const struct XCSF *xcsf, const struct Cl *c, const double *x)
257{
258 (*c->cond_vptr->cond_impl_cover)(xcsf, c, x);
259}
260
266static inline void
267cond_free(const struct XCSF *xcsf, const struct Cl *c)
268{
269 (*c->cond_vptr->cond_impl_free)(xcsf, c);
270}
271
277static inline void
278cond_init(const struct XCSF *xcsf, struct Cl *c)
279{
280 (*c->cond_vptr->cond_impl_init)(xcsf, c);
281}
282
288static inline void
289cond_print(const struct XCSF *xcsf, const struct Cl *c)
290{
291 (*c->cond_vptr->cond_impl_print)(xcsf, c);
292}
293
300static inline char *
301cond_json_export(const struct XCSF *xcsf, const struct Cl *c)
302{
303 return (*c->cond_vptr->cond_impl_json_export)(xcsf, c);
304}
305
312static inline void
313cond_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
314{
315 const cJSON *item = cJSON_GetObjectItem(json, "type");
316 if (item == NULL || !cJSON_IsString(item)) {
317 printf("cond_json_import(): missing type\n");
318 exit(EXIT_FAILURE);
319 }
320 const char *type = item->valuestring;
321 if (condition_type_as_int(type) != xcsf->cond->type) {
322 printf("cond_json_import(): mismatched type\n");
323 printf("XCSF type = %s, but imported type = %s\n",
324 condition_type_as_string(xcsf->cond->type), type);
325 exit(EXIT_FAILURE);
326 }
327 (*c->cond_vptr->cond_impl_json_import)(xcsf, c, json);
328}
329
330/* parameter setters */
331
332void
333cond_param_set_eta(struct XCSF *xcsf, const double a);
334
335void
336cond_param_set_min(struct XCSF *xcsf, const double a);
337
338void
339cond_param_set_max(struct XCSF *xcsf, const double a);
340
341void
342cond_param_set_p_dontcare(struct XCSF *xcsf, const double a);
343
344void
345cond_param_set_mu(struct XCSF *xcsf, const double a);
346
347void
348cond_param_set_p_mu(struct XCSF *xcsf, const double a);
349
350void
351cond_param_set_sam(struct XCSF *xcsf, const bool a);
352
353void
354cond_param_set_spread_min(struct XCSF *xcsf, const double a);
355
356void
357cond_param_set_bits(struct XCSF *xcsf, const int a);
358
359int
360cond_param_set_type_string(struct XCSF *xcsf, const char *a);
361
362void
363cond_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:506
static void cond_print(const struct XCSF *xcsf, const struct Cl *c)
Prints the classifier condition.
Definition condition.h:289
static void cond_free(const struct XCSF *xcsf, const struct Cl *c)
Frees the memory used by the classifier condition.
Definition condition.h:267
static bool cond_mutate(const struct XCSF *xcsf, const struct Cl *c)
Performs classifier condition mutation.
Definition condition.h:232
void cond_param_set_spread_min(struct XCSF *xcsf, const double a)
Definition condition.c:474
static bool cond_crossover(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Performs classifier condition crossover.
Definition condition.h:193
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
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:207
void cond_param_set_bits(struct XCSF *xcsf, const int a)
Definition condition.c:485
static size_t cond_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Writes the condition to a file.
Definition condition.h:141
void cond_param_set_min(struct XCSF *xcsf, const double a)
Definition condition.c:414
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:313
size_t cond_param_save(const struct XCSF *xcsf, FILE *fp)
Saves condition parameters.
Definition condition.c:335
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:301
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
void cond_param_set_mu(struct XCSF *xcsf, const double a)
Definition condition.c:440
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:244
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
static double cond_size(const struct XCSF *xcsf, const struct Cl *c)
Returns the size of the classifier condition.
Definition condition.h:166
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:256
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:278
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:220
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:154
void cond_param_free(struct XCSF *xcsf)
Frees condition parameters.
Definition condition.c:387
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:179
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
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
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:107
void(* cond_impl_copy)(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Definition condition.h:115
void(* cond_impl_init)(const struct XCSF *xcsf, struct Cl *c)
Definition condition.h:120
void(* cond_impl_print)(const struct XCSF *xcsf, const struct Cl *c)
Definition condition.h:121
void(* cond_impl_json_import)(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Definition condition.h:129
double(* cond_impl_size)(const struct XCSF *xcsf, const struct Cl *c)
Definition condition.h:124
void(* cond_impl_free)(const struct XCSF *xcsf, const struct Cl *c)
Definition condition.h:119
void(* cond_impl_update)(const struct XCSF *xcsf, const struct Cl *c, const double *x, const double *y)
Definition condition.h:122
bool(* cond_impl_crossover)(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Definition condition.h:108
bool(* cond_impl_general)(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Definition condition.h:110
bool(* cond_impl_mutate)(const struct XCSF *xcsf, const struct Cl *c)
Definition condition.h:114
char *(* cond_impl_json_export)(const struct XCSF *xcsf, const struct Cl *c)
Definition condition.h:128
size_t(* cond_impl_load)(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Definition condition.h:127
void(* cond_impl_cover)(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Definition condition.h:117
size_t(* cond_impl_save)(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Definition condition.h:125
bool(* cond_impl_match)(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Definition condition.h:112
XCSF data structure.
Definition xcsf.h:85
XCSF data structures.