# Variable logical bound

**URL:** <https://discuss.ampl.com/t/variable-logical-bound/476>\
**Category:** Support\
**Tags:** modeling\
**Created:** [April 5, 2023, 10:02am UTC](https://discuss.ampl.com/t/variable-logical-bound/476 "2023-04-05T10:02:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Milan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/milan/32/845_2.png) [@Milan](https://discuss.ampl.com/u/Milan)\
**Post date:** [April 5, 2023, 10:02am UTC](https://discuss.ampl.com/t/variable-logical-bound/476/1 "2023-04-05T10:02:01Z")

</div>

Dear All,

using the following  
subject to LL {j in POP}: pl[j] \* Use[j] \<= p[j];

The variable can not be lower than pl, but it can be zero.

var Use {POP} binary;

Gurobi solves to optimality, no issue reported, but some values are below pl (and above 0). Can you see anything wrong? Thank you.

---

<div class="post-metadata">

**Author:** ![Milan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/milan/32/845_2.png) [@Milan](https://discuss.ampl.com/u/Milan)\
**Post date:** [April 5, 2023, 1:57pm UTC](https://discuss.ampl.com/t/variable-logical-bound/476/2 "2023-04-05T13:57:44Z")

</div>

I just figured it out:  
subject to LL {j in POP}: pl[j] \<= p[j] or 0 == p[j];

---

<div class="post-metadata">

**Author:** ![fdabrandao](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/fdabrandao/32/63_2.png) [@fdabrandao](https://discuss.ampl.com/u/fdabrandao)\
**Post date:** [April 6, 2023, 2:07pm UTC](https://discuss.ampl.com/t/variable-logical-bound/476/3 "2023-04-06T14:07:21Z")

</div>

Hi @Milan, in this case the logical constraint as numerical benefits over the other approach since it can avoid numerical issues.

When you had the `Use` variables, what were the magnitude of the violations? Typically that would be a bug, but if the value of pl[j] is high, then `pl[j] * Use[j] <= p[j]` can result in `p[j]` lower than `pl[j]` since there is integrality tolerance in Gurobi that allows it to be something like `0.99999` which when multiplied by a large number would allow `p[j]` to be smaller than `pl[j]`.

---

<div class="post-metadata">

**Author:** ![Milan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/milan/32/845_2.png) [@Milan](https://discuss.ampl.com/u/Milan)\
**Post date:** [April 7, 2023, 5:51am UTC](https://discuss.ampl.com/t/variable-logical-bound/476/4 "2023-04-07T05:51:24Z")

</div>

> [@Milan](#):
>
> subject to LL {j in POP}: pl[j] \* Use[j] \<= p[j];

Thank you for your reply. I have attached the .mod and .dat files (a small example). I tested with Gurobi and COPT. The first approach (logical variable) provides:

Gurobi 10.0.1: Gurobi 10.0.1: optimal solution; objective 12.48871853  
0 simplex iterations  
6 barrier iterations  
1 branching nodes

p [\*] :=  
1 0.0000  
**2 0.0224**  
3 0.4665  
4 0.5111  
;

The second approach satisfies the min limits (0.05):  
Gurobi 10.0.1: Gurobi 10.0.1: optimal solution; objective 12.47575482  
16 simplex iterations  
1 branching nodes  
absmipgap=3.24048e-05, relmipgap=2.59742e-06  
Gain = 12.4758

p [\*] :=  
1 0.0000  
**2 0.0500**  
3 0.4242  
4 0.5258  
;

I would appreciate any advice on this. COPT and GUROBI provided identical results.

Thank you, Milan

[osq.dat](https://discuss.ampl.com/uploads/short-url/blUZ9KXxsNoml38uqfo9fOojM8q.dat) (292 Bytes)  
[osq.mod](https://discuss.ampl.com/uploads/short-url/m31hMAUUXuORuU04lOpGgGwfXAr.mod) (518 Bytes)

---

<div class="post-metadata">

**Author:** ![4er](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/4er/32/530_2.png) [@4er](https://discuss.ampl.com/u/4er)\
**Post date:** [April 7, 2023, 10:40pm UTC](https://discuss.ampl.com/t/variable-logical-bound/476/5 "2023-04-07T22:40:43Z")

</div>

The version with

```auto
subject to LL {j in POP}: pl[j] <= p[j] or 0 == p[j];

```

specifies that either `p[j]=0` or `p[j]>=pl[j]`. Gurobi’s solution satisfies this requirement, with p[1] equal to 0 and p[2], p[3], and p[4] equal to 0.05 or larger. The other version has

```auto
var Use {POP} binary;
subject to LL {j in POP}: pl[j] * Use[j] <= p[j];

```

which specifies that either `p[j]>=0` (when `Use[j]=0`) or `p[j]>=pl[j]` (when `Use[j]=1`) — which is not the same thing. To get the same effect as the previous formulation, you should modify constraints LL and LU as follows:

```auto
var Use {POP} binary;
subject to LL {j in POP}: p[j] >= Use[j] * pl[j];
subject to LU {j in POP}: p[j] <= Use[j] * pu[j];

```

Here you can see that either `p[j]>=0` and `p[j]<=0`, hence `pj[j]=0` (when `Use[j]=0`) or `p[j]>=pl[j`] and `p[j]<=pu[j]` (when `Use[j]=0`).

---

<div class="post-metadata">

**Author:** ![Milan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/milan/32/845_2.png) [@Milan](https://discuss.ampl.com/u/Milan)\
**Post date:** [April 9, 2023, 3:55am UTC](https://discuss.ampl.com/t/variable-logical-bound/476/6 "2023-04-09T03:55:16Z")

</div>

> [@4er](#):
>
> `subject to LU {j in POP}: p[j] <= Use[j] * pu[j];`

Thank you very much for your help. Indeed everything works now. Given the specifics of this problem, which of the two approaches should be preferred?

---

<div class="post-metadata">

**Author:** ![4er](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/4er/32/530_2.png) [@4er](https://discuss.ampl.com/u/4er)\
**Post date:** [April 11, 2023, 4:35pm UTC](https://discuss.ampl.com/t/variable-logical-bound/476/7 "2023-04-11T16:35:41Z")

</div>

I prefer the approach that uses the `or` operator, because it expresses the constraint more clearly.

You might want to experiment with the other approach, just to see if it’s noticeably faster with the solver you are using. Sometimes, different formulation approaches cause the problem to be sent to the solver in different ways, which can possibly make a difference in solve times. I would only recommend this if you have trouble with your solve taking a very long time, however.
