XCSF 1.4.8
XCSF learning classifier system
Loading...
Searching...
No Matches
utils.h
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
25#pragma once
26
27#include "../lib/cJSON/cJSON.h"
28#include "../lib/dSFMT/dSFMT.h"
29#include <math.h>
30#include <stdbool.h>
31#include <stdio.h>
32#include <stdlib.h>
33
34double
35rand_normal(const double mu, const double sigma);
36
37double
38rand_uniform(const double min, const double max);
39
40int
41rand_uniform_int(const int min, const int max);
42
43void
44rand_init(void);
45
46void
47rand_init_seed(const uint32_t seed);
48
49void
50utils_json_parse_check(const cJSON *json);
51
59static inline double
60clamp(const double a, const double min, const double max)
61{
62 return (a < min) ? min : (a > max) ? max : a;
63}
64
72static inline int
73clamp_int(const int a, const int min, const int max)
74{
75 return (a < min) ? min : (a > max) ? max : a;
76}
77
85static inline int
86argmax(const double *X, const int N)
87{
88 if (N < 1) {
89 printf("argmax() error: N < 1\n");
90 exit(EXIT_FAILURE);
91 }
92 int max_i = 0;
93 double max = X[0];
94 for (int i = 1; i < N; ++i) {
95 if (X[i] > max) {
96 max_i = i;
97 max = X[i];
98 }
99 }
100 return max_i;
101}
102
109static inline void
110float_to_binary(const double f, char *binary, const int bits)
111{
112 if (f >= 1) {
113 for (int i = 0; i < bits; ++i) {
114 binary[i] = '1';
115 }
116 } else if (f <= 0) {
117 for (int i = 0; i < bits; ++i) {
118 binary[i] = '0';
119 }
120 } else {
121 int a = (int) (f * pow(2, bits));
122 for (int i = 0; i < bits; ++i) {
123 binary[bits - 1 - i] = (a % 2) + '0';
124 a /= 2;
125 }
126 }
127}
128
133static inline void
134catch_error(const char *ret)
135{
136 if (ret != NULL) {
137 printf("%s\n", ret);
138 exit(EXIT_FAILURE);
139 }
140}
141
149static inline bool
150check_array_eq(const double *arr1, const double *arr2, int size)
151{
152 const double tol = 1e-5;
153 for (int i = 0; i < size; ++i) {
154 if (arr1[i] < arr2[i] - tol || arr1[i] > arr2[i] + tol) {
155 return false;
156 }
157 }
158 return true;
159}
160
168static inline bool
169check_array_eq_int(const int *arr1, const int *arr2, int size)
170{
171 for (int i = 0; i < size; ++i) {
172 if (arr1[i] != arr2[i]) {
173 return false;
174 }
175 }
176 return true;
177}
static int clamp_int(const int a, const int min, const int max)
Returns an integer clamped within the specified range.
Definition utils.h:73
static double clamp(const double a, const double min, const double max)
Returns a float clamped within the specified range.
Definition utils.h:60
static bool check_array_eq_int(const int *arr1, const int *arr2, int size)
Checks whether two integer arrays are equal.
Definition utils.h:169
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
static bool check_array_eq(const double *arr1, const double *arr2, int size)
Checks whether two double arrays are approximately equal.
Definition utils.h:150
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
static void float_to_binary(const double f, char *binary, const int bits)
Generates a binary string from a float.
Definition utils.h:110
void rand_init_seed(const uint32_t seed)
Initialises the pseudo-random number generator with a fixed seed.
Definition utils.c:50
static void catch_error(const char *ret)
Catches parameter value errors.
Definition utils.h:134
static int argmax(const double *X, const int N)
Returns the index of the largest element in vector X.
Definition utils.h:86