XCSF 1.4.8
XCSF learning classifier system
Loading...
Searching...
No Matches
xcsf.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 "cl.h"
25#include "clset.h"
26#include "cond_neural.h"
27#include "loss.h"
28#include "pa.h"
29#include "param.h"
30#include "pred_neural.h"
31
36void
38{
39 xcsf->time = 0;
40 xcsf->error = xcsf->E0;
41 xcsf->mset_size = 0;
42 xcsf->aset_size = 0;
43 xcsf->mfrac = 0;
44 xcsf->explore = false;
45 clset_init(&xcsf->pset);
46 clset_init(&xcsf->prev_pset);
49}
50
55void
57{
58 xcsf->time = 0;
59 xcsf->error = xcsf->E0;
60 xcsf->mset_size = 0;
61 xcsf->aset_size = 0;
62 xcsf->mfrac = 0;
63 xcsf->explore = false;
64 clset_kill(xcsf, &xcsf->pset);
65 clset_kill(xcsf, &xcsf->prev_pset);
67}
68
76void
77xcsf_print_pset(const struct XCSF *xcsf, const bool print_cond,
78 const bool print_act, const bool print_pred)
79{
80 clset_print(xcsf, &xcsf->pset, print_cond, print_act, print_pred);
81}
82
89size_t
90xcsf_save(const struct XCSF *xcsf, const char *filename)
91{
92 FILE *fp = fopen(filename, "wb");
93 if (fp == 0) {
94 printf("Error saving file: %s. %s.\n", filename, strerror(errno));
95 exit(EXIT_FAILURE);
96 }
97 size_t s = 0;
98 s += fwrite(&VERSION_MAJOR, sizeof(int), 1, fp);
99 s += fwrite(&VERSION_MINOR, sizeof(int), 1, fp);
100 s += fwrite(&VERSION_BUILD, sizeof(int), 1, fp);
101 s += param_save(xcsf, fp);
102 s += clset_pset_save(xcsf, fp);
103 fclose(fp);
104 return s;
105}
106
113size_t
114xcsf_load(struct XCSF *xcsf, const char *filename)
115{
116 if (xcsf->pset.size > 0) {
117 clset_kill(xcsf, &xcsf->pset);
118 clset_init(&xcsf->pset);
119 }
120 FILE *fp = fopen(filename, "rb");
121 if (fp == 0) {
122 printf("Error loading file: %s. %s.\n", filename, strerror(errno));
123 exit(EXIT_FAILURE);
124 }
125 size_t s = 0;
126 int major = 0;
127 int minor = 0;
128 int build = 0;
129 s += fread(&major, sizeof(int), 1, fp);
130 s += fread(&minor, sizeof(int), 1, fp);
131 s += fread(&build, sizeof(int), 1, fp);
132 if (major != VERSION_MAJOR || minor != VERSION_MINOR) {
133 printf("Error loading file: %s. Version mismatch. ", filename);
134 printf("This version: %d.%d\n", VERSION_MAJOR, VERSION_MINOR);
135 printf("Loaded version: %d.%d\n", major, minor);
136 fclose(fp);
137 exit(EXIT_FAILURE);
138 }
139 s += param_load(xcsf, fp);
140 s += clset_pset_load(xcsf, fp);
141 fclose(fp);
142 return s;
143}
144
150void
152{
153 const struct Clist *iter = xcsf->pset.list;
154 while (iter != NULL) {
155 pred_neural_expand(xcsf, iter->cl);
156 iter->cl->fit = xcsf->INIT_FITNESS;
157 iter->cl->err = xcsf->INIT_ERROR;
158 iter->cl->exp = 0;
159 iter->cl->time = xcsf->time;
160 iter = iter->next;
161 }
162}
163
170void
171xcsf_ae_to_classifier(struct XCSF *xcsf, const int y_dim, const int n_del)
172{
173 pa_free(xcsf);
174 param_set_y_dim(xcsf, y_dim);
176 pa_init(xcsf);
177 const struct Clist *iter = xcsf->pset.list;
178 while (iter != NULL) {
179 free(iter->cl->prediction);
180 iter->cl->prediction = calloc(xcsf->y_dim, sizeof(double));
181 pred_neural_ae_to_classifier(xcsf, iter->cl, n_del);
182 iter->cl->fit = xcsf->INIT_FITNESS;
183 iter->cl->err = xcsf->INIT_ERROR;
184 iter->cl->exp = 0;
185 iter->cl->time = xcsf->time;
186 iter = iter->next;
187 }
188}
189
194void
196{
197 clset_kill(xcsf, &xcsf->prev_pset);
198 const struct Clist *iter = xcsf->pset.list;
199 while (iter != NULL) {
200 struct Cl *new = malloc(sizeof(struct Cl));
201 const struct Cl *src = iter->cl;
202 cl_init_copy(xcsf, new, src);
203 clset_add(&xcsf->prev_pset, new);
204 iter = iter->next;
205 }
206}
207
212void
214{
215 if (xcsf->prev_pset.size < 1) {
216 printf("warning: xcsf_retrieve_pset() no previous population found\n");
217 return;
218 }
219 clset_kill(xcsf, &xcsf->pset);
220 xcsf->pset = xcsf->prev_pset;
221 clset_init(&xcsf->prev_pset);
222}
void cl_init_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Initialises and creates a copy of one classifier from another.
Definition cl.c:84
Functions operating on classifiers.
void clset_pset_init(struct XCSF *xcsf)
Initialises a new population of random classifiers.
Definition clset.c:308
void clset_kill(const struct XCSF *xcsf, struct Set *set)
Frees the set and the classifiers.
Definition clset.c:590
void clset_add(struct Set *set, struct Cl *c)
Adds a classifier to the set.
Definition clset.c:423
void clset_print(const struct XCSF *xcsf, const struct Set *set, const bool print_cond, const bool print_act, const bool print_pred)
Prints the classifiers in the set.
Definition clset.c:515
size_t clset_pset_save(const struct XCSF *xcsf, FILE *fp)
Writes the population set to a file.
Definition clset.c:610
void clset_init(struct Set *set)
Initialises a new set.
Definition clset.c:328
size_t clset_pset_load(struct XCSF *xcsf, FILE *fp)
Reads the population set from a file.
Definition clset.c:649
Functions operating on sets of classifiers.
Multi-layer perceptron neural network condition functions.
Loss functions for calculating prediction error.
#define LOSS_ONEHOT
One-hot encoding classification error.
Definition loss.h:34
void pa_free(const struct XCSF *xcsf)
Frees the prediction array.
Definition pa.c:192
void pa_init(struct XCSF *xcsf)
Initialises the prediction array.
Definition pa.c:46
Prediction array functions.
void param_set_loss_func(struct XCSF *xcsf, const int a)
Definition param.c:699
size_t param_load(struct XCSF *xcsf, FILE *fp)
Reads the XCSF data structure from a file.
Definition param.c:548
const char * param_set_y_dim(struct XCSF *xcsf, const int a)
Definition param.c:888
size_t param_save(const struct XCSF *xcsf, FILE *fp)
Writes the XCSF data structure to a file.
Definition param.c:495
Functions for setting and printing parameters.
void pred_neural_ae_to_classifier(const struct XCSF *xcsf, const struct Cl *c, const int n_del)
Removes prediction (decoder) layers and inserts softmax output layer.
void pred_neural_expand(const struct XCSF *xcsf, const struct Cl *c)
Creates and inserts a hidden layer before the prediction output layer.
Multi-layer perceptron neural network prediction functions.
Classifier data structure.
Definition xcsf.h:45
int time
Time EA last executed in a participating set.
Definition xcsf.h:57
double err
Error.
Definition xcsf.h:52
int exp
Experience.
Definition xcsf.h:55
double fit
Fitness.
Definition xcsf.h:53
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
XCSF data structure.
Definition xcsf.h:85
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
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
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
static const int VERSION_MINOR
XCSF minor version number.
Definition xcsf.h:39
static const int VERSION_MAJOR
XCSF major version number.
Definition xcsf.h:38
static const int VERSION_BUILD
XCSF build version number.
Definition xcsf.h:40