ommx.adapter#
Attributes#
Exceptions#
Raised when an instance is not applicable to an adapter. |
|
Raised when no solution was returned. |
|
Raised when the problem is proven to be unbounded. |
Classes#
Adapter diagnostic report convertible with |
|
Receiver for adapter-defined diagnostics emitted during a solve. |
|
An abstract interface for OMMX Sampler Adapters, defining how samplers should be used with OMMX. |
|
An abstract interface for OMMX Solver Adapters, defining how solvers should be used with OMMX. |
Module Contents#
- exception AdapterNotApplicableError(adapter: str, report: InstanceClassMembershipReport)#
Raised when an instance is not applicable to an adapter.
- report: InstanceClassMembershipReport#
- exception NoSolutionReturned#
Raised when no solution was returned.
This indicates that the solver did not return any solution (whether feasible or not) (e.g., due to time limits). This does not prove that the mathematical model itself is infeasible.
- exception UnboundedDetected#
Raised when the problem is proven to be unbounded.
This corresponds to
Optimality.OPTIMALITY_UNBOUNDEDand indicates that the mathematical model itself is unbounded. Should not be used when unboundedness cannot be proven (e.g., heuristic solvers).
- class DiagnosticReport#
Adapter diagnostic report convertible with
dataclasses.asdict.
- class DiagnosticsSink#
Receiver for adapter-defined diagnostics emitted during a solve.
Adapters may call
recordwhile the backend solver is still running, including from backend callbacks. Sink implementations should keeprecordappend-only, defer validation or serialization until after the solve, and preserve the order in which diagnostics are received.A conforming sink must not raise from
record. If recording fails, the sink should log the failure and return normally. Ifrecorddoes raise, that is a sink contract violation; adapters may let the exception propagate and do not need to recover from it.- record(diagnostic: DiagnosticReport) None#
Record one adapter-defined dataclass diagnostic report or event.
This method must not raise under normal sink failures. Custom sinks should log failures and return instead.
- class SamplerAdapter#
An abstract interface for OMMX Sampler Adapters, defining how samplers should be used with OMMX.
See the Adapter implementation tutorial for the complete implementation contract and reference implementations.
The exact-input, easy-API preparation, strict preparation-free API, and used-variable contracts of
SolverAdapteralso apply to sampler adapters. A sampler whose backend labels are the OMMX variable IDs may preserve that identity directly; other representations need an explicit correspondence. In either case, each decoded sample must contain a value for every variable inommx_instance.used_decision_variablesbeforeevaluate_samplesis called against an unchangedommx.Instancesnapshot mathematically identical to the encoded exact input. Backend formats that omit a zero-interaction variable therefore need an adapter-owned decoding default consistent with that format.- classmethod check_applicability(ommx_instance: Instance) InstanceClassMembershipReport#
Check
INPUT_CLASSmembership without mutation.
- classmethod recommended_preparation_policy() PreparationPolicy#
Return a fresh policy recommended for this Adapter’s
INPUT_CLASS.The easy APIs apply it to an isolated copy. Advanced callers may edit and apply it explicitly before using a preparation-free API. This method itself neither prepares an instance nor guarantees applicability. The default policy is empty.
- classmethod require_applicable(ommx_instance: Instance) InstanceClassMembershipReport#
Return the membership report or raise
AdapterNotApplicableError.
- classmethod sample(ommx_instance: Instance, *, diagnostics: DiagnosticsSink | None = None) SampleSet#
Prepare and sample an isolated copy of an OMMX instance.
The input
ommx_instanceis never modified. The copy is prepared forINPUT_CLASSwithrecommended_preparation_policy(), then passed tosample_without_preparation().Run.log_sampleowns the reserveddiagnosticskeyword and uses it the same way asRun.log_solve.Nonemeans diagnostics are disabled.
- classmethod sample_without_preparation(ommx_instance: Instance, *, diagnostics: DiagnosticsSink | None = None) SampleSet#
- Abstractmethod:
Sample an exact Adapter input without running
Instance.prepare.ommx_instancemust belong toINPUT_CLASS. Implementations must reject non-members withAdapterNotApplicableErrorand must not prepare or otherwise modify the input instance.
- classmethod solve(ommx_instance: Instance, *, diagnostics: DiagnosticsSink | None = None) Solution#
Prepare and solve an isolated copy of an OMMX instance.
The input
ommx_instanceis never modified. The copy is prepared forINPUT_CLASSwithrecommended_preparation_policy(), then passed tosolve_without_preparation().Run.log_solveowns the reserveddiagnosticskeyword. When called withstore_diagnostics=True, it passes a sink to the adapter and stores recorded diagnostics with the Solve entry. Adapters may record adapter-defined dataclass diagnostics into the sink during the solve;Nonemeans diagnostics are disabled. Adapters do not need to catch exceptions raised by a non-conforming diagnostics sink.
- classmethod solve_without_preparation(ommx_instance: Instance, *, diagnostics: DiagnosticsSink | None = None) Solution#
Return the best feasible result from
sample_without_preparation().
- INPUT_CLASS: ClassVar[InstanceClass]#
Required condition for an exact Adapter input.
- property sampler_input: SamplerInput#
- Abstractmethod:
- property solver_input: SolverInput#
Expose
sampler_inputthrough the SolverAdapter interface.
- class SolverAdapter#
An abstract interface for OMMX Solver Adapters, defining how solvers should be used with OMMX.
See the Adapter implementation tutorial for the complete implementation contract and reference implementation.
Subclasses declare
INPUT_CLASSas the OMMX-defined structural class of exact inputs they can encode. Membership in that class is the complete applicability condition:check_applicabilityonly checks membership and does not mutate the input. A subclass should callrequire_applicablebefore constructing backend input.The easy
solve()API prepares an isolated copy with the Adapter’s recommended policy. Usesolve_without_preparation()when the caller has prepared an exact input explicitly and wants the Adapter to requireINPUT_CLASSmembership without applying preparation.When encoding OMMX decision variables, use
ommx_instance.used_decision_variablesrather than the complete decision variable table. Backend-only auxiliary variables may be added separately. Preserve the correspondence between each used OMMX variable ID and its backend representation. Decoding must recover a value for every used OMMX variable, construct aommx.State, and evaluate it against an unchangedommx.Instancesnapshot that is mathematically identical to the exact input encoded for the backend. The Instance owns population of fixed, dependent, and irrelevant variables and construction of the finalommx.Solution.- classmethod check_applicability(ommx_instance: Instance) InstanceClassMembershipReport#
Check
INPUT_CLASSmembership without mutation.
- classmethod recommended_preparation_policy() PreparationPolicy#
Return a fresh policy recommended for this Adapter’s
INPUT_CLASS.The easy APIs apply it to an isolated copy. Advanced callers may edit and apply it explicitly before using a preparation-free API. This method itself neither prepares an instance nor guarantees applicability. The default policy is empty.
- classmethod require_applicable(ommx_instance: Instance) InstanceClassMembershipReport#
Return the membership report or raise
AdapterNotApplicableError.
- classmethod solve(ommx_instance: Instance, *, diagnostics: DiagnosticsSink | None = None) Solution#
Prepare and solve an isolated copy of an OMMX instance.
The input
ommx_instanceis never modified. The copy is prepared forINPUT_CLASSwithrecommended_preparation_policy(), then passed tosolve_without_preparation().Run.log_solveowns the reserveddiagnosticskeyword. When called withstore_diagnostics=True, it passes a sink to the adapter and stores recorded diagnostics with the Solve entry. Adapters may record adapter-defined dataclass diagnostics into the sink during the solve;Nonemeans diagnostics are disabled. Adapters do not need to catch exceptions raised by a non-conforming diagnostics sink.
- classmethod solve_without_preparation(ommx_instance: Instance, *, diagnostics: DiagnosticsSink | None = None) Solution#
- Abstractmethod:
Solve an exact Adapter input without running
Instance.prepare.ommx_instancemust belong toINPUT_CLASS. Implementations must reject non-members withAdapterNotApplicableErrorand must not prepare or otherwise modify the input instance.
- INPUT_CLASS: ClassVar[InstanceClass]#
Required condition for an exact Adapter input.
- property solver_input: SolverInput#
- Abstractmethod:
- SamplerInput#
- SamplerOutput#
- SolverInput#
- SolverOutput#