gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 05:08:46 25 4
gpt4 key购买 nike

我正在尝试使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com