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:
- dfmcontrol.Helper.helper.Ndbit2floatIEEE754(valarr: numpy.ndarray, bitsize: int, **kwargs) numpy.ndarray
Conversion of bit m x n (big endian) bit array (numpy) to IEEE 754 double precision float
- Parameters
valarr – m x n ndarray of numpy integers representing a bit
bitsize – Size of the bit, big endian
kwargs –
- Returns
m x n/bitsize ndarray of IEEE 754 double precision floats
- dfmcontrol.Helper.helper.b2dfloat(bit: numpy.ndarray) numpy.ndarray
Conversion of bit m x n (big endian) bit array (numpy) to IEEE 754 double precision float according to Jaime’s solution (https://stackoverflow.com/questions/26018448/convert-a-binary-string-into-ieee-754-single-precision-python) :param bit: m x n ndarray of numpy integers representing a bit :return: m x n ndarray of IEEE 754 double precision floats
- dfmcontrol.Helper.helper.b2int(bit: numpy.ndarray) numpy.ndarray
Conversion of m x n (big endian) bit array to integers. :param bit: m x n ndarray of numpy integers (0, 1) representing a bit :return: m x n ndarray of integers
- dfmcontrol.Helper.helper.b2sfloat(bit: numpy.ndarray) numpy.ndarray
Conversion of m x n (big endian) bit array (numpy) to IEEE 754 single precision float according to Jaime’s solution (https://stackoverflow.com/questions/26018448/convert-a-binary-string-into-ieee-754-single-precision-python) :param bit: m x n ndarray of numpy integers representing a bit :return: m x n ndarray of IEEE 754 single precision floats
- dfmcontrol.Helper.helper.bitdict(*args)
- dfmcontrol.Helper.helper.convertpop2n(bit2num=None, target=None, bitsize=None, **kwargs)
FROM genetic_algortim.get_numeric() TO BE USED IN OTHER FILES.
Convert results list with np.ndarrays of dimension mx1 to numeric data using provided method or builtin routine for multi variable float conversion. Provided method needs to accept np.ndarray with binary information stored as dtype np.uint8 and return np.ndarray of shape nx1 contaning numeric data of float, int. Example def b2int(bit: np.ndarray) -> np.ndarray:
‘’’ Conversion of m x n (big endian) bit array to integers. :param bit2num: m x n ndarray of numpy integers (0, 1) representing a bit :return: m x n ndarray of integers ‘’’ m, n = bit.shape a = 2 ** np.arange(n) return bit @ a
- Parameters
bit2num – Binary to numeric conversion routine
kwargs – kwargs for bit2num
- Returns
numeric values for results of the loaded GA data
- dfmcontrol.Helper.helper.decdicts(func)
- dfmcontrol.Helper.helper.flip_a_coin() bool
Flip a coin function with 50/50 chance for True/False
- dfmcontrol.Helper.helper.float2NdbitIEEE754(valarr: numpy.ndarray, bitsize: int) numpy.ndarray
Conversion of bit m x n (big endian) bit array (numpy) to IEEE 754 double precision float :param valarr: m x n ndarray of numpy integers representing a bit :param bitsize: Size of the bit, big endian :return: m x n/bitsize ndarray of IEEE 754 double precision floats
- dfmcontrol.Helper.helper.floatToBinary32(val: numpy.float32)
https://www.technical-recipes.com/2012/converting-between-binary-and-decimal-representations-of-ieee-754-floating-point-numbers-in-c/ :param value: float :return: binary representation of float
- dfmcontrol.Helper.helper.floatToBinary64(val: numpy.float64)
Conversion of float to binary representation
https://www.technical-recipes.com/2012/converting-between-binary-and-decimal-representations-of-ieee-754-floating-point-numbers-in-c/ :param value: float :return: binary representation of float
- dfmcontrol.Helper.helper.floatdict(*args)
- dfmcontrol.Helper.helper.int2ndbit(valarr: numpy.ndarray, bitsize: int, **kwargs)
Convert an array of integers (or floats) to a bit array of size bitsize * len(valarr) if the valarr is a float, the float will be normalised to the range 0 to 1 and a factor / bias may be applied if specified in kwargs. So that
float = valarr / factor - bias
The float will then be assigned an integer value between 0 and 2**bitsize-1
int = int(float * 2**bitsize-1) # Rounding error may occur
The integer will then be converted to a bit array of size bitsize.
- Parameters
valarr – MxN matrix of integers
bitsize – Size of the bit, big endian, first bit is sign the others are val
kwargs – factor: float / bias: float
- Returns
MxN matrix of 0, 1 with dtype np.uint8
- dfmcontrol.Helper.helper.int_to_binary(integer: int, size: int) numpy.ndarray
https://stackoverflow.com/questions/699866/python-int-to-binary
- Parameters
integer – integer to be converted
size – size of binary representation
- Returns
binary representation of integer
- dfmcontrol.Helper.helper.is_decorated(func: Callable) bool
Check if function is decorated
- Parameters
func – function of type function
- Returns
True if decorated, False if not
- dfmcontrol.Helper.helper.ndbit2int(valarr: numpy.ndarray, bitsize: int, normalised: bool = True, **kwargs)
Conversion of bit m x n (big endian) bit array (numpy) to integer or float depending on the normalised flag. If normalised is true, the integer is normalised to the range 0 to 1 and a factor / bias may be applied if specified in kwargs.
The factor / bias is applied to the integer after normalisation so that the conversion is done as follows
float = (b2n(valarr)/2^(bitsize) * factor) + bias
- Parameters
valarr – MxN matrix of 0, 1 with dtype np.uint8 with M arrays and N the length of val * bitsize
bitsize – Size of the bit, big endian, first bit is sign the others are val
normalised – divide by 2**bitsize-1 and apply factor / bias if true
kwargs – factor: float / bias: float
- Returns
MxN matrix of integers or floats (np.float64)
- dfmcontrol.Helper.helper.plot3d(fx, min, max, resolution=100, mode='plot_surface', **kwargs)
Plot 3d function
- Parameters
fx – function to plot
min – min value of x and y
max – max value of x and y
resolution – resolution of the plot
mode – plot_surface or contour
kwargs – kwargs for plot_surface, contour / show = True
- Returns
None if show = True, else fig, ax
- dfmcontrol.Helper.helper.sigmoid(x)
Sigmoid function
- Parameters
x – input
- Returns
sigmoid(x)
- dfmcontrol.Helper.helper.sigmoid2(x, a=1, b=-1, c=0.5, d=0, Q=0.5, nu=1)
Sigmoid function
- Parameters
x – input
a –
b –
c –
d –
Q –
nu –
- Returns
sigmoid(x)
- dfmcontrol.Helper.helper.sigmoid_derivative(x)
Derivative of sigmoid function
- Parameters
x – input
- Returns
sigmoid_derivative(x)
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