XCSF 1.4.8
XCSF learning classifier system
Loading...
Searching...
No Matches
cond_gp.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_gp.h"
25#include "ea.h"
26#include "sam.h"
27#include "utils.h"
28
34void
35cond_gp_init(const struct XCSF *xcsf, struct Cl *c)
36{
37 struct CondGP *new = malloc(sizeof(struct CondGP));
38 tree_rand(&new->gp, xcsf->cond->targs);
39 c->cond = new;
40}
41
47void
48cond_gp_free(const struct XCSF *xcsf, const struct Cl *c)
49{
50 (void) xcsf;
51 const struct CondGP *cond = c->cond;
52 tree_free(&cond->gp);
53 free(c->cond);
54}
55
62void
63cond_gp_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
64{
65 (void) xcsf;
66 struct CondGP *new = malloc(sizeof(struct CondGP));
67 const struct CondGP *src_cond = src->cond;
68 tree_copy(&new->gp, &src_cond->gp);
69 dest->cond = new;
70}
71
78void
79cond_gp_cover(const struct XCSF *xcsf, const struct Cl *c, const double *x)
80{
81 struct CondGP *cond = c->cond;
82 do {
83 tree_free(&cond->gp);
84 tree_rand(&cond->gp, xcsf->cond->targs);
85 } while (!cond_gp_match(xcsf, c, x));
86}
87
95void
96cond_gp_update(const struct XCSF *xcsf, const struct Cl *c, const double *x,
97 const double *y)
98{
99 (void) xcsf;
100 (void) c;
101 (void) x;
102 (void) y;
103}
104
112bool
113cond_gp_match(const struct XCSF *xcsf, const struct Cl *c, const double *x)
114{
115 struct CondGP *cond = c->cond;
116 cond->gp.pos = 0;
117 if (tree_eval(&cond->gp, xcsf->cond->targs, x) > 0.5) {
118 return true;
119 }
120 return false;
121}
122
129bool
130cond_gp_mutate(const struct XCSF *xcsf, const struct Cl *c)
131{
132 (void) xcsf;
133 struct CondGP *cond = c->cond;
134 return tree_mutate(&cond->gp, xcsf->cond->targs);
135}
136
144bool
145cond_gp_crossover(const struct XCSF *xcsf, const struct Cl *c1,
146 const struct Cl *c2)
147{
148 (void) xcsf;
149 struct CondGP *cond1 = c1->cond;
150 struct CondGP *cond2 = c2->cond;
151 if (rand_uniform(0, 1) < xcsf->ea->p_crossover) {
152 tree_crossover(&cond1->gp, &cond2->gp);
153 return true;
154 }
155 return false;
156}
157
165bool
166cond_gp_general(const struct XCSF *xcsf, const struct Cl *c1,
167 const struct Cl *c2)
168{
169 (void) xcsf;
170 (void) c1;
171 (void) c2;
172 return false;
173}
174
180void
181cond_gp_print(const struct XCSF *xcsf, const struct Cl *c)
182{
183 char *json_str = cond_gp_json_export(xcsf, c);
184 printf("%s\n", json_str);
185 free(json_str);
186}
187
194double
195cond_gp_size(const struct XCSF *xcsf, const struct Cl *c)
196{
197 (void) xcsf;
198 const struct CondGP *cond = c->cond;
199 return cond->gp.len;
200}
201
209size_t
210cond_gp_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
211{
212 (void) xcsf;
213 const struct CondGP *cond = c->cond;
214 size_t s = tree_save(&cond->gp, fp);
215 return s;
216}
217
225size_t
226cond_gp_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
227{
228 (void) xcsf;
229 struct CondGP *new = malloc(sizeof(struct CondGP));
230 size_t s = tree_load(&new->gp, fp);
231 c->cond = new;
232 return s;
233}
234
241char *
242cond_gp_json_export(const struct XCSF *xcsf, const struct Cl *c)
243{
244 const struct CondGP *cond = c->cond;
245 cJSON *json = cJSON_CreateObject();
246 cJSON_AddStringToObject(json, "type", "tree_gp");
247 char *tree_str = tree_json_export(&cond->gp, xcsf->cond->targs);
248 cJSON *tree = cJSON_Parse(tree_str);
249 free(tree_str);
250 cJSON_AddItemToObject(json, "tree", tree);
251 char *string = cJSON_Print(json);
252 cJSON_Delete(json);
253 return string;
254}
255
261char *
263{
264 return tree_args_json_export(xcsf->cond->targs);
265}
266
273char *
275{
276 char *ret = tree_args_json_import(xcsf->cond->targs, json);
277 tree_args_init_constants(xcsf->cond->targs);
278 return ret;
279}
280
285void
287{
288 struct ArgsGPTree *args = malloc(sizeof(struct ArgsGPTree));
289 tree_args_init(args);
290 tree_param_set_max(args, 1);
291 tree_param_set_min(args, 0);
293 tree_param_set_max_len(args, 10000);
295 tree_param_set_n_inputs(args, xcsf->x_dim);
297 xcsf->cond->targs = args;
298}
299
306void
307cond_gp_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
308{
309 const cJSON *item = cJSON_GetObjectItem(json, "tree");
310 if (item == NULL) {
311 printf("Import error: missing tree\n");
312 exit(EXIT_FAILURE);
313 }
314 struct CondGP *cond = c->cond;
315 tree_json_import(&cond->gp, xcsf->cond->targs, item);
316}
void cond_gp_print(const struct XCSF *xcsf, const struct Cl *c)
Prints a tree-GP condition.
Definition cond_gp.c:181
char * cond_gp_json_export(const struct XCSF *xcsf, const struct Cl *c)
Returns a json formatted string representation of a tree-GP condition.
Definition cond_gp.c:242
void cond_gp_cover(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Generates a GP tree that matches the current input.
Definition cond_gp.c:79
void cond_gp_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Copies a tree-GP condition from one classifier to another.
Definition cond_gp.c:63
size_t cond_gp_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Writes a tree-GP condition to a file.
Definition cond_gp.c:210
bool cond_gp_general(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Dummy general function.
Definition cond_gp.c:166
void cond_gp_param_defaults(struct XCSF *xcsf)
Initialises default tree GP condition parameters.
Definition cond_gp.c:286
bool cond_gp_crossover(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Performs sub-tree crossover with two tree-GP conditions.
Definition cond_gp.c:145
size_t cond_gp_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Reads a tree-GP condition from a file.
Definition cond_gp.c:226
bool cond_gp_mutate(const struct XCSF *xcsf, const struct Cl *c)
Mutates a tree-GP condition with the self-adaptive rate.
Definition cond_gp.c:130
void cond_gp_free(const struct XCSF *xcsf, const struct Cl *c)
Frees the memory used by a tree-GP condition.
Definition cond_gp.c:48
bool cond_gp_match(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Calculates whether a GP tree condition matches an input.
Definition cond_gp.c:113
double cond_gp_size(const struct XCSF *xcsf, const struct Cl *c)
Returns the size of a tree-GP condition.
Definition cond_gp.c:195
void cond_gp_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Creates a tree GP condition from a cJSON object.
Definition cond_gp.c:307
void cond_gp_init(const struct XCSF *xcsf, struct Cl *c)
Creates and initialises a tree-GP condition.
Definition cond_gp.c:35
void cond_gp_update(const struct XCSF *xcsf, const struct Cl *c, const double *x, const double *y)
Dummy update function.
Definition cond_gp.c:96
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.
Evolutionary algorithm functions.
void tree_json_import(struct GPTree *gp, const struct ArgsGPTree *args, const cJSON *json)
Creates a GP tree from a cJSON object.
Definition gp.c:273
char * tree_args_json_export(const struct ArgsGPTree *args)
Returns a json formatted string of the GP tree parameters.
Definition gp.c:449
char * tree_args_json_import(struct ArgsGPTree *args, cJSON *json)
Sets the GP tree parameters from a cJSON object.
Definition gp.c:469
char * tree_json_export(const struct GPTree *gp, const struct ArgsGPTree *args)
Returns a json formatted string representation of a GP tree.
Definition gp.c:253
size_t tree_load(struct GPTree *gp, FILE *fp)
Reads a GP tree from a file.
Definition gp.c:400
void tree_param_set_max_len(struct ArgsGPTree *args, const int a)
Definition gp.c:598
void tree_crossover(struct GPTree *p1, struct GPTree *p2)
Performs sub-tree crossover.
Definition gp.c:318
size_t tree_save(const struct GPTree *gp, FILE *fp)
Writes the GP tree to a file.
Definition gp.c:383
void tree_param_set_max(struct ArgsGPTree *args, const double a)
Definition gp.c:553
void tree_param_set_n_inputs(struct ArgsGPTree *args, const int a)
Definition gp.c:565
double tree_eval(struct GPTree *gp, const struct ArgsGPTree *args, const double *x)
Evaluates a GP tree.
Definition gp.c:133
void tree_param_set_n_constants(struct ArgsGPTree *args, const int a)
Definition gp.c:576
void tree_args_init_constants(struct ArgsGPTree *args)
Builds global constants used by GP trees.
Definition gp.c:539
void tree_param_set_init_depth(struct ArgsGPTree *args, const int a)
Definition gp.c:587
bool tree_mutate(struct GPTree *gp, const struct ArgsGPTree *args)
Performs point mutation on a GP tree.
Definition gp.c:355
void tree_args_init(struct ArgsGPTree *args)
Sets tree GP parameters to default values.
Definition gp.c:422
void tree_param_set_min(struct ArgsGPTree *args, const double a)
Definition gp.c:559
void tree_copy(struct GPTree *dest, const struct GPTree *src)
Copies a GP tree.
Definition gp.c:302
void tree_free(const struct GPTree *gp)
Frees a GP tree.
Definition gp.c:119
void tree_rand(struct GPTree *gp, const struct ArgsGPTree *args)
Creates a random GP tree.
Definition gp.c:102
Self-adaptive mutation functions.
Parameters for initialising GP trees.
Definition gp.h:31
Classifier data structure.
Definition xcsf.h:45
void * cond
Condition structure.
Definition xcsf.h:49
Tree GP condition data structure.
Definition cond_gp.h:33
struct GPTree gp
GP tree.
Definition cond_gp.h:34
int pos
Current position in the tree.
Definition gp.h:47
int len
Size of the tree.
Definition gp.h:46
XCSF data structure.
Definition xcsf.h:85
double rand_uniform(const double min, const double max)
Returns a uniform random float [min,max].
Definition utils.c:62
Utility functions for random number handling, etc.