XCSF  1.4.7
XCSF learning classifier system
cond_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 "cond_neural.h"
25 #include "neural_activations.h"
26 #include "neural_layer_connected.h"
28 #include "neural_layer_dropout.h"
29 #include "neural_layer_lstm.h"
30 #include "neural_layer_maxpool.h"
31 #include "neural_layer_noise.h"
32 #include "neural_layer_recurrent.h"
33 #include "neural_layer_softmax.h"
34 #include "utils.h"
35 
41 void
42 cond_neural_init(const struct XCSF *xcsf, struct Cl *c)
43 {
44  struct CondNeural *new = malloc(sizeof(struct CondNeural));
45  neural_create(&new->net, xcsf->cond->largs);
46  c->cond = new;
47 }
48 
54 void
55 cond_neural_free(const struct XCSF *xcsf, const struct Cl *c)
56 {
57  (void) xcsf;
58  struct CondNeural *cond = c->cond;
59  neural_free(&cond->net);
60  free(c->cond);
61 }
62 
69 void
70 cond_neural_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
71 {
72  (void) xcsf;
73  struct CondNeural *new = malloc(sizeof(struct CondNeural));
74  const struct CondNeural *src_cond = src->cond;
75  neural_copy(&new->net, &src_cond->net);
76  dest->cond = new;
77 }
78 
85 void
86 cond_neural_cover(const struct XCSF *xcsf, const struct Cl *c, const double *x)
87 {
88  const struct CondNeural *cond = c->cond;
89  do {
90  neural_rand(&cond->net);
91  } while (!cond_neural_match(xcsf, c, x));
92 }
93 
101 void
102 cond_neural_update(const struct XCSF *xcsf, const struct Cl *c, const double *x,
103  const double *y)
104 {
105  (void) xcsf;
106  (void) c;
107  (void) x;
108  (void) y;
109 }
110 
117 bool
118 cond_neural_match(const struct XCSF *xcsf, const struct Cl *c, const double *x)
119 {
120  struct CondNeural *cond = c->cond;
121  neural_propagate(&cond->net, x, xcsf->explore);
122  if (neural_output(&cond->net, 0) > 0.5) {
123  return true;
124  }
125  return false;
126 }
127 
134 bool
135 cond_neural_mutate(const struct XCSF *xcsf, const struct Cl *c)
136 {
137  (void) xcsf;
138  const struct CondNeural *cond = c->cond;
139  return neural_mutate(&cond->net);
140 }
141 
149 bool
150 cond_neural_crossover(const struct XCSF *xcsf, const struct Cl *c1,
151  const struct Cl *c2)
152 {
153  (void) xcsf;
154  (void) c1;
155  (void) c2;
156  return false;
157 }
158 
166 bool
167 cond_neural_general(const struct XCSF *xcsf, const struct Cl *c1,
168  const struct Cl *c2)
169 {
170  (void) xcsf;
171  (void) c1;
172  (void) c2;
173  return false;
174 }
175 
181 void
182 cond_neural_print(const struct XCSF *xcsf, const struct Cl *c)
183 {
184  char *json_str = cond_neural_json_export(xcsf, c);
185  printf("%s\n", json_str);
186  free(json_str);
187 }
188 
196 double
197 cond_neural_size(const struct XCSF *xcsf, const struct Cl *c)
198 {
199  (void) xcsf;
200  const struct CondNeural *cond = c->cond;
201  return neural_size(&cond->net);
202 }
203 
211 size_t
212 cond_neural_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
213 {
214  (void) xcsf;
215  const struct CondNeural *cond = c->cond;
216  size_t s = neural_save(&cond->net, fp);
217  return s;
218 }
219 
227 size_t
228 cond_neural_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
229 {
230  (void) xcsf;
231  struct CondNeural *new = malloc(sizeof(struct CondNeural));
232  size_t s = neural_load(&new->net, fp);
233  c->cond = new;
234  return s;
235 }
236 
244 int
245 cond_neural_neurons(const struct XCSF *xcsf, const struct Cl *c, int layer)
246 {
247  (void) xcsf;
248  const struct CondNeural *cond = c->cond;
249  const struct Net *net = &cond->net;
250  const struct Llist *iter = net->tail;
251  int i = 0;
252  while (iter != NULL) {
253  if (i == layer) {
254  if (iter->layer->type == CONVOLUTIONAL) {
255  return iter->layer->n_filters;
256  }
257  return iter->layer->n_outputs;
258  }
259  iter = iter->prev;
260  ++i;
261  }
262  return 0;
263 }
264 
272 int
273 cond_neural_connections(const struct XCSF *xcsf, const struct Cl *c, int layer)
274 {
275  (void) xcsf;
276  const struct CondNeural *cond = c->cond;
277  const struct Net *net = &cond->net;
278  const struct Llist *iter = net->tail;
279  int i = 0;
280  while (iter != NULL) {
281  if (i == layer) {
282  return iter->layer->n_active;
283  }
284  iter = iter->prev;
285  ++i;
286  }
287  return 0;
288 }
289 
296 int
297 cond_neural_layers(const struct XCSF *xcsf, const struct Cl *c)
298 {
299  (void) xcsf;
300  const struct CondNeural *cond = c->cond;
301  const struct Net *net = &cond->net;
302  return net->n_layers;
303 }
304 
311 char *
312 cond_neural_json_export(const struct XCSF *xcsf, const struct Cl *c)
313 {
314  (void) xcsf;
315  const struct CondNeural *cond = c->cond;
316  cJSON *json = cJSON_CreateObject();
317  cJSON_AddStringToObject(json, "type", "neural");
318  char *network_str = neural_json_export(&cond->net, false);
319  cJSON *network = cJSON_Parse(network_str);
320  free(network_str);
321  cJSON_AddItemToObject(json, "network", network);
322  char *string = cJSON_Print(json);
323  cJSON_Delete(json);
324  return string;
325 }
326 
333 void
334 cond_neural_json_import(const struct XCSF *xcsf, struct Cl *c,
335  const cJSON *json)
336 {
337  const cJSON *item = cJSON_GetObjectItem(json, "network");
338  if (item == NULL) {
339  printf("Import error: missing network\n");
340  exit(EXIT_FAILURE);
341  }
342  struct CondNeural *cond = c->cond;
343  neural_json_import(&cond->net, xcsf->cond->largs, item);
344 }
345 
352 char *
354 {
355  layer_args_free(&xcsf->cond->largs);
356  for (cJSON *iter = json; iter != NULL; iter = iter->next) {
357  struct ArgsLayer *larg = malloc(sizeof(struct ArgsLayer));
358  layer_args_init(larg);
359  larg->n_inputs = xcsf->x_dim;
360  char *ret = layer_args_json_import(larg, iter->child);
361  if (ret != NULL) {
362  return ret;
363  }
364  if (xcsf->cond->largs == NULL) {
365  xcsf->cond->largs = larg;
366  } else {
367  struct ArgsLayer *layer_iter = xcsf->cond->largs;
368  while (layer_iter->next != NULL) {
369  layer_iter = layer_iter->next;
370  }
371  layer_iter->next = larg;
372  }
373  }
374  layer_args_validate(xcsf->cond->largs);
375  return NULL;
376 }
377 
382 void
384 {
385  // hidden layer
386  struct ArgsLayer *args = malloc(sizeof(struct ArgsLayer));
387  layer_args_init(args);
388  args->type = CONNECTED;
389  args->n_inputs = xcsf->x_dim;
390  args->n_init = 10;
391  args->n_max = 100;
392  args->max_neuron_grow = 1;
393  args->function = LOGISTIC;
394  args->evolve_weights = true;
395  args->evolve_neurons = true;
396  args->evolve_connect = true;
397  xcsf->cond->largs = args;
398  // output layer
399  args->next = layer_args_copy(args);
400  args->next->function = LINEAR;
401  args->next->n_inputs = args->n_init;
402  args->next->n_max = 1;
403  args->next->evolve_neurons = false;
404 }
bool cond_neural_general(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Dummy general function.
Definition: cond_neural.c:167
bool cond_neural_crossover(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Dummy crossover function.
Definition: cond_neural.c:150
void cond_neural_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Creates a neural condition from a cJSON object.
Definition: cond_neural.c:334
int cond_neural_neurons(const struct XCSF *xcsf, const struct Cl *c, int layer)
Returns the number of neurons in a neural condition layer.
Definition: cond_neural.c:245
void cond_neural_init(const struct XCSF *xcsf, struct Cl *c)
Creates and initialises a neural network condition.
Definition: cond_neural.c:42
int cond_neural_layers(const struct XCSF *xcsf, const struct Cl *c)
Returns the number of layers within a neural network condition.
Definition: cond_neural.c:297
void cond_neural_free(const struct XCSF *xcsf, const struct Cl *c)
Frees the memory used by a neural network condition.
Definition: cond_neural.c:55
size_t cond_neural_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Writes a neural network condition to a file.
Definition: cond_neural.c:212
char * cond_neural_json_export(const struct XCSF *xcsf, const struct Cl *c)
Returns a json formatted string representation of a neural condition.
Definition: cond_neural.c:312
bool cond_neural_match(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Generates a neural network that matches the current input.
Definition: cond_neural.c:118
double cond_neural_size(const struct XCSF *xcsf, const struct Cl *c)
Returns the size of a neural network condition.
Definition: cond_neural.c:197
size_t cond_neural_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Reads a neural network condition from a file.
Definition: cond_neural.c:228
char * cond_neural_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the neural network parameters from a cJSON object.
Definition: cond_neural.c:353
void cond_neural_param_defaults(struct XCSF *xcsf)
Initialises default neural condition parameters.
Definition: cond_neural.c:383
void cond_neural_update(const struct XCSF *xcsf, const struct Cl *c, const double *x, const double *y)
Dummy update function.
Definition: cond_neural.c:102
int cond_neural_connections(const struct XCSF *xcsf, const struct Cl *c, int layer)
Returns the number of active connections in a neural condition layer.
Definition: cond_neural.c:273
bool cond_neural_mutate(const struct XCSF *xcsf, const struct Cl *c)
Mutates a neural network condition with the self-adaptive rates.
Definition: cond_neural.c:135
void cond_neural_cover(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Generates a neural network that matches the current input.
Definition: cond_neural.c:86
void cond_neural_print(const struct XCSF *xcsf, const struct Cl *c)
Prints a neural network condition.
Definition: cond_neural.c:182
void cond_neural_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Copies a neural network condition from one classifier to another.
Definition: cond_neural.c:70
Multi-layer perceptron neural network condition functions.
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.
#define LOGISTIC
Logistic [0,1].
#define LINEAR
Linear [-inf,inf].
#define CONVOLUTIONAL
Layer type convolutional.
Definition: neural_layer.h:36
#define CONNECTED
Layer type connected.
Definition: neural_layer.h:29
void layer_args_init(struct ArgsLayer *args)
Sets layer parameters to default values.
char * layer_args_json_import(struct ArgsLayer *args, cJSON *json)
Sets the layer parameters from a cJSON object.
void layer_args_free(struct ArgsLayer **largs)
Frees memory used by a list of layer parameters and points to NULL.
void layer_args_validate(struct ArgsLayer *args)
Checks network layer arguments are valid.
struct ArgsLayer * layer_args_copy(const struct ArgsLayer *src)
Creates and returns a copy of specified layer parameters.
An implementation of a fully-connected layer of perceptrons.
An implementation of a 2D convolutional layer.
An implementation of a dropout layer.
An implementation of a long short-term memory layer.
An implementation of a 2D maxpooling layer.
An implementation of a Gaussian noise adding layer.
An implementation of a recurrent layer of perceptrons.
An implementation of a softmax layer.
Parameters for initialising a neural network layer.
_Bool evolve_weights
Ability to evolve weights.
int n_init
Initial number of units / neurons / filters.
_Bool evolve_neurons
Ability to evolve number of units.
int function
Activation function.
int max_neuron_grow
Maximum number neurons to add per mutation event.
int n_max
Maximum number of units / neurons.
int n_inputs
Number of inputs.
int type
Layer type: CONNECTED, DROPOUT, etc.
struct ArgsLayer * next
Next layer parameters.
_Bool evolve_connect
Ability to evolve weight connectivity.
Classifier data structure.
Definition: xcsf.h:45
void * cond
Condition structure.
Definition: xcsf.h:49
Multi-layer perceptron neural network condition data structure.
Definition: cond_neural.h:35
struct Net net
Neural network.
Definition: cond_neural.h:36
int n_filters
Conv.
Definition: neural_layer.h:136
int n_outputs
Number of layer outputs.
Definition: neural_layer.h:91
int n_active
Number of active weights / connections.
Definition: neural_layer.h:96
int type
Layer type: CONNECTED, DROPOUT, etc.
Definition: neural_layer.h:74
Forward declaration of layer structure.
Definition: neural.h:39
struct Llist * prev
Pointer to the previous layer (forward)
Definition: neural.h:41
struct Layer * layer
Pointer to the layer data structure.
Definition: neural.h:40
Neural network data structure.
Definition: neural.h:48
int n_layers
Number of layers (hidden + output)
Definition: neural.h:49
struct Llist * tail
Pointer to the tail layer (first layer)
Definition: neural.h:54
XCSF data structure.
Definition: xcsf.h:85
Utility functions for random number handling, etc.