gpt4 book ai didi

python - PuLP 优化

转载 作者:行者123 更新时间:2023-12-01 01:52:31 25 4
gpt4 key购买 nike

我正在尝试使用 PuLP 库创建一个程序,当您按下按钮时,它会解决线性问题并输出值。但我无法让它发挥作用。它只是写我的“输入更多值”并且不想解决。也许我对输入​​值有一些问题,但我不太确定。

这是我的代码:

from tkinter import*
from pulp import*

def problem(a_val, b_val, c_val, d_val, e_val, f_val, g_val):
prob=LpProblem("problem", LpMaximize)
x1=LpVariable("x1", lowBound=0)
x2=LpVariable("x2", lowBound=0)
x3=LpVariable("x3", lowBound=0)
prob+= a_val*x1 +b_val*x2 +c_val*x3,
prob+= d_val*x1 +e_val*x2 + f_val*x3 <= g_val,
prob.solve ()
print("status:", LpStatus[prob.status])
for v in prob.variables():
print (v.name, "=", v.varValue)
print("objective=%s$" % value(prob.objective))
root =Tk()
root.title("System")
root.geometry("1300x500+0+0")
a=Entry(Top, font=("arial", 10, "bold"), bd=8)
a.grid(row=1, column=1)

b=Entry(Top, font=("arial", 10, "bold"), bd=8)
b.grid(row=1, column=2)

c=Entry(Top, font=("arial", 10, "bold"), bd=8)
c.grid(row=1, column=3)

d=Entry(Top, font=("arial", 10, "bold"), bd=8)
d.grid(row=2, column=1)

e=Entry(Top, font=("arial", 10, "bold"), bd=8)
e.grid(row=2, column=2)

f=Entry(Top, font=("arial", 10, "bold"), bd=8)
f.grid(row=2, column=3)

g=Entry(Top, font=("arial", 10, "bold"), bd=8)
g.grid(row=3, column=1)

def inserter (value):
w.delete("0.0", "end")
w.insert("0.0", value)
def handler():
try:
g_val = float(g.get())
a_val = float(a.get())
b_val = float(b.get())
c_val = float(c.get())
d_val = float(d.get())
e_val = float(e.get())
f_val = float(f.get())
inserter(problem(a_val,b_val,c_val,d_val,e_val,f_val, g_val))
except ValueError:
inserter("Enter more values")

w=Text(Top, font=("arial", 10, "bold"), bd=6)
w.grid(row=4, column=1)

info6=Button(Top, font=("arial", 10,"bold"), text="Optimize", bd=8, command=handler)
info6.grid(row=4, column=0)

root.mainloop()

最佳答案

根据 PuLP 文档,您需要在函数语句末尾放置一个短字符串。

The variable prob now begins collecting problem data with the += operator. The objective function is logically entered first, with an important comma , at the end of the statement and a short string explaining what this objective function is:

# The objective function is added to 'prob' first
prob += 0.013*x1 + 0.008*x2, "Total Cost of Ingredients per can"

所以你需要:

prob+= a_val*x1 +b_val*x2 +c_val*x3, "whats this"
prob+= d_val*x1 +e_val*x2 + f_val*x3 <= g_val, "something here too"

此外,您的函数问题不会返回任何内容(无),并且您正尝试将其放入文本小部件中。您需要在函数内进行插入(或添加 return 语句)。例如:

def problem(a_val, b_val, c_val, d_val, e_val, f_val, g_val):

...

w.delete("0.0", "end") # Clear the Text widget
print("status:", LpStatus[prob.status])
for v in prob.variables():
print (v.name, "=", v.varValue)
print("objective=%s$" % value(prob.objective))
w.insert(END, str(v.name) + "=" + str(v.varValue) + '\n' ) # Insert data at the END (rather then at the beginning)
w.insert(END, "objective=%s$" % value(prob.objective) + '\n' )

如果您决定执行类似的操作(没有 return 语句),请确保删除自定义插入调用:

#inserter(problem(a_val,b_val,c_val,d_val,e_val,f_val, g_val))     #instead of this
problem(a_val,b_val,c_val,d_val,e_val,f_val, g_val) #do this

关于python - PuLP 优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50572512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com