XCSF 1.4.8
XCSF learning classifier system
Loading...
Searching...
No Matches
act_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 "act_neural.h"
25#include "neural_activations.h"
30#include "neural_layer_lstm.h"
32#include "neural_layer_noise.h"
36#include "utils.h"
37
43void
44act_neural_init(const struct XCSF *xcsf, struct Cl *c)
45{
46 struct ActNeural *new = malloc(sizeof(struct ActNeural));
47 neural_create(&new->net, xcsf->act->largs);
48 c->act = new;
49}
50
58bool
59act_neural_crossover(const struct XCSF *xcsf, const struct Cl *c1,
60 const struct Cl *c2)
61{
62 (void) xcsf;
63 (void) c1;
64 (void) c2;
65 return false;
66}
67
75bool
76act_neural_general(const struct XCSF *xcsf, const struct Cl *c1,
77 const struct Cl *c2)
78{
79 (void) xcsf;
80 (void) c1;
81 (void) c2;
82 return false;
83}
84
91bool
92act_neural_mutate(const struct XCSF *xcsf, const struct Cl *c)
93{
94 (void) xcsf;
95 const struct ActNeural *act = c->act;
96 return neural_mutate(&act->net);
97}
98
106int
107act_neural_compute(const struct XCSF *xcsf, const struct Cl *c, const double *x)
108{
109 struct ActNeural *act = c->act;
110 neural_propagate(&act->net, x, xcsf->explore);
111 const double *outputs = neural_outputs(&act->net);
112 return argmax(outputs, xcsf->n_actions);
113}
114
121void
122act_neural_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
123{
124 (void) xcsf;
125 struct ActNeural *new = malloc(sizeof(struct ActNeural));
126 const struct ActNeural *src_act = src->act;
127 neural_copy(&new->net, &src_act->net);
128 dest->act = new;
129}
130
136void
137act_neural_print(const struct XCSF *xcsf, const struct Cl *c)
138{
139 char *json_str = act_neural_json_export(xcsf, c);
140 printf("%s\n", json_str);
141 free(json_str);
142}
143
151void
152act_neural_cover(const struct XCSF *xcsf, const struct Cl *c, const double *x,
153 const int action)
154{
155 const struct ActNeural *act = c->act;
156 do {
157 neural_rand(&act->net);
158 } while (action != act_neural_compute(xcsf, c, x));
159}
160
166void
167act_neural_free(const struct XCSF *xcsf, const struct Cl *c)
168{
169 (void) xcsf;
170 struct ActNeural *act = c->act;
171 neural_free(&act->net);
172 free(c->act);
173}
174
182void
183act_neural_update(const struct XCSF *xcsf, const struct Cl *c, const double *x,
184 const double *y)
185{
186 (void) xcsf;
187 (void) c;
188 (void) x;
189 (void) y;
190}
191
199size_t
200act_neural_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
201{
202 (void) xcsf;
203 const struct ActNeural *act = c->act;
204 size_t s = neural_save(&act->net, fp);
205 return s;
206}
207
215size_t
216act_neural_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
217{
218 (void) xcsf;
219 struct ActNeural *new = malloc(sizeof(struct ActNeural));
220 size_t s = neural_load(&new->net, fp);
221 c->act = new;
222 return s;
223}
224
231char *
232act_neural_json_export(const struct XCSF *xcsf, const struct Cl *c)
233{
234 (void) xcsf;
235 const struct ActNeural *act = c->act;
236 cJSON *json = cJSON_CreateObject();
237 cJSON_AddStringToObject(json, "type", "neural");
238 char *network_str = neural_json_export(&act->net, false);
239 cJSON *network = cJSON_Parse(network_str);
240 free(network_str);
241 cJSON_AddItemToObject(json, "network", network);
242 char *string = cJSON_Print(json);
243 cJSON_Delete(json);
244 return string;
245}
246
253void
254act_neural_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
255{
256 const cJSON *item = cJSON_GetObjectItem(json, "network");
257 if (item == NULL) {
258 printf("Import error: missing network\n");
259 exit(EXIT_FAILURE);
260 }
261 struct ActNeural *act = c->act;
262 neural_json_import(&act->net, xcsf->act->largs, item);
263}
264
269void
271{
272 // hidden layer
273 struct ArgsLayer *la = malloc(sizeof(struct ArgsLayer));
274 layer_args_init(la);
275 la->type = CONNECTED;
276 la->n_inputs = xcsf->x_dim;
277 la->n_init = 1;
278 la->n_max = 100;
279 la->max_neuron_grow = 1;
280 la->function = LOGISTIC;
281 la->evolve_weights = true;
282 la->evolve_neurons = true;
283 la->evolve_connect = true;
284 xcsf->act->largs = la;
285 // softmax output layer
286 la->next = layer_args_copy(la);
287 la->next->function = LINEAR;
288 la->next->n_inputs = la->n_init;
289 la->next->n_init = xcsf->n_actions;
290 la->next->n_max = xcsf->n_actions;
291 la->next->evolve_neurons = false;
292 la->next->next = layer_args_copy(la->next);
293 la->next->next->n_inputs = la->next->n_init;
294 la->next->next->type = SOFTMAX;
295 la->next->next->scale = 1;
296}
297
304char *
306{
307 layer_args_free(&xcsf->act->largs);
308 for (cJSON *iter = json; iter != NULL; iter = iter->next) {
309 struct ArgsLayer *larg = malloc(sizeof(struct ArgsLayer));
310 layer_args_init(larg);
311 larg->n_inputs = xcsf->x_dim;
312 char *ret = layer_args_json_import(larg, iter->child);
313 if (ret != NULL) {
314 return ret;
315 }
316 if (xcsf->act->largs == NULL) {
317 xcsf->act->largs = larg;
318 } else {
319 struct ArgsLayer *layer_iter = xcsf->act->largs;
320 while (layer_iter->next != NULL) {
321 layer_iter = layer_iter->next;
322 }
323 layer_iter->next = larg;
324 }
325 }
326 layer_args_validate(xcsf->act->largs);
327 return NULL;
328}
char * act_neural_json_export(const struct XCSF *xcsf, const struct Cl *c)
Returns a json formatted string representation of a neural action.
Definition act_neural.c:232
size_t act_neural_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Reads a neural network action from a file.
Definition act_neural.c:216
bool act_neural_general(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Dummy function since neural actions do not generalise another.
Definition act_neural.c:76
void act_neural_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Creates a neural action from a cJSON object.
Definition act_neural.c:254
void act_neural_free(const struct XCSF *xcsf, const struct Cl *c)
Frees the memory used by a neural network action.
Definition act_neural.c:167
void act_neural_print(const struct XCSF *xcsf, const struct Cl *c)
Prints a neural network action.
Definition act_neural.c:137
void act_neural_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Copies a neural network action from one classifier to another.
Definition act_neural.c:122
void act_neural_update(const struct XCSF *xcsf, const struct Cl *c, const double *x, const double *y)
Dummy function since neural network actions are not updated.
Definition act_neural.c:183
char * act_neural_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the neural network parameters from a cJSON object.
Definition act_neural.c:305
int act_neural_compute(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Computes the current neural network action using the input.
Definition act_neural.c:107
void act_neural_cover(const struct XCSF *xcsf, const struct Cl *c, const double *x, const int action)
Generates a neural network that covers the specified input:action.
Definition act_neural.c:152
bool act_neural_crossover(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Dummy function since crossover is not performed on neural actions.
Definition act_neural.c:59
void act_neural_init(const struct XCSF *xcsf, struct Cl *c)
Creates and initialises an action neural network.
Definition act_neural.c:44
void act_neural_param_defaults(struct XCSF *xcsf)
Initialises default neural action parameters.
Definition act_neural.c:270
bool act_neural_mutate(const struct XCSF *xcsf, const struct Cl *c)
Mutates a neural network action.
Definition act_neural.c:92
size_t act_neural_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Writes a neural network action to a file.
Definition act_neural.c:200
Neural network action functions.
double * neural_outputs(const struct Net *net)
Returns the outputs from the output layer of a neural network.
Definition neural.c:384
bool neural_mutate(const struct Net *net)
Mutates a neural network.
Definition neural.c:257
void neural_create(struct Net *net, struct ArgsLayer *arg)
Initialises and creates a new neural network from a parameter list.
Definition neural.c:54
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_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
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 SOFTMAX
Layer type softmax.
#define CONNECTED
Layer type connected.
void layer_args_init(struct ArgsLayer *args)
Sets layer parameters to default values.
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.
char * layer_args_json_import(struct ArgsLayer *args, cJSON *json)
Sets the layer parameters from a cJSON object.
struct ArgsLayer * layer_args_copy(const struct ArgsLayer *src)
Creates and returns a copy of specified layer parameters.
An implementation of an average pooling layer.
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.
An implementation of a 2D upsampling layer.
Neural network action data structure.
Definition act_neural.h:35
struct Net net
Neural network.
Definition act_neural.h:36
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.
double scale
Usage depends on layer implementation.
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 * act
Action structure.
Definition xcsf.h:51
XCSF data structure.
Definition xcsf.h:85
Utility functions for random number handling, etc.
static int argmax(const double *X, const int N)
Returns the index of the largest element in vector X.
Definition utils.h:86