Using logic constraint in yalmip

1,375 views
Skip to first unread message

joushua

unread,
Mar 31, 2013, 8:11:34 AM3/31/13
to yal...@googlegroups.com
Hello Johan

I have some questions that can change known parameter in different constraints.
for my objective function: 
x=sdpvar(1,144);
F2=0;F3=0;F4=0;
for i=1:36
f2=((x(2+4*(i-1))-d1(i))/d1(i))^2;
f3=((x(3+4*(i-1))-d2(i))/d2(i))^2;
f4=((x(4+4*(i-1))-smax)/smax)^2;
F2=F2+f2;F3=F3+f3;F4=F4+f4;
end
f=F2+F3+F4;
d1(i),smax,ruled are parameters 
Now I want to set some constraints if x(4+4*(i-1))<=ruled(i) then d1(i)=0.5*d1(i);etc...
I use implies but  implies only used in binvar,how can I solve these problems.
I try to set new binary variable to using in implies  but it doesn't work.Thanks a lot

Johan Löfberg

unread,
Mar 31, 2013, 8:32:44 AM3/31/13
to yal...@googlegroups.com
I don't understand "then d1(i)=0.5*d1(i);". Doesn't make sense.

Implies applies to other things than binaries (although it is sensitive and easily can lead to poor models)

joushua

unread,
Mar 31, 2013, 9:03:33 AM3/31/13
to yal...@googlegroups.com
Before I write my objection function,I have defined some parameters.For example: smax, d1, d2. 

 "then d1(i)=0.5*d1(i);" This point I mean " if x(4+4*(i-1))<=ruled(i)'' then orginal d1(i) numbers multiply by 0.5 and replace d1(i)

Johan Löfberg

unread,
Mar 31, 2013, 11:44:01 AM3/31/13
to yal...@googlegroups.com
If the value of the parameter d1 depends on a decision variable, then by definition it is not a parameter, but a decision variable just as any other decision variable.

If I understand you correctly, you have an if-else

if x(4+4*(i-1))<=ruled(i)
 d1
(i) should be equal to VALUE
else
 d1
(i) should be equal to 0.5*VALUE
end


where VALUE is a known constant.

In principle, you could do
d1 = sdpvar(36,1);

for i...
Constraints = [Constraints, implies(
x(4+4*(i-1))<=ruled(i), d1(i) == 0.5*VALUE)];
Constraints = [Constraints, implies(x(4+4*(i-1))>ruled(i),  d1(i) == VALUE)];
end

However, this will lead to a very sensitive model and it is very likely that the value of d1 is undefined (since solvers have a tolerances around 0, so both conditions might be declared as false by the solver. Additionally, strict inequality is not supported by YALMIP, so the code is merely theoretical.

A better model is to introduce a binary variable manually (there are binary models introduced above) and explicitly declare the disjunction such that exactly one condition is met
d1 = sdpvar(36,1);
active1 = binvar(36,1);
for i...
Constraints = [Constraints, implies(
active1(i), [x(4+4*(i-1))<=ruled(i), d1(i) == 0.5*VALUE(i)])];
Constraints = [Constraints, implies(~active1(i), [x(4+4*(i-1))>=ruled(i),  d1(i) == VALUE(i)])];
end

Although this will connect d1 better with the condition on x, it will not be a good way to attack your model, since your model will become nonlinear and nonconvex when you use the decision variable in f2 (division). To combat that, you should model the different cases of f2 directly without introducing the decision variable d1.

f2 = sdpvar(36,1);
for i = ...
Constraints = [Constraints, implies(active1(i), [x(4+4*(i-1))<=ruled(i),f2==((x(2+4*(i-1))-0.5*VALUE(i))/(0.5*VALUE(i))
)^2;
Constraints = [Constraints, implies(~active1(i), [x(4+4*(i-1))>=ruled(i),f2==((x(2+4*(i-1))-VALUE(i))/VALUE(i)
)^2;

end
F2 = sum(f2)





sd
 

Sumedha Sharma

unread,
Mar 12, 2018, 4:32:09 AM3/12/18
to YALMIP
I have two sdpvars, Q and t.
I want to write the following constraints-
for k=1:N (N is the horizon)
if Q(k)=0, t(k+1)=t(k), else t(k+1)=0
end
I also have the initial value for t i.e. t(1).
How can I write this constraint.

Johan Löfberg

unread,
Mar 12, 2018, 4:53:37 AM3/12/18
to YALMIP
So if Q(k) = 0.000000000000000000000000000000000000000000000000000000000000000000000000001, you want to have a very different behavior compared to the case Q(k)=0?

trying to say that you model doesn't sound very robust.

In general though, you use the implies operator. 

I would say you have three case, 

for loop
 binvar a b c
 
Model = [Model, a+b+c == 1
                 implies
(a, [Q(k) <= -tol, t(k+1)==0])+
                 implies
(b, [-tol <= Q(k) <= tol, t(k+1)==t(k)]) +
                 implies
(c, [tol <= Q(k), t(k+1)==0])]
end



Marius Masuch

unread,
Mar 23, 2022, 10:01:31 AM3/23/22
to YALMIP
Hello Johan,

I have an optimization model where exactly the same case occurs, which you have described in your last answer. 
(I want to use the implies function to figure out if the value of the elements of the decision variable Q are zero or not. If an element is unequal to zero, it should be marked in the vector t with a "1" and if it is equal to 0, then a "0" should be added to the vector t.)

I have implemented the implies function as described by you with the three cases and the tolerance barrier "tol".

 d = binvar(3);
for loop
 Constraints = [Constraints, sum(d) == 1, lowerbound <= Q(k,1) <= upperbound, 0 <= t(k,1) <= 1];
 Constraints = [Constraints, implies(d(1), [Q(k,1) <= -tol, t(k,1) == 1]) ];
 Constraints = [Constraints, implies(d(2), [-tol <= Q(k,1) <= tol, t(k,1) == 0]) ];
 Constraints = [Constraints, implies(d(3), [tol <= Q(k,1), t(k,1) == 1]) ];
end

Unfortunately, in some cases the solver sets the value for some elements of Q(k,1) to the value of "tol", even though the objective function would be minimized, if these elements would be 0. (This also leads to some elements in the vector t being marked with a 1, although they are supposed to be 0)

Therefore I would like to ask you if you could explain to me why some elements are set to the value "tol" and if you could help me with this problem.

Many thanks in advance!
Marius

Johan Löfberg

unread,
Mar 23, 2022, 10:10:48 AM3/23/22
to YALMIP
Being not zero, for a continuous variable, is extremely challenging to model with a numerical solver, and even after adding margins you can have issues, as you will struggle with the solver tolerances for seeing constraints as feasible. YOu can have something like x <= margin should be equivalent to d=1, and then get a solution d=1 with x = margin+1e-8 as that is a good-enough solution for the solver

but your code looks fishy as you use the same vector d for all conditions as it is defined outside the loop

Johan Löfberg

unread,
Mar 23, 2022, 10:13:36 AM3/23/22
to YALMIP
and why don't you define t as binary, as it only can take values 0 or 1 (or better yet simply assign it to be d(k,1)+d(k,3))

Marius Masuch

unread,
Mar 24, 2022, 10:35:11 AM3/24/22
to YALMIP
okay, thanks for your answer! So there is no more robust way to identify nonzero elements?

But why does the solver sets the value of some Q elements to the value of "tol"? Even when I set tol to a higher value (i.e.: tol = 0.01), the solver calculates some elements of Q also to 0.01, even though it would be better for minimizing the objective function if these elements would be zero. (The lowerbound in these cases is = 0 and the upperbound is a positive value, which represents the maximal possible value of the element. So the bounds of the element should allow, that it could be equal to 0)



Johan Löfberg

unread,
Mar 24, 2022, 10:46:36 AM3/24/22
to YALMIP
Although I am not clear what you are talking about, but if you are talking about tol in the sense of the margin of the model for the MILP model, not solver tolerance then if you accept to model x=0 as -0.01<=x<=0.01, you can still have that the solver returns 0.0100000000001 but thinks the (big-M related) constraint is satisfied, as it only violates the constraint with 0.00000000001, so essentially around 0.01 either d2 or d3 can be 1.

but as I said, the code you showed is buggy as it uses the same d.

Marius Masuch

unread,
Apr 5, 2022, 8:19:33 AM4/5/22
to YALMIP
Thanks for your answer! I understood that for very small deviations the solver can think that the constraints are still satisfied and I have adjusted the problem with the vector d.

Please excuse me if I have expressed myself unclearly. By the variable "tol" I mean a variable that I have created to check if the elements of Q are in a certain range (i.e. around the value 0). I did not mean the solver tolerance.

Independent of the values that the vector d takes, my problem now is that the solver sets the values of Q elements to the value "tol" in some cases (where it would be better to set the elements to 0 to minimize the objective function).
(For example: The value of "tol" is set to 0.1 by me and to minimize the objective function it would be optimal if the element Q(1,1) is 0. The variable "lowerbound" has the value 0 and "upperbound" a positive value (which corresponds to the theoretically possible maximum of the element Q(1,1)). Without my constraints using the implies function, the solver sets the value of the element Q(1,1) to 0. But when I add the constraints, the solver sets the value of the element Q(1,1) to the value of the variable "tol" (so in this case to 0.1), resulting in a higher objective function value. (Although the bounds would allow Q(1,1) to be set to 0).

Johan Löfberg

unread,
Apr 5, 2022, 10:37:20 AM4/5/22
to YALMIP
You would have to supply an (minimal) example where the solver has the opportunity to lower the cost significantly does not do so.
Reply all
Reply to author
Forward
0 new messages