Why do I get the following error using fmincon?

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

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 = [];
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.
Thanks. How do you show that it reduces to one quadratic optimisation problem? It is a min-max problem. I have added the formulation of the problem at the beginning of my question.
Never mind. You should be using fminimax, though.
Thanks, but could not understand how to use fminimax in my case. In the examples the inner maximisation problem is always "discrete".

Sign in to comment.

 Accepted Answer

Matt J
Matt J on 27 Feb 2020
Edited: Matt J on 27 Feb 2020
Because the are unconstrained, the only way the inner max operation can have a finite result is if [A1;A2]*x=[0;0] and therefore the problem is equivalent to the linear program,
You can plug this into linprog
f=[0.5450 0.8175 -0.5451 0.2724]; %this is A1 above
A2=[0 0.0852 0 -0.0852]; %this is A2 above
A3=[2.0132 1.0066 -2.0132 -1.0066]; %this is A3 above
Aeq=[A2;A3]; beq=[0;0];
lb=zeros(4,1);
x=linprog(f,[],[],Aeq,beq,lb)
However, it finds that the problem is unbounded,
Problem is unbounded.
x =
[]

More Answers (1)

Matt J
Matt J on 27 Feb 2020
Edited: Matt J on 27 Feb 2020
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.

Asked:

CT
on 27 Feb 2020

Edited:

on 27 Feb 2020

Community Treasure Hunt

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

Start Hunting!