0

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?

2
  • first you should show FULL error message - don't expect that we will run code to see problem. Besides code can work correctly on our computers. Full code should show you in which line you have problem and you could use print() to see values in variables in this line. It should help to see when you get wrong value in variable and which variable makes problem. Commented May 2, 2022 at 2:17
  • Added full error message, Thank you! Commented May 2, 2022 at 2:38

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.