XCSF  1.4.7
XCSF learning classifier system
pa.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 "pa.h"
25 #include "cl.h"
26 #include "utils.h"
27 
32 static void
33 pa_reset(const struct XCSF *xcsf)
34 {
35  for (int i = 0; i < xcsf->pa_size; ++i) {
36  xcsf->pa[i] = 0;
37  xcsf->nr[i] = 0;
38  }
39 }
40 
45 void
46 pa_init(struct XCSF *xcsf)
47 {
48  xcsf->pa_size = xcsf->n_actions * xcsf->y_dim;
49  xcsf->pa = malloc(sizeof(double) * xcsf->pa_size);
50  xcsf->nr = malloc(sizeof(double) * xcsf->pa_size);
51  xcsf->cover = calloc(xcsf->pa_size, sizeof(double));
52 }
53 
61 void
62 pa_build(const struct XCSF *xcsf, const double *x)
63 {
64  const struct Set *set = &xcsf->mset;
65  double *pa = xcsf->pa;
66  double *nr = xcsf->nr;
67  pa_reset(xcsf);
68 #ifdef PARALLEL_PRED
69  // (parallel) propagate input and compute predictions
70  struct Cl *clist[set->size];
71  struct Clist *iter = set->list;
72  for (int i = 0; i < set->size; ++i) {
73  clist[i] = NULL;
74  if (iter != NULL) {
75  clist[i] = iter->cl;
76  iter = iter->next;
77  }
78  }
79  #pragma omp parallel for
80  for (int i = 0; i < set->size; ++i) {
81  if (clist[i] != NULL) {
82  cl_predict(xcsf, clist[i], x);
83  }
84  }
85 #else
86  // (series) propagate input and compute predictions
87  const struct Clist *iter = set->list;
88  while (iter != NULL) {
89  cl_predict(xcsf, iter->cl, x);
90  iter = iter->next;
91  }
92 #endif
93  // compute the prediction array in series for determinism
94  iter = set->list;
95  while (iter != NULL) {
96  const double *pred = iter->cl->prediction;
97  const double fitness = iter->cl->fit;
98  for (int j = 0; j < xcsf->y_dim; ++j) {
99  pa[iter->cl->action * xcsf->y_dim + j] += pred[j] * fitness;
100  nr[iter->cl->action * xcsf->y_dim + j] += fitness;
101  }
102  iter = iter->next;
103  }
104  for (int i = 0; i < xcsf->n_actions; ++i) {
105  for (int j = 0; j < xcsf->y_dim; ++j) {
106  const int k = i * xcsf->y_dim + j;
107  if (nr[k] != 0) {
108  pa[k] /= nr[k];
109  } else {
110  pa[k] = 0;
111  }
112  }
113  }
114 }
115 
122 int
123 pa_best_action(const struct XCSF *xcsf)
124 {
125  int *max_i = calloc(xcsf->n_actions, sizeof(int));
126  double max = xcsf->pa[0];
127  int n_max = 1;
128  for (int i = 1; i < xcsf->n_actions; ++i) {
129  const double val = xcsf->pa[i];
130  if (val > max) {
131  max = val;
132  max_i[0] = i;
133  n_max = 1;
134  } else if (val == max) {
135  max_i[n_max] = i;
136  ++n_max;
137  }
138  }
139  const int best = max_i[rand_uniform_int(0, n_max)];
140  free(max_i);
141  return best;
142 }
143 
149 int
150 pa_rand_action(const struct XCSF *xcsf)
151 {
152  int action = 0;
153  do {
154  action = rand_uniform_int(0, xcsf->n_actions);
155  } while (xcsf->nr[action] == 0);
156  return action;
157 }
158 
164 double
165 pa_best_val(const struct XCSF *xcsf)
166 {
167  const int max_i = argmax(xcsf->pa, xcsf->pa_size);
168  return xcsf->pa[max_i];
169 }
170 
177 double
178 pa_val(const struct XCSF *xcsf, const int action)
179 {
180  if (action < 0 || action >= xcsf->n_actions) {
181  printf("pa_val() error: invalid action specified: %d\n", action);
182  exit(EXIT_FAILURE);
183  }
184  return xcsf->pa[action];
185 }
186 
191 void
192 pa_free(const struct XCSF *xcsf)
193 {
194  free(xcsf->pa);
195  free(xcsf->nr);
196  free(xcsf->cover);
197 }
const double * cl_predict(const struct XCSF *xcsf, const struct Cl *c, const double *x)
Computes the current classifier prediction using the input.
Definition: cl.c:318
Functions operating on classifiers.
Definition: __init__.py:1
double pa_best_val(const struct XCSF *xcsf)
Returns the highest value in the prediction array.
Definition: pa.c:165
int pa_rand_action(const struct XCSF *xcsf)
Returns a random action from the prediction array.
Definition: pa.c:150
static void pa_reset(const struct XCSF *xcsf)
Resets the prediction array to zero.
Definition: pa.c:33
double pa_val(const struct XCSF *xcsf, const int action)
Returns the value of a specified action in the prediction array.
Definition: pa.c:178
void pa_build(const struct XCSF *xcsf, const double *x)
Builds the prediction array for the specified input.
Definition: pa.c:62
void pa_free(const struct XCSF *xcsf)
Frees the prediction array.
Definition: pa.c:192
int pa_best_action(const struct XCSF *xcsf)
Returns the best action in the prediction array.
Definition: pa.c:123
void pa_init(struct XCSF *xcsf)
Initialises the prediction array.
Definition: pa.c:46
Prediction array functions.
Classifier data structure.
Definition: xcsf.h:45
int action
Current classifier action.
Definition: xcsf.h:60
double fit
Fitness.
Definition: xcsf.h:53
double * prediction
Current classifier prediction.
Definition: xcsf.h:59
Classifier linked list.
Definition: xcsf.h:68
struct Clist * next
Pointer to the next list element.
Definition: xcsf.h:70
struct Cl * cl
Pointer to classifier data structure.
Definition: xcsf.h:69
Classifier set.
Definition: xcsf.h:76
int size
Number of macro-classifiers.
Definition: xcsf.h:78
struct Clist * list
Linked list of classifiers.
Definition: xcsf.h:77
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
Utility functions for random number handling, etc.
static int argmax(const double *X, const int N)
Returns the index of the largest element in vector X.
Definition: utils.h:86