简体   繁体   中英

Python: Setting the objective function in pulp

Im trying to set the objective function, which is to maximize the sales,

df.index = ['City 1', 'City 2', 'City 3', 'City 4', 'City 5', 'City 6', 'City 7',
            'City 8', 'City 9', 'City 10', 'City 11', 'City 12', 'City 13']

df['Sales'] = [372.0, 404.0, 319.0, 212.0, 206.0, 287.0, 181.0, 483.0, 376.0, 269.0, 
               354.0, 278.0, 333.0]

df['Cost'] = [178.56, 311.08, 102.08, 116.60, 160.68, 192.29, 128.51, 
              236.67, 251.92, 78.01, 233.64, 100.08, 183.15]

CITY_SALES = {k:v for k,v in zip(df.index, df['Sales'])}
CITY_COST = {k:v for k,v in zip(df.index, df['Cost'])}

from pulp import *

PROB = LpProblem('MAX_PROB', LpMaximize)
CITY_VARS = LpVariable.dicts('city', df.index, lowBound=0, cat=0) #Decision Vars
PROB += lpSum(CITY_COST[c] * CITY_VARS[c] for c in df.index) #Objective Function

print(PROB)

Output:

> KeyError                                  Traceback (most recent call last)
<ipython-input-55-ecd06f0a5060> in <module>()
     17 PROB += lpSum(CITY_COST[c] * CITY_VARS[c] for c in df.index) #Objective Function
     18 
---> 19 print(PROB)

/usr/local/lib/python3.7/dist-packages/pulp/pulp.py in __repr__(self)
   1388         s += "VARIABLES\n"
   1389         for v in self.variables():
-> 1390             s += v.asCplexLpVariable() + " " + const.LpCategories[v.cat] + "\n"
   1391         return s
   1392 

KeyError: 0

I am expecting to see some summary, But I get a KeyError instead, Where did I get this wrong? How do I make the objective function work?

In your variable declaration, you are declaring cat=0 ?? I think that is causing some internal catastrophe in pulp

The only valid options are Continuous, Integer, Binary

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