XCSF  1.4.7
XCSF learning classifier system
rule_neural.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_neural.h"
25 #include "neural_activations.h"
26 #include "neural_layer_connected.h"
27 #include "neural_layer_dropout.h"
28 #include "neural_layer_lstm.h"
29 #include "neural_layer_recurrent.h"
30 #include "utils.h"
31 
32 /* CONDITION FUNCTIONS */
33 
34 void
35 rule_neural_cond_init(const struct XCSF *xcsf, struct Cl *c)
36 {
37  struct RuleNeural *new = malloc(sizeof(struct RuleNeural));
38  neural_create(&new->net, xcsf->cond->largs);
39  const int expected = (int) fmax(1, ceil(log2(xcsf->n_actions))) + 1;
40  if (new->net.n_outputs != expected) {
41  printf("rule_neural_init(): n_outputs(%d) != expected(%d)\n",
42  new->net.n_outputs, expected);
43  printf("neural rules output binary actions + 1 matching neuron\n");
44  exit(EXIT_FAILURE);
45  }
46  c->cond = new;
47 }
48 
49 void
50 rule_neural_cond_free(const struct XCSF *xcsf, const struct Cl *c)
51 {
52  (void) xcsf;
53  struct RuleNeural *cond = c->cond;
54  neural_free(&cond->net);
55  free(c->cond);
56 }
57 
58 void
59 rule_neural_cond_copy(const struct XCSF *xcsf, struct Cl *dest,
60  const struct Cl *src)
61 {
62  (void) xcsf;
63  struct RuleNeural *new = malloc(sizeof(struct RuleNeural));
64  const struct RuleNeural *src_cond = src->cond;
65  neural_copy(&new->net, &src_cond->net);
66  dest->cond = new;
67 }
68 
69 void
70 rule_neural_cond_cover(const struct XCSF *xcsf, const struct Cl *c,
71  const double *x)
72 {
73  (void) xcsf;
74  (void) c;
75  (void) x;
76 }
77 
78 void
79 rule_neural_cond_update(const struct XCSF *xcsf, const struct Cl *c,
80  const double *x, const double *y)
81 {
82  (void) xcsf;
83  (void) c;
84  (void) x;
85  (void) y;
86 }
87 
88 bool
89 rule_neural_cond_match(const struct XCSF *xcsf, const struct Cl *c,
90  const double *x)
91 {
92  struct RuleNeural *cond = c->cond;
93  neural_propagate(&cond->net, x, xcsf->explore);
94  if (neural_output(&cond->net, 0) > 0.5) {
95  return true;
96  }
97  return false;
98 }
99 
100 bool
101 rule_neural_cond_mutate(const struct XCSF *xcsf, const struct Cl *c)
102 {
103  (void) xcsf;
104  const struct RuleNeural *cond = c->cond;
105  return neural_mutate(&cond->net);
106 }
107 
108 bool
109 rule_neural_cond_crossover(const struct XCSF *xcsf, const struct Cl *c1,
110  const struct Cl *c2)
111 {
112  (void) xcsf;
113  (void) c1;
114  (void) c2;
115  return false;
116 }
117 
118 bool
119 rule_neural_cond_general(const struct XCSF *xcsf, const struct Cl *c1,
120  const struct Cl *c2)
121 {
122  (void) xcsf;
123  (void) c1;
124  (void) c2;
125  return false;
126 }
127 
128 void
129 rule_neural_cond_print(const struct XCSF *xcsf, const struct Cl *c)
130 {
131  char *json_str = rule_neural_cond_json_export(xcsf, c);
132  printf("%s\n", json_str);
133  free(json_str);
134 }
135 
136 double
137 rule_neural_cond_size(const struct XCSF *xcsf, const struct Cl *c)
138 {
139  (void) xcsf;
140  const struct RuleNeural *cond = c->cond;
141  return neural_size(&cond->net);
142 }
143 
144 size_t
145 rule_neural_cond_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
146 {
147  (void) xcsf;
148  const struct RuleNeural *cond = c->cond;
149  size_t s = neural_save(&cond->net, fp);
150  return s;
151 }
152 
153 size_t
154 rule_neural_cond_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
155 {
156  (void) xcsf;
157  struct RuleNeural *new = malloc(sizeof(struct RuleNeural));
158  size_t s = neural_load(&new->net, fp);
159  c->cond = new;
160  return s;
161 }
162 
163 char *
164 rule_neural_cond_json_export(const struct XCSF *xcsf, const struct Cl *c)
165 {
166  (void) xcsf;
167  (void) c;
168  const struct RuleNeural *cond = c->cond;
169  cJSON *json = cJSON_CreateObject();
170  cJSON_AddStringToObject(json, "type", "rule_neural");
171  char *network_str = neural_json_export(&cond->net, false);
172  cJSON *network = cJSON_Parse(network_str);
173  free(network_str);
174  cJSON_AddItemToObject(json, "network", network);
175  char *string = cJSON_Print(json);
176  cJSON_Delete(json);
177  return string;
178 }
179 
180 void
181 rule_neural_cond_json_import(const struct XCSF *xcsf, struct Cl *c,
182  const cJSON *json)
183 {
184  const cJSON *item = cJSON_GetObjectItem(json, "network");
185  if (item == NULL) {
186  printf("Import error: missing network\n");
187  exit(EXIT_FAILURE);
188  }
189  struct RuleNeural *cond = c->cond;
190  neural_json_import(&cond->net, xcsf->cond->largs, item);
191 }
192 
193 /* ACTION FUNCTIONS */
194 
195 void
196 rule_neural_act_init(const struct XCSF *xcsf, struct Cl *c)
197 {
198  (void) xcsf;
199  (void) c;
200 }
201 
202 void
203 rule_neural_act_free(const struct XCSF *xcsf, const struct Cl *c)
204 {
205  (void) xcsf;
206  (void) c;
207 }
208 
209 void
210 rule_neural_act_copy(const struct XCSF *xcsf, struct Cl *dest,
211  const struct Cl *src)
212 {
213  (void) xcsf;
214  (void) dest;
215  (void) src;
216 }
217 
218 void
219 rule_neural_act_print(const struct XCSF *xcsf, const struct Cl *c)
220 {
221  char *json_str = rule_neural_act_json_export(xcsf, c);
222  printf("%s\n", json_str);
223  free(json_str);
224 }
225 
226 void
227 rule_neural_act_cover(const struct XCSF *xcsf, const struct Cl *c,
228  const double *x, const int action)
229 {
230  const struct RuleNeural *cond = c->cond;
231  do {
232  neural_rand(&cond->net);
233  } while (!rule_neural_cond_match(xcsf, c, x) &&
234  rule_neural_act_compute(xcsf, c, x) != action);
235 }
236 
237 int
238 rule_neural_act_compute(const struct XCSF *xcsf, const struct Cl *c,
239  const double *x)
240 {
241  (void) xcsf;
242  (void) x;
243  const struct RuleNeural *cond = c->cond;
244  int action = 0;
245  for (int i = 1; i < cond->net.n_outputs; ++i) {
246  if (neural_output(&cond->net, i) > 0.5) {
247  action += (int) pow(2, i - 1);
248  }
249  }
250  action = clamp_int(action, 0, xcsf->n_actions - 1);
251  return action;
252 }
253 
254 void
255 rule_neural_act_update(const struct XCSF *xcsf, const struct Cl *c,
256  const double *x, const double *y)
257 {
258  (void) xcsf;
259  (void) c;
260  (void) x;
261  (void) y;
262 }
263 
264 bool
265 rule_neural_act_crossover(const struct XCSF *xcsf, const struct Cl *c1,
266  const struct Cl *c2)
267 {
268  (void) xcsf;
269  (void) c1;
270  (void) c2;
271  return false;
272 }
273 
274 bool
275 rule_neural_act_general(const struct XCSF *xcsf, const struct Cl *c1,
276  const struct Cl *c2)
277 {
278  (void) xcsf;
279  (void) c1;
280  (void) c2;
281  return false;
282 }
283 
284 bool
285 rule_neural_act_mutate(const struct XCSF *xcsf, const struct Cl *c)
286 {
287  (void) xcsf;
288  (void) c;
289  return false;
290 }
291 
292 size_t
293 rule_neural_act_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
294 {
295  (void) xcsf;
296  (void) c;
297  (void) fp;
298  return 0;
299 }
300 
301 size_t
302 rule_neural_act_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
303 {
304  (void) xcsf;
305  (void) c;
306  (void) fp;
307  return 0;
308 }
309 
310 char *
311 rule_neural_act_json_export(const struct XCSF *xcsf, const struct Cl *c)
312 {
313  (void) xcsf;
314  (void) c;
315  cJSON *json = cJSON_CreateObject();
316  cJSON_AddStringToObject(json, "type", "rule_neural");
317  char *string = cJSON_Print(json);
318  cJSON_Delete(json);
319  return string;
320 }
321 
322 void
323 rule_neural_act_json_import(const struct XCSF *xcsf, struct Cl *c,
324  const cJSON *json)
325 {
326  (void) xcsf;
327  (void) c;
328  (void) json;
329 }
Definition: __init__.py:1
bool neural_mutate(const struct Net *net)
Mutates a neural network.
Definition: neural.c:257
double neural_size(const struct Net *net)
Returns the total number of non-zero weights in a neural network.
Definition: neural.c:450
void neural_create(struct Net *net, struct ArgsLayer *arg)
Initialises and creates a new neural network from a parameter list.
Definition: neural.c:54
double neural_output(const struct Net *net, const int IDX)
Returns the output of a specified neuron in the output layer of a neural network.
Definition: neural.c:369
size_t neural_load(struct Net *net, FILE *fp)
Reads a neural network from a file.
Definition: neural.c:499
void neural_json_import(struct Net *net, const struct ArgsLayer *arg, const cJSON *json)
Creates a neural network from a cJSON object.
Definition: neural.c:434
void neural_free(struct Net *net)
Frees a neural network.
Definition: neural.c:224
void neural_rand(const struct Net *net)
Randomises the layers within a neural network.
Definition: neural.c:242
void neural_copy(struct Net *dest, const struct Net *src)
Copies a neural network.
Definition: neural.c:208
void neural_propagate(struct Net *net, const double *input, const bool train)
Forward propagates a neural network.
Definition: neural.c:310
char * neural_json_export(const struct Net *net, const bool return_weights)
Returns a json formatted string representation of a neural network.
Definition: neural.c:407
size_t neural_save(const struct Net *net, FILE *fp)
Writes a neural network to a file.
Definition: neural.c:478
Neural network activation functions.
An implementation of a fully-connected layer of perceptrons.
An implementation of a dropout layer.
An implementation of a long short-term memory layer.
An implementation of a recurrent layer of perceptrons.
bool rule_neural_cond_match(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Definition: rule_neural.c:89
size_t rule_neural_cond_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Definition: rule_neural.c:154
bool rule_neural_act_crossover(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Definition: rule_neural.c:265
size_t rule_neural_cond_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Definition: rule_neural.c:145
char * rule_neural_act_json_export(const struct XCSF *xcsf, const struct Cl *c)
Definition: rule_neural.c:311
size_t rule_neural_act_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Definition: rule_neural.c:293
void rule_neural_act_init(const struct XCSF *xcsf, struct Cl *c)
Definition: rule_neural.c:196
void rule_neural_act_update(const struct XCSF *xcsf, const struct Cl *c, const double *x, const double *y)
Definition: rule_neural.c:255
bool rule_neural_cond_crossover(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Definition: rule_neural.c:109
void rule_neural_cond_print(const struct XCSF *xcsf, const struct Cl *c)
Definition: rule_neural.c:129
bool rule_neural_cond_mutate(const struct XCSF *xcsf, const struct Cl *c)
Definition: rule_neural.c:101
void rule_neural_cond_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Definition: rule_neural.c:181
void rule_neural_act_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Definition: rule_neural.c:323
void rule_neural_cond_free(const struct XCSF *xcsf, const struct Cl *c)
Definition: rule_neural.c:50
void rule_neural_act_free(const struct XCSF *xcsf, const struct Cl *c)
Definition: rule_neural.c:203
size_t rule_neural_act_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Definition: rule_neural.c:302
bool rule_neural_act_mutate(const struct XCSF *xcsf, const struct Cl *c)
Definition: rule_neural.c:285
void rule_neural_cond_update(const struct XCSF *xcsf, const struct Cl *c, const double *x, const double *y)
Definition: rule_neural.c:79
void rule_neural_act_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Definition: rule_neural.c:210
bool rule_neural_act_general(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Definition: rule_neural.c:275
char * rule_neural_cond_json_export(const struct XCSF *xcsf, const struct Cl *c)
Definition: rule_neural.c:164
bool rule_neural_cond_general(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Definition: rule_neural.c:119
void rule_neural_cond_init(const struct XCSF *xcsf, struct Cl *c)
Definition: rule_neural.c:35
int rule_neural_act_compute(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Definition: rule_neural.c:238
void rule_neural_cond_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Definition: rule_neural.c:59
double rule_neural_cond_size(const struct XCSF *xcsf, const struct Cl *c)
Definition: rule_neural.c:137
void rule_neural_cond_cover(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Definition: rule_neural.c:70
void rule_neural_act_cover(const struct XCSF *xcsf, const struct Cl *c, const double *x, const int action)
Definition: rule_neural.c:227
void rule_neural_act_print(const struct XCSF *xcsf, const struct Cl *c)
Definition: rule_neural.c:219
Neural network rule (condition + action) functions.
Classifier data structure.
Definition: xcsf.h:45
void * cond
Condition structure.
Definition: xcsf.h:49
int n_outputs
Number of network outputs.
Definition: neural.h:51
Neural network rule data structure.
Definition: rule_neural.h:34
struct Net net
Neural network.
Definition: rule_neural.h:35
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