XCSF 1.4.8
XCSF learning classifier system
Loading...
Searching...
No Matches
prediction.h
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#pragma once
25
26#include "xcsf.h"
27
28#define PRED_TYPE_INVALID (-1)
29#define PRED_TYPE_CONSTANT (0)
30#define PRED_TYPE_NLMS_LINEAR (1)
31#define PRED_TYPE_NLMS_QUADRATIC (2)
32#define PRED_TYPE_RLS_LINEAR (3)
33#define PRED_TYPE_RLS_QUADRATIC (4)
34#define PRED_TYPE_NEURAL (5)
35
36#define PRED_STRING_CONSTANT ("constant\0")
37#define PRED_STRING_NLMS_LINEAR ("nlms_linear\0")
38#define PRED_STRING_NLMS_QUADRATIC ("nlms_quadratic\0")
39#define PRED_STRING_RLS_LINEAR ("rls_linear\0")
40#define PRED_STRING_RLS_QUADRATIC ("rls_quadratic\0")
41#define PRED_STRING_NEURAL ("neural\0")
42
43#define PRED_TYPE_OPTIONS \
44 "constant, nlms_linear, nlms_quadratic, rls_linear, rls_quadratic, " \
45 "neural"
46
50struct ArgsPred {
51 int type;
53 double eta;
54 double eta_min;
55 double lambda;
56 double scale_factor;
57 double x0;
58 struct ArgsLayer *largs;
59};
60
61const char *
63
64int
65prediction_type_as_int(const char *type);
66
67size_t
68pred_param_load(struct XCSF *xcsf, FILE *fp);
69
70size_t
71pred_param_save(const struct XCSF *xcsf, FILE *fp);
72
73void
75
76void
78
79char *
80pred_param_json_import(struct XCSF *xcsf, cJSON *json);
81
82char *
83pred_param_json_export(const struct XCSF *xcsf);
84
85void
86pred_transform_input(const struct XCSF *xcsf, const double *x, const double X0,
87 double *tmp_input);
88
89void
90prediction_set(const struct XCSF *xcsf, struct Cl *c);
91
96struct PredVtbl {
97 bool (*pred_impl_crossover)(const struct XCSF *xcsf, const struct Cl *c1,
98 const struct Cl *c2);
99 bool (*pred_impl_mutate)(const struct XCSF *xcsf, const struct Cl *c);
100 void (*pred_impl_compute)(const struct XCSF *xcsf, const struct Cl *c,
101 const double *x);
102 void (*pred_impl_copy)(const struct XCSF *xcsf, struct Cl *dest,
103 const struct Cl *src);
104 void (*pred_impl_free)(const struct XCSF *xcsf, const struct Cl *c);
105 void (*pred_impl_init)(const struct XCSF *xcsf, struct Cl *c);
106 void (*pred_impl_print)(const struct XCSF *xcsf, const struct Cl *c);
107 void (*pred_impl_update)(const struct XCSF *xcsf, const struct Cl *c,
108 const double *x, const double *y);
109 double (*pred_impl_size)(const struct XCSF *xcsf, const struct Cl *c);
110 size_t (*pred_impl_save)(const struct XCSF *xcsf, const struct Cl *c,
111 FILE *fp);
112 size_t (*pred_impl_load)(const struct XCSF *xcsf, struct Cl *c, FILE *fp);
113 char *(*pred_impl_json_export)(const struct XCSF *xcsf, const struct Cl *c);
114 void (*pred_impl_json_import)(const struct XCSF *xcsf, struct Cl *c,
115 const cJSON *json);
116};
117
125static inline size_t
126pred_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
127{
128 return (*c->pred_vptr->pred_impl_save)(xcsf, c, fp);
129}
130
138static inline size_t
139pred_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
140{
141 return (*c->pred_vptr->pred_impl_load)(xcsf, c, fp);
142}
143
150static inline double
151pred_size(const struct XCSF *xcsf, const struct Cl *c)
152{
153 return (*c->pred_vptr->pred_impl_size)(xcsf, c);
154}
155
163static inline bool
164pred_crossover(const struct XCSF *xcsf, const struct Cl *c1,
165 const struct Cl *c2)
166{
167 return (*c1->pred_vptr->pred_impl_crossover)(xcsf, c1, c2);
168}
169
176static inline bool
177pred_mutate(const struct XCSF *xcsf, const struct Cl *c)
178{
179 return (*c->pred_vptr->pred_impl_mutate)(xcsf, c);
180}
181
188static inline void
189pred_compute(const struct XCSF *xcsf, const struct Cl *c, const double *x)
190{
191 (*c->pred_vptr->pred_impl_compute)(xcsf, c, x);
192}
193
200static inline void
201pred_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
202{
203 (*src->pred_vptr->pred_impl_copy)(xcsf, dest, src);
204}
205
211static inline void
212pred_free(const struct XCSF *xcsf, const struct Cl *c)
213{
214 (*c->pred_vptr->pred_impl_free)(xcsf, c);
215}
216
222static inline void
223pred_init(const struct XCSF *xcsf, struct Cl *c)
224{
225 (*c->pred_vptr->pred_impl_init)(xcsf, c);
226}
227
233static inline void
234pred_print(const struct XCSF *xcsf, const struct Cl *c)
235{
236 (*c->pred_vptr->pred_impl_print)(xcsf, c);
237}
238
247static inline void
248pred_update(const struct XCSF *xcsf, const struct Cl *c, const double *x,
249 const double *y)
250{
251 (*c->pred_vptr->pred_impl_update)(xcsf, c, x, y);
252}
253
260static inline char *
261pred_json_export(const struct XCSF *xcsf, const struct Cl *c)
262{
263 return (*c->pred_vptr->pred_impl_json_export)(xcsf, c);
264}
265
272static inline void
273pred_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
274{
275 const cJSON *item = cJSON_GetObjectItem(json, "type");
276 if (item == NULL || !cJSON_IsString(item)) {
277 printf("pred_json_import(): missing type\n");
278 exit(EXIT_FAILURE);
279 }
280 const char *type = item->valuestring;
281 if (prediction_type_as_int(type) != xcsf->pred->type) {
282 printf("pred_json_import(): mismatched type\n");
283 printf("XCSF type = %s, but imported type = %s\n",
284 prediction_type_as_string(xcsf->pred->type), type);
285 exit(EXIT_FAILURE);
286 }
287 (*c->pred_vptr->pred_impl_json_import)(xcsf, c, json);
288}
289
290/* parameter setters */
291
292void
293pred_param_set_eta(struct XCSF *xcsf, const double a);
294
295void
296pred_param_set_eta_min(struct XCSF *xcsf, const double a);
297
298void
299pred_param_set_lambda(struct XCSF *xcsf, const double a);
300
301void
302pred_param_set_scale_factor(struct XCSF *xcsf, const double a);
303
304void
305pred_param_set_x0(struct XCSF *xcsf, const double a);
306
307void
308pred_param_set_evolve_eta(struct XCSF *xcsf, const bool a);
309
310void
311pred_param_set_type(struct XCSF *xcsf, const int a);
312
313int
314pred_param_set_type_string(struct XCSF *xcsf, const char *a);
size_t pred_param_load(struct XCSF *xcsf, FILE *fp)
Loads prediction parameters.
Definition prediction.c:232
char * pred_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the prediction parameters from a cJSON object.
Definition prediction.c:179
void pred_param_free(struct XCSF *xcsf)
Frees prediction parameters.
Definition prediction.c:252
static double pred_size(const struct XCSF *xcsf, const struct Cl *c)
Returns the size of the classifier prediction.
Definition prediction.h:151
static char * pred_json_export(const struct XCSF *xcsf, const struct Cl *c)
Returns a json formatted string representation of a prediction.
Definition prediction.h:261
static void pred_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Creates a prediction from a cJSON object.
Definition prediction.h:273
static bool pred_mutate(const struct XCSF *xcsf, const struct Cl *c)
Performs classifier prediction mutation.
Definition prediction.h:177
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
static void pred_compute(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Computes the current classifier prediction using the input.
Definition prediction.h:189
static size_t pred_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Writes the prediction to a file.
Definition prediction.h:126
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
static void pred_update(const struct XCSF *xcsf, const struct Cl *c, const double *x, const double *y)
Updates the classifier's prediction.
Definition prediction.h:248
void prediction_set(const struct XCSF *xcsf, struct Cl *c)
Sets a classifier's prediction functions to the implementations.
Definition prediction.c:36
static void pred_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Copies the prediction from one classifier to another.
Definition prediction.h:201
static bool pred_crossover(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Performs classifier prediction crossover.
Definition prediction.h:164
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
static void pred_init(const struct XCSF *xcsf, struct Cl *c)
Initialises a classifier's prediction.
Definition prediction.h:223
char * pred_param_json_export(const struct XCSF *xcsf)
Returns a json formatted string of the prediction parameters.
Definition prediction.c:138
static void pred_print(const struct XCSF *xcsf, const struct Cl *c)
Prints the classifier prediction.
Definition prediction.h:234
static size_t pred_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Reads the prediction from a file.
Definition prediction.h:139
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
const char * prediction_type_as_string(const int type)
Returns a string representation of a prediction type from the integer.
Definition prediction.c:65
static void pred_free(const struct XCSF *xcsf, const struct Cl *c)
Frees the memory used by the classifier prediction.
Definition prediction.h:212
void pred_param_defaults(struct XCSF *xcsf)
Initialises default prediction parameters.
Definition prediction.c:120
Parameters for initialising a neural network layer.
int type
Layer type: CONNECTED, DROPOUT, etc.
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
Prediction interface data structure.
Definition prediction.h:96
bool(* pred_impl_mutate)(const struct XCSF *xcsf, const struct Cl *c)
Definition prediction.h:99
void(* pred_impl_update)(const struct XCSF *xcsf, const struct Cl *c, const double *x, const double *y)
Definition prediction.h:107
void(* pred_impl_print)(const struct XCSF *xcsf, const struct Cl *c)
Definition prediction.h:106
void(* pred_impl_init)(const struct XCSF *xcsf, struct Cl *c)
Definition prediction.h:105
bool(* pred_impl_crossover)(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Definition prediction.h:97
double(* pred_impl_size)(const struct XCSF *xcsf, const struct Cl *c)
Definition prediction.h:109
void(* pred_impl_copy)(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Definition prediction.h:102
void(* pred_impl_json_import)(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Definition prediction.h:114
size_t(* pred_impl_save)(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Definition prediction.h:110
char *(* pred_impl_json_export)(const struct XCSF *xcsf, const struct Cl *c)
Definition prediction.h:113
void(* pred_impl_free)(const struct XCSF *xcsf, const struct Cl *c)
Definition prediction.h:104
void(* pred_impl_compute)(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Definition prediction.h:100
size_t(* pred_impl_load)(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Definition prediction.h:112
XCSF data structure.
Definition xcsf.h:85
XCSF data structures.