Você está na página 1de 30

2008 Solutions 4U Sdn Bhd.

All Rights Reserved


Algoritmos genticos (Matlab)
MATLAB Optimization Toolbox
Iury Steiner de Oliveira Bezerra
Optimization Toolbox
2008 Solutions 4U Sdn Bhd. All Rights Reserved
Tpicos
Introduo
Otimizao de funes
Optimization Toolbox
Rotinas / Algoritmos Disponveis
Problemas de minimizao
Sem restries
Com Restries
Exemplos
Descrio do algoritmo
Optimization Toolbox
2008 Solutions 4U Sdn Bhd. All Rights Reserved
Otimizao se refere basicamente a maximizao ou
minimizao de funes
Problema tpico de otimizao:
( )
~
~
min
x
f x
( )
~
0
j
g x s
( )
~
0
i
h x =
L U
k k k
x x x s s
Restries de igualdade
Subject to:
Restries de desigualdade
Restries de fronteira
( )
~
f x
a funo objetivo, o que medir e avaliar o desempenho de um sistema.
Em um problema padro, estamos minimizando a funo. Para
maximizao, equivalente minimizao funo objetivo multiplicada por
-1.
Where:
~
x
um vetor coluna de variveis consideradas, que pode
afetar o desempenho da otimizao.
Otimizao de Funes
Optimization Toolbox
2008 Solutions 4U Sdn Bhd. All Rights Reserved
( )
~
0
i
h x =
L U
k k k
x x x s s
Restries de igualdade
Restries de desigualdades
Restries de fronteira ou domnio
( )
~
0
j
g x s
Muitos algoritmos necessitam dessa condio
Restries Delimitao do espao de solues vivies .
Podem ser basicamente lineares e no lineares
Function Optimization (Cont.)
Optimization Toolbox
2008 Solutions 4U Sdn Bhd. All Rights Reserved
uma coleo de funes que estendem a capacidade de MATLAB.
As rotinas incluem:
Otimizao sem restries
Otimizao com restries lineares e no-lineares.
Programao Quadrtica e programao 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 minimizao
Optimization Toolbox
2008 Solutions 4U Sdn Bhd. All Rights Reserved
Algoritmos de minimizao
(Cont.)
Optimization Toolbox
2008 Solutions 4U Sdn Bhd. All Rights Reserved
Algoritmos para resolver
equaes
Optimization Toolbox
2008 Solutions 4U Sdn Bhd. All Rights Reserved
Algoritmos de mnimimos
quadrados
Optimization Toolbox
2008 Solutions 4U Sdn Bhd. All Rights Reserved
A maioria destas rotinas de otimizao exigem a definio de um
M- arquivo que contm a funo , f, a ser minimizada.
A maxizao de funes conseguida minimizando f.
Opes de otimizao so passadas para os algoritmos do Opt.
Toolbox.
Os parmetros default da otimizao 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 1
x
x
f x e x x x x x = + + + +
| |
1 2
~
T
x x x =
Passos:
Criar um M-file que retorna o valor da funo(Objective
Function). Chame-a de objfun.m
Ento chamar a rotina de minimizao. 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
~
T
x 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 Sida
Argumentos de entrada
Ponto Inicial
Configurao de parametros na varivel 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 funo objetivo.
x0 : Um ponto de partida. Deve ser um vetor que possu o
mesmo nmero de variaveis consideradas na otimizao.
Option : Configura a otmizao
P1,P2, :Passando a parmetros 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
Minimizao com restries
Optimization Toolbox
2008 Solutions 4U Sdn Bhd. All Rights Reserved
( )
~
1 2 3
~
min
x
f x x x x =
2
1 2
2 0 x x + s
1 2 3
1 2 3
2 2 0
2 2 72
x x x
x x x
s
+ + s
1 2 3
0 , , 30 x x x s s
Sujeito :
1 2 2 0
,
1 2 2 72
A 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 2
2 0 x x + s
Para
Crie um funo nonlcon que retorna dois vetores [C,Ceq]
function [C,Ceq]=nonlcon(x)
C=2*x(1)^2+x(2);
Ceq=[];
Remember to return a null
Matrix if the constraint does
not 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 72
A 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 2
2 0 x x + s
1 2 3
1 2 3
2 2 0
2 2 72
x x x
x x x
s
+ + s
1
2
3
0 30
0 30
0 30
x
x
x
s s
s s
s s
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.

Você também pode gostar