5

like the title suggest, I am trying to constraint a boolean with another boolean, if that makes sense.

x and y are both booleans, where y=1 when Parameter < Variable1

What I am trying to do is the following.

if y = 1, then 0 <= x <= 1. x can be either 0 or 1
if y = 0, then x <= 0. x can only be 0

I hope it makes sense, and I would really appreciate any help

Kuifje
  • 13,324
  • 1
  • 23
  • 56
DVRJ
  • 91
  • 2
  • 1
    I didnt get the part where y=1 when Parameter>Variable1. Can you please clarify that. Otherwise, relation between x & y can be modeled as x <= y – anjikum Oct 11 '21 at 14:48
  • Hi, what I meant was that y is a boolean that is dependant from being over a certain threshold. For example, when filling a glass of water, y = 1 when the amount of water (Variable1) that is already in the glass is OVER 250ml (Parameter) i.e. if amount of water in the glass is 350ml, then y =1. Also, I already tried x <= y but using Pyomo and Gurobi it takes ages to solve – DVRJ Oct 11 '21 at 15:05
  • 1
    Some optimization problems having boolean or integer variables (MIP) take ages to solve. That is the unfortunate reality. – Mark L. Stone Oct 11 '21 at 15:08
  • Hi, yeah I am aware of that, perhaps I am missing something, but by adding said constraint (if y =1 then 0 <= x <= 1...) it goes from solving 1,000,000 values in around 1min, to almost 16hrs... Not sure if can model it in a different way so it does not take that long. – DVRJ Oct 11 '21 at 15:17
  • 2
    @DVRJ welcome to NP-hardness. I suggest you post your full model in a separate question for some tips on how to improve it. – Kuifje Oct 11 '21 at 15:18

1 Answers1

6

It looks like you want to model $x \; \Longrightarrow \; y $, or in conjunctive normal form : \begin{align*} \lnot x \vee y \\ 1-x + y \ge 1 \\ x \le y \end{align*}

If $y$ takes value $1$ when a given parameter $p$ is larger than a given variable $z$, then you need to add $p > z \; \Longrightarrow y=1$, or its contraposition $y=0 \; \Longrightarrow \; p \le z$ with, for example, a bigM constraint: $$ p \le z + My $$ $M$ is the "smallest large constant" you can think of.

Kuifje
  • 13,324
  • 1
  • 23
  • 56
  • Hi, thanks for your comment. I already did the bit where y = 1 when p < z. I did something like this... p-z <= bigM * (1-y) and z-p <= bigM * y. Also, I am trying your suggestion 1-x+y >= 1 and I will let you know if it worked or not – DVRJ Oct 11 '21 at 15:24
  • Note that $1-x+y \ge 1$ is the same as $x\le y$ (obviously). And I suspect it is your bigM constraints that are making the problem hard to solve. Remove them, and replace them with the suggestion above. In particular, your first equation models $y=1; \Longrightarrow ; p \le z$, which is not what you want. And your second one models $y=0; \Longrightarrow ; z \le p$, which is not want you want either. – Kuifje Oct 11 '21 at 15:31