GAlib A C++ Library of Genetic Algorithm Components Vanessa Herves Gómez [email protected] Department of Computer Architecture and Technology, Technical University of Madrid, Spain.

Download Report

Transcript GAlib A C++ Library of Genetic Algorithm Components Vanessa Herves Gómez [email protected] Department of Computer Architecture and Technology, Technical University of Madrid, Spain.

GAlib
A C++ Library of Genetic Algorithm
Components
Vanessa Herves Gómez [email protected]
Department of Computer Architecture and Technology,
Technical University of Madrid, Spain
Types of genetic algorithms (GAs)
 The library contains four types of genetic
algorithms:




Simple genetic algorithm
Steady state genetic algorithm
Incremental genetic algorithm
Deme genetic algorithm.
Scheme of a genetic algorithm
Resolve a problem with GAs
 In order to solve a problem using a genetic
algorithm you must do three things:



Define a representation
Define the genetic operators
Define the objective function → Defined by you
 To generate new individuals the genetic
algorithm uses:


Genetic operators
Selection/replacement strategies
Representation
 A single solution to the problem is represented
in a single data structure called genome.
 The library contains four types of genomes:




GAListGenome
GATreeGenome
GAArrayGenome
GABinaryStringGenome
The Genetic Operators
 Built into the genome.
 Each genome have three primary operators:
 Initialization
 Mutation
 Crossover
 GAlib comes whith these operators pre-defined
for each genome type.
 You can customize any of them.
 Each genome must also contain an objective
function and may also contain a comparator.
Selection strategies
 Is defined in the population.
 It is use by the genetic algorithms to choose which
individuals should mate.
 The library contains the following selection strategies:






GARankSelector
GARouletteWheelSelector
GATournamentSelector
GADSSelector
GASRSSelector
GAUniformSelector
Objective Functions and Fitness Scaling (I)
 Given a single solution to a problem, the
objective function provides a measure of how
good is it.
 The objective score is the value returned by your
objective function.
 The fitness score is a possibly-transformed
rating used by the GA to determinate the fitness
of individuals for mating.
 The genetic algoritm uses the fitness score to do
selection.
Objective Functions and Fitness Scaling (II)
 GAlib contains the following scaling scheme:





GANoScaling
GALinearScaling
GASigmaTruncationScaling
GaPowerLawScaling
GASharing
Stopping criterion
 You must tell the algorithm when to stop.
 The library contains the following stopping
criterion:



TerminateUponGeneration
TerminateUponConvergence
TerminateUponPopConvergence
Class Hierarchy (I)
a) GA hierarchy
a) Scaling hierarchy
a) Selection hierarchy
Class Hierarchy (II)
d) Genome Hierarchy
The form of a typical optimization problem
#include <ga/GASimpleGA.h>
#include <ga/GA1DBinStrGenome.h>
float Objective(GAGenome &);
main(int argc, char **argv){
int length = 10;
GA1DBinaryStringGenome genome(length, Objective); //create a genome
GASimpleGA ga(genome);
//create the genetic algorithm
ga.evolve();
//do the evolution
cout << ga.statistics() << endl;
//print out the results
}
float Objective(GAGenome & g) {
GA1DBinaryStringGenome & genome = (GA1DBinaryStringGenome &)g;
//put here your objective function
}
Parameters names (I)
Parameters names (II)
Change the behaviour of the GA (I)
 By setting various parameters:
ga.populationSize(popsize);
ga.nGenerations(ngen);
ga.pMutation(pmut);
ga.pCrossover(pcross);
 Parameters in comand line:
GAParameterList params;
GASimpleGA::registerDefaultParameters(params);
params.set(gaNnGenerations, 2000);
params.parse(argc, argv, gaFalse);
GA1DBinaryStringGenome genome(length, Objective);
GASimpleGA ga(genome);
ga.parameters(params);
Change the behaviour of the GA (II)
 GASigmaTruncationScaling scale;





Ga.scaling(scale);
GARankSelector scheme;
ga.selector(scheme);
ga.terminator(GAGeneticAlgorithm::TerminateUponCo
nvergence);
genome.crossover(GA2DBinaryStringGenome::OnePoi
ntCrossover);
genome.mutator(GA2DBinaryStringGenome::FlipMutat
or);
ga.replacement(GAIncrementalGA::RANDOM);
Define our own operators
int MyMutator(GAGenome&, float);
void MyInitializer(GAGenome&);
float MyComparator(const GAGenome&,
const GAGenome&);
int MyCrossover(const GAGenome&, const GAGenome&,
GAGenome*, GAGenome*);
GA1DBinaryStringGenome genome(20);
genome.initializer(MyInitializer);
genome.mutator(MyMutator);
genome.comparator(MyComparator);
genome.crossover(MyCrossover);
Traveling Salesman Problem (TSP)
 Given a finite set of cities C={c1, c2, …, cn} and
a distance d(ci,cj), i≠j, between each pair, the
TSP asks for finding a tour through all of the
cities, visiting each exactly once, and returning
to the originating city such that the total distance
traveled is minimized.
Format of the file that contains cities
City
X
Y
1
1
1
2
4
2
3
2
3
4
1
4
5
12
6
…
…
…
N -1
29
3
N
17
20
Genome and Initializer
 To resolve this problem:

Use GAListGenome. The cities are listed in
the order in which they are visited.
10

3
5
9
2
7
4
1
6
8
Define your own Initializer


Use GARandomInt(int, int);
Use a vector in which are stored the visited
cities.
Crossover
 Define your own crossover:

This crossover selects the first city of one parent,
compares the cities leaving that city in both
parents, and chooses the closer one to extend the
tour. If one city has already appeared in the tour,
we choose the other city. If both cities have
already appeared, we randomly select a nonselected city."
Mutator
 Define your own mutator


This mutator randomly select two cities from one
chromosome and swap them if the new
(swapped) tour length is shorter than the old one.
Example:
Before mutation
10
3
5
9
2
7
4
1
6
8
9
1
6
8
≥
After mutation
10
3
5
4
2
7