discopt.ml.formulations.base#

Formulation wrappers for embedding trained ML models into discopt.

Classes#

NNFormulation

Embed a trained neural network into a discopt optimization model.

TreeFormulation

Embed a trained tree ensemble into a discopt optimization model.

Module Contents#

class discopt.ml.formulations.base.NNFormulation(model: discopt.modeling.core.Model, network: discopt.ml.network.NetworkDefinition, strategy: str = 'full_space', prefix: str = 'nn', scaling: discopt.ml.scaling.OffsetScaling | None = None, *, n_partitions: int | None = None, partition_strategy: str | discopt.ml.formulations.relu_partition.PartitionFn | None = None, ideal_cuts: bool = False)#

Embed a trained neural network into a discopt optimization model.

Follows the builder pattern: takes an existing Model, adds variables and constraints to represent the neural network, and exposes input/output variable handles for connecting to the rest of the optimization model.

Parameters:
  • model (discopt.Model) – The optimization model to embed the network into.

  • network (NetworkDefinition) – The trained network to embed.

  • strategy (str) – Formulation strategy. One of "full_space" (smooth activations), "relu_bigm" (ReLU via big-M MILP), "relu_partition" (the partition-based MILP of Tsay et al. 2021, between big-M and the convex hull), "relu_extended" (the per-neuron convex hull of Anderson et al. 2020), or "reduced_space" (nested expressions, no intermediate variables). The three relu_* MILPs have the same integer-feasible set; their LP relaxations are ordered big-M <= partition <= extended. "relu_complementarity" (#2167) encodes each unstable ReLU neuron as a complementarity relation 0 <= y ⊥ s >= 0 with y - s = z, with no binary and no big-M; it needs no input_bounds. The relations are declared, not lowered: pass relations to discopt.mpec.solve_mpec() with method="sos1"/"gdp" (exact) or "scholtes" (local, no certificate). Model.solve() refuses the model until then.

  • prefix (str) – Name prefix for created variables and constraints.

  • scaling (OffsetScaling or None) – Optional input/output scaling.

  • n_partitions (int, optional) – relu_partition only: partitions per unstable neuron (default 2; clipped to its number of nonzero weights). 1 is big-M.

  • partition_strategy (str or callable, optional) – relu_partition only: "equal_size" (default, weight-sorted blocks), "contiguous", or f(weights, P) -> index arrays; see partition_indices().

  • ideal_cuts (bool) – relu_* strategies only. Build an AndersonCutSeparator for the embedded neurons, available after formulate() as cut_callback; pass it as model.solve(cut_callback=...). On a pure MILP the inequalities are then separated natively in Rust (#2157): over repeated root LP solves on the default HiGHS route, at every fractional node on the Rust MILP engine.

Example

>>> nn = NNFormulation(model, network, strategy="relu_bigm")
>>> nn.formulate()
>>> model.minimize(dm.sum(nn.outputs))
property relations: list#

The complementarity relations the strategy declared (#2167).

One Complementarity per unstable ReLU neuron for strategy="relu_complementarity", in layer-then-neuron order; empty for every other strategy. Hand them to discopt.mpec.solve_mpec().

local_starts(n_starts: int, *, seed: int | None = 0) → list[dict]#

n_starts forward-pass warm starts from sampled inputs (#2167).

relu_complementarity only. Inputs are drawn uniformly from the input box, or from a standard normal per coordinate where a side is unbounded; each start is the network’s exact point at that input (see ReluComplementarityFormulation.start_point()), so every relation holds with zero residual at the start. Variables outside the network are left to the solver’s default.

solve_local(n_starts: int = 20, *, seed: int | None = 0, **kwargs)#

Multistart local solve of the embedded network’s model (#2167).

Runs discopt.mpec.solve_mpec_multistart() over relations from local_starts(). A local result: local_optimal / local_infeasible, never a bound or certificate; x is only ever a point verified against the source relations and every model row. kwargs are forwarded (polish, t_min, time_limit, …).

property inputs: discopt.modeling.core.Variable#

Input variables (unscaled, in the user’s domain).

property outputs: discopt.modeling.core.Variable#

Output variables (unscaled, in the user’s domain).

formulate() → None#

Add all variables and constraints to the model.

This method can only be called once per NNFormulation instance.

property cut_callback: discopt.ml.formulations.relu_partition.AndersonCutSeparator#

The Anderson ideal-cut separator (ideal_cuts=True only).

class discopt.ml.formulations.base.TreeFormulation(model: discopt.modeling.core.Model, ensemble: discopt.ml.tree.TreeEnsembleDefinition, prefix: str = 'tree', scaling: discopt.ml.scaling.OffsetScaling | None = None, split_eps: float = 1e-05)#

Embed a trained tree ensemble into a discopt optimization model.

Parameters:
  • model (discopt.Model) – The optimization model to embed the tree ensemble into.

  • ensemble (TreeEnsembleDefinition) – The trained tree ensemble.

  • prefix (str) – Name prefix for created variables and constraints.

  • scaling (OffsetScaling or None) – Optional input/output scaling.

  • split_eps (float) – Half-width of the separation band around each split threshold (default 1e-5). It must exceed the solver’s absolute feasibility tolerance (1e-6): a tree is discontinuous at its thresholds, so tolerance-sized slack becomes an O(1) prediction error. The default matches TreeEnsembleFormulation (#1447, #1620); see that class for the measured rationale.

Example

>>> tf = TreeFormulation(model, ensemble)
>>> tf.formulate()
>>> model.minimize(tf.outputs[0])
property inputs: discopt.modeling.core.Variable#

Input variables.

property outputs: discopt.modeling.core.Variable#

Output variables.

formulate() → None#

Add all variables and constraints to the model.