8.6.12. MConstr

class MConstr

A multidimensional array consisting of constraints. Returned by Model.addConstr or Model.addMConstr .

Properties

ndim

Number of MConstr dimensions

shape

Shape of the MConstr

size

Number of constraints contained in the MConstr

ndim

Number of MConstr dimensions

shape

Shape of the MConstr

size

Number of constraints contained in the MConstr

Methods

getAttr()

Obtain the value of the associated attribute

item()

Get the unique constraint contained in the current MConstr

setAttr()

Set the value of the associated attribute

tolist()

Return an array containing all constraints in the current MConstr

getAttr(attrname)

Obtain the value of the associated attribute.

Parameters

attrname – The name of the attribute to obtain the value.

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 can also be read and written directly through object attributes, in this case, the attribute name is case-insensitive

item()

Get the unique constraint contained in the current 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

An exception is thrown if the current MConstr contains more than one constraint.

setAttr(attrname, attrvalues)

Set the value of the associated attribute.

Parameters
  • attrname – The name of the attribute to be set.

  • attrvalues – The new value of the attribute to be set. Can be scalar or array

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 can also be read and written directly through object attributes, in this case, the attribute name is case-insensitive

tolist()

Return an array containing all constraints in the current MConstr.

example:

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