fmincon very slow for a small scale problem

조회 수: 4 (최근 30일)
Hassan Benchekroun
Hassan Benchekroun 2020년 10월 22일
댓글: Hassan Benchekroun 2020년 10월 23일
Hello, I have been trying to use fmincon for a simple problem with a small number of variables and yet it is very slow or does not even return an answer. Could you please advise on suggestions? I have even used fminsearch since my constraints are just lower bounds on the variables and it's worse. It took 429 sec to solve this with T=9; I need to go up to 20, which should still be ok but it doesn't even return an answer.
Thanks for suggestions
tic
clear all
T=9;
r=1;
K=1;
beta=4;
delta=0.02;
rho=1/(1+delta);
X0=0.1;
Y0=[ones(1,T).*0.05]';
%YY0=ones(1,T).*0.05;
%Xstar=K*(r*(1+beta)-delta)./(r*(beta+2));
%Ystar=r*Xstar*(1-Xstar./K);
[X,Y,NETR,summm,PV] = fnsum(T,X0,r,delta,rho,beta,K);
%obj0 = @(YY) double(subs(PV,Y,YY));
%obj0(YY0)
objd = matlabFunction(PV,'Vars',{[Y(:)]});
%objd([Y0])
%Resd = matlabFunction(Res,'Vars',{[Q(:)]});
%NetrevOpt = matlabFunction(NETR,'Vars',{[Q(:)]});
%summOpt = matlabFunction(NETR,'Vars',{[Q(:)]});
lb=ones(T,1)./1000;
A=[];b=[];
Aeq=[];beq=[];
%[Yopt0,fval0]=fminsearchbnd(obj0,YY0,lb,[])
%[Yopt,fval]=fminsearch(obj0,YY0);
[Yopt,fval]=fmincon(objd,Y0,A,b,[],[],lb,[])
%[Yopt,fval]=fmincon(obj0,YY0,A,b,[],[],lb,[])
%[NetrevOpt(Qopt)]';
%[Resd(Qopt)]';
%plot(Resd(Qopt))
toc
function [X,Y,netrev,summm,summt] = fnsum(t,X0,r,delta,rho,beta,K)
X = sym('X',1:t+1);
netrev = sym('netrev',1:t);
Y = sym('Y',[1 t]);
X(1)=X0;
%a(2:end)=b(2:end)+a(1:end-1).*c(2:end);
%X(2:end)=X(1:end-1)-Y(1:end)+r*X(1:end-1).*(1-X(1:end-1)./K);
%XX=X(1:end-1);
%netrev = log(Y)+beta*log(X(1:end-1));
for i=1:t
netrev(i)= -(log(Y(i))+beta*log(X(i)));
X(i+1)=X(i)-Y(i)+r*X(i).*(1-X(i)./K);
end
n=1:t;
summm = sum(((rho).^(n-1)).*(netrev(n)));
summt=summm- ((rho).^(t)).*((log(r*X(t+1).*(1-X(t+1)./K))+beta*log(X(t+1)))./delta);
end
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 10월 22일
X = sym('X',1:t+1);
netrev = sym('netrev',1:t);
That declares X to be a 1 x 2 x 3 x 4 x 5 x ... t+1 object, and declares netrev to be a 1 x 2 x 3 x 4 x 5 x ... t object.
You want [1, t+1] and [1, t] instead.
Hassan Benchekroun
Hassan Benchekroun 2020년 10월 23일
Thanks, when I tried it I got this error:
Error: File: S29b_essai2.m Line: 47 Column: 20
Invalid expression. When calling a function or indexing a variable, use parentheses.
Otherwise, check for mismatched delimiters.

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2020년 10월 22일
편집: Matt J 2020년 10월 22일
Don't use Symbolic Toolbox operations. Remove all the sym operations and just convert fnsum to a numerical objective function.
  댓글 수: 6
Matt J
Matt J 2020년 10월 23일
objd = matlabFunction(PV,'Vars',{[Y(:)]}); is converting it to a numeric objective function.
Very sub-optimally, it would seem.
Hassan Benchekroun
Hassan Benchekroun 2020년 10월 23일
Indeed, I also tried something like
obj0 = @(YY) double(subs(PV,Y,YY));
obj0(YY0)
instead of matlabFunction but also did not work.
Anyway thanks again.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with Optimization Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by