简体   繁体   中英

How to Formulate a Pulp Objective Function with a max operator

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'

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] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM