gaPopulation¶
Creates an Imsls_d_population data structure from user supplied individuals.
Synopsis¶
gaPopulation, (n, chromosome, individual)
Required Arguments¶
- int
n
(Input) - The number of individuals in the population.
- Imsls_d_chromosome
chromosome
(Input) - A chromosome data structure created by
gaChromosome
describing the chromosome encoding for individuals. - Imsls_d_individual
individual[]
(Input) - An array of
n
individuals.
Return Value¶
Function gaPopulation
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.
fitness
, float[]
(Input)- An array of length
n
containing the fitness values for the individuals in the population.fitness[i]
is the fitness for the i-th individual. fitnessFcn
, floatfitness
(
Imsls_d_individualindividual)
(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.
Description¶
The geneticAlgorithm operates on a population of
individuals. gaPopulation
allows users to systematically create an
initial population by adding individuals to that population. It takes the
individuals created using gaIndividual and
encapsulates them into an Imsls_d_population data structure.
Example¶
This example creates a population of 40 individuals each with one binary,
two nominal, three integer and two 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 ctypes import POINTER
from numpy import *
from pyimsl.stat.statStructs import Imsls_d_individual
from pyimsl.stat.free import free
from pyimsl.stat.gaChromosome import gaChromosome
from pyimsl.stat.gaIndividual import gaIndividual
from pyimsl.stat.gaPopulation import gaPopulation
from pyimsl.stat.gaFreeIndividual import gaFreeIndividual
from pyimsl.stat.gaFreePopulation import gaFreePopulation
from pyimsl.stat.randomBinomial import randomBinomial
from pyimsl.stat.randomSeedSet import randomSeedSet
from pyimsl.stat.randomUniform import randomUniform
from pyimsl.stat.randomUniformDiscrete import randomUniformDiscrete
# number of phenotypes by category
n_binary = 1
n_nominal = 2
n_integer = 3
n_real = 2
n = 40 # population size
binaryPhenotype = [1] # binary phenotype
# number of categories for nomial phenotypes
n_categories = [2, 3]
# nominal phenotype values
nominalPhenotype = [1, 2]
# number of intervals and boundaries for integer phenotypes
i_intervals = [16, 16, 16]
i_bounds = [[0, 1000], [-10, 10], [-20, 0]]
# integer phenotype values
integerPhenotype = [200, -5, -5]
# number of intervals and boundaries for real phenotypes
r_intervals = [512, 1024]
r_bounds = [[0.0, 20.0], [-20.0, 20.0]]
# real phenotype values
realPhenotype = [19.9, 19.9]
# fitness array for individuals
fitness = empty((40), dtype=double)
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)
randomSeedSet(12345)
# Create individuals
individualArrayType = (POINTER(Imsls_d_individual)) * 40
individuals = individualArrayType()
print("Creating ", n, " Individuals")
for i in range(0, n):
# generate random values for phenotypes
binaryPhenotype = randomBinomial(1, 1, 0.5)
irandom = randomUniformDiscrete(1, n_categories[0])
nominalPhenotype[0] = irandom[0] - 1
irandom = randomUniformDiscrete(1, n_categories[1])
nominalPhenotype[1] = irandom[0] - 1
irandom = randomUniformDiscrete(1, i_bounds[0][1] - i_bounds[0][0])
integerPhenotype[0] = irandom[0] - 1
irandom = randomUniformDiscrete(1, i_bounds[1][1] - i_bounds[1][0])
integerPhenotype[1] = irandom[0] - 1 + i_bounds[1][0]
irandom = randomUniformDiscrete(1, i_bounds[2][1] - i_bounds[2][0])
integerPhenotype[2] = irandom[0] - 1 + i_bounds[2][0]
rrandom = randomUniform(1)
realPhenotype[0] = \
rrandom[0] * (r_bounds[0][1] - r_bounds[0][0]) + r_bounds[0][0]
rrandom = randomUniform(1)
realPhenotype[1] = \
rrandom[0] * (r_bounds[1][1] - r_bounds[1][0]) + r_bounds[1][0]
# create individual from these phenotypes
individuals[i] = gaIndividual(chromosome,
binary=binaryPhenotype,
nominal=nominalPhenotype,
integer=integerPhenotype,
real=realPhenotype)
# calculate fitness for this individual
fitness[i] = 100.0 + 10 * binaryPhenotype[0]
fitness[i] += 2 * nominalPhenotype[1] - 4 * nominalPhenotype[0]
fitness[i] += 0.0001 * integerPhenotype[0] + \
abs(integerPhenotype[1] + integerPhenotype[2]) * 0.1
fitness[i] += 0.1 * realPhenotype[0]
if (realPhenotype[1] > 0):
fitness[i] += 0.2 * realPhenotype[1]
else:
fitness[i] -= 0.2 * realPhenotype[1]
print("Creating Population from ", n, " Individuals")
population = gaPopulation(n, chromosome, individuals,
fitness=fitness)
for i in range(0, n):
gaFreeIndividual(individuals[i])
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 integers and real phenotypes.
Creating 40 Individuals
Creating Population from 40 Individuals