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 aSolutionwith 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
statecontains the populated assignment. Missing required IDs are errors; reconstruction, default values for irrelevant variables, and tolerance-based normalization are documented bypopulate_state().- __contains__(key: int) bool#
- __len__() int#
- __repr__() str#
- items() list[tuple[int, float]]#
- keys() list[int]#
- load_qplib_solution(path: str, *, num_variables: int) State#
Load a published QPLIB
.solfile into aState.Pass the variable count of the original QPLIB instance as
num_variables. Omitted variables receive zero. The standard QPLIB solution namesxN,bN, andiNmap to OMMX IDN - 2;objvaris ignored because the objective is computed byevaluate(). Custom names and other solvers’.solformats are not supported. Names are case insensitive.Malformed input, duplicate IDs, nonfinite values, and out-of-range IDs raise
ValueErrorwith a line number. File read failures raiseRuntimeError. Loading does not check bounds or feasibility. See thepublished candidate exampleto 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]#