Feasible High-Order Neural ODE Solver / sweep.py
Mechanism failed
1import json
2import numpy as np
3from experiment import integrate
4
5
6def run(lam, T, ns):
7 f = lambda t, y: lam * (1.0 - y)
8 box = (np.array([0.0]), np.array([1.0]))
9 exact = 1.0 - np.exp(-lam * T)
10 out = []
11 for n in ns:
12 row = {"n": n}
13 for method in ["unconstrained", "clip", "constrained"]:
14 try:
15 y, violation, residual, iters = integrate(
16 method, [0.0], f, T, n, box)
17 row[method] = {
18 "final": float(y[0]),
19 "abs_error": float(abs(y[0] - exact)),
20 "max_constraint_violation": float(violation),
21 "mean_residual": float(residual),
22 "inner_iterations": int(iters),
23 }
24 except Exception as exc:
25 row[method] = {"error": str(exc)}
26 out.append(row)
27 return out
28
29
30if __name__ == "__main__":
31 result = {
32 "lambda_10": run(10.0, 1.0, [2, 4, 8, 16, 32]),
33 "lambda_40": run(40.0, 1.0, [2, 4, 8, 16, 32]),
34 }
35 with open("sweep_results.json", "w") as fp:
36 json.dump(result, fp, indent=2)
37 print(json.dumps(result, indent=2))