gaRandomPopulation

Creates an Imsls_d_population data structure from randomly generated individuals.

Synopsis

gaRandomPopulation (n, chromosome)

Required Arguments

int n (Input)
The number of individuals to be randomly generated for the population.
Imsls_d_chromosome chromosome (Input)
A chromosome data structure created by gaChromosome describing the chromosome encoding for individuals.

Return Value

Function gaRandomPopulation returns an Imsls_d_population data structure, which is required input to geneticAlgorithm. The memory allocated to this data structure can be released using gaFreePopulation.

Optional Arguments

t_print, (Input)
By default, intermediate results are not printed. This option turns on printing of intermediate results.
grayEncoding, (Input)

Specifies whether alleles are encoded using Base-2 or Gray encoding for integer and real phenotypes.

Default: Base-2 encoding.

pmxCrossover, (Input)

This optional argument applies partially matched crossover to the nominal portion of the chromosome. Although gaRandomPopulation 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, …, nNominal-1.

Default: Standard crossover. Each nominal phenotype can independently have values from 0 to nNominal-1.

fitnessFcn, float fitness(Imsls_d_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.
binarySelectionProb, float[], (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, binarySelectionProb can be used to specify any Bernoulli distribution as the random selection model for individual binary phenotypes. binarySelectionProb is a one-dimensional array of length n_binary. binarySelectionProb[i] is equal to the probability that the i-th binary phenotype equals zero. Hence the probability it equals one is 1–binarySelectionProb[i].

nominalSelectionProb, float[] (Input)

The random selection model for randomly generating values for the nominal phenotypes are described by an array of length nCats, where

\[\mathit{nCats} = \sum_{\mathrm{i}=0}^{\mathrm{nNominal}-1} \mathrm{nCategories}[i]\]

By default all integer values between zero and nCategories[i]-1 are selected with equal probability. However, nominalSelectionProb can be used to specify any multinomial distribution as the random selection model for individual nominal phenotypes. nominalSelectionProb is a jagged two dimensional array. The values in the i-th row of this array contain the probability of selecting 0, 1, …, nCategories[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 nCategories[i]. See gaChromosome for a description of nCategories.

integerSelectionModel, int intSModel[], float iParms[] (Input/Output)

The random selection model for randomly generating values for the integer phenotypes. intSModel[i] declares the random selection model for the i-th integer phenotype. If intSModel[i]= 0, all integer values between the upper and lower limits specified in chromosome, iBounds[2i] and iBounds[2i+1], are selected with equal probability for integer i, i = 0, …, nInteger- 1. This is the default selection method for integer phenotypes. If intSModel[i]= 0, the contents of iParms[2i] and iParms[2i+1] are replaced with the lower limit of the interval and its width, respectively.

If intSModel[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 iParms[2i] is used as the mean for the Poisson distribution and the value of iParms[2i+1] is ignored.

realSelectionModel, int realSModel[], float rParms[] (Input/Output)

The random selection model for randomly generating values for the real phenotypes. realSModel[i] can be used to specify the random selection model for the i-th real phenotype. If realSModel[i]= 0, all real values between iBounds[2i] and iBounds[2i+1] are selected with equal probability using the uniform distribution. This is the default selection method for real phenotypes.

If realSModel[i]=1, then the Gaussian distribution is used. In this case, the value of rParms[2i] should be set to the mean of this distribution and rParms[2i+1] should equal its variance.

Description

The geneticAlgorithm operates on a population of individuals. gaRandomPopulation creates an initial population of n randomly selected individuals. gaRandomPopulation takes the chromosome structure described by the chromosome argument and randomly generates values for each phenotype. These are then encoded into Imsls_d_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 nCategories[i] values for the i-th nominal phenotype is 1/(chromosome.nCategories[i]).

By default, random values for the integer phenotypes are generated using the discrete uniform distribution. All values between iBounds[2i] and iBounds[2i+1] are sampled with equal probability. This default can be changed using the optional argument integerSelectionModel.

Likewise, random values for real phenotypes are generated using the continuous uniform random distribution. All values between rBounds[2i] and rBounds[2i+1] are sampled with equal probability. This default can be changed using the optional realSelectionModel argument.

Example

This example creates a population of 40 individuals each with 1 binary, 2 nominals, 3 integers and 2 real phenotypes. The t_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 fitness argument. If fitness is not initialized, the fitness array in the data structure is set to None. It can be initialized using an optional argument with geneticAlgorithm.

from __future__ import print_function
from numpy import *
from pyimsl.stat.free import free
from pyimsl.stat.ompOptions import ompOptions
from pyimsl.stat.gaChromosome import gaChromosome
from pyimsl.stat.gaRandomPopulation import gaRandomPopulation
from pyimsl.stat.gaFreePopulation import gaFreePopulation
from pyimsl.stat.randomSeedSet import randomSeedSet


def fitness(individual):
    # calculate fitness for this individual
    f = 100.0 + 10 * individual[0].binaryPhenotype[0]
    f += 2 * individual[0].nominalPhenotype[1] - \
        4 * individual[0].nominalPhenotype[0]
    f += 0.0001 * individual[0].integerPhenotype[0] + \
        abs(individual[0].integerPhenotype[1]
            + individual[0].integerPhenotype[2]) * 0.1
    f += 0.1 * individual[0].realPhenotype[0]
    if(individual[0].realPhenotype[1] > 0):
        f += 0.2 * individual[0].realPhenotype[1]
    else:
        f += -0.2 * individual[0].realPhenotype[1]
    return f


# number of phenotypes by category
n_binary = 1
n_nominal = 2
n_integer = 3
n_real = 2
n = 40                     # population size
# number of categories for nomial phenotypes
n_categories = [2, 3]
# number of intervals and boundaries for integer phenotypes
i_intervals = [16, 16, 16]
i_bounds = [[0, 1000], [-10, 10], [-20, 0]]
# number of intervals and boundaries for real phenotypes
r_intervals = [512, 1024]
r_bounds = [[0.0, 20.0], [-20.0, 20.0]]

##########################
# 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.
##########################
ompOptions(setFunctionsThreadSafe=1)

integer = {"iIntervals": i_intervals, "iBounds": i_bounds}
real = {"rIntervals": r_intervals, "rBounds": r_bounds}
chromosome = gaChromosome(binary=n_binary, nominal=n_categories,
                          integer=integer, real=real)

# Create individuals
randomSeedSet(12345)
print("Creating Population with ", n, " Individuals")
population = gaRandomPopulation(n, chromosome,
                                fitnessFcn=fitness, t_print=True)

print("Releasing Allocated Memory")
gaFreePopulation(population)

Output

The t_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 gaPopulation because the fitness function is identical and the random phenotype generation uses the same random seed.

Population Size: 40

Average   Fitness: 109.400072
Std. Dev. Fitness: 5.923696
Maximum Fitness: 120.044496
Minimum Fitness: 98.022013
Chromosome:
*******************************
**** CHROMOSOME STRUCTURE *****

Chromosome length:       34 Bits

*****BIT ASSIGNMENTS***********
Binary:    0 -   0 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.114512

           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************************************************
0 1 2 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 1 0 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.741793

           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.805905

           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 1 0 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 ********************************************Creating Population with  40  Individuals
Releasing Allocated Memory
****
   Number of Parents: 2
   Encoding: BASE-2
   Fitness: 114.571028

           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 1 2 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 
********************************************************************