How to include min() in optimization constraint

11 views (last 30 days)
Greg76
Greg76 on 19 Apr 2015
Edited: Derya on 21 Apr 2015
Hi,
I have an optimization problem for which I want to find a solution using the GlobalSearch solver. I have several constraints of the form "X(20) <= 100 - max([max(X(1:3)) max(X(5:7)) max(X(10:17))])" which I implemented in the "c" argument: "X(20) - (100 - max([max(X(1:3)) max(X(5:7)) max(X(10:17))]);" (<= 0)
However, these constraints seem to be ignored by Matlab during optimization. This leads me to suspect that it is not possible to use min() and max() when writing constraints. Is this true? If so, is there a convenient way to implement this without having to write out every single constraint, i.e. "X(20) - (100-X(1)); X(20) - (100-X(2)); etc."?
Thank you!

Answers (2)

Matt J
Matt J on 19 Apr 2015
Edited: Matt J on 21 Apr 2015
If so, is there a convenient way to implement this without having to write out every single constraint, i.e. "X(20) - (100-X(1)); X(20) - (100-X(2));
These are linear constraints. You should be using matrix/vector inputs to express them and you should construct the matrix/vector input data using vectorized methods. The code below generates A,b that express your constraints in the appropriate matrix/vector form A*X<=b,
J=[1:3,5:7, 10:17];
numcon=length(J);
A=sparse(1:numcon,J,1,numcon,length(X));
A(:,20)=1;
b(1:numcon,1)=100;

Derya
Derya on 21 Apr 2015
Edited: Derya on 21 Apr 2015
Hello Greg,
In regards to your question whether one can use min() or max() in writing their nonlinear constraints, the answer is yes.
A possible reason for why your constraint is ignored would be that the solver doesn't know about it.
I assume that you have implemented your nonlinear constraint function (say function [c, ceq] = myconst(x)) and your problem structure has a field called 'nonlcon' and its value is 'myconst'. As an example, using createOptimProblem
problem = createOptimProblem('fmincon','x0',randn(2,1), 'objective',myobjfun,'nonlcon',myconst,'lb',[-2;-2],'ub',[2;2],'options',opts);
you had your problem, that was fed into GlobalSearch as:
gs = GlobalSearch;
[x,f] = run(gs,problem)
Could you confirm that you have (more or less) done this?
Regards,
Derya
P.S.: I think you can use "X(20) <= 100 - max([X(1:3) X(5:7) X(10:17)])" instead of "X(20) <= 100 - max([max(X(1:3)) max(X(5:7)) max(X(10:17))])".

Community Treasure Hunt

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

Start Hunting!