0

I am trying to formulate an objective function for cost optimization in PuLP wherein the maximum of an array is added to the objective function. Please ignore the indentation.

#Decision Variables
allocation_vars = LpVariable.dicts(
    'Allocation',
    [(i,j,k) for i in TruckTypes for j in Days for k in RS],
    0,
    LpInteger
)

#Objective Function
for i in TruckTypes:
    for j in Days:
        prob += max(allocation_vars[(i, j, k)] * TransCost[i][k] for k in RS)

I am getting the following error when trying to run the above :

prob += max(allocation_vars[(i, j, k)] * TransCost[i][k] for k in RS)

TypeError: '>' not supported between instances of 'LpAffineExpression' and 'LpAffineExpression'
Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49
  • `max()` is not a linear function, so this violates the requirement for a linear objective function.... you're going to have to reformulate. – AirSquid Sep 13 '20 at 18:45
  • Reformulation seems infeasible as per the logic of the problem. Can you suggest a python package/solver that can solve Integer Non Linear Optimization Problem such as above? – Utkarsh Srivastava Sep 13 '20 at 19:52

1 Answers1

2

You should reformulate, as @AirSquid said.

Try instead the following:

  1. Create a dummy variable m[i][j], add that to the objective function;
m = LpVariable.dicts(
    'maxCosts',
    [(i,j) for i in TruckTypes for j in Days],
    0,
    LpInteger
)

prob += lpSum([m[i][j] for j in Days for j in TruckTypes])
  1. Add the following constraint:
for i in TruckTypes:
    for j in Days:
        for k in RS:
            prob += allocation_vars[(i,j,k)]*TransCost[i][k] <= m[i][j]

Supposing you have a minimisation problem, this will work exactly the same as a max: it will reduce m[i][j] as much as possible, and to reduce it more, it will try to reduce the maximum of all allocation_vars[(i,j,k)]*TransCost[i][k].

Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49
  • Whoa! Great Logic! Should this dummy variable m[i][j] be declared as a decision variable as well? Since, its value will be decided by the solver... – Utkarsh Srivastava Sep 13 '20 at 20:20