complicated constraint set

63 views
Skip to first unread message

Xun Xiao

unread,
Nov 19, 2014, 2:56:48 PM11/19/14
to yal...@googlegroups.com
Hi Johan,

Thank you for developing  such a great modeling language for us. I am using YALMIP to solve MIP. My constraint set is a little bit complicated, it cannot be written as a matrix form but a function form.

Suppose I define the decision variable as x = sdpvar(1), in my constraint function, I need to compare a vector v with the variable x, and find the first element in v less than x. I use:

idx = find(v<x, 1, 'first');

However, I got the error:

Undefined function 'find' for input arguments of type 'constraint'.

Could you tell me what can I do? Thanks a lot.

Johan Löfberg

unread,
Nov 19, 2014, 3:23:32 PM11/19/14
to
You would have to create the MILP model for that manually

I would define a binary vector s to represent which element you are looking for, and a vector u to say if x>v or not.  The constraint you would have would be things like

sum(s)==1
if u(i) then x>=v
if ~u(i) then v<=x (the strict case cannot be handled in finite numerics, unless x and v are integer)
if u(i)==1 and u(1:i-1)==0 then s(i)==1 (i.e., something like s(i)<=u(i), s(i) >= u(i)-sum(u(1:i-1)) etc)

Those things you implement using simple binary constraints and implies operator. The final index value would be s'*(1:n)



 

Xun Xiao

unread,
Nov 19, 2014, 3:38:36 PM11/19/14
to yal...@googlegroups.com
Thanks for your reply.

Do I have to use 'implies' to implement the above process or not? But I cannot quite understand how to use 'implies' to replace 'if' operator....

Actually, I have to use the idx I found to further calculate something to define the constraint set...

What I am doing is really like using 'fmincon' and write a constraint function 'nonlcon'

Johan Löfberg

unread,
Nov 19, 2014, 3:44:13 PM11/19/14
to
[implies(u(i), x(i)>=v(i)),implies(1-u(i), x(i)<=v(i))]
etc

Johan Löfberg

unread,
Nov 20, 2014, 3:02:49 AM11/20/14
to yal...@googlegroups.com
A semi-highlevel model would be

m = 15
n
= 2;
A
= randn(m,n);
b
= rand(m,1)*5;

x
= sdpvar(n,1);
plot
(A*x <= b);
plothyperplanes
(A*x <= b);
u
= binvar(m,1); % Which constraint is violated?
s
= binvar(m,1); % Which is the first one?
index
= (1:m)*s;
model
= [-10 <= x <= 10, implies(A*x >= b, u)];
% s(i) is activated iff u(i) is 1 and s(1:i-1) is 0
model
= [model, sum(s)==1,s >= u - cumsum([0;s(1:end-1)]), s <= u];

optimize
(model,-sum(x));
[double(A*x-b) double(u) double(s)]


Reply all
Reply to author
Forward
0 new messages