XCSF  1.4.7
XCSF learning classifier system
rule_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 "rule_dgp.h"
25 #include "utils.h"
26 
27 /* CONDITION FUNCTIONS */
28 
29 void
30 rule_dgp_cond_init(const struct XCSF *xcsf, struct Cl *c)
31 {
32  struct RuleDGP *new = malloc(sizeof(struct RuleDGP));
33  new->n_outputs = (int) fmax(1, ceil(log2(xcsf->n_actions)));
34  graph_init(&new->dgp, xcsf->cond->dargs);
35  graph_rand(&new->dgp);
36  c->cond = new;
37 }
38 
39 void
40 rule_dgp_cond_free(const struct XCSF *xcsf, const struct Cl *c)
41 {
42  (void) xcsf;
43  const struct RuleDGP *cond = c->cond;
44  graph_free(&cond->dgp);
45  free(c->cond);
46 }
47 
48 void
49 rule_dgp_cond_copy(const struct XCSF *xcsf, struct Cl *dest,
50  const struct Cl *src)
51 {
52  struct RuleDGP *new = malloc(sizeof(struct RuleDGP));
53  const struct RuleDGP *src_cond = src->cond;
54  graph_init(&new->dgp, xcsf->cond->dargs);
55  graph_copy(&new->dgp, &src_cond->dgp);
56  new->n_outputs = src_cond->n_outputs;
57  dest->cond = new;
58 }
59 
60 void
61 rule_dgp_cond_cover(const struct XCSF *xcsf, const struct Cl *c,
62  const double *x)
63 {
64  (void) xcsf;
65  (void) c;
66  (void) x;
67 }
68 
69 void
70 rule_dgp_cond_update(const struct XCSF *xcsf, const struct Cl *c,
71  const double *x, const double *y)
72 {
73  (void) xcsf;
74  (void) c;
75  (void) x;
76  (void) y;
77 }
78 
79 bool
80 rule_dgp_cond_match(const struct XCSF *xcsf, const struct Cl *c,
81  const double *x)
82 {
83  const struct RuleDGP *cond = c->cond;
84  graph_update(&cond->dgp, x, !xcsf->STATEFUL);
85  if (graph_output(&cond->dgp, 0) > 0.5) {
86  return true;
87  }
88  return false;
89 }
90 
91 bool
92 rule_dgp_cond_mutate(const struct XCSF *xcsf, const struct Cl *c)
93 {
94  (void) xcsf;
95  struct RuleDGP *cond = c->cond;
96  return graph_mutate(&cond->dgp);
97 }
98 
99 bool
100 rule_dgp_cond_crossover(const struct XCSF *xcsf, const struct Cl *c1,
101  const struct Cl *c2)
102 {
103  (void) xcsf;
104  (void) c1;
105  (void) c2;
106  return false;
107 }
108 
109 bool
110 rule_dgp_cond_general(const struct XCSF *xcsf, const struct Cl *c1,
111  const struct Cl *c2)
112 {
113  (void) xcsf;
114  (void) c1;
115  (void) c2;
116  return false;
117 }
118 
119 void
120 rule_dgp_cond_print(const struct XCSF *xcsf, const struct Cl *c)
121 {
122  char *json_str = rule_dgp_cond_json_export(xcsf, c);
123  printf("%s\n", json_str);
124  free(json_str);
125 }
126 
127 double
128 rule_dgp_cond_size(const struct XCSF *xcsf, const struct Cl *c)
129 {
130  (void) xcsf;
131  const struct RuleDGP *cond = c->cond;
132  return cond->dgp.n;
133 }
134 
135 size_t
136 rule_dgp_cond_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
137 {
138  (void) xcsf;
139  const struct RuleDGP *cond = c->cond;
140  size_t s = graph_save(&cond->dgp, fp);
141  return s;
142 }
143 
144 size_t
145 rule_dgp_cond_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
146 {
147  struct RuleDGP *new = malloc(sizeof(struct RuleDGP));
148  size_t s = graph_load(&new->dgp, fp);
149  new->n_outputs = (int) fmax(1, ceil(log2(xcsf->n_actions)));
150  c->cond = new;
151  return s;
152 }
153 
154 char *
155 rule_dgp_cond_json_export(const struct XCSF *xcsf, const struct Cl *c)
156 {
157  (void) xcsf;
158  const struct RuleDGP *cond = c->cond;
159  cJSON *json = cJSON_CreateObject();
160  cJSON_AddStringToObject(json, "type", "rule_dgp");
161  char *graph_str = graph_json_export(&cond->dgp);
162  cJSON *graph = cJSON_Parse(graph_str);
163  free(graph_str);
164  cJSON_AddItemToObject(json, "graph", graph);
165  char *string = cJSON_Print(json);
166  cJSON_Delete(json);
167  return string;
168 }
169 
170 void
171 rule_dgp_cond_json_import(const struct XCSF *xcsf, struct Cl *c,
172  const cJSON *json)
173 {
174  const cJSON *item = cJSON_GetObjectItem(json, "graph");
175  if (item == NULL) {
176  printf("Import error: missing graph\n");
177  exit(EXIT_FAILURE);
178  }
179  struct RuleDGP *cond = c->cond;
180  graph_free(&cond->dgp);
181  graph_json_import(&cond->dgp, xcsf->cond->dargs, item);
182 }
183 
184 /* ACTION FUNCTIONS */
185 
186 void
187 rule_dgp_act_init(const struct XCSF *xcsf, struct Cl *c)
188 {
189  (void) xcsf;
190  (void) c;
191 }
192 
193 void
194 rule_dgp_act_free(const struct XCSF *xcsf, const struct Cl *c)
195 {
196  (void) xcsf;
197  (void) c;
198 }
199 
200 void
201 rule_dgp_act_copy(const struct XCSF *xcsf, struct Cl *dest,
202  const struct Cl *src)
203 {
204  (void) xcsf;
205  (void) dest;
206  (void) src;
207 }
208 
209 void
210 rule_dgp_act_print(const struct XCSF *xcsf, const struct Cl *c)
211 {
212  char *json_str = rule_dgp_act_json_export(xcsf, c);
213  printf("%s\n", json_str);
214  free(json_str);
215 }
216 
217 void
218 rule_dgp_act_cover(const struct XCSF *xcsf, const struct Cl *c, const double *x,
219  const int action)
220 {
221  struct RuleDGP *cond = c->cond;
222  do {
223  graph_rand(&cond->dgp);
224  } while (!rule_dgp_cond_match(xcsf, c, x) &&
225  rule_dgp_act_compute(xcsf, c, x) != action);
226 }
227 
228 int
229 rule_dgp_act_compute(const struct XCSF *xcsf, const struct Cl *c,
230  const double *x)
231 {
232  (void) xcsf;
233  (void) x;
234  const struct RuleDGP *cond = c->cond;
235  int action = 0;
236  for (int i = 0; i < cond->n_outputs; ++i) {
237  if (graph_output(&cond->dgp, i + 1) > 0.5) {
238  action += (int) pow(2, i);
239  }
240  }
241  action = clamp_int(action, 0, xcsf->n_actions - 1);
242  return action;
243 }
244 
245 void
246 rule_dgp_act_update(const struct XCSF *xcsf, const struct Cl *c,
247  const double *x, const double *y)
248 {
249  (void) xcsf;
250  (void) c;
251  (void) x;
252  (void) y;
253 }
254 
255 bool
256 rule_dgp_act_crossover(const struct XCSF *xcsf, const struct Cl *c1,
257  const struct Cl *c2)
258 {
259  (void) xcsf;
260  (void) c1;
261  (void) c2;
262  return false;
263 }
264 
265 bool
266 rule_dgp_act_general(const struct XCSF *xcsf, const struct Cl *c1,
267  const struct Cl *c2)
268 {
269  (void) xcsf;
270  (void) c1;
271  (void) c2;
272  return false;
273 }
274 
275 bool
276 rule_dgp_act_mutate(const struct XCSF *xcsf, const struct Cl *c)
277 {
278  (void) xcsf;
279  (void) c;
280  return false;
281 }
282 
283 size_t
284 rule_dgp_act_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
285 {
286  (void) xcsf;
287  (void) c;
288  (void) fp;
289  return 0;
290 }
291 
292 size_t
293 rule_dgp_act_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
294 {
295  (void) xcsf;
296  (void) c;
297  (void) fp;
298  return 0;
299 }
300 
301 char *
302 rule_dgp_act_json_export(const struct XCSF *xcsf, const struct Cl *c)
303 {
304  (void) xcsf;
305  (void) c;
306  cJSON *json = cJSON_CreateObject();
307  cJSON_AddStringToObject(json, "type", "rule_dgp");
308  char *string = cJSON_Print(json);
309  cJSON_Delete(json);
310  return string;
311 }
312 
313 void
314 rule_dgp_act_json_import(const struct XCSF *xcsf, struct Cl *c,
315  const cJSON *json)
316 {
317  (void) xcsf;
318  (void) c;
319  (void) json;
320 }
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
size_t graph_save(const struct Graph *dgp, FILE *fp)
Writes DGP graph to a file.
Definition: dgp.c:563
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
char * graph_json_export(const struct Graph *dgp)
Returns a json formatted string representation of a DGP graph.
Definition: dgp.c:346
size_t graph_load(struct Graph *dgp, FILE *fp)
Reads DGP graph from a file.
Definition: dgp.c:588
bool graph_mutate(struct Graph *dgp)
Mutates a specified DGP graph.
Definition: dgp.c:540
Definition: __init__.py:1
bool rule_dgp_act_mutate(const struct XCSF *xcsf, const struct Cl *c)
Definition: rule_dgp.c:276
char * rule_dgp_act_json_export(const struct XCSF *xcsf, const struct Cl *c)
Definition: rule_dgp.c:302
void rule_dgp_cond_init(const struct XCSF *xcsf, struct Cl *c)
Definition: rule_dgp.c:30
char * rule_dgp_cond_json_export(const struct XCSF *xcsf, const struct Cl *c)
Definition: rule_dgp.c:155
bool rule_dgp_cond_mutate(const struct XCSF *xcsf, const struct Cl *c)
Definition: rule_dgp.c:92
void rule_dgp_cond_free(const struct XCSF *xcsf, const struct Cl *c)
Definition: rule_dgp.c:40
bool rule_dgp_cond_general(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Definition: rule_dgp.c:110
size_t rule_dgp_act_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Definition: rule_dgp.c:284
void rule_dgp_act_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Definition: rule_dgp.c:201
size_t rule_dgp_act_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Definition: rule_dgp.c:293
bool rule_dgp_act_general(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Definition: rule_dgp.c:266
void rule_dgp_cond_print(const struct XCSF *xcsf, const struct Cl *c)
Definition: rule_dgp.c:120
void rule_dgp_cond_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Definition: rule_dgp.c:49
bool rule_dgp_cond_crossover(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Definition: rule_dgp.c:100
void rule_dgp_act_init(const struct XCSF *xcsf, struct Cl *c)
Definition: rule_dgp.c:187
void rule_dgp_cond_update(const struct XCSF *xcsf, const struct Cl *c, const double *x, const double *y)
Definition: rule_dgp.c:70
void rule_dgp_act_update(const struct XCSF *xcsf, const struct Cl *c, const double *x, const double *y)
Definition: rule_dgp.c:246
bool rule_dgp_cond_match(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Definition: rule_dgp.c:80
void rule_dgp_act_print(const struct XCSF *xcsf, const struct Cl *c)
Definition: rule_dgp.c:210
void rule_dgp_act_free(const struct XCSF *xcsf, const struct Cl *c)
Definition: rule_dgp.c:194
bool rule_dgp_act_crossover(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Definition: rule_dgp.c:256
void rule_dgp_cond_cover(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Definition: rule_dgp.c:61
void rule_dgp_act_cover(const struct XCSF *xcsf, const struct Cl *c, const double *x, const int action)
Definition: rule_dgp.c:218
size_t rule_dgp_cond_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Definition: rule_dgp.c:136
int rule_dgp_act_compute(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Definition: rule_dgp.c:229
void rule_dgp_act_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Definition: rule_dgp.c:314
double rule_dgp_cond_size(const struct XCSF *xcsf, const struct Cl *c)
Definition: rule_dgp.c:128
size_t rule_dgp_cond_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Definition: rule_dgp.c:145
void rule_dgp_cond_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Definition: rule_dgp.c:171
Dynamical GP graph rule (condition + action) functions.
Classifier data structure.
Definition: xcsf.h:45
void * cond
Condition structure.
Definition: xcsf.h:49
int n
Number of nodes.
Definition: dgp.h:53
Dynamical GP graph rule data structure.
Definition: rule_dgp.h:34
struct Graph dgp
DGP graph.
Definition: rule_dgp.h:35
int n_outputs
Number of action nodes (binarised)
Definition: rule_dgp.h:36
XCSF data structure.
Definition: xcsf.h:85
Utility functions for random number handling, etc.
static int clamp_int(const int a, const int min, const int max)
Returns an integer clamped within the specified range.
Definition: utils.h:73