Week 6 Guided Lab: Explain bounds before comparing solvers

Problem and setup

Embed the same frozen 2→12→12→3 reactor network using manual constraints and OMLT. Inputs are T∈[310,350] K and tau∈[2,10] min; feed is fixed at 1.2 mol/L. Require B≥0.38, C≤0.25, and nonnegative outputs. Minimize tau+0.02(T−310). Investigate how valid bounds change the LP relaxation while preserving the network.

Extract the lab ZIP, keeping requirements.txt, the notebook, Python scripts, and supplied models together. These exercises were checked with Python 3.12. Use that folder as the working directory and run the commands below, or execute the notebook cells in order. Downloading the notebook alone can omit required scripts or model files.

python -m pip install -r requirements.txt
python guided_lab.py

guided_lab.py executes the calculations below in order and checks key values with assertions. Files in guided-results/ belong to your new run; compare them with the supplied reference results. For Jupyter, select a kernel from the same Python environment. For ModuleNotFoundError, check that requirements were installed in that environment. For missing local files, check the working directory.

1. Propagate signs through one affine bound

For a=2x1−3x2+0.5 and x1,x2∈[0,1], the smallest value uses x1=0,x2=1; the largest uses x1=1,x2=0. Positive weights multiply lower bounds for a lower estimate; negative weights multiply upper bounds. This is the calculation performed layer by layer.

from pathlib import Path
import json
import numpy as np
import week06_lab as lab
lo, hi = np.zeros(2), np.ones(2)
w = np.array([2., -3.]); b = .5
lower = lo@np.maximum(w,0)+hi@np.minimum(w,0)+b
upper = hi@np.maximum(w,0)+lo@np.minimum(w,0)+b
print("affine bounds:", lower, upper, "ReLU bounds:", max(0,lower), max(0,upper))
np.testing.assert_allclose([lower,upper], [-2.5,2.5])
net = json.loads(Path("frozen_cstr_network.json").read_text())
bounds = lab.interval_bounds(net)
print("first three neuron bounds:", list(zip(bounds[0][0][:3],bounds[0][1][:3])))

After ReLU the interval is [0,2.5]. A preactivation upper bound ≤0 fixes a neuron off; a lower bound ≥0 makes it linear. Only intervals crossing zero need binaries. Broad independent intervals may miss correlations between neurons.

2. Explain the two-neuron relaxation gap

The toy model has x1+x2=1, so a0=x1+x2−1=0 exactly. The other activation is max(0,x1−x2)=max(0,2x1−1). Requiring total activation ≥0.4 gives the exact solution x1≥0.7. A loose LP can inflate activations; fixing the first activation to zero removes one source of inflation.

toy = {}
for tight in [False,True]:
    for relax in [True,False]:
        result = lab.toy_case(tight,relax)
        toy[tight,relax] = result["objective"]
        print("tight, LP, objective:", tight,relax,result["objective"])
np.testing.assert_allclose([toy[False,True],toy[True,True],toy[True,False]], [0,.4,.7], atol=1e-7)

LP lower bounds improve from 0 to 0.4; the MILP optimum remains 0.7. The tightened LP is still a relaxation, not an exact forward graph. A stronger lower bound can reduce search, but preprocessing also takes time.

3. Follow physical scaling and layer outputs

At [330 K,6 min], subtract [310,2] and divide by [40,8] to obtain [0.5,0.5]. Each saved weight matrix maps a row of activations to the next layer. Apply ReLU only where the layer metadata says relu; the output layer stays linear.

physical = np.array([330.,6.])
state = (physical-net["offset"])/net["scale"]
print("scaled input:", state)
for index, layer in enumerate(net["layers"]):
    pre = state@np.asarray(layer["weights"])+np.asarray(layer["bias"])
    state = np.maximum(pre,0) if layer["activation"] == "relu" else pre
    print("layer",index,"first preactivations",pre[:3],"first outputs",state[:3])
np.testing.assert_allclose(state,lab.forward(physical,net))
fixed = lab.solve_report(lab.make_omlt(net,fixed=[.5,.5],specifications=False))
assert fixed["has_incumbent"]
np.testing.assert_allclose(fixed["outputs"],state,atol=1e-6)

The direct forward output and fixed-input OMLT output should agree within tolerance. This checks scaling, matrix orientation, and activation encoding without mixing in product feasibility. The fixed argument uses scaled coordinates [0.5,0.5].

4. Compare complete reports, including failures

The full run compares loose, interval, LP-tightened, and OMLT formulations using identical coefficients. LP-OBBT solves auxiliary LPs to bound each preactivation over the declared input domain. Record bound computation, model construction, and solve time separately.

import subprocess, sys
subprocess.run([sys.executable,"week06_lab.py","--output-dir","guided-results"],check=True)
report = json.loads(Path("guided-results/results.json").read_text())
for row in report["comparisons"]:
    print(row["formulation"],"LP bound",row.get("lp_objective"),"incumbent",row["objective"],
          "binaries",row["binary_variables"],"total seconds",row["total_seconds"])
print("infeasible:",report["infeasible_case"])
print("zero time limit:",report["zero_time_limit_case"])
assert report["max_forward_error"] < 1e-5
assert not report["infeasible_case"]["has_incumbent"]

The feasible optimal objectives should agree while relaxation bounds and binary counts can differ. Compare total time, including OBBT, and avoid drawing a speed ranking from one run. An absent incumbent means no selected inputs or gap can be reported. Submit a decoded bound, toy calculation, layer trace, equivalence error, solver status, and reference outputs at the selected point.

What to report

Record the inputs, outputs, units and fixed conditions; your hand prediction; intermediate computed values; and final metrics with interpretation. Before changing one setting, predict the direction of its effect, then revise that explanation after execution. Record the environment and seed.

Lecture · Derivations and sources