XCSF  1.4.7
XCSF learning classifier system
xcsf.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 "utils.h"
27 #include <errno.h>
28 #include <float.h>
29 #include <inttypes.h>
30 #include <limits.h>
31 #include <math.h>
32 #include <stdbool.h>
33 #include <stdint.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 
38 static const int VERSION_MAJOR = 1;
39 static const int VERSION_MINOR = 4;
40 static const int VERSION_BUILD = 7;
41 
45 struct Cl {
46  struct CondVtbl const *cond_vptr;
47  struct PredVtbl const *pred_vptr;
48  struct ActVtbl const *act_vptr;
49  void *cond;
50  void *pred;
51  void *act;
52  double err;
53  double fit;
54  int num;
55  int exp;
56  double size;
57  int time;
58  bool m;
59  double *prediction;
60  int action;
61  int age;
62  int mtotal;
63 };
64 
68 struct Clist {
69  struct Cl *cl;
70  struct Clist *next;
71 };
72 
76 struct Set {
77  struct Clist *list;
78  int size;
79  int num;
80 };
81 
85 struct XCSF {
86  struct Set pset;
87  struct Set prev_pset;
88  struct Set mset;
89  struct Set aset;
90  struct Set kset;
91  struct Set prev_aset;
92  struct ArgsAct *act;
93  struct ArgsCond *cond;
94  struct ArgsPred *pred;
95  struct ArgsEA *ea;
96  struct EnvVtbl const *env_vptr;
97  void *env;
98  double error;
99  double mset_size;
100  double aset_size;
101  double mfrac;
102  double prev_reward;
103  double prev_pred;
104  double *pa;
105  double *nr;
106  double *prev_state;
107  double *cover;
108  int time;
109  int pa_size;
110  int x_dim;
111  int y_dim;
112  int n_actions;
113  bool explore;
114  double (*loss_ptr)(const struct XCSF *, const double *,
115  const double *);
116  double GAMMA;
117  double P_EXPLORE;
118  double ALPHA;
119  double BETA;
120  double DELTA;
121  double E0;
122  double INIT_ERROR;
123  double INIT_FITNESS;
124  double NU;
125  double HUBER_DELTA;
129  int POP_SIZE;
130  int LOSS_FUNC;
132  int THETA_DEL;
134  int THETA_SUB;
136  bool POP_INIT;
138  bool STATEFUL;
139  bool COMPACTION;
141 };
142 
146 struct Input {
147  double *x;
148  double *y;
149  int x_dim;
150  int y_dim;
151  int n_samples;
152 };
153 
154 size_t
155 xcsf_load(struct XCSF *xcsf, const char *filename);
156 
157 size_t
158 xcsf_save(const struct XCSF *xcsf, const char *filename);
159 
160 void
161 xcsf_free(struct XCSF *xcsf);
162 
163 void
164 xcsf_init(struct XCSF *xcsf);
165 
166 void
167 xcsf_print_pset(const struct XCSF *xcsf, const bool print_cond,
168  const bool print_act, const bool print_pred);
169 
170 void
171 xcsf_ae_to_classifier(struct XCSF *xcsf, const int y_dim, const int n_del);
172 
173 void
174 xcsf_pred_expand(const struct XCSF *xcsf);
175 
176 void
177 xcsf_retrieve_pset(struct XCSF *xcsf);
178 
179 void
180 xcsf_store_pset(struct XCSF *xcsf);
Definition: __init__.py:1
Action interface data structure.
Definition: action.h:76
Parameters for initialising and operating actions.
Definition: action.h:40
Parameters for initialising and operating conditions.
Definition: condition.h:60
Parameters for operating the evolutionary algorithm.
Definition: ea.h:40
Parameters for initialising and operating predictions.
Definition: prediction.h:50
Classifier data structure.
Definition: xcsf.h:45
int mtotal
Total number of times actually matched an input.
Definition: xcsf.h:62
void * act
Action structure.
Definition: xcsf.h:51
struct PredVtbl const * pred_vptr
Functions acting on predictions.
Definition: xcsf.h:47
void * pred
Prediction structure.
Definition: xcsf.h:50
int time
Time EA last executed in a participating set.
Definition: xcsf.h:57
void * cond
Condition structure.
Definition: xcsf.h:49
struct ActVtbl const * act_vptr
Functions acting on actions.
Definition: xcsf.h:48
double err
Error.
Definition: xcsf.h:52
int action
Current classifier action.
Definition: xcsf.h:60
bool m
Whether the classifier matches current input.
Definition: xcsf.h:58
int age
Total number of times match testing been performed.
Definition: xcsf.h:61
int num
Numerosity.
Definition: xcsf.h:54
int exp
Experience.
Definition: xcsf.h:55
double size
Average participated set size.
Definition: xcsf.h:56
double fit
Fitness.
Definition: xcsf.h:53
struct CondVtbl const * cond_vptr
Functions acting on conditions.
Definition: xcsf.h:46
double * prediction
Current classifier prediction.
Definition: xcsf.h:59
Classifier linked list.
Definition: xcsf.h:68
struct Clist * next
Pointer to the next list element.
Definition: xcsf.h:70
struct Cl * cl
Pointer to classifier data structure.
Definition: xcsf.h:69
Condition interface data structure.
Definition: condition.h:104
Built-in problem environment interface data structure.
Definition: env.h:35
Input data structure.
Definition: xcsf.h:146
double * x
Feature variables.
Definition: xcsf.h:147
int y_dim
Number of target variables.
Definition: xcsf.h:150
int x_dim
Number of feature variables.
Definition: xcsf.h:149
int n_samples
Number of instances.
Definition: xcsf.h:151
double * y
Target variables.
Definition: xcsf.h:148
Prediction interface data structure.
Definition: prediction.h:96
Classifier set.
Definition: xcsf.h:76
int size
Number of macro-classifiers.
Definition: xcsf.h:78
struct Clist * list
Linked list of classifiers.
Definition: xcsf.h:77
int num
The total numerosity of classifiers.
Definition: xcsf.h:79
XCSF data structure.
Definition: xcsf.h:85
int RANDOM_STATE
Random number seed.
Definition: xcsf.h:135
int M_PROBATION
Trials since creation a cl must match at least 1 input.
Definition: xcsf.h:133
double HUBER_DELTA
Delta parameter for Huber loss calculation.
Definition: xcsf.h:125
double * nr
Prediction array (stores total fitness)
Definition: xcsf.h:105
double mset_size
Average match set size.
Definition: xcsf.h:99
bool STATEFUL
Whether classifiers should retain state across trials.
Definition: xcsf.h:138
double INIT_FITNESS
Initial classifier fitness value.
Definition: xcsf.h:123
double INIT_ERROR
Initial classifier error value.
Definition: xcsf.h:122
struct Set prev_pset
Previously stored population set.
Definition: xcsf.h:87
double P_EXPLORE
Probability of exploring vs. exploiting.
Definition: xcsf.h:117
int x_dim
Number of problem input variables.
Definition: xcsf.h:110
double GAMMA
Discount factor for multi-step reward.
Definition: xcsf.h:116
int PERF_TRIALS
Number of problem instances to avg performance output.
Definition: xcsf.h:128
double * pa
Prediction array (stores fitness weighted predictions)
Definition: xcsf.h:104
bool explore
Whether the system is currently exploring or exploiting.
Definition: xcsf.h:113
struct ArgsAct * act
Action parameters.
Definition: xcsf.h:92
int TELETRANSPORTATION
Maximum steps for a multi-step problem.
Definition: xcsf.h:131
struct ArgsCond * cond
Condition parameters.
Definition: xcsf.h:93
double prev_reward
Reward from previous step in a multi-step trial.
Definition: xcsf.h:102
double aset_size
Average action set size.
Definition: xcsf.h:100
bool POP_INIT
Pop initially empty or filled with random conditions.
Definition: xcsf.h:136
int THETA_DEL
Min experience before fitness used during deletion.
Definition: xcsf.h:132
double NU
Exponent used in calculating classifier accuracy.
Definition: xcsf.h:124
int pa_size
Prediction array size.
Definition: xcsf.h:109
bool COMPACTION
if sys err < E0: largest of 2 roulette spins deleted
Definition: xcsf.h:139
int n_actions
Number of class labels / actions.
Definition: xcsf.h:112
char * population_file
Name of a JSON file containing an initial pop.
Definition: xcsf.h:140
double prev_pred
Payoff prediction made on the previous step.
Definition: xcsf.h:103
int POP_SIZE
Maximum number of micro-classifiers in the population.
Definition: xcsf.h:129
bool SET_SUBSUMPTION
Whether to perform match set subsumption.
Definition: xcsf.h:137
double DELTA
Fraction of population to increase deletion vote.
Definition: xcsf.h:120
struct Set pset
Population set.
Definition: xcsf.h:86
double * prev_state
Environment state on the previous step.
Definition: xcsf.h:106
double BETA
Learning rate for updating error, fitness, and set size.
Definition: xcsf.h:119
struct Set mset
Match set.
Definition: xcsf.h:88
int MAX_TRIALS
Number of problem instances to run in one experiment.
Definition: xcsf.h:127
struct Set kset
Kill set.
Definition: xcsf.h:90
struct ArgsPred * pred
Prediction parameters.
Definition: xcsf.h:94
double * cover
Values to return for a prediction instead of covering.
Definition: xcsf.h:107
double ALPHA
Linear coefficient used to calculate classifier accuracy.
Definition: xcsf.h:118
double error
Average system error.
Definition: xcsf.h:98
int OMP_NUM_THREADS
Number of threads for parallel processing.
Definition: xcsf.h:126
struct Set aset
Action set.
Definition: xcsf.h:89
struct Set prev_aset
Previous action set.
Definition: xcsf.h:91
int time
Current number of EA executions.
Definition: xcsf.h:108
double(* loss_ptr)(const struct XCSF *, const double *, const double *)
Error function.
Definition: xcsf.h:114
double mfrac
Generalisation measure.
Definition: xcsf.h:101
int LOSS_FUNC
Which loss/error function to apply.
Definition: xcsf.h:130
struct ArgsEA * ea
EA parameters.
Definition: xcsf.h:95
struct EnvVtbl const * env_vptr
Functions acting on environments.
Definition: xcsf.h:96
double E0
Target error under which classifier accuracy is set to 1.
Definition: xcsf.h:121
int THETA_SUB
Minimum experience of a classifier to become a subsumer.
Definition: xcsf.h:134
void * env
Environment structure (for built-in problems)
Definition: xcsf.h:97
int y_dim
Number of problem output variables.
Definition: xcsf.h:111
Utility functions for random number handling, etc.
static const int VERSION_MINOR
XCSF minor version number.
Definition: xcsf.h:39
void xcsf_store_pset(struct XCSF *xcsf)
Stores the current population.
Definition: xcsf.c:195
size_t xcsf_save(const struct XCSF *xcsf, const char *filename)
Writes the current state of XCSF to a file.
Definition: xcsf.c:90
static const int VERSION_MAJOR
XCSF major version number.
Definition: xcsf.h:38
void xcsf_retrieve_pset(struct XCSF *xcsf)
Retrieves the previously stored population.
Definition: xcsf.c:213
void xcsf_pred_expand(const struct XCSF *xcsf)
Inserts a new hidden layer before the output layer within all prediction neural networks in the popul...
Definition: xcsf.c:151
static const int VERSION_BUILD
XCSF build version number.
Definition: xcsf.h:40
void xcsf_ae_to_classifier(struct XCSF *xcsf, const int y_dim, const int n_del)
Switches from autoencoding to classification.
Definition: xcsf.c:171
size_t xcsf_load(struct XCSF *xcsf, const char *filename)
Reads the state of XCSF from a file.
Definition: xcsf.c:114
void xcsf_print_pset(const struct XCSF *xcsf, const bool print_cond, const bool print_act, const bool print_pred)
Prints the current XCSF population.
Definition: xcsf.c:77
void xcsf_init(struct XCSF *xcsf)
Initialises XCSF with an empty population.
Definition: xcsf.c:37
void xcsf_free(struct XCSF *xcsf)
Frees XCSF population sets.
Definition: xcsf.c:56