XCSF  1.4.7
XCSF learning classifier system
sam.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 "sam.h"
25 #include "utils.h"
26 
27 #define MU_EPSILON 0.0005
28 #define N_RATES (10)
29 
33 static const double mrates[N_RATES] = { 0.0005, 0.001, 0.002, 0.003, 0.005,
34  0.01, 0.015, 0.02, 0.05, 0.1 };
35 
42 void
43 sam_init(double *mu, const int N, const int *type)
44 {
45  for (int i = 0; i < N; ++i) {
46  switch (type[i]) {
47  case SAM_LOG_NORMAL:
48  case SAM_UNIFORM:
49  mu[i] = rand_uniform(MU_EPSILON, 1);
50  break;
51  case SAM_RATE_SELECT:
52  mu[i] = mrates[rand_uniform_int(0, N_RATES)];
53  break;
54  default:
55  printf("sam_init(): invalid sam function: %d\n", type[i]);
56  exit(EXIT_FAILURE);
57  }
58  }
59 }
60 
67 void
68 sam_adapt(double *mu, const int N, const int *type)
69 {
70  for (int i = 0; i < N; ++i) {
71  switch (type[i]) {
72  case SAM_LOG_NORMAL:
73  mu[i] *= exp(rand_normal(0, 1));
74  mu[i] = clamp(mu[i], MU_EPSILON, 1);
75  break;
76  case SAM_RATE_SELECT:
77  if (rand_uniform(0, 1) < 0.1) {
78  mu[i] = mrates[rand_uniform_int(0, N_RATES)];
79  }
80  break;
81  case SAM_UNIFORM:
82  if (rand_uniform(0, 1) < 0.1) {
83  mu[i] = rand_uniform(MU_EPSILON, 1);
84  }
85  break;
86  default:
87  printf("sam_adapt(): invalid sam function: %d\n", type[i]);
88  exit(EXIT_FAILURE);
89  }
90  }
91 }
92 
99 void
100 sam_json_import(double *mu, const int N, const cJSON *json)
101 {
102  const cJSON *item = cJSON_GetObjectItem(json, "mutation");
103  if (item != NULL && cJSON_IsArray(item)) {
104  if (cJSON_GetArraySize(item) != N) {
105  printf("Import error: mutation length mismatch\n");
106  exit(EXIT_FAILURE);
107  }
108  for (int i = 0; i < N; ++i) {
109  const cJSON *item_i = cJSON_GetArrayItem(item, i);
110  if (item_i->valuedouble < 0 || item_i->valuedouble > 1) {
111  printf("Import error: mutation value out of bounds\n");
112  exit(EXIT_FAILURE);
113  }
114  mu[i] = item_i->valuedouble;
115  }
116  }
117 }
static const double mrates[(10)]
Values for rate selection adaptation.
Definition: sam.c:33
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
#define N_RATES
number of mutation rates for rate selection adaptation
Definition: sam.c:28
#define MU_EPSILON
smallest mutation rate allowable
Definition: sam.c:27
Self-adaptive mutation functions.
#define SAM_RATE_SELECT
Ten normally distributed rates.
Definition: sam.h:29
#define SAM_LOG_NORMAL
Log normal self-adaptation.
Definition: sam.h:28
#define SAM_UNIFORM
Uniformly random self-adaptation.
Definition: sam.h:30
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_normal(const double mu, const double sigma)
Returns a random Gaussian with specified mean and standard deviation.
Definition: utils.c:87
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.
static double clamp(const double a, const double min, const double max)
Returns a float clamped within the specified range.
Definition: utils.h:60