-1

In Python, I'm using the PuLP library to maximize values of side_1 and side_2. They have a constraint: The values of side_1 and side_2 must be equal and opposite to each other. I would like to set a minimum absolute value of side_1 and side_2. If that minimum absolute value cannot be reached, I'd like for the value to be 0 for both side_1 and side_2.

Below, the code will give me a value of -2 for side_1 and 2 for side_2. side_1 value must equal side_2 value:

model = p.pulp.LpProblem('linear_programming', p.LpMaximize)
solver = p.getSolver('PULP_CBC_CMD')

side_1 = p.LpVariable('side_1',lowBound=-3,upBound=0,cat='integer')
side_2 = p.LpVariable('side_2',lowBound=0,upBound=2,cat='integer')

sum_obj = side_2 - side_1
model+=sum_obj

sum_cons = side_2 + side_1 == 0
model += sum_cons

results = model.solve(solver=solver)
print(f'Model Resuts: {p.LpStatus[results]}')

I'd like to set an absolute minimum_threshold that the values can be, or set the result values to 0 for side_1 and side_2. In this case, if I set a threshold of 3, it would return 0 because side_2 has a upBound of 2

I've tried to add a constraint:

minimum_threshold = 3
constraint = side_2 * (minimum_threshold - side_2) >= 0
model += constraint

This would work, but PuLP does not allow multiplications of vars unless one side is a constant.

Are there any ways around this, or a different library that can handle the multiplication I tried above?

  • It isn't really clear what you are trying to do. The upper/lower bounds are *fixed* so there is no "if the upper bound..." Do you mean to say the *value* of one variable influences the value of the other variable? If so, could you edit your post and state more clearly? It might be helpful to include a small table or example showing the relationship of the values, etc. – AirSquid Mar 19 '23 at 05:07
  • Sorry about that @AirSquid and thanks for your answer. I rewrote the question without changing the upper bound since it is fixed. the relationship between side_1 and side_2 is that the model must result in equal and opposite values assigned to them. Is this a little more clear? – Frankie No Naps Mar 19 '23 at 05:39
  • Still some confusion. I think you misunderstand the use of the bounds. If you know the `minimum_threshold` and the `upper_bound` before you solve the problem, and the threshold > upper bound, you already know the outcome.... No solving required. Why can't you compute the value of `side_2` and therefore `side_1` in this case ? Also, if two variables are "equal and opposite", you only have one variable that can be expressed both ways: `-x, x`. – AirSquid Mar 19 '23 at 14:20

1 Answers1

1

I'd like to set an absolute minimum_threshold that the values can be, or set the result values to 0 for side_1 and side_2. This is called a semi continuous variable Most solvers and tools have direct support for this, but afaik PuLP does not. You can use a binary variable however:

   δ⋅THRESHOLD ≤ side_2 ≤ δ⋅UPBOUND
   side_2 ∈ [0,UPBOUND] 
   δ ∈ {0,1}

As delta_1 = -delta_2, it will also obey the threshold.

Note that the bounds in

   side_1 = p.LpVariable('side_1',lowBound=-3,upBound=0,cat='integer')
   side_2 = p.LpVariable('side_2',lowBound=0,upBound=2,cat='integer')

do not make sense as

 side_1 = -side_2

We want: the lowerbound on side_1 is equal to -upperbound of side_2.

Erwin Kalvelagen
  • 15,677
  • 2
  • 14
  • 39