我试图使用PyOMO解决约束混合整数非线性优化问题。具体来说,我试图找到齿轮直径和齿数,以满足两个给定的齿轮比。关于如何使用Set()
和Var()
,我真的非常感兴趣。我一直在阅读文档,但它并不是超级明确的集合实际上是什么!它是一个索引,我可以用来访问问题的类似分组部分?这是我的代码:(Python 3.5)
from pyomo.environ import *
from pyomo.opt import SolverFactory
import numpy as np
# Define Forward and Reverse Gear Ratios
fwd_ratio = 4.3
rev_ratio = 9.1
D_guess = [4.5, 11.5, 6.0, 10.0, 4.5, 2.5, 2.25, 9.0]
N_guess = [18, 46, 24, 40, 18, 20, 18, 72]
idx = np.arange(0,8)
print(idx)
model = AbstractModel()
# Declare Model Sets??? I tried this as first argument to Var(), didn't work
#model.Didx = Set(D_guess)
#model.Nidx = Set(N_guess)
# Declare Model Variables
model.D = Var(D_guess, within='PositiveReals', bounds=(1.0,None))
model.N = Var(N_guess, within='PositiveInteger', bounds=(18,None))
# Declare Objective Functions
def obj_funcD(model):
F1 = (model.D[1]/model.D[0])*(model.D[3]/model.D[2]) - fwd_ratio
F2 = (model.D[1]/model.D[4])*(model.D[6]/model.D[5])*(model.D[7]/model.D[6]) - rev_ratio
return F1 + F2
def obj_funcN(model):
F1 = (model.N[1]/model.N[0])*(model.N[3]/model.N[2]) - fwd_ratio
F2 = (model.N[1]/model.N[4])*(model.N[6]/model.N[5])*(model.N[7]/model.N[6]) - rev_ratio
return F1 + F2
# Declare Constraint
def con_func1(model):
return model.D[1]/model.D[0] == model.N[1]/model.N[0]
def con_func2(model):
return model.D[3]/model.D[2] == model.N[3]/model.N[3]
def con_func3(model):
return model.D[1]/model.D[4] == model.N[1]/model.N[4]
def con_func4(model):
return model.D[6]/model.D[5] == model.N[6]/model.N[5]
def con_func5(model):
return model.D[7]/model.D[6] == model.N[7]/model.N[6]
# Create Constraint List
model.c1 = Constraint(rule=con_func1)
model.c2 = Constraint(rule=con_func2)
model.c3 = Constraint(rule=con_func3)
model.c4 = Constraint(rule=con_func4)
model.c5 = Constraint(rule=con_func5)
# Create Objectives
model.obj1 = Objective(rule=obj_funcD,sense='minimize')
model.obj2 = Objective(rule=obj_funcN,sense='minimize')
# Solve the Problem?
opt = SolverFactory('glpk')
instance = model.create_instance()
results = opt.solve(instance)
此代码出现以下错误:
WARNING: Element 4.5 already exists in set D_index; no action taken.
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pyomo/core/base/PyomoModel.py", line 920, in _initialize_component
ERROR: Constructing component 'D' from data=None failed:
declaration.construct(data)
ValueError: PositiveReals is not a valid domain. Variable domains must be an instance of one of (<class 'pyomo.core.base.set_types.RealSet' at 0x1004bee98>, <class 'pyomo.core.base.set_types.IntegerSet' at 0x1004f2558>, <class 'pyomo.core.base.set_types.BooleanSet' at 0x1004f28f8>), or an object that declares a method for bounds (like a Pyomo Set). Examples: NonNegativeReals, Integers, Binary
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pyomo/core/base/var.py", line 573, in construct
component=None)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pyomo/core/base/var.py", line 299, in __init__
"Integers, Binary" % (domain, (RealSet, IntegerSet, BooleanSet)))
ValueError: PositiveReals is not a valid d
我还尝试使用RangeSet()
并将关联的Set作为Var()
的第一个参数传递,但这也没有做任何事情!我知道我错过了一些非常明显的东西,但我现在已经盯着屏幕看了4个小时,我正在寻求你的帮助!感谢
答案 0 :(得分:1)
将within='PositiveReals'
更改为within=PositiveReals
等
within
(或domain
)关键字应分配一个从pyomo.environ
导入的设置域对象。不应该为它们分配字符串。