State

State#

class State#

A candidate assignment from integer decision-variable IDs to numeric values.

A State contains values, not the variable domains, objective, or constraints of an optimization problem. Creating one does not establish feasibility or optimality. Pass it to evaluate() on the corresponding problem to obtain a Solution with evaluation results.

>>> from ommx import Instance, State
>>> instance = Instance.maximize()
>>> x = instance.new_binary("x")
>>> instance.objective = x
>>> candidate = State({x.id: 1})
>>> assert candidate.entries == {x.id: 1.0}
>>> result = instance.evaluate(candidate)
>>> assert result.feasible and result.objective == 1.0

APIs accepting a State also accept an ID-to-value mapping or an iterable of ID/value pairs. The assignment must use the IDs of the Instance being evaluated. After transforming a problem, evaluate the returned candidate with that transformed Instance so that its recorded fixed values, dependencies, retained constraints, and output objective can be interpreted correctly.

The returned Solution’s state contains the populated assignment. Missing required IDs are errors; reconstruction, default values for irrelevant variables, and tolerance-based normalization are documented by populate_state().

__contains__(key: int) → bool#
__copy__() → State#
__deepcopy__(_memo: Any) → State#
__len__() → int#
__new__(entries: ToState) → State#
__repr__() → str#
from_v1_bytes(bytes: bytes) → State#
get(key: int) → Optional[float]#
items() → list[tuple[int, float]]#
keys() → list[int]#
load_qplib_solution(path: str, *, num_variables: int) → State#

Load a published QPLIB .sol file into a State.

Pass the variable count of the original QPLIB instance as num_variables. Omitted variables receive zero. The standard QPLIB solution names xN, bN, and iN map to OMMX ID N - 2; objvar is ignored because the objective is computed by evaluate(). Custom names and other solvers’ .sol formats are not supported. Names are case insensitive.

Malformed input, duplicate IDs, nonfinite values, and out-of-range IDs raise ValueError with a line number. File read failures raise RuntimeError. Loading does not check bounds or feasibility. See the published candidate example to read a matching problem, evaluate the assignment, and inspect violations.

>>> import tempfile
>>> from pathlib import Path
>>> from ommx import State
>>> with tempfile.TemporaryDirectory() as directory:
...     path = Path(directory) / "example.sol"
...     _ = path.write_text("objvar 12\nx2 0.5\nb4 1\n")
...     state = State.load_qplib_solution(str(path), num_variables=3)
>>> state.entries
{0: 0.5, 1: 0.0, 2: 1.0}
set(key: int, value: float) → None#
to_v1_bytes() → bytes#
values() → list[float]#
property entries: dict[int, float]#