Skip to content

Commit 9774a36

Browse files
committed
bench: restructuring files
1 parent 7f677aa commit 9774a36

File tree

5 files changed

+212
-222
lines changed

5 files changed

+212
-222
lines changed

benchmark/0_bench_setup.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Ts = 400.0
2+
sys = [ tf(1.90,[1800.0,1]) tf(1.90,[1800.0,1]) tf(1.90,[1800.0,1]);
3+
tf(-0.74,[800.0,1]) tf(0.74,[800.0,1]) tf(-0.74,[800.0,1]) ]
4+
function f!(ẋ, x, u, d, p)
5+
mul!(ẋ, p.A, x)
6+
mul!(ẋ, p.Bu, u, 1, 1)
7+
mul!(ẋ, p.Bd, d, 1, 1)
8+
return nothing
9+
end
10+
function h!(y, x, d, p)
11+
mul!(y, p.C, x)
12+
mul!(y, p.Dd, d, 1, 1)
13+
return nothing
14+
end

benchmark/1_bench_sim_model.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
linmodel = setop!(LinModel(sys, Ts, i_d=[3]), uop=[10, 50], yop=[50, 30], dop=[5])
2+
nonlinmodel = NonLinModel(f!, h!, Ts, 2, 4, 2, 1, p=linmodel, solver=nothing)
3+
nonlinmodel = setop!(nonlinmodel, uop=[10, 50], yop=[50, 30], dop=[5])
4+
u, d, y = [10, 50], [5], [50, 30]
5+
6+
## ----------------- Runtime benchmarks ---------------------------------------------
7+
# TODO: Add runtime benchmarks for SimModel
8+
9+
10+
## ----------------- Allocation benchmarks ------------------------------------------
11+
samples, evals = 1, 1
12+
SUITE["allocation"]["SimModel"]["LinModel"]["updatestate!"] = @benchmarkable(
13+
updatestate!($linmodel, $u, $d); samples=samples, evals=evals
14+
)
15+
SUITE["allocation"]["SimModel"]["LinModel"]["evaloutput"] = @benchmarkable(
16+
evaloutput($linmodel, $d); samples=samples, evals=evals
17+
)
18+
SUITE["allocation"]["SimModel"]["NonLinModel"]["updatestate!"] = @benchmarkable(
19+
updatestate!($nonlinmodel, $u, $d); samples=samples, evals=evals
20+
)
21+
SUITE["allocation"]["SimModel"]["NonLinModel"]["evaloutput"] = @benchmarkable(
22+
evaloutput($nonlinmodel, $d); samples=samples, evals=evals
23+
)
24+
SUITE["allocation"]["SimModel"]["NonLinModel"]["linearize!"] = @benchmarkable(
25+
linearize!($linmodel, $nonlinmodel); samples=samples, evals=evals
26+
)

benchmark/2_bench_state_estim.jl

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
## ----------------- Runtime benchmarks ---------------------------------------------
2+
# TODO: Add runtime benchmarks for StateEstimator
3+
4+
5+
## ----------------- Allocation benchmarks ------------------------------------------
6+
samples, evals = 1, 1
7+
8+
skf = SteadyKalmanFilter(linmodel)
9+
SUITE["allocation"]["StateEstimator"]["SteadyKalmanFilter"]["preparestate!"] =
10+
@benchmarkable(
11+
preparestate!($skf, $y, $d),
12+
samples=samples, evals=evals
13+
)
14+
SUITE["allocation"]["StateEstimator"]["SteadyKalmanFilter"]["updatestate!"] =
15+
@benchmarkable(
16+
updatestate!($skf, $u, $y, $d),
17+
setup=preparestate!($skf, $y, $d),
18+
samples=samples, evals=evals
19+
)
20+
SUITE["allocation"]["StateEstimator"]["SteadyKalmanFilter"]["evaloutput"] =
21+
@benchmarkable(
22+
evaloutput($skf, $d),
23+
setup=preparestate!($skf, $y, $d),
24+
samples=samples, evals=evals
25+
)
26+
27+
kf = KalmanFilter(linmodel, nint_u=[1, 1], direct=false)
28+
SUITE["allocation"]["StateEstimator"]["KalmanFilter"]["preparestate!"] =
29+
@benchmarkable(
30+
preparestate!($kf, $y, $d),
31+
samples=samples, evals=evals
32+
)
33+
SUITE["allocation"]["StateEstimator"]["KalmanFilter"]["updatestate!"] =
34+
@benchmarkable(
35+
updatestate!($kf, $u, $y, $d),
36+
setup=preparestate!($kf, $y, $d),
37+
samples=samples, evals=evals
38+
)
39+
40+
lo = Luenberger(linmodel, nint_u=[1, 1])
41+
SUITE["allocation"]["StateEstimator"]["Luenberger"]["preparestate!"] =
42+
@benchmarkable(
43+
preparestate!($lo, $y, $d),
44+
samples=samples, evals=evals
45+
)
46+
SUITE["allocation"]["StateEstimator"]["Luenberger"]["updatestate!"] =
47+
@benchmarkable(
48+
updatestate!($lo, $u, $y, $d),
49+
setup=preparestate!($lo, $y, $d),
50+
samples=samples, evals=evals
51+
)
52+
53+
im = InternalModel(nonlinmodel)
54+
SUITE["allocation"]["StateEstimator"]["InternalModel"]["preparestate!"] =
55+
@benchmarkable(
56+
preparestate!($im, $y, $d),
57+
samples=samples, evals=evals
58+
)
59+
SUITE["allocation"]["StateEstimator"]["InternalModel"]["updatestate!"] =
60+
@benchmarkable(
61+
updatestate!($im, $u, $y, $d),
62+
setup=preparestate!($im, $y, $d),
63+
samples=samples, evals=evals
64+
)
65+
66+
ukf = UnscentedKalmanFilter(nonlinmodel)
67+
SUITE["allocation"]["StateEstimator"]["UnscentedKalmanFilter"]["preparestate!"] =
68+
@benchmarkable(
69+
preparestate!($ukf, $y, $d),
70+
samples=samples, evals=evals
71+
)
72+
SUITE["allocation"]["StateEstimator"]["UnscentedKalmanFilter"]["updatestate!"] =
73+
@benchmarkable(
74+
updatestate!($ukf, $u, $y, $d),
75+
setup=preparestate!($ukf, $y, $d),
76+
samples=samples, evals=evals
77+
)
78+
SUITE["allocation"]["StateEstimator"]["UnscentedKalmanFilter"]["evaloutput"] =
79+
@benchmarkable(
80+
evaloutput($ukf, $d),
81+
setup=preparestate!($ukf, $y, $d),
82+
samples=samples, evals=evals
83+
)
84+
85+
ekf = ExtendedKalmanFilter(linmodel, nint_u=[1, 1], direct=false)
86+
SUITE["allocation"]["StateEstimator"]["ExtendedKalmanFilter"]["preparestate!"] =
87+
@benchmarkable(
88+
preparestate!($ekf, $y, $d),
89+
samples=samples, evals=evals
90+
)
91+
SUITE["allocation"]["StateEstimator"]["ExtendedKalmanFilter"]["updatestate!"] =
92+
@benchmarkable(
93+
updatestate!($ekf, $u, $y, $d),
94+
setup=preparestate!($ekf, $y, $d),
95+
samples=samples, evals=evals
96+
)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
G = [ tf(1.90, [18, 1]) tf(1.90, [18, 1]);
2+
tf(-0.74,[8, 1]) tf(0.74, [8, 1]) ]
3+
uop, yop = [20, 20], [50, 30]
4+
model = setop!(LinModel(G, 2.0); uop, yop)
5+
function test_mpc(mpc, plant)
6+
plant.x0 .= 0; y = plant()
7+
initstate!(mpc, plant.uop, y)
8+
N = 75; ry = [50, 30]; ul = 0
9+
U, Y, Ry = zeros(2, N), zeros(2, N), zeros(2, N)
10+
for i = 1:N
11+
i == 26 && (ry = [48, 35])
12+
i == 51 && (ul = -10)
13+
y = plant()
14+
preparestate!(mpc, y)
15+
u = mpc(ry)
16+
U[:,i], Y[:,i], Ry[:,i] = u, y, ry
17+
updatestate!(mpc, u, y)
18+
updatestate!(plant, u+[0,ul])
19+
end
20+
return U, Y, Ry
21+
end
22+
23+
## ----------------- Runtime benchmarks ---------------------------------------------
24+
optim = JuMP.Model(OSQP.Optimizer, add_bridges=false)
25+
transcription = SingleShooting()
26+
mpc_osqp_ss = setconstraint!(LinMPC(model; optim, transcription), ymin=[45, -Inf])
27+
JuMP.unset_time_limit_sec(mpc_osqp_ss.optim)
28+
29+
optim = JuMP.Model(OSQP.Optimizer, add_bridges=false)
30+
transcription = MultipleShooting()
31+
mpc_osqp_ms = setconstraint!(LinMPC(model; optim, transcription), ymin=[45, -Inf])
32+
JuMP.unset_time_limit_sec(mpc_osqp_ms.optim)
33+
34+
optim = JuMP.Model(DAQP.Optimizer, add_bridges=false)
35+
transcription = SingleShooting()
36+
mpc_daqp_ss = setconstraint!(LinMPC(model; optim, transcription), ymin=[45, -Inf])
37+
38+
optim = JuMP.Model(DAQP.Optimizer, add_bridges=false)
39+
transcription = MultipleShooting()
40+
mpc_daqp_ms = setconstraint!(LinMPC(model; optim, transcription), ymin=[45, -Inf])
41+
# needed to solve Hessians with eigenvalues at zero, like in MultipleShooting transcription:
42+
JuMP.set_attribute(mpc_daqp_ms.optim, "eps_prox", 1e-6)
43+
44+
samples, evals = 500, 1
45+
SUITE["runtime"]["PredictiveController"]["CSTR"]["LinMPC"]["OSQP"]["SingleShooting"] =
46+
@benchmarkable(test_mpc($mpc_osqp_ss, $model);
47+
samples=samples, evals=evals
48+
)
49+
SUITE["runtime"]["PredictiveController"]["CSTR"]["LinMPC"]["OSQP"]["MultipleShooting"] =
50+
@benchmarkable(test_mpc($mpc_osqp_ms, $model);
51+
samples=samples, evals=evals
52+
)
53+
SUITE["runtime"]["PredictiveController"]["CSTR"]["LinMPC"]["DAQP"]["SingleShooting"] =
54+
@benchmarkable(test_mpc($mpc_daqp_ss, $model);
55+
samples=samples, evals=evals
56+
)
57+
SUITE["runtime"]["PredictiveController"]["CSTR"]["LinMPC"]["DAQP"]["MultipleShooting"] =
58+
@benchmarkable(test_mpc($mpc_daqp_ms, $model);
59+
samples=samples, evals=evals
60+
)
61+
62+
63+
# ---------------------- Allocation benchmarks ------------------------------------------
64+
empc = ExplicitMPC(linmodel, Mwt=[1, 1], Nwt=[0.1, 0.1], Lwt=[0.1, 0.1])
65+
66+
samples, evals = 1, 1
67+
SUITE["allocation"]["PredictiveController"]["ExplicitMPC"]["moveinput!"] =
68+
@benchmarkable(
69+
moveinput!($empc, $y, $d),
70+
setup=preparestate!($empc, $y, $d),
71+
samples=samples, evals=evals
72+
)

0 commit comments

Comments
 (0)