custom output function for genetic algorithm -凯发k8网页登录
this example shows the use of a custom output function in the genetic algorithm solver ga
. the custom output function performs the following tasks:
plot the range of the first two components of the population as a rectangle. the left and lower sides of the rectangle are at the minima of
x(1)
andx(2)
respectively, and the right and upper sides are at the respective maxima.halt the iterations when the best function value drops below
0.1
(the minimum value of the objective function is0
).record the entire population in a variable named
gapopulationhistory
in your matlab® workspace every 10 generations.modify the initial crossover fraction to the custom value
0.2
, and then update it back to the default0.8
after 25 generations. the initial setting of0.2
causes the first several iterations to search primarily at random via mutation. the later setting of0.8
causes the following iterations to search primarily via combinations of existing population members.
objective function
the objective function is for four-dimensional x
whose first two components are integer-valued.
function f = gaintobj(x)
f = rastriginsfcn([x(1)-6 x(2)-13]);
f = f rastriginsfcn([x(3)-3*pi x(4)-5*pi]);
output function
the custom output function sets up the plot during initialization, and maintains the plot during iterations. the output function also pauses the iterations for 0.1s
so you can see the plot as it develops.
function [state,options,optchanged] = gaoutfun(options,state,flag) persistent h1 history r optchanged = false; switch flag case 'init' h1 = figure; ax = gca; ax.xlim = [0 21]; ax.ylim = [0 21]; l1 = min(state.population(:,1)); m1 = max(state.population(:,1)); l2 = min(state.population(:,2)); m2 = max(state.population(:,2)); r = rectangle(ax,'position',[l1 l2 m1-l1 m2-l2]); history(:,:,1) = state.population; assignin('base','gapopulationhistory',history); case 'iter' % update the history every 10 generations. if rem(state.generation,10) == 0 ss = size(history,3); history(:,:,ss 1) = state.population; assignin('base','gapopulationhistory',history); end % find the best objective function, and stop if it is low. ibest = state.best(end); ibest = find(state.score == ibest,1,'last'); bestx = state.population(ibest,:); bestf = gaintobj(bestx); if bestf <= 0.1 state.stopflag = 'y'; disp('got below 0.1') end % update the plot. figure(h1) l1 = min(state.population(:,1)); m1 = max(state.population(:,1)); l2 = min(state.population(:,2)); m2 = max(state.population(:,2)); r.position = [l1 l2 m1-l1 m2-l2]; pause(0.1) % update the fraction of mutation and crossover after 25 generations. if state.generation == 25 options.crossoverfraction = 0.8; optchanged = true; end case 'done' % include the final population in the history. ss = size(history,3); history(:,:,ss 1) = state.population; assignin('base','gapopulationhistory',history); end
problem setup and solution
set the lower and upper bounds.
lb = [1 1 -30 -30]; ub = [20 20 70 70];
set the integer variables and number of variables.
intcon = [1 2]; nvar = 4;
set options to call the custom output function, and to initially have little crossover.
options = optimoptions('ga','outputfcn',@gaoutfun,'crossoverfraction',0.2);
for reproducibility, set the random number generator.
rng default
set the objective function and call the solver.
fun = @gaintobj; [x,fval] = ga(fun,nvar,[],[],[],[],lb,ub,[],intcon,options)
got below 0.1 optimization terminated: y x = 6.0000 13.0000 9.4201 15.7052 fval = 0.0059
the output function halted the solver.
view the size of the recorded history.
disp(size(gapopulationhistory))
40 4 6
there are six records of the 40-by-4 population matrix (40 individuals, each a 4-element row vector).