XCSF  1.4.7
XCSF learning classifier system
prediction.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 "pred_constant.h"
25 #include "pred_neural.h"
26 #include "pred_nlms.h"
27 #include "pred_rls.h"
28 #include "utils.h"
29 
35 void
36 prediction_set(const struct XCSF *xcsf, struct Cl *c)
37 {
38  switch (xcsf->pred->type) {
39  case PRED_TYPE_CONSTANT:
41  break;
45  break;
49  break;
50  case PRED_TYPE_NEURAL:
52  break;
53  default:
54  printf("prediction_set(): invalid type: %d\n", xcsf->pred->type);
55  exit(EXIT_FAILURE);
56  }
57 }
58 
64 const char *
66 {
67  switch (type) {
68  case PRED_TYPE_CONSTANT:
69  return PRED_STRING_CONSTANT;
78  case PRED_TYPE_NEURAL:
79  return PRED_STRING_NEURAL;
80  default:
81  printf("prediction_type_as_string(): invalid type: %d\n", type);
82  exit(EXIT_FAILURE);
83  }
84 }
85 
91 int
92 prediction_type_as_int(const char *type)
93 {
94  if (strncmp(type, PRED_STRING_CONSTANT, 9) == 0) {
95  return PRED_TYPE_CONSTANT;
96  }
97  if (strncmp(type, PRED_STRING_NLMS_LINEAR, 12) == 0) {
98  return PRED_TYPE_NLMS_LINEAR;
99  }
100  if (strncmp(type, PRED_STRING_NLMS_QUADRATIC, 15) == 0) {
102  }
103  if (strncmp(type, PRED_STRING_RLS_LINEAR, 11) == 0) {
104  return PRED_TYPE_RLS_LINEAR;
105  }
106  if (strncmp(type, PRED_STRING_RLS_QUADRATIC, 14) == 0) {
108  }
109  if (strncmp(type, PRED_STRING_NEURAL, 7) == 0) {
110  return PRED_TYPE_NEURAL;
111  }
112  return PRED_TYPE_INVALID;
113 }
114 
119 void
121 {
123  pred_param_set_eta(xcsf, 0.1);
124  pred_param_set_eta_min(xcsf, 0.00001);
130 }
131 
137 char *
139 {
140  const struct ArgsPred *pred = xcsf->pred;
141  cJSON *json = cJSON_CreateObject();
142  cJSON_AddStringToObject(json, "type",
144  char *json_str = NULL;
145  switch (pred->type) {
148  json_str = pred_nlms_param_json_export(xcsf);
149  break;
152  json_str = pred_rls_param_json_export(xcsf);
153  break;
154  case PRED_TYPE_NEURAL:
155  json_str = layer_args_json_export(xcsf->pred->largs);
156  break;
157  default:
158  break;
159  }
160  if (json_str != NULL) {
161  cJSON *params = cJSON_Parse(json_str);
162  if (params != NULL) {
163  cJSON_AddItemToObject(json, "args", params);
164  }
165  free(json_str);
166  }
167  char *string = cJSON_Print(json);
168  cJSON_Delete(json);
169  return string;
170 }
171 
178 char *
179 pred_param_json_import(struct XCSF *xcsf, cJSON *json)
180 {
181  char *ret = NULL;
182  switch (xcsf->pred->type) {
183  case PRED_TYPE_CONSTANT:
184  break;
187  ret = pred_nlms_param_json_import(xcsf, json->child);
188  break;
191  ret = pred_rls_param_json_import(xcsf, json->child);
192  break;
193  case PRED_TYPE_NEURAL:
194  ret = pred_neural_param_json_import(xcsf, json->child);
195  break;
196  default:
197  printf("pred_param_json_import(): unknown type.\n");
198  exit(EXIT_FAILURE);
199  }
200  return ret;
201 }
202 
209 size_t
210 pred_param_save(const struct XCSF *xcsf, FILE *fp)
211 {
212  const struct ArgsPred *pred = xcsf->pred;
213  size_t s = 0;
214  s += fwrite(&pred->type, sizeof(int), 1, fp);
215  s += fwrite(&pred->eta, sizeof(double), 1, fp);
216  s += fwrite(&pred->eta_min, sizeof(double), 1, fp);
217  s += fwrite(&pred->lambda, sizeof(double), 1, fp);
218  s += fwrite(&pred->scale_factor, sizeof(double), 1, fp);
219  s += fwrite(&pred->x0, sizeof(double), 1, fp);
220  s += fwrite(&pred->evolve_eta, sizeof(bool), 1, fp);
221  s += layer_args_save(pred->largs, fp);
222  return s;
223 }
224 
231 size_t
232 pred_param_load(struct XCSF *xcsf, FILE *fp)
233 {
234  struct ArgsPred *pred = xcsf->pred;
235  size_t s = 0;
236  s += fread(&pred->type, sizeof(int), 1, fp);
237  s += fread(&pred->eta, sizeof(double), 1, fp);
238  s += fread(&pred->eta_min, sizeof(double), 1, fp);
239  s += fread(&pred->lambda, sizeof(double), 1, fp);
240  s += fread(&pred->scale_factor, sizeof(double), 1, fp);
241  s += fread(&pred->x0, sizeof(double), 1, fp);
242  s += fread(&pred->evolve_eta, sizeof(bool), 1, fp);
243  s += layer_args_load(&pred->largs, fp);
244  return s;
245 }
246 
251 void
253 {
254  layer_args_free(&xcsf->pred->largs);
255 }
256 
264 void
265 pred_transform_input(const struct XCSF *xcsf, const double *x, const double X0,
266  double *tmp_input)
267 {
268  // bias term
269  tmp_input[0] = X0;
270  int idx = 1;
271  // linear terms
272  for (int i = 0; i < xcsf->x_dim; ++i) {
273  tmp_input[idx] = x[i];
274  ++idx;
275  }
276  // quadratic terms
277  if (xcsf->pred->type == PRED_TYPE_NLMS_QUADRATIC ||
278  xcsf->pred->type == PRED_TYPE_RLS_QUADRATIC) {
279  for (int i = 0; i < xcsf->x_dim; ++i) {
280  for (int j = i; j < xcsf->x_dim; ++j) {
281  tmp_input[idx] = x[i] * x[j];
282  ++idx;
283  }
284  }
285  }
286 }
287 
288 /* parameter setters */
289 
290 void
291 pred_param_set_eta(struct XCSF *xcsf, const double a)
292 {
293  if (a < 0) {
294  printf("Warning: tried to set PRED ETA too small\n");
295  xcsf->pred->eta = 0;
296  } else if (a > 1) {
297  printf("Warning: tried to set PRED ETA too large\n");
298  xcsf->pred->eta = 1;
299  } else {
300  xcsf->pred->eta = a;
301  }
302 }
303 
304 void
305 pred_param_set_eta_min(struct XCSF *xcsf, const double a)
306 {
307  if (a < 0) {
308  printf("Warning: tried to set PRED ETA_MIN too small\n");
309  xcsf->pred->eta_min = 0;
310  } else if (a > 1) {
311  printf("Warning: tried to set PRED ETA_MIN too large\n");
312  xcsf->pred->eta_min = 1;
313  } else {
314  xcsf->pred->eta_min = a;
315  }
316 }
317 
318 void
319 pred_param_set_lambda(struct XCSF *xcsf, const double a)
320 {
321  xcsf->pred->lambda = a;
322 }
323 
324 void
325 pred_param_set_scale_factor(struct XCSF *xcsf, const double a)
326 {
327  xcsf->pred->scale_factor = a;
328 }
329 
330 void
331 pred_param_set_x0(struct XCSF *xcsf, const double a)
332 {
333  xcsf->pred->x0 = a;
334 }
335 
336 void
337 pred_param_set_evolve_eta(struct XCSF *xcsf, const bool a)
338 {
339  xcsf->pred->evolve_eta = a;
340 }
341 
342 void
343 pred_param_set_type(struct XCSF *xcsf, const int a)
344 {
345  if (a < 0) {
346  printf("Warning: tried to set PRED TYPE too small\n");
347  xcsf->pred->type = 0;
348  } else {
349  xcsf->pred->type = a;
350  }
351 }
352 
353 int
354 pred_param_set_type_string(struct XCSF *xcsf, const char *a)
355 {
356  const int type = prediction_type_as_int(a);
357  if (type != PRED_TYPE_INVALID) {
358  xcsf->pred->type = type;
359  }
360  return type;
361 }
Definition: __init__.py:1
char * layer_args_json_export(struct ArgsLayer *args)
Returns a json formatted string of the neural layer parameters.
size_t layer_args_load(struct ArgsLayer **largs, FILE *fp)
Loads neural network layer parameters.
void layer_args_free(struct ArgsLayer **largs)
Frees memory used by a list of layer parameters and points to NULL.
size_t layer_args_save(const struct ArgsLayer *args, FILE *fp)
Saves neural network layer parameters.
Piece-wise constant prediction functions.
static struct PredVtbl const pred_constant_vtbl
Constant prediction implemented functions.
Definition: pred_constant.h:76
char * pred_neural_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the neural network parameters from a cJSON object.
Definition: pred_neural.c:465
void pred_neural_param_defaults(struct XCSF *xcsf)
Initialises default neural prediction parameters.
Definition: pred_neural.c:431
Multi-layer perceptron neural network prediction functions.
static struct PredVtbl const pred_neural_vtbl
Multi-layer perceptron neural network prediction implemented functions.
Definition: pred_neural.h:114
char * pred_nlms_param_json_export(const struct XCSF *xcsf)
Returns a json formatted string of the NLMS parameters.
Definition: pred_nlms.c:319
char * pred_nlms_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the NLMS parameters from a cJSON object.
Definition: pred_nlms.c:341
Normalised least mean squares prediction functions.
static struct PredVtbl const pred_nlms_vtbl
Normalised least mean squares prediction implemented functions.
Definition: pred_nlms.h:91
char * pred_rls_param_json_export(const struct XCSF *xcsf)
Returns a json formatted string of the RLS parameters.
Definition: pred_rls.c:323
char * pred_rls_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the RLS parameters from a cJSON object.
Definition: pred_rls.c:342
Recursive least mean squares prediction functions.
static struct PredVtbl const pred_rls_vtbl
Recursive least mean squares prediction implemented functions.
Definition: pred_rls.h:93
size_t pred_param_load(struct XCSF *xcsf, FILE *fp)
Loads prediction parameters.
Definition: prediction.c:232
void pred_param_free(struct XCSF *xcsf)
Frees prediction parameters.
Definition: prediction.c:252
const char * prediction_type_as_string(const int type)
Returns a string representation of a prediction type from the integer.
Definition: prediction.c:65
size_t pred_param_save(const struct XCSF *xcsf, FILE *fp)
Saves prediction parameters.
Definition: prediction.c:210
int prediction_type_as_int(const char *type)
Returns the integer representation of a prediction type given a name.
Definition: prediction.c:92
void pred_param_set_x0(struct XCSF *xcsf, const double a)
Definition: prediction.c:331
int pred_param_set_type_string(struct XCSF *xcsf, const char *a)
Definition: prediction.c:354
void pred_param_set_lambda(struct XCSF *xcsf, const double a)
Definition: prediction.c:319
char * pred_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the prediction parameters from a cJSON object.
Definition: prediction.c:179
void prediction_set(const struct XCSF *xcsf, struct Cl *c)
Sets a classifier's prediction functions to the implementations.
Definition: prediction.c:36
void pred_param_set_type(struct XCSF *xcsf, const int a)
Definition: prediction.c:343
void pred_param_set_eta_min(struct XCSF *xcsf, const double a)
Definition: prediction.c:305
void pred_param_set_evolve_eta(struct XCSF *xcsf, const bool a)
Definition: prediction.c:337
void pred_param_set_eta(struct XCSF *xcsf, const double a)
Definition: prediction.c:291
void pred_param_set_scale_factor(struct XCSF *xcsf, const double a)
Definition: prediction.c:325
void pred_transform_input(const struct XCSF *xcsf, const double *x, const double X0, double *tmp_input)
Prepares the input state for least squares computation.
Definition: prediction.c:265
void pred_param_defaults(struct XCSF *xcsf)
Initialises default prediction parameters.
Definition: prediction.c:120
char * pred_param_json_export(const struct XCSF *xcsf)
Returns a json formatted string of the prediction parameters.
Definition: prediction.c:138
#define PRED_TYPE_NLMS_LINEAR
Prediction type linear nlms.
Definition: prediction.h:30
#define PRED_STRING_NLMS_LINEAR
Linear nlms.
Definition: prediction.h:37
#define PRED_STRING_RLS_QUADRATIC
Quadratic rls.
Definition: prediction.h:40
#define PRED_TYPE_NEURAL
Prediction type neural.
Definition: prediction.h:34
#define PRED_TYPE_RLS_QUADRATIC
Prediction type quadratic rls.
Definition: prediction.h:33
#define PRED_STRING_CONSTANT
Constant.
Definition: prediction.h:36
#define PRED_STRING_NEURAL
Neural.
Definition: prediction.h:41
#define PRED_TYPE_NLMS_QUADRATIC
Prediction type quadratic nlms.
Definition: prediction.h:31
#define PRED_STRING_NLMS_QUADRATIC
Quadratic nlms.
Definition: prediction.h:38
#define PRED_TYPE_CONSTANT
Prediction type constant.
Definition: prediction.h:29
#define PRED_TYPE_INVALID
Error code for invalid prediction.
Definition: prediction.h:28
#define PRED_TYPE_RLS_LINEAR
Prediction type linear rls.
Definition: prediction.h:32
#define PRED_STRING_RLS_LINEAR
Linear rls.
Definition: prediction.h:39
Parameters for initialising and operating predictions.
Definition: prediction.h:50
double eta_min
Minimum gradient descent rate.
Definition: prediction.h:54
bool evolve_eta
Whether to evolve the gradient descent rate.
Definition: prediction.h:52
double scale_factor
Initial values for the RLS gain-matrix.
Definition: prediction.h:56
double x0
Prediction weight vector offset value.
Definition: prediction.h:57
struct ArgsLayer * largs
Linked-list of layer parameters.
Definition: prediction.h:58
int type
Classifier prediction type: least squares, etc.
Definition: prediction.h:51
double lambda
RLS forget rate.
Definition: prediction.h:55
double eta
Gradient descent rate.
Definition: prediction.h:53
Classifier data structure.
Definition: xcsf.h:45
struct PredVtbl const * pred_vptr
Functions acting on predictions.
Definition: xcsf.h:47
XCSF data structure.
Definition: xcsf.h:85
Utility functions for random number handling, etc.