Results of optimization algorithm change when called inside a loop in comparison to results from corresponding stand-alone execution
Show older comments
Hi everyone,
I have a doubt about the results my Matlab code is generating. I have to run an optimization using ‘fmincon’ (which is called by the function ‘nest_minimization’ in the code below). This optimization problem depends on several parameters and I want to analyze how its solution changes when varying just one of the parameters (theta in the code below), while keeping all the other ones fixed.
Therefore I run a loop to gradually increase theta and execute the optimization algorithm in each step. I save the produced solution vectors and some additional information over the whole range of theta considered in the loop in an excel file.
Now my problem is that the results of the optimization algorithm are different for a given parameter configuration when on the one hand called inside the loop and one the other hand called as a single optimization without any loop. For instance, I run the optimization problem once outside the loop with theta=20. Now I run the optimization problem with all parameters but theta being fixed in a loop letting theta range from 18 to 21. The result produced for the parameter configuration with theta=20 is different from the one produced outside the loop. How can that be? There are no random numbers generated in any of the functions called within ‘nest_minimization’.
Thanks in advance for any help.
clc;
clearvars;
theta=18;
w=5;
K_bar=2;
beta_i=1;
beta_j=0.9;
pi_down=0.8;
pi_up=1;
A=2;
r_K=0.1;
p_H=1;
p_L=0.9;
%Start assuming that pi follows a uniform distribution
%Linear constraints
Amat=[0 -1 0 1 0 0 0 0 0 0; -1 0 1 0 0 0 0 0 0 0; 0 0 0 0 1 0 p_H p_L 0 0];
bmat=zeros(3,1);
bmat(3) = w;
%Initial values
x0=[8 8 .5 .5 1.2 .07 .5 0 0 .5]';
incr=0.1;
row=1;
sol_compl=zeros(11,1);
results=zeros(30,11);
excesses=zeros(30,3);
check=zeros(30,2);
while(theta<21)
[sol,fval,exitflag] = nest_minimization(x0,theta,beta_i,w,p_H,p_L,A,pi_down,pi_up,Amat,bmat);
down = [sol(9)+sol(1)-sol(3)+pi_down*A*sol(10)+pi_down*A*(sol(2)-sol(4))-(1+sol(6))*sol(5);0];
both = [sol(9)+sol(1)-sol(3)+(0.5*pi_down+0.5*pi_up)*A*sol(10)+(0.5*pi_down+0.5*pi_up)*A*(sol(2)-sol(4))-(1+sol(6))*sol(5);0];
up=[sol(9)+sol(1)-sol(3)+pi_up*A*sol(10)+pi_up*A*(sol(2)-sol(4))-(1+sol(6))*sol(5);0];
simps_approx=(pi_up-pi_down)/(6)*(max(down)*unifpdf(pi_down,pi_down,pi_up)+...
4*max(both)*unifpdf(0.5*pi_down+0.5*pi_up,pi_down,pi_up)+...
max(up)*unifpdf(pi_up,pi_down,pi_up));
Kj=simps_approx/(1+sol(7));
sol_compl=[sol;Kj];
excess_l=sol(8)+sol(10)-sol(4);
excess_h=sol(7)+sol(9)-sol(3);
excess_rk=Kj-K_bar;
check(row,:)=[fval exitflag];
results(row,:)=[sol_compl];
excesses(row,:)=[excess_l excess_h excess_rk]';
theta=theta+incr;
row=row+1;
end
out=[check results excesses];
summary=xlswrite('output',out);
1 Comment
Elisa Pacheco
on 1 Nov 2017
PeterK I'm having exactly the same problem. Have you discovered a solution?
Answers (1)
Alan Weiss
on 17 Apr 2013
0 votes
fmincon is a deterministic set of algorithms. Therefore, I suspect that you have either a different set of options when running in the loop (yeah, that's not likely), or much more likely you have slightly differering values that you are passing.
I suggest you use the debugger to see exactly what is happening in the loop. Just check what values are being passed to fmincon, down to the last bit, and what options there are. Compare these inputs to the values you pass outside the loop.
Alan Weiss
MATLAB mathematical toolbox documentation
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!