galib a c++ library of genetic algorithm components

23
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

Upload: jemima

Post on 10-Jan-2016

46 views

Category:

Documents


0 download

DESCRIPTION

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: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: GAlib A C++ Library of Genetic Algorithm Components

GAlibA C++ Library of Genetic Algorithm

Components

Vanessa Herves Gómez [email protected] of Computer Architecture and Technology,

Technical University of Madrid, Spain

Page 2: GAlib A C++ Library of Genetic Algorithm Components

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.

Page 3: GAlib A C++ Library of Genetic Algorithm Components

Scheme of a genetic algorithm

Page 4: GAlib A C++ Library of Genetic Algorithm Components

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

Page 5: GAlib A C++ Library of Genetic Algorithm Components

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

Page 6: GAlib A C++ Library of Genetic Algorithm Components

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.

Page 7: GAlib A C++ Library of Genetic Algorithm Components

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

Page 8: GAlib A C++ Library of Genetic Algorithm Components

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.

Page 9: GAlib A C++ Library of Genetic Algorithm Components

Objective Functions and Fitness Scaling (II)

GAlib contains the following scaling scheme: GANoScaling GALinearScaling GASigmaTruncationScaling GaPowerLawScaling GASharing

Page 10: GAlib A C++ Library of Genetic Algorithm Components

Stopping criterion

You must tell the algorithm when to stop. The library contains the following stopping

criterion: TerminateUponGeneration TerminateUponConvergence TerminateUponPopConvergence

Page 11: GAlib A C++ Library of Genetic Algorithm Components

Class Hierarchy (I)

a) GA hierarchy a) Scaling hierarchy a) Selection hierarchy

Page 12: GAlib A C++ Library of Genetic Algorithm Components

Class Hierarchy (II)

d) Genome Hierarchy

Page 13: GAlib A C++ Library of Genetic Algorithm Components

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

}

Page 14: GAlib A C++ Library of Genetic Algorithm Components

Parameters names (I)

Page 15: GAlib A C++ Library of Genetic Algorithm Components

Parameters names (II)

Page 16: GAlib A C++ Library of Genetic Algorithm Components

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

Page 17: GAlib A C++ Library of Genetic Algorithm Components

Change the behaviour of the GA (II)

GASigmaTruncationScaling scale;Ga.scaling(scale);

GARankSelector scheme;ga.selector(scheme);

ga.terminator(GAGeneticAlgorithm::TerminateUponConvergence);

genome.crossover(GA2DBinaryStringGenome::OnePointCrossover);

genome.mutator(GA2DBinaryStringGenome::FlipMutator);

ga.replacement(GAIncrementalGA::RANDOM);

Page 18: GAlib A C++ Library of Genetic Algorithm Components

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

Page 19: GAlib A C++ Library of Genetic Algorithm Components

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.

Page 20: GAlib A C++ Library of Genetic Algorithm Components

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

Page 21: GAlib A C++ Library of Genetic Algorithm Components

Genome and Initializer

To resolve this problem: Use GAListGenome. The cities are listed in

the order in which they are visited.

Define your own Initializer Use GARandomInt(int, int); Use a vector in which are stored the visited

cities.

10 3 5 9 2 7 4 1 6 8

Page 22: GAlib A C++ Library of Genetic Algorithm Components

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 non-selected city."

Page 23: GAlib A C++ Library of Genetic Algorithm Components

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:

10 3 5 9 2 7 4 1 6 8

10 3 5 4 2 7 9 1 6 8

Before mutation

After mutation