Biorbd Release_1.13.0
Loading...
Searching...
No Matches
StaticOptimizationIpopt.h
1#ifndef BIORBD_MUSCLES_STATIC_OPTIMIZATION_IPOPT_H
2#define BIORBD_MUSCLES_STATIC_OPTIMIZATION_IPOPT_H
3
4#include "biorbdConfig.h"
5
6#include <IpIpoptApplication.hpp>
7#include <IpTNLP.hpp>
8#include <memory>
9
10namespace BIORBD_NAMESPACE {
11class Model;
12
13namespace utils {
14class Vector;
15}
16
17namespace rigidbody {
21} // namespace rigidbody
22
23namespace internal_forces {
24namespace muscles {
25class State;
29class BIORBD_API StaticOptimizationIpopt : public Ipopt::TNLP {
30 public:
45 Model& model,
48 const rigidbody::GeneralizedTorque& torqueTarget,
49 const utils::Vector& activationInit,
50 bool useResidual = true,
51 unsigned int pNormFactor = 2,
52 int verbose = 0,
53 double eps = 1e-8);
54
59
69 virtual bool get_nlp_info(
70 Ipopt::Index& n,
71 Ipopt::Index& m,
72 Ipopt::Index& nnz_jac_g,
73 Ipopt::Index& nnz_h_lag,
74 IndexStyleEnum& index_style);
75
86 virtual bool get_bounds_info(
87 Ipopt::Index n,
88 Ipopt::Number* x_l,
89 Ipopt::Number* x_u,
90 Ipopt::Index m,
91 Ipopt::Number* g_l,
92 Ipopt::Number* g_u);
93
108 virtual bool get_starting_point(
109 Ipopt::Index n,
110 bool init_x,
111 Ipopt::Number* x,
112 bool init_z,
113 Ipopt::Number* z_L,
114 Ipopt::Number* z_U,
115 Ipopt::Index m,
116 bool init_lambda,
117 Ipopt::Number* lambda);
118
119 // Method to return the objective value
128 virtual bool eval_f(
129 Ipopt::Index n,
130 const Ipopt::Number* x,
131 bool new_x,
132 Ipopt::Number& obj_value);
133
142 virtual bool eval_grad_f(
143 Ipopt::Index n,
144 const Ipopt::Number* x,
145 bool new_x,
146 Ipopt::Number* grad_f);
147
157 virtual bool eval_g(
158 Ipopt::Index n,
159 const Ipopt::Number* x,
160 bool new_x,
161 Ipopt::Index m,
162 Ipopt::Number* g);
163
176 virtual bool eval_jac_g(
177 Ipopt::Index n,
178 const Ipopt::Number* x,
179 bool new_x,
180 Ipopt::Index m,
181 Ipopt::Index nele_jac,
182 Ipopt::Index* iRow,
183 Ipopt::Index* jCol,
184 Ipopt::Number* values);
185
200 virtual void finalize_solution(
201 Ipopt::SolverReturn status,
202 Ipopt::Index n,
203 const Ipopt::Number* x,
204 const Ipopt::Number* z_L,
205 const Ipopt::Number* z_U,
206 Ipopt::Index m,
207 const Ipopt::Number* g,
208 const Ipopt::Number* lambda,
209 Ipopt::Number obj_value,
210 const Ipopt::IpoptData* ip_data,
211 Ipopt::IpoptCalculatedQuantities* ip_cq);
212
219
226
227 protected:
228 static const double m_activationLowerBound;
229 static const double m_activationUpperBound;
230
232 std::shared_ptr<unsigned int>
234 std::shared_ptr<unsigned int>
236 std::shared_ptr<unsigned int> m_nbMus;
237 std::shared_ptr<unsigned int> m_nbDof;
238 std::shared_ptr<unsigned int> m_nbTorque;
239 std::shared_ptr<unsigned int>
241 std::shared_ptr<double> m_eps;
242 std::shared_ptr<utils::Vector> m_activations;
243 std::shared_ptr<rigidbody::GeneralizedCoordinates>
245 std::shared_ptr<rigidbody::GeneralizedVelocity>
247 std::shared_ptr<rigidbody::GeneralizedTorque>
249 std::shared_ptr<utils::Vector> m_torqueResidual;
250 std::shared_ptr<double> m_torquePonderation;
251 std::shared_ptr<std::vector<std::shared_ptr<State>>>
253 std::shared_ptr<unsigned int> m_pNormFactor;
254 std::shared_ptr<int> m_verbose;
255 std::shared_ptr<utils::Vector> m_finalSolution;
256 std::shared_ptr<utils::Vector> m_finalResidual;
257
262 void dispatch(const Ipopt::Number* x);
263};
264
265} // namespace muscles
266} // namespace internal_forces
267} // namespace BIORBD_NAMESPACE
268
269#endif // BIORBD_MUSCLES_OPTIMIZATION_IPOPT_H
The actual musculoskeletal model that holds everything in biorbd.
EMG holder to interact with the muscle.
Definition State.h:17
virtual bool eval_f(Ipopt::Index n, const Ipopt::Number *x, bool new_x, Ipopt::Number &obj_value)
Compute the objective value from a set of variables.
std::shared_ptr< unsigned int > m_nbQdot
The number of generalized velocities.
std::shared_ptr< utils::Vector > m_torqueResidual
The torque residual.
std::shared_ptr< std::vector< std::shared_ptr< State > > > m_states
The muscle states.
void dispatch(const Ipopt::Number *x)
To dispatch the variables into biorbd format.
std::shared_ptr< unsigned int > m_nbTorque
The number of torques to match.
virtual bool eval_g(Ipopt::Index n, const Ipopt::Number *x, bool new_x, Ipopt::Index m, Ipopt::Number *g)
Method to return the constraint residuals.
std::shared_ptr< unsigned int > m_nbMus
The number of muscles.
std::shared_ptr< double > m_eps
Precision of the finite differentiate.
virtual bool get_nlp_info(Ipopt::Index &n, Ipopt::Index &m, Ipopt::Index &nnz_jac_g, Ipopt::Index &nnz_h_lag, IndexStyleEnum &index_style)
Get info about the NLP.
std::shared_ptr< unsigned int > m_pNormFactor
The p-norm factor.
std::shared_ptr< unsigned int > m_nbTorqueResidual
The number of torque residual.
StaticOptimizationIpopt(Model &model, const rigidbody::GeneralizedCoordinates &Q, const rigidbody::GeneralizedVelocity &Qdot, const rigidbody::GeneralizedTorque &torqueTarget, const utils::Vector &activationInit, bool useResidual=true, unsigned int pNormFactor=2, int verbose=0, double eps=1e-8)
Construct an Ipopt static optimization problem.
virtual bool get_starting_point(Ipopt::Index n, bool init_x, Ipopt::Number *x, bool init_z, Ipopt::Number *z_L, Ipopt::Number *z_U, Ipopt::Index m, bool init_lambda, Ipopt::Number *lambda)
Return the starting point for the algorithm.
std::shared_ptr< rigidbody::GeneralizedVelocity > m_Qdot
The generalized velocities.
virtual void finalize_solution(Ipopt::SolverReturn status, Ipopt::Index n, const Ipopt::Number *x, const Ipopt::Number *z_L, const Ipopt::Number *z_U, Ipopt::Index m, const Ipopt::Number *g, const Ipopt::Number *lambda, Ipopt::Number obj_value, const Ipopt::IpoptData *ip_data, Ipopt::IpoptCalculatedQuantities *ip_cq)
Finalize the optimization.
std::shared_ptr< unsigned int > m_nbQ
The number of generalized coordinates.
std::shared_ptr< rigidbody::GeneralizedCoordinates > m_Q
The generalized coordinates.
std::shared_ptr< utils::Vector > m_finalResidual
The final residual.
virtual bool get_bounds_info(Ipopt::Index n, Ipopt::Number *x_l, Ipopt::Number *x_u, Ipopt::Index m, Ipopt::Number *g_l, Ipopt::Number *g_u)
Return the bounds for my problem.
std::shared_ptr< rigidbody::GeneralizedTorque > m_torqueTarget
The torque to match.
virtual bool eval_jac_g(Ipopt::Index n, const Ipopt::Number *x, bool new_x, Ipopt::Index m, Ipopt::Index nele_jac, Ipopt::Index *iRow, Ipopt::Index *jCol, Ipopt::Number *values)
Return the jacobian of contraints.
virtual bool eval_grad_f(Ipopt::Index n, const Ipopt::Number *x, bool new_x, Ipopt::Number *grad_f)
Return the gradient of the objective.
std::shared_ptr< double > m_torquePonderation
The torque ponderation.
std::shared_ptr< unsigned int > m_nbDof
The number of degrees of freedom.
std::shared_ptr< utils::Vector > m_finalSolution
The final solution.
Wrapper of the Eigen VectorXd.
Definition Vector.h:21