python - Pyomo 值错误 : PositiveReals is not a valid domain

标签 python nonlinear-optimization pyomo valueerror mixed-integer-programming

我正在尝试使用 PyOMO 解决约束混合整数非线性优化问题。具体来说,我试图找到满足两个给定齿轮比的齿轮直径和齿数。我真的很困惑如何使用 Set()Var()。我一直在阅读文档,但对于 Set 实际上是什么并不是非常清楚!它是我可以用来访问问题的类似分组部分的索引吗?这是我的代码:(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 个小时了,我正在寻求你的帮助!谢谢

最佳答案

within='PositiveReals' 更改为 within=PositiveReals

within(或domain)关键字应分配给从pyomo.environ 导入的集合域对象之一。不应为它们分配字符串。

关于python - Pyomo 值错误 : PositiveReals is not a valid domain,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43623241/

相关文章:

python - 使用 Dask 删除 Dataframe 中高度相关的成对特征?

r - R 中 optim() 的优化(L-BFGS-B 需要 'fn' 的有限值)

r - 寻找曲线以匹配数据

mathematical-optimization - 建议 ILP 求解器的下限

parameters - Pyomo:如何更新现有模型实例的约束定义?

python - 访问 pyomo 约束中出现的所有变量

python - 安装脚本退出并出现错误 : command 'x86_64-linux-gnu-gcc' failed with exit status 1

python - 正则表达式:如何在否定前瞻之前引入非贪婪运算符

julia - Julia 非线性最小二乘包中的 Levenberg-Marquardt

python - 如何将变量传递给mapnik数据源类?