Warm start in c_generated code

Hi :wave:

I’m using python interface and c_generated_code.
In python, I set the warm start option as below.

Code
ocp.solver_options.qp_solver_warm_start = 1

In c_generated_code, however, I’m not sure that the warm start appropriately works.

To use warm start in c_generated_code, should I assign the previous optimal solution to x_init?
Or not, let me know how to use or implement warm start in c_generated_code.

Here is my code snippet in c, generated automatically.

int main()
{

    pp_mpcc_solver_capsule *acados_ocp_capsule = pp_mpcc_acados_create_capsule();
    // there is an opportunity to change the number of shooting intervals in C without new code generation
    int N = PP_MPCC_N;
    // allocate the array and fill it accordingly
    double* new_time_steps = NULL;
    int status = pp_mpcc_acados_create_with_discretization(acados_ocp_capsule, N, new_time_steps);

    if (status)
    {
        printf("pp_mpcc_acados_create() returned status %d. Exiting.\n", status);
        exit(1);
    }

    ocp_nlp_config *nlp_config = pp_mpcc_acados_get_nlp_config(acados_ocp_capsule);
    ocp_nlp_dims *nlp_dims = pp_mpcc_acados_get_nlp_dims(acados_ocp_capsule);
    ocp_nlp_in *nlp_in = pp_mpcc_acados_get_nlp_in(acados_ocp_capsule);
    ocp_nlp_out *nlp_out = pp_mpcc_acados_get_nlp_out(acados_ocp_capsule);
    ocp_nlp_solver *nlp_solver = pp_mpcc_acados_get_nlp_solver(acados_ocp_capsule);
    void *nlp_opts = pp_mpcc_acados_get_nlp_opts(acados_ocp_capsule);

    // initial condition
    int idxbx0[NBX0];
    idxbx0[0] = 0;
    idxbx0[1] = 1;
    idxbx0[2] = 2;
    idxbx0[3] = 3;
    idxbx0[4] = 4;
    idxbx0[5] = 5;
    idxbx0[6] = 6;
    idxbx0[7] = 7;

    double lbx0[NBX0];
    double ubx0[NBX0];
    lbx0[0] = 0;
    ubx0[0] = 0;
    lbx0[1] = 0;
    ubx0[1] = 0;
    lbx0[2] = 0;
    ubx0[2] = 0;
    lbx0[3] = 50;
    ubx0[3] = 50;
    lbx0[4] = 0;
    ubx0[4] = 0;
    lbx0[5] = 0;
    ubx0[5] = 0;
    lbx0[6] = 0;
    ubx0[6] = 0;
    lbx0[7] = 0;
    ubx0[7] = 0;

    ocp_nlp_constraints_model_set(nlp_config, nlp_dims, nlp_in, 0, "idxbx", idxbx0);
    ocp_nlp_constraints_model_set(nlp_config, nlp_dims, nlp_in, 0, "lbx", lbx0);
    ocp_nlp_constraints_model_set(nlp_config, nlp_dims, nlp_in, 0, "ubx", ubx0);

    // initialization for state values
    double x_init[NX];
    x_init[0] = 0.0;
    x_init[1] = 0.0;
    x_init[2] = 0.0;
    x_init[3] = 0.0;
    x_init[4] = 0.0;
    x_init[5] = 0.0;
    x_init[6] = 0.0;
    x_init[7] = 0.0;

    // initial value for control input
    double u0[NU];
    u0[0] = 0.0;
    u0[1] = 0.0;
    // set parameters
    double p[NP];
    p[0] = 0;

    for (int ii = 0; ii <= N; ii++)
    {
        pp_mpcc_acados_update_params(acados_ocp_capsule, ii, p, NP);
    }
  

    // prepare evaluation
    int NTIMINGS = 1;
    double min_time = 1e12;
    double kkt_norm_inf;
    double elapsed_time;
    int sqp_iter;

    double xtraj[NX * (N+1)];
    double utraj[NU * N];


    // solve ocp in loop
    int rti_phase = 0;

    for (int ii = 0; ii < NTIMINGS; ii++)
    {
        // initialize solution
        for (int i = 0; i < N; i++)
        {
            ocp_nlp_out_set(nlp_config, nlp_dims, nlp_out, i, "x", x_init);
            ocp_nlp_out_set(nlp_config, nlp_dims, nlp_out, i, "u", u0);
        }
        ocp_nlp_out_set(nlp_config, nlp_dims, nlp_out, N, "x", x_init);
        ocp_nlp_solver_opts_set(nlp_config, nlp_opts, "rti_phase", &rti_phase);
        status = pp_mpcc_acados_solve(acados_ocp_capsule);
        ocp_nlp_get(nlp_config, nlp_solver, "time_tot", &elapsed_time);
        min_time = MIN(elapsed_time, min_time);
    }

    /* print solution and statistics */
    for (int ii = 0; ii <= nlp_dims->N; ii++)
        ocp_nlp_out_get(nlp_config, nlp_dims, nlp_out, ii, "x", &xtraj[ii*NX]);
    for (int ii = 0; ii < nlp_dims->N; ii++)
        ocp_nlp_out_get(nlp_config, nlp_dims, nlp_out, ii, "u", &utraj[ii*NU]);

    printf("\n--- xtraj ---\n");
    d_print_exp_tran_mat( NX, N+1, xtraj, NX);
    printf("\n--- utraj ---\n");
    d_print_exp_tran_mat( NU, N, utraj, NU );
    // ocp_nlp_out_print(nlp_solver->dims, nlp_out);

    printf("\nsolved ocp %d times, solution printed above\n\n", NTIMINGS);

    if (status == ACADOS_SUCCESS)
    {
        printf("pp_mpcc_acados_solve(): SUCCESS!\n");
    }
    else
    {
        printf("pp_mpcc_acados_solve() failed with status %d.\n", status);
    }

    // get solution
    ocp_nlp_out_get(nlp_config, nlp_dims, nlp_out, 0, "kkt_norm_inf", &kkt_norm_inf);
    ocp_nlp_get(nlp_config, nlp_solver, "sqp_iter", &sqp_iter);

    pp_mpcc_acados_print_stats(acados_ocp_capsule);

    printf("\nSolver info:\n");
    printf(" SQP iterations %2d\n minimum time for %d solve %f [ms]\n KKT %e\n",
           sqp_iter, NTIMINGS, min_time*1000, kkt_norm_inf);

    // free solver
    status = pp_mpcc_acados_free(acados_ocp_capsule);
    if (status) {
        printf("pp_mpcc_acados_free() returned status %d. \n", status);
    }
    // free solver capsule
    status = pp_mpcc_acados_free_capsule(acados_ocp_capsule);
    if (status) {
        printf("pp_mpcc_acados_free_capsule() returned status %d. \n", status);
    }

    return status;
}

These hints should help other people to help you :handshake:
Thanks for using acados :pray:

1 Like