fmincon very slow for a small scale problem
4 views (last 30 days)
Show older comments
Hassan Benchekroun
on 22 Oct 2020
Commented: Hassan Benchekroun
on 23 Oct 2020
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 Comments
Walter Roberson
on 22 Oct 2020
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.
Accepted Answer
Matt J
on 22 Oct 2020
Edited: Matt J
on 22 Oct 2020
Don't use Symbolic Toolbox operations. Remove all the sym operations and just convert fnsum to a numerical objective function.
6 Comments
Matt J
on 23 Oct 2020
objd = matlabFunction(PV,'Vars',{[Y(:)]}); is converting it to a numeric objective function.
Very sub-optimally, it would seem.
More Answers (0)
See Also
Categories
Find more on Get Started with Optimization Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!