【问题标题】:PuLP optimization纸浆优化
【发布时间】:2018-05-29 06:04:31
【问题描述】:

我正在尝试使用 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()

【问题讨论】:

标签: python tkinter pulp


【解决方案1】:

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

变量 prob 现在开始使用 += 收集问题数据 操作员。首先在逻辑上输入目标函数, 重要的逗号,在语句的末尾和一个短字符串 解释这个目标函数是什么:

# 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"

此外,您的函数 problem 不返回任何内容(无),而您正试图将其放入 Text 小部件中。您需要在函数内部进行插入(或添加 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 2020-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多