XCSF  1.4.7
XCSF learning classifier system
act_integer.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 "act_integer.h"
25 #include "sam.h"
26 #include "utils.h"
27 
28 #define N_MU (1)
29 
33 static const int MU_TYPE[N_MU] = { SAM_LOG_NORMAL };
34 
42 bool
43 act_integer_crossover(const struct XCSF *xcsf, const struct Cl *c1,
44  const struct Cl *c2)
45 {
46  (void) xcsf;
47  (void) c1;
48  (void) c2;
49  return false;
50 }
51 
59 bool
60 act_integer_general(const struct XCSF *xcsf, const struct Cl *c1,
61  const struct Cl *c2)
62 {
63  (void) xcsf;
64  const struct ActInteger *act1 = c1->act;
65  const struct ActInteger *act2 = c2->act;
66  if (act1->action != act2->action) {
67  return false;
68  }
69  return true;
70 }
71 
78 bool
79 act_integer_mutate(const struct XCSF *xcsf, const struct Cl *c)
80 {
81  struct ActInteger *act = c->act;
82  sam_adapt(act->mu, N_MU, MU_TYPE);
83  if (rand_uniform(0, 1) < act->mu[0]) {
84  const int old = act->action;
85  act->action = rand_uniform_int(0, xcsf->n_actions);
86  if (old != act->action) {
87  return true;
88  }
89  }
90  return false;
91 }
92 
100 int
101 act_integer_compute(const struct XCSF *xcsf, const struct Cl *c,
102  const double *x)
103 {
104  (void) xcsf;
105  (void) x;
106  const struct ActInteger *act = c->act;
107  return act->action;
108 }
109 
116 void
117 act_integer_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
118 {
119  (void) xcsf;
120  struct ActInteger *new = malloc(sizeof(struct ActInteger));
121  const struct ActInteger *src_act = src->act;
122  new->action = src_act->action;
123  new->mu = malloc(sizeof(double) * N_MU);
124  memcpy(new->mu, src_act->mu, sizeof(double) * N_MU);
125  dest->act = new;
126 }
127 
133 void
134 act_integer_print(const struct XCSF *xcsf, const struct Cl *c)
135 {
136  char *json_str = act_integer_json_export(xcsf, c);
137  printf("%s\n", json_str);
138  free(json_str);
139 }
140 
148 void
149 act_integer_cover(const struct XCSF *xcsf, const struct Cl *c, const double *x,
150  const int action)
151 {
152  (void) xcsf;
153  (void) x;
154  struct ActInteger *act = c->act;
155  act->action = action;
156 }
157 
163 void
164 act_integer_free(const struct XCSF *xcsf, const struct Cl *c)
165 {
166  (void) xcsf;
167  const struct ActInteger *act = c->act;
168  free(act->mu);
169  free(c->act);
170 }
171 
177 void
178 act_integer_init(const struct XCSF *xcsf, struct Cl *c)
179 {
180  struct ActInteger *new = malloc(sizeof(struct ActInteger));
181  new->mu = malloc(sizeof(double) * N_MU);
182  sam_init(new->mu, N_MU, MU_TYPE);
183  new->action = rand_uniform_int(0, xcsf->n_actions);
184  c->act = new;
185 }
186 
194 void
195 act_integer_update(const struct XCSF *xcsf, const struct Cl *c, const double *x,
196  const double *y)
197 {
198  (void) xcsf;
199  (void) c;
200  (void) x;
201  (void) y;
202 }
203 
211 size_t
212 act_integer_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
213 {
214  (void) xcsf;
215  size_t s = 0;
216  const struct ActInteger *act = c->act;
217  s += fwrite(&act->action, sizeof(int), 1, fp);
218  s += fwrite(act->mu, sizeof(double), N_MU, fp);
219  return s;
220 }
221 
229 size_t
230 act_integer_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
231 {
232  (void) xcsf;
233  size_t s = 0;
234  struct ActInteger *new = malloc(sizeof(struct ActInteger));
235  s += fread(&new->action, sizeof(int), 1, fp);
236  new->mu = malloc(sizeof(double) * N_MU);
237  s += fread(new->mu, sizeof(double), N_MU, fp);
238  c->act = new;
239  return s;
240 }
241 
248 char *
249 act_integer_json_export(const struct XCSF *xcsf, const struct Cl *c)
250 {
251  (void) xcsf;
252  const struct ActInteger *act = c->act;
253  cJSON *json = cJSON_CreateObject();
254  cJSON_AddStringToObject(json, "type", "integer");
255  cJSON_AddNumberToObject(json, "action", act->action);
256  cJSON *mutation = cJSON_CreateDoubleArray(act->mu, N_MU);
257  cJSON_AddItemToObject(json, "mutation", mutation);
258  char *string = cJSON_Print(json);
259  cJSON_Delete(json);
260  return string;
261 }
262 
269 void
270 act_integer_json_import(const struct XCSF *xcsf, struct Cl *c,
271  const cJSON *json)
272 {
273  (void) xcsf;
274  struct ActInteger *act = c->act;
275  const cJSON *item = cJSON_GetObjectItem(json, "action");
276  if (item != NULL && cJSON_IsNumber(item)) {
277  act->action = item->valueint;
278  }
279  sam_json_import(act->mu, N_MU, json);
280 }
void act_integer_copy(const struct XCSF *xcsf, struct Cl *dest, const struct Cl *src)
Copies an integer action from one classifier to another.
Definition: act_integer.c:117
void act_integer_free(const struct XCSF *xcsf, const struct Cl *c)
Frees the memory used by an integer action.
Definition: act_integer.c:164
bool act_integer_crossover(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Dummy function since integer actions do not perform crossover.
Definition: act_integer.c:43
static const int MU_TYPE[(1)]
Self-adaptation method for mutating integer actions.
Definition: act_integer.c:33
int act_integer_compute(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Returns a classifier's integer action.
Definition: act_integer.c:101
size_t act_integer_save(const struct XCSF *xcsf, const struct Cl *c, FILE *fp)
Writes an integer action to a file.
Definition: act_integer.c:212
void act_integer_update(const struct XCSF *xcsf, const struct Cl *c, const double *x, const double *y)
Dummy function since integer actions are not updated.
Definition: act_integer.c:195
void act_integer_json_import(const struct XCSF *xcsf, struct Cl *c, const cJSON *json)
Creates an integer action from a cJSON object.
Definition: act_integer.c:270
#define N_MU
Number of integer action mutation rates.
Definition: act_integer.c:28
void act_integer_init(const struct XCSF *xcsf, struct Cl *c)
Initialises an integer action.
Definition: act_integer.c:178
bool act_integer_mutate(const struct XCSF *xcsf, const struct Cl *c)
Mutates an integer action.
Definition: act_integer.c:79
bool act_integer_general(const struct XCSF *xcsf, const struct Cl *c1, const struct Cl *c2)
Returns whether the action of classifier c1 is more general than c2.
Definition: act_integer.c:60
size_t act_integer_load(const struct XCSF *xcsf, struct Cl *c, FILE *fp)
Reads an integer action from a file.
Definition: act_integer.c:230
char * act_integer_json_export(const struct XCSF *xcsf, const struct Cl *c)
Returns a json formatted string representation of an integer action.
Definition: act_integer.c:249
void act_integer_cover(const struct XCSF *xcsf, const struct Cl *c, const double *x, const int action)
Sets an integer action to a specified value.
Definition: act_integer.c:149
void act_integer_print(const struct XCSF *xcsf, const struct Cl *c)
Prints an integer action.
Definition: act_integer.c:134
integer action functions.
Definition: __init__.py:1
void sam_json_import(double *mu, const int N, const cJSON *json)
Initialises a mutation vector from a cJSON object.
Definition: sam.c:100
void sam_init(double *mu, const int N, const int *type)
Initialises self-adaptive mutation rates.
Definition: sam.c:43
void sam_adapt(double *mu, const int N, const int *type)
Self-adapts mutation rates.
Definition: sam.c:68
Self-adaptive mutation functions.
#define SAM_LOG_NORMAL
Log normal self-adaptation.
Definition: sam.h:28
Integer action data structure.
Definition: act_integer.h:32
int action
Integer action.
Definition: act_integer.h:33
double * mu
Mutation rates.
Definition: act_integer.h:34
Classifier data structure.
Definition: xcsf.h:45
void * act
Action structure.
Definition: xcsf.h:51
XCSF data structure.
Definition: xcsf.h:85
int rand_uniform_int(const int min, const int max)
Returns a uniform random integer [min,max] not inclusive of max.
Definition: utils.c:74
double rand_uniform(const double min, const double max)
Returns a uniform random float [min,max].
Definition: utils.c:62
Utility functions for random number handling, etc.