纸浆求解器错误,无法使用.solve()

发布于 2025-01-19 22:44:14 字数 1703 浏览 0 评论 0原文

我在使用 .solve() 解决 PuLP 问题时遇到困难。我收到错误:

PulpSolverError:Pulp:尝试执行时出错,使用 msg=True 获取更多详细信息cbc

我不知道如何纠正它。任何建议表示赞赏。

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()

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文