acados_solver_ros2 main
Acados-based NMPC controllers for ROS2 control
Loading...
Searching...
No Matches
acados_solver.hpp
Go to the documentation of this file.
1// Copyright 2023 ICUBE Laboratory, University of Strasbourg
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Author: Thibault Poignonec (tpoignonec@unistra.fr)
16
17#ifndef ACADOS_SOLVER_BASE__ACADOS_SOLVER_HPP_
18#define ACADOS_SOLVER_BASE__ACADOS_SOLVER_HPP_
19
20#include <Eigen/Dense>
21
22#include <iostream>
23#include <vector>
24#include <exception>
25
28
29
30// Acados C interface
31#include "acados_c/ocp_nlp_interface.h"
32
33
34namespace acados
35{
36
42{
43public:
48 {
49public:
50 // Basic dimensions
51
53 unsigned int nx;
54
56 unsigned int nz;
57
59 unsigned int np;
60
62 unsigned int nu;
63
64 // Bounds on x and u
65
67 unsigned int nbx;
68
71 unsigned int nbx_0;
72
74 unsigned int nbx_N;
75
78 unsigned int nbu;
79
80 // Polytope constraints
81
83 unsigned int ng;
84
86 unsigned int ng_N;
87
88 // Non-linear constraints
89
91 unsigned int nh;
92
94 unsigned int nh_N;
95
97 unsigned int nphi;
98
100 unsigned int nphi_N;
101
102 // Slack-related dimensions
103
105 unsigned int ns;
106
108 unsigned int ns_N;
109
111 unsigned int nsbx;
112
114 unsigned int nsbx_N;
115
117 unsigned int nsbu;
118
120 unsigned int nsh;
121
123 unsigned int nsh_N;
124
126 unsigned int nsg;
127
129 unsigned int nsg_N;
130
131
133 unsigned int nsphi;
134
136 unsigned int nsphi_N;
137
138 // Cost ref.
140 unsigned int ny;
141
143 unsigned int ny_0;
144
146 unsigned int ny_N;
147
148 // Misc.
149
152 unsigned int nr;
153 };
154
155public:
156// Acados solver public API
157
167
174 virtual ~AcadosSolver();
175
176// Solver management
177
187 int init(unsigned int N, double Ts);
188
194 int reset();
195
196// Solve and introspection
197
210 int solve();
211
212// Setters
213
221
228 int set_initial_state_values(ValueMap const & x_0_map);
229
240 unsigned int stage,
241 IndexVector & idxbx,
242 ValueVector & lbx,
243 ValueVector & ubx);
244
256 IndexVector & idxbx,
257 ValueVector & lbx,
258 ValueVector & ubx);
259
270 unsigned int stage,
271 IndexVector & idxbu,
272 ValueVector & lbu,
273 ValueVector & ubu);
285 IndexVector & idxbu,
286 ValueVector & lbu,
287 ValueVector & ubu);
288
289// Runtime parameters
290
298 int set_runtime_parameters(unsigned int stage, ValueVector & p_i);
299
309 int set_runtime_parameters(unsigned int stage, ValueMap const & p_i_map);
310
311
321
331 int set_runtime_parameters(ValueMap const & p_i_map);
332
333// Initialization state
334
346 int initialize_state_values(unsigned int stage, ValueVector & x_i);
347
357 int initialize_state_values(unsigned int stage, ValueMap const & x_i_map);
358
366
373 int initialize_state_values(ValueMap const & x_i_map);
374
375// Initialization controls
376
384 int initialize_control_values(unsigned int stage, ValueVector & u_i);
385
393 int initialize_control_values(unsigned int stage, ValueMap const & u_i_map);
394
402
410
411// Getters
418 ValueVector get_state_values(unsigned int stage);
419
427
435
436
444
451 ValueVector get_control_values(unsigned int stage);
452
460
461// Getters variable mappings
462
468 const IndexMap & x_index_map() const;
469
475 const IndexMap & z_index_map() const;
476
482 const IndexMap & p_index_map() const;
483
489 const IndexMap & u_index_map() const;
490
491// Values map utils
492
502 IndexMap const & index_map,
503 unsigned int expected_total_indexes);
504
514 ValueMap const & values_map,
515 unsigned int expected_total_indexes);
516
527 static bool is_values_map_complete(IndexMap const & index_map, ValueMap const & values_map);
528
545 IndexMap const & index_map,
546 ValueMap const & values_map,
547 unsigned int vector_size,
548 ValueVector & values);
549
559 IndexMap const & index_map,
560 ValueMap const & values_map,
561 unsigned int vector_size);
562
572 IndexMap const & index_map,
573 ValueVector const & values);
574
575// Problem dimensions and convenience getters for commonly used attributes
576
582 const Dimensions & dims() const;
583
591 unsigned int nx() const;
592
600 unsigned int nz() const;
601
609 unsigned int np() const;
610
618 unsigned int nu() const;
619
625 unsigned int N() const;
626
634 double Ts() const;
635
636
642 std::vector<double> sampling_intervals() const;
643
644protected:
647
650
653
656
659
660// Solver-specific functions
661
663 virtual int create_index_maps() = 0;
664
665protected:
676
677// Imported Acados solver C-code interface ("internal" --> internal use only!)
678
687 virtual int internal_create_capsule() = 0;
688
699 virtual int internal_create_with_discretization(int n_time_steps, double * new_time_steps) = 0;
700
709 virtual int internal_reset(int reset_qp_solver_mem = 1) = 0;
710
719 virtual int internal_free() = 0;
720
728 virtual int internal_free_capsule() = 0;
729
738 virtual int internal_update_qp_solver_cond_N(int qp_solver_cond_N) = 0;
739
750 virtual int internal_update_params(unsigned int stage, double * value, int np) = 0;
751
764 unsigned int stage,
765 int * idx,
766 double * p,
767 int n_update) = 0;
768
776 virtual int internal_solve() = 0;
777
783 virtual void internal_print_stats() const = 0;
784
785public:
793 virtual ocp_nlp_in * get_nlp_in() const = 0;
794
802 virtual ocp_nlp_out * get_nlp_out() const = 0;
803
812 virtual ocp_nlp_out * get_sens_out() const = 0;
813
821 virtual ocp_nlp_solver * get_nlp_solver() const = 0;
822
830 virtual ocp_nlp_config * get_nlp_config() const = 0;
831
839 virtual void * get_nlp_opts() const = 0;
840
848 virtual ocp_nlp_dims * get_nlp_dims() const = 0;
849
857 virtual ocp_nlp_plan_t * get_nlp_plan() const = 0;
858
866 virtual unsigned int get_nlp_np() const = 0;
867
868// Imported Acados SIM solver C-code interface (for internal use only!)
869
870protected:
871 // TODO(tpoignonec) : add sim solver private c-interface
872
873public:
874 // TODO(tpoignonec) : add sim solver public c-interface
875
876 // Private own attributes
877
878private:
880 bool _is_initialized = false;
881
883 double _Ts = -1;
884// Runtime data
885
887 int _rti_phase = 0;
888};
889
890} // namespace acados
891
892#endif // ACADOS_SOLVER_BASE__ACADOS_SOLVER_HPP_
Container for the (fixed) dimensions of the imported Acados OCP.
Definition acados_solver.hpp:48
unsigned int nbx
Number of state stage bounds.
Definition acados_solver.hpp:67
unsigned int ns
Total number of slack variable.
Definition acados_solver.hpp:105
unsigned int nsphi
Number of slack variables used by convex-over-nonlinear constraints.
Definition acados_solver.hpp:133
unsigned int nu
Control vector size.
Definition acados_solver.hpp:62
unsigned int nphi
Number of "convex-over-nonlinear" constraints (see Acados documentation)
Definition acados_solver.hpp:97
unsigned int nsg
Number of slack variables used by polytopic constraints.
Definition acados_solver.hpp:126
unsigned int nbx_0
Number of initial state stage bounds.
Definition acados_solver.hpp:71
unsigned int ng
Number of polytopic constraints.
Definition acados_solver.hpp:83
unsigned int nphi_N
Number of "convex-over-nonlinear" constraints at stage N.
Definition acados_solver.hpp:100
unsigned int nsbx
Number of slack variables used by state bounds.
Definition acados_solver.hpp:111
unsigned int np
Runtime parameters vector size.
Definition acados_solver.hpp:59
unsigned int ny_0
Size of the reference vector at initial stage.
Definition acados_solver.hpp:143
unsigned int ny
Size of the reference vector.
Definition acados_solver.hpp:140
unsigned int nbx_N
Number of terminal state bounds.
Definition acados_solver.hpp:74
unsigned int nsbu
Number of slack variables used by control bounds.
Definition acados_solver.hpp:117
unsigned int nh_N
Number of non-linear constraints at stage N.
Definition acados_solver.hpp:94
unsigned int nsbx_N
Number of slack variables used by terminal state bounds.
Definition acados_solver.hpp:114
unsigned int ny_N
Size of the reference vector at terminal stage.
Definition acados_solver.hpp:146
unsigned int nh
Number of non-linear constraints.
Definition acados_solver.hpp:91
unsigned int nsg_N
Number of slack variables used by (terminal) polytopic constraints.
Definition acados_solver.hpp:129
unsigned int ng_N
Number of polytopic constraints at stage N.
Definition acados_solver.hpp:86
unsigned int nx
Differential state vector size.
Definition acados_solver.hpp:53
unsigned int nr
Dimensions of the image of the inner nonlinear function in positive definite constr....
Definition acados_solver.hpp:152
unsigned int ns_N
Total number of slack variable at stage N.
Definition acados_solver.hpp:108
unsigned int nsphi_N
Number of slack variables used by (terminal) convex-over-nonlinear constraints.
Definition acados_solver.hpp:136
unsigned int nz
Algebraic state vector size.
Definition acados_solver.hpp:56
unsigned int nsh_N
Number of slack variables used by (terminal) non-linear constraints.
Definition acados_solver.hpp:123
unsigned int nsh
Number of slack variables used by non-linear constraints.
Definition acados_solver.hpp:120
unsigned int nbu
Number of control bounds.
Definition acados_solver.hpp:78
Abstract C++ wrapper of generated Acados solver C-code.
Definition acados_solver.hpp:42
int initialize_control_values(ValueMap const &u_i_map)
Initialize the control variable values for ALL stages at once from a key/values map.
ValueVector get_algebraic_state_values(unsigned int stage)
Retrieve the algebraic state variables at a given stage.
IndexMap _z_index_map
Unordered map used to set or retrieve the values of algebraic state variables by name.
Definition acados_solver.hpp:652
int set_runtime_parameters(ValueVector &p_i)
Set the runtime parameters for all stages at once from ordered value vector.
int set_initial_state_values(ValueMap const &x_0_map)
Set the initial state values (i.e., add constraints on initial state) from a map of key/ValueVector p...
int set_control_bounds(IndexVector &idxbu, ValueVector &lbu, ValueVector &ubu)
Set control bounds for all stages.
ValueMap get_algebraic_state_values_as_map(unsigned int stage)
Retrieve the algebraic state variables at a given stage and package them as a ValueMap.
ValueVector get_control_values(unsigned int stage)
Retrieve the control variables at a given stage.
int solve()
Solve the non-linear optimization problem given the provided initial state, constraints,...
int initialize_state_values(ValueMap const &x_i_map)
Initialize the (differential) state values for ALL stages at once from a key/values map.
int initialize_state_values(ValueVector &x_i)
Initialize the (differential) state values for ALL stages at once from an ordered value vector.
const IndexMap & x_index_map() const
Returns a constant reference to the Key/IndexVector map of differential state variables internally st...
const IndexMap & u_index_map() const
Returns a constant reference to the Key/IndexVector map of control variables internally stored in Aca...
const IndexMap & z_index_map() const
Returns a constant reference to the Key/IndexVector map of algebraic state variables internally store...
virtual void internal_print_stats() const =0
Print the solver stats (e.g., number of SQP iters, number of QP iters, etc.) to the console.
virtual int internal_update_params_sparse(unsigned int stage, int *idx, double *p, int n_update)=0
A sparse implementation of internal_update_params_sparse().
virtual ocp_nlp_plan_t * get_nlp_plan() const =0
Legacy C-function to retrieve the C-interface ocp_nlp_plan property of the solver.
static bool is_values_map_complete(IndexMap const &index_map, ValueMap const &values_map)
Check if for each key-index pair contained in the index map there is a value provided by the value ma...
int set_state_bounds(unsigned int stage, IndexVector &idxbx, ValueVector &lbx, ValueVector &ubx)
Set (differential) state bounds at a given stage.
const IndexMap & p_index_map() const
Returns a constant reference to the Key/IndexVector map of runtime parameters internally stored in Ac...
static ValueVector value_vector_from_map(IndexMap const &index_map, ValueMap const &values_map, unsigned int vector_size)
Returns a vector containing the values provided through the value map.
int free_memory()
Free all the allocated memory.
virtual ocp_nlp_solver * get_nlp_solver() const =0
Legacy C-function to retrieve the C-interface ocp_nlp_solver property of the solver.
IndexMap _p_index_map
Unordered map used to set the values of runtime parameters by name.
Definition acados_solver.hpp:655
const Dimensions & dims() const
Returns the (fixed) dimensions of the OCP problem stored in AcadosSolver::_dims.
int initialize_control_values(unsigned int stage, ValueVector &u_i)
Initialize the control variable values for one stage from an ordered value vector.
int reset()
Resets the Acados solver, including the internal QP solver and the RTI phase cache.
unsigned int np() const
Returns the number of runtime parameters.
int set_state_bounds(IndexVector &idxbx, ValueVector &lbx, ValueVector &ubx)
Set (differential) state bounds for all stages.
virtual ocp_nlp_in * get_nlp_in() const =0
Legacy C-function to retrieve the C-interface ocp_nlp_in property of the solver.
virtual int internal_update_params(unsigned int stage, double *value, int np)=0
Internal update of the solver runtime parameters.
ValueMap get_control_values_as_map(unsigned int stage)
Retrieve the control variables at a given stage and package them as a ValueMap.
int initialize_control_values(unsigned int stage, ValueMap const &u_i_map)
Initialize the control variable values for one stage from a key/values map.
double Ts() const
Returns the (constant) sampling interval Ts (in seconds).
virtual int internal_create_with_discretization(int n_time_steps, double *new_time_steps)=0
Initialize Acados solver and (optionally) redefine the shooting nodes.
virtual int internal_free_capsule()=0
Free the memory allocated for the capsule.
virtual ocp_nlp_dims * get_nlp_dims() const =0
Legacy C-function to retrieve the C-interface ocp_nlp_dims property of the solver that contains the d...
static bool is_map_size_consistent(IndexMap const &index_map, unsigned int expected_total_indexes)
Check if the total number of indexes contained in the provided index map is equal to an expected numb...
int set_runtime_parameters(ValueMap const &p_i_map)
Set the runtime parameters for all stages at once from a key/values map.
static void fill_vector_from_map(IndexMap const &index_map, ValueMap const &values_map, unsigned int vector_size, ValueVector &values)
Fill (and resize if necessary) a vector with the values contained in the value map.
int initialize_state_values(unsigned int stage, ValueVector &x_i)
Initialize the (differential) state values for a stage from an ordered value vector.
virtual ocp_nlp_out * get_sens_out() const =0
Legacy C-function to retrieve the C-interface ocp_nlp_out property of the solver.
virtual int internal_reset(int reset_qp_solver_mem=1)=0
Reset the solver memory and (opt.) the internal QP solver.
int initialize_state_values(unsigned int stage, ValueMap const &x_i_map)
Initialize the (differential) state values for a stage from a key/values map.
virtual int internal_free()=0
Free the memory allocated for the solver.
virtual ocp_nlp_out * get_nlp_out() const =0
Legacy C-function to retrieve the C-interface ocp_nlp_out property of the solver.
unsigned int nz() const
Returns the dimensions of the algebraic state vector.
IndexMap _x_index_map
Unordered map used to set or retrieve the values of diff. state variables by name.
Definition acados_solver.hpp:649
int init(unsigned int N, double Ts)
Initialize the solver and set the (constant) sampling intervals.
virtual int internal_update_qp_solver_cond_N(int qp_solver_cond_N)=0
Update the QP solver qp_solver_cond_N setting.
static ValueMap create_map_from_values(IndexMap const &index_map, ValueVector const &values)
Returns a vector ValueMap object containing the values of the value vector.
Dimensions _dims
Fixed dimensions of the imported Acados OCP.
Definition acados_solver.hpp:646
virtual int create_index_maps()=0
Reinitialize the maps used to set or retrieve value using string variable names.
int set_control_bounds(unsigned int stage, IndexVector &idxbu, ValueVector &lbu, ValueVector &ubu)
Set control bounds at a given stage.
int set_initial_state_values(ValueVector &x_0)
Set the initial state values (i.e., add constraints on initial state)
IndexMap _u_index_map
Unordered map used to set or retrieve the values of control variables by name.
Definition acados_solver.hpp:658
unsigned int N() const
Returns the number of shooting nodes.
virtual int internal_solve()=0
Solve the non-linear optimization`.
int set_runtime_parameters(unsigned int stage, ValueVector &p_i)
Set the runtime parameters for one stage from ordered value vector.
AcadosSolver()
Constructor of the AcadosSolver object.
ValueVector get_state_values(unsigned int stage)
Retrieve the differential state variables at a given stage.
virtual ~AcadosSolver()
Destructor of the AcadosSolver object.
ValueMap get_state_values_as_map(unsigned int stage)
Retrieve the differential state variables at a given stage and package them as a ValueMap.
virtual int internal_create_capsule()=0
Create the Acados solver's capsule used by the Acados C-interface.
virtual ocp_nlp_config * get_nlp_config() const =0
Legacy C-function to retrieve the C-interface ocp_nlp_config property of the solver.
virtual unsigned int get_nlp_np() const =0
Legacy C-function to retrieve the number of runtime parameters from the Acados capsule.
static bool is_map_size_consistent(ValueMap const &values_map, unsigned int expected_total_indexes)
Check if the total number of values contained in the provided value map is equal to an expected numbe...
unsigned int nx() const
Returns the dimensions of the differential state vector.
virtual void * get_nlp_opts() const =0
Legacy C-function to retrieve the C-interface get_nlp_opts property of the solver.
int set_runtime_parameters(unsigned int stage, ValueMap const &p_i_map)
Set the runtime parameters for one stage from a key/values map.
int initialize_control_values(ValueVector &u_i)
Initialize the control variable values for ALL stages at once from an ordered value vector.
unsigned int nu() const
Returns the number of control variables.
std::vector< double > sampling_intervals() const
Returns the vector of sampling interval (in seconds).
Definition acados_solver.hpp:35
std::unordered_map< std::string, IndexVector > IndexMap
Mapping between keys (std::string) and indexes (acados::IndexVector).
Definition acados_types.hpp:36
std::vector< unsigned int > IndexVector
List of indexes.
Definition acados_types.hpp:30
std::vector< double > ValueVector
List of values (double).
Definition acados_types.hpp:33
std::unordered_map< std::string, ValueVector > ValueMap
Mapping between keys (std::string) and data (acados::ValueVector).
Definition acados_types.hpp:39