15"""Linear constraint in a model."""
17from typing
import Any, Iterator, NamedTuple
26 """A linear constraint for an optimization model.
28 A LinearConstraint adds the following restriction on feasible solutions to an
30 lb <= sum_{i in I} a_i * x_i <= ub
31 where x_i are the decision variables of the problem. lb == ub is allowed, this
32 models the equality constraint:
33 sum_{i in I} a_i * x_i == b
34 Setting lb > ub will result in an InvalidArgument error at solve time (the
35 values are allowed to cross temporarily between solves).
37 A LinearConstraint can be configured as follows:
38 * lower_bound: a float property, lb above. Should not be NaN nor +inf.
39 * upper_bound: a float property, ub above. Should not be NaN nor -inf.
40 * set_coefficient() and get_coefficient(): get and set the a_i * x_i
41 terms. The variable must be from the same model as this constraint, and
42 the a_i must be finite and not NaN. The coefficient for any variable not
43 set is 0.0, and setting a coefficient to 0.0 removes it from I above.
45 The name is optional, read only, and used only for debugging. Non-empty names
48 Do not create a LinearConstraint directly, use Model.add_linear_constraint()
49 instead. Two LinearConstraint objects can represent the same constraint (for
50 the same model). They will have the same underlying LinearConstraint.elemental
51 for storing the data. The LinearConstraint class is simply a reference to an
55 __slots__ =
"_elemental",
"_id"
57 def __init__(self, elem: elemental.Elemental, cid: int) ->
None:
58 """Internal only, prefer Model functions (add_linear_constraint() and get_linear_constraint())."""
59 if not isinstance(cid, int):
60 raise TypeError(f
"cid type should be int, was:{type(cid).__name__!r}")
67 enums.DoubleAttr1.LINEAR_CONSTRAINT_LOWER_BOUND, (self.
_id,)
73 enums.DoubleAttr1.LINEAR_CONSTRAINT_LOWER_BOUND, (self.
_id,), value
79 enums.DoubleAttr1.LINEAR_CONSTRAINT_UPPER_BOUND, (self.
_id,)
85 enums.DoubleAttr1.LINEAR_CONSTRAINT_UPPER_BOUND, (self.
_id,), value
91 enums.ElementType.LINEAR_CONSTRAINT, self.
_id
99 def elemental(self) -> elemental.Elemental:
100 """Internal use only."""
104 from_model.model_is_same(var, self)
106 enums.DoubleAttr2.LINEAR_CONSTRAINT_COEFFICIENT,
112 from_model.model_is_same(var, self)
114 enums.DoubleAttr2.LINEAR_CONSTRAINT_COEFFICIENT, (self.
_id, var.id)
117 def terms(self) -> Iterator[variables.LinearTerm]:
118 """Yields the variable/coefficient pairs with nonzero coefficient for this linear constraint."""
120 enums.DoubleAttr2.LINEAR_CONSTRAINT_COEFFICIENT, 0, self.
_id
123 enums.DoubleAttr2.LINEAR_CONSTRAINT_COEFFICIENT, keys
125 for i
in range(len(keys)):
128 coefficient=float(coefs[i]),
132 """Returns the bounded expression from lower_bound, upper_bound and terms."""
133 return variables.BoundedLinearExpression(
138 """Returns the name, or a string containing the id if the name is empty."""
139 return self.
name if self.
name else f
"linear_constraint_{self.id}"
142 return f
"<LinearConstraint id: {self.id}, name: {self.name!r}>"
145 if isinstance(other, LinearConstraint):
146 return self.
_id == other._id
and self.
_elemental is other._elemental
150 return hash(self.
_id)
154 linear_constraint: LinearConstraint