8.6.11. MConstr

class MConstr

代表多个约束组成的多维数组。 由 Model.addConstrModel.addMConstr 返回。

属性

ndim

获取MConstr的维度个数

shape

获取MConstr的shape

size

获取MConstr包含的约束个数

方法

getAttr()

获取关联的attribute的值

item()

获取当前MConstr唯一包含的约束

setAttr()

设置关联的attribute的值

tolist()

返回一个数组,包含当前MConstr中所有的约束

getAttr(attrname)

获取关联的attribute的值

Parameters

attrname – 要获取值的attribute名称

example:

m = Model()
mat = m.addMVar((2, 2))
c = m.addMConstr([[1, 2], [3, 4]], mat[0], '<', [1, 2])
c.constrname = ["c0", "c1"]
print(c.constrname)
print(c.getAttr(MDO.Attr.ConstrName))

Note

Attribute的读写也可以直接通过对象属性读写完,这种情况下,属性名称大小写不敏感

item()

获取当前MConstr唯一包含的约束

example:

mat = m.addMVar((2, 2))
c = m.addMConstr([[1, 2], [3, 4]], mat[0], '<', [1, 2])
first = c[0]
print(type(first))
print(type(first.item()))

Note

如果当前MConstr包含不止一个约束将抛出异常

setAttr(attrname, attrvalues)

设置关联的attribute的值

Parameters
  • attrname – 要设置的attribute的名称

  • attrvalues – 要设置的attribute的新值。可以是标量或数组

example:

m = Model()
mat = m.addMVar((2, 2))
c = m.addMConstr([[1, 2], [3, 4]], mat[0], '<', [1, 2])
c.constrname = ["c0", "c1"]
c.setAttr(MDO.Attr.RHS, 5.0)

Note

Attribute的读写也可以直接通过对象属性读写完,这种情况下,属性名称大小写不敏感

tolist()

返回一个数组,包含当前MConstr中所有的约束。

example:

mat = m.addMVar((2, 2))
c = self.m.addMConstr([[1, 2], [3, 4]], mat[0], '<', [1, 2])
print(c.tolist())