无法理解如何 运行 pyomo 脚本

Can't understand how to run pyomo scripts

您好,我正在尝试使用 Pyomo 来 运行 优化问题。我找到了很多关于如何构建模型等的教程和视频。但是我无法理解如何实际 运行 脚本。

这是本教程中的一段简单代码 -> video

这是第 7 分钟的代码

from pyomo.environ import *

# creating a model object
model = ConcreteModel()
# declaring the decision variables
model.x = Var(within=NonNegativeReals)
# declaring the objective function
model.maximizeZ = Objective(expr=model.x, sense=maximize)
# declaring the constraints
model.Constraint1=Constraint (expr=model.x <=100) #cant be more than 100
print("helloworld")

然后,如果我 运行 来自 Spyder,由于 print 语句,我只会得到“helloworld”作为输出。然后视频中的人打开命令提示符并运行s以下命令以解决使用pyomo的优化问题:

pyomo solve --solver=glpk hello.py

我的文件名为 hello.py,我的提示与 hello.py 文件位于同一目录中。但这对我的电脑没有任何作用。我尝试使用常规 windows 提示符和 Anaconda 提示符。 Windows 说 pyomo 不存在,Anaconda 什么也没说也没做。这是我在命令提示符中 运行 的行。

有人知道我应该做什么不同的事情吗?

使用 pyomo 模型的主要方法有 2 种。您可以尝试使用命令行(如上所示)和 pyomo 可执行命令。另一种方法(我认为更可取)是将所有求解命令嵌入到脚本中,然后 运行 它就像任何旧的 python 程序一样。如果您查看我(或其他人)使用 pyomo 标签发布的一些答案,您会看到后一种方法的许多示例。

第一个问题(无论哪种方法):您是否安装了求解器?那里有几个免费赠品。有些比其他的更容易安装。

之后,如果您在脚本中求解,您将看到如下行:

solver=SoverFactory('glpk')  # or other legal solver name that you have installed
results = solver.solve(model)
print(results)
model.display()

然后您可以 运行 与来自命令行或 IDE.

的任何 python 文件一样