XCSF  1.4.7
XCSF learning classifier system
utils.h File Reference

Utility functions for random number handling, etc. More...

#include "../lib/cJSON/cJSON.h"
#include "../lib/dSFMT/dSFMT.h"
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
Include dependency graph for utils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

double rand_normal (const double mu, const double sigma)
 Returns a random Gaussian with specified mean and standard deviation. More...
 
double rand_uniform (const double min, const double max)
 Returns a uniform random float [min,max]. More...
 
int rand_uniform_int (const int min, const int max)
 Returns a uniform random integer [min,max] not inclusive of max. More...
 
void rand_init (void)
 Initialises the pseudo-random number generator. More...
 
void rand_init_seed (const uint32_t seed)
 Initialises the pseudo-random number generator with a fixed seed. More...
 
void utils_json_parse_check (const cJSON *json)
 Checks whether JSON parsed correctly. More...
 
static double clamp (const double a, const double min, const double max)
 Returns a float clamped within the specified range. More...
 
static int clamp_int (const int a, const int min, const int max)
 Returns an integer clamped within the specified range. More...
 
static int argmax (const double *X, const int N)
 Returns the index of the largest element in vector X. More...
 
static void float_to_binary (const double f, char *binary, const int bits)
 Generates a binary string from a float. More...
 
static void catch_error (const char *ret)
 Catches parameter value errors. More...
 
static bool check_array_eq (const double *arr1, const double *arr2, int size)
 Checks whether two double arrays are approximately equal. More...
 
static bool check_array_eq_int (const int *arr1, const int *arr2, int size)
 Checks whether two integer arrays are equal. More...
 

Detailed Description

Utility functions for random number handling, etc.

Author
Richard Preen rpree.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om
David Pätzel
Date
2015–2023.

Definition in file utils.h.

Function Documentation

◆ argmax()

static int argmax ( const double *  X,
const int  N 
)
static

Returns the index of the largest element in vector X.

First occurrence is selected in the case of a tie.

Parameters
[in]XVector with N elements.
[in]NThe number of elements in vector X.
Returns
The index of the largest element.

Definition at line 86 of file utils.h.

Referenced by act_neural_compute(), loss_onehot(), and pa_best_val().

Here is the caller graph for this function:

◆ catch_error()

static void catch_error ( const char *  ret)
static

Catches parameter value errors.

Parameters
[in]retString return type from JSON import.

Definition at line 134 of file utils.h.

Referenced by ea_param_json_import(), param_json_import_cl_general(), param_json_import_general(), param_json_import_multi(), and param_json_import_subsump().

Here is the caller graph for this function:

◆ check_array_eq()

static bool check_array_eq ( const double *  arr1,
const double *  arr2,
int  size 
)
static

Checks whether two double arrays are approximately equal.

Parameters
[in]arr1Array.
[in]arr2Array.
[in]sizeLength of the arrays.
Returns
Whether the arrays are equal.

Definition at line 150 of file utils.h.

◆ check_array_eq_int()

static bool check_array_eq_int ( const int *  arr1,
const int *  arr2,
int  size 
)
static

Checks whether two integer arrays are equal.

Parameters
[in]arr1Array.
[in]arr2Array.
[in]sizeLength of the arrays.
Returns
Whether the arrays are equal.

Definition at line 169 of file utils.h.

◆ clamp()

static double clamp ( const double  a,
const double  min,
const double  max 
)
static

Returns a float clamped within the specified range.

Parameters
[in]aThe value to be clamped.
[in]minMinimum value.
[in]maxMaximum value.
Returns
The clamped number.

Definition at line 60 of file utils.h.

Referenced by cond_ellipsoid_mutate(), cond_rectangle_mutate(), layer_mutate_eta(), layer_mutate_neurons(), layer_mutate_weights(), layer_weight_clamp(), neural_activate_array(), neural_layer_convolutional_mutate_filter(), node_activate(), pred_nlms_mutate(), sam_adapt(), and tree_eval().

Here is the caller graph for this function:

◆ clamp_int()

static int clamp_int ( const int  a,
const int  min,
const int  max 
)
static

Returns an integer clamped within the specified range.

Parameters
[in]aThe value to be clamped.
[in]minMinimum value.
[in]maxMaximum value.
Returns
The clamped number.

Definition at line 73 of file utils.h.

Referenced by rule_dgp_act_compute(), and rule_neural_act_compute().

Here is the caller graph for this function:

◆ float_to_binary()

static void float_to_binary ( const double  f,
char *  binary,
const int  bits 
)
static

Generates a binary string from a float.

Parameters
[in]fThe float to binarise.
[out]binaryThe converted binary string.
[in]bitsThe number of bits to use for binarising.

Definition at line 110 of file utils.h.

Referenced by cond_ternary_cover(), and cond_ternary_match().

Here is the caller graph for this function:

◆ rand_init()

void rand_init ( void  )

Initialises the pseudo-random number generator.

Definition at line 34 of file utils.c.

Referenced by param_set_random_state().

Here is the caller graph for this function:

◆ rand_init_seed()

void rand_init_seed ( const uint32_t  seed)

Initialises the pseudo-random number generator with a fixed seed.

Parameters
[in]seedRandom number seed.

Definition at line 50 of file utils.c.

Referenced by param_set_random_state().

Here is the caller graph for this function:

◆ rand_normal()

double rand_normal ( const double  mu,
const double  sigma 
)

Returns a random Gaussian with specified mean and standard deviation.

Box-Muller transform.

Parameters
[in]muMean.
[in]sigmaStandard deviation.
Returns
A random float.

Definition at line 87 of file utils.c.

Referenced by cond_ellipsoid_mutate(), cond_rectangle_mutate(), layer_add_neurons(), layer_ensure_input_represention(), layer_mutate_connectivity(), layer_mutate_eta(), layer_mutate_neurons(), layer_mutate_weights(), layer_weight_rand(), neural_layer_connected_init(), neural_layer_connected_resize(), neural_layer_convolutional_add_filters(), neural_layer_convolutional_init(), neural_layer_convolutional_mutate_filter(), neural_layer_convolutional_resize(), neural_layer_noise_forward(), pred_nlms_mutate(), and sam_adapt().

Here is the caller graph for this function:

◆ rand_uniform()

◆ rand_uniform_int()

int rand_uniform_int ( const int  min,
const int  max 
)

Returns a uniform random integer [min,max] not inclusive of max.

Parameters
[in]minMinimum value.
[in]maxMaximum value (non-inclusive).
Returns
A random integer.

Definition at line 74 of file utils.c.

References rand_uniform().

Referenced by act_integer_init(), act_integer_mutate(), ea_subsume(), env_maze_reset(), graph_mutate_functions(), graph_rand(), layer_ensure_input_represention(), layer_mutate_functions(), pa_best_action(), pa_rand_action(), random_connection(), sam_adapt(), sam_init(), tree_crossover(), tree_grow(), tree_mutate(), and xcs_supervised_sample().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ utils_json_parse_check()

void utils_json_parse_check ( const cJSON *  json)

Checks whether JSON parsed correctly.

Precondition
cJSON_Parse() called on the cJSON object.
Parameters
[in]jsoncJSON object.

Definition at line 109 of file utils.c.

Referenced by clset_json_insert(), XCS::json_insert_cl(), and param_json_import().

Here is the caller graph for this function: