sasoptpy.Constraint

class Constraint(**kwargs)[source]

Bases: sasoptpy.core.expression.Expression

Creates a linear or quadratic constraint for optimization models

Constraints should be created by adding logical relations to Expression objects.

Parameters
expExpression

A logical expression that forms the constraint

directionstring, optional

Direction of the logical expression

Possible values are

  • E for equality (=) constraints

  • L for less than or euqal to (<=) constraints

  • G for greater than or equal to (>=) constraints

namestring, optional

Name of the constraint object

crangefloat, optional

Range for ranged constraints

Notes

  • A constraint can be generated in two different ways:

    • Using the sasoptpy.Model.add_constraint() method

      >>> m = so.Model(name='m')
      >>> c1 = m.add_constraint(3 * x - 5 * y <= 10, name='c1')
      >>> print(repr(c1))
      sasoptpy.Constraint( -  5.0 * y  +  3.0 * x  <=  10, name='c1')
      
    • Using the constructor

      >>> c1 = sasoptpy.Constraint(3 * x - 5 * y <= 10, name='c1')
      >>> print(repr(c1))
      sasoptpy.Constraint( -  5.0 * y  +  3.0 * x  <=  10, name='c1')
      
  • The same constraint can be included into other models using the Model.include() method.

Examples

>>> x = so.Variable(name='x')
>>> y = so.Variable(name='y')
>>> c1 = so.Constraint( 3 * x - 5 * y <= 10, name='c1')
>>> print(repr(c1))
sasoptpy.Constraint( -  5.0 * y  +  3.0 * x  <=  10, name='c1')
>>> c2 = so.Constraint( - x + 2 * y - 5, direction='L', name='c2')
sasoptpy.Constraint( -  x  +  2.0 * y  <=  5, name='c2')