XCSF 1.4.8
XCSF learning classifier system
Loading...
Searching...
No Matches
param.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 "param.h"
25#include "action.h"
26#include "condition.h"
27#include "ea.h"
28#include "prediction.h"
29#include "utils.h"
30
31#ifdef PARALLEL
32 #include <omp.h>
33#endif
34
35#define MAX_LEN 512
36
44void
45param_init(struct XCSF *xcsf, const int x_dim, const int y_dim,
46 const int n_actions)
47{
48 xcsf->time = 0;
49 xcsf->error = xcsf->E0;
50 xcsf->mset_size = 0;
51 xcsf->aset_size = 0;
52 xcsf->mfrac = 0;
53 xcsf->ea = malloc(sizeof(struct ArgsEA));
54 xcsf->act = malloc(sizeof(struct ArgsAct));
55 xcsf->cond = malloc(sizeof(struct ArgsCond));
56 xcsf->pred = malloc(sizeof(struct ArgsPred));
57 xcsf->population_file = malloc(sizeof(char));
58 xcsf->population_file[0] = '\0';
59 param_set_n_actions(xcsf, n_actions);
60 param_set_x_dim(xcsf, x_dim);
61 param_set_y_dim(xcsf, y_dim);
70 param_set_e0(xcsf, 0.01);
73 param_set_beta(xcsf, 0.1);
81 param_set_gamma(xcsf, 0.95);
90}
91
92void
94{
95 if (xcsf->population_file != NULL) {
96 free(xcsf->population_file);
97 }
101 free(xcsf->ea);
102 free(xcsf->act);
103 free(xcsf->cond);
104 free(xcsf->pred);
105}
106
112char *
114{
115 char v[256];
116 snprintf(v, 256, "%d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_BUILD);
117 cJSON *json = cJSON_CreateObject();
118 cJSON_AddStringToObject(json, "version", v);
119 cJSON_AddNumberToObject(json, "x_dim", xcsf->x_dim);
120 cJSON_AddNumberToObject(json, "y_dim", xcsf->y_dim);
121 cJSON_AddNumberToObject(json, "n_actions", xcsf->n_actions);
122 cJSON_AddNumberToObject(json, "omp_num_threads", xcsf->OMP_NUM_THREADS);
123 cJSON_AddNumberToObject(json, "random_state", xcsf->RANDOM_STATE);
124 if (xcsf->population_file != NULL) {
125 cJSON_AddStringToObject(json, "population_file", xcsf->population_file);
126 }
127 cJSON_AddBoolToObject(json, "pop_init", xcsf->POP_INIT);
128 cJSON_AddNumberToObject(json, "max_trials", xcsf->MAX_TRIALS);
129 cJSON_AddNumberToObject(json, "perf_trials", xcsf->PERF_TRIALS);
130 cJSON_AddNumberToObject(json, "pop_size", xcsf->POP_SIZE);
131 cJSON_AddStringToObject(json, "loss_func",
132 loss_type_as_string(xcsf->LOSS_FUNC));
133 if (xcsf->LOSS_FUNC == LOSS_HUBER) {
134 cJSON_AddNumberToObject(json, "huber_delta", xcsf->HUBER_DELTA);
135 }
136 if (xcsf->n_actions > 1) {
137 cJSON_AddNumberToObject(json, "gamma", xcsf->GAMMA);
138 cJSON_AddNumberToObject(json, "teletransportation",
139 xcsf->TELETRANSPORTATION);
140 cJSON_AddNumberToObject(json, "p_explore", xcsf->P_EXPLORE);
141 }
142 cJSON_AddBoolToObject(json, "set_subsumption", xcsf->SET_SUBSUMPTION);
143 cJSON_AddNumberToObject(json, "theta_sub", xcsf->THETA_SUB);
144 cJSON_AddNumberToObject(json, "e0", xcsf->E0);
145 cJSON_AddNumberToObject(json, "alpha", xcsf->ALPHA);
146 cJSON_AddNumberToObject(json, "nu", xcsf->NU);
147 cJSON_AddNumberToObject(json, "beta", xcsf->BETA);
148 cJSON_AddNumberToObject(json, "delta", xcsf->DELTA);
149 cJSON_AddNumberToObject(json, "theta_del", xcsf->THETA_DEL);
150 cJSON_AddNumberToObject(json, "init_fitness", xcsf->INIT_FITNESS);
151 cJSON_AddNumberToObject(json, "init_error", xcsf->INIT_ERROR);
152 cJSON_AddNumberToObject(json, "m_probation", xcsf->M_PROBATION);
153 cJSON_AddBoolToObject(json, "stateful", xcsf->STATEFUL);
154 cJSON_AddBoolToObject(json, "compaction", xcsf->COMPACTION);
155 char *ea_param_str = ea_param_json_export(xcsf);
156 cJSON *ea_params = cJSON_Parse(ea_param_str);
157 cJSON_AddItemToObject(json, "ea", ea_params);
158 free(ea_param_str);
159 switch (xcsf->cond->type) {
160 case RULE_TYPE_DGP:
161 case RULE_TYPE_NEURAL:
163 break;
164 default:
165 if (xcsf->n_actions > 1) {
166 char *act_param_str = action_param_json_export(xcsf);
167 cJSON *act_params = cJSON_Parse(act_param_str);
168 cJSON_AddItemToObject(json, "action", act_params);
169 free(act_param_str);
170 }
171 break;
172 }
173 char *cond_param_str = cond_param_json_export(xcsf);
174 cJSON *cond_params = cJSON_Parse(cond_param_str);
175 cJSON_AddItemToObject(json, "condition", cond_params);
176 free(cond_param_str);
177 char *pred_param_str = pred_param_json_export(xcsf);
178 cJSON *pred_params = cJSON_Parse(pred_param_str);
179 cJSON_AddItemToObject(json, "prediction", pred_params);
180 free(pred_param_str);
181 char *string = cJSON_Print(json);
182 cJSON_Delete(json);
183 return string;
184}
185
192static bool
193param_json_import_general(struct XCSF *xcsf, const cJSON *json)
194{
195 if (strncmp(json->string, "version\0", 8) == 0) {
196 return true;
197 } else if (strncmp(json->string, "x_dim\0", 6) == 0 &&
198 cJSON_IsNumber(json)) {
199 catch_error(param_set_x_dim(xcsf, json->valueint));
200 } else if (strncmp(json->string, "y_dim\0", 6) == 0 &&
201 cJSON_IsNumber(json)) {
202 catch_error(param_set_y_dim(xcsf, json->valueint));
203 } else if (strncmp(json->string, "n_actions\0", 10) == 0 &&
204 cJSON_IsNumber(json)) {
205 catch_error(param_set_n_actions(xcsf, json->valueint));
206 } else if (strncmp(json->string, "omp_num_threads\0", 16) == 0 &&
207 cJSON_IsNumber(json)) {
209 } else if (strncmp(json->string, "random_state\0", 13) == 0 &&
210 cJSON_IsNumber(json)) {
211 catch_error(param_set_random_state(xcsf, json->valueint));
212 } else if (strncmp(json->string, "population_file\0", 16) == 0 &&
213 cJSON_IsString(json)) {
214 catch_error(param_set_population_file(xcsf, json->valuestring));
215 } else if (strncmp(json->string, "pop_size\0", 9) == 0 &&
216 cJSON_IsNumber(json)) {
217 catch_error(param_set_pop_size(xcsf, json->valueint));
218 } else if (strncmp(json->string, "max_trials\0", 11) == 0 &&
219 cJSON_IsNumber(json)) {
220 catch_error(param_set_max_trials(xcsf, json->valueint));
221 } else if (strncmp(json->string, "pop_init\0", 9) == 0 &&
222 cJSON_IsBool(json)) {
223 const bool init = true ? json->type == cJSON_True : false;
225 } else if (strncmp(json->string, "perf_trials\0", 12) == 0 &&
226 cJSON_IsNumber(json)) {
227 catch_error(param_set_perf_trials(xcsf, json->valueint));
228 } else if (strncmp(json->string, "loss_func\0", 10) == 0 &&
229 cJSON_IsString(json)) {
230 if (param_set_loss_func_string(xcsf, json->valuestring) ==
232 printf("Invalid loss function: %s\n", json->valuestring);
233 printf("Options: {%s}\n", LOSS_OPTIONS);
234 exit(EXIT_FAILURE);
235 }
236 } else if (strncmp(json->string, "huber_delta\0", 12) == 0 &&
237 cJSON_IsNumber(json)) {
238 catch_error(param_set_huber_delta(xcsf, json->valuedouble));
239 } else {
240 return false;
241 }
242 return true;
243}
244
252static bool
253param_json_import_multi(struct XCSF *xcsf, const cJSON *json)
254{
255 if (strncmp(json->string, "teletransportation\0", 19) == 0 &&
256 cJSON_IsNumber(json)) {
258 } else if (strncmp(json->string, "gamma\0", 6) == 0 &&
259 cJSON_IsNumber(json)) {
260 catch_error(param_set_gamma(xcsf, json->valuedouble));
261 } else if (strncmp(json->string, "p_explore\0", 10) == 0 &&
262 cJSON_IsNumber(json)) {
263 catch_error(param_set_p_explore(xcsf, json->valuedouble));
264 } else {
265 return false;
266 }
267 return true;
268}
269
277static bool
278param_json_import_subsump(struct XCSF *xcsf, const cJSON *json)
279{
280 if (strncmp(json->string, "set_subsumption\0", 16) == 0 &&
281 cJSON_IsBool(json)) {
282 const bool sub = true ? json->type == cJSON_True : false;
284 } else if (strncmp(json->string, "theta_sub\0", 10) == 0 &&
285 cJSON_IsNumber(json)) {
286 catch_error(param_set_theta_sub(xcsf, json->valueint));
287 } else {
288 return false;
289 }
290 return true;
291}
292
300static bool
301param_json_import_cl_general(struct XCSF *xcsf, const cJSON *json)
302{
303 if (strncmp(json->string, "alpha\0", 6) == 0 && cJSON_IsNumber(json)) {
304 catch_error(param_set_alpha(xcsf, json->valuedouble));
305 } else if (strncmp(json->string, "beta\0", 5) == 0 &&
306 cJSON_IsNumber(json)) {
307 catch_error(param_set_beta(xcsf, json->valuedouble));
308 } else if (strncmp(json->string, "delta\0", 6) == 0 &&
309 cJSON_IsNumber(json)) {
310 catch_error(param_set_delta(xcsf, json->valuedouble));
311 } else if (strncmp(json->string, "nu\0", 3) == 0 && cJSON_IsNumber(json)) {
312 catch_error(param_set_nu(xcsf, json->valuedouble));
313 } else if (strncmp(json->string, "theta_del\0", 10) == 0 &&
314 cJSON_IsNumber(json)) {
315 catch_error(param_set_theta_del(xcsf, json->valueint));
316 } else if (strncmp(json->string, "init_fitness\0", 13) == 0 &&
317 cJSON_IsNumber(json)) {
318 catch_error(param_set_init_fitness(xcsf, json->valuedouble));
319 } else if (strncmp(json->string, "init_error\0", 11) == 0 &&
320 cJSON_IsNumber(json)) {
321 catch_error(param_set_init_error(xcsf, json->valuedouble));
322 } else if (strncmp(json->string, "e0\0", 3) == 0 && cJSON_IsNumber(json)) {
323 catch_error(param_set_e0(xcsf, json->valuedouble));
324 } else if (strncmp(json->string, "m_probation\0", 12) == 0 &&
325 cJSON_IsNumber(json)) {
326 catch_error(param_set_m_probation(xcsf, json->valueint));
327 } else if (strncmp(json->string, "stateful\0", 9) == 0 &&
328 cJSON_IsBool(json)) {
329 const bool stateful = true ? json->type == cJSON_True : false;
331 } else if (strncmp(json->string, "compaction\0", 11) == 0 &&
332 cJSON_IsBool(json)) {
333 const bool compact = true ? json->type == cJSON_True : false;
335 } else {
336 return false;
337 }
338 return true;
339}
340
346static void
347param_json_import_action(struct XCSF *xcsf, cJSON *json)
348{
349 cJSON *token = cJSON_GetObjectItem(json, "type");
350 if (token == NULL) {
351 printf("No action type has been specified: cannot set params\n");
352 exit(EXIT_FAILURE);
353 }
354 if (!cJSON_IsString(token) ||
355 action_param_set_type_string(xcsf, token->valuestring) ==
357 printf("Invalid action type\n");
358 printf("Options: {%s}\n", ACT_TYPE_OPTIONS);
359 exit(EXIT_FAILURE);
360 }
361 token = cJSON_GetObjectItem(json, "args");
362 if (token != NULL) {
363 const char *ret = action_param_json_import(xcsf, token);
364 if (ret != NULL) {
365 printf("Invalid action parameter %s\n", ret);
366 exit(EXIT_FAILURE);
367 }
368 }
369}
370
376static void
378{
379 cJSON *token = cJSON_GetObjectItem(json, "type");
380 if (token == NULL) {
381 printf("No condition type has been specified: cannot set params\n");
382 exit(EXIT_FAILURE);
383 }
384 if (!cJSON_IsString(token) ||
385 cond_param_set_type_string(xcsf, token->valuestring) ==
387 printf("Invalid condition type\n");
388 printf("Options: {%s}\n", COND_TYPE_OPTIONS);
389 exit(EXIT_FAILURE);
390 }
391 token = cJSON_GetObjectItem(json, "args");
392 if (token != NULL) {
393 const char *ret = cond_param_json_import(xcsf, token);
394 if (ret != NULL) {
395 printf("Invalid condition parameter %s\n", ret);
396 exit(EXIT_FAILURE);
397 }
398 }
399}
400
406static void
408{
409 cJSON *token = cJSON_GetObjectItem(json, "type");
410 if (token == NULL) {
411 printf("No prediction type has been specified: cannot set params\n");
412 exit(EXIT_FAILURE);
413 }
414 if (!cJSON_IsString(token) ||
415 pred_param_set_type_string(xcsf, token->valuestring) ==
417 printf("Invalid prediction type\n");
418 printf("Options: {%s}\n", PRED_TYPE_OPTIONS);
419 exit(EXIT_FAILURE);
420 }
421 token = cJSON_GetObjectItem(json, "args");
422 if (token != NULL) {
423 const char *ret = pred_param_json_import(xcsf, token);
424 if (ret != NULL) {
425 printf("Invalid prediction parameter %s\n", ret);
426 exit(EXIT_FAILURE);
427 }
428 }
429}
430
436void
437param_json_import(struct XCSF *xcsf, const char *json_str)
438{
439 cJSON *json = cJSON_Parse(json_str);
441 for (cJSON *iter = json->child; iter != NULL; iter = iter->next) {
442 if (param_json_import_general(xcsf, iter)) {
443 continue;
444 }
445 if (param_json_import_multi(xcsf, iter)) {
446 continue;
447 }
448 if (param_json_import_subsump(xcsf, iter)) {
449 continue;
450 }
452 continue;
453 }
454 if (strncmp(iter->string, "ea\0", 3) == 0) {
455 ea_param_json_import(xcsf, iter->child);
456 continue;
457 }
458 if (strncmp(iter->string, "action\0", 7) == 0) {
460 continue;
461 }
462 if (strncmp(iter->string, "condition\0", 10) == 0) {
464 continue;
465 }
466 if (strncmp(iter->string, "prediction\0", 11) == 0) {
468 continue;
469 }
470 printf("Error: unable to import parameter: %s\n", iter->string);
471 exit(EXIT_FAILURE);
472 }
473 cJSON_Delete(json);
474}
475
480void
481param_print(const struct XCSF *xcsf)
482{
483 char *json_str = param_json_export(xcsf);
484 printf("%s\n", json_str);
485 free(json_str);
486}
487
494size_t
495param_save(const struct XCSF *xcsf, FILE *fp)
496{
497 size_t s = 0;
498 size_t len = strnlen(xcsf->population_file, MAX_LEN) + 1;
499 s += fwrite(&len, sizeof(size_t), 1, fp);
500 s += fwrite(xcsf->population_file, sizeof(char), len, fp);
501 s += fwrite(&xcsf->time, sizeof(int), 1, fp);
502 s += fwrite(&xcsf->error, sizeof(double), 1, fp);
503 s += fwrite(&xcsf->mset_size, sizeof(double), 1, fp);
504 s += fwrite(&xcsf->aset_size, sizeof(double), 1, fp);
505 s += fwrite(&xcsf->mfrac, sizeof(double), 1, fp);
506 s += fwrite(&xcsf->explore, sizeof(bool), 1, fp);
507 s += fwrite(&xcsf->x_dim, sizeof(int), 1, fp);
508 s += fwrite(&xcsf->y_dim, sizeof(int), 1, fp);
509 s += fwrite(&xcsf->n_actions, sizeof(int), 1, fp);
510 s += fwrite(&xcsf->OMP_NUM_THREADS, sizeof(int), 1, fp);
511 s += fwrite(&xcsf->RANDOM_STATE, sizeof(int), 1, fp);
512 s += fwrite(&xcsf->POP_INIT, sizeof(bool), 1, fp);
513 s += fwrite(&xcsf->MAX_TRIALS, sizeof(int), 1, fp);
514 s += fwrite(&xcsf->PERF_TRIALS, sizeof(int), 1, fp);
515 s += fwrite(&xcsf->POP_SIZE, sizeof(int), 1, fp);
516 s += fwrite(&xcsf->LOSS_FUNC, sizeof(int), 1, fp);
517 s += fwrite(&xcsf->HUBER_DELTA, sizeof(double), 1, fp);
518 s += fwrite(&xcsf->GAMMA, sizeof(double), 1, fp);
519 s += fwrite(&xcsf->TELETRANSPORTATION, sizeof(int), 1, fp);
520 s += fwrite(&xcsf->P_EXPLORE, sizeof(double), 1, fp);
521 s += fwrite(&xcsf->SET_SUBSUMPTION, sizeof(bool), 1, fp);
522 s += fwrite(&xcsf->THETA_SUB, sizeof(int), 1, fp);
523 s += fwrite(&xcsf->E0, sizeof(double), 1, fp);
524 s += fwrite(&xcsf->ALPHA, sizeof(double), 1, fp);
525 s += fwrite(&xcsf->NU, sizeof(double), 1, fp);
526 s += fwrite(&xcsf->BETA, sizeof(double), 1, fp);
527 s += fwrite(&xcsf->DELTA, sizeof(double), 1, fp);
528 s += fwrite(&xcsf->THETA_DEL, sizeof(int), 1, fp);
529 s += fwrite(&xcsf->INIT_FITNESS, sizeof(double), 1, fp);
530 s += fwrite(&xcsf->INIT_ERROR, sizeof(double), 1, fp);
531 s += fwrite(&xcsf->M_PROBATION, sizeof(int), 1, fp);
532 s += fwrite(&xcsf->STATEFUL, sizeof(bool), 1, fp);
533 s += fwrite(&xcsf->COMPACTION, sizeof(bool), 1, fp);
534 s += ea_param_save(xcsf, fp);
535 s += action_param_save(xcsf, fp);
536 s += cond_param_save(xcsf, fp);
537 s += pred_param_save(xcsf, fp);
538 return s;
539}
540
547size_t
548param_load(struct XCSF *xcsf, FILE *fp)
549{
550 size_t s = 0;
551 size_t len = 0;
552 s += fread(&len, sizeof(size_t), 1, fp);
553 if (len < 1) {
554 printf("param_load(): error len < 1\n");
555 exit(EXIT_FAILURE);
556 }
557 free(xcsf->population_file);
558 xcsf->population_file = malloc(sizeof(char) * len);
559 s += fread(xcsf->population_file, sizeof(char), len, fp);
560 s += fread(&xcsf->time, sizeof(int), 1, fp);
561 s += fread(&xcsf->error, sizeof(double), 1, fp);
562 s += fread(&xcsf->mset_size, sizeof(double), 1, fp);
563 s += fread(&xcsf->aset_size, sizeof(double), 1, fp);
564 s += fread(&xcsf->mfrac, sizeof(double), 1, fp);
565 s += fread(&xcsf->explore, sizeof(bool), 1, fp);
566 s += fread(&xcsf->x_dim, sizeof(int), 1, fp);
567 s += fread(&xcsf->y_dim, sizeof(int), 1, fp);
568 s += fread(&xcsf->n_actions, sizeof(int), 1, fp);
569 if (xcsf->x_dim < 1 || xcsf->y_dim < 1 || xcsf->n_actions < 1) {
570 printf("param_load(): read error\n");
571 exit(EXIT_FAILURE);
572 }
573 s += fread(&xcsf->OMP_NUM_THREADS, sizeof(int), 1, fp);
574 s += fread(&xcsf->RANDOM_STATE, sizeof(int), 1, fp);
575 s += fread(&xcsf->POP_INIT, sizeof(bool), 1, fp);
576 s += fread(&xcsf->MAX_TRIALS, sizeof(int), 1, fp);
577 s += fread(&xcsf->PERF_TRIALS, sizeof(int), 1, fp);
578 s += fread(&xcsf->POP_SIZE, sizeof(int), 1, fp);
579 s += fread(&xcsf->LOSS_FUNC, sizeof(int), 1, fp);
580 s += fread(&xcsf->HUBER_DELTA, sizeof(double), 1, fp);
581 s += fread(&xcsf->GAMMA, sizeof(double), 1, fp);
582 s += fread(&xcsf->TELETRANSPORTATION, sizeof(int), 1, fp);
583 s += fread(&xcsf->P_EXPLORE, sizeof(double), 1, fp);
584 s += fread(&xcsf->SET_SUBSUMPTION, sizeof(bool), 1, fp);
585 s += fread(&xcsf->THETA_SUB, sizeof(int), 1, fp);
586 s += fread(&xcsf->E0, sizeof(double), 1, fp);
587 s += fread(&xcsf->ALPHA, sizeof(double), 1, fp);
588 s += fread(&xcsf->NU, sizeof(double), 1, fp);
589 s += fread(&xcsf->BETA, sizeof(double), 1, fp);
590 s += fread(&xcsf->DELTA, sizeof(double), 1, fp);
591 s += fread(&xcsf->THETA_DEL, sizeof(int), 1, fp);
592 s += fread(&xcsf->INIT_FITNESS, sizeof(double), 1, fp);
593 s += fread(&xcsf->INIT_ERROR, sizeof(double), 1, fp);
594 s += fread(&xcsf->M_PROBATION, sizeof(int), 1, fp);
595 s += fread(&xcsf->STATEFUL, sizeof(bool), 1, fp);
596 s += fread(&xcsf->COMPACTION, sizeof(bool), 1, fp);
597 s += ea_param_load(xcsf, fp);
598 s += action_param_load(xcsf, fp);
599 s += cond_param_load(xcsf, fp);
600 s += pred_param_load(xcsf, fp);
602 return s;
603}
604
605/* setters */
606
613const char *
615{
616 if (a < 1 || a > 1000) {
617 return "Invalid OMP_NUM_THREADS. Range: [1,1000]";
618 }
619 xcsf->OMP_NUM_THREADS = a;
620#ifdef PARALLEL
621 omp_set_num_threads(xcsf->OMP_NUM_THREADS);
622#endif
623 return NULL;
624}
625
626const char *
627param_set_random_state(struct XCSF *xcsf, const int a)
628{
629 xcsf->RANDOM_STATE = a;
630 if (a < 0) {
631 rand_init();
632 } else {
634 }
635 return NULL;
636}
637
638const char *
639param_set_population_file(struct XCSF *xcsf, const char *a)
640{
641 free(xcsf->population_file);
642 size_t length = strnlen(a, sizeof(char) * MAX_LEN) + 1;
643 xcsf->population_file = malloc(sizeof(char) * length);
644 strncpy(xcsf->population_file, a, length);
645 return NULL;
646}
647
648const char *
649param_set_pop_init(struct XCSF *xcsf, const bool a)
650{
651 xcsf->POP_INIT = a;
652 return NULL;
653}
654
655const char *
656param_set_max_trials(struct XCSF *xcsf, const int a)
657{
658 if (a < 1) {
659 return "MAX_TRIALS must be > 0";
660 }
661 xcsf->MAX_TRIALS = a;
662 return NULL;
663}
664
665const char *
666param_set_perf_trials(struct XCSF *xcsf, const int a)
667{
668 if (a < 1) {
669 return "PERF_TRIALS must be > 0";
670 }
671 xcsf->PERF_TRIALS = a;
672 return NULL;
673}
674
675const char *
676param_set_pop_size(struct XCSF *xcsf, const int a)
677{
678 if (a < 1) {
679 return "POP_SIZE must be > 0";
680 }
681 xcsf->POP_SIZE = a;
682 return NULL;
683}
684
685int
686param_set_loss_func_string(struct XCSF *xcsf, const char *a)
687{
688 const int loss = loss_type_as_int(a);
689 if (loss != LOSS_INVALID) {
690 xcsf->LOSS_FUNC = loss;
692 return PARAM_FOUND;
693 }
694 }
695 return PARAM_INVALID;
696}
697
698void
699param_set_loss_func(struct XCSF *xcsf, const int a)
700{
701 if (a < 0 || a >= LOSS_NUM) {
702 printf("param_set_loss_func(): invalid LOSS_FUNC: %d\n", a);
703 exit(EXIT_FAILURE);
704 }
705 xcsf->LOSS_FUNC = a;
707}
708
709const char *
710param_set_stateful(struct XCSF *xcsf, const bool a)
711{
712 xcsf->STATEFUL = a;
713 return NULL;
714}
715
716const char *
717param_set_compaction(struct XCSF *xcsf, const bool a)
718{
719 xcsf->COMPACTION = a;
720 return NULL;
721}
722
723const char *
724param_set_huber_delta(struct XCSF *xcsf, const double a)
725{
726 if (a < 0) {
727 return "HUBER_DELTA must be >= 0";
728 }
729 xcsf->HUBER_DELTA = a;
730 return NULL;
731}
732
733const char *
734param_set_gamma(struct XCSF *xcsf, const double a)
735{
736 if (a < 0 || a > 1) {
737 return "Invalid GAMMA. Range: [0,1]";
738 }
739 xcsf->GAMMA = a;
740 return NULL;
741}
742
743const char *
745{
746 if (a < 1) {
747 return "TELETRANSPORTATION must be > 0";
748 }
749 xcsf->TELETRANSPORTATION = a;
750 return NULL;
751}
752
753const char *
754param_set_p_explore(struct XCSF *xcsf, const double a)
755{
756 if (a < 0 || a > 1) {
757 return "Invalid P_EXPLORE. Range: [0,1]";
758 }
759 xcsf->P_EXPLORE = a;
760 return NULL;
761}
762
763const char *
764param_set_alpha(struct XCSF *xcsf, const double a)
765{
766 if (a < 0 || a > 1) {
767 return "Invalid ALPHA. Range: [0,1]";
768 }
769 xcsf->ALPHA = a;
770 return NULL;
771}
772
773const char *
774param_set_beta(struct XCSF *xcsf, const double a)
775{
776 if (a < 0 || a > 1) {
777 return "Invalid BETA. Range: [0,1]";
778 }
779 xcsf->BETA = a;
780 return NULL;
781}
782
783const char *
784param_set_delta(struct XCSF *xcsf, const double a)
785{
786 if (a < 0 || a > 1) {
787 return "Invalid DELTA. Range: [0,1]";
788 }
789 xcsf->DELTA = a;
790 return NULL;
791}
792
793const char *
794param_set_e0(struct XCSF *xcsf, const double a)
795{
796 if (a < 0) {
797 return "E0 must be >= 0";
798 }
799 xcsf->E0 = a;
800 return NULL;
801}
802
803const char *
804param_set_init_error(struct XCSF *xcsf, const double a)
805{
806 if (a < 0) {
807 return "INIT_ERROR must be >= 0";
808 }
809 xcsf->INIT_ERROR = a;
810 return NULL;
811}
812
813const char *
814param_set_init_fitness(struct XCSF *xcsf, const double a)
815{
816 if (a < 0) {
817 return "INIT_FITNESS must be >= 0";
818 }
819 xcsf->INIT_FITNESS = a;
820 return NULL;
821}
822
823const char *
824param_set_nu(struct XCSF *xcsf, const double a)
825{
826 if (a < 0) {
827 return "NU must be >= 0";
828 }
829 xcsf->NU = a;
830 return NULL;
831}
832
833const char *
834param_set_theta_del(struct XCSF *xcsf, const int a)
835{
836 if (a < 0) {
837 return "THETA_DEL must be >= 0";
838 }
839 xcsf->THETA_DEL = a;
840 return NULL;
841}
842
843const char *
844param_set_m_probation(struct XCSF *xcsf, const int a)
845{
846 if (a < 0) {
847 return "M_PROBATION must be >= 0";
848 }
849 xcsf->M_PROBATION = a;
850 return NULL;
851}
852
853const char *
854param_set_set_subsumption(struct XCSF *xcsf, const bool a)
855{
856 xcsf->SET_SUBSUMPTION = a;
857 return NULL;
858}
859
860const char *
861param_set_theta_sub(struct XCSF *xcsf, const int a)
862{
863 if (a < 0) {
864 return "THETA_SUB must be >= 0";
865 }
866 xcsf->THETA_SUB = a;
867 return NULL;
868}
869
870const char *
871param_set_x_dim(struct XCSF *xcsf, const int a)
872{
873 if (a < 1) {
874 return "x_dim must be > 0";
875 }
876 xcsf->x_dim = a;
877 return NULL;
878}
879
880const char *
881param_set_explore(struct XCSF *xcsf, const bool a)
882{
883 xcsf->explore = a;
884 return NULL;
885}
886
887const char *
888param_set_y_dim(struct XCSF *xcsf, const int a)
889{
890 if (a < 1) {
891 return "y_dim must be > 0";
892 }
893 xcsf->y_dim = a;
894 return NULL;
895}
896
897const char *
898param_set_n_actions(struct XCSF *xcsf, const int a)
899{
900 if (a < 1) {
901 return "n_actions must be > 0";
902 }
903 xcsf->n_actions = a;
904 return NULL;
905}
size_t action_param_load(struct XCSF *xcsf, FILE *fp)
Loads action parameters.
Definition action.c:170
size_t action_param_save(const struct XCSF *xcsf, FILE *fp)
Saves action parameters.
Definition action.c:154
int action_param_set_type_string(struct XCSF *xcsf, const char *a)
Definition action.c:192
char * action_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the action parameters from a cJSON object.
Definition action.c:131
void action_param_defaults(struct XCSF *xcsf)
Initialises default action parameters.
Definition action.c:91
char * action_param_json_export(const struct XCSF *xcsf)
Returns a json formatted string of the action parameters.
Definition action.c:103
void action_param_free(struct XCSF *xcsf)
Frees action parameters.
Definition action.c:184
Interface for classifier actions.
#define ACT_TYPE_INVALID
Error code for invalid actions.
Definition action.h:28
#define ACT_TYPE_OPTIONS
Definition action.h:35
size_t cond_param_load(struct XCSF *xcsf, FILE *fp)
Loads condition parameters.
Definition condition.c:343
size_t cond_param_save(const struct XCSF *xcsf, FILE *fp)
Saves condition parameters.
Definition condition.c:319
int cond_param_set_type_string(struct XCSF *xcsf, const char *a)
Definition condition.c:440
char * cond_param_json_export(const struct XCSF *xcsf)
Returns a json formatted string of the condition parameters.
Definition condition.c:232
void cond_param_defaults(struct XCSF *xcsf)
Initialises default condition parameters.
Definition condition.c:166
void cond_param_free(struct XCSF *xcsf)
Frees condition parameters.
Definition condition.c:365
char * cond_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the condition parameters from a cJSON object.
Definition condition.c:281
Interface for classifier conditions.
#define COND_TYPE_INVALID
Error code for invalid condition.
Definition condition.h:28
#define RULE_TYPE_NEURAL
Condition type and action type neural.
Definition condition.h:38
#define RULE_TYPE_DGP
Condition type and action type DGP.
Definition condition.h:37
#define RULE_TYPE_NETWORK
Condition type and prediction type neural.
Definition condition.h:39
#define COND_TYPE_OPTIONS
Definition condition.h:53
char * ea_param_json_export(const struct XCSF *xcsf)
Returns a json formatted string representation of the EA parameters.
Definition ea.c:256
void ea_param_defaults(struct XCSF *xcsf)
Initialises default evolutionary algorithm parameters.
Definition ea.c:237
size_t ea_param_load(struct XCSF *xcsf, FILE *fp)
Loads evolutionary algorithm parameters.
Definition ea.c:355
size_t ea_param_save(const struct XCSF *xcsf, FILE *fp)
Saves evolutionary algorithm parameters.
Definition ea.c:333
void ea_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the EA parameters from a cJSON object.
Definition ea.c:282
Evolutionary algorithm functions.
const char * loss_type_as_string(const int type)
Returns a string representation of a loss type from the integer.
Definition loss.c:194
int loss_type_as_int(const char *type)
Returns the integer representation of a loss type given a name.
Definition loss.c:223
int loss_set_func(struct XCSF *xcsf)
Sets the XCSF error function to the implemented function.
Definition loss.c:159
#define LOSS_NUM
Total number of selectable loss functions.
Definition loss.h:36
#define LOSS_MAE
Mean absolute error.
Definition loss.h:29
#define LOSS_OPTIONS
Definition loss.h:46
#define LOSS_HUBER
Huber loss.
Definition loss.h:35
#define LOSS_INVALID
Error code for invalid loss type.
Definition loss.h:28
const char * param_set_compaction(struct XCSF *xcsf, const bool a)
Definition param.c:717
const char * param_set_pop_size(struct XCSF *xcsf, const int a)
Definition param.c:676
const char * param_set_stateful(struct XCSF *xcsf, const bool a)
Definition param.c:710
void param_json_import(struct XCSF *xcsf, const char *json_str)
Sets the parameters from a json formatted string.
Definition param.c:437
void param_print(const struct XCSF *xcsf)
Prints all XCSF parameters.
Definition param.c:481
const char * param_set_explore(struct XCSF *xcsf, const bool a)
Definition param.c:881
const char * param_set_n_actions(struct XCSF *xcsf, const int a)
Definition param.c:898
const char * param_set_init_fitness(struct XCSF *xcsf, const double a)
Definition param.c:814
const char * param_set_nu(struct XCSF *xcsf, const double a)
Definition param.c:824
const char * param_set_e0(struct XCSF *xcsf, const double a)
Definition param.c:794
const char * param_set_teletransportation(struct XCSF *xcsf, const int a)
Definition param.c:744
char * param_json_export(const struct XCSF *xcsf)
Returns a json formatted string representation of the parameters.
Definition param.c:113
const char * param_set_max_trials(struct XCSF *xcsf, const int a)
Definition param.c:656
const char * param_set_x_dim(struct XCSF *xcsf, const int a)
Definition param.c:871
static bool param_json_import_multi(struct XCSF *xcsf, const cJSON *json)
Sets the multi-step parameters from a cJSON object.
Definition param.c:253
void param_set_loss_func(struct XCSF *xcsf, const int a)
Definition param.c:699
static void param_json_import_action(struct XCSF *xcsf, cJSON *json)
Sets the action parameters from a json formatted string.
Definition param.c:347
const char * param_set_theta_del(struct XCSF *xcsf, const int a)
Definition param.c:834
const char * param_set_m_probation(struct XCSF *xcsf, const int a)
Definition param.c:844
const char * param_set_p_explore(struct XCSF *xcsf, const double a)
Definition param.c:754
static void param_json_import_condition(struct XCSF *xcsf, cJSON *json)
Sets the condition parameters from a json formatted string.
Definition param.c:377
static bool param_json_import_cl_general(struct XCSF *xcsf, const cJSON *json)
Sets the general classifier parameters from a cJSON object.
Definition param.c:301
const char * param_set_init_error(struct XCSF *xcsf, const double a)
Definition param.c:804
#define MAX_LEN
Maximum length of a population filename.
Definition param.c:35
const char * param_set_population_file(struct XCSF *xcsf, const char *a)
Definition param.c:639
const char * param_set_random_state(struct XCSF *xcsf, const int a)
Definition param.c:627
static void param_json_import_prediction(struct XCSF *xcsf, cJSON *json)
Sets the prediction parameters from a json formatted string.
Definition param.c:407
const char * param_set_huber_delta(struct XCSF *xcsf, const double a)
Definition param.c:724
int param_set_loss_func_string(struct XCSF *xcsf, const char *a)
Definition param.c:686
static bool param_json_import_subsump(struct XCSF *xcsf, const cJSON *json)
Sets the subsumption parameters from a cJSON object.
Definition param.c:278
void param_init(struct XCSF *xcsf, const int x_dim, const int y_dim, const int n_actions)
Initialises default XCSF parameters.
Definition param.c:45
const char * param_set_alpha(struct XCSF *xcsf, const double a)
Definition param.c:764
void param_free(struct XCSF *xcsf)
Definition param.c:93
const char * param_set_omp_num_threads(struct XCSF *xcsf, const int a)
Sets the number of OMP threads.
Definition param.c:614
const char * param_set_theta_sub(struct XCSF *xcsf, const int a)
Definition param.c:861
static bool param_json_import_general(struct XCSF *xcsf, const cJSON *json)
Sets the general parameters from a cJSON object.
Definition param.c:193
const char * param_set_set_subsumption(struct XCSF *xcsf, const bool a)
Definition param.c:854
const char * param_set_delta(struct XCSF *xcsf, const double a)
Definition param.c:784
const char * param_set_beta(struct XCSF *xcsf, const double a)
Definition param.c:774
const char * param_set_gamma(struct XCSF *xcsf, const double a)
Definition param.c:734
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_pop_init(struct XCSF *xcsf, const bool a)
Definition param.c:649
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
const char * param_set_perf_trials(struct XCSF *xcsf, const int a)
Definition param.c:666
Functions for setting and printing parameters.
#define PARAM_FOUND
Code for a found parameter.
Definition param.h:31
#define PARAM_INVALID
Code for invalid parameter.
Definition param.h:29
size_t pred_param_load(struct XCSF *xcsf, FILE *fp)
Loads prediction parameters.
Definition prediction.c:232
char * pred_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the prediction parameters from a cJSON object.
Definition prediction.c:179
void pred_param_free(struct XCSF *xcsf)
Frees prediction parameters.
Definition prediction.c:252
size_t pred_param_save(const struct XCSF *xcsf, FILE *fp)
Saves prediction parameters.
Definition prediction.c:210
int pred_param_set_type_string(struct XCSF *xcsf, const char *a)
Definition prediction.c:354
char * pred_param_json_export(const struct XCSF *xcsf)
Returns a json formatted string of the prediction parameters.
Definition prediction.c:138
void pred_param_defaults(struct XCSF *xcsf)
Initialises default prediction parameters.
Definition prediction.c:120
Interface for classifier predictions.
#define PRED_TYPE_OPTIONS
Definition prediction.h:43
#define PRED_TYPE_INVALID
Error code for invalid prediction.
Definition prediction.h:28
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
XCSF data structure.
Definition xcsf.h:85
void rand_init(void)
Initialises the pseudo-random number generator.
Definition utils.c:34
void utils_json_parse_check(const cJSON *json)
Checks whether JSON parsed correctly.
Definition utils.c:109
void rand_init_seed(const uint32_t seed)
Initialises the pseudo-random number generator with a fixed seed.
Definition utils.c:50
Utility functions for random number handling, etc.
static void catch_error(const char *ret)
Catches parameter value errors.
Definition utils.h:134
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