discopt.ml.formulations.base#
Formulation wrappers for embedding trained ML models into discopt.
Classes#
Embed a trained neural network into a discopt optimization model. |
|
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 threerelu_*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 relation0 <= y ⊥ s >= 0withy - s = z, with no binary and no big-M; it needs noinput_bounds. The relations are declared, not lowered: passrelationstodiscopt.mpec.solve_mpec()withmethod="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_partitiononly: partitions per unstable neuron (default 2; clipped to its number of nonzero weights).1is big-M.partition_strategy (str or callable, optional) –
relu_partitiononly:"equal_size"(default, weight-sorted blocks),"contiguous", orf(weights, P) -> index arrays; seepartition_indices().ideal_cuts (bool) –
relu_*strategies only. Build anAndersonCutSeparatorfor the embedded neurons, available afterformulate()ascut_callback; pass it asmodel.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
Complementarityper unstable ReLU neuron forstrategy="relu_complementarity", in layer-then-neuron order; empty for every other strategy. Hand them todiscopt.mpec.solve_mpec().
- local_starts(n_starts: int, *, seed: int | None = 0) list[dict]#
n_startsforward-pass warm starts from sampled inputs (#2167).relu_complementarityonly. 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 (seeReluComplementarityFormulation.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()overrelationsfromlocal_starts(). A local result:local_optimal/local_infeasible, never a bound or certificate;xis only ever a point verified against the source relations and every model row.kwargsare 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=Trueonly).
- 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.