Why do I get the following error using fmincon?
Show older comments
I am trying to run the following fmincon problem, where evaluating fun requires to solve a linear programming problem.
Before showing you my code, let me write the optimisation problem in math symbols.
where x is a
vector,
is a
vector of zeros,
means that every element of x should be non-negative,
are
vectors of real numbers.
clear
rng default
XZW_temp=[0.5450 0.8175 -0.5451 0.2724]; %this is A1 above
X1_temp=[0 0.0852 0 -0.0852]; %this is A2 above
X2_temp=[2.0132 1.0066 -2.0132 -1.0066]; %this is A3 above
options = optimset('linprog');
options.Display = 'off';
fun=@(x)inner_max(x,XZW_temp, X1_temp, X2_temp, options);
ub=Inf*ones(4,1);
lb=zeros(4,1);
x0=ones(4,1);
[~, f]=fmincon(fun,x0,[],[],[],[],lb,ub);
The function inner_max is
function i_m=inner_max(x,XZW_temp, X1_temp, X2_temp, options)
f=-[x.'*XZW_temp.'; x.'*X1_temp.'; x.'*X2_temp.'];
Aeq=[1 0 0];
beq=1;
lb=-Inf*ones(3,1);
ub=Inf*ones(3,1);
[~,fval] = linprog(f,[],[],Aeq,beq,lb,ub,options);
i_m=-fval;
end
I get the following error
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in finitedifferences
Error in computeFinDiffGradAndJac
Error in barrier
Error in fmincon (line 798)
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = barrier(funfcn,X,A,B,Aeq,Beq,l,u,confcn,options.HessFcn, ...
Could you help me to understand what is wrong?
5 Comments
John D'Errico
on 27 Feb 2020
Notethat you do not need to make all bounds inf, if there are no bounds.
lb=-Inf*ones(3,1);
ub=Inf*ones(3,1);
Simpler and better is just:
lb = [];
ub = [];
Matt J
on 27 Feb 2020
It would be better to rewrite this problem as a quadratic program and use quadprog. The objective to be minimized,
f(x,y)=-[x.'*XZW_temp.'; x.'*X1_temp.'; x.'*X2_temp.']*y;
is quadratic in (x,y) with all linear constraints on these variables.
CT
on 27 Feb 2020
Matt J
on 27 Feb 2020
Never mind. You should be using fminimax, though.
CT
on 27 Feb 2020
Accepted Answer
More Answers (1)
Could you help me to understand what is wrong?
linprog() and therefore innermax() can return an empty matrix [] when it does not find a solution. Your code does not test for and provide a contingency for that situation.
Categories
Find more on Solver Outputs and Iterative Display 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!