Genetic Algorithms MATLAB
Da Bioingegneria Elettronica e Informatica.
Versione del 30 nov 2019 alle 12:04 di Profbevilacqua (Discussione | contributi)
Vitoantonio Bevilacqua vitoantonio.bevilacqua@poliba.it
Nicola Altini nicola.altini@poliba.it
Indice
- 1 Mono-objective Function Optimization
- 1.1 Optimization Function
- 1.2 Unconstrained Optimization
- 1.3 Optimization with Linear Inequality Constraints
- 1.4 Optimization with Linear Equality and Inequality Constraints
- 1.5 Optimization with Linear Constraints and Bounds
- 1.6 Optimization with Nonlinear Constraints
- 1.7 Optimization with Integer Constraints
- 1.8 Obtaining Diagnostic Information
- 1.9 Helper Functions
- 2 Multi-objective Function Optimization
Mono-objective Function Optimization
Optimization Function
%%% Optimization Function
% Define and plot the function to optimize
xi = linspace(-6,6,300);
yi = linspace(-6,6,300);
[X,Y] = meshgrid(xi,yi);
Z = ps_example([X(:),Y(:)]);
% Note that ps_example is a non-smooth function
Z = reshape(Z,size(X));
surf(X,Y,Z,'MeshStyle','none')
colormap 'jet'
view(-26,43)
xlabel('x(1)')
ylabel('x(2)')
title('Teaching Example')
hold on
dim_area = 200;
Unconstrained Optimization
%% Optimization with Genetic Algorithm
rng default % For reproducibility
x = ga(@ps_example,2);
fprintf('The minimum found using GA is: ');
disp(x);
fprintf('f(x) = %.4f\n', ps_example(x));
scatter3(x(1), x(2), ps_example(x),dim_area,'g','filled');
Optimization with Linear Inequality Constraints
%% Nonsmooth Function with Linear Constraints
% Linear Constraints
% Inequalities to the matrix form A*x <= b
% -x(1) -x(2) <= -1
% -x(1) +x(2) <= 5
A = [-1, -1;
-1, 1];
b = [-1; 5];
rng default % For reproducibility
fun = @ps_example;
x = ga(fun,2,A,b);
fprintf('The minimum found using GA (with Linear Constraints) is: ');
disp(x);
fprintf('f(x) = %.4f\n', ps_example(x));
fprintf('Check constraint:\n');
fprintf('A * x - b = ');
disp(A*x' - b);
scatter3(x(1), x(2), ps_example(x),dim_area,'r','filled');
Optimization with Linear Equality and Inequality Constraints
%% Linear Equality and Inequality Constraints
% Inequalities to the matrix form A*x <= b
% Equalities to the matrix form Aeq*x = beq
A = [-1 -1];
b = -1;
Aeq = [-1 1];
beq = 5;
% Optimization
rng default % For reproducibility
fun = @ps_example;
x = ga(fun,2,A,b,Aeq,beq);
fprintf('The minimum found using GA (with Linear Equality and Inequality Constraints) is: ');
disp(x);
fprintf('f(x) = %.4f\n', ps_example(x));
fprintf('Check constraints:\n');
fprintf('A * x - b = ');
disp(A*x' - b);
fprintf('Aeq * x - beq = ');
disp(Aeq*x' - beq);
scatter3(x(1), x(2), ps_example(x),dim_area,'b','filled');
Excercise 3
%% Linear Equality and Inequality Constraints - Excercise 3
% Constraints
A = [-2 -4; 3 -1; -1 -1];
b = [-1; -2; 0];
Aeq = [2 1];
beq = 5;
% Optimization
rng default % For reproducibility
fun = @ps_example;
x = ga(fun,2,A,b,Aeq,beq);
fprintf('The minimum found using GA (with Linear Equality and Inequality Constraints) is: ');
disp(x);
fprintf('f(x) = %.4f\n', ps_example(x));
fprintf('Check constraints:\n');
fprintf('A * x - b = ');
disp(A*x' - b);
fprintf('Aeq * x - beq = ');
disp(Aeq*x' - beq);
scatter3(x(1), x(2), ps_example(x),dim_area,'b','filled');
Optimization with Linear Constraints and Bounds
%% Linear Constraints and Bounds
% -x(1) -x(2) <= -1
% -x(1) +x(2) == 5
A = [-1 -1];
b = -1;
Aeq = [-1 1];
beq = 5;
% 1 <= x(1) <= 6
% -3 <= x(2) <= 8
lb = [1 -3];
ub = [6 8];
% Optimization
rng default % For reproducibility
fun = @ps_example;
x = ga(fun,2,A,b,Aeq,beq,lb,ub);
fprintf('The minimum found using GA (with Linear Constraints and Bounds) is: ');
disp(x);
fprintf('f(x) = %.4f\n', ps_example(x));
fprintf('Check constraints:\n');
fprintf('A * x - b = ');
disp(A*x' - b);
fprintf('Aeq * x - beq = ');
disp(Aeq*x' - beq);
scatter3(x(1), x(2), ps_example(x),dim_area,'p','filled');
Optimization with Nonlinear Constraints
Example 1
%% Nonlinear Constraints
% minimize ps_example function on the region
% 2x(1).^2 + x(2).^2 <= 3
% (x(1) + 1).^2 = (x(2) / 2).^4
nonlcon = @ellipsecons;
% Optimization
fun = @ps_example;
rng default % For reproducibility
x = ga(fun,2,[],[],[],[],[],[],nonlcon);
disp(x);
fprintf('f(x) = %.4f\n', ps_example(x));
[c,ceq] = nonlcon(x);
fprintf('Check constraints:\n');
fprintf('nonlcon(x) -> c = %.3f, ceq = %.3f\n', c, ceq);
% Constraints satisfied when
% c <= 0
% ceq == 0
scatter3(x(1), x(2), ps_example(x),dim_area,'y','filled');
Example 2
%% Genetic Algorithm with Non-Linear constraints
use_circle_equality_constraint = 1;
if use_circle_equality_constraint
nonlcon = @circlecons_eq;
else
nonlcon = @circlecons_lt;
end
rng default % For reproducibility
fun = @squarednorm;
A = [];
b = [];
Aeq = [];
beq = [];
lb = [2, 2];
ub = [7, 8];
IntCon = [];
[x,fval,exitflag,output,population,scores] = ga(fun,2,A,b,Aeq,beq,lb,ub,nonlcon,IntCon);
Optimization with Integer Constraints
%% Integer Constraints
IntCon = 1;
rng default % For reproducibility
fun = @ps_example;
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];
nonlcon = [];
x = ga(fun,2,A,b,Aeq,beq,lb,ub,nonlcon,IntCon);
disp(x);
fprintf('f(x) = %.4f\n', ps_example(x));
scatter3(x(1), x(2), ps_example(x),dim_area,'c','filled');
Obtaining Diagnostic Information
Example 1
%%% Example 1
rng default % For reproducibility
fun = @ps_example;
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];
nonlcon = [];
IntCon = 1;
% options = optimoptions(SolverName,Name,Value)
options = optimoptions('ga','PlotFcn', @gaplotbestf);
[x,fval,exitflag,output,population,scores] = ga(fun,2,A,b,Aeq,beq,lb,ub,nonlcon,IntCon,options);
% Final Population and Scores
disp(population(1:10,:));
disp(scores(1:10));
Example 2
%%% Example 2
options = optimoptions('ga','ConstraintTolerance',1e-6,'PlotFcn', @gaplotbestf);
[x,fval,exitflag,output,population,scores] = ga(fun,2,A,b,Aeq,beq,lb,ub,nonlcon,options);
% Final Population and Scores
disp(population(1:10,:));
disp(scores(1:10));
Helper Functions
%% Helper Functions
function [c,ceq] = circlecons_eq(x)
c = [];
ceq = (x(1)-8)^2 +(x(2)-6)^2 -9;
end
function [c,ceq] = circlecons_lt(x)
c = (x(1)-8)^2 +(x(2)-6)^2 -9;
ceq = [];
end
function [c, ceq] = ellipsecons(x)
c = 2*x(1)^2 + x(2)^2 - 3;
ceq = (x(1)+1)^2 - (x(2)/2)^4;
end
function f = squarednorm(x)
f = x(1)^2 + x(2)^2;
end
Multi-objective Function Optimization
Unconstrained Optimization
%% Multi-objective Optimization Example
%%% No Constraints
% Defining fitness function
% Find the Pareto front for a simple multiobjective problem. There are two objectives and two decision variables x.
fitnessfcn = @(x)[norm(x)^2 , 0.5*norm(x(:)-[2;-1])^2+2];
% Find the Pareto front for this objective function.
% Optimization
rng default % For reproducibility
x = gamultiobj(fitnessfcn,2);
% Optimization terminated: average change in the spread of Pareto solutions less than options.FunctionTolerance.
% Plot the solution points.
plot(x(:,1),x(:,2),'ko')
xlabel('x(1)')
ylabel('x(2)')
title('Pareto Points in Parameter Space')
Optimization with Linear Constraints
%% Linear Inequality Constraint
A = [1,1];
b = 1/2;
% Find the Pareto front.
rng default % For reproducibility
[x, fval] = gamultiobj(fitnessfcn,2,A,b);
% Optimization terminated: average change in the spread of Pareto solutions less than options.FunctionTolerance.
% Plot the constrained solution and the linear constraint.
plot(x(:,1),x(:,2),'ko')
t = linspace(-1/2,2);
y = 1/2 - t;
hold on
plot(t,y,'b--')
xlabel('x(1)')
ylabel('x(2)')
title('Pareto Points in Parameter Space')
hold off
Optimization with Bound Constraints
Example 1
%% Bound Constraints
fitnessfcn = @(x)[sin(x),cos(x)];
nvars = 1;
lb = 0;
ub = 2*pi;
rng default % for reproducibility
[x, fval] = gamultiobj(fitnessfcn,nvars,[],[],[],[],lb,ub);
plot(sin(x),cos(x),'r*')
xlabel('sin(x)')
ylabel('cos(x)')
title('Pareto Front')
legend('Pareto front')
Example 2
%% Multi-objective Optimization Example 2
% Defining and plotting function to optimize
x = -1:0.1:8;
y = schaffer2(x);
plot(x,y(:,1),'r',x,y(:,2),'b');
xlabel x
ylabel 'schaffer2(x)'
legend('Objective 1','Objective 2')
% Bounds
lb = -5;
ub = 10;
% Set options to view the Pareto front as gamultiobj runs.
options = optimoptions('gamultiobj','PlotFcn',@gaplotpareto);
% Optimization - call gamultiobj.
rng default % For reproducibility
[x,fval,exitflag,output] = gamultiobj(@schaffer2,1,[],[],[],[],lb,ub,options);
Helper Functions
%% Helper Functions
function f = my_multi(x)
f(1) = x(1)^4 - 10*x(1)^2+x(1)*x(2) + x(2)^4 -(x(1)^2)*(x(2)^2);
f(2) = x(2)^4 - (x(1)^2)*(x(2)^2) + x(1)^4 + x(1)*x(2);
end
function f = pareto_example(x)
f = [norm(x)^2 , 0.5*norm(x(:)-[2;-1])^2+2];
end
function y = schaffer2(x) % y has two columns
% Initialize y for two objectives and for all x
y = zeros(length(x),2);
% Evaluate first objective.
% This objective is piecewise continuous.
for i = 1:length(x)
if x(i) <= 1
y(i,1) = -x(i);
elseif x(i) <= 3
y(i,1) = x(i) -2;
elseif x(i) <= 4
y(i,1) = 4 - x(i);
else
y(i,1) = x(i) - 4;
end
end
% Evaluate second objective
y(:,2) = (x -5).^2;
end