ga_population

Creates an Imsls_f_population data structure from user supplied individuals.

Synopsis

#include <imsls.h>

Imsls_f_population *imsls_f_ga_population, (int n, Imsls_f_chromosome *chromosome, Imsls_f_individual *individual[], ..., 0)

The type double function is imsls_d_ga_population.

Required Arguments

int n (Input)
The number of individuals in the population.

Imsls_f_chromosome *chromosome (Input)
A chromosome data structure created by imsls_f_ga_chromosome describing the chromosome encoding for individuals.

Imsls_f_individual *individual[] (Input)
An array of pointers to n individuals.

Return Value

Function imsls_f_ga_population returns 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.

Synopsis with Optional Arguments

#include <imsls.h>

Imsls_f_population *imsls_f_ga_population (int n, Imsls_f_chromosome *chromosome, Imsls_f_individual *individual[],

IMSLS_PRINT,

IMSLS_GRAY_ENCODING,

IMSLS_FITNESS, float fitness[],

IMSLS_FITNESS_FCN, float fitness(),

IMSLS_FITNESS_FCN_WITH_PARMS, float fitness(), void *parms,

0)

Optional Arguments

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_FITNESS, float fitness[] (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.

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.

Description

The imsls_f_genetic_algorithm operates on a population of individuals. ga_population allows users to systematically create an initial population by adding individuals to that population. It takes the individuals created using imsls_f_ga_individual and encapsulates them into an Imsls_f_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 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 <stdlib.h>

int main(){

int i;

/* number of phenotypes by category */

int n_binary=1, n_nominal=2, n_integer=3, n_real=2;

int n = 40; /* population size */

int irandom[1]; /* temporary working storage */

float rrandom[1]; /* temporary working storage */

int binaryPhenotype[] = {1}; /* binary phenotype */

/* number of categories for nomial phenotypes */

int n_categories[] = {2, 3};

/* nominal phenotype values */

int nominalPhenotype[] = {1, 2};

/* number of intervals and boundaries for integer */

/* phenotypes */

int i_intervals[] = {16, 16, 16};

int i_bounds[] = {0, 1000, -10, 10, -20, 0};

/* integer phenotype values */

int integerPhenotype[] = {200, -5, -5};

/* number of intervals and boundaries for real */

/* phenotypes */

int r_intervals[] = {512, 1024};

float r_bounds[] = {0.0, 20.0, -20.0, 20.0};

/* real phenotype values */

float realPhenotype[] = {19.9, 19.9};

/* fitness array for individuals */

float fitness[40];

/* Chromosome Data Structure */

Imsls_f_chromosome* chromosome=NULL;

/* Individual Data Structure */

Imsls_f_individual* individuals[40];

/* Population Data Structure */

Imsls_f_population* population=NULL;

 

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);

 

imsls_random_seed_set(12345);

/* Create individuals */

printf("Creating %d Individuals\n", n);

for(i=0; i<n; i++){

/* generate random values for phenotypes */

imsls_f_random_binomial(1, 1, 0.5,

IMSLS_RETURN_USER, binaryPhenotype, 0);

imsls_f_random_uniform_discrete(1, n_categories[0],

IMSLS_RETURN_USER, irandom, 0);

nominalPhenotype[0] = irandom[0]-1;

imsls_f_random_uniform_discrete(1, n_categories[1],

IMSLS_RETURN_USER, irandom, 0);

nominalPhenotype[1] = irandom[0]-1;

imsls_f_random_uniform_discrete(1, i_bounds[1]-i_bounds[0],

IMSLS_RETURN_USER, irandom, 0);

integerPhenotype[0] = irandom[0]-1;

imsls_f_random_uniform_discrete(1, i_bounds[3]-i_bounds[2],

IMSLS_RETURN_USER, irandom, 0);

integerPhenotype[1] = irandom[0]-1+i_bounds[2];

imsls_f_random_uniform_discrete(1, i_bounds[5]-i_bounds[4],

IMSLS_RETURN_USER, irandom, 0);

integerPhenotype[2] = irandom[0]-1+i_bounds[4];

imsls_f_random_uniform(1, IMSLS_RETURN_USER, rrandom, 0);

realPhenotype[0] =

rrandom[0] * (r_bounds[1]-r_bounds[0]) + r_bounds[0];

imsls_f_random_uniform(1, IMSLS_RETURN_USER, rrandom, 0);

realPhenotype[1] =

rrandom[0]*(r_bounds[3]-r_bounds[2]) + r_bounds[2];

/* create individual from these phenotypes */

individuals[i] = imsls_f_ga_individual(chromosome,

IMSLS_BINARY, binaryPhenotype,

IMSLS_NOMINAL, nominalPhenotype,

IMSLS_INTEGER, integerPhenotype,

IMSLS_REAL, realPhenotype, 0);

/* 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];

}

 

printf("Creating Population from %d Individuals\n", n);

 

population = imsls_f_ga_population(n, chromosome, individuals,

IMSLS_FITNESS, fitness, IMSLS_PRINT, 0);

 

imsls_free(chromosome);

for(i=0; i<n; i++) imsls_f_ga_free_individual(individuals[i]);

imsls_f_ga_free_population(population);

}

Output

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 integers and real phenotypes.

 

Creating 40 Individuals

Creating Population from 40 Individuals

Population Size: 40

 

Average Fitness: 109.472527

Std. Dev. Fitness: 5.927261

Maximum Fitness: 120.244392

Minimum Fitness: 98.221916

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

 

PHENOTYPES

*************BINARY************

Variable 0: 1

************NOMINAL************

Variable 0: 1

Variable 1: 0

************INTEGER************

Variable 0: 195

Variable 1: -5

Variable 2: -5

**************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.841797

 

PHENOTYPES

*************BINARY************

Variable 0: 0

************NOMINAL************

Variable 0: 0

Variable 1: 0

************INTEGER************

Variable 0: 167

Variable 1: 7

Variable 2: -16

**************REAL*************

Variable 0: 18.3331

Variable 1: -10.4589

 

**********CHROMOSOME************************************************

0 0 0 0 0 1 0 1 1 0 1 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.905807

 

PHENOTYPES

*************BINARY************

Variable 0: 1

************NOMINAL************

Variable 0: 1

Variable 1: 0

************INTEGER************

Variable 0: 629

Variable 1: 0

Variable 2: -17

**************REAL*************

Variable 0: 18.213

Variable 1: -6.608

 

**********CHROMOSOME************************************************

1 0 1 1 0 1 0 1 0 0 0 0 0 1 0 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.371025

 

PHENOTYPES

*************BINARY************

Variable 0: 1

************NOMINAL************

Variable 0: 1

Variable 1: 2

************INTEGER************

Variable 0: 51

Variable 1: 8

Variable 2: -3

**************REAL*************

Variable 0: 7.13049

Variable 1: -15.7644

 

**********CHROMOSOME************************************************

1 2 1 0 0 0 0 1 1 1 0 1 1 0 1 0 1 0 1 1 0 1 1 0 0 0 0 1 1 0 1 1 0 0

********************************************************************