我正在使用Pyomo,我想进行一系列的优化,其中先前优化的一些结果将用于下一个。
AgOpt.py
import pyomo
import pyomo.opt
from pyomo.environ import *
import numpy as np
AvailLand = 330
AvailWat = 945
AlfWat = 4
AlmWat = 1.5
CornWat = 2.5
AlfMaxY = 7
AlmMaxY = 6.5
CornMaxY = 7
AlfPrice = 132
AlmPrice = 610
CornPrice = 300
AlfCostAcre = 681
AlmCostAcre = 3478
CornCostAcre = 610
N=[1,2,3]
model = ConcreteModel()
model.x = Var(N, within=NonNegativeReals)
model.Obj = Objective(expr=model.x[1]*(AlfMaxY*AlfPrice-AlfCostAcre)+\
model.x[2]*(AlmMaxY*AlmPrice-AlmCostAcre)+\
model.x[3]*(CornMaxY*CornPrice-CornCostAcre), sense=maximize)
model.con1=Constraint(expr=model.x[1]<=100.1)
model.con2=Constraint(expr=model.x[2]<=30.03)
model.con3=Constraint(expr=model.x[3]<=200.2)
model.con4=Constraint(expr=model.x[1]+model.x[2]+model.x[3]<=330)
model.dual=Suffix(direction=Suffix.IMPORT_EXPORT)
opt = SolverFactory("glpk")
results = opt.solve(model)
model.solutions.load_from(results)
results.write()
model.Obj.display()
model.x.display()
model.dual.display()
问题在于,当我解决代码(使用Spyder)一次时,第二次引发此错误:
“接口IPyomoTask已经定义”
我必须退出Spyder并再次进入。
有什么建议吗?
Python和anaconda版本:
Python 3.5.2 | Anaconda 4.2.0(64位)| (默认,2016年7月5日,11:41:13)[MSC v.1900 64 bit(AMD64)]
Traceback (most recent call last):
File "<ipython-input-2-f27cc52b2eaf>", line 1, in <module>
runfile('//fresno/users/escriva/Documents/Python Scripts/AgOpt.py', wdir='//fresno/users/escriva/Documents/Python Scripts')
File "C:\Users\escriva\AppData\Local\Continuum\Anaconda2\envs\py35\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "C:\Users\escriva\AppData\Local\Continuum\Anaconda2\envs\py35\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "//fresno/users/escriva/Documents/Python Scripts/AgOpt.py", line 9, in <module>
import pyomo.opt
File "c:\users\escriva\appdata\local\continuum\anaconda2\envs\py35\lib\site-packages\pyomo-4.2.10784-py3.5.egg\pyomo\opt\__init__.py", line 10, in <module>
from pyomo.util.plugin import PluginGlobals
File "c:\users\escriva\appdata\local\continuum\anaconda2\envs\py35\lib\site-packages\pyomo-4.2.10784-py3.5.egg\pyomo\util\__init__.py", line 10, in <module>
from pyomo.util._task import pyomo_api, PyomoAPIData, PyomoAPIFactory
File "c:\users\escriva\appdata\local\continuum\anaconda2\envs\py35\lib\site-packages\pyomo-4.2.10784-py3.5.egg\pyomo\util\_task.py", line 102, in <module>
class IPyomoTask(plugin.Interface):
File "C:\Users\escriva\AppData\Local\Continuum\Anaconda2\envs\py35\lib\site-packages\PyUtilib-5.2.3601-py3.5.egg\pyutilib\component\core\core.py", line 600, in __new__
raise PluginError("Interface %s has already been defined" % name)
PluginError: Interface IPyomoTask has already been defined
答案 0 :(得分:2)
已解决...我正在使用环境来运行python 3.5(因为我最初使用的是python 2.7)。所以我卸载了所有东西,我只使用python 3.5再次安装了anaconda,现在它可以正常工作。
答案 1 :(得分:0)
也许是对这个问题的额外了解,似乎您的Spyder安装试图访问PyUtillib的另一个来源。
这是在安装Pyomo的用户版本(带有conda install -c conda-forge pyomo and pyutillib
)然后移至开发人员版本(克隆git存储库)之后发生的。
要解决此问题,只需通过conda-forge
(conda remove -c conda-forge pyomo and pyutillib
)删除Anaconda管理的软件包,然后按照此处的安装说明进行操作:https://github.com/Pyomo/pyomo/issues/90#issuecomment-335051682