Upload
guilmar-mgdmgm
View
537
Download
7
Embed Size (px)
Citation preview
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Algoritmos genéticos (Matlab)
MATLAB Optimization Toolbox
Iury Steiner de Oliveira Bezerra
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Tópicos
• Introdução
• Otimização de funções
• Optimization Toolbox
• Rotinas / Algoritmos Disponíveis
• Problemas de minimização
• Sem restrições
• Com Restrições
• Exemplos
• Descrição do algoritmo
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Otimização se refere basicamente a maximização ou
minimização de funções
Problema típico de otimização:
~
~min
xf x
~
0jg x
~
0ih x
L U
k k kx x x
Restrições de igualdadeSubject to:
Restrições de desigualdade
Restrições de fronteira
~
f x é a função objetivo, o que medir e avaliar o desempenho de um sistema. Em um problema padrão, estamos minimizando a função. Para maximização, é equivalente à minimização função objetivo multiplicada por -1.
Where:
~x é um vetor coluna de variáveis consideradas, que pode
afetar o desempenho da otimização.
Otimização de Funções
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
~
0ih x
L U
k k kx x x
Restrições de igualdade
Restrições de desigualdades
Restrições de fronteira ou domínio
~
0jg x
Muitos algoritmos necessitam dessa condição
Restrições – Delimitação do espaço de soluções viávies .
Podem ser basicamente lineares e não lineares
Function Optimization (Cont.)
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
É uma coleção de funções que estendem a capacidade de MATLAB.
As rotinas incluem:
•Otimização sem restrições
•Otimização com restrições lineares e não-lineares.
• Programação Quadrática e programação linear
• Nonlinear least squares and curve fitting
• Nonlinear systems of equations solving
• Constrained linear least squares
•Algoritmos para large scale problems
Optimization Toolbox
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Algoritmos de minimização
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Algoritmos de minimização(Cont.)
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Algoritmos para resolver equações
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Algoritmos de mínimimosquadrados
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
A maioria destas rotinas de otimização exigem a definição de um M- arquivo que contém a função , f, a ser minimizada.
A maxização de funções é conseguida minimizando –f.
Opções de otimização são passadas para os algoritmos do Opt.
Toolbox.
Os parâmetros default da otimização podem ser mudados em uma
estrutura propria .
Trabalhando com o Opt. Toolbox
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Considere o problema de encontrar um conjunto de valores [x1 x2]T que
resolva
1
~
2 2
1 2 1 2 2~
min 4 2 4 2 1x
xf x e x x x x x
1 2~
Tx x x
Passos:
• Criar um M-file que retorna o valor da função(Objective Function). Chame-a de objfun.m
• Então chamar a rotina de minimização. Use fminunc, fminsearch, etc…
Unconstrained Minimization
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Passo 1 – Obj. Function
function f = objfun(x)
f=exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1);
1 2~
Tx x x
Objective function
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Passo 2 – a rotina
x0 = [-1,1];
options = optimset(‘LargeScale’,’off’);
[xmin,feval,exitflag,output]=
fminunc(‘objfun’,x0,options);
Argumentos de SáidaArgumentos de entrada
Ponto Inicial
Configuração de parametros na variável option
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
xmin =
0.5000 -1.0000
feval =
1.3028e-010
exitflag =
1
output =
iterations: 7
funcCount: 40
stepsize: 1
firstorderopt: 8.1998e-004
algorithm: 'medium-scale: Quasi-Newton line search'
Minimum point of design variables
Objective function value
Exitflag tells if the algorithm is converged.If exitflag > 0, then local minimum is found
Some other information
Resultados
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
[xmin,feval,exitflag,output,grad,hessian]=
fminunc(fun,x0,options,P1,P2,…)
Mais sobre a entrada da fminunc
fun : A função objetivo.
x0 : Um ponto de partida. Deve ser um vetor que possuí o mesmo número de variaveis consideradas na otimização.
Option : Configura a otmização
P1,P2,… :Passando a parâmetros adicionais.
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
[xmin,feval,exitflag,output,grad,hessian]=
fminunc(fun,x0,options,P1,P2,…)
xmin : Vector of the minimum point (optimal point). The size is the number of design variables.
feval : The objective function value of at the optimal point.
exitflag : A value shows whether the optimization routine is terminated successfully. (converged if >0)
Output : This structure gives more details about the optimization
grad : The gradient value at the optimal point.
hessian : The hessian value of at the optimal point
More on fminunc – Output
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Options =
optimset(‘param1’,value1, ‘param2’,value2,…)
Options Setting – optimset
The routines in Optimization Toolbox has a set of default
optimization parameters.
However, the toolbox allows you to alter some of those
parameters, for example: the tolerance, the step size, the gradient
or hessian values, the max. number of iterations etc.
There are also a list of features available, for example: displaying
the values at each iterations, compare the user supply gradient or
hessian, etc.
You can also choose the algorithm you wish to use.
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Options =
optimset(‘param1’,value1, ‘param2’,value2,…)
LargeScale - Use large-scale algorithm if
possible [ {on} | off ]
The default is with { }
Parameter (param1)
Value (value1)
Options Setting (Cont.)
Type help optimset in command window, a list of options
setting available will be displayed.
How to read? For example:
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
LargeScale - Use large-scale algorithm if
possible [ {on} | off ]
• Since the default is on, if we would like to turn off, we just type:
Options = optimset(‘LargeScale’, ‘off’)
Options =
optimset(‘param1’,value1, ‘param2’,value2,…)
Agora as entradas da fminuc.
Options Setting (Cont.)
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Display - Level of display [ off | iter | notify | final ]
MaxIter - Maximum number of iterations allowed [ positive integer ]
TolCon - Termination tolerance on the constraint violation [
positive scalar ]
TolFun - Termination tolerance on the function value [ positive
scalar ]
TolX - Termination tolerance on X [ positive scalar ]
Highly recommended to use!!!
Useful Option Settings
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
fminunc and fminsearch
fminunc uses algorithm with gradient and hessian information.
Two modes:
• Large-Scale: interior-reflective Newton
• Medium-Scale: quasi-Newton (BFGS)
Not preferred in solving highly discontinuous functions.
This function may only give local solutions..
fminsearch is generally less efficient than fminunc for
problems of order greater than two. However, when the problem is highly discontinuous, fminsearch may be more robust.
This is a direct search method that does not use numerical or analytic gradients as in fminunc.
This function may only give local solutions.
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
[xmin,feval,exitflag,output,lambda,grad,hessian]
=
fmincon(fun,x0,A,B,Aeq,Beq,LB,UB,NONLCON,options,
P1,P2,…)
Multiplicadores de
Lagrange
Minimização com restrições
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
~
1 2 3~
minx
f x x x x
2
1 22 0x x
1 2 3
1 2 3
2 2 0
2 2 72
x x x
x x x
1 2 30 , , 30x x x
Sujeito à:
1 2 2 0,
1 2 2 72A B
0 30
0 , 30
0 30
LB UB
function f = myfun(x)
f=-x(1)*x(2)*x(3);
Exemplo
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
2
1 22 0x x Para
Crie um função nonlcon que retorna dois vetores [C,Ceq]
function [C,Ceq]=nonlcon(x)
C=2*x(1)^2+x(2);
Ceq=[];Remember to return a nullMatrix if the constraint doesnot apply
Example (Cont.)
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
x0=[10;10;10];
A=[-1 -2 -2;1 2 2];
B=[0 72]';
LB = [0 0 0]';
UB = [30 30 30]';
[x,feval]=fmincon(@myfun,x0,A,B,[],[],LB,UB,@nonlcon)
1 2 2 0,
1 2 2 72A B
Initial guess (3 design variables)
CAREFUL!!!
fmincon(fun,x0,A,B,Aeq,Beq,LB,UB,NONLCON,options,P1,P2,…)
0 30
0 , 30
0 30
LB UB
Example (Cont.)
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Warning: Large-scale (trust region) method does not currently solve this type of problem, switching to medium-scale (line search).
>
Optimization terminated successfully:
Magnitude of directional derivative in search direction less than 2*options.TolFun and maximum constraint violation is less than options.TolCon
Active Constraints:
2
9
x =
0.00050378663220
0.00000000000000
30.00000000000000
feval =
-4.657237250542452e-035
2
1 22 0x x
1 2 3
1 2 3
2 2 0
2 2 72
x x x
x x x
1
2
3
0 30
0 30
0 30
x
x
x
Const. 1
Const. 2
Const. 3
Const. 4
Const. 5
Const. 6
Const. 7
Sequence: A,B,Aeq,Beq,LB,UB,C,Ceq
Const. 8
Const. 9
Exemplo(Cont.)
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Set Fitness function to @rastriginsfcn.
Set Number of variables to 2.
Select Best fitness in the Plot functions pane.
Select Distance in the Plot functions pane.
Set Initial range to [1; 1.1].
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
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);
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Optimization Toolbox
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Fim.