OneHotConstraint

OneHotConstraint#

class OneHotConstraint#

Require exactly one member of a nonempty set of Binary variables to equal one.

All other members must equal zero, so the mathematical condition is \(\sum_i x_i=1\). This is a structural constraint: it stores member IDs rather than an explicit Function. Keeping the selection structure allows transformations and result inspection to retain its meaning.

Each variables entry accepts a variable ID, a standalone DecisionVariable, or an AttachedDecisionVariable. These accepted forms are exposed as the VariableIDLike type alias. Only their IDs are stored; duplicate entries identify the same member. The constructor rejects an empty set. The enclosing Instance validates that every referenced variable exists and is Binary when inserting the constraint.

Construct and evaluate a choice

>>> from ommx import ConstraintKind, Instance, OneHotConstraint, OneHotConstraintID
>>> instance = Instance.maximize()
>>> choices = [instance.new_binary("choice", subscripts=[i]) for i in range(3)]
>>> instance.objective = sum(value * x for value, x in zip([5, 10, 3], choices))
>>> condition = OneHotConstraint(variables=choices)
>>> assert condition.variables == [x.id for x in choices]
>>> attached = instance.add_one_hot_constraint(condition)
>>> cid = attached.constraint_id
>>> assert isinstance(cid, OneHotConstraintID)
>>> chosen = instance.evaluate({choices[0].id: 0, choices[1].id: 1, choices[2].id: 0})
>>> assert chosen.feasible and chosen.objective == 10
>>> assert not instance.evaluate({x.id: 0 for x in choices}).feasible
>>> assert not instance.evaluate({x.id: 1 for x in choices}).feasible
>>> rows = chosen.constraints_df(kind=ConstraintKind.OneHot)
>>> assert list(rows.index) == [cid]
>>> assert rows.loc[cid, "violation"] == 0.0

Unlike Sos1Constraint, this condition does not allow selecting no member. Numerical evaluation and reported violations use the contracts of evaluate() and constraints_df().

Identity and lowering

add_one_hot_constraint() returns an attached handle. The constructor creates an independent value; context changes on it do not edit a constraint already inserted into a host. For explicit IDs, use the one_hot_constraints mapping of from_components(). Constraint numbers share the active/removed cross-family space described by Instance.

convert_one_hot_to_constraint() lowers the condition to a regular equality, assigns the equality a fresh ID, and retains the original OneHot for evaluation. Generated constraints carry provenance back to that original; the method’s example shows how to inspect the relationship. lower_special_constraints() selects whole families to lower. For automatic preparation and execution, consult the selected Adapter’s Reference, such as OMMXPySCIPOptAdapter.

__copy__() → OneHotConstraint#
__deepcopy__(_memo: Any) → OneHotConstraint#
__new__(*, variables: Sequence[VariableIDLike], name: Optional[str] = None, subscripts: Sequence[int] = [], description: Optional[str] = None, parameters: Mapping[str, str] = {}) → OneHotConstraint#

Create a new one-hot constraint.

Args:

  • variables: Variable IDs or decision-variable objects (exactly one must be 1). Only their IDs are stored; the enclosing host requires the referenced variables to be binary when the constraint is inserted.

  • name / subscripts / description / parameters: Optional context. Drained into the host’s SoA store on insertion.

__repr__() → str#
set_description(description: str) → OneHotConstraint#

Set the description. Returns self for method chaining (snapshot mutation).

set_name(name: str) → OneHotConstraint#

Set the name. Returns self for method chaining (snapshot mutation).

set_parameters(parameters: Mapping[str, str]) → OneHotConstraint#

Replace all parameters. Returns self for method chaining (snapshot mutation).

set_subscripts(subscripts: Sequence[int]) → OneHotConstraint#

Set the subscripts. Returns self for method chaining (snapshot mutation).

property description: Optional[str]#

Read-only property.

property name: Optional[str]#

Read-only property.

property parameters: dict[str, str]#

Read-only property.

property subscripts: list[int]#

Read-only property.

property variables: list[int]#

Read-only property.