XCSF 1.4.8
XCSF learning classifier system
Loading...
Searching...
No Matches
xcs_supervised.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 "xcs_supervised.h"
25#include "clset.h"
26#include "ea.h"
27#include "loss.h"
28#include "pa.h"
29#include "param.h"
30#include "perf.h"
31#include "utils.h"
32
40static int
41xcs_supervised_sample(const struct Input *data, const int cnt,
42 const bool shuffle)
43{
44 if (shuffle) {
45 return rand_uniform_int(0, data->n_samples);
46 }
47 return cnt % data->n_samples;
48}
49
58static void
59xcs_supervised_trial(struct XCSF *xcsf, const double *x, const double *y,
60 const double *cover)
61{
62 clset_init(&xcsf->mset);
63 clset_init(&xcsf->kset);
64 if (cover != NULL) {
65 // no coverage is to be performed
66 clset_match(xcsf, x, false);
67 if (xcsf->mset.size < 1) {
68 // empty match set, return the specified array
69 memcpy(xcsf->pa, cover, sizeof(double) * xcsf->pa_size);
70 } else {
71 // non-empty match set, build prediction as usual
72 pa_build(xcsf, x);
73 }
74 } else {
75 // perform covering if required
76 clset_match(xcsf, x, true);
77 pa_build(xcsf, x);
78 }
79 if (xcsf->explore) {
80 clset_update(xcsf, &xcsf->mset, x, y, true);
81 ea(xcsf, &xcsf->mset);
82 }
83 clset_kill(xcsf, &xcsf->kset);
84 clset_free(&xcsf->mset);
85}
86
98double
99xcs_supervised_fit(struct XCSF *xcsf, const struct Input *train_data,
100 const struct Input *test_data, const bool shuffle,
101 const int start, const int trials)
102{
103 double err = 0; // training error: total over all trials
104 double werr = 0; // training error: windowed total
105 double wterr = 0; // testing error: windowed total
106 for (int cnt = 0; cnt < trials; ++cnt) {
107 // training sample
108 int row = xcs_supervised_sample(train_data, cnt + start, shuffle);
109 const double *x = &train_data->x[row * train_data->x_dim];
110 const double *y = &train_data->y[row * train_data->y_dim];
111 param_set_explore(xcsf, true);
112 xcs_supervised_trial(xcsf, x, y, NULL);
113 const double error = (xcsf->loss_ptr)(xcsf, xcsf->pa, y);
114 werr += error;
115 err += error;
116 xcsf->error += (error - xcsf->error) * xcsf->BETA;
117 // test sample
118 if (test_data != NULL) {
119 row = xcs_supervised_sample(test_data, cnt + start, shuffle);
120 x = &test_data->x[row * test_data->x_dim];
121 y = &test_data->y[row * test_data->y_dim];
122 param_set_explore(xcsf, false);
123 xcs_supervised_trial(xcsf, x, y, NULL);
124 wterr += (xcsf->loss_ptr)(xcsf, xcsf->pa, y);
125 }
126 perf_print(xcsf, &werr, &wterr, cnt);
127 }
128 return err / trials;
129}
130
140void
141xcs_supervised_predict(struct XCSF *xcsf, const double *x, double *pred,
142 const int n_samples, const double *cover)
143{
144 param_set_explore(xcsf, false);
145 for (int row = 0; row < n_samples; ++row) {
146 xcs_supervised_trial(xcsf, &x[row * xcsf->x_dim], NULL, cover);
147 memcpy(&pred[row * xcsf->pa_size], xcsf->pa,
148 sizeof(double) * xcsf->pa_size);
149 }
150}
151
160double
161xcs_supervised_score(struct XCSF *xcsf, const struct Input *data,
162 const double *cover)
163{
164 param_set_explore(xcsf, false);
165 double err = 0;
166 for (int row = 0; row < data->n_samples; ++row) {
167 const double *x = &data->x[row * data->x_dim];
168 const double *y = &data->y[row * data->y_dim];
169 xcs_supervised_trial(xcsf, x, y, cover);
170 err += (xcsf->loss_ptr)(xcsf, xcsf->pa, y);
171 }
172 return err / data->n_samples;
173}
174
184double
185xcs_supervised_score_n(struct XCSF *xcsf, const struct Input *data, const int N,
186 const double *cover)
187{
188 if (N > data->n_samples) {
189 return xcs_supervised_score(xcsf, data, cover);
190 }
191 param_set_explore(xcsf, false);
192 double err = 0;
193 for (int i = 0; i < N; ++i) {
194 const int row = xcs_supervised_sample(data, i, true);
195 const double *x = &data->x[row * data->x_dim];
196 const double *y = &data->y[row * data->y_dim];
197 xcs_supervised_trial(xcsf, x, y, cover);
198 err += (xcsf->loss_ptr)(xcsf, xcsf->pa, y);
199 }
200 return err / N;
201}
void clset_kill(const struct XCSF *xcsf, struct Set *set)
Frees the set and the classifiers.
Definition clset.c:590
void clset_update(struct XCSF *xcsf, struct Set *set, const double *x, const double *y, const bool cur)
Provides reinforcement to the set and performs set subsumption.
Definition clset.c:448
void clset_init(struct Set *set)
Initialises a new set.
Definition clset.c:328
void clset_match(struct XCSF *xcsf, const double *x, const bool cover)
Constructs the match set - forward propagates conditions and actions.
Definition clset.c:356
void clset_free(struct Set *set)
Frees the set, but not the classifiers.
Definition clset.c:572
Functions operating on sets of classifiers.
void ea(struct XCSF *xcsf, const struct Set *set)
Executes the evolutionary algorithm (EA).
Definition ea.c:199
Evolutionary algorithm functions.
Loss functions for calculating prediction error.
void pa_build(const struct XCSF *xcsf, const double *x)
Builds the prediction array for the specified input.
Definition pa.c:62
Prediction array functions.
const char * param_set_explore(struct XCSF *xcsf, const bool a)
Definition param.c:881
Functions for setting and printing parameters.
void perf_print(const struct XCSF *xcsf, double *error, double *terror, const int trial)
Displays the current training and test performance.
Definition perf.c:34
System performance printing.
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
XCSF data structure.
Definition xcsf.h:85
int rand_uniform_int(const int min, const int max)
Returns a uniform random integer [min,max] not inclusive of max.
Definition utils.c:74
Utility functions for random number handling, etc.
double xcs_supervised_fit(struct XCSF *xcsf, const struct Input *train_data, const struct Input *test_data, const bool shuffle, const int start, const int trials)
Executes MAX_TRIALS number of XCSF learning iterations using the training data and test iterations us...
double xcs_supervised_score(struct XCSF *xcsf, const struct Input *data, const double *cover)
Calculates the XCSF error for the input data.
static void xcs_supervised_trial(struct XCSF *xcsf, const double *x, const double *y, const double *cover)
Executes a single XCSF trial.
double xcs_supervised_score_n(struct XCSF *xcsf, const struct Input *data, const int N, const double *cover)
Calculates the XCSF error for a subsample of the input data.
static int xcs_supervised_sample(const struct Input *data, const int cnt, const bool shuffle)
Selects a data sample for training or testing.
void xcs_supervised_predict(struct XCSF *xcsf, const double *x, double *pred, const int n_samples, const double *cover)
Calculates the XCSF predictions for the provided input.
Supervised regression learning functions.