4

I have a continuous variable $x_t$. A binary variable $b_t$ should be coupled to $x_t$ such that $b_t$ has the value $1$ if $x_t$ has a value greater than $0$ and $b_t$ has the value $0$ if $x_t$ has the value $0$. Any idea how I can do that without including the term $b_t$ in the objective function?

worldsmithhelper
  • 4,007
  • 6
  • 21
PeterBe
  • 1,632
  • 2
  • 15
  • 30
  • This is a FAQ -- frequently asked here and frequently answered here. – prubin May 17 '21 at 20:22
  • You will have to choose a tolerance as to how close to zero should be considered zero, taking into account solver tolerance, wherein a variable for which the true optimum is exactly zero will not be exactly zero as computed in and returned by the solver. – Mark L. Stone May 18 '21 at 00:14
  • @prubin I agree this is a FAQ, here is quite a complete answer. But it may be worth answering $x_t=0; \Rightarrow ; b_t = 0$ (?), which is the hard part, I think. – Kuifje May 18 '21 at 06:53
  • @MarkL.Stone Thanks for your answer Mark L. Stone. How can I formulate this with equations? – PeterBe May 18 '21 at 09:04
  • @prubin: Thanks for your comment. Would you mind sending me 2 or 3 links to such questions? I'd highly apprciate this. – PeterBe May 18 '21 at 09:16
  • @Kuifje: Thanks for the link. I think that the answer there does not quite answer my question. There it is stated for continous variables there are 2 cases: Case 1 "if x>b then y=1" --> this also holds for my example and is in line with what I want. In my case b is just 0. However, the Case 2: "if x=b then y=1" is not in line with what I want. if x=b (and b is 0 in my case) then y should be also 0. Any idea how I can formulate this with equations? – PeterBe May 18 '21 at 10:50
  • @PeterBe Try typing "binary variable continuous variable" in the search box. Not all hits are relevant, but it is easy to find some that are. – prubin May 19 '21 at 15:54
  • @prubin: Thanks for your answer. I searched for it but I could not find a question that exactly answered my question. The answers that I found are either wrong or they deal with another case – PeterBe May 20 '21 at 08:39
  • @MarkL.Stone, Kuifje : Do yo have any idea how I can link those variables? I'd appreciate every further comments from you. – PeterBe May 20 '21 at 08:43

3 Answers3

4

I'm only aware of a mechanism that works if there is an upper bound for the continuous variable.

\begin{align}x_{t, \max}\cdot b_t &\geq x_t\\ m\cdot x_t &\geq b_t\end{align}

I used this in answering this question. I'm not aware of a way to solve it for unbounded $x_t$, unless the solver handles floating points infinities correctly. This would need to be tested by solvers. The second equation implements the $x_t < \frac{1}{m}\implies b_t=0 $. If the trueness of $b_t$ is penalized in the objective the second term is unnecessary.

worldsmithhelper
  • 4,007
  • 6
  • 21
  • Thanks worldsmithhelper for your answer. Basically the continous variable has an upper bound of 1 and a lower bound of 0. Nonetheless I think your approach does not work and is wrong. If x(t) has the value 0.2, then your second equations forces b(t) to have the value 0 which is wrong. – PeterBe May 20 '21 at 09:01
  • Mhh you are right. You could fix that by multiplying $x_t$ by a sufficiently large numbers. I edited my answer. – worldsmithhelper May 20 '21 at 09:03
  • Thanks for your answer and effort worldsmithhelper. This solution might work. I will try it. How would your suggested solution change, if the lower bound of x is let's say 0.25 (while the upper bounds remains 1)? – PeterBe May 20 '21 at 09:12
  • $x_{t,max} = 1$ and $m=4$ – worldsmithhelper May 20 '21 at 09:16
  • 1
    Thanks worldsmithhelper for your answer. I upvoted and accepted your answer. I really appreciate your tremendous help. – PeterBe May 20 '21 at 10:22
4

Enforcing $b_t$ to take value $1$ when $x_t$ is positive is done with $x_t \le b_t$, assuming $x_t \le 1$.

For the second part, quoting @MarkL.Stone:

You will have to choose a tolerance as to how close to zero should be considered zero

Let $\epsilon$ be this tolerance. So you want to enforce $$ x_t < \epsilon\implies b_t = 0 $$

Now referring to this link (careful as $b$ in the link is a constant $\neq$ your $b_t$):

To enforce "if $x < b$ then $y=1$": $$b - x \le My,$$ where $M$ is a large constant. The logic is that if $b - x > 0$, then $y$ must equal 1, and otherwise it may equal 0.

Given that $x_t \le 1$, and that you want the binary variable to take value $0$ (and not $1$), the constraint becomes: $$ \epsilon-x_t \le 1-b_t $$

It is easy to see that if $b_t$ takes value $1$, then it implies $x_t \ge \epsilon$, which is the contrapositive of what you want.

Kuifje
  • 13,324
  • 1
  • 23
  • 56
  • Thanks Kuifje for your answer (I upvoted it). The shorter (and simpler) answer given by worldsmithhelper seems to work. I have implement it and not experienced any problems so far. Do you see any problems with that answer? – PeterBe May 20 '21 at 10:10
  • 2
    Both approaches are similar and deal with a tolerance ($\epsilon$ or $1/m$), but the way the tolerance is handled is a bit different. – Kuifje May 20 '21 at 10:18
  • I would say try both and reward the one giving you better performance in your problem. – worldsmithhelper May 20 '21 at 10:22
0

Many solvers and APIs allow logical constraints.

For instance with OPL CPLEX you may write

dvar float+ x;
dvar boolean b;

subject to {

b==!(x==0); }

Alex Fleischer
  • 4,000
  • 5
  • 11
  • Thanks for your answerAlex How can I formulate this with equations? I do not use OPL CPLEX and I would like to have an answer that is independent from the modelling language and the solver. – PeterBe May 18 '21 at 09:05
  • Then see https://or.stackexchange.com/questions/33/in-an-integer-program-how-i-can-force-a-binary-variable-to-equal-1-if-some-cond?rq=1 – Alex Fleischer May 18 '21 at 09:44
  • Thanks for the link Alex. I think that the answer there does not quite answer my question. There it is stated for continous variables that there are 2 cases: Case 1 "if x>b then y=1" --> this also holds for my example and is in line with what I want. In my case b is just 0. However, the Case 2: "if x=b then y=1" is not in line with what I want. if x=b (and b is 0 in my case) then y should be also 0. Any idea how I can formulate this with equations? – PeterBe May 18 '21 at 10:49