# Assigning a binary variable 1 when another variable exceeds threshold

**URL:** <https://discuss.ampl.com/t/assigning-a-binary-variable-1-when-another-variable-exceeds-threshold/820>\
**Category:** Support\
**Created:** [November 20, 2023, 4:55pm UTC](https://discuss.ampl.com/t/assigning-a-binary-variable-1-when-another-variable-exceeds-threshold/820 "2023-11-20T16:55:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sarah\_O](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/sarah_o/32/321_2.png) [@Sarah\_O](https://discuss.ampl.com/u/Sarah_O)\
**Post date:** [November 20, 2023, 4:55pm UTC](https://discuss.ampl.com/t/assigning-a-binary-variable-1-when-another-variable-exceeds-threshold/820/1 "2023-11-20T16:55:47Z")

</div>

Hi

I am trying to assign 1 to a binary variable if another variable exceeds a certain threshold value to satisify a QoS constraint. The objective function is a weighing of maximizing the number of elements satisfying the QoS constraint. Here is a snippet of the relevant lines of the model

param beta=1e3;  
var SINR{u in U}\>=0;  
var C{u in U} \>=0 binary;

# Objectives

maximize ParetosSINRandServedUsers: sum{u in U} SINR[u] + beta \* sum{u in U} C[u];

# QoS constraints

s.t. ConsQoS{u in U}:  
C[u] = 1 ==\> SINR[u] \>= 36.3 else C[u]= 0;

I receive the following error message  
CPLEX 22.1.1.0:  
logical constraint \_slogcon[2] is not an indicator constraint.  
which when I used “print \_slogconname[2];” indicate constraint ConsQoS[2] which I know is the case when SINR does not exceed the threshold according to the model input data. I do not receive an error in other cases where model input data allow QoS to be satisfied.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Sarah\_O](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/sarah_o/32/321_2.png) [@Sarah\_O](https://discuss.ampl.com/u/Sarah_O)\
**Post date:** [November 21, 2023, 3:21pm UTC](https://discuss.ampl.com/t/assigning-a-binary-variable-1-when-another-variable-exceeds-threshold/820/2 "2023-11-21T15:21:09Z")

</div>

I used the constraint SINR[u]\>=36.3_C[u]; which results in C equals to zero when SINR is less than 36.3.  
While it seems working like this, I am still not conviced since I want the SINR not to be caluclated in the case of C equals zero. I am thinkin of multiplying it by the numerator which contains another binary varialbe and then linearize the multiplication of two binary variables. This is done by adding adding a new binary variable z (to replace x_y) and then addition the three constraints z\<=x; z\<=y; z\>=x+y-1;

P.S the way of expressing the logical constraint (had I used this approach) is not correct after else I should have typed SINR[u]\<36.3 instead of C[u]=0;.

---

<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:** [November 21, 2023, 5:33pm UTC](https://discuss.ampl.com/t/assigning-a-binary-variable-1-when-another-variable-exceeds-threshold/820/3 "2023-11-21T17:33:09Z")

</div>

Are you trying to say that “if SINR[u] exceeds 36.3, then C[u] = 1”? This could be written

```auto
SINR[u] > 36.3 ==> C[u] = 1

```

and that is equivalent to the following indicator constraint:

```auto
C[u] = 0 ==> SINR[u] <= 36.3

```

If you also want to say that “if SINR[u] does not exceed 36.3, then C[u] = 0” then you could write

```auto
C[u] = 0 ==> SINR[u] <= 36.3 else SINR[u] >= 36.3001

```

(You can’t write “SINR[u] \> 36.3” because CPLEX recognizes only \>= and \<=, so instead I have written \>= 36.3 + a small number.)

(If you still have trouble, please upload files that can be used to reproduce the error that you are seeing.)

---

<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:** [November 22, 2023, 5:12pm UTC](https://discuss.ampl.com/t/assigning-a-binary-variable-1-when-another-variable-exceeds-threshold/820/4 "2023-11-22T17:12:38Z")

</div>

I did not see your most recent message before I replied. You can use

```auto
SINR[u] >= 36.3 * C[u]

```

which will force SINR[u] to be \>= 36.3 when C[u] = 1. However when C[u] = 0 this constraint will become SINR[u] \>= 0 and it will not have any effect (since SINR[u] is already defined as a nonnegative variable).

I am not able to understand some of your other statements:

- _I want the SINR not to be calculated in the case of C equals zero._ SINR[u] is a variable, so the solver will always assign it some value for each u.

- _I am thinking of multiplying it by the numerator . . ._ It is not clear which numerator you mean. There should be some fraction of the form a/b, where a is the numerator.

---

<div class="post-metadata">

**Author:** ![Sarah\_O](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/sarah_o/32/321_2.png) [@Sarah\_O](https://discuss.ampl.com/u/Sarah_O)\
**Post date:** [November 30, 2023, 2:15pm UTC](https://discuss.ampl.com/t/assigning-a-binary-variable-1-when-another-variable-exceeds-threshold/820/5 "2023-11-30T14:15:30Z")

</div>

First, thank you so much for the reply and your service to the community of optimization with AMPL Professor.

I am not familiar with the format of logical constraints, but it is now clear. I have used your suggestion: C[u] = 0 ==\> SINR[u] \<= 36.3 else SINR[u] \>= 36.3001

I could not regenerate the logical constraint error message for now as I made multiple modifications, but will post about it if I have a regenerateable error. However, the “greater than” causes it as you explained AMPL does not recognize it.

SINR will now be equal to 0 in the model when QoS constraint is not met as I want it because I removed a constraint that enforces setting of another binary variable to equal 1 for each user (my wording was not accurate, may be I should’ve said not included or I want it to be zero)

Sorry my question was not be clear because I have adapted it to reflect only my needs ( SINR variable is not calculated directly, it is a ratio of signal to noise-plus-interference which is a nonlinear term. There is a binary variable in the numberator multiplied by a parameter. This binary variable is also in the denominator and I did cross multiplication and then further linearization to the quadratic term where a binary variable is multiplied by the continuous variable, some of the model equations are attached as I could not attach a PDF file).

Again many thanks \<3.  
 ![equations](https://us1.discourse-cdn.com/flex019/uploads/ampl/original/1X/8cc947e74874d55d5498411be0c362b8ba61fa08.png)
