• Tidak ada hasil yang ditemukan

Population Diversity

Genetic Algorithm Examples

a matter of trial and error. If the diversity is too high or too low, the genetic algorithm might not perform well.

This section explains how to control diversity by setting theInitial rangeof the population. “Setting the Amount of Mutation” on page 6-36 describes how the amount of mutation affects diversity.

This section also explains how to set the population size.

Example — Setting the Initial Range

By default, the Optimization Tool creates a random initial population using a creation function. You can specify the range of the vectors in the initial population in theInitial rangefield inPopulationoptions.

NoteThe initial range only restricts the range of the points in the initial population by specifying the lower and upper bounds. Subsequent generations can contain points whose entries do not lie in the initial range. Additionally, the upper and lower bounds can be adjusted by setting theBoundsfields in theConstraintspanel.

If you know approximately where the solution to a problem lies, you should specify the initial range so that it contains your guess for the solution.

However, the genetic algorithm canfind the solution even if it does not lie in the initial range, provided that the populations have enough diversity.

The following example shows how the initial range affects the performance of the genetic algorithm. The example uses Rastrigin’s function, described in

“Example — Rastrigin’s Function” on page 3-8. The minimum value of the function is 0, which occurs at the origin.

To run the example, make the following settings in the Optimization Tool:

SetFitness functionto@rastriginsfcn.

SetNumber of variablesto2.

SelectBestfitnessin thePlot functionspane.

SetInitial rangeto [1; 1.1].

Then clickStart. The genetic algorithm returns the bestfitness function value of approximately 2 and displays the plots in the followingfigure.

10 20 30 40 50 60 70 80 90 100

100.3 100.4 100.5 100.6

Generation

Fitness value

Best: 1.9899 Mean: 1.9911

10 20 30 40 50 60 70 80 90 100

0 0.05 0.1 0.15 0.2

generation

Average Distance between individuals

The upper plot, which displays the bestfitness at each generation, shows little progress in lowering thefitness value. The lower plot shows the average distance between individuals at each generation, which is a good measure of the diversity of a population. For this setting of initial range, there is too little diversity for the algorithm to make progress.

Next, try settingInitial rangeto[1; 100]and running the algorithm. The genetic algorithm returns the bestfitness value of approximately 3.9 and displays the following plots.

Genetic Algorithm Examples

10 20 30 40 50 60 70 80 90 100

100 105

Generation

Fitness value

Best: 3.8861 Mean: 10.2159

10 20 30 40 50 60 70 80 90 100

0 50 100 150

generation

Average Distance between individuals

This time, the genetic algorithm makes progress, but because the average distance between individuals is so large, the best individuals are far from the optimal solution.

Finally, setInitial rangeto[1; 2]and run the genetic algorithm. This returns the bestfitness value of approximately .012 and displays the following plots.

10 20 30 40 50 60 70 80 90 100 10−2

10−1 100 101 102

Generation

Fitness value

Best: 0.011658 Mean: 0.062498

10 20 30 40 50 60 70 80 90 100

0 0.5 1 1.5 2

generation

Average Distance between individuals

The diversity in this case is better suited to the problem, so the genetic algorithm returns a much better result than in the previous two cases.

Example — Linearly Constrained Population and Custom Plot Function

This example shows how the default creation function for linearly constrained problems,gacreationlinearfeasible, creates a well-dispersed population that satisfies linear constraints and bounds. It also contains an example of a custom plot function.

The problem uses the objective function inlincontest6.m, a quadratic:

f x x

x x x x x

( )= 12 + 22− − − .

1 2 1 2

2 2 6

Genetic Algorithm Examples

To see code for the function, entertype lincontest6. The constraints are three linear inequalities:

x1+x2≤2, –x1+x2≤2, 2x1+ 2x2≤3.

Also, the variablesxiare restricted to be positive.

1 Create a custom plot functionfile containing the following code:

function state = gaplotshowpopulation2(unused,state,flag,fcn)

% This plot function works in 2-d only if size(state.Population,2) > 2

return;

end

if nargin < 4 fcn = [];

end

% Dimensions to plot dimensionsToPlot = [1 2];

switch flag

% Plot initialization case 'init'

pop = state.Population(:,dimensionsToPlot);

plotHandle = plot(pop(:,1),pop(:,2),'*');

set(plotHandle,'Tag','gaplotshowpopulation2')

title('Population plot in two dimension','interp','none') xlabelStr = sprintf('%s %s','Variable ',...

num2str(dimensionsToPlot(1)));

ylabelStr = sprintf('%s %s','Variable ',...

num2str(dimensionsToPlot(2)));

xlabel(xlabelStr,'interp','none');

ylabel(ylabelStr,'interp','none');

hold on;

% plot the inequalities

plot([0 1.5],[2 0.5],'m-.') % x1 + x2 <= 2

plot([0 1.5],[3 0],'m-.'); % 2*x1 + x2 <= 3

% plot lower bounds

plot([0 0], [0 2],'m-.'); % lb = [ 0 0];

plot([0 1.5], [0 0],'m-.'); % lb = [ 0 0];

set(gca,'xlim',[-0.7,2.2]) set(gca,'ylim',[-0.7,2.7])

% Contour plot the objective function if ~isempty(fcn)

range = [-0.5,2;-0.5,2];

pts = 100;

span = diff(range')/(pts - 1);

x = range(1,1): span(1) : range(1,2);

y = range(2,1): span(2) : range(2,2);

pop = zeros(pts * pts,2);

values = zeros(pts,1);

k = 1;

for i = 1:pts for j = 1:pts

pop(k,:) = [x(i),y(j)];

values(k) = fcn(pop(k,:));

k = k + 1;

end end

values = reshape(values,pts,pts);

contour(x,y,values);

colorbar end

% Pause for three seconds to view the initial plot pause(3);

case 'iter'

pop = state.Population(:,dimensionsToPlot);

plotHandle = findobj(get(gca,'Children'),'Tag',...

'gaplotshowpopulation2');

set(plotHandle,'Xdata',pop(:,1),'Ydata',pop(:,2));

end

Genetic Algorithm Examples

The custom plot function plots the lines representing the linear inequalities and bound constraints, plots level curves of the objective (fitness) function, and plots the population as it evolves.

2 At the command line, enter the constraints as a matrix and vectors:

A = [1,1;-1,2;2,1]; b = [2;2;3]; lb = zeros(2,1);

3 Setoptionsto usegaplotshowpopulation:

options = gaoptimset('PlotFcns',@gaplotshowpopulation);

4 Run the optimization usingoptions:

[x,fval] = ga(@lincontest6,2,A,b,[],[],lb,[],[],options);

A plot window appears showing the linear constraints, bounds, level curves of the objective function, and initial distribution of the population:

The population eventually concentrates around the minimum point:

Setting the Population Size

ThePopulation sizefield inPopulationoptions determines the size of the population at each generation. Increasing the population size enables the genetic algorithm to search more points and thereby obtain a better result.

However, the larger the population size, the longer the genetic algorithm takes to compute each generation.

NoteYou should setPopulation sizeto be at least the value ofNumber of variables, so that the individuals in each population span the space being searched.

You can experiment with different settings forPopulation sizethat return good results without taking a prohibitive amount of time to run.

Genetic Algorithm Examples

Dokumen terkait