Using x_pre to add in the current x constraints

Hello! I’d like to set a constraint, and it’s a path constraint. That is to say, each x(k) is constrained by an equation involving x(k - 1). How can I define this problem? I find that it’s not easy for me to set up the constraint function.

I use this strategy to update the parameter p. However, if I do it this way, I won’t be able to solve it all at once:

    for i in range(20):
        # Update triangle parameters
        acados_solver.set(i, "p", np.array([x_prev[0], x_prev[1], theta_prev, L]))

        # Solve the OCP
        status = acados_solver.solve()

        if status != 0:
            print(f"Solver failed at step {i} with status {status}")
            break

        # Retrieve current state and update x_prev and theta_prev
        x_prev = acados_solver.get(i, "x")
        theta_prev = np.arctan2(acados_solver.get(i, "u")[1], acados_solver.get(i, "u")[0])

Hi, is the Index k here referring to the stage or the control problem instant in an NMPC scheme?

Hi kaethe, Index k refers to the time step or stage within the prediction horizon.
The possible method that occurred to me is to expand the state space and then store xk and xk-1. However, I don’t know how to define this model.

Yes that is the main idea!

If you use discrete dynamics, this is very simple. The augmented model is simply \tilde{x}_k = (x_k, x_{k-1}) with \tilde{x}_{k+1} = \begin{bmatrix} f(x_k, u_k) \\ x_k \end{bmatrix}.

If you use an integrator, then you need to reformulate the original dynamics \dot{x} = f(x, u) as follows:
\dot{\tilde{x}} = \begin{bmatrix} v \\ \frac{\partial f}{\partial t}(x, u) \end{bmatrix} where \tilde{x} = (x, v). Often you want to do the same augmentation for the controls (i.e. include a = \dot{u} in your state vector).

Best, Katrin

1 Like

Hello! Kaethe, thank you for your response.

I am currently facing a dilemma in choosing the type of path constraint: whether to use the h type or the g type. The h type represents nonlinear constraints, while the g type represents linear ones. I noticed that in the OCP interface, the method constraints_set(stage_, field_, value_, api='warn') allows setting parameters for path constraints, where p corresponds to h type constraints and C and D correspond to g type constraints.

So, does this mean that when defining h type constraints, I need to set p to make the constraints different at each step? Similarly, for g type constraints, I need to set C and D.

However, I have another concern. If the constraint is nonlinear and the mathematical model of the constraint differs at each step, it seems impossible to define a new constraint function merely by changing p. This is because con_h_expr is defined globally in the AcadosModel, and it cannot be redefined for a specific stage. Therefore, I am wondering if, in scenarios where the time horizon is divided into segments (e.g., with a step size of 20, where h is h1 for the first 10 steps and h2 for the next 10 steps), this approach would not work.

Yes nonlinear constraints might be adapted stagewise by setting the parameters p. If you need structurally different constraints, you can use a multi-phase formulation.

See preprint and example.

Thank u ! Kaethe! It is a fantastic framework!

1 Like