Solver stopped prematurely. fmincon stopped because it exceeded the function evaluation limit, options.Ma​xFunctionE​valuations = 3.000000e+03.

45 views (last 30 days)
%constraints c, ceq
% size(D)=[69,69]
%size(sigma)=[69,69]
%length(h)=50
function[c,ceq]= nlcon (x)
load ('workspacefmincon.mat','n','sigma','D','h','h_min','h_max');
n=length(D);
x=zeros(1,n);
h=linspace(h_min,h_max,50);
for i=1:length(h)
c(i)=h(i)-(x*D*x');
ceq=[];
end
%optimization
x=zeros(1,n);
h=linspace(h_min,h_max,50);
fun=@(x) x*sigma*x';
x_rao=zeros(1,n);
x0_rao=zeros(1,n);
A_rao=[];
b_rao=[];
Aeq_rao=ones(length(h),n);
beq_rao=ones(length(h),1);
l_b_rao=zeros(1,n);
u_b_rao=ones(1,n);
risk_rao = zeros(1,n);
risk_rao = var_min;
constr=@nlcon;
[x_rao, risk_rao]=fmincon(fun,x0_rao,A_rao,b_rao,Aeq_rao,beq_rao,l_b_rao,u_b_rao,constr);
% If I add options, the problem is the same
% options = optimoptions('fmincon','Display','iter','Algorithm','sqp');

Accepted Answer

Stephan
Stephan on 1 Dec 2020
You have to give the options to the solver and to insert the MaxFunctionEvaluations Option into the optimoptions struct:
MyValue = 10e4;
options = optimoptions('fmincon','Display','iter','Algorithm','sqp', 'MaxFunctionEvaluations',MyValue);
[x_rao, risk_rao]=fmincon(fun,x0_rao,A_rao,b_rao,Aeq_rao,beq_rao,l_b_rao,u_b_rao,constr,options);
  9 Comments
Stephan
Stephan on 1 Dec 2020
Edited: Stephan on 1 Dec 2020
This appears to work - i ran it as 1 script:
index=xlsread('C:\Users\desyp\Desktop\Tesi finass\NASDAQ100.xlsx');
P = index;
RR = diff(P)./P(1:end-1,:);
sigma=cov(RR);
rho=corrcoef(RR);
mu=mean(RR);
n=length(mu);
D=1-rho;
H=2*D;
f=zeros(n,1);
Aeq=ones(1,n);
beq=1;
l_b=zeros(1,n);
x0 = rand(1,n);
options=optimoptions('quadprog','algorithm','active-set','MaxIter',1.e7,...
'TolFun',1.e-10,'TolX',1.e-10);
[x_h_min,var_min]=quadprog(H,f,[],[],Aeq,beq,l_b,[],x0,options);
sum_xhmin=sum(x_h_min);
x_hmin_unit=x_h_min/sum_xhmin;
h_min=x_hmin_unit'*D*x_hmin_unit;
[x_h_max,var_max]=quadprog(-H,f,[],[],Aeq,beq,l_b,[],x0,options);
sum_xhmax=sum(x_h_max);
x_hmax_unit=x_h_max/sum_xhmax;
h_max=x_hmax_unit'*D*x_hmax_unit;
h=linspace(h_min,h_max,50);
%% optimization
fun=@(x) x*sigma*x';
x0_rao=rand(1,n);
A_rao=[];
b_rao=[];
Aeq_rao=ones(1,n);
beq_rao=1;
l_b_rao=-zeros(1,n);
u_b_rao=ones(1,n);
constr=@(x)nlcon(x,D,h_max);
opts = optimoptions('fmincon','Display','final-detailed','Algorithm','sqp',...
'MaxFunctionEvaluations', 5e4,'MaxIterations',5e3,'ConstraintTolerance',...
1e-6);
[x_rao, risk_rao,exitflag,output]=fmincon(fun,x0_rao,A_rao,b_rao,Aeq_rao,...
beq_rao,l_b_rao,u_b_rao,constr,opts)
function[c,ceq]= nlcon(x,D,h_max)
c=h_max-(x*D*x');
ceq=[];
end
But there is still a warning on the first call of quadprog, that the problem is non-convex. Also note that i assumed that my conclusion regarding the nonlinear constraint function is correct. You have to check if that can be correct.
Ylenia Placella
Ylenia Placella on 7 Dec 2020
%if now I want to calculate X and var_RAO for every h. How can I do?
%I used this code. Does not mark error, but X and var_RAO are matrices with all zero.
load ("work_rao_completo.mat","sigma","risk_rao","x_rao","n","D","x0_rao","h_max","h_min")
h=linspace(h_min,h_max,50);
X_rao=x_rao'
func=@(x) X(:,i)'*sigma*X(:,i)
X = zeros(n,length(h))
var_RAO = zeros(1,length(h))
var_RAO(1)=risk_rao
vincoli=@(x)con(x,D)
function[c_i,ceq]= con(x,D)
for i=1:length(h)
c_i= h(i)-X(:,i)'*D*X(:,i)
ceq=[];
[X(:,i),var_RAO(i),exitflag,output]=fmincon(func,x0_rao,A_rao,b_rao,Aeq_rao,...
beq_rao,l_b_rao,u_b_rao,vincoli,opts)
end
end

Sign in to comment.

More Answers (1)

Ylenia Placella
Ylenia Placella on 7 Dec 2020
%if now I want to calculate X and var_RAO for every h. How can I do?
%I used this code. Does not mark error, but X and var_RAO are matrices with all zero.
load ("work_rao_completo.mat","sigma","risk_rao","x_rao","n","D","x0_rao","h_max","h_min")
h=linspace(h_min,h_max,50);
X_rao=x_rao'
func=@(x) X(:,i)'*sigma*X(:,i)
X = zeros(n,length(h))
var_RAO = zeros(1,length(h))
var_RAO(1)=risk_rao
vincoli=@(x)con(x,D)
function[c_i,ceq]= con(x,D)
for i=1:length(h)
c_i= h(i)-X(:,i)'*D*X(:,i)
ceq=[];
[X(:,i),var_RAO(i),exitflag,output]=fmincon(func,x0_rao,A_rao,b_rao,Aeq_rao,...
beq_rao,l_b_rao,u_b_rao,vincoli,opts)
end
end

Community Treasure Hunt

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

Start Hunting!