Beruflich Dokumente
Kultur Dokumente
Msc. Iury Steiner © 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Optimization Toolbox
Tópicos
• Introdução
• Otimização de funções
• Optimization Toolbox
• Rotinas / Algoritmos Disponíveis
• Algoritmos Genéticos
• Problemas de minimização
• Sem restrições
• Com Restrições
• Exemplos
• Descrição do algoritmo
Msc. Iury Steiner © 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Optimization Toolbox
Otimização de Funções
Otimização se refere basicamente a maximização ou
minimização de funções
Problema típico de otimização:
min f x
x
~
~
Subject to:
hi x 0 Restrições de igualdade
~
g x 0
j Restrições de desigualdade
~
x L k xk xU k Restrições de fronteira
Where:
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.
x é um vetor coluna de variáveis consideradas, que pode
~ afetar o desempenho da otimização.
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Optimization Toolbox
hi x 0
~
Restrições de igualdade
g x 0
j
Restrições de desigualdades
~
Muitos algoritmos necessitam dessa condição
Msc. Iury Steiner © 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Optimization Toolbox
Optimization Toolbox
É 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.
Algoritmos de minimização
Msc. Iury Steiner © 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Optimization Toolbox
Algoritmos de minimização
(Cont.)
Msc. Iury Steiner © 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Optimization Toolbox
Algoritmos de mínimimos
quadrados
Msc. Iury Steiner © 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Optimization Toolbox
Msc. Iury Steiner © 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Optimization Toolbox
Unconstrained Minimization
x
~
min f x e x1 4 x12 2 x22 4 x1 x2 2 x2 1
~
x x1 x2
T
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…
Msc. Iury Steiner © 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Optimization Toolbox
x x1 x2
T
~
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);
Objective function
Msc. Iury Steiner © 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Optimization Toolbox
Passo 2 – a rotina
Ponto Inicial
options = optimset(‘LargeScale’,’off’);
[xmin,feval,exitflag,output]=
fminunc(‘objfun’,x0,options);
Argumentos de Sáida
Argumentos de entrada
Msc. Iury Steiner © 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 =
1 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'
Msc. Iury Steiner © 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Optimization Toolbox
[xmin,feval,exitflag,output,grad,hessian]=
fminunc(fun,x0,options,P1,P2,…)
[xmin,feval,exitflag,output,grad,hessian]=
fminunc(fun,x0,options,P1,P2,…)
Msc. Iury Steiner © 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Optimization Toolbox
Options =
optimset(‘param1’,value1, ‘param2’,value2,…)
As rotinas no Optimization tem um conjunto de parametros
default;
Msc. Iury Steiner © 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Optimization Toolbox
Options =
optimset(‘param1’,value1, ‘param2’,value2,…)
Digite help optimset no command window, uma lista de opções
será mostrada.
Por exemplo:
Value (value1)
Parameter (param1)
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Optimization Toolbox
Options =
optimset(‘param1’,value1, ‘param2’,value2,…)
Msc. Iury Steiner © 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Optimization Toolbox
Msc. Iury Steiner © 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Optimization Toolbox
[xmin,feval,exitflag,output,lambda,grad,hessian]
=
fmincon(fun,x0,A,B,Aeq,Beq,LB,UB,NONLCON,options,
P1,P2,…)
Msc. Iury Steiner © 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Optimization Toolbox
Exemplo
function f = myfun(x)
x
~
min f x x1 x2 x3
~
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
0 30
1 2 2 0
A , B LB 0 , UB 30
1 2 2 72
0 30
Example (Cont.)
Para
2 x12 x2 0
Crie um função nonlcon que retorna dois vetores [C,Ceq]
function [C,Ceq]=nonlcon(x)
Example (Cont.)
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).
>
Const. 7
30.00000000000000
0 x3 30 Const. 8
X = GA(FITNESSFCN,NVARS)
X = GA(FITNESSFCN,NVARS,A,b)
X = GA(FITNESSFCN,NVARS,A,b,Aeq,beq)
X = GA(FITNESSFCN,NVARS,A,b,Aeq,beq,lb,ub)
X = GA(FITNESSFCN,NVARS,A,b,Aeq,beq,lb,ub,NONLCON)
X = GA(FITNESSFCN,NVARS,A,b,Aeq,beq,lb,ub,NONLCON,options)
options = gaoptimset('PlotFcns',...
{@gaplotbestf});
[x,fval,exitflag,output] = ga(@rastriginsfcn,2,[],[],[],[],[],[],[],options)
[x,y]=meshgrid(-10:0.05:10,-10:0.05:10);
f6=@(x,y)0.5-((sin(sqrt(x.^2+y.^2)).^2)- 0.5)./((1+0.001.*(x.^2+y.^2)).^2);
z=f6(x,y);
figure,mesh(x,y,z)
© 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);
[x,fval] = ga(@lincontest6,2,A,b,[],[],lb,[],[],options);
Restrições não-lineares
function y = funcao_fitness(x)
y = 100*(x(1)^2 - x(2))^2 + (1 - x(1))^2;
end
ObjectiveFunction = @ funcao_fitness ;
nvars = 2; % Numero de Variáveis
LB = [0 0]; % mínimo do espaço de busca
UB = [1 13]; % maximo do espaço de busca
ConstraintFunction = @ funcao_restricoes;
[x,fval] = ga(ObjectiveFunction,nvars,[],[],[],[],LB,UB,ConstraintFunction)
options = gaoptimset('MutationFcn',@mutationadaptfeasible);
[x,fval] = ga(ObjectiveFunction,nvars,[],[],[],[],LB,UB,ConstraintFunction,options)
Um estudo de caso
Nosso exemplo
• É estudado em programação matemática
• É um dos modelos utilizados em pesquisa
operacional.
• É um problema de otimização.
• Tem como objetivo:
• "Alocar recursos escassos (ou limitados) a atividades
em concorrência (em competição)"
Msc. Iury Steiner © 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Optimization Toolbox
Msc. Iury Steiner © 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Optimization Toolbox
Pergunta-se
Msc. Iury Steiner © 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Optimization Toolbox
Pergunta-se
Msc. Iury Steiner © 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Optimization Toolbox
Resolvendo Intuitivamente
Msc. Iury Steiner © 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Transformando os dados em
expressões matemáticas
• A função lucro
• Não havendo economia de escala
• É claro que o lucro máximo seria ilimitado se não
fosse a escassez de recursos.
• Em outros problemas a demanda do mercado
também é um fator limitador.
L 4 x1 x2
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Transformando os dados em
expressões matemáticas
• As restrições
• Não se pode utilizar o que não se tem!
• A quantidade utilizada deve ser menor ou igual a
quantidade disponível.
• As quantidades de fabricação devem ser não
negativas
H .H . 9 x1 x2 18
H .M . 3x1 x2 12
x1 0 x2 0
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
O modelo do problema
Função Objetivo
Max L 4 x1 x2
x1 ,x2 Variáveis de Decisão
Limitações
Matriz
H .H . 9 x1 x2 18
Tecnológica
H .M . 3x1 x2 12
Conjunto das
Possibilidades de
Produção
x1 0 x2 0
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Solução Gráfica: Construindo o
conjunto de possibilidades
x2
x1 0 x2 0
0 x1
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Solução Gráfica: Construindo o
conjunto de possibilidades
x2
18 9 x1 x2 18
9 x1 x2 18
2
0 x1
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Solução Gráfica: Construindo o
conjunto de possibilidades
x2
12 3x1 x2 12
3x1 x2 12
4
0 x1
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Solução Gráfica: Construindo o
conjunto de possibilidades
x2
12
Conjunto
de Possibilidades
0 2 x1
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Solução Gráfica: Definindo as Curvas
de Níveis do Objetivo
x2
L9
L7 Direção de
Crescimento do Lucro
L5
0 x1
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Solução Gráfica: Reunindo os
componentes e resolvendo
x2
12 L 13
9
Conjunto
de Possibilidades
0 1 2 x1
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
A solução
x2
Valores p/
Restrição 1
Valores p/
Restrição 2
0 x1
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Casos onde a solução não existe
• A solução é ilimitada
• Não há como definir a decisão
• Exemplo:
x2 Direção de
Crescimento
do Lucro
Conjunto de
Possibilidades
0 x1
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Caso de Infinitas Soluções
x2
As soluções
são
combinações
lineares dos
pontos Conjunto
de Possibilidades
extremos Isolucro
0 x1
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Exercícios: Resolva
1. Maximize o lucro
L 2 x1 3x2
Sujeito a:
x1 x2 4
x1 2 x2 6
x1 3 x2 9
x1 0; x2 0
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Exercícios: Resolva
2. Maximize a receita
R 0,3x1 0,5x2
Sujeito a:
2 x1 x2 2
x1 3x2 3
x1 0; x2 0
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Exercícios: Resolva Graficamente
3. Maximize o lucro
L 2 x1 3x2
Sujeito a:
x1 2 x2 4
x1 x2 6
x1 3 x2 9
x1 0; x2 0
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Exercícios: Resolva Graficamente
Max L 4 x1 x2
x1 ,x2
H .H . 9 x1 x2 18
H .M . 3x1 x2 12
x1 0 x2 0
Lembrando que foi resolvido graficamente,
analise.......
Msc. Iury Steiner © 2008 Solutions 4U Sdn Bhd. All Rights Reserved
Optimization Toolbox
Fim.
© 2008 Solutions 4U Sdn Bhd. All Rights Reserved