1 wendy williams metaheuristic algorithms genetic algorithms: a tutorial “genetic algorithms are...

25
1 Wendy Williams Metaheuristic Algorithms Genetic Algorithms: A Tutorial “Genetic Algorithms are good at taking large, potentially huge search spaces and navigating them, looking for optimal combinations of things, solutions you might not otherwise find in a lifetime.” - Salvatore Mangano Computer Design, May 1995 Genetic Algorithms: Genetic Algorithms: A Tutorial A Tutorial

Upload: clifford-manship

Post on 01-Apr-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Wendy Williams Metaheuristic Algorithms Genetic Algorithms: A Tutorial “Genetic Algorithms are good at taking large, potentially huge search spaces and

1Wendy WilliamsMetaheuristic Algorithms

Genetic Algorithms: A Tutorial

“Genetic Algorithms are good at taking large,

potentially huge search spaces and navigating

them, looking for optimal combinations of things, solutions you might not

otherwise find in a lifetime.”

- Salvatore Mangano

Computer Design, May 1995

Genetic Algorithms:Genetic Algorithms:A TutorialA Tutorial

Page 2: 1 Wendy Williams Metaheuristic Algorithms Genetic Algorithms: A Tutorial “Genetic Algorithms are good at taking large, potentially huge search spaces and

2Wendy WilliamsMetaheuristic Algorithms

Genetic Algorithms: A Tutorial

A Simple Example

The Traveling Salesman Problem:

Find a tour of a given set of cities so that each city is visited only once the total distance traveled is minimized

Page 3: 1 Wendy Williams Metaheuristic Algorithms Genetic Algorithms: A Tutorial “Genetic Algorithms are good at taking large, potentially huge search spaces and

3Wendy WilliamsMetaheuristic Algorithms

Genetic Algorithms: A Tutorial

Classes of Search Techniques

F inonacc i N ew ton

D irect m ethods Indirec t m ethods

C alcu lus-based techn iques

E volu tionary s trategies

C entra l ized D is tr ibuted

Para l le l

S teady-s ta te G enera tiona l

S equentia l

G ene tic a lgori thm s

E volutionary a lgori thm s S im u lated annealing

G uided random search techniques

D ynam ic program m ing

E num erative techn iques

S earch techniques

Page 4: 1 Wendy Williams Metaheuristic Algorithms Genetic Algorithms: A Tutorial “Genetic Algorithms are good at taking large, potentially huge search spaces and

4Wendy WilliamsMetaheuristic Algorithms

Genetic Algorithms: A Tutorial

Components of a GA

A problem to solve, and ... Encoding technique (gene, chromosome)

Initialization procedure (creation)

Evaluation function (environment)

Selection of parents (reproduction)

Genetic operators (mutation, recombination)

Parameter settings (practice and art)

Page 5: 1 Wendy Williams Metaheuristic Algorithms Genetic Algorithms: A Tutorial “Genetic Algorithms are good at taking large, potentially huge search spaces and

5Wendy WilliamsMetaheuristic Algorithms

Genetic Algorithms: A Tutorial

Simple Genetic Algorithm

{

initialize population;

evaluate population;

while TerminationCriteriaNotSatisfied{

select parents for reproduction;

perform recombination and mutation;

evaluate population;}

}

Page 6: 1 Wendy Williams Metaheuristic Algorithms Genetic Algorithms: A Tutorial “Genetic Algorithms are good at taking large, potentially huge search spaces and

6Wendy WilliamsMetaheuristic Algorithms

Genetic Algorithms: A Tutorial

The GA Cycle of Reproduction

reproduction

population evaluation

modification

discard

deleted members

parents

children

modifiedchildren

evaluated children

Page 7: 1 Wendy Williams Metaheuristic Algorithms Genetic Algorithms: A Tutorial “Genetic Algorithms are good at taking large, potentially huge search spaces and

7Wendy WilliamsMetaheuristic Algorithms

Genetic Algorithms: A Tutorial

Population

Chromosomes could be: Bit strings (0101 ... 1100) Real numbers (43.2 -33.1 ... 0.0 89.2) Permutations of element (E11 E3 E7 ... E1 E15) Lists of rules (R1 R2 R3 ... R22 R23) Program elements (genetic programming) ... any data structure ...

population

Page 8: 1 Wendy Williams Metaheuristic Algorithms Genetic Algorithms: A Tutorial “Genetic Algorithms are good at taking large, potentially huge search spaces and

8Wendy WilliamsMetaheuristic Algorithms

Genetic Algorithms: A Tutorial

Reproduction

reproduction

population

parents

children

Parents are selected at random with selection chances biased in relation to chromosome evaluations.

Page 9: 1 Wendy Williams Metaheuristic Algorithms Genetic Algorithms: A Tutorial “Genetic Algorithms are good at taking large, potentially huge search spaces and

9Wendy WilliamsMetaheuristic Algorithms

Genetic Algorithms: A Tutorial

Chromosome Modification

modificationchildren

Modifications are stochastically triggered Operator types are:

Mutation Crossover (recombination)

modified children

Page 10: 1 Wendy Williams Metaheuristic Algorithms Genetic Algorithms: A Tutorial “Genetic Algorithms are good at taking large, potentially huge search spaces and

10Wendy WilliamsMetaheuristic Algorithms

Genetic Algorithms: A Tutorial

Mutation: Local Modification

Before: (1 0 1 1 0 1 1 0)

After: (0 1 1 0 0 1 1 0)

Before: (1.38 -69.4 326.44 0.1)

After: (1.38 -67.5 326.44 0.1)

Causes movement in the search space(local or global)

Restores lost information to the population

Page 11: 1 Wendy Williams Metaheuristic Algorithms Genetic Algorithms: A Tutorial “Genetic Algorithms are good at taking large, potentially huge search spaces and

11Wendy WilliamsMetaheuristic Algorithms

Genetic Algorithms: A Tutorial

Crossover: Recombination

P1 (0 1 1 0 1 0 0 0) (0 1 0 0 1 0 0 0) C1

P2 (1 1 0 1 1 0 1 0) (1 1 1 1 1 0 1 0) C2

Crossover is a critical feature of genetic

algorithms: It greatly accelerates search early in

evolution of a population It leads to effective combination of

schemata (subsolutions on different chromosomes)

*

Page 12: 1 Wendy Williams Metaheuristic Algorithms Genetic Algorithms: A Tutorial “Genetic Algorithms are good at taking large, potentially huge search spaces and

12Wendy WilliamsMetaheuristic Algorithms

Genetic Algorithms: A Tutorial

Evaluation

The evaluator decodes a chromosome and assigns it a fitness measure

The evaluator is the only link between a classical GA and the problem it is solving

evaluation

evaluatedchildren

modifiedchildren

Page 13: 1 Wendy Williams Metaheuristic Algorithms Genetic Algorithms: A Tutorial “Genetic Algorithms are good at taking large, potentially huge search spaces and

13Wendy WilliamsMetaheuristic Algorithms

Genetic Algorithms: A Tutorial

Deletion

Generational GA:entire populations replaced with each iteration

Steady-state GA:a few members replaced each generation

population

discard

discarded members

Page 14: 1 Wendy Williams Metaheuristic Algorithms Genetic Algorithms: A Tutorial “Genetic Algorithms are good at taking large, potentially huge search spaces and

14Wendy WilliamsMetaheuristic Algorithms

Genetic Algorithms: A Tutorial

A Simple Example

The Traveling Salesman Problem:

Find a tour of a given set of cities so that each city is visited only once the total distance traveled is minimized

Page 15: 1 Wendy Williams Metaheuristic Algorithms Genetic Algorithms: A Tutorial “Genetic Algorithms are good at taking large, potentially huge search spaces and

15Wendy WilliamsMetaheuristic Algorithms

Genetic Algorithms: A Tutorial

Representation

Representation is an ordered list of city

numbers known as an order-based GA.

1) London 3) Dunedin 5) Beijing 7) Tokyo

2) Venice 4) Singapore 6) Phoenix 8) Victoria

CityList1 (3 5 7 2 1 6 4 8)

CityList2 (2 5 7 6 8 1 3 4)

Page 16: 1 Wendy Williams Metaheuristic Algorithms Genetic Algorithms: A Tutorial “Genetic Algorithms are good at taking large, potentially huge search spaces and

16Wendy WilliamsMetaheuristic Algorithms

Genetic Algorithms: A Tutorial

Crossover

Crossover combines inversion and

recombination:

* *

Parent1 (3 5 7 2 1 6 4 8)

Parent2 (2 5 7 6 8 1 3 4)

Child (5 8 7 2 1 6 3 4)

This operator is called the Order1 crossover.

Page 17: 1 Wendy Williams Metaheuristic Algorithms Genetic Algorithms: A Tutorial “Genetic Algorithms are good at taking large, potentially huge search spaces and

17Wendy WilliamsMetaheuristic Algorithms

Genetic Algorithms: A Tutorial

Mutation involves reordering of the list:

* *

Before: (5 8 7 2 1 6 3 4)

After: (5 8 6 2 1 7 3 4)

Mutation

Page 18: 1 Wendy Williams Metaheuristic Algorithms Genetic Algorithms: A Tutorial “Genetic Algorithms are good at taking large, potentially huge search spaces and

18Wendy WilliamsMetaheuristic Algorithms

Genetic Algorithms: A Tutorial

TSP Example: 30 Cities

0

10

20

30

40

50

60

70

80

90

100

0 10 20 30 40 50 60 70 80 90 100

x

y

Page 19: 1 Wendy Williams Metaheuristic Algorithms Genetic Algorithms: A Tutorial “Genetic Algorithms are good at taking large, potentially huge search spaces and

19Wendy WilliamsMetaheuristic Algorithms

Genetic Algorithms: A Tutorial

Solution i (Distance = 941)

TSP30 (Performance = 941)

0

10

20

30

40

50

60

70

80

90

100

0 10 20 30 40 50 60 70 80 90 100

x

y

Page 20: 1 Wendy Williams Metaheuristic Algorithms Genetic Algorithms: A Tutorial “Genetic Algorithms are good at taking large, potentially huge search spaces and

20Wendy WilliamsMetaheuristic Algorithms

Genetic Algorithms: A Tutorial

Solution j(Distance = 800)

TSP30 (Performance = 800)

0

10

20

30

40

50

60

70

80

90

100

0 10 20 30 40 50 60 70 80 90 100

x

y

Page 21: 1 Wendy Williams Metaheuristic Algorithms Genetic Algorithms: A Tutorial “Genetic Algorithms are good at taking large, potentially huge search spaces and

21Wendy WilliamsMetaheuristic Algorithms

Genetic Algorithms: A Tutorial

Solution k(Distance = 652)

TSP30 (Performance = 652)

0

10

20

30

40

50

60

70

80

90

100

0 10 20 30 40 50 60 70 80 90 100

x

y

Page 22: 1 Wendy Williams Metaheuristic Algorithms Genetic Algorithms: A Tutorial “Genetic Algorithms are good at taking large, potentially huge search spaces and

22Wendy WilliamsMetaheuristic Algorithms

Genetic Algorithms: A Tutorial

Best Solution (Distance = 420)

TSP30 Solution (Performance = 420)

0

10

20

30

40

50

60

70

80

90

100

0 10 20 30 40 50 60 70 80 90 100

x

y

Page 23: 1 Wendy Williams Metaheuristic Algorithms Genetic Algorithms: A Tutorial “Genetic Algorithms are good at taking large, potentially huge search spaces and

23Wendy WilliamsMetaheuristic Algorithms

Genetic Algorithms: A Tutorial

Overview of Performance

TSP30 - Overview of Performance

0

200

400

600

800

1000

1200

1400

1600

1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31

Generations (1000)

Dis

tan

ce

Best

Worst

Average

Page 24: 1 Wendy Williams Metaheuristic Algorithms Genetic Algorithms: A Tutorial “Genetic Algorithms are good at taking large, potentially huge search spaces and

24Wendy WilliamsMetaheuristic Algorithms

Genetic Algorithms: A Tutorial

Some GA Application Types

Domain Application Types

Control gas pipeline, pole balancing, missile evasion, pursuit

Design semiconductor layout, aircraft design, keyboardconfiguration, communication networks

Scheduling manufacturing, facility scheduling, resource allocation

Robotics trajectory planning

Machine Learning designing neural networks, improving classificationalgorithms, classifier systems

Signal Processing filter design

Game Playing poker, checkers, prisoner’s dilemma

CombinatorialOptimization

set covering, travelling salesman, routing, bin packing,graph colouring and partitioning

Page 25: 1 Wendy Williams Metaheuristic Algorithms Genetic Algorithms: A Tutorial “Genetic Algorithms are good at taking large, potentially huge search spaces and

25Wendy WilliamsMetaheuristic Algorithms

Genetic Algorithms: A Tutorial

%TSPO_GA Open Traveling Salesman Problem (TSP) Genetic Algorithm (GA)% Finds a (near) optimal solution to a variation of the TSP by setting up% a GA to search for the shortest route (least distance for the salesman% to travel to each city exactly once without returning to the starting city)%% Summary:% 1. A single salesman travels to each of the cities but does not close% the loop by returning to the city he started from% 2. Each city is visited by the salesman exactly once%% Input:% XY (float) is an Nx2 matrix of city locations, where N is the number of cities% DMAT (float) is an NxN matrix of point to point distances/costs% POPSIZE (scalar integer) is the size of the population (should be divisible by 4)% NUMITER (scalar integer) is the number of desired iterations for the algorithm to run% SHOWPROG (scalar logical) shows the GA progress if true% SHOWRESULT (scalar logical) shows the GA results if true%% Output:% OPTROUTE (integer array) is the best route found by the algorithm% MINDIST (scalar float) is the cost of the best route%% Example:% n = 50;% xy = 10*rand(n,2);% popSize = 60;% numIter = 1e4;% showProg = 1;% showResult = 1;% a = meshgrid(1:n);% dmat = reshape(sqrt(sum((xy(a,:)-xy(a',:)).^2,2)),n,n);% [optRoute,minDist] = tspo_ga(xy,dmat,popSize,numIter,showProg,showResult);%% Example:% n = 50;% phi = (sqrt(5)-1)/2;% theta = 2*pi*phi*(0:n-1);% rho = (1:n).^phi;% [x,y] = pol2cart(theta(:),rho(:));% xy = 10*([x y]-min([x;y]))/(max([x;y])-min([x;y]));% popSize = 60;% numIter = 1e4;% showProg = 1;% showResult = 1;% a = meshgrid(1:n);% dmat = reshape(sqrt(sum((xy(a,:)-xy(a',:)).^2,2)),n,n);% [optRoute,minDist] = tspo_ga(xy,dmat,popSize,numIter,showProg,showResult);%% Example:% n = 50;% xyz = 10*rand(n,3);% popSize = 60;% numIter = 1e4;% showProg = 1;% showResult = 1;% a = meshgrid(1:n);% dmat = reshape(sqrt(sum((xyz(a,:)-xyz(a',:)).^2,2)),n,n);% [optRoute,minDist] = tspo_ga(xyz,dmat,popSize,numIter,showProg,showResult);%% See also: tsp_ga, tsp_nn, tspof_ga, tspofs_ga, distmat%% Author: Joseph Kirk% Email: [email protected]% Release: 1.3% Release Date: 11/07/11function varargout = tspo_ga(xy,dmat,popSize,numIter,showProg,showResult) % Process Inputs and Initialize Defaultsnargs = 6;for k = nargin:nargs-1 switch k case 0 xy = 10*rand(50,2); case 1 N = size(xy,1); a = meshgrid(1:N); dmat = reshape(sqrt(sum((xy(a,:)-xy(a',:)).^2,2)),N,N); case 2 popSize = 100; case 3 numIter = 1e4; case 4 showProg = 1; case 5 showResult = 1; otherwise endend % Verify Inputs[N,dims] = size(xy);[nr,nc] = size(dmat);if N ~= nr || N ~= nc error('Invalid XY or DMAT inputs!')endn = N; % Sanity CheckspopSize = 4*ceil(popSize/4);numIter = max(1,round(real(numIter(1))));showProg = logical(showProg(1));showResult = logical(showResult(1)); % Initialize the Populationpop = zeros(popSize,n);pop(1,:) = (1:n); for k = 2:popSize pop(k,:) = randperm(n);end % Run the GAglobalMin = Inf;totalDist = zeros(1,popSize);distHistory = zeros(1,numIter);tmpPop = zeros(4,n);newPop = zeros(popSize,n);if showProg pfig = figure('Name','TSPO_GA | Current Best Solution','Numbertitle','off');endfor iter = 1:numIter % Evaluate Each Population Member (Calculate Total Distance) for p = 1:popSize d = 0; % Open Path for k = 2:n d = d + dmat(pop(p,k-1),pop(p,k)); end totalDist(p) = d; end % Find the Best Route in the Population [minDist,index] = min(totalDist); distHistory(iter) = minDist; if minDist < globalMin globalMin = minDist; optRoute = pop(index,:); if showProg % Plot the Best Route figure(pfig); %gambar grafik route if dims > 2, plot3(xy(optRoute,1),xy(optRoute,2),xy(optRoute,3),'r.-'); else plot(xy(optRoute,1),xy(optRoute,2),'r.-'); end title(sprintf('Total Distance = %1.4f, Iteration = %d',minDist,iter)); end end % Genetic Algorithm Operators randomOrder = randperm(popSize); for p = 4:4:popSize rtes = pop(randomOrder(p-3:p),:); dists = totalDist(randomOrder(p-3:p)); [ignore,idx] = min(dists); %#ok bestOf4Route = rtes(idx,:); routeInsertionPoints = sort(ceil(n*rand(1,2))); I = routeInsertionPoints(1); J = routeInsertionPoints(2); for k = 1:4 % Mutate the Best to get Three New Routes tmpPop(k,:) = bestOf4Route; switch k case 2 % Flip tmpPop(k,I:J) = tmpPop(k,J:-1:I); case 3 % Swap tmpPop(k,[I J]) = tmpPop(k,[J I]); case 4 % Slide tmpPop(k,I:J) = tmpPop(k,[I+1:J I]); otherwise % Do Nothing end end newPop(p-3:p,:) = tmpPop; end pop = newPop;end if showResult % Plots the GA Results figure('Name','TSPO_GA | Results','Numbertitle','off'); subplot(2,2,1); pclr = ~get(0,'DefaultAxesColor'); if dims > 2, plot3(xy(:,1),xy(:,2),xy(:,3),'.','Color',pclr); else plot(xy(:,1),xy(:,2),'.','Color',pclr); end title('City Locations'); subplot(2,2,2); imagesc(dmat(optRoute,optRoute)); title('Distance Matrix'); subplot(2,2,3); if dims > 2, plot3(xy(optRoute,1),xy(optRoute,2),xy(optRoute,3),'r.-'); else plot(xy(optRoute,1),xy(optRoute,2),'r.-'); end title(sprintf('Total Distance = %1.4f',minDist)); subplot(2,2,4); plot(distHistory,'b','LineWidth',2); title('Best Solution History'); set(gca,'XLim',[0 numIter+1],'YLim',[0 1.1*max([1 distHistory])]);end % Return Outputsif nargout varargout{1} = optRoute; varargout{2} = minDist;end