1 year ago
#383714
Jessica Farr
Pulp Solver Error, unable to use .solve()
I'm having difficulty with .solve() for my PuLP problem. I'm receiving the error:
PulpSolverError: Pulp: Error while trying to execute, use msg=True for more detailscbc
I'm not sure how to correct it. Any advice is appreciated.
import pulp
import pandas as pd
solver_list = pulp.listSolvers()
#read in excel file
df = pd.read_excel (r'/Users/***/Desktop/diet.xls', sheet_name='Sheet1')
print (df)
#nested list from values
diet_data = df.values.tolist()
#list of foods
foods = [x[0] for x in diet_data]
#meats =
# Make dictionaries (key / value pairs) from the lists
# Key = food name
# Value = value
price = {x[0]:x[1] for x in diet_data}
cals = {x[0]:x[3] for x in diet_data}
cholesterol = {x[0]:x[4] for x in diet_data}
fat = {x[0]:x[5] for x in diet_data}
sodium = {x[0]:x[6] for x in diet_data}
carbs = {x[0]:x[7] for x in diet_data}
fiber = {x[0]:x[8] for x in diet_data}
protein = {x[0]:x[9] for x in diet_data}
vitA = {x[0]:x[10] for x in diet_data}
vitC = {x[0]:x[11] for x in diet_data}
calcium= {x[0]:x[12] for x in diet_data}
iron = {x[0]:x[13] for x in diet_data}
#creating optimization problem
prob = pulp.LpProblem("DietProblem", pulp.LpMinimize)
# Creating the variables
lp_variables = pulp.LpVariable.dicts( "Amounts", foods, 0 )
# Creating objective function
# objective function
prob += pulp.lpSum(price[i] * lp_variables[i] for i in foods)
# min 1500 cals
prob += pulp.lpSum(cals[i] * lp_variables[i] for i in foods) >= 1500
# max 2500 cals
prob += pulp.lpSum(cals[i] * lp_variables[i] for i in foods) <= 2500
# min 30g chol
prob += pulp.lpSum(cholesterol[i] * lp_variables[i] for i in foods) >= 30
# max 240g chol
prob += pulp.lpSum(cholesterol[i] * lp_variables[i] for i in foods) <= 240
solution = prob.solve()
python
pulp
0 Answers
Your Answer