XCSF  1.4.7
XCSF learning classifier system
cond_dgp.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 "sam.h"
26 #include "utils.h"
27 
33 void
34 cond_dgp_init(const struct XCSF *xcsf, struct Cl *c)
35 {
36  struct CondDGP *new = malloc(sizeof(struct CondDGP));
37  graph_init(&new->dgp, xcsf->cond->dargs);
38  graph_rand(&new->dgp);
39  c->cond = new;
40 }
41 
47 void
48 cond_dgp_free(const struct XCSF *xcsf, const struct Cl *c)
49 {
50  (void) xcsf;
51  const struct CondDGP *cond = c->cond;
52  graph_free(&cond->dgp);
53  free(c->cond);
54 }
55 
62 void
63 cond_dgp_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
64 {
65  struct CondDGP *new = malloc(sizeof(struct CondDGP));
66  const struct CondDGP *src_cond = src->cond;
67  graph_init(&new->dgp, xcsf->cond->dargs);
68  graph_copy(&new->dgp, &src_cond->dgp);
69  dest->cond = new;
70 }
71 
78 void
79 cond_dgp_cover(const struct XCSF *xcsf, const struct Cl *c, const double *x)
80 {
81  (void) xcsf;
82  struct CondDGP *cond = c->cond;
83  do {
84  graph_rand(&cond->dgp);
85  } while (!cond_dgp_match(xcsf, c, x));
86 }
87 
95 void
96 cond_dgp_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 
112 bool
113 cond_dgp_match(const struct XCSF *xcsf, const struct Cl *c, const double *x)
114 {
115  const struct CondDGP *cond = c->cond;
116  graph_update(&cond->dgp, x, !xcsf->STATEFUL);
117  if (graph_output(&cond->dgp, 0) > 0.5) {
118  return true;
119  }
120  return false;
121 }
122 
129 bool
130 cond_dgp_mutate(const struct XCSF *xcsf, const struct Cl *c)
131 {
132  (void) xcsf;
133  struct CondDGP *cond = c->cond;
134  return graph_mutate(&cond->dgp);
135 }
136 
144 bool
145 cond_dgp_crossover(const struct XCSF *xcsf, const struct Cl *c1,
146  const struct Cl *c2)
147 {
148  (void) xcsf;
149  (void) c1;
150  (void) c2;
151  return false;
152 }
153 
161 bool
162 cond_dgp_general(const struct XCSF *xcsf, const struct Cl *c1,
163  const struct Cl *c2)
164 {
165  (void) xcsf;
166  (void) c1;
167  (void) c2;
168  return false;
169 }
170 
176 void
177 cond_dgp_print(const struct XCSF *xcsf, const struct Cl *c)
178 {
179  char *json_str = cond_dgp_json_export(xcsf, c);
180  printf("%s\n", json_str);
181  free(json_str);
182 }
183 
190 double
191 cond_dgp_size(const struct XCSF *xcsf, const struct Cl *c)
192 {
193  (void) xcsf;
194  const struct CondDGP *cond = c->cond;
195  return cond->dgp.n;
196 }
197 
205 size_t
206 cond_dgp_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
207 {
208  (void) xcsf;
209  const struct CondDGP *cond = c->cond;
210  size_t s = graph_save(&cond->dgp, fp);
211  return s;
212 }
213 
221 size_t
222 cond_dgp_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
223 {
224  (void) xcsf;
225  struct CondDGP *new = malloc(sizeof(struct CondDGP));
226  size_t s = graph_load(&new->dgp, fp);
227  c->cond = new;
228  return s;
229 }
230 
237 char *
238 cond_dgp_json_export(const struct XCSF *xcsf, const struct Cl *c)
239 {
240  (void) xcsf;
241  const struct CondDGP *cond = c->cond;
242  cJSON *json = cJSON_CreateObject();
243  cJSON_AddStringToObject(json, "type", "dgp");
244  char *graph_str = graph_json_export(&cond->dgp);
245  cJSON *graph = cJSON_Parse(graph_str);
246  free(graph_str);
247  cJSON_AddItemToObject(json, "graph", graph);
248  char *string = cJSON_Print(json);
249  cJSON_Delete(json);
250  return string;
251 }
252 
259 void
260 cond_dgp_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
261 {
262  const cJSON *item = cJSON_GetObjectItem(json, "graph");
263  if (item == NULL) {
264  printf("Import error: missing graph\n");
265  exit(EXIT_FAILURE);
266  }
267  struct CondDGP *cond = c->cond;
268  graph_free(&cond->dgp);
269  graph_json_import(&cond->dgp, xcsf->cond->dargs, item);
270 }
271 
277 char *
279 {
280  return graph_args_json_export(xcsf->cond->dargs);
281 }
282 
289 char *
290 cond_dgp_param_json_import(struct XCSF *xcsf, cJSON *json)
291 {
292  return graph_args_json_import(xcsf->cond->dargs, json);
293 }
294 
299 void
301 {
302  struct ArgsDGP *args = malloc(sizeof(struct ArgsDGP));
303  graph_args_init(args);
304  graph_param_set_max_k(args, 2);
305  graph_param_set_max_t(args, 10);
306  graph_param_set_n(args, 10);
307  graph_param_set_n_inputs(args, xcsf->x_dim);
308  graph_param_set_evolve_cycles(args, true);
309  xcsf->cond->dargs = args;
310 }
void cond_dgp_print(const struct XCSF *xcsf, const struct Cl *c)
Prints a dynamical GP graph condition.
Definition: cond_dgp.c:177
size_t cond_dgp_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Reads a dynamical GP graph condition from a file.
Definition: cond_dgp.c:222
void cond_dgp_init(const struct XCSF *xcsf, struct Cl *c)
Creates and initialises a dynamical GP graph condition.
Definition: cond_dgp.c:34
bool cond_dgp_general(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Dummy general function.
Definition: cond_dgp.c:162
void cond_dgp_update(const struct XCSF *xcsf, const struct Cl *c, const double *x, const double *y)
Dummy update function.
Definition: cond_dgp.c:96
void cond_dgp_cover(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Generates a dynamical GP graph that matches the current input.
Definition: cond_dgp.c:79
bool cond_dgp_match(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Calculates whether a dynamical GP graph condition matches an input.
Definition: cond_dgp.c:113
bool cond_dgp_mutate(const struct XCSF *xcsf, const struct Cl *c)
Mutates a dynamical GP graph condition with the self-adaptive rates.
Definition: cond_dgp.c:130
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_free(const struct XCSF *xcsf, const struct Cl *c)
Frees the memory used by a dynamical GP graph condition.
Definition: cond_dgp.c:48
double cond_dgp_size(const struct XCSF *xcsf, const struct Cl *c)
Returns the size of a dynamical GP graph condition.
Definition: cond_dgp.c:191
void cond_dgp_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Copies a dynamical GP graph condition from one classifier to another.
Definition: cond_dgp.c:63
size_t cond_dgp_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Writes a dynamical GP graph condition to a file.
Definition: cond_dgp.c:206
void cond_dgp_param_defaults(struct XCSF *xcsf)
Initialises default DGP condition parameters.
Definition: cond_dgp.c:300
bool cond_dgp_crossover(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Dummy crossover function.
Definition: cond_dgp.c:145
void cond_dgp_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Creates a DGP condition from a cJSON object.
Definition: cond_dgp.c:260
char * cond_dgp_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the DGP parameters from a cJSON object.
Definition: cond_dgp.c:290
char * cond_dgp_json_export(const struct XCSF *xcsf, const struct Cl *c)
Returns a json formatted string representation of a DGP condition.
Definition: cond_dgp.c:238
Dynamical GP graph condition functions.
char * graph_args_json_export(const struct ArgsDGP *args)
Returns a json formatted string of the DGP parameters.
Definition: dgp.c:639
void graph_param_set_max_t(struct ArgsDGP *args, const int a)
Definition: dgp.c:730
void graph_copy(struct Graph *dest, const struct Graph *src)
Copies a DGP graph.
Definition: dgp.c:251
void graph_free(const struct Graph *dgp)
Frees a DGP graph.
Definition: dgp.c:523
void graph_rand(struct Graph *dgp)
Randomises a specified DGP graph.
Definition: dgp.c:296
void graph_param_set_n(struct ArgsDGP *args, const int a)
Definition: dgp.c:741
size_t graph_save(const struct Graph *dgp, FILE *fp)
Writes DGP graph to a file.
Definition: dgp.c:563
void graph_args_init(struct ArgsDGP *args)
Sets DGP parameters to default values.
Definition: dgp.c:624
void graph_json_import(struct Graph *dgp, const struct ArgsDGP *args, const cJSON *json)
Creates a DGP graph from a cJSON object.
Definition: dgp.c:477
void graph_update(const struct Graph *dgp, const double *inputs, const bool reset)
Updates a DGP graph T cycles.
Definition: dgp.c:318
void graph_init(struct Graph *dgp, const struct ArgsDGP *args)
Initialises a new DGP graph.
Definition: dgp.c:226
double graph_output(const struct Graph *dgp, const int IDX)
Returns the current state of a specified node in the graph.
Definition: dgp.c:274
void graph_param_set_n_inputs(struct ArgsDGP *args, const int a)
Definition: dgp.c:752
char * graph_json_export(const struct Graph *dgp)
Returns a json formatted string representation of a DGP graph.
Definition: dgp.c:346
void graph_param_set_max_k(struct ArgsDGP *args, const int a)
Definition: dgp.c:719
size_t graph_load(struct Graph *dgp, FILE *fp)
Reads DGP graph from a file.
Definition: dgp.c:588
char * graph_args_json_import(struct ArgsDGP *args, cJSON *json)
Sets the DGP graph parameters from a cJSON object.
Definition: dgp.c:658
void graph_param_set_evolve_cycles(struct ArgsDGP *args, const bool a)
Definition: dgp.c:763
bool graph_mutate(struct Graph *dgp)
Mutates a specified DGP graph.
Definition: dgp.c:540
Definition: __init__.py:1
Self-adaptive mutation functions.
Parameters for initialising DGP graphs.
Definition: dgp.h:31
Classifier data structure.
Definition: xcsf.h:45
void * cond
Condition structure.
Definition: xcsf.h:49
Dynamical GP graph condition data structure.
Definition: cond_dgp.h:33
struct Graph dgp
DGP graph.
Definition: cond_dgp.h:34
int n
Number of nodes.
Definition: dgp.h:53
XCSF data structure.
Definition: xcsf.h:85
Utility functions for random number handling, etc.