Skip to content

Commit 6fdbce5

Browse files
authored
Merge pull request #275 from JuliaControl/exact_hessian
added: `hessian` keyword argument in `NonLinMPC`
2 parents b9f325f + df1bb37 commit 6fdbce5

File tree

8 files changed

+349
-91
lines changed

8 files changed

+349
-91
lines changed

benchmark/2_bench_state_estim.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,21 +335,21 @@ JuMP.set_attribute(mhe_pendulum_madnlp_pred.optim, "tol", 1e-7)
335335
samples, evals, seconds = 25, 1, 15*60
336336
CASE_ESTIM["Pendulum"]["MovingHorizonEstimator"]["Ipopt"]["Current form"] =
337337
@benchmarkable(
338-
sim!($mhe_pendulum_ipopt_curr, $N, $u; plant=$plant, x_0=$x_0, x̂_0=$x̂_0),
338+
sim!($mhe_pendulum_ipopt_curr, $N, $u; plant=$plant, x_0=$x_0, x̂_0=$x̂_0, progress=false),
339339
samples=samples, evals=evals, seconds=seconds
340340
)
341341
CASE_ESTIM["Pendulum"]["MovingHorizonEstimator"]["Ipopt"]["Prediction form"] =
342342
@benchmarkable(
343-
sim!($mhe_pendulum_ipopt_pred, $N, $u; plant=$plant, x_0=$x_0, x̂_0=$x̂_0),
343+
sim!($mhe_pendulum_ipopt_pred, $N, $u; plant=$plant, x_0=$x_0, x̂_0=$x̂_0, progress=false),
344344
samples=samples, evals=evals, seconds=seconds
345345
)
346346
CASE_ESTIM["Pendulum"]["MovingHorizonEstimator"]["MadNLP"]["Current form"] =
347347
@benchmarkable(
348-
sim!($mhe_pendulum_madnlp_curr, $N, $u; plant=$plant, x_0=$x_0, x̂_0=$x̂_0),
348+
sim!($mhe_pendulum_madnlp_curr, $N, $u; plant=$plant, x_0=$x_0, x̂_0=$x̂_0, progress=false),
349349
samples=samples, evals=evals, seconds=seconds
350350
)
351351
CASE_ESTIM["Pendulum"]["MovingHorizonEstimator"]["MadNLP"]["Prediction form"] =
352352
@benchmarkable(
353-
sim!($mhe_pendulum_madnlp_pred, $N, $u; plant=$plant, x_0=$x_0, x̂_0=$x̂_0),
353+
sim!($mhe_pendulum_madnlp_pred, $N, $u; plant=$plant, x_0=$x_0, x̂_0=$x̂_0, progress=false),
354354
samples=samples, evals=evals, seconds=seconds
355355
)

benchmark/3_bench_predictive_control.jl

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,18 @@ nmpc_nonlin_ss = NonLinMPC(
4646
nonlinmodel, transcription=SingleShooting(),
4747
Mwt=[1, 1], Nwt=[0.1, 0.1], Lwt=[0.1, 0.1], Hp=10
4848
)
49+
nmpc_nonlin_ss_hess = NonLinMPC(
50+
nonlinmodel, transcription=SingleShooting(), hessian=true,
51+
Mwt=[1, 1], Nwt=[0.1, 0.1], Lwt=[0.1, 0.1], Hp=10
52+
)
4953
nmpc_nonlin_ms = NonLinMPC(
5054
nonlinmodel, transcription=MultipleShooting(),
5155
Mwt=[1, 1], Nwt=[0.1, 0.1], Lwt=[0.1, 0.1], Hp=10
5256
)
57+
nmpc_nonlin_ms_hess = NonLinMPC(
58+
nonlinmodel, transcription=MultipleShooting(), hessian=true,
59+
Mwt=[1, 1], Nwt=[0.1, 0.1], Lwt=[0.1, 0.1], Hp=10
60+
)
5361
nmpc_nonlin_tc = NonLinMPC(
5462
nonlinmodel_c, transcription=TrapezoidalCollocation(),
5563
Mwt=[1], Nwt=[0.1], Lwt=[0.1], Hp=10
@@ -74,12 +82,24 @@ UNIT_MPC["NonLinMPC"]["moveinput!"]["NonLinModel"]["SingleShooting"] =
7482
setup=preparestate!($nmpc_nonlin_ss, $y, $d),
7583
samples=samples, evals=evals, seconds=seconds
7684
)
85+
UNIT_MPC["NonLinMPC"]["moveinput!"]["NonLinModel"]["SingleShootingHessian"] =
86+
@benchmarkable(
87+
moveinput!($nmpc_nonlin_ss, $y, $d),
88+
setup=preparestate!($nmpc_nonlin_ss_hess, $y, $d),
89+
samples=samples, evals=evals, seconds=seconds
90+
)
7791
UNIT_MPC["NonLinMPC"]["moveinput!"]["NonLinModel"]["MultipleShooting"] =
7892
@benchmarkable(
7993
moveinput!($nmpc_nonlin_ms, $y, $d),
8094
setup=preparestate!($nmpc_nonlin_ms, $y, $d),
8195
samples=samples, evals=evals, seconds=seconds
8296
)
97+
UNIT_MPC["NonLinMPC"]["moveinput!"]["NonLinModel"]["MultipleShootingHessian"] =
98+
@benchmarkable(
99+
moveinput!($nmpc_nonlin_ms, $y, $d),
100+
setup=preparestate!($nmpc_nonlin_ms_hess, $y, $d),
101+
samples=samples, evals=evals, seconds=seconds
102+
)
83103
UNIT_MPC["NonLinMPC"]["moveinput!"]["NonLinModel"]["TrapezoidalCollocation"] =
84104
@benchmarkable(
85105
moveinput!($nmpc_nonlin_tc, $y_c, $d_c),
@@ -258,12 +278,24 @@ nmpc_ipopt_ss = NonLinMPC(estim; Hp, Hc, Mwt, Nwt, Cwt, optim, transcription)
258278
nmpc_ipopt_ss = setconstraint!(nmpc_ipopt_ss; umin, umax)
259279
JuMP.unset_time_limit_sec(nmpc_ipopt_ss.optim)
260280

281+
optim = JuMP.Model(optimizer_with_attributes(Ipopt.Optimizer,"sb"=>"yes"), add_bridges=false)
282+
transcription, hessian = SingleShooting(), true
283+
nmpc_ipopt_ss_hess = NonLinMPC(estim; Hp, Hc, Mwt, Nwt, Cwt, optim, transcription, hessian)
284+
nmpc_ipopt_ss_hess = setconstraint!(nmpc_ipopt_ss_hess; umin, umax)
285+
JuMP.unset_time_limit_sec(nmpc_ipopt_ss_hess.optim)
286+
261287
optim = JuMP.Model(optimizer_with_attributes(Ipopt.Optimizer,"sb"=>"yes"), add_bridges=false)
262288
transcription = MultipleShooting()
263289
nmpc_ipopt_ms = NonLinMPC(estim; Hp, Hc, Mwt, Nwt, Cwt, optim, transcription)
264290
nmpc_ipopt_ms = setconstraint!(nmpc_ipopt_ms; umin, umax)
265291
JuMP.unset_time_limit_sec(nmpc_ipopt_ms.optim)
266292

293+
optim = JuMP.Model(optimizer_with_attributes(Ipopt.Optimizer,"sb"=>"yes"), add_bridges=false)
294+
transcription, hessian = MultipleShooting(), true
295+
nmpc_ipopt_ms_hess = NonLinMPC(estim; Hp, Hc, Mwt, Nwt, Cwt, optim, transcription, hessian)
296+
nmpc_ipopt_ms_hess = setconstraint!(nmpc_ipopt_ms_hess; umin, umax)
297+
JuMP.unset_time_limit_sec(nmpc_ipopt_ms_hess.optim)
298+
267299
optim = JuMP.Model(optimizer_with_attributes(Ipopt.Optimizer,"sb"=>"yes"), add_bridges=false)
268300
transcription = MultipleShooting(f_threads=true)
269301
nmpc_ipopt_mst = NonLinMPC(estim; Hp, Hc, Mwt, Nwt, Cwt, optim, transcription)
@@ -305,32 +337,42 @@ JuMP.unset_time_limit_sec(nmpc_madnlp_ss.optim)
305337
samples, evals, seconds = 100, 1, 15*60
306338
CASE_MPC["Pendulum"]["NonLinMPC"]["Noneconomic"]["Ipopt"]["SingleShooting"] =
307339
@benchmarkable(
308-
sim!($nmpc_ipopt_ss, $N, $ry; plant=$plant, x_0=$x_0, x̂_0=$x̂_0),
340+
sim!($nmpc_ipopt_ss, $N, $ry; plant=$plant, x_0=$x_0, x̂_0=$x̂_0, progress=false),
341+
samples=samples, evals=evals, seconds=seconds
342+
)
343+
CASE_MPC["Pendulum"]["NonLinMPC"]["Noneconomic"]["Ipopt"]["SingleShooting (Hessian)"] =
344+
@benchmarkable(
345+
sim!($nmpc_ipopt_ss_hess, $N, $ry; plant=$plant, x_0=$x_0, x̂_0=$x̂_0, progress=false),
309346
samples=samples, evals=evals, seconds=seconds
310347
)
311348
CASE_MPC["Pendulum"]["NonLinMPC"]["Noneconomic"]["Ipopt"]["MultipleShooting"] =
312349
@benchmarkable(
313-
sim!($nmpc_ipopt_ms, $N, $ry; plant=$plant, x_0=$x_0, x̂_0=$x̂_0),
350+
sim!($nmpc_ipopt_ms, $N, $ry; plant=$plant, x_0=$x_0, x̂_0=$x̂_0, progress=false),
351+
samples=samples, evals=evals, seconds=seconds
352+
)
353+
CASE_MPC["Pendulum"]["NonLinMPC"]["Noneconomic"]["Ipopt"]["MultipleShooting (Hessian)"] =
354+
@benchmarkable(
355+
sim!($nmpc_ipopt_ms_hess, $N, $ry; plant=$plant, x_0=$x_0, x̂_0=$x̂_0, progress=false),
314356
samples=samples, evals=evals, seconds=seconds
315357
)
316358
CASE_MPC["Pendulum"]["NonLinMPC"]["Noneconomic"]["Ipopt"]["MultipleShooting (threaded)"] =
317359
@benchmarkable(
318-
sim!($nmpc_ipopt_mst, $N, $ry; plant=$plant, x_0=$x_0, x̂_0=$x̂_0),
360+
sim!($nmpc_ipopt_mst, $N, $ry; plant=$plant, x_0=$x_0, x̂_0=$x̂_0, progress=false),
319361
samples=samples, evals=evals, seconds=seconds
320362
)
321363
CASE_MPC["Pendulum"]["NonLinMPC"]["Noneconomic"]["Ipopt"]["TrapezoidalCollocation"] =
322364
@benchmarkable(
323-
sim!($nmpc_ipopt_tc, $N, $ry; plant=$plant, x_0=$x_0, x̂_0=$x̂_0),
365+
sim!($nmpc_ipopt_tc, $N, $ry; plant=$plant, x_0=$x_0, x̂_0=$x̂_0, progress=false),
324366
samples=samples, evals=evals, seconds=seconds
325367
)
326368
CASE_MPC["Pendulum"]["NonLinMPC"]["Noneconomic"]["Ipopt"]["TrapezoidalCollocation (threaded)"] =
327369
@benchmarkable(
328-
sim!($nmpc_ipopt_tct, $N, $ry; plant=$plant, x_0=$x_0, x̂_0=$x̂_0),
370+
sim!($nmpc_ipopt_tct, $N, $ry; plant=$plant, x_0=$x_0, x̂_0=$x̂_0, progress=false),
329371
samples=samples, evals=evals, seconds=seconds
330372
)
331373
CASE_MPC["Pendulum"]["NonLinMPC"]["Noneconomic"]["MadNLP"]["SingleShooting"] =
332374
@benchmarkable(
333-
sim!($nmpc_madnlp_ss, $N, $ry; plant=$plant, x_0=$x_0, x̂_0=$x̂_0),
375+
sim!($nmpc_madnlp_ss, $N, $ry; plant=$plant, x_0=$x_0, x̂_0=$x̂_0, progress=false),
334376
samples=samples, evals=evals, seconds=seconds
335377
)
336378

@@ -376,22 +418,22 @@ JuMP.unset_time_limit_sec(empc_madnlp_ss.optim)
376418
samples, evals, seconds = 100, 1, 15*60
377419
CASE_MPC["Pendulum"]["NonLinMPC"]["Economic"]["Ipopt"]["SingleShooting"] =
378420
@benchmarkable(
379-
sim!($empc_ipopt_ss, $N, $ry; plant=$plant2, x_0=$x_0, x̂_0=$x̂_0),
421+
sim!($empc_ipopt_ss, $N, $ry; plant=$plant2, x_0=$x_0, x̂_0=$x̂_0, progress=false),
380422
samples=samples, evals=evals, seconds=seconds
381423
)
382424
CASE_MPC["Pendulum"]["NonLinMPC"]["Economic"]["Ipopt"]["MultipleShooting"] =
383425
@benchmarkable(
384-
sim!($empc_ipopt_ms, $N, $ry; plant=$plant2, x_0=$x_0, x̂_0=$x̂_0),
426+
sim!($empc_ipopt_ms, $N, $ry; plant=$plant2, x_0=$x_0, x̂_0=$x̂_0, progress=false),
385427
samples=samples, evals=evals, seconds=seconds
386428
)
387429
CASE_MPC["Pendulum"]["NonLinMPC"]["Economic"]["Ipopt"]["TrapezoidalCollocation"] =
388430
@benchmarkable(
389-
sim!($empc_ipopt_tc, $N, $ry; plant=$plant2, x_0=$x_0, x̂_0=$x̂_0),
431+
sim!($empc_ipopt_tc, $N, $ry; plant=$plant2, x_0=$x_0, x̂_0=$x̂_0, progress=false),
390432
samples=samples, evals=evals, seconds=seconds
391433
)
392434
CASE_MPC["Pendulum"]["NonLinMPC"]["Economic"]["MadNLP"]["SingleShooting"] =
393435
@benchmarkable(
394-
sim!($empc_madnlp_ss, $N, $ry; plant=$plant2, x_0=$x_0, x̂_0=$x̂_0),
436+
sim!($empc_madnlp_ss, $N, $ry; plant=$plant2, x_0=$x_0, x̂_0=$x̂_0, progress=false),
395437
samples=samples, evals=evals, seconds=seconds
396438
)
397439

@@ -442,17 +484,17 @@ JuMP.unset_time_limit_sec(nmpc2_ipopt_tc.optim)
442484
samples, evals, seconds = 100, 1, 15*60
443485
CASE_MPC["Pendulum"]["NonLinMPC"]["Custom constraints"]["Ipopt"]["SingleShooting"] =
444486
@benchmarkable(
445-
sim!($nmpc2_ipopt_ss, $N, $ry; plant=$plant2, x_0=$x_0, x̂_0=$x̂_0),
487+
sim!($nmpc2_ipopt_ss, $N, $ry; plant=$plant2, x_0=$x_0, x̂_0=$x̂_0, progress=false),
446488
samples=samples, evals=evals, seconds=seconds
447489
)
448490
CASE_MPC["Pendulum"]["NonLinMPC"]["Custom constraints"]["Ipopt"]["MultipleShooting"] =
449491
@benchmarkable(
450-
sim!($nmpc2_ipopt_ms, $N, $ry; plant=$plant2, x_0=$x_0, x̂_0=$x̂_0),
492+
sim!($nmpc2_ipopt_ms, $N, $ry; plant=$plant2, x_0=$x_0, x̂_0=$x̂_0, progress=false),
451493
samples=samples, evals=evals, seconds=seconds
452494
)
453495
CASE_MPC["Pendulum"]["NonLinMPC"]["Custom constraints"]["Ipopt"]["TrapezoidalCollocation"] =
454496
@benchmarkable(
455-
sim!($nmpc2_ipopt_tc, $N, $ry; plant=$plant2, x_0=$x_0, x̂_0=$x̂_0),
497+
sim!($nmpc2_ipopt_tc, $N, $ry; plant=$plant2, x_0=$x_0, x̂_0=$x̂_0, progress=false),
456498
samples=samples, evals=evals, seconds=seconds
457499
)
458500

docs/src/internals/predictive_control.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ ModelPredictiveControl.relaxterminal
2424
ModelPredictiveControl.init_quadprog
2525
ModelPredictiveControl.init_stochpred
2626
ModelPredictiveControl.init_matconstraint_mpc
27-
ModelPredictiveControl.get_nonlinops(::NonLinMPC, ::ModelPredictiveControl.GenericModel)
27+
ModelPredictiveControl.get_nonlinobj_op(::NonLinMPC, ::ModelPredictiveControl.GenericModel)
28+
ModelPredictiveControl.get_nonlincon_oracle(::NonLinMPC, ::ModelPredictiveControl.GenericModel)
2829
```
2930

3031
## Update Quadratic Optimization

src/ModelPredictiveControl.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ using Random: randn
66

77
using RecipesBase
88

9-
using DifferentiationInterface: ADTypes.AbstractADType, AutoForwardDiff, AutoSparse
10-
using DifferentiationInterface: gradient!, jacobian!, prepare_gradient, prepare_jacobian
11-
using DifferentiationInterface: value_and_gradient!, value_and_jacobian!
9+
using DifferentiationInterface: ADTypes.AbstractADType, AutoForwardDiff
10+
using DifferentiationInterface: AutoSparse, SecondOrder
11+
using DifferentiationInterface: gradient!, value_and_gradient!, prepare_gradient
12+
using DifferentiationInterface: jacobian!, value_and_jacobian!, prepare_jacobian
13+
using DifferentiationInterface: hessian!, value_gradient_and_hessian!, prepare_hessian
1214
using DifferentiationInterface: Constant, Cache
1315
using SparseConnectivityTracer: TracerSparsityDetector
1416
using SparseMatrixColorings: GreedyColoringAlgorithm, sparsity_pattern

0 commit comments

Comments
 (0)