ActiveConstraintCollection#
- class ActiveConstraintCollection#
Live, read-only mapping of all active constraint families in a model.
Obtained from
active_constraintsoractive_constraints. 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.active_constraints >>> cid = rule.constraint_id >>> rows[cid].set_name("capacity") >>> assert model.active_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 inactive model constraints
Independent snapshots
Active and retained inactive constraints in a Solution
Independent evaluated snapshots
Active and retained inactive 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. Inactive and result snapshots compare their stored data, including metadata and inactivity 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=..., min_role=ConstraintRole.ACTIVE). Its typed index matches this collection’s keys for the selected family. The defaultmin_role=ConstraintRole.EVALUATION_ONLYincludes all active and inactive rows, including those skipped by default feasibility checks.>>> from ommx import ConstraintRole, ConstraintKind >>> model.deactivate_constraint(cid, "retain for reporting") >>> table = model.constraints_df( ... kind=ConstraintKind.Regular, ... min_role=ConstraintRole.ACTIVE, ... ) >>> assert set(table.index) == set(rows.of_kind(ConstraintKind.Regular)) >>> assert cid not in table.index >>> assert cid in model.constraints_df(kind=ConstraintKind.Regular).index
- __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: ConstraintKind[ConstraintID]) ConstraintFamilyView[ConstraintID, AttachedConstraint]#
- of_kind(kind: ConstraintKind[IndicatorConstraintID]) ConstraintFamilyView[IndicatorConstraintID, AttachedIndicatorConstraint]
- of_kind(kind: ConstraintKind[OneHotConstraintID]) ConstraintFamilyView[OneHotConstraintID, AttachedOneHotConstraint]
- of_kind(kind: ConstraintKind[Sos1ConstraintID]) ConstraintFamilyView[Sos1ConstraintID, AttachedSos1Constraint]
- of_kind(kind: ConstraintKind[_K]) ConstraintFamilyView[_K, ConstraintView]
Return a live view of one family without collecting rows. Uses the same ConstraintKind selector 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.