# [AMPL 24807] modeling with conditionals

Dear AMPL group, I am trying to model the next conditional constraint: I have a constant vector W[t], In each t If W[t] >= MAX then x[t] = MAX, otherwise if W[t] < MAX, then x[t] = W[t]. Could anyone help me with some tips to do that.

Best regards.

Julio

x[t] = min (MAX, W[t])

You can write the above expression directly in your AMPL model if MAX is also a constant, or if you are using our current versions of Gurobi, COPT, Xpress, Mosek, HiGHS, CBC, or SCIP. For other solvers, depending on your model, you may be able to replace the above constraint with these two:

x[t] <= MAX
x[t] <= W[t]

If none of these cases apply, however, then write back for more help with transforming the constraint to a linear one.

Dear Robert thanks for your help. I have a concern about the formulation x[t] <= MAX, x[t] <= W[t], for instance, as MAX = 3.5 is a constant for all period, if any time t W[t] = 4 (different constant values about the time), instead x[t] = 3.5, it is zero or any other value less than 3.5, for example 2, 1, something like that. I am trying to understand that, and in the literature several authors recommend that formulation as the linearization the min() function.

Regards

x[t] = min (MAX, W[t]) is equivalent to these two constraints:

(1) x[t] <= min (MAX, W[t])
(2) x[t] >= min (MAX, W[t])

You can linearize each of these separately. (1) is easy, as it’s equivalent to the following two linear constraints;

(1a) x[t] <= MAX
(1b) x[t] <= W[t]

(2) is more difficult. You need to define a collection of new binary variables – let’s call them z[t] – and you need to choose a constant M that is larger than any possible value of abs(MAX - W[t]). Then (2) can be specified by adding the following two linear constraints:

(2a) x[t] >= MAX - M * z[t]
(2b) x[t] >= W[t] - M * (1-z[t])

In general, you will need all four constraints (1a), (1b), (2a), (2b) to linearize x[t] = min (MAX, W[t]). If you study the model carefully, you might find that only (1) needs to be linearized, using only (1a) and (1b), but if you are not sure then you can use the complete linearization.

(Also as mentioned previously, if you are using our current versions of Gurobi, COPT, Xpress, Mosek, HiGHS, CBC, or SCIP, then you can just write “x[t] = min (MAX, W[t])” in your model and let AMPL take care of linearizing it.)

Thanks Robert, good explanation. I added the extra constraints, now my MM is working.

Regards