8.6.8. Constr

class Constr

Represents a constraint in the model. The index of the constraint increases from 0. Each time a constraint is added to the model, the index is increased by 1. Constraints are usually expressed as an expression.

Properties

index

The index of the constraint

Methods

getAttr()

Obtain the attribute value associated with the constraint

sameAs()

Test whether the constraint is the same as another constraint

setAttr()

Set the attribute value associated with the constraint

getAttr(attrname)

Obtain the attribute value associated with the constraint.

Parameters

attrname – Attribute name

example:

m = Model()
x = m.addVar()
c = m.addConstr(2 * x <= 1)
print(c.rhs)
print(c.getAttr(MDO.Attr.RHS))

Note

Attribute can also be read and written directly through object attributes, in this case, the attribute name is case-insensitive

sameAs(constr)

Test whether the constraint is the same as another constraint.

Parameters

constr – Another constraint to be tested

example:

m = Model()
x = m.addVar()
c = m.addConstr(2 * x <= 1)
assert (c.sameAs(m.getConstrs()[0]))
setAttr(attrname, attrvalue)

Set the attribute value associated with the constraint.

Parameters
  • attrname – The name of the attribute.

  • attrvalue – The value of the attribute to be set.

example:

m = Model()
x = m.addVar()
c = m.addConstr(2 * x <= 1)
c.rhs = 2.0
c.setAttr(MDO.Attr.RHS, 2.0)

Note

Attribute can also be read and written directly through object attributes, in this case, the attribute name is case-insensitive