Pyomo:如何使用二维数组或集合
Pyomo: how to work with 2D arrays or Sets
我有一个矩阵,每个单元格承载不同的整数状态。我只是想通过特定规则约束每个单元格,但 pyomo
不理解数据类型。
这是我的
import pyomo.environ as pe
def assign_server_locations(model, i, j):
return model[i, j] >= 0
server_map = [[-1,1],[2,-2],[-3,3]]
model = pe.ConcreteModel()
model.server_x = range(2)
model.server_y = range(3)
model.server_map = pe.Set(dimen = 2, initialize = server_map)
model.c1 = pe.Constraint(model.server_map, model.server_y, model.server_x, rule=assign_server_locations)
创建约束的错误是
TypeError: assign_server_locations() takes 3 positional arguments but 5 were given
如何才能:
- 正确创建二维矩阵
- 正确传递给规则函数的索引(我预计每次调用总共有 3 个参数:模型和单个 x-y 索引)
提前致谢!
欢迎来到本站...
你在那里遇到了一些问题......简要地说:
约束结构令人作呕,因为你给它传递了 5 个值(模型,server.map
的 2 个值,以及 x 和 y。)而你只捕获了 3
在你的约束中,你没有变量! model[i, j]
将导致错误。您需要创建一个正确索引的变量。
您正在创建一些模型元素(例如 model.x
和 model.y
作为 non-pyomo 元素。我建议您将它们保留为 pyomo 元素,这有助于故障排除。
这是一个示例,我认为它可以做很多与您正在寻找的类似的事情。
import pyomo.environ as pe
matrix_dims = (4, 2) # row, col
limit_positions = { (2, 1): 2,
(1, 0): 1,
(3, 1): 2}
model = pe.ConcreteModel()
# SETS
model.server_x = pe.Set(initialize=range(matrix_dims[0])) # may come in handy to total by rows or such. Also, make it a pyomo Set
model.server_y = pe.Set(initialize=range(matrix_dims[1]))
model.matrix = pe.Set(initialize=model.server_x*model.server_y)
model.low_positions = pe.Set(within=model.matrix, initialize=limit_positions.keys()) # use of "within" will error-check
# PARAMS
model.limit_positions = pe.Param(model.low_positions, initialize=limit_positions)
# VARS
model.X = pe.Var(model.matrix, domain=pe.NonNegativeReals)
# CONSTRAINTS
# limit all positions to max of 5
def limit_all(model, i, j):
return model.X[i, j] <= 5
model.C1 = pe.Constraint(model.matrix, rule=limit_all)
# limit the low-positions to their limit...
def limit_lows(model, i, j):
return model.X[i, j] <= model.limit_positions[i, j]
model.C2 = pe.Constraint(model.low_positions, rule=limit_lows)
model.pprint()
产量:
4 Set Declarations
low_positions : Size=1, Index=None, Ordered=Insertion
Key : Dimen : Domain : Size : Members
None : 2 : matrix : 3 : {(2, 1), (1, 0), (3, 1)}
matrix : Size=1, Index=None, Ordered=Insertion
Key : Dimen : Domain : Size : Members
None : 2 : Any : 8 : {(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1), (3, 0), (3, 1)}
server_x : Size=1, Index=None, Ordered=Insertion
Key : Dimen : Domain : Size : Members
None : 1 : Any : 4 : {0, 1, 2, 3}
server_y : Size=1, Index=None, Ordered=Insertion
Key : Dimen : Domain : Size : Members
None : 1 : Any : 2 : {0, 1}
1 Param Declarations
limit_positions : Size=3, Index=low_positions, Domain=Any, Default=None, Mutable=False
Key : Value
(1, 0) : 1
(2, 1) : 2
(3, 1) : 2
1 Var Declarations
X : Size=8, Index=matrix
Key : Lower : Value : Upper : Fixed : Stale : Domain
(0, 0) : 0 : None : None : False : True : NonNegativeReals
(0, 1) : 0 : None : None : False : True : NonNegativeReals
(1, 0) : 0 : None : None : False : True : NonNegativeReals
(1, 1) : 0 : None : None : False : True : NonNegativeReals
(2, 0) : 0 : None : None : False : True : NonNegativeReals
(2, 1) : 0 : None : None : False : True : NonNegativeReals
(3, 0) : 0 : None : None : False : True : NonNegativeReals
(3, 1) : 0 : None : None : False : True : NonNegativeReals
2 Constraint Declarations
C1 : Size=8, Index=matrix, Active=True
Key : Lower : Body : Upper : Active
(0, 0) : -Inf : X[0,0] : 5.0 : True
(0, 1) : -Inf : X[0,1] : 5.0 : True
(1, 0) : -Inf : X[1,0] : 5.0 : True
(1, 1) : -Inf : X[1,1] : 5.0 : True
(2, 0) : -Inf : X[2,0] : 5.0 : True
(2, 1) : -Inf : X[2,1] : 5.0 : True
(3, 0) : -Inf : X[3,0] : 5.0 : True
(3, 1) : -Inf : X[3,1] : 5.0 : True
C2 : Size=3, Index=low_positions, Active=True
Key : Lower : Body : Upper : Active
(1, 0) : -Inf : X[1,0] : 1.0 : True
(2, 1) : -Inf : X[2,1] : 2.0 : True
(3, 1) : -Inf : X[3,1] : 2.0 : True
8 Declarations: server_x server_y matrix low_positions limit_positions X C1 C2
我有一个矩阵,每个单元格承载不同的整数状态。我只是想通过特定规则约束每个单元格,但 pyomo
不理解数据类型。
这是我的
import pyomo.environ as pe
def assign_server_locations(model, i, j):
return model[i, j] >= 0
server_map = [[-1,1],[2,-2],[-3,3]]
model = pe.ConcreteModel()
model.server_x = range(2)
model.server_y = range(3)
model.server_map = pe.Set(dimen = 2, initialize = server_map)
model.c1 = pe.Constraint(model.server_map, model.server_y, model.server_x, rule=assign_server_locations)
创建约束的错误是
TypeError: assign_server_locations() takes 3 positional arguments but 5 were given
如何才能:
- 正确创建二维矩阵
- 正确传递给规则函数的索引(我预计每次调用总共有 3 个参数:模型和单个 x-y 索引)
提前致谢!
欢迎来到本站...
你在那里遇到了一些问题......简要地说:
约束结构令人作呕,因为你给它传递了 5 个值(模型,
server.map
的 2 个值,以及 x 和 y。)而你只捕获了 3在你的约束中,你没有变量!
model[i, j]
将导致错误。您需要创建一个正确索引的变量。您正在创建一些模型元素(例如
model.x
和model.y
作为 non-pyomo 元素。我建议您将它们保留为 pyomo 元素,这有助于故障排除。
这是一个示例,我认为它可以做很多与您正在寻找的类似的事情。
import pyomo.environ as pe
matrix_dims = (4, 2) # row, col
limit_positions = { (2, 1): 2,
(1, 0): 1,
(3, 1): 2}
model = pe.ConcreteModel()
# SETS
model.server_x = pe.Set(initialize=range(matrix_dims[0])) # may come in handy to total by rows or such. Also, make it a pyomo Set
model.server_y = pe.Set(initialize=range(matrix_dims[1]))
model.matrix = pe.Set(initialize=model.server_x*model.server_y)
model.low_positions = pe.Set(within=model.matrix, initialize=limit_positions.keys()) # use of "within" will error-check
# PARAMS
model.limit_positions = pe.Param(model.low_positions, initialize=limit_positions)
# VARS
model.X = pe.Var(model.matrix, domain=pe.NonNegativeReals)
# CONSTRAINTS
# limit all positions to max of 5
def limit_all(model, i, j):
return model.X[i, j] <= 5
model.C1 = pe.Constraint(model.matrix, rule=limit_all)
# limit the low-positions to their limit...
def limit_lows(model, i, j):
return model.X[i, j] <= model.limit_positions[i, j]
model.C2 = pe.Constraint(model.low_positions, rule=limit_lows)
model.pprint()
产量:
4 Set Declarations
low_positions : Size=1, Index=None, Ordered=Insertion
Key : Dimen : Domain : Size : Members
None : 2 : matrix : 3 : {(2, 1), (1, 0), (3, 1)}
matrix : Size=1, Index=None, Ordered=Insertion
Key : Dimen : Domain : Size : Members
None : 2 : Any : 8 : {(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1), (3, 0), (3, 1)}
server_x : Size=1, Index=None, Ordered=Insertion
Key : Dimen : Domain : Size : Members
None : 1 : Any : 4 : {0, 1, 2, 3}
server_y : Size=1, Index=None, Ordered=Insertion
Key : Dimen : Domain : Size : Members
None : 1 : Any : 2 : {0, 1}
1 Param Declarations
limit_positions : Size=3, Index=low_positions, Domain=Any, Default=None, Mutable=False
Key : Value
(1, 0) : 1
(2, 1) : 2
(3, 1) : 2
1 Var Declarations
X : Size=8, Index=matrix
Key : Lower : Value : Upper : Fixed : Stale : Domain
(0, 0) : 0 : None : None : False : True : NonNegativeReals
(0, 1) : 0 : None : None : False : True : NonNegativeReals
(1, 0) : 0 : None : None : False : True : NonNegativeReals
(1, 1) : 0 : None : None : False : True : NonNegativeReals
(2, 0) : 0 : None : None : False : True : NonNegativeReals
(2, 1) : 0 : None : None : False : True : NonNegativeReals
(3, 0) : 0 : None : None : False : True : NonNegativeReals
(3, 1) : 0 : None : None : False : True : NonNegativeReals
2 Constraint Declarations
C1 : Size=8, Index=matrix, Active=True
Key : Lower : Body : Upper : Active
(0, 0) : -Inf : X[0,0] : 5.0 : True
(0, 1) : -Inf : X[0,1] : 5.0 : True
(1, 0) : -Inf : X[1,0] : 5.0 : True
(1, 1) : -Inf : X[1,1] : 5.0 : True
(2, 0) : -Inf : X[2,0] : 5.0 : True
(2, 1) : -Inf : X[2,1] : 5.0 : True
(3, 0) : -Inf : X[3,0] : 5.0 : True
(3, 1) : -Inf : X[3,1] : 5.0 : True
C2 : Size=3, Index=low_positions, Active=True
Key : Lower : Body : Upper : Active
(1, 0) : -Inf : X[1,0] : 1.0 : True
(2, 1) : -Inf : X[2,1] : 2.0 : True
(3, 1) : -Inf : X[3,1] : 2.0 : True
8 Declarations: server_x server_y matrix low_positions limit_positions X C1 C2