我无法获得整数形式的LpProblem答案

问题描述 投票:0回答:1

我正在研究“饮食问题”,并尝试以整数形式而不是连续的方式获得答案。我使用LpVaraible声明了问题中的变量,并将类别指定为整数。但是,我在运行代码后得到的答案是连续的形式。我想知道我在做什么错?请帮忙。

#Import the necessary libraries
import pandas as pd
from pulp import *
#Import the necessary liberaries
import pandas as pd
from pulp import *
#Create LpProblem 
prob = LpProblem("Simple Diet Problem",LpMinimize)
#Read the file
df = pd.read_csv("The Diet Problem.csv")
#Creat a list of food items
food_items = list(df['Foods'])
#Create a dictionary of cost for all foods
cost = dict(zip(food_items,df['price']))
#Create a dictionary of A for all foods
A = dict(zip(food_items,df['A']))
#Create a dictionary of C for all foods
C = dict(zip(food_items,df['C']))
#Create a dictionary of B1 for all foods
B1 = dict(zip(food_items,df['B1']))
#Create a dictionary of B2 for all foods
B2 = dict(zip(food_items,df['B2']))
I
#Create food variable with a lower bound = 0
food_vars = LpVariable.dicts("Food",food_items,lowBound=0, cat='Integer')
#Building the LpProblem by adding the main objective function
prob += lpSum([cost[i]*food_vars[i] for i in food_items])
=
#Add the constraints to the model
prob += lpSum([A[f] * food_vars[f] for f in food_items]) == 700
prob += lpSum([C[f] * food_vars[f] for f in food_items]) ==700
prob += lpSum([B1[f] * food_vars[f] for f in food_items]) == 700
prob += lpSum([B2[f] * food_vars[f] for f in food_items]) ==700
#Solve the problem and display the status
prob.solve()
print ("Status:", LpStatus[prob.status])
Status: Infeasible
#Display the full solution
for v in prob.variables():
    if v.varValue>=0:
        print(v.name, "=", v.varValue)
Food_Beef = 0.0
Food_CHK = 19.543147
Food_FISH = 0.0
Food_HAM = 0.0
Food_MCH = 16.345178
Food_MTL = 4.2639594
Food_SPG = 0.0
Food_TUR = 0.0
python optimization pulp
1个回答
0
投票

状态:不可行

这告诉您,由于您在案例中设置的右侧常量,因此未找到可行的解决方案。因此,您不应考虑变量的值。

© www.soinside.com 2019 - 2024. All rights reserved.