Pipal  1.2.0
Penalty Interior-Point ALgorithm
Loading...
Searching...
No Matches
Problem.hxx
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
2 * Copyright (c) 2025, Davide Stocco and Enrico Bertolazzi. *
3 * *
4 * The Pipal project is distributed under the MIT License. *
5 * *
6 * Davide Stocco Enrico Bertolazzi *
7 * University of Trento University of Trento *
8 * e-mail: davide.stocco@unitn.it e-mail: enrico.bertolazzi@unitn.it *
9\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10
11#pragma once
12
13#ifndef INCLUDE_PIPAL_PROBLEM_HXX
14#define INCLUDE_PIPAL_PROBLEM_HXX
15
16namespace Pipal {
17
26 template <typename Real>
27 class Problem
28 {
29 static_assert(std::is_floating_point_v<Real>,
30 "Pipal::Problem<Real>: Real must be a floating-point type.");
31
32 std::string m_name{"(Unnamed Pipal Problem)"};
33
34 public:
35 using UniquePtr = std::unique_ptr<Problem>;
36
43 Problem() = default;
44
49 Problem(std::string t_name) : m_name(std::move(t_name)) {};
50
55 Problem(Problem const &) = delete;
56
61 Problem & operator=(Problem const &) = delete;
62
67 Problem(Problem &&) = delete;
68
73 Problem & operator=(Problem &&) = delete;
74
78 virtual ~Problem() = default;
79
84 std::string const & name() const {return this->m_name;}
85
90 void name(std::string const & t_name) {this->m_name = t_name;}
91
98 virtual bool objective(Vector<Real> const & x, Real & out) const = 0;
99
106 virtual bool objective_gradient(Vector<Real> const & x, Vector<Real> & out) const = 0;
107
114 virtual bool constraints(Vector<Real> const & x, Vector<Real> & out) const = 0;
115
122 virtual bool constraints_jacobian(Vector<Real> const & x, SparseMatrix<Real> & out) const = 0;
123
131 virtual bool lagrangian_hessian(Vector<Real> const & x, Vector<Real> const & l, SparseMatrix<Real> & out) const = 0;
132
138 virtual bool primal_lower_bounds(Vector<Real> & out) const = 0;
139
145 virtual bool primal_upper_bounds(Vector<Real> & out) const = 0;
146
152 virtual bool constraints_lower_bounds(Vector<Real> & out) const = 0;
153
159 virtual bool constraints_upper_bounds(Vector<Real> & out) const = 0;
160
161 }; // class Problem
162
170 template <typename Real>
171 class ProblemWrapper : public Problem<Real>
172 {
173 public:
174 using ObjectiveFunc = std::function<bool(Vector<Real> const &, Real &)>;
175 using ConstraintsFunc = std::function<bool(Vector<Real> const &, Vector<Real> &)>;
176 using ObjectiveGradientFunc = std::function<bool(Vector<Real> const &, Vector<Real> &)>;
177 using ConstraintsJacobianFunc = std::function<bool(Vector<Real> const &, SparseMatrix<Real> &)>;
178 using LagrangianHessianFunc = std::function<bool(Vector<Real> const &, Vector<Real> const &, SparseMatrix<Real> &)>;
179 using BoundsFunc = std::function<bool(Vector<Real> &)>;
180
181 private:
182 // Problem functions
188
189 // Bounds functions
194
195 public:
213 std::string const & t_name,
214 ObjectiveFunc const & t_objective,
215 ObjectiveGradientFunc const & t_objective_gradient,
216 ConstraintsFunc const & t_constraints,
217 ConstraintsJacobianFunc const & t_constraints_jacobian,
218 LagrangianHessianFunc const & t_lagrangian_hessian,
219 BoundsFunc const & t_primal_lower_bounds,
220 BoundsFunc const & t_primal_upper_bounds,
221 BoundsFunc const & t_constraints_lower_bounds,
222 BoundsFunc const & t_constraints_upper_bounds
223 )
224 : Problem<Real>(t_name)
225 , m_objective(t_objective)
226 , m_objective_gradient(t_objective_gradient)
227 , m_constraints(t_constraints)
228 , m_constraints_jacobian(t_constraints_jacobian)
229 , m_lagrangian_hessian(t_lagrangian_hessian)
230 , m_primal_lower_bounds(t_primal_lower_bounds)
231 , m_primal_upper_bounds(t_primal_upper_bounds)
232 , m_constraints_lower_bounds(t_constraints_lower_bounds)
233 , m_constraints_upper_bounds(t_constraints_upper_bounds)
234 {}
235
239 ~ProblemWrapper() override {};
240
245 ObjectiveFunc & objective() {return this->m_objective;}
246
252 {
253 this->m_objective = objective;
254 }
255
260 ObjectiveGradientFunc & objective_gradient() {return this->m_objective_gradient;}
261
267 {
268 this->m_objective_gradient = objective_gradient;
269 }
270
275 ConstraintsFunc & constraints() {return this->m_constraints;}
276
282 {
283 this->m_constraints = constraints;
284 }
285
290 ConstraintsJacobianFunc & constraints_jacobian() {return this->m_constraints_jacobian;}
291
297 {
298 this->m_constraints_jacobian = constraints_jacobian;
299 }
300
305 BoundsFunc & primal_lower_bounds() {return this->m_primal_lower_bounds;}
306
312 {
313 this->m_primal_lower_bounds = primal_lower_bounds;
314 }
315
320 BoundsFunc & primal_upper_bounds() {return this->m_primal_upper_bounds;}
321
327 {
328 this->m_primal_upper_bounds = primal_upper_bounds;
329 }
330
335 BoundsFunc & constraints_lower_bounds() {return this->m_constraints_lower_bounds;}
336
342 {
343 this->m_constraints_lower_bounds = constraints_lower_bounds;
344 }
345
350 BoundsFunc & constraints_upper_bounds() {return this->m_constraints_upper_bounds;}
351
357 {
358 this->m_constraints_upper_bounds = constraints_upper_bounds;
359 }
360
365 LagrangianHessianFunc & lagrangian_hessian() {return this->m_lagrangian_hessian;}
366
372 {
373 this->m_lagrangian_hessian = lagrangian_hessian;
374 }
375
382 bool objective(Vector<Real> const & x, Real & out) const override
383 {
384 return this->m_objective(x, out);
385 }
386
393 bool objective_gradient(Vector<Real> const & x, Vector<Real> & out) const override
394 {
395 return this->m_objective_gradient(x, out);
396 }
397
404 bool constraints(Vector<Real> const & x, Vector<Real> & out) const override
405 {
406 return this->m_constraints(x, out);
407 }
408
415 bool constraints_jacobian(Vector<Real> const & x, SparseMatrix<Real> & out) const override
416 {
417 return this->m_constraints_jacobian(x, out);
418 }
419
428 const override
429 {
430 return this->m_lagrangian_hessian(x, l, out);
431 }
432
438 bool primal_lower_bounds(Vector<Real> & out) const override
439 {
440 return this->m_primal_lower_bounds(out);
441 }
442
448 bool primal_upper_bounds(Vector<Real> & out) const override
449 {
450 return this->m_primal_upper_bounds(out);
451 }
452
458 bool constraints_lower_bounds(Vector<Real> & out) const override
459 {
460 return this->m_constraints_lower_bounds(out);
461 }
462
468 bool constraints_upper_bounds(Vector<Real> & out) const override
469 {
470 return this->m_constraints_upper_bounds(out);
471 }
472
473 }; // class ProblemWrapper
474
475} // namespace Pipal
476
477#endif // INCLUDE_PIPAL_PROBLEM_HXX
virtual bool primal_lower_bounds(Vector< Real > &out) const =0
Lower bounds on the primal variables.
virtual ~Problem()=default
Default destructor.
virtual bool lagrangian_hessian(Vector< Real > const &x, Vector< Real > const &l, SparseMatrix< Real > &out) const =0
Evaluate the Hessian of the Lagrangian function with respect to the primal variables.
std::unique_ptr< Problem > UniquePtr
Definition Problem.hxx:35
std::string m_name
Definition Problem.hxx:32
void name(std::string const &t_name)
Set the name of the optimization problem.
Definition Problem.hxx:90
Problem(std::string t_name)
Problem constructor.
Definition Problem.hxx:49
Problem & operator=(Problem const &)=delete
Deleted assignment operator.
virtual bool constraints_lower_bounds(Vector< Real > &out) const =0
Lower bounds on the constraints.
virtual bool primal_upper_bounds(Vector< Real > &out) const =0
Upper bounds on the primal variables.
virtual bool objective(Vector< Real > const &x, Real &out) const =0
Evaluate the objective function.
virtual bool constraints_upper_bounds(Vector< Real > &out) const =0
Upper bounds on the constraints.
virtual bool constraints(Vector< Real > const &x, Vector< Real > &out) const =0
Evaluate the constraints function.
Problem(Problem &&)=delete
Deleted move constructor.
Problem(Problem const &)=delete
Deleted copy constructor.
virtual bool constraints_jacobian(Vector< Real > const &x, SparseMatrix< Real > &out) const =0
Evaluate the Jacobian of the constraints function with respect to the primal variables.
Problem & operator=(Problem &&)=delete
Deleted move assignment operator.
Problem()=default
Default constructor.
std::string const & name() const
Get the name of the optimization problem.
Definition Problem.hxx:84
virtual bool objective_gradient(Vector< Real > const &x, Vector< Real > &out) const =0
Evaluate the gradient of the objective function.
bool constraints_upper_bounds(Vector< Real > &out) const override
Upper bounds on the constraints.
Definition Problem.hxx:468
std::function< bool(Vector< Real > &)> BoundsFunc
Definition Problem.hxx:179
void primal_lower_bounds(BoundsFunc const &primal_lower_bounds)
Set the lower bounds on the primal variables function.
Definition Problem.hxx:311
BoundsFunc & constraints_lower_bounds()
Get the lower bounds on the constraints function.
Definition Problem.hxx:335
void constraints_upper_bounds(BoundsFunc const &constraints_upper_bounds)
Set the upper bounds on the constraints function.
Definition Problem.hxx:356
LagrangianHessianFunc m_lagrangian_hessian
Definition Problem.hxx:187
ConstraintsJacobianFunc m_constraints_jacobian
Definition Problem.hxx:186
bool constraints_lower_bounds(Vector< Real > &out) const override
Lower bounds on the constraints.
Definition Problem.hxx:458
void objective_gradient(ObjectiveGradientFunc const &objective_gradient)
Set the gradient of the objective function.
Definition Problem.hxx:266
BoundsFunc m_constraints_lower_bounds
Definition Problem.hxx:192
void lagrangian_hessian(LagrangianHessianFunc const &lagrangian_hessian)
Set the Hessian of the Lagrangian function.
Definition Problem.hxx:371
std::function< bool(Vector< Real > const &, Vector< Real > &)> ObjectiveGradientFunc
Definition Problem.hxx:176
bool primal_lower_bounds(Vector< Real > &out) const override
Lower bounds on the primal variables.
Definition Problem.hxx:438
bool constraints_jacobian(Vector< Real > const &x, SparseMatrix< Real > &out) const override
Evaluate the Jacobian of the constraints function.
Definition Problem.hxx:415
BoundsFunc m_primal_lower_bounds
Definition Problem.hxx:190
bool lagrangian_hessian(Vector< Real > const &x, Vector< Real > const &l, SparseMatrix< Real > &out) const override
Evaluate the Hessian of the Lagrangian function.
Definition Problem.hxx:427
void constraints_lower_bounds(BoundsFunc const &constraints_lower_bounds)
Set the lower bounds on the constraints function.
Definition Problem.hxx:341
ConstraintsFunc m_constraints
Definition Problem.hxx:185
std::function< bool(Vector< Real > const &, Vector< Real > const &, SparseMatrix< Real > &)> LagrangianHessianFunc
Definition Problem.hxx:178
void constraints(ConstraintsFunc const &constraints)
Set the constraints function.
Definition Problem.hxx:281
std::function< bool(Vector< Real > const &, Real &)> ObjectiveFunc
Definition Problem.hxx:174
void constraints_jacobian(ConstraintsJacobianFunc const &constraints_jacobian)
Set the Jacobian of the constraints function.
Definition Problem.hxx:296
BoundsFunc m_constraints_upper_bounds
Definition Problem.hxx:193
ProblemWrapper(std::string const &t_name, ObjectiveFunc const &t_objective, ObjectiveGradientFunc const &t_objective_gradient, ConstraintsFunc const &t_constraints, ConstraintsJacobianFunc const &t_constraints_jacobian, LagrangianHessianFunc const &t_lagrangian_hessian, BoundsFunc const &t_primal_lower_bounds, BoundsFunc const &t_primal_upper_bounds, BoundsFunc const &t_constraints_lower_bounds, BoundsFunc const &t_constraints_upper_bounds)
Constructor for the ProblemWrapper class (without the Hessian of the Lagrangian).
Definition Problem.hxx:212
ObjectiveGradientFunc m_objective_gradient
Definition Problem.hxx:184
ObjectiveFunc & objective()
Get the objective function.
Definition Problem.hxx:245
~ProblemWrapper() override
Default destructor for the ProblemWrapper class.
Definition Problem.hxx:239
ObjectiveFunc m_objective
Definition Problem.hxx:183
ObjectiveGradientFunc & objective_gradient()
Get the gradient of the objective function.
Definition Problem.hxx:260
void objective(ObjectiveFunc const &objective)
Set the objective function.
Definition Problem.hxx:251
ConstraintsJacobianFunc & constraints_jacobian()
Get the Jacobian of the constraints function.
Definition Problem.hxx:290
void primal_upper_bounds(BoundsFunc const &primal_upper_bounds)
Set the upper bounds on the primal variables function.
Definition Problem.hxx:326
BoundsFunc m_primal_upper_bounds
Definition Problem.hxx:191
bool constraints(Vector< Real > const &x, Vector< Real > &out) const override
Evaluate the constraints function.
Definition Problem.hxx:404
LagrangianHessianFunc & lagrangian_hessian()
Get the Hessian of the Lagrangian function.
Definition Problem.hxx:365
BoundsFunc & constraints_upper_bounds()
Get the upper bounds on the constraints function.
Definition Problem.hxx:350
BoundsFunc & primal_upper_bounds()
Get the upper bounds on the primal variables function.
Definition Problem.hxx:320
BoundsFunc & primal_lower_bounds()
Get the lower bounds on the primal variables function.
Definition Problem.hxx:305
bool objective_gradient(Vector< Real > const &x, Vector< Real > &out) const override
Evaluate the gradient of the objective function.
Definition Problem.hxx:393
std::function< bool(Vector< Real > const &, Vector< Real > &)> ConstraintsFunc
Definition Problem.hxx:175
ConstraintsFunc & constraints()
Get the constraints function.
Definition Problem.hxx:275
bool objective(Vector< Real > const &x, Real &out) const override
Evaluate the objective function.
Definition Problem.hxx:382
bool primal_upper_bounds(Vector< Real > &out) const override
Upper bounds on the primal variables.
Definition Problem.hxx:448
std::function< bool(Vector< Real > const &, SparseMatrix< Real > &)> ConstraintsJacobianFunc
Definition Problem.hxx:177
Namespace for the Pipal library.
Definition Acceptance.hxx:16
Eigen::SparseMatrix< Real > SparseMatrix
Definition Types.hxx:114
Eigen::Vector< Real, Eigen::Dynamic > Vector
Definition Types.hxx:112