XCSF  1.4.7
XCSF learning classifier system
action.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 "action.h"
25 #include "act_integer.h"
26 #include "act_neural.h"
27 #include "utils.h"
28 
34 void
35 action_set(const struct XCSF *xcsf, struct Cl *c)
36 {
37  switch (xcsf->act->type) {
38  case ACT_TYPE_INTEGER:
40  break;
41  case ACT_TYPE_NEURAL:
43  break;
44  default:
45  printf("Invalid action type specified: %d\n", xcsf->act->type);
46  exit(EXIT_FAILURE);
47  }
48 }
49 
55 const char *
56 action_type_as_string(const int type)
57 {
58  switch (type) {
59  case ACT_TYPE_INTEGER:
60  return ACT_STRING_INTEGER;
61  case ACT_TYPE_NEURAL:
62  return ACT_STRING_NEURAL;
63  default:
64  printf("action_type_as_string(): invalid type: %d\n", type);
65  exit(EXIT_FAILURE);
66  }
67 }
68 
74 int
75 action_type_as_int(const char *type)
76 {
77  if (strncmp(type, ACT_STRING_INTEGER, 8) == 0) {
78  return ACT_TYPE_INTEGER;
79  }
80  if (strncmp(type, ACT_STRING_NEURAL, 7) == 0) {
81  return ACT_TYPE_NEURAL;
82  }
83  return ACT_TYPE_INVALID;
84 }
85 
90 void
92 {
95 }
96 
102 char *
104 {
105  const struct ArgsAct *act = xcsf->act;
106  cJSON *json = cJSON_CreateObject();
107  cJSON_AddStringToObject(json, "type", action_type_as_string(act->type));
108  char *json_str = NULL;
109  if (xcsf->act->type == ACT_TYPE_NEURAL) {
110  json_str = layer_args_json_export(xcsf->act->largs);
111  }
112  if (json_str != NULL) {
113  cJSON *params = cJSON_Parse(json_str);
114  if (params != NULL) {
115  cJSON_AddItemToObject(json, "args", params);
116  }
117  free(json_str);
118  }
119  char *string = cJSON_Print(json);
120  cJSON_Delete(json);
121  return string;
122 }
123 
130 char *
131 action_param_json_import(struct XCSF *xcsf, cJSON *json)
132 {
133  char *ret = NULL;
134  switch (xcsf->act->type) {
135  case ACT_TYPE_INTEGER:
136  break;
137  case ACT_TYPE_NEURAL:
138  ret = act_neural_param_json_import(xcsf, json->child);
139  break;
140  default:
141  printf("action_param_json_import(): unknown type.\n");
142  exit(EXIT_FAILURE);
143  }
144  return ret;
145 }
146 
153 size_t
154 action_param_save(const struct XCSF *xcsf, FILE *fp)
155 {
156  const struct ArgsAct *act = xcsf->act;
157  size_t s = 0;
158  s += fwrite(&act->type, sizeof(int), 1, fp);
159  s += layer_args_save(act->largs, fp);
160  return s;
161 }
162 
169 size_t
170 action_param_load(struct XCSF *xcsf, FILE *fp)
171 {
172  struct ArgsAct *act = xcsf->act;
173  size_t s = 0;
174  s += fread(&act->type, sizeof(int), 1, fp);
175  s += layer_args_load(&act->largs, fp);
176  return s;
177 }
178 
183 void
185 {
186  layer_args_free(&xcsf->act->largs);
187 }
188 
189 /* parameter setters */
190 
191 int
192 action_param_set_type_string(struct XCSF *xcsf, const char *a)
193 {
194  const int type = action_type_as_int(a);
195  if (type != ACT_TYPE_INVALID) {
196  xcsf->act->type = type;
197  }
198  return type;
199 }
200 
201 void
202 action_param_set_type(struct XCSF *xcsf, const int a)
203 {
204  if (a < 0) {
205  printf("Warning: tried to set ACT TYPE too small\n");
206  xcsf->act->type = 0;
207  } else {
208  xcsf->act->type = a;
209  }
210 }
integer action functions.
static struct ActVtbl const act_integer_vtbl
Integer action implemented functions.
Definition: act_integer.h:89
void act_neural_param_defaults(struct XCSF *xcsf)
Initialises default neural action parameters.
Definition: act_neural.c:270
char * act_neural_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the neural network parameters from a cJSON object.
Definition: act_neural.c:305
Neural network action functions.
static struct ActVtbl const act_neural_vtbl
neural action implemented functions.
Definition: act_neural.h:96
size_t action_param_load(struct XCSF *xcsf, FILE *fp)
Loads action parameters.
Definition: action.c:170
void action_param_set_type(struct XCSF *xcsf, const int a)
Definition: action.c:202
int action_type_as_int(const char *type)
Returns the integer representation of an action type given a name.
Definition: action.c:75
char * action_param_json_import(struct XCSF *xcsf, cJSON *json)
Sets the action parameters from a cJSON object.
Definition: action.c:131
void action_set(const struct XCSF *xcsf, struct Cl *c)
Sets a classifier's action functions to the implementations.
Definition: action.c:35
const char * action_type_as_string(const int type)
Returns a string representation of an action type from an integer.
Definition: action.c:56
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
void action_param_defaults(struct XCSF *xcsf)
Initialises default action parameters.
Definition: action.c:91
void action_param_free(struct XCSF *xcsf)
Frees action parameters.
Definition: action.c:184
char * action_param_json_export(const struct XCSF *xcsf)
Returns a json formatted string of the action parameters.
Definition: action.c:103
Interface for classifier actions.
#define ACT_TYPE_NEURAL
Action type neural network.
Definition: action.h:30
#define ACT_TYPE_INVALID
Error code for invalid actions.
Definition: action.h:28
#define ACT_STRING_INTEGER
Integer.
Definition: action.h:32
#define ACT_STRING_NEURAL
Neural.
Definition: action.h:33
#define ACT_TYPE_INTEGER
Action type integer.
Definition: action.h:29
Definition: __init__.py:1
char * layer_args_json_export(struct ArgsLayer *args)
Returns a json formatted string of the neural layer parameters.
size_t layer_args_load(struct ArgsLayer **largs, FILE *fp)
Loads neural network layer parameters.
void layer_args_free(struct ArgsLayer **largs)
Frees memory used by a list of layer parameters and points to NULL.
size_t layer_args_save(const struct ArgsLayer *args, FILE *fp)
Saves neural network layer parameters.
Parameters for initialising and operating actions.
Definition: action.h:40
struct ArgsLayer * largs
Linked-list of layer parameters.
Definition: action.h:42
int type
Classifier action type.
Definition: action.h:41
Classifier data structure.
Definition: xcsf.h:45
struct ActVtbl const * act_vptr
Functions acting on actions.
Definition: xcsf.h:48
XCSF data structure.
Definition: xcsf.h:85
Utility functions for random number handling, etc.