参考
(函数来自 pyomo.core.base.reference)
- pyomo.core.base.reference.Reference(reference, ctype=NOTSET)[source]
创建一个引用其他组件的组件
Reference生成一个引用组件;即一个不包含数据但引用存储在由组件切片定义的其他组件中的数据的索引组件。ctype 参数设置生成的索引组件的Component.type()。如果未设置 ctype 参数并且切片(在构建时)识别的所有数据共享一个共同的Component.type(),则假定为该类型。如果 ctype 参数为None或数据具有多个 ctype,则生成的索引组件将具有IndexedComponent的 ctype。如果组件切片中的通配符索引对于切片识别的所有数据都引用相同的
Set对象,那么生成的索引组件将由这些集合的乘积索引。然而,如果所有数据不共享共同的集合对象,或者多维集合中只有一部分索引作为通配符出现,那么生成的索引组件将由包含切片_ReferenceSet的SetOf索引。- Parameters:
参考 (
IndexedComponent_slice) – 定义要包含在参考组件中的数据的组件切片ctype (
type[可选]) – 用于创建结果索引组件的类型。如果未指定,将使用数据的ctype(如果所有数据共享一个共同的ctype)。如果找到多个数据ctype或类型为None,则将使用IndexedComponent。
示例
>>> from pyomo.environ import * >>> m = ConcreteModel() >>> @m.Block([1,2],[3,4]) ... def b(b,i,j): ... b.x = Var(bounds=(i,j)) ... >>> m.r1 = Reference(m.b[:,:].x) >>> m.r1.pprint() r1 : Size=4, Index={1, 2}*{3, 4}, ReferenceTo=b[:, :].x Key : Lower : Value : Upper : Fixed : Stale : Domain (1, 3) : 1 : None : 3 : False : True : Reals (1, 4) : 1 : None : 4 : False : True : Reals (2, 3) : 2 : None : 3 : False : True : Reals (2, 4) : 2 : None : 4 : False : True : Reals
参考组件也可以引用原始数据的子集:
>>> m.r2 = Reference(m.b[:,3].x) >>> m.r2.pprint() r2 : Size=2, Index={1, 2}, ReferenceTo=b[:, 3].x Key : Lower : Value : Upper : Fixed : Stale : Domain 1 : 1 : None : 3 : False : True : Reals 2 : 2 : None : 3 : False : True : Reals
参考组件可能在模型层次结构的多个级别上具有通配符:
>>> m = ConcreteModel() >>> @m.Block([1,2]) ... def b(b,i): ... b.x = Var([3,4], bounds=(i,None)) ... >>> m.r3 = Reference(m.b[:].x[:]) >>> m.r3.pprint() r3 : Size=4, Index=ReferenceSet(b[:].x[:]), ReferenceTo=b[:].x[:] Key : Lower : Value : Upper : Fixed : Stale : Domain (1, 3) : 1 : None : None : False : True : Reals (1, 4) : 1 : None : None : False : True : Reals (2, 3) : 2 : None : None : False : True : Reals (2, 4) : 2 : None : None : False : True : Reals
生成的引用组件可以像任何其他组件一样使用。对存储数据的更改将反映在原始对象中:
>>> m.r3[1,4] = 10 >>> m.b[1].x.pprint() x : Size=2, Index={3, 4} Key : Lower : Value : Upper : Fixed : Stale : Domain 3 : 1 : None : None : False : True : Reals 4 : 1 : 10 : None : False : False : Reals