How can I model this binary logic?

I am looking for a constraint to express the following:

IF W1 = 0 AND W2 = 0 THEN Y = 0
IF W1 = 0 AND W2 = 1 THEN Y = 1
IF W1 = 1 AND W2 = 0 THEN Y = 0
IF W1 = 1 AND W2 = 1 THEN Y <= 1

Variables W1, W2, Y are binaries. Y is determined by the aforementioned relations.

• With your previous question (which has been answered), you should be able to do this one easily. Otherwise you did not understand the previous one, obviously :) I suggest you give it a try and the community will guide you based on your try. – Kuifje Mar 5 at 14:46
• Well, I understand that the previous one is correct, but I am not good enough to get to the new one. – Clement Mar 5 at 14:51
• Do you understand @RobPratt's explanation ? If not, how do you relate both $Y$s in the two questions ? – Kuifje Mar 5 at 14:55
• Sorry, I am not familiar with what Rob was writting about. – Clement Mar 5 at 15:00
• – SecretAgentMan Mar 11 at 17:09

As in your other question, the fourth proposition is a tautology. The other three propositions can be expressed as $$((\neg W_1 \land \neg W_2) \implies \neg Y) \land ((\neg W_1 \land W_2) \implies Y) \land ((W_1 \land \neg W_2) \implies \neg Y)$$ More simply, combine your first and third propositions and omit the fourth one to obtain $$(\neg W_2 \implies \neg Y) \land ((\neg W_1 \land W_2) \implies Y)$$

Now rewrite in conjunctive normal form, by replacing $$P \implies Q$$ with $$\neg P \lor Q$$, pushing $$\neg$$ inwards, and distributing $$\lor$$ over $$\land$$: $$(W_2 \lor \neg Y) \land ((W_1 \lor \neg W_2) \lor Y)\\ (W_2 + (1 - Y) \ge 1) \land (W_1 + (1 - W_2) + Y \ge 1)\\ (W_2 \ge Y) \land (W_1 + Y \ge W_2)$$

Several other examples are here.

A good reference for this is:

Raman, R. and I.E. Grossmann, Relation Between MILP Modelling and Logical Inference for Chemical Process Synthesis, Computers Chem. Engng. 15 (1991).

• Thanks, I will look into that. – Clement Mar 5 at 15:34
• Ok, I think I have got it. <br/> W1 + W2 => Y <br/> W1 - W2 => -Y <br/> W1 - W2 <= 1 - Y <br/> – Clement Mar 5 at 17:01

Regardless of Rob's comprehensive answer, the problem is a bit strange. The last constraint is always met, and in the others $$W_1$$ does not affect the result, almost. That's why I get the trivial solution $$Y = W_2$$. However, if $$W_1 = W_2 = 1$$, then $$Y$$ can take both 0 and 1.