Skip to content

Commit

Permalink
Minimal example: use GN Hessian, improve warning (acados#1135)
Browse files Browse the repository at this point in the history
  • Loading branch information
FreyJo authored Jun 14, 2024
1 parent e91626c commit 3e91fb8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
20 changes: 13 additions & 7 deletions examples/acados_python/getting_started/minimal_example_ocp.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from acados_template import AcadosOcp, AcadosOcpSolver
from pendulum_model import export_pendulum_ode_model
import numpy as np
import casadi as ca
from utils import plot_pendulum

def main():
Expand All @@ -52,16 +53,21 @@ def main():
# set prediction horizon
ocp.solver_options.tf = Tf

# set cost
# cost matrices
Q_mat = 2*np.diag([1e3, 1e3, 1e-2, 1e-2])
R_mat = 2*np.diag([1e-2])

# the 'EXTERNAL' cost type can be used to define general cost terms
# NOTE: This leads to additional (exact) hessian contributions when using GAUSS_NEWTON hessian.
ocp.cost.cost_type = 'EXTERNAL'
ocp.cost.cost_type_e = 'EXTERNAL'
ocp.model.cost_expr_ext_cost = model.x.T @ Q_mat @ model.x + model.u.T @ R_mat @ model.u
ocp.model.cost_expr_ext_cost_e = model.x.T @ Q_mat @ model.x
# path cost
ocp.cost.cost_type = 'NONLINEAR_LS'
ocp.model.cost_y_expr = ca.vertcat(model.x, model.u)
ocp.cost.yref = np.zeros((nx+nu,))
ocp.cost.W = ca.diagcat(Q_mat, R_mat).full()

# terminal cost
ocp.cost.cost_type_e = 'NONLINEAR_LS'
ocp.cost.yref_e = np.zeros((nx,))
ocp.model.cost_y_expr_e = model.x
ocp.cost.W_e = Q_mat

# set constraints
Fmax = 80
Expand Down
2 changes: 1 addition & 1 deletion interfaces/acados_template/acados_template/acados_ocp.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def make_consistent(self) -> None:
if opts.hessian_approx == 'GAUSS_NEWTON' and opts.ext_cost_num_hess == 0 and model.cost_expr_ext_cost_custom_hess_0 is None:
print("\nWARNING: Gauss-Newton Hessian approximation with EXTERNAL cost type not possible!\n"
"got cost_type_0: EXTERNAL, hessian_approx: 'GAUSS_NEWTON.'\n"
"GAUSS_NEWTON hessian is only supported for cost_types [NON]LINEAR_LS.\n"
"GAUSS_NEWTON hessian is not defined for EXTERNAL cost formulation.\n"
"If you continue, acados will proceed computing the exact hessian for the cost term.\n"
"Note: There is also the option to use the external cost module with a numerical hessian approximation (see `ext_cost_num_hess`).\n"
"OR the option to provide a symbolic custom hessian approximation (see `cost_expr_ext_cost_custom_hess`).\n")
Expand Down

0 comments on commit 3e91fb8

Please sign in to comment.