ConstraintIDBase#
- class ConstraintIDBase#
Common base of constraint IDs, including families introduced by future SDKs.
Consumers must handle the concrete ID types they support and reject all others. This base alone is not a usable constraint identity. Integers are not instances of this class and are never accepted by mixed-family lookups.
Typed identity and integer inputs
The currently supported concrete types are
ConstraintID,IndicatorConstraintID,OneHotConstraintID, andSos1ConstraintID. An ID carries its family and an unsigned 64-bit number. IDs from different families are unequal, even if their numbers coincide. Within one Instance, constraint numbers must still be unique across all active and inactive families.APIs whose argument already fixes a family promote integer inputs to that family’s ID: for example,
active_constraints={1: constraint}infrom_components(), orinstance.constraints.of_kind(ConstraintKind.Regular)[1]. They reject IDs of another family. Such mapping inputs reject1andConstraintID(1)together withValueError, because the keys normalize to the same identity. Mixed-family collections andconstraint_violation(id)require typed IDs. DataFrame indexes also retain typed IDs. Constraint IDs do not supportint()or integer-index conversion; variable and sample IDs remain integers.IDs are immutable. Shallow and deep copies return the same ID object; copying containers or pickling preserves ID family and value, including IDs used in DataFrame indexes or columns.
>>> import copy >>> import pickle >>> from ommx import ConstraintKind, ConstraintID, OneHotConstraintID >>> cid = ConstraintID(7) >>> assert cid != 7 and cid != OneHotConstraintID(7) >>> assert copy.copy(cid) is cid >>> assert copy.deepcopy(cid) is cid >>> assert copy.deepcopy({cid: [cid]}) == {cid: [cid]} >>> restored = pickle.loads(pickle.dumps({cid: [cid]})) >>> assert restored == {cid: [cid]} >>> assert type(next(iter(restored))) is ConstraintID
Supporting additional families
New special-constraint families are additive API extensions. Collections, including inactive and result collections, expose the open common interfaces
Mapping[ConstraintIDBase, ConstraintView]. Filtered active/inactive and evaluated/sampled mappings retain precise row overloads for known ID families. Modelconstraintscombines active and inactive rows, so even concrete IDs return the commonConstraintViewtype; role must be checked at runtime. The currently supported families are not an exhaustive list of future possibilities.A solver adapter or transformation must reject active constraint families it cannot handle. Silently skipping their constraints can relax the optimization problem. Inactive constraints belong to the retained interpretation context and are not part of the Active Formulation consumed here. Keep this check even if an earlier applicability check normally excludes unsupported kinds. For example, a consumer requiring regular rows can validate the complete active collection before processing it:
>>> from ommx import ConstraintKind, ConstraintID, Instance, OneHotConstraint >>> def regular_rows(model): ... rows = [] ... for cid in model.active_constraints: ... if not isinstance(cid, ConstraintID): ... raise NotImplementedError("Unsupported constraint family") ... rows.append(model.active_constraints[cid]) ... return rows >>> model = Instance.minimize() >>> x = model.new_binary("x") >>> y = model.new_binary("y") >>> rule = model.add_constraint(x + y <= 1) >>> assert regular_rows(model) == [rule] >>> special = model.add_one_hot_constraint(OneHotConstraint(variables=[x, y])) >>> special_id = special.constraint_id >>> regular_rows(model) Traceback (most recent call last): ... NotImplementedError: Unsupported constraint family
After lowering, the original one-hot constraint remains inactive for evaluation. The consumer accepts the generated active regular constraint:
>>> converted = model.convert_all_one_hots_to_constraints() >>> assert special_id in model.inactive_constraints >>> assert [row.constraint_id for row in regular_rows(model)] == [rule.constraint_id, *converted]
Handle additional concrete ID types explicitly when supported and retain the rejecting fallback. Receiving a new family in an older consumer should fail explicitly. Metadata-only inspection can use
ConstraintViewwithout interpreting each family’s mathematical meaning.- __new__() ConstraintIDBase#