Você está na página 1de 30

Algoritmos genticos (Matlab)

MATLAB Optimization Toolbox

Iury Steiner de Oliveira Bezerra


2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

Tpicos

Introduo
Otimizao de funes
Optimization Toolbox
Rotinas / Algoritmos Disponveis
Problemas de minimizao
Sem restries
Com Restries

Exemplos
Descrio do algoritmo

2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

Otimizao de Funes
Otimizao se refere basicamente a maximizao ou
minimizao de funes
Problema tpico de otimizao:

min f x
x

Subject to:

g x 0

hi x 0 Restries de igualdade
~

Restries de desigualdade

x L k xk xU k Restries de fronteira

Where:

f x
~

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.
um vetor coluna de variveis consideradas, que pode
afetar o desempenho da otimizao.
2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

Function Optimization (Cont.)


Restries Delimitao do espao de solues vivies .
Podem ser basicamente lineares e no lineares

g x 0

hi x 0

Restries de igualdade

Restries de desigualdades

Muitos algoritmos necessitam dessa condio

x L k xk xU k

Restries de fronteira ou domnio

2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

Optimization Toolbox
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

2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

Algoritmos de minimizao

2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

Algoritmos de minimizao
(Cont.)

2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

Algoritmos para resolver


equaes

2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

Algoritmos de mnimimos
quadrados

2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

Trabalhando com o Opt.


Toolbox
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 .

2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

Unconstrained Minimization
Considere o problema de encontrar um conjunto de valores [x1 x2]T que
resolva

min f x e x1 4 x12 2 x22 4 x1 x2 2 x2 1


x
~

x x1
~

x2

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

2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

Passo 1 Obj. Function

x x1
function f = objfun(x)

x2

f=exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1);

Objective function

2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

Passo 2 a rotina
Ponto Inicial

x0 = [-1,1];

Configurao de parametros na varivel option

options = optimset(LargeScale,off);
[xmin,feval,exitflag,output]=
fminunc(objfun,x0,options);

Argumentos de Sida

Argumentos de entrada

2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

Resultados
xmin =
0.5000

-1.0000

Minimum point of design variables

feval =
1.3028e-010

Objective function value

exitflag =
Exitflag tells if the algorithm is converged.
If exitflag > 0, then local minimum is found

output =
iterations: 7
funcCount: 40

stepsize: 1

Some other information

firstorderopt: 8.1998e-004
algorithm: 'medium-scale: Quasi-Newton line search'
2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

Mais sobre a entrada da fminunc


[xmin,feval,exitflag,output,grad,hessian]=
fminunc(fun,x0,options,P1,P2,)
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.

2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

More on fminunc Output


[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

2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

Options Setting optimset


Options =
optimset(param1,value1, param2,value2,)
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.

2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

Options Setting (Cont.)


Options =
optimset(param1,value1, param2,value2,)
Type help optimset in command window, a list of options
setting available will be displayed.
How to read? For example:

LargeScale - Use large-scale algorithm if


possible [ {on} | off ]
The default is with { }
Value (value1)
Parameter (param1)
2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

Options Setting (Cont.)


Options =
optimset(param1,value1, param2,value2,)

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)


Agora as entradas da fminuc.

2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

Useful Option Settings


Highly recommended to use!!!

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 ]

2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

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.

2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

Minimizao com restries

Multiplicadores de
Lagrange

[xmin,feval,exitflag,output,lambda,grad,hessian]
=
fmincon(fun,x0,A,B,Aeq,Beq,LB,UB,NONLCON,options,
P1,P2,)

2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

Exemplo
function f = myfun(x)

min f x x1 x2 x3
x

f=-x(1)*x(2)*x(3);

Sujeito :

2 x12 x2 0

x1 2 x2 2 x3 0
x1 2 x2 2 x3 72
0 x1 , x2 , x3 30

1 2 2
0
A
, B

1 2 2
72

0
30
LB 0 , UB 30
0
30
2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

Example (Cont.)

Para

2 x12 x2 0

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

2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

Example (Cont.)

Initial guess (3 design variables)


x0=[10;10;10];

A=[-1 -2 -2;1 2 2];


B=[0 72]';
LB = [0 0 0]';

UB = [30 30 30]';

1 2 2
0
A
, B

1 2 2
72
0
30
LB 0 , UB 30
0
30

[x,feval]=fmincon(@myfun,x0,A,B,[],[],LB,UB,@nonlcon)
CAREFUL!!!

fmincon(fun,x0,A,B,Aeq,Beq,LB,UB,NONLCON,options,P1,P2,)
2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

Exemplo(Cont.)
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
Const. 1

x1 2 x2 2 x3 0

Active Constraints:
2
9
x=
0.00050378663220
0.00000000000000
30.00000000000000
feval =

-4.657237250542452e-035

Const. 3

x1 2 x2 2 x3 72 Const. 2
Const. 5
0 x1 30

Const. 4

0 x2 30

Const. 7

0 x3 30
2 x12 x2 0

Const. 6
Const. 8
Const. 9

Sequence: A,B,Aeq,Beq,LB,UB,C,Ceq
2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

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].

2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

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);

2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

2008 Solutions 4U Sdn Bhd. All Rights Reserved

Optimization Toolbox

Fim.
2008 Solutions 4U Sdn Bhd. All Rights Reserved

Você também pode gostar