Logical Operators in binvar

156 views
Skip to first unread message

Carlos Neves

unread,
May 19, 2016, 11:42:10 AM5/19/16
to YALMIP
Hello everyone,

I am working with a hybrid system (continuous and discrete) and I want to create an MPC to control it. The dynamics of the system are inserted as constraints. In order to change from the continuous dynamics to discrete, two binary variables must be true. The following constraints are rising an error.

    for t=1:k
        constraints
=[constraints, heelstrike(t)==(2*q(2*t-1)-q(2*t)<=0)];                           %When current 2*teta <= phi
        constraints
=[constraints, swingforward(t)==(q(2*t)<=0.075)];                                 %AND phi <=0.075
        constraints
=[constraints, iff(heelstrike(t) && swingforward(t),ddq(2*t-1:2*t)==Hinv*(u(2*t-1:2*t)+ext(2*t-1:2*t)-Gm-C))];
        constraints
=[constraints, dq(2*(t+1)-1:2*(t+1))==dq(2*t-1:2*t)+ddq(2*t-1:2*t)*dt*t];
        constraints
=[constraints, q(2*(t+1)-1:2*(t+1))==q(2*t-1:2*t)+dq(2*(t+1)-1:2*(t+1))*dt*t];    
   
end

 The error is
The following error occurred converting from sdpvar to logical:
Conversion to logical from sdpvar is not possible.

Error in linearized (line 50)
        constraints=[constraints, implies(binary(heelstrike(t) &&
        swingforward(t)),ddq(2*t-1:2*t)==Hinv*(u(2*t-1:2*t)+ext(2*t-1:2*t)-Gm-C))];

Since I  have also tried to assign fixed values to both binary variables, all that is left is that it is not possible to use && in iff(). Is this correct? Is there a way around this?

Thank you in advance,
Carlos

Johan Löfberg

unread,
May 19, 2016, 11:56:30 AM5/19/16
to YALMIP
Applying short-circuit and (&&) does not make sense. You should use logical and (&)

Is it intentional that ddq has no defined behaviour, if the logical condition doesn't occur?

Carlos Neves

unread,
May 19, 2016, 12:07:23 PM5/19/16
to YALMIP
Well, that solves it. I thought && was the representation of "and" in Matlab.

It is intentional, in the sense that it's the next step :)

Carlos Neves

unread,
May 19, 2016, 12:07:58 PM5/19/16
to YALMIP
Thank you for such a quick response!

Johan Löfberg

unread,
May 19, 2016, 12:15:27 PM5/19/16
to YALMIP
&& is short-circuit and, which isn't  logical condition only, but also a program flow construct

What ever you do, don't do

iff(x<=0,something) +iff(x>=0,something else)

Both x<=0 and x>=0 can be declared false by numerics, and thus you have no defined behavior. Much better to enumerate the case explicitly

binvar case1 case2
implies
(case1,[x<=0, something])+
implies(case2,[x>=0, somethingelse])+sum(case1+case2==1)

Reply all
Reply to author
Forward
0 new messages