- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 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/
我是 python 和优化方面的新手。我遇到了一些错误,请帮助我解决它。我尝试在运行 Anaconda 3 的 PyCharm 中运行下面提到的代码 from pulp import * x = Lp
我有以下由 PuLP 生成的 LP 文件: \* copynumber *\ Minimize OBJ: PenaltyTree_48 Subject To _C1: - A_0 + A_3 - ov
我有一个 python PuLP 线性程序,它可以最大限度地降低成本。在没有办法降低成本的退化情况下,我希望它返回固定成本。然而,在没有变量的情况下, PuLP 似乎添加了一个 __dummy 变量,
我是 Pulp 的新手,因此在尝试进行条件约束时遇到了问题。我制作了一个梦幻足球优化器,可以选择 9 名球员的最佳选择,我的求解器目前完全适用于职位限制、工资限制等。 我需要添加的最后一件事是一个约束
我对 PuLP 完全陌生,想知道是否需要优化以下内容: x = pulp.LpVariable.dicts("Volume", range(0, 7), cat='Binary') 只要有一个 0,就
我正在尝试在 puLP (Python) 中求解 MILP,但不断收到以下错误: Traceback (most recent call last): File "main_lp.py", lin
我正在与一位合作者合作开展某个涉及线性规划的优化项目。我们都使用硬币或分支切割求解器来解决这个问题。我使用基于 Python 的 PuLP 包构建 .LP 文件。我不完全确定合作者如何创建他们的 .L
我希望使用 PULP 在 Python 中设置约束检查。假设我有变量 A1,..,Xn 和约束 (AffineExpression) A1X1 + ... + AnXn <= B,其中 A1,..,A
我有一个具有以下值的 Pandas 数据框: Name Age City Points 1 John 24 CHI 35 2 Mary
我正在尝试使用 PuLP 库创建一个程序,当您按下按钮时,它会解决线性问题并输出值。但我无法让它发挥作用。它只是写我的“输入更多值”并且不想解决。也许我对输入值有一些问题,但我不太确定。 这是我的
在 PuLP 的传输优化问题中: from pulp import * Warehouses = ["A","B"] # Creates a dictionary for the number of
在开始一个更大的问题之前,我试图做以下简单的示例优化问题。代码: from pulp import * x = LpVariable("x", 0, 3) y = LpVariable("y", 0,
我正在使用线性规划解决一个特定问题,并试图让自己熟悉 PuLP。我的问题是我的几个约束只包含一些决策变量,我正试图找到一种有效的方法来选择它们。 我的意思是: 我为所有决策变量设置了一个列表。 inv
我正在尝试使用 PuLP 来优化系统,从而最大限度地降低成本。我正在使用多个 If,问题是它总是满足第一个条件。这是我的代码。我希望有人能帮助我,因为我才刚刚开始学习这门语言。 import nump
我正在尝试运行一个优化问题,但我似乎无法解决。我是Python菜鸟。 我有一个包含 8760 个数字 (0 - 1) 的数据框。我需要将该数组中的每一行乘以一个因子,然后对该数组求和。该总和应等于 x
我正在尝试使用 Pulp Solver 将元素包装到卡车中,当元素数量较少(即 <25)时,它工作得很好,但当我将数量增加到 30-32 时,它需要很长时间才能解决。 这是 PuLP 求解器的代码:
我会尽量让我的问题简短。如果您需要任何进一步的信息,请告诉我。 我有一个 MIP,使用 PuLP 包在 Python 中实现。 (大约 100 个变量和约束)问题的数学公式来自一篇研究论文。本文还包括
在 Python PuLP 中,线性规划约束可以转化为弹性子问题。 http://www.coin-or.org/PuLP/pulp.html?highlight=lpsum#elastic-cons
在多年使用 Matlab 之后,我对 Python 还是很陌生。我正在尝试使用 Pulp 来设置整数线性程序。 给定一个数字数组: {P[i]:i=1...N} 我想最大化: sum( x_i P_i
我正在使用 python 作为编程语言并实现将相似长度分组在一起的约束以满足线性规划。引用下图代码 import pulp from itertools import product import p
我是一名优秀的程序员,十分优秀!