XCSF 1.4.8
XCSF learning classifier system
Loading...
Searching...
No Matches
action.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 ACT_TYPE_INVALID (-1)
29#define ACT_TYPE_INTEGER (0)
30#define ACT_TYPE_NEURAL (1)
31
32#define ACT_STRING_INTEGER ("integer\0")
33#define ACT_STRING_NEURAL ("neural\0")
34
35#define ACT_TYPE_OPTIONS "integer, neural"
36
40struct ArgsAct {
41 int type;
42 struct ArgsLayer *largs;
43};
44
45void
46action_set(const struct XCSF *xcsf, struct Cl *c);
47
48const char *
50
51int
52action_type_as_int(const char *type);
53
54void
56
57void
59
60char *
61action_param_json_import(struct XCSF *xcsf, cJSON *json);
62
63char *
65
66size_t
67action_param_save(const struct XCSF *xcsf, FILE *fp);
68
69size_t
70action_param_load(struct XCSF *xcsf, FILE *fp);
71
76struct ActVtbl {
77 bool (*act_impl_general)(const struct XCSF *xcsf, const struct Cl *c1,
78 const struct Cl *c2);
79 bool (*act_impl_crossover)(const struct XCSF *xcsf, const struct Cl *c1,
80 const struct Cl *c2);
81 bool (*act_impl_mutate)(const struct XCSF *xcsf, const struct Cl *c);
82 int (*act_impl_compute)(const struct XCSF *xcsf, const struct Cl *c,
83 const double *x);
84 void (*act_impl_copy)(const struct XCSF *xcsf, struct Cl *dest,
85 const struct Cl *src);
86 void (*act_impl_cover)(const struct XCSF *xcsf, const struct Cl *c,
87 const double *x, const int action);
88 void (*act_impl_free)(const struct XCSF *xcsf, const struct Cl *c);
89 void (*act_impl_init)(const struct XCSF *xcsf, struct Cl *c);
90 void (*act_impl_print)(const struct XCSF *xcsf, const struct Cl *c);
91 void (*act_impl_update)(const struct XCSF *xcsf, const struct Cl *c,
92 const double *x, const double *y);
93 size_t (*act_impl_save)(const struct XCSF *xcsf, const struct Cl *c,
94 FILE *fp);
95 size_t (*act_impl_load)(const struct XCSF *xcsf, struct Cl *c, FILE *fp);
96 char *(*act_impl_json_export)(const struct XCSF *xcsf, const struct Cl *c);
97 void (*act_impl_json_import)(const struct XCSF *xcsf, struct Cl *c,
98 const cJSON *json);
99};
100
108static inline size_t
109act_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
110{
111 return (*c->act_vptr->act_impl_save)(xcsf, c, fp);
112}
113
121static inline size_t
122act_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
123{
124 return (*c->act_vptr->act_impl_load)(xcsf, c, fp);
125}
126
134static inline bool
135act_general(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
136{
137 return (*c1->act_vptr->act_impl_general)(xcsf, c1, c2);
138}
139
147static inline bool
148act_crossover(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
149{
150 return (*c1->act_vptr->act_impl_crossover)(xcsf, c1, c2);
151}
152
159static inline bool
160act_mutate(const struct XCSF *xcsf, const struct Cl *c)
161{
162 return (*c->act_vptr->act_impl_mutate)(xcsf, c);
163}
164
172static inline int
173act_compute(const struct XCSF *xcsf, const struct Cl *c, const double *x)
174{
175 return (*c->act_vptr->act_impl_compute)(xcsf, c, x);
176}
177
184static inline void
185act_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
186{
187 (*src->act_vptr->act_impl_copy)(xcsf, dest, src);
188}
189
197static inline void
198act_cover(const struct XCSF *xcsf, const struct Cl *c, const double *x,
199 const int action)
200{
201 (*c->act_vptr->act_impl_cover)(xcsf, c, x, action);
202}
203
209static inline void
210act_free(const struct XCSF *xcsf, const struct Cl *c)
211{
212 (*c->act_vptr->act_impl_free)(xcsf, c);
213}
214
220static inline void
221act_init(const struct XCSF *xcsf, struct Cl *c)
222{
223 (*c->act_vptr->act_impl_init)(xcsf, c);
224}
225
231static inline void
232act_print(const struct XCSF *xcsf, const struct Cl *c)
233{
234 (*c->act_vptr->act_impl_print)(xcsf, c);
235}
236
244static inline void
245act_update(const struct XCSF *xcsf, const struct Cl *c, const double *x,
246 const double *y)
247{
248 (*c->act_vptr->act_impl_update)(xcsf, c, x, y);
249}
250
257static inline char *
258act_json_export(const struct XCSF *xcsf, const struct Cl *c)
259{
260 return (*c->act_vptr->act_impl_json_export)(xcsf, c);
261}
262
269static inline void
270act_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
271{
272 const cJSON *item = cJSON_GetObjectItem(json, "type");
273 if (item == NULL || !cJSON_IsString(item)) {
274 printf("action_json_import(): missing type\n");
275 exit(EXIT_FAILURE);
276 }
277 const char *type = item->valuestring;
278 if (action_type_as_int(type) != xcsf->act->type) {
279 printf("action_json_import(): mismatched type\n");
280 printf("XCSF type = %s, but imported type = %s\n",
281 action_type_as_string(xcsf->act->type), type);
282 exit(EXIT_FAILURE);
283 }
284 (*c->act_vptr->act_impl_json_import)(xcsf, c, json);
285}
286
287/* parameter setters */
288
289int
290action_param_set_type_string(struct XCSF *xcsf, const char *a);
291
292void
293action_param_set_type(struct XCSF *xcsf, const int a);
size_t action_param_load(struct XCSF *xcsf, FILE *fp)
Loads action parameters.
Definition action.c:170
static void act_init(const struct XCSF *xcsf, struct Cl *c)
Initialises a classifier's action.
Definition action.h:221
void action_param_set_type(struct XCSF *xcsf, const int a)
Definition action.c:202
static bool act_crossover(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Performs classifier action crossover.
Definition action.h:148
static void act_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Copies the action from one classifier to another.
Definition action.h:185
int action_type_as_int(const char *type)
Returns the integer representation of an action type given a name.
Definition action.c:75
static bool act_mutate(const struct XCSF *xcsf, const struct Cl *c)
Performs classifier action mutation.
Definition action.h:160
static void act_cover(const struct XCSF *xcsf, const struct Cl *c, const double *x, const int action)
Generates an action that matches the specified value.
Definition action.h:198
static size_t act_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Reads the action from a file.
Definition action.h:122
void action_set(const struct XCSF *xcsf, struct Cl *c)
Sets a classifier's action functions to the implementations.
Definition action.c:35
static size_t act_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Writes the action to a file.
Definition action.h:109
static int act_compute(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Computes the current classifier action using the input.
Definition action.h:173
static void act_print(const struct XCSF *xcsf, const struct Cl *c)
Prints the classifier action.
Definition action.h:232
static bool act_general(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Returns whether the action of classifier c1 is more general than c2.
Definition action.h:135
static void act_free(const struct XCSF *xcsf, const struct Cl *c)
Frees the memory used by the classifier action.
Definition action.h:210
static void act_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Creates an action from a cJSON object.
Definition action.h:270
size_t action_param_save(const struct XCSF *xcsf, FILE *fp)
Saves action parameters.
Definition action.c:154
int action_param_set_type_string(struct XCSF *xcsf, const char *a)
Definition action.c:192
char * action_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the action parameters from a cJSON object.
Definition action.c:131
void action_param_defaults(struct XCSF *xcsf)
Initialises default action parameters.
Definition action.c:91
char * action_param_json_export(const struct XCSF *xcsf)
Returns a json formatted string of the action parameters.
Definition action.c:103
static void act_update(const struct XCSF *xcsf, const struct Cl *c, const double *x, const double *y)
Updates the classifier's action.
Definition action.h:245
void action_param_free(struct XCSF *xcsf)
Frees action parameters.
Definition action.c:184
const char * action_type_as_string(const int type)
Returns a string representation of an action type from an integer.
Definition action.c:56
static char * act_json_export(const struct XCSF *xcsf, const struct Cl *c)
Returns a json formatted string representation of an action .
Definition action.h:258
Action interface data structure.
Definition action.h:76
void(* act_impl_print)(const struct XCSF *xcsf, const struct Cl *c)
Definition action.h:90
void(* act_impl_json_import)(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Definition action.h:97
size_t(* act_impl_load)(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Definition action.h:95
bool(* act_impl_general)(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Definition action.h:77
size_t(* act_impl_save)(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Definition action.h:93
bool(* act_impl_mutate)(const struct XCSF *xcsf, const struct Cl *c)
Definition action.h:81
void(* act_impl_update)(const struct XCSF *xcsf, const struct Cl *c, const double *x, const double *y)
Definition action.h:91
bool(* act_impl_crossover)(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Definition action.h:79
void(* act_impl_copy)(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Definition action.h:84
void(* act_impl_init)(const struct XCSF *xcsf, struct Cl *c)
Definition action.h:89
void(* act_impl_free)(const struct XCSF *xcsf, const struct Cl *c)
Definition action.h:88
int(* act_impl_compute)(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Definition action.h:82
char *(* act_impl_json_export)(const struct XCSF *xcsf, const struct Cl *c)
Definition action.h:96
void(* act_impl_cover)(const struct XCSF *xcsf, const struct Cl *c, const double *x, const int action)
Definition action.h:86
Parameters for initialising and operating actions.
Definition action.h:40
struct ArgsLayer * largs
Linked-list of layer parameters.
Definition action.h:42
int type
Classifier action type.
Definition action.h:41
Parameters for initialising a neural network layer.
int type
Layer type: CONNECTED, DROPOUT, etc.
Classifier data structure.
Definition xcsf.h:45
struct ActVtbl const * act_vptr
Functions acting on actions.
Definition xcsf.h:48
int action
Current classifier action.
Definition xcsf.h:60
XCSF data structure.
Definition xcsf.h:85
XCSF data structures.