Vous êtes sur la page 1sur 7

Exemplos MATLAB Algoritmos Genticos no MATLAB Exemplo 2

function y = simple_fitness(x) y = 100*(x(1)^2 - x(2))^2 + (1 - x(1))^2;

function [c, ceq] = simple_constraint(x) c = [1.5 + x(1)*x(2) + x(1) - x(2);... -x(1)*x(2) + 10]; ceq = [];

ObjectiveFunction = @simple_fitness; nvars = 2; % Number of variables

LB = [0 0]; % Lower bound UB = [1 13]; % Upper bound ConstraintFunction = @simple_constraint; [x,fval] = ga(ObjectiveFunction,nvars,[],[],[],[],LB,UB,ConstraintFunction)

ObjectiveFunction = @simple_fitness; nvars = 2; % Number of variables LB = [0 0]; % Lower bound UB = [1 13]; % Upper bound ConstraintFunction = @simple_constraint; [x,fval] = ga(ObjectiveFunction,nvars,[],[],[],[],LB,UB,ConstraintFunction) Warning: 'mutationgaussian' mutation function is for unconstrained minimization only; using @mutationadaptfeasible mutation function. Set @mutationadaptfeasible as MutationFcn options using GAOPTIMSET. Optimization terminated: current tolerance on f(x) 1e-007 is less than options.TolFun and constraint violation is less than options.TolCon. x= 0.8122 12.3122 fval = 1.3578e+004
options = gaoptimset('MutationFcn',@mutationadaptfeasible); [x,fval] = ga(ObjectiveFunction,nvars,[],[],[], [],LB,UB,ConstraintFunction,options) Optimization terminated: current tolerance on f(x) 1e-007 is less than options.TolFun and constraint violation is less than options.TolCon.

x= 0.8122 12.3122

fval = 2

1.3578e+004

options = gaoptimset(options,'PlotFcns', {@gaplotbestf,@gaplotmaxconstr},'Display','iter'); [x,fval] = ga(ObjectiveFunction,nvars,[],[],[],[],... LB,UB,ConstraintFunction,options) Best Generation f-count 1 2 3 4 5 849 1567 2334 3043 3752 max f(x) Stall constraint Generations 0 0 0 0 0 0 0 1 2 3

14915.8 13578.3 13578.3 13578.3 13578.3

Optimization terminated: current tolerance on f(x) 1e-009 is less than options.TolFun and constraint violation is less than options.TolCon.

x= 0.8122 12.3123

fval = 1.3578e+004

Exemplo 3
X0 = [0.5 0.5]; % Start point (row vector) options = gaoptimset(options,'InitialPopulation',X0); [x,fval] = ga(ObjectiveFunction,nvars,[],[],[],[],... LB,UB,ConstraintFunction,options) Best Generation f-count 1 2 3 965 1728 2422 max f(x) Stall constraint Generations 0 0 0

13579.6

13578.2 1.776e-015 13578.2 0 0

Optimization terminated: current tolerance on f(x) 1e-007 is less than options.TolFun and constraint violation is less than options.TolCon.

x= 0.8122 12.3122

fval = 1.3578e+004

Create a custom plot function file containing the following code: function state = gaplotshowpopulation2(unused,state,flag,fcn) % This plot function works in 2-d only 4

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 5

plot([0 1.5],[1 3.5/2],'m-.'); % -x1 + 2*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 6

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

A = [1,1;-1,2;2,1]; b = [2;2;3]; lb = zeros(2,1); options = gaoptimset('PlotFcns',@gaplotshowpopulation2); x,fval] = ga(@lincontest6,2,A,b,[],[],lb,[],[],options);

Vous aimerez peut-être aussi