XCSF 1.4.8
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 (void) xcsf;
50 (void) dest;
51 (void) src;
52}
53
59void
60pred_constant_free(const struct XCSF *xcsf, const struct Cl *c)
61{
62 (void) xcsf;
63 (void) c;
64}
65
73void
74pred_constant_update(const struct XCSF *xcsf, const struct Cl *c,
75 const double *x, const double *y)
76{
77 (void) x;
78 if (c->exp * xcsf->BETA < 1) {
79 for (int i = 0; i < xcsf->y_dim; ++i) {
80 c->prediction[i] =
81 (c->prediction[i] * (c->exp - 1) + y[i]) / c->exp;
82 }
83 } else {
84 for (int i = 0; i < xcsf->y_dim; ++i) {
85 c->prediction[i] += xcsf->BETA * (y[i] - c->prediction[i]);
86 }
87 }
88}
89
96void
97pred_constant_compute(const struct XCSF *xcsf, const struct Cl *c,
98 const double *x)
99{
100 (void) xcsf;
101 (void) c;
102 (void) x;
103}
104
110void
111pred_constant_print(const struct XCSF *xcsf, const struct Cl *c)
112{
113 char *json_str = pred_constant_json_export(xcsf, c);
114 printf("%s\n", json_str);
115 free(json_str);
116}
117
125bool
126pred_constant_crossover(const struct XCSF *xcsf, const struct Cl *c1,
127 const struct Cl *c2)
128{
129 (void) xcsf;
130 (void) c1;
131 (void) c2;
132 return false;
133}
134
141bool
142pred_constant_mutate(const struct XCSF *xcsf, const struct Cl *c)
143{
144 (void) xcsf;
145 (void) c;
146 return false;
147}
148
155double
156pred_constant_size(const struct XCSF *xcsf, const struct Cl *c)
157{
158 (void) c;
159 return xcsf->y_dim;
160}
161
169size_t
170pred_constant_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
171{
172 (void) xcsf;
173 (void) c;
174 (void) fp;
175 return 0;
176}
177
185size_t
186pred_constant_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
187{
188 (void) xcsf;
189 (void) c;
190 (void) fp;
191 return 0;
192}
193
200char *
201pred_constant_json_export(const struct XCSF *xcsf, const struct Cl *c)
202{
203 cJSON *json = cJSON_CreateObject();
204 cJSON_AddStringToObject(json, "type", "constant");
205 cJSON *prediction = cJSON_CreateDoubleArray(c->prediction, xcsf->y_dim);
206 cJSON_AddItemToObject(json, "prediction", prediction);
207 char *string = cJSON_Print(json);
208 cJSON_Delete(json);
209 return string;
210}
211
218void
219pred_constant_json_import(const struct XCSF *xcsf, struct Cl *c,
220 const cJSON *json)
221{
222 const cJSON *item = cJSON_GetObjectItem(json, "prediction");
223 if (item != NULL && cJSON_IsArray(item)) {
224 if (cJSON_GetArraySize(item) == xcsf->y_dim) {
225 for (int i = 0; i < xcsf->y_dim; ++i) {
226 const cJSON *item_i = cJSON_GetArrayItem(item, i);
227 c->prediction[i] = item_i->valuedouble;
228 }
229 } else {
230 printf("Import error: prediction length mismatch\n");
231 exit(EXIT_FAILURE);
232 }
233 }
234}
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.