ConstraintCollection#
- class ConstraintCollection#
Live, read-only mapping of all active constraint families in a model.
Obtained from
constraintsorconstraints. The collection keeps its owner alive and observes subsequent owner mutations. Obtaining it does not collect constraint rows; indexing fetches the selected row from the owner’s current state. Active model rows are attached handles whose label setters update that owner.>>> from ommx import ConstraintID, Instance >>> model = Instance.minimize() >>> x = model.new_binary("x") >>> rule = model.add_constraint(x <= 1, "limit") >>> rows = model.constraints >>> cid = rule.constraint_id >>> rows[cid].set_name("capacity") >>> assert model.constraints[cid].name == "capacity" >>> assert rows.get(ConstraintID(99)) is None >>> assert rows.get(ConstraintID(99), "missing") == "missing"
Typed keys and row ownership
Mixed-family lookups require typed IDs, even though constraint numbers are unique across all families in an Instance. Bare integers raise
TypeError; a missing typed ID raisesKeyError.get(id)returnsNonefor a missing typed ID, or the supplied default inget(id, default). Indexing andgetpreserve the concrete row type for a known concrete ID; the latter’s return type also includes the default’s type. SeeConstraintIDBasefor ID families, integer promotion, and the extension contract.The collection cannot be changed by item assignment or deletion. Change the model through its owner methods. Related collections use the same lookup and iteration rules but have different row ownership:
Collection
Included constraints
Returned rows
Active model constraints
Attached handles
Retained removed model constraints
Independent snapshots
Active and retained removed constraints in a Solution
Independent evaluated snapshots
Active and retained removed constraints in a SampleSet
Independent sampled snapshots
Attached handles compare equal when they refer to the same owner object and typed ID. Handles from different owners are unequal even if the models contain identical data. Removed and result snapshots compare their stored data, including metadata and removal reasons. Mapping equality and membership in
values()oritems()use those row equality rules.Live views and iteration
keys(),values(), anditems()are live views.iter(rows)snapshots typed IDs immediately; iterators over the standard Python views snapshot them on the firstnext(). Within the currently supported families, IDs follow family order (regular, indicator, one-hot, SOS1), then numeric order. Values are fetched as they are visited. If a snapshotted key is removed before its value is fetched, that access raisesKeyError.>>> ids_before_addition = iter(rows) >>> keys = rows.keys() >>> added = model.add_constraint(x >= 0, "lower") >>> assert list(ids_before_addition) == [cid] >>> assert list(keys) == [cid, added.constraint_id] >>> assert rows == dict(rows)
dict(rows)materializes a mapping at that time; its active model values are still attached handles. To inspect just one family, useof_kindandConstraintFamilyView. To collect a table, use the owner’sconstraints_df(kind=...). Its typed index matches the selected family’s keys; on models,removed=Trueincludes both active and retained removed rows.- __contains__(key: object) bool#
- __getitem__(key: ConstraintID) AttachedConstraint#
- __getitem__(key: IndicatorConstraintID) AttachedIndicatorConstraint
- __getitem__(key: OneHotConstraintID) AttachedOneHotConstraint
- __getitem__(key: Sos1ConstraintID) AttachedSos1Constraint
- __getitem__(key: ConstraintIDBase) ConstraintView
Fetch the constraint identified by a typed ID from the current owner.
- __iter__() Iterator[ConstraintIDBase]#
Iterate a snapshot of typed IDs; constraint rows are not collected.
- __len__() int#
- __repr__() str#
- get(key: ConstraintID, default: None = None) AttachedConstraint | None#
- get(key: ConstraintID, default: _T) AttachedConstraint | _T
- get(key: IndicatorConstraintID, default: None = None) AttachedIndicatorConstraint | None
- get(key: IndicatorConstraintID, default: _T) AttachedIndicatorConstraint | _T
- get(key: OneHotConstraintID, default: None = None) AttachedOneHotConstraint | None
- get(key: OneHotConstraintID, default: _T) AttachedOneHotConstraint | _T
- get(key: Sos1ConstraintID, default: None = None) AttachedSos1Constraint | None
- get(key: Sos1ConstraintID, default: _T) AttachedSos1Constraint | _T
- get(key: ConstraintIDBase, default: None = None) ConstraintView | None
- get(key: ConstraintIDBase, default: ConstraintView) ConstraintView
- get(key: ConstraintIDBase, default: _T) ConstraintView | _T
- items() ItemsView[ConstraintIDBase, ConstraintView]#
Live item view; each row is fetched when visited.
- keys() KeysView[ConstraintIDBase]#
Live key view. Iteration order is family order, then numeric ID.
- of_kind(kind: Literal["regular"]) ConstraintFamilyView[ConstraintID, AttachedConstraint]#
- of_kind(kind: Literal["indicator"]) ConstraintFamilyView[IndicatorConstraintID, AttachedIndicatorConstraint]
- of_kind(kind: Literal["one_hot"]) ConstraintFamilyView[OneHotConstraintID, AttachedOneHotConstraint]
- of_kind(kind: Literal["sos1"]) ConstraintFamilyView[Sos1ConstraintID, AttachedSos1Constraint]
Return a live view of one family without collecting rows. Uses the same kind strings as constraints_df. Integer keys are promoted; wrong-family IDs are rejected. The view retains this collection and its owner; row lifecycle and iteration snapshot rules match the original collection.
- values() ValuesView[ConstraintView]#
Live value view; each row is fetched when visited.