def get_sparsity_in(self, i): n = nlpsol_out(i) if n == "f": return Sparsity.scalar() elif n in ("x", "lam_x"): return Sparsity.dense(self.nx) elif n in ("g", "lam_g"): return Sparsity.dense(self.ng) else: return Sparsity(0, 0)
def get_sparsity_in(self, i: int) -> tuple: """ Get the sparsity of a specific variable Parameters ---------- i: int The index of the variable Returns ------- The sparsity of the variable """ n = nlpsol_out(i) if n == "f": return Sparsity.scalar() elif n in ("x", "lam_x"): return Sparsity.dense(self.nx) elif n in ("g", "lam_g"): return Sparsity.dense(self.ng) else: return Sparsity(0, 0)
ocp.cost.cost_type_e = 'EXTERNAL' W_u = 1e-3 theta = model.x[1] ocp.model.cost_expr_ext_cost = tanh(theta)**2 + .5 * (model.x[0]**2 + W_u * model.u**2) ocp.model.cost_expr_ext_cost_e = tanh(theta)**2 + .5 * model.x[0]**2 custom_hess_u = W_u J = horzcat(SX.eye(2), SX(2, 2)) print(DM(J.sparsity())) # diagonal matrix with second order terms of outer loss function. D = SX.sym('D', Sparsity.diag(2)) D[0, 0] = 1 [hess_tan, grad_tan] = hessian(tanh(theta)**2, theta) D[1, 1] = if_else(theta == 0, hess_tan, grad_tan / theta) custom_hess_x = J.T @ D @ J zeros = SX(1, nx) cost_expr_ext_cost_custom_hess = blockcat(custom_hess_u, zeros, zeros.T, custom_hess_x) cost_expr_ext_cost_custom_hess_e = custom_hess_x ocp.model.cost_expr_ext_cost_custom_hess = cost_expr_ext_cost_custom_hess ocp.model.cost_expr_ext_cost_custom_hess_e = cost_expr_ext_cost_custom_hess_e # set constraints