XCSF 1.5.1
XCSF learning classifier system
Loading...
Searching...
No Matches
pred_constant.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 "pred_constant.h"
25#include "utils.h"
26
32void
33pred_constant_init(const struct XCSF *xcsf, struct Cl *c)
34{
35 (void) xcsf;
36 (void) c;
37}
38
45void
46pred_constant_copy(const struct XCSF *xcsf, struct Cl *dest,
47 const struct Cl *src)
48{
49 memcpy(dest->prediction, src->prediction, sizeof(double) * xcsf->y_dim);
50}
51
57void
58pred_constant_free(const struct XCSF *xcsf, const struct Cl *c)
59{
60 (void) xcsf;
61 (void) c;
62}
63
71void
72pred_constant_update(const struct XCSF *xcsf, const struct Cl *c,
73 const double *x, const double *y)
74{
75 (void) x;
76 if (c->exp * xcsf->BETA < 1) {
77 for (int i = 0; i < xcsf->y_dim; ++i) {
78 c->prediction[i] =
79 (c->prediction[i] * (c->exp - 1) + y[i]) / c->exp;
80 }
81 } else {
82 for (int i = 0; i < xcsf->y_dim; ++i) {
83 c->prediction[i] += xcsf->BETA * (y[i] - c->prediction[i]);
84 }
85 }
86}
87
94void
95pred_constant_compute(const struct XCSF *xcsf, const struct Cl *c,
96 const double *x)
97{
98 (void) xcsf;
99 (void) c;
100 (void) x;
101}
102
108void
109pred_constant_print(const struct XCSF *xcsf, const struct Cl *c)
110{
111 char *json_str = pred_constant_json_export(xcsf, c);
112 printf("%s\n", json_str);
113 free(json_str);
114}
115
123bool
124pred_constant_crossover(const struct XCSF *xcsf, const struct Cl *c1,
125 const struct Cl *c2)
126{
127 (void) xcsf;
128 (void) c1;
129 (void) c2;
130 return false;
131}
132
139bool
140pred_constant_mutate(const struct XCSF *xcsf, const struct Cl *c)
141{
142 (void) xcsf;
143 (void) c;
144 return false;
145}
146
153double
154pred_constant_size(const struct XCSF *xcsf, const struct Cl *c)
155{
156 (void) c;
157 return xcsf->y_dim;
158}
159
167size_t
168pred_constant_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
169{
170 (void) xcsf;
171 (void) c;
172 (void) fp;
173 return 0;
174}
175
183size_t
184pred_constant_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
185{
186 (void) xcsf;
187 (void) c;
188 (void) fp;
189 return 0;
190}
191
198char *
199pred_constant_json_export(const struct XCSF *xcsf, const struct Cl *c)
200{
201 cJSON *json = cJSON_CreateObject();
202 cJSON_AddStringToObject(json, "type", "constant");
203 cJSON *prediction = cJSON_CreateDoubleArray(c->prediction, xcsf->y_dim);
204 cJSON_AddItemToObject(json, "prediction", prediction);
205 char *string = cJSON_Print(json);
206 cJSON_Delete(json);
207 return string;
208}
209
216void
217pred_constant_json_import(const struct XCSF *xcsf, struct Cl *c,
218 const cJSON *json)
219{
220 const cJSON *item = cJSON_GetObjectItem(json, "prediction");
221 if (item != NULL && cJSON_IsArray(item)) {
222 if (cJSON_GetArraySize(item) == xcsf->y_dim) {
223 for (int i = 0; i < xcsf->y_dim; ++i) {
224 const cJSON *item_i = cJSON_GetArrayItem(item, i);
225 c->prediction[i] = item_i->valuedouble;
226 }
227 } else {
228 printf("Import error: prediction length mismatch\n");
229 exit(EXIT_FAILURE);
230 }
231 }
232}
size_t pred_constant_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Dummy function since constant predictions have no data structure.
void pred_constant_init(const struct XCSF *xcsf, struct Cl *c)
Dummy function since constant predictions have no data structure.
void pred_constant_free(const struct XCSF *xcsf, const struct Cl *c)
Dummy function since constant predictions have no data structure.
void pred_constant_compute(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Dummy function since constant predictions are not computed.
char * pred_constant_json_export(const struct XCSF *xcsf, const struct Cl *c)
Returns a json formatted string representation of a prediction.
void pred_constant_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Creates a constant prediction from a cJSON object.
bool pred_constant_crossover(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Dummy function since constant predictions do not perform crossover.
double pred_constant_size(const struct XCSF *xcsf, const struct Cl *c)
Returns the size of a constant prediction.
void pred_constant_print(const struct XCSF *xcsf, const struct Cl *c)
Prints a constant prediction.
void pred_constant_update(const struct XCSF *xcsf, const struct Cl *c, const double *x, const double *y)
Updates a constant prediction for a given input and truth sample.
bool pred_constant_mutate(const struct XCSF *xcsf, const struct Cl *c)
Dummy function since constant predictions do not perform mutation.
void pred_constant_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Dummy function since constant predictions have no data structure.
size_t pred_constant_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Dummy function since constant predictions have no data structure.
Piece-wise constant prediction functions.
Classifier data structure.
Definition xcsf.h:45
int exp
Experience.
Definition xcsf.h:55
double * prediction
Current classifier prediction.
Definition xcsf.h:59
XCSF data structure.
Definition xcsf.h:85
Utility functions for random number handling, etc.