Creates an Imsls_f_population data structure from randomly generated individuals.
#include <imsls.h>
Imsls_f_population
*imsls_f_ga_random_population (int
n,
Imsls_f_chromosome
*chromosome,…,0)
The type double function is imsls_d_ga_random_population.
int n (Input)
The
number of individuals to be randomly generated for the population.
Imsls_f_chromosome
*chromosome
(Input)
A chromosome data structure created by imsls_f_ga_chromosome describing the chromosome encoding for
individuals.
Function imsls_f_ga_random_population returns a pointer to an Imsls_f_population data structure, which is required input to imsls_f_genetic_algorithm. The memory allocated to this data structure can be released using imsls_f_ga_free_population.
#include <imsls.h>
Imsls_f_population
*imsls_f_ga_random_population (int
n,
Imsls_f_chromosome
*chromosome,
IMSLS_PRINT,
IMSLS_GRAY_ENCODING,
IMSLS_PMX_CROSSOVER,
IMSLS_FITNESS_FCN,
float
fitness(),
IMSLS_FITNESS_FCN_WITH_PARMS, float
fitness(), void
*parms,
IMSLS_BINARY_SELECTION_PROB, float
binary_prob[],
IMSLS_NOMINAL_SELECTION_PROB, float
nominal_prob[],
IMSLS_INTEGER_SELECTION_MODEL, int
int_s_model[], float
i_parms[],
IMSLS_REAL_SELECTION_MODEL,
int real_s_model[], float
r_parms[],
0)
IMSLS_PRINT
(Input)
By default, intermediate results are not printed. This
option turns on printing of intermediate results.
IMSLS_GRAY_ENCODING
(Input)
Specifies whether alleles are encoded using Base-2 or Gray encoding
for integer and real phenotypes.
Default: Base-2 encoding.
IMSLS_PMX_CROSSOVER (Input)
This
optional argument applies partially matched crossover to the nominal portion of
the chromosome. Although imsls_f_ga_random_population
does not perform crossover in the population, this option signals that the
nominal phenotypes are sequential with values consisting of an arrangement of
the integers 0, 1, …, n_nominal-1.
Default: Standard crossover. Each nominal phenotype can independently
have values from 0 to n_nominal-1.
IMSLS_FITNESS_FCN, float
fitness(Imsls_f_individual
*individual) (Input)
User-supplied function to calculate
fitness for individual. If
this is supplied, fitness values are calculated for each individual and included
within the expanded population data structure. Otherwise they are set to
zero.
IMSLS_FITNESS_FCN_WITH_PARMS, float
fitness(Imsls_f_individual *individual,
void
*parms), void
*parms, (Input)
User-supplied
function to calculate fitness for individual. If
this is supplied, fitness values are calculated for each individual and included
in the expanded population data structure. The parameters in parms are passed to
the function.
IMSLS_BINARY_SELECTION_PROB, float binary_prob[],
(Input)
The random selection model for randomly generating values for the
binary phenotypes. By default binary phenotype values are selected with
equal probability,
i.e. p(0) = p (1) = 0.5.
However,
binary_prob can
be used to specify any Bernoulli distribution as the random selection model for
individual binary phenotypes. binary_prob is a
one-dimensional array of length n_binary. binary_prob[i] is
equal to the probability that the i-th binary phenotype equals
zero. Hence the probability it equals one is 1-binary_prob[i].
IMSLS_NOMINAL_SELECTION_PROB, float
nominal_prob[] (Input)
The random selection model for
randomly generating values for the nominal phenotypes are described by an array
of length n_nominal, where
By default all integer values between zero and n_categories[i]-1 are selected with equal probability. However, nominal_prob can be used to specify any multinomial distribution as the random selection model for individual nominal phenotypes. nominal_prob is a jagged two dimensional array. The values in the i-th row of this array contain the probability of selecting 0, 1, …, n_categories[i]-1 for the i-th nominal attribute. These must be valid probabilities scaled between 0 and 1, and they must sum to 1.0. The number of values in the i-th row is equal to n_categories[i]. See imsls_f_ga_chromosome for a description of n_categories.
IMSLS_INTEGER_SELECTION_MODEL, int int_s_model[], float i_parms[]
(Input/Output)
The random selection model for randomly generating values for
the integer phenotypes. int_s_model[i]
declares the random selection model for the i-th integer phenotype.
If int_s_model[i]= 0, all
integer values between the upper and lower limits specified in chromosome, i_bounds[2i] and i_bounds[2i+1], are selected with equal probability for integer i,
i = 0, …, n_integer- 1.
This is the default selection method for integer phenotypes. If int_s_model[i]= 0, the
contents of i_parms[2i] and i_parms[2i+1] are replaced with
the lower limit of the interval and its width, respectively.
If int_s_model[i]=1,
the Poisson random selection model is used. The Poisson distribution
models a population of non-negative integers. If this model is selected,
then all values for the i-th integer phenotype must be
non-negative. The user supplied value of i_parms[2i] is used as the mean
for the Poisson distribution and the value of i_parms[2i+1] is ignored.
IMSLS_REAL_SELECTION_MODEL, int real_s_model[],
float
r_parms[] (Input/Output)
The random selection model for
randomly generating values for the real phenotypes. real_s_model[i] can be
used to specify the random selection model for the i-th real
phenotype. If real_s_model[i]= 0,
all real values between i_bounds[2i] and i_bounds[2i+1] are selected with
equal probability using the uniform distribution. This is the default
selection method for real phenotypes.
If real_s_model[i]=1,
then the Gaussian distribution is used. In this case, the value of r_parms[2i] should be set to
the mean of this distribution and r_parms[2i+1] should equal its
variance.
The imsls_f_genetic_algorithm operates on a population of individuals. imsls_f_ga_random_population creates an initial population of n randomly selected individuals. imsls_f_ga_random_population takes the chromosome structure described by the chromosome argument and randomly generates values for each phenotype. These are then encoded into Imsls_f_individual data structures and placed into the population.
Binary phenotypes are randomly generated Bernoulli random variables with p(0) = p(1)=0.5.
Values for nominal phenotypes are generated with equal probability. That is the probability of sampling each of the n_categories[i] values for the i-th nominal phenotype is 1/(chromosome->n_categories[i]).
By default, random values for the integer phenotypes are generated using the discrete uniform distribution. All values between i_bounds[2i] and i_bounds[2i+1] are sampled with equal probability. This default can be changed using the optional argument IMSLS_INTEGER_SELECTION_MODEL.
Likewise, random values for real phenotypes are generated using the continuous uniform random distribution. All values between r_bounds[2i] and r_bounds[2i+1] are sampled with equal probability. This default can be changed using the optional IMSLS_REAL_SELECTION_MODEL argument.
This example creates a population of 40 individuals each with with 1 binary, 2 nominals, 3 integers and 2 real phenotypes. The IMSLS_PRINT argument is used to print a description of the population. A simple fitness function calculation is used to illustrate how fitness values can be used to initialize a population with the IMSLS_FITNESS argument. If fitness is not initialized, the fitness array in the data structure is set to NULL. It can be initialized using an optional argument with imsls_f_genetic_algorithm.
#include <imsls.h>
#include <stdio.h>
#include <malloc.h>
#include <math.h>
int main(){
/* number of phenotypes by category */
int n_binary=1, n_nominal=2, n_integer=3, n_real=2;
int n = 40; /* population size */
/* number of categories for nomial phenotypes */
int n_categories[] = {2, 3};
/* number of intervals and boundaries for integer */
/* phenotypes */
int i_intervals[] = {16, 16, 16};
int i_bounds[] = {0, 1000, -10, 10, -20, 0};
/* number of intervals and boundaries for real */
/* phenotypes */
int r_intervals[] = {512, 1024};
float r_bounds[] = {0.0, 20.0, -20.0, 20.0};
/* Fittness Function */
static float fitness(Imsls_f_individual* individual);
/* Chromosome Data Structure */
Imsls_f_chromosome* chromosome;
/* Population Data Structure */
Imsls_f_population* population;
/**************************************************/
/* In this example the user function is thread */
/* safe. Let CNL know it is safe, which allows *
/* genetic algorithm to run in parallel, if that */
/* capability exists on the user computer. */
imsls_omp_options(IMSLS_SET_FUNCTIONS_THREAD_SAFE, 1, 0);
chromosome = imsls_f_ga_chromosome(
IMSLS_BINARY, n_binary,
IMSLS_NOMINAL, n_nominal, n_categories,
IMSLS_INTEGER, n_integer, i_intervals, i_bounds,
IMSLS_REAL, n_real, r_intervals, r_bounds, 0);
/* Create individuals */
imsls_random_seed_set(12345);
printf("Creating Population with %d Individuals\n", n);
population = imsls_f_ga_random_population(n, chromosome,
IMSLS_FITNESS_FCN, fitness,
IMSLS_PRINT, 0);
printf("Releasing Allocated Memory\n");
free(chromosome);
imsls_f_ga_free_population(population);
return;
}
static float fitness(Imsls_f_individual* individual){
float f;
/* calculate fitness for this individual */
f = 100.0 + 10*individual->binaryPhenotype[0];
f += 2*individual->nominalPhenotype[1] - 4*individual->nominalPhenotype[0];
f += 0.0001*individual->integerPhenotype[0] +
abs(individual->integerPhenotype[1]+individual->integerPhenotype[2])*0.1;
f += 0.1*individual->realPhenotype[0];
if(individual->realPhenotype[1]>0) f += 0.2*individual->realPhenotype[1];
else f += -0.2*individual->realPhenotype[1];
return f;
}
The IMSLS_PRINT option produced the following description of the population. A summary of the population chromosome structure and fitness are printed followed by detailed information for the first 5 individuals in the population.
This example also illustrates the bit ordering within chromosomes. Nominal phenotypes are placed in the first bits followed by binary and encoded integer and real phenotypes. Note that this output is identical to the example for imsls_f_ga_population because the fitness function is identical and the random phenotype generation uses the same random seed.
Creating Population with 40 Individuals
Population Size: 40
Average Fitness: 109.400070
Std. Dev. Fitness: 5.923696
Maximum Fitness: 120.044495
Minimum Fitness: 98.022011
Chromosome:
*******************************
**** CHROMOSOME STRUCTURE *****
Chromosome length: 34 Bits
*****BIT ASSIGNMENTS***********
Binary: 2 - 2 n_binary = 1
Nominal: 1 - 2 n_nominal= 2
Integer: 3 - 14 n_integer= 3
Real: 15 - 33 n_real = 2
*******************************
NOMINAL CATEGORIES*************
Variable 0: 2 categories
Variable 1: 3 categories
*******************************
INTEGER BOUNDS*****************
Variable 0: [ 0, 1000]
Variable 1: [ -10, 10]
Variable 2: [ -20, 0]
*******************************
INTEGER BITS*******************
Variable 0: 4 bits
Variable 1: 4 bits
Variable 2: 4 bits
*******************************
INTEGER DISCRETE INTERVALS*****
Variable 0: 16 intervals
Variable 1: 16 intervals
Variable 2: 16 intervals
*******************************
REAL BOUNDS********************
Variable 0: [0,20]
Variable 1: [-20,20]
*******************************
REAL BITS**********************
Variable 0: 9 bits
Variable 1: 10 bits
*******************************
REAL DISCRETE INTERVALS********
Variable 0: 512 intervals
Variable 1: 1024 intervals
*******************************
First 5 Individuals
****** INDIVIDUAL 0 ************************************************
Number of Parents: 2
Encoding: BASE-2
Fitness: 105.114510
PHENOTYPES
*************BINARY************
Variable 0: 0
************NOMINAL************
Variable 0: 1
Variable 1: 2
************INTEGER************
Variable 0: 35
Variable 1: -10
Variable 2: -19
**************REAL*************
Variable 0: 15.3157
Variable 1: 3.39719
**********CHROMOSOME************************************************
1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 1 0 1 0 1 1 0
********************************************************************
****** INDIVIDUAL 1 ************************************************
Number of Parents: 2
Encoding: BASE-2
Fitness: 111.696175
PHENOTYPES
*************BINARY************
Variable 0: 1
************NOMINAL************
Variable 0: 1
Variable 1: 0
************INTEGER************
Variable 0: 195
Variable 1: -5
Variable 2: -4
**************REAL*************
Variable 0: 19.6777
Variable 1: -14.0445
**********CHROMOSOME************************************************
1 0 1 0 0 1 1 0 1 0 0 1 1 0 0 1 1 1 1 1 0 1 1 1 0 0 1 0 0 1 1 0 0 0
********************************************************************
****** INDIVIDUAL 2 ************************************************
Number of Parents: 2
Encoding: BASE-2
Fitness: 104.741791
PHENOTYPES
*************BINARY************
Variable 0: 0
************NOMINAL************
Variable 0: 0
Variable 1: 0
************INTEGER************
Variable 0: 167
Variable 1: 8
Variable 2: -16
**************REAL*************
Variable 0: 18.3331
Variable 1: -10.4589
**********CHROMOSOME************************************************
0 0 0 0 0 1 0 1 1 1 0 0 0 1 1 1 1 1 0 1 0 1 0 1 0 0 1 1 1 1 0 1 0 0
********************************************************************
****** INDIVIDUAL 3 ************************************************
Number of Parents: 2
Encoding: BASE-2
Fitness: 110.805908
PHENOTYPES
*************BINARY************
Variable 0: 1
************NOMINAL************
Variable 0: 1
Variable 1: 0
************INTEGER************
Variable 0: 630
Variable 1: 0
Variable 2: -16
**************REAL*************
Variable 0: 18.213
Variable 1: -6.608
**********CHROMOSOME************************************************
1 0 1 1 0 1 0 1 0 0 0 0 0 1 1 1 1 1 0 1 0 0 1 0 0 1 0 1 0 1 0 1 1 0
********************************************************************
****** INDIVIDUAL 4 ************************************************
Number of Parents: 2
Encoding: BASE-2
Fitness: 114.571030
PHENOTYPES
*************BINARY************
Variable 0: 1
************NOMINAL************
Variable 0: 1
Variable 1: 2
************INTEGER************
Variable 0: 51
Variable 1: 9
Variable 2: -2
**************REAL*************
Variable 0: 7.13049
Variable 1: -15.7644
**********CHROMOSOME************************************************
1 2 1 0 0 0 0 1 1 1 1 1 1 1 0 0 1 0 1 1 0 1 1 0 0 0 0 1 1 0 1 1 0 0
********************************************************************
Releasing Allocated Memory