Problem and setup
Use the Week 6 frozen reactor network on T∈[310,350] K and tau∈[2,10] min. Keep feed=1.2 mol/L and specifications B≥0.38, C≤0.25, and nonnegative concentrations. The assumed hourly cost is 12·tau+0.24·(T−310). Compare a feasible baseline, an unadjusted optimizer, calibration margins, local correction, and reference repair.
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. Check the baseline by hand
At 330 K and 6 min, k1=0.2 and k2=0.1 per minute. A=1.2/2.2; B=1.2·A/1.6; C=0.6·B. Test product shortfall max(0,0.38−B), byproduct excess max(0,C−0.25), negativity, and sum error. Cost is 72+4.8=76.8 dollars/hour under the stated teaching assumptions.
from pathlib import Path
import json
import numpy as np
import week08_lab as lab
net = json.loads(Path("frozen_cstr_network.json").read_text())
baseline = lab.evaluate("baseline",[330.,6.],net)
print("baseline reference:",baseline["reference"])
print("residuals:",baseline["reference_residuals"],"cost:",baseline["cost"])
np.testing.assert_allclose(baseline["reference"],[.5454545454545,.4090909090909,.2454545454545])
np.testing.assert_allclose(baseline["cost"],76.8)
assert baseline["reference_feasible"]
All baseline residuals are zero up to floating-point tolerance. Retain this feasible comparison while assessing cheaper candidates; a low reported cost alone is not an acceptable operating decision.
2. Revalidate the optimizer at the same coordinates
Solve the surrogate model, then evaluate the reference at exactly the returned T and tau. Average held-out accuracy does not guarantee accuracy at a boundary selected by optimization. Keep prediction error signed so that overpredicted B can be linked directly to product shortfall.
point, solver = lab.solve_candidate(net)
candidate = lab.evaluate("unadjusted",point,net,solver)
print("selected [T,tau]:",point)
print("prediction:",candidate["prediction"],"reference:",candidate["reference"])
print("signed error:",candidate["prediction_error"],"residuals:",candidate["reference_residuals"])
assert solver["has_incumbent"] and solver["forward_error"] < 1e-6
assert not candidate["reference_feasible"]
assert candidate["savings_vs_baseline"] is None
The unadjusted network meets its own product specification, while the reference B is about 0.371 mol/L and therefore below 0.38. Forward agreement verifies the formulation; the reference check rejects this operating candidate. Savings are left undefined for an infeasible candidate.
3. Calculate directional margins on calibration data
B is dangerous when predicted too high; C is dangerous when predicted too low. On a separate 31×31 calibration grid, take max(predB−refB,0) and max(refC−predC,0). Add the first margin to the B lower bound and subtract the second from the C upper bound.
axis = np.linspace(0,1,31)
scaled = np.array(np.meshgrid(axis,axis)).reshape(2,-1).T
calibration = lab.OFFSET+lab.SCALE*scaled
error = lab.forward(calibration,net)-lab.reference(calibration)
b_margin = max(0.,float(error[:,1].max()))
c_margin = max(0.,float((-error[:,2]).max()))
print("calibration rows:",len(calibration),"B margin:",b_margin,"C margin:",c_margin)
print("new surrogate limits:",.38+b_margin,.25-c_margin)
np.testing.assert_allclose([b_margin,c_margin],[.02130093477436318,.01304798025717252],atol=1e-8)
margin_point, margin_solver = lab.solve_candidate(net,b_margin,c_margin)
margin_candidate = lab.evaluate("margin",margin_point,net,margin_solver)
print("margin reference residuals:",margin_candidate["reference_residuals"])
The adjusted surrogate limits are about B≥0.401301 and C≤0.236952 mol/L. These are sample maxima on 961 points, not uniform error guarantees between points. Revalidate the resulting candidate against the original 0.38/0.25 specifications.
4. See what nine local evaluations fit
Center a 3×3 grid at the failed decision using scaled offsets −0.06,0,0.06 and clip to the input box. Fit reference minus prediction as an affine correction in [1,scaled T,scaled tau]. A 9×3 design and 9×3 residual matrix produce 3×3 correction coefficients. The correction is optimized only within that local box.
center = (point-lab.OFFSET)/lab.SCALE
axes = [np.clip(center[j]+np.array([-.06,0,.06]),0,1) for j in range(2)]
local_scaled = np.array(np.meshgrid(*axes)).reshape(2,-1).T
local = lab.OFFSET+lab.SCALE*local_scaled
design = np.column_stack([np.ones(len(local)),local_scaled])
residual = lab.reference(local)-lab.forward(local,net)
correction, _, rank, _ = np.linalg.lstsq(design,residual,rcond=None)
print("first three physical inputs:",local[:3])
print("first residual and fitted correction:",residual[0],design[0]@correction)
print("design shape, rank, coefficient shape:",design.shape,rank,correction.shape)
assert design.shape == (9,3) and correction.shape == (3,3) and rank == 3
report = lab.run(Path("guided-results"))
np.testing.assert_allclose(correction,report["local_update"]["coefficients"],atol=1e-8)
The affine fit minimizes local squared residuals; it does not force every residual to zero or certify feasibility. The full run keeps the failed local candidate, then performs a separate reference-model repair. Read targeted_data.csv to see which nine reference calls supported the update.
5. Compare only accepted candidates on cost
For each candidate read the reference residuals before cost savings. A residual tolerance of 1e−7 is used by evaluate. The reference benchmark uses a temperature scan and scalar refinement; it is a numerical comparison point without a certified global lower bound.
for row in report["candidates"]:
print(row["name"],"feasible",row["reference_feasible"],"cost",row["cost"],
"residuals",row["reference_residuals"],"savings",row["savings_vs_baseline"])
accepted = [r for r in report["candidates"] if r["reference_feasible"]]
best = min(accepted,key=lambda r:r["cost"])
print("lowest cost among revalidated candidates:",best["name"],best["cost"])
assert all(r["savings_vs_baseline"] is None for r in report["candidates"] if not r["reference_feasible"])
assert not report["benchmark"]["global_certificate"]
Report feasibility, reference cost, and savings separately. The 997 held-out points estimate prediction accuracy; the 961 calibration points set margins; the nine targeted points fit the correction. Keep those roles separate when changing a margin or local width. Revalidate every new decision and explain a rejected candidate as carefully as an accepted one.
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.