# \[AMPL 24819\] linearizing conditional constraints

**URL:** <https://discuss.ampl.com/t/ampl-24819-linearizing-conditional-constraints/756>\
**Category:** Google Group Mirror\
**Created:** [October 7, 2023, 4:15pm UTC](https://discuss.ampl.com/t/ampl-24819-linearizing-conditional-constraints/756 "2023-10-07T16:15:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jclq](https://avatars.discourse-cdn.com/v4/letter/j/bb73d2/32.png) [@jclq](https://discuss.ampl.com/u/jclq)\
**Post date:** [October 7, 2023, 4:15pm UTC](https://discuss.ampl.com/t/ampl-24819-linearizing-conditional-constraints/756/1 "2023-10-07T16:15:33Z")

</div>

Dear team,

I have developed the next constraints for the next conational:

I want a binary variable let say z[t]=0, when 0\<= v[t] \< VMAX, that means, while v[t] is zero in t, then z[t] = 0, On other hand, z[t]=1, when VMIN \<= v[t] \<= VMAX. To get that I formulated the next two constraints:

1. v[t] \>= VMIN\*z[t]
2. v[t] \<= (VMIN - eps)_(1 - z[t]) +VMAX_z[t], where eps is a smallest 1E-5 value.

In this case, I want to use the binary variable z[t] the next equation:

r[t] = x[t] + s[t] + H[t], where r, x, y are no negative variables and H[t] is a parameter. Here I want to say that while z[t]=0, that is 0\<= v[t] \< VMAX, then r[t] = H[t] and x[t] = s[t] = 0, and only after v[t] reached the VMIN value, that is VMIN \<= v[t] \<= VMAX, then r[t] = x[t] + s[t] + H[t], where x and s are positive different of zero. To get that I made:

r[t] = x[t] + s[t] + H[t]

x[t] \<= M_z[t]  
s[t] \<= M_z[t]

Please help me checking the proposed formulation, and if you have another best option to formulate that, your suggestions always will be important.

Regards

Julio

---

<div class="post-metadata">

**Author:** ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)\
**Post date:** [October 12, 2023, 12:57pm UTC](https://discuss.ampl.com/t/ampl-24819-linearizing-conditional-constraints/756/2 "2023-10-12T12:57:51Z")

</div>

Are you saying that you want z[t] and v[t] to be related like this?

z[t] = 0 ==\> v[t] = 0  
z[t] = 1 ==\> VMIN \<= v[t] \<= VMAX

That can be written equivalently as these linear constraints:

v[t] \>= VMIN \* z[t]  
v[t] \<= VMAX \* z[t]

However, the constraints you have written imply that

z[t] = 0 \<==\> 0 \<= v[t] \<= VMIN - eps  
z[t] = 1 \<==\> VMIN \<= v[t] \<= VMAX

If that is what you want, then your constraints are correct. 1e-5 would be a reasonable value to try for eps.

For your other constraints x[t] \<= M_z[t] and s[t] \<= M_z[t], it should be clear that they only say z[t] = 0 ==\> x[t] = 0 and z[t] = 0 ==\> s[t] = 0.

In general, if you want to check constraints involving a binary variable, just substitute 0 for the variable and see what constraints you get, then substitute 1 for the variable and see what constraints you get.

---

<div class="post-metadata">

**Author:** ![jclq](https://avatars.discourse-cdn.com/v4/letter/j/bb73d2/32.png) [@jclq](https://discuss.ampl.com/u/jclq)\
**Post date:** [October 12, 2023, 1:39pm UTC](https://discuss.ampl.com/t/ampl-24819-linearizing-conditional-constraints/756/3 "2023-10-12T13:39:52Z")

</div>

Thanks, Robert, for your reply.

To understand mare clearly mu problem. I have an equation v[t] = x[t] + s[t] + other variables, and I have other related equation r[t] = x[t] + s[t] + H[t], where r, x, y are no negative variables and H[t] is a parameter, also VMIN \<= v[t] \<= VMAX, where VMIN is not zero. v[t] represents the water volume of a reservoir. I start in the first-time t0 of analysis with the reservoir empty, so, I want that while 0 \<= v[t] \< VMIN i.e. While v[t] does not reach the VMIN the variables x[t] = s[t] = 0 and r[t]=H[t] where H[t] is an external inflow.

Summarizing, I want to do that:

z[t] = 0 if 0 \<= v[t] \< VMIN  
z[t] = 1 if VMIN \<= v[t] \<= VMAX

and

r[t] = x[t] + s[t] + H[t]

x[t] \<= M_z[t]  
s[t] \<= M_z[t]

Would represent the equations in the previous email the above equations.

Regards

Julio

---

<div class="post-metadata">

**Author:** ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)\
**Post date:** [October 14, 2023, 2:05pm UTC](https://discuss.ampl.com/t/ampl-24819-linearizing-conditional-constraints/756/4 "2023-10-14T14:05:05Z")

</div>

Given your description of the constraints that you want to include, your linearization looks correct to me.

---

<div class="post-metadata">

**Author:** ![jclq](https://avatars.discourse-cdn.com/v4/letter/j/bb73d2/32.png) [@jclq](https://discuss.ampl.com/u/jclq)\
**Post date:** [October 18, 2023, 2:33pm UTC](https://discuss.ampl.com/t/ampl-24819-linearizing-conditional-constraints/756/5 "2023-10-18T14:33:49Z")

</div>

Thanks dear Robert
