我正在使用 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 位 (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