Genetic Algorithms MATLAB: differenze tra le versioni
Da Bioingegneria Elettronica e Informatica.
(Creata pagina con "== Genetic Algorithms with MATLAB == [http://www.vitoantoniobevilacqua.it/wiki/images/8/88/GA.pdf Slides] '''Vitoantonio Bevilacqua''' [mailto:vitoantonio.bevilacqua@poliba....") |
(Nessuna differenza)
|
Versione delle 11:56, 30 nov 2019
Indice
- 1 Genetic Algorithms with MATLAB
- 2 Mono-objective Function Optimization
- 2.1 Optimization Function
- 2.2 Unconstrained Optimization
- 2.3 Optimization with Linear Inequality Constraints
- 2.4 Optimization with Linear Equality and Inequality Constraints
- 2.5 Optimization with Linear Constraints and Bounds
- 2.6 Optimization with Nonlinear Constraints
- 2.7 Optimization with Integer Constraints
- 2.8 Obtaining Diagnostic Information
- 3 Helper Functions
Genetic Algorithms with MATLAB
Vitoantonio Bevilacqua vitoantonio.bevilacqua@poliba.it
Nicola Altini nicola.altini@poliba.it
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