Modelling Question - if-then constraint

Dear all,

I would really like to seek for your kind help regarding an if-then constraint. I found in the community a similar topic but my condition is a bit different from that, therefore I want to show my case and ask for your tips again.


Specifically, I have the following variables:

A: decision variable

B: variable

Q: variable


And I want to achieve the following:

1)if A-B>=0, then Q=A-B

2)if A-B<0, then Q=0


I would like to ask if it is possible to consrtuct a linear formulation for this if-then logic to be realised.


Many thanks in advance!




Best answer by Marcel Hunting 30 June 2020, 15:58

View original

10 replies

Userlevel 5
Badge +5

@Xubo , one way to do this is to

  1. Declare Q as a non-negative variable
  2. Declare a constraint Q >= A - B
  3. Add Q to your objective function such that Q is minimized. Eg, if Obj is your objective function, modify it as
    Obj + Q for a minimization problem
    Obj - Q for a maximization problem. 

    The constraint in [2] and the minimization in [3] will make sure that Q stays at its lower bound. The lower bound will be 0 when A - B is <= 0 and it will be A - B when A -B is >= 0. 

Hi @mohansx ,


Thanks a lot for this cool idea! I really appreciate it.


Just there is an issue, that is, under my specific model setting, if Q is not exactly set to A-B, and is allowed the possibility of being larger than A-B, then even if I add it into the objective function to minimize it, it may still not take A-B.

Because there’s possibility that taking a Q which is larger than A-B can reduce some other relevant cost in the objective function, which finally minimizes the total cost. And if I set a big M as per unit cost for Q in the objective function, this big cost may have side-effect which influences the decision of other variables.


Therefore, maybe would it be possible to find another way in which Q can be set to exact A-B when A-B>0?



Userlevel 4
Badge +5

Hi @Xubo,


So, what are you actually trying to achieve with constructing Q as part of your math program, because clearly you can't minimize it in the objective if the particular values of A and B also can have an influence on the objective function?





Userlevel 5
Badge +4

Hi @Xubo. So you want to model Q = max(A-B,0). You can do this as follows, if you know some lower and upper bound on A-B, i.e.,

L <= A-B <= U 

Now introduce a new binary variable b.

Then you can use this MIP formulation:

Q >= A-B
Q <= A-B + (U-L) * b
Q >= 0
Q <= 0 + (U-L) * (1-b)

If A>B then b will be 0, and Q will be A-B. If A<B then b will be 1, and Q will be 0. If A=B then b can be 0 or 1, and Q will be 0.

Hi @Marcel Hunting,


Sorry for my late replying… and really thanks for your support.

Your solution is really amazing and I think it can work perfectly. Just before realizing it and running it once more in AIMMS, I just detected another problem of my model and it is a similar issue of “if-then”. I am struggling with it right now and maybe please allow me to write it down here, and I would be very much grateful if you and all others who are interested in this topic could also help me on figuring it out.


The “if-then” I want to realize with linear formulations is:

a, b, c, d, D: variables


  1. If a-D>=0, then b=D

            1.1. If a-D-c>=0, then d=c

            1.2. If a-D-c<0, then d=a-D

  1. If a-D<0, then b=a, d=0


Many many thanks again to all of you.



Best regards,


Hi @MarcelRoelofs ,


Sorry that it is really hard for me to answer your question clearly here…..

But basically it is, variables A,B, and Q are all not in the objective function, but they will influence the variables in the objective function…


And also thanks a lot for your consideration of my question!


Best regards,



Dear all,


I just worked out a solution to the second question I addressed above. I have not yet tested it in AIMMS but I would like to share with you the formulations of the constraints and look forward to your comments on them and suggestions for improvements, as well as ideas of other solutions to this problem.


The formulations are as follows:

b <= a  (1)

b <= D  (2)

b >= 0  (3)

d <= a - b  (4)

d <= c  (5)

d >= (a - b) - A * m  (6)

d >= c - C * (1 - m)  (7)

d >= 0  (8)

where a, b, c, d, D are variables, A and C are upper bound of a and c, m is binary variable.


(1) - (3) ensure b = min { a, D },

(4) - (8) ensure d = min { c, a - b }.


Best regards,




Userlevel 5
Badge +4

Hi @Xubo.

The first part misses two constraints, namely:

b >= a - A * m1

b >= D - Dupper * (1 - m1)

Here m1 is another binary variable.


With those two constraints added the formulations are correct if you assume that that a, c and D are nonnegative.

Hi @Marcel Hunting,


Thanks a lot for your feedback!


Sorry that I forgot to mention, that there’s a term in the objective function as:

min cost * e


And there’s a constraint as:

D1 = b + e

(here, D1 is another D, which is a stochastic parameter input; so the D in the discussion above is also a parameter input actually, but it is varying from time to time and from scenario to scenario)


Therefore, I think b here is naturally having a tendency to maximize itself, so here (1) - (3) are enough for it to take min { a, D }, right?


Many thanks!






Userlevel 5
Badge +4

Hi @Xubo. Not sure whether that is correct as a higher b can result in a lower d which might have a negative impact on the objective (indirectly).


Didn't find what you were looking for? Try searching on our documentation pages:

AIMMS Developer & PRO | AIMMS How-To | AIMMS SC Navigator