Helper
This file contains the helper functions used all over the library. It is not intended to be used directly by the user but can be useful in certain situations.
floating point conversion routines
The floating point to binary (and back) conversions support the IEEE 754 standard and custom method proposed in here. Both these functions are intended for n > 2 matrices of binary values but support single array conversions.
Plotting routines
There is a function to plot 3d functions (like the ones provided in
Tests functions), this function is called
plot3d() and supports all plotting routines for 3d plots provided by
matplotlib.
Various functions
Sigmoid
The file also contains a function for the sigmoid function in its more simple form and a version containing all the different variables used in the sigmoid function. A derivative of the sigmoid function is also provided.
The simplified sigmoid function is called sigmoid() and is defined as,
The more complex version is called sigmoid2() with the following
definition,
The derivative of the sigmoid function is called sigmoid_derivative().
Decorator test
The is_decorated function test if a function is decorated with a decorator.
Convertpop2bin
Converts a population of binary values to a population of integers.
When using C
This file is also included in the C library and is used to convert the integers to binary values and in reverse. The functions are called:
integer to binary
int2bin()intarr2binarr()intmat2binmat()
binary to integer
bin2int()binarr2intarr()binmat2intmat()
Furthermore, the more high level bin to float and float to bin functions are also included in the C library. These functions are named like the python functions and can both be used for integers and floats.
These functions include:
ndbit2int()int2ndbit()
And use the same bias and factor parameters as the python functions to compute the floating point values.
Note
The C library does not initialise arrays/matrices these should be initialised before calling the functions in the form of a value-array, the amount of genes/individuals/bitsize and a correctly sized result-array. The functions will not check if the result-array is correctly sized and will overwrite any values in the result-array.
The C library also includes the sigmoid function and its derivative. These functions are named as in the python library. The sigmoid functions operate on arrays and are used in the same way as the conversion functions where it is expected that the result-array is correctly sized.
Furthermore, it is possible to print matrices of integers and floats in a similar manner to numpy
using the functions printMatrix() and printMatrixf().
And routines for performing roulette wheel selection on an array of probabilities using the function roulette_wheel().
Known errors
The conversion routines for ndbit2int and int2ndbit are prone to rounding errors. This is due to the fact that the conversion routines use the floating point representation of the integers. This means that the conversion routines are not exact and can be off by a small amount.
These errors become very present when working with large bitsizes (>32 bits) and when using small bitsizes (<4 bits). In C and python the error rates can be found in the figure below.
Speed-up of the C library
Below is a graph showing the speed-up of the C library compared to the python library for the conversion routines of the population as floats to binary and back. The tests are done for 16 bits populations with a size of 16 individuals where the amount of genes is varied between 1 and 40. The bias and factor are set to 0 and 5 respectively for the normalisation of the integer to floating point values.
Floating point to binary conversion using int2ndbit:
Binary to floating point conversion using ndbit2int:
Below is a graph showing the routines in Python:
The difference between the two routines in C:
Helper functions in Python:
Helper functions in C:
-
void int2bin(int *value, int *result, int bitsize)
Convert an integer to a bitarray
- Parameters
value (int) – The integer to be converted to a bitarray
bitsize (int) – is the size of the bitarray
result (array of ints (int *)) – is the bitarray to be filled with the converted values
- Returns
void
-
void intarr2binarr(int *value, int *result, int bitsize, int size)
Convert an array of integers to an array of bitarrays
- Parameters
valarr (array of ints (int *)) – The array of integers to be converted to bitarrays (a)
bitsize (int) – The size of the bitarrays
genes (int) – The number of genes in the bitarrays (n = genes / bitsize; n = a / bitsize)
result (array of ints (int *)) – The array of bitarrays to be filled with the converted values (n * bitsize)
- Returns
void
-
void intmat2binmat(int *value, int *result, int bitsize, int size)
Convert a matrix of integers to a matrix of bitarrays (a x b) (individuals x genes)
- Parameters
valmat (array of ints (int **)) – The matrix of integers to be converted to bitarrays (a x b) (individuals x genes)
bitsize (int) – The size of the bitarrays
genes (int) – The number of genes in the bitarrays (n = genes * bitsize; n = b * bitsize)
individuals (int) – The number of individuals in the bitarrays (m = individuals; m = a)
result (array of ints (int **)) – The matrix of bitarrays to be filled with the converted values (m x n)
-
int bin2int(int *value, int bitsize)
Convert a bitarray to an integer
- Parameters
value (array of ints (int *)) – The bitarray to be converted to an integer
bitsize (int) – The size of the bitarray
- Returns
The integer value of the bitarray
- Return type
int
-
void binarr2intarr(int *value, int *result, int bitsize, int size)
Convert an array of bitarrays to an array of integers
- Parameters
valarr (array of ints (int *)) – The array of bitarrays to be converted to integers (a)
bitsize (int) – The size of the bitarrays
genes (int) – The number of genes in the bitarrays (n = genes / bitsize; n = a / bitsize)
result (array of ints (int *)) – The array of integers to be filled with the converted values (n)
- Returns
void
-
void binmat2intmat(int *value, int *result, int bitsize, int size)
Convert a matrix of bitarrays to a matrix of integers (a x b) (individuals x genes)
- Parameters
valmat (array of ints (int **)) – The matrix of bitarrays to be converted to integers (a x b) (individuals x genes)
bitsize (int) – The size of the bitarrays
genes (int) – The number of genes in the bitarrays (n = genes * bitsize; n = b * bitsize)
individuals (int) – The number of individuals in the bitarrays (m = individuals; m = a)
result (array of ints (int **)) – The matrix of integers to be filled with the converted values (m x n)
-
void ndbit2int(int **valarr, int bitsize, int genes, int individuals, float factor, float bias, int normalised, float **result)
Convert an integer matrix to a floating point value matrix normalized by a bias and factor or to an integer matrix (of type float**) with integers between -2^bitsize and 2^bitsize - 1.
- Parameters
valarr (array of ints (int **)) – The matrix of integers to be converted to floating point values (a x b) (individuals x genes)
bitsize (int) – The size of the bitarrays
genes (int) – The number of genes in the bitarrays (n = genes * bitsize; n = b * bitsize)
individuals (int) – The number of individuals in the bitarrays (m = individuals; m = a)
factor (float) – The factor to be used for the conversion
bias (float) – The bias to be used for the conversion
normalised (int) – If 1, the values are normalized by the bias and factor, if 0, the values are not normalized
result (array of floats (float **)) – The matrix of floating point values to be filled with the converted values (m x n)
- Returns
void
-
void int2ndbit(float **valarr, int bitsize, int genes, int individuals, float factor, float bias, int normalised, int **result)
Convert a floating point value matrix normalized by a bias and factor or an integer matrix (of type float**) with integers between -2^bitsize and 2^bitsize - 1 to an integer matrix of binary arrays.
- Parameters
valarr (array of floats (float **)) – The matrix of floating point values to be converted to integers (a x b) (individuals x genes)
bitsize (int) – The size of the bitarrays
genes (int) – The number of genes in the bitarrays (n = genes * bitsize; n = b * bitsize)
individuals (int) – The number of individuals in the bitarrays (m = individuals; m = a)
factor (float) – The factor to be used for the conversion
bias (float) – The bias to be used for the conversion
normalised (int) – If 1, the values are normalized by the bias and factor, if 0, the values are not normalized
result (array of ints (int **)) – The matrix of integers to be filled with the converted values (m x n) (individuals x genes * bitsize)
-
void printMatrix(int **matrix, int rows, int cols)
Print a matrix of integers
- Parameters
matrix (array of ints (int **)) – The matrix to be printed
rows (int) – The number of rows in the matrix
cols (int) – The number of columns in the matrix
- Returns
void
-
void printMatrixf(float **matrix, int rows, int cols, int precision)
Print a matrix of floats
- Parameters
matrix (array of floats (float **)) – The matrix to be printed
rows (int) – The number of rows in the matrix
cols (int) – The number of columns in the matrix
precision (int) – The precision of the floats to be printed
- Returns
void
-
void sigmoid(float *value, float *result, int size)
Calculate the sigmoid of an array of floats
-
void sigmoid_derivative(float *value, float *result, int size)
Calculate the sigmoid derivative of an array of floats
-
void sigmoid2(float *x, float a, float b, float c, float d, float Q, float nu, float *result, int size)
Calculate the sigmoid of an array of floats
\[f(x) = \dfrac{a + (b - a)}{1 + Q \cdot \left( \exp(-c \cdot (x -d) \right)^{1/nu}}\]- Parameters
x (array of floats (float *)) – The array of floats to be converted to sigmoid values (a)
a (float) – The a parameter of the sigmoid function
b (float) – The b parameter of the sigmoid function
c (float) – The c parameter of the sigmoid function
d (float) – The d parameter of the sigmoid function
Q (float) – The Q parameter of the sigmoid function
nu (float) – The nu parameter of the sigmoid function
size (int) – The size of the array
result (array of floats (float *)) – The array of floats to be filled with the converted values (a)
- Returns
void
-
void uniform_random(int m, int n, int lower, int upper, int **result)
Create a matrix filled with uniformly distributed integers.
- Parameters
m (int) – Amount of rows
n (int) – Amount of cols
lower (int) – Lower bound of the distribution
upper (int) – Upper bound of the distribution
result (**int (m x n)) – Result matrix to which the distibutution is written to
- Returns
void
-
float gaussian(float x, float mu, float sigma)
Calculate the gaussian (pdf) of a float using the following equation:
\[f(x) = \frac{1}{\sigma \sqrt{2 \pi}} \exp \left( - \frac{(x - \mu)^2}{2 \sigma^2} \right)\]- Parameters
x (float) – The float to be converted to gaussian values
mu (float) – The mu parameter of the gaussian function
sigma (float) – The sigma parameter of the gaussian function
- Returns
The gaussian value of the float
- Return type
float
-
float cauchy(float x, float mu, float sigma)
Calculate the cauchy (pdf) of a float using the following equation:
\[f(x) = \frac{1}{\pi} \cdot [ \dfrac{\sigma}{( \frac{x - \mu})^2 + sigma^2} ]}\]- Parameters
x (float) – The float to be converted to cauchy values
mu (float) – The mu parameter of the cauchy function
sigma (float) – The sigma parameter of the cauchy function
- Returns
The cauchy value of the float
- Return type
float
-
void roulette_wheel(double *probabilities, int size, int ressize, int *result)
Roulette wheel selection of an index based on probabilities