simple_optimization_problem.h
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement
4  *
5  * Copyright (c) 2020,
6  * TU Dortmund - Institute of Control Theory and Systems Engineering.
7  * All rights reserved.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  *
22  * Authors: Christoph Rösmann
23  *********************************************************************/
24 
25 #ifndef SRC_OPTIMIZATION_INCLUDE_CORBO_OPTIMIZATION_SIMPLE_OPTIMIZATION_PROBLEM_H_
26 #define SRC_OPTIMIZATION_INCLUDE_CORBO_OPTIMIZATION_SIMPLE_OPTIMIZATION_PROBLEM_H_
27 
29 
30 #include <functional>
31 
32 namespace corbo {
33 
51 class SimpleOptimizationProblem : public OptimizationProblemInterface
52 {
53  public:
55  explicit SimpleOptimizationProblem(int parameter_dim)
56  {
58  resizeParameterVector(parameter_dim);
59  }
60 
63 
65  int getNonLsqObjectiveDimension() override = 0;
66  int getLsqObjectiveDimension() override = 0;
67  int getObjectiveDimension() override = 0;
69  int getEqualityDimension() override = 0;
71  int getInequalityDimension() override = 0;
72 
74 
77 
78  // implements interface method
80 
81  // implements interface method
82  double computeValueNonLsqObjective() override = 0;
83 
84  // implements interface method
85  void computeValuesEquality(Eigen::Ref<Eigen::VectorXd> values) override = 0;
86 
87  // implements interface method
88  void computeValuesInequality(Eigen::Ref<Eigen::VectorXd> values) override = 0;
89 
91 
94 
100  void resizeParameterVector(int parameter_dim);
101 
103 
106 
107  void setX(const Eigen::Ref<const Eigen::VectorXd>& x) { _x = x; }
108  Eigen::VectorXd& getXRef() { return _x; }
109  const Eigen::VectorXd& getX() const { return _x; }
110 
111  void setLowerBounds(const Eigen::Ref<const Eigen::VectorXd>& lb) { _lb = lb; }
112  Eigen::VectorXd& getLowerBoundsRef() { return _lb; }
113  const Eigen::VectorXd& getLowerBounds() const { return _lb; }
114 
115  void setUpperBounds(const Eigen::Ref<const Eigen::VectorXd>& ub) { _ub = ub; }
116  Eigen::VectorXd& getUpperBoundsRef() { return _ub; }
117  const Eigen::VectorXd& getUpperBounds() const { return _ub; }
118 
119  // implements interface method
120  double getParameterValue(int idx) override { return _x[idx]; }
121  // implements interface method
122  void setParameterValue(int idx, double x) override { _x[idx] = x; }
123 
124  // implements interface method
126  // implements interface method
128  // implements interface method
129  double getLowerBound(int idx) override { return _lb[idx]; }
130  // implements interface method
131  double getUpperBound(int idx) override { return _ub[idx]; }
132  // implements interface method
133  void setLowerBound(int idx, double lb) override { _lb[idx] = lb; }
134  // implements interface method
135  void setUpperBound(int idx, double ub) override { _ub[idx] = ub; }
136 
138  void getParameterVector(Eigen::Ref<Eigen::VectorXd> x) override { x = _x; }
140  void setParameterVector(const Eigen::Ref<const Eigen::VectorXd>& x) override { setX(x); }
141 
143 
146 
147  // implements interface method
148  int getParameterDimension() override { return _x.size(); }
149 
150  // implements interface method
151  void applyIncrement(const Eigen::Ref<const Eigen::VectorXd>& increment) override { _x += increment; }
152  // implements interface method
153  void applyIncrement(int idx, double increment) override { _x[idx] += increment; }
154 
155  // implements interface method
156  void backupParameters() override { _x_backup.push_back(_x); }
157  // implements interface method
158  void restoreBackupParameters(bool keep_backup) override;
159  // implements interface method
160  void discardBackupParameters(bool all = false) override
161  {
162  _x_backup.pop_back();
163  if (all)
164  {
165  while (!_x_backup.empty()) _x_backup.pop_back();
166  }
167  }
168 
170 
173 
174  // implements interface method
175  bool isLeastSquaresProblem() const override = 0;
176 
178 
179  private:
180  Eigen::VectorXd _x;
181  Eigen::VectorXd _lb;
182  Eigen::VectorXd _ub;
183 
184  std::vector<Eigen::VectorXd> _x_backup;
185 };
186 
204 {
205  public:
209  explicit SimpleOptimizationProblemWithCallbacks(int param_dim) : SimpleOptimizationProblem(param_dim) {}
210 
213 
232  void setObjectiveFunction(std::function<void(const Eigen::VectorXd&, Eigen::Ref<Eigen::VectorXd>)> obj_fun, int obj_dim, bool lsq_form = false);
233 
248  void setEqualityConstraint(std::function<void(const Eigen::VectorXd&, Eigen::Ref<Eigen::VectorXd>)> eq_fun, int eq_dim);
249 
264  void setInequalityConstraint(std::function<void(const Eigen::VectorXd&, Eigen::Ref<Eigen::VectorXd>)> ineq_fun, int ineq_dim);
265 
267 
268  // implements interface method
269  int getNonLsqObjectiveDimension() override { return (!_lsq_form && _obj_dim > 0) ? 1 : 0; }
270  // implements interface method
271  int getLsqObjectiveDimension() override { return _lsq_form ? _obj_dim : 0; }
272  // implements interface method
273  int getObjectiveDimension() override { return _obj_dim > 0 ? 1 : 0; }
274  // implements interface method
275  int getEqualityDimension() override { return _eq_dim; }
276  // implements interface method
277  int getInequalityDimension() override { return _ineq_dim; }
278 
279  // implements interface method
280  bool isLeastSquaresProblem() const override { return _lsq_form; }
281 
282  // implmements interface method
283  double computeValueNonLsqObjective() override
284  {
285  if (!_obj_fun || _lsq_form) return 0;
286  Eigen::VectorXd values(getNonLsqObjectiveDimension());
287  _obj_fun(getX(), values);
288  return values.sum();
289  }
290 
291  // implements interface method
293  {
294  if (_obj_fun) _obj_fun(getX(), values);
295  }
296 
297  // implements interface method
299  {
300  if (_eq_fun) _eq_fun(getX(), values);
301  }
302 
303  // implements interface method
305  {
306  if (_ineq_fun) _ineq_fun(getX(), values);
307  }
308 
309  private:
310  int _obj_dim = 0;
311  int _eq_dim = 0;
312  int _ineq_dim = 0;
313  bool _lsq_form = false;
314 
315  std::function<void(const Eigen::VectorXd&, Eigen::Ref<Eigen::VectorXd>)> _obj_fun;
316  std::function<void(const Eigen::VectorXd&, Eigen::Ref<Eigen::VectorXd>)> _eq_fun;
317  std::function<void(const Eigen::VectorXd&, Eigen::Ref<Eigen::VectorXd>)> _ineq_fun;
318 };
319 
320 } // namespace corbo
321 
322 #endif // SRC_OPTIMIZATION_INCLUDE_CORBO_OPTIMIZATION_SIMPLE_OPTIMIZATION_PROBLEM_H_
corbo::SimpleOptimizationProblemWithCallbacks
Simple optimization problem formulation (callback based configuration)
Definition: simple_optimization_problem.h:225
corbo::SimpleOptimizationProblem::getInequalityDimension
int getInequalityDimension() override=0
Total dimension of general inequality constraints.
corbo::SimpleOptimizationProblem::_lb
Eigen::VectorXd _lb
Definition: simple_optimization_problem.h:225
corbo::SimpleOptimizationProblem::getEqualityDimension
int getEqualityDimension() override=0
Total dimension of equality constraints.
corbo::SimpleOptimizationProblem::getLowerBounds
const Eigen::VectorXd & getLowerBounds() const
Definition: simple_optimization_problem.h:157
corbo::SimpleOptimizationProblem::_ub
Eigen::VectorXd _ub
Definition: simple_optimization_problem.h:226
corbo::SimpleOptimizationProblemWithCallbacks::getEqualityDimension
int getEqualityDimension() override
Total dimension of equality constraints.
Definition: simple_optimization_problem.h:297
corbo::SimpleOptimizationProblem::isLeastSquaresProblem
bool isLeastSquaresProblem() const override=0
Check if the underlying problem is defined in the least squares form.
corbo
Definition: communication/include/corbo-communication/utilities.h:37
corbo::SimpleOptimizationProblem::setLowerBound
void setLowerBound(int idx, double lb) override
Set specific lower bound of a parameter.
Definition: simple_optimization_problem.h:177
corbo::OptimizationProblemInterface::_warn_if_not_specialized
bool _warn_if_not_specialized
Definition: optimization_problem_interface.h:672
corbo::SimpleOptimizationProblemWithCallbacks::setObjectiveFunction
void setObjectiveFunction(std::function< void(const Eigen::VectorXd &, Eigen::Ref< Eigen::VectorXd >)> obj_fun, int obj_dim, bool lsq_form=false)
Set objective function callback.
Definition: simple_optimization_problem.cpp:78
corbo::SimpleOptimizationProblem
Simple optimization problem formulation.
Definition: simple_optimization_problem.h:73
corbo::SimpleOptimizationProblem::getBounds
void getBounds(Eigen::Ref< Eigen::VectorXd > lb, Eigen::Ref< Eigen::VectorXd > ub) override
Get lower and upper bound vector.
Definition: simple_optimization_problem.cpp:66
corbo::SimpleOptimizationProblem::getUpperBounds
const Eigen::VectorXd & getUpperBounds() const
Definition: simple_optimization_problem.h:161
corbo::SimpleOptimizationProblemWithCallbacks::_eq_fun
std::function< void(const Eigen::VectorXd &, Eigen::Ref< Eigen::VectorXd >)> _eq_fun
Definition: simple_optimization_problem.h:338
corbo::SimpleOptimizationProblem::computeValueNonLsqObjective
double computeValueNonLsqObjective() override=0
corbo::SimpleOptimizationProblemWithCallbacks::isLeastSquaresProblem
bool isLeastSquaresProblem() const override
Check if the underlying problem is defined in the least squares form.
Definition: simple_optimization_problem.h:302
corbo::SimpleOptimizationProblemWithCallbacks::getNonLsqObjectiveDimension
int getNonLsqObjectiveDimension() override
Total dimension of objective function terms.
Definition: simple_optimization_problem.h:291
corbo::SimpleOptimizationProblem::getObjectiveDimension
int getObjectiveDimension() override=0
Get dimension of the objective (should be zero or one, includes Lsq objectives if present)
corbo::SimpleOptimizationProblemWithCallbacks::computeValuesLsqObjective
void computeValuesLsqObjective(Eigen::Ref< Eigen::VectorXd > values) override
Compute the objective function values f(x) for the current parameter set.
Definition: simple_optimization_problem.h:314
corbo::SimpleOptimizationProblem::restoreBackupParameters
void restoreBackupParameters(bool keep_backup) override
Discard last backup (or all)
Definition: simple_optimization_problem.cpp:59
corbo::SimpleOptimizationProblem::setUpperBounds
void setUpperBounds(const Eigen::Ref< const Eigen::VectorXd > &ub)
Definition: simple_optimization_problem.h:159
corbo::SimpleOptimizationProblem::getXRef
Eigen::VectorXd & getXRef()
Definition: simple_optimization_problem.h:152
corbo::SimpleOptimizationProblemWithCallbacks::getObjectiveDimension
int getObjectiveDimension() override
Get dimension of the objective (should be zero or one, includes Lsq objectives if present)
Definition: simple_optimization_problem.h:295
corbo::SimpleOptimizationProblemWithCallbacks::getInequalityDimension
int getInequalityDimension() override
Total dimension of general inequality constraints.
Definition: simple_optimization_problem.h:299
corbo::SimpleOptimizationProblem::setUpperBound
void setUpperBound(int idx, double ub) override
Set specific upper bound of a parameter.
Definition: simple_optimization_problem.h:179
corbo::SimpleOptimizationProblem::_x
Eigen::VectorXd _x
Definition: simple_optimization_problem.h:224
corbo::SimpleOptimizationProblem::getLowerBound
double getLowerBound(int idx) override
Return specific lower bound value of a parameter.
Definition: simple_optimization_problem.h:173
corbo::SimpleOptimizationProblem::getParameterDimension
int getParameterDimension() override
Effictive dimension of the optimization parameter set (changeable, non-fixed part)
Definition: simple_optimization_problem.h:192
corbo::SimpleOptimizationProblemWithCallbacks::_eq_dim
int _eq_dim
Definition: simple_optimization_problem.h:333
corbo::SimpleOptimizationProblem::setLowerBounds
void setLowerBounds(const Eigen::Ref< const Eigen::VectorXd > &lb)
Definition: simple_optimization_problem.h:155
corbo::SimpleOptimizationProblem::getX
const Eigen::VectorXd & getX() const
Definition: simple_optimization_problem.h:153
corbo::SimpleOptimizationProblem::getUpperBoundsRef
Eigen::VectorXd & getUpperBoundsRef()
Definition: simple_optimization_problem.h:160
corbo::SimpleOptimizationProblemWithCallbacks::computeValueNonLsqObjective
double computeValueNonLsqObjective() override
Definition: simple_optimization_problem.h:305
corbo::SimpleOptimizationProblem::getLsqObjectiveDimension
int getLsqObjectiveDimension() override=0
Total dimension of least-squares objective function terms.
corbo::SimpleOptimizationProblem::setParameterValue
void setParameterValue(int idx, double x) override
Set specific value of the parameter vector.
Definition: simple_optimization_problem.h:166
corbo::SimpleOptimizationProblemWithCallbacks::_ineq_dim
int _ineq_dim
Definition: simple_optimization_problem.h:334
x
Scalar * x
Definition: level1_cplx_impl.h:89
corbo::SimpleOptimizationProblem::computeValuesEquality
void computeValuesEquality(Eigen::Ref< Eigen::VectorXd > values) override=0
Compute the equality constraint values ceq(x) for the current parameter set.
corbo::SimpleOptimizationProblem::_x_backup
std::vector< Eigen::VectorXd > _x_backup
Definition: simple_optimization_problem.h:228
corbo::SimpleOptimizationProblem::setX
void setX(const Eigen::Ref< const Eigen::VectorXd > &x)
Definition: simple_optimization_problem.h:151
corbo::SimpleOptimizationProblem::getParameterVector
void getParameterVector(Eigen::Ref< Eigen::VectorXd > x) override
Same as getX() but less efficient (overrides interface method, but here x must not be pre-allocated)
Definition: simple_optimization_problem.h:182
corbo::SimpleOptimizationProblem::setParameterVector
void setParameterVector(const Eigen::Ref< const Eigen::VectorXd > &x) override
Same as setX() (overrides interface method)
Definition: simple_optimization_problem.h:184
corbo::SimpleOptimizationProblem::computeValuesLsqObjective
void computeValuesLsqObjective(Eigen::Ref< Eigen::VectorXd > values) override=0
Compute the objective function values f(x) for the current parameter set.
corbo::SimpleOptimizationProblemWithCallbacks::getLsqObjectiveDimension
int getLsqObjectiveDimension() override
Total dimension of least-squares objective function terms.
Definition: simple_optimization_problem.h:293
corbo::SimpleOptimizationProblem::getUpperBound
double getUpperBound(int idx) override
Return specific upper bound of a parameter.
Definition: simple_optimization_problem.h:175
corbo::SimpleOptimizationProblem::getLowerBoundsRef
Eigen::VectorXd & getLowerBoundsRef()
Definition: simple_optimization_problem.h:156
corbo::SimpleOptimizationProblem::setBounds
void setBounds(const Eigen::Ref< const Eigen::VectorXd > &lb, const Eigen::Ref< const Eigen::VectorXd > &ub) override
Set lower and upper bound vector.
Definition: simple_optimization_problem.cpp:72
corbo::SimpleOptimizationProblem::getNonLsqObjectiveDimension
int getNonLsqObjectiveDimension() override=0
Total dimension of objective function terms.
corbo::SimpleOptimizationProblemWithCallbacks::SimpleOptimizationProblemWithCallbacks
SimpleOptimizationProblemWithCallbacks()
Default constructor (do not forget to initialize the parameter vector dimension manually)
Definition: simple_optimization_problem.h:229
Eigen::Ref
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:192
corbo::SimpleOptimizationProblemWithCallbacks::_obj_fun
std::function< void(const Eigen::VectorXd &, Eigen::Ref< Eigen::VectorXd >)> _obj_fun
Definition: simple_optimization_problem.h:337
corbo::SimpleOptimizationProblem::applyIncrement
void applyIncrement(const Eigen::Ref< const Eigen::VectorXd > &increment) override
Apply increment to the current parameter set.
Definition: simple_optimization_problem.h:195
corbo::SimpleOptimizationProblem::discardBackupParameters
void discardBackupParameters(bool all=false) override
Definition: simple_optimization_problem.h:204
corbo::SimpleOptimizationProblemWithCallbacks::computeValuesEquality
void computeValuesEquality(Eigen::Ref< Eigen::VectorXd > values) override
Compute the equality constraint values ceq(x) for the current parameter set.
Definition: simple_optimization_problem.h:320
corbo::SimpleOptimizationProblem::resizeParameterVector
void resizeParameterVector(int parameter_dim)
Resize the dimension of the parameter vector.
Definition: simple_optimization_problem.cpp:51
corbo::SimpleOptimizationProblemWithCallbacks::setEqualityConstraint
void setEqualityConstraint(std::function< void(const Eigen::VectorXd &, Eigen::Ref< Eigen::VectorXd >)> eq_fun, int eq_dim)
Set equality constraint callback.
Definition: simple_optimization_problem.cpp:86
corbo::SimpleOptimizationProblem::backupParameters
void backupParameters() override
Restore parameter set from the last backup and keep backup if desired.
Definition: simple_optimization_problem.h:200
corbo::SimpleOptimizationProblemWithCallbacks::computeValuesInequality
void computeValuesInequality(Eigen::Ref< Eigen::VectorXd > values) override
Compute the inequality constraint values c(x) for the current parameter set.
Definition: simple_optimization_problem.h:326
corbo::SimpleOptimizationProblem::SimpleOptimizationProblem
SimpleOptimizationProblem()
Definition: simple_optimization_problem.h:98
corbo::SimpleOptimizationProblemWithCallbacks::_obj_dim
int _obj_dim
Definition: simple_optimization_problem.h:332
corbo::SimpleOptimizationProblem::computeValuesInequality
void computeValuesInequality(Eigen::Ref< Eigen::VectorXd > values) override=0
Compute the inequality constraint values c(x) for the current parameter set.
corbo::SimpleOptimizationProblemWithCallbacks::_lsq_form
bool _lsq_form
Definition: simple_optimization_problem.h:335
corbo::SimpleOptimizationProblem::getParameterValue
double getParameterValue(int idx) override
Return specific value of the parameter vector.
Definition: simple_optimization_problem.h:164
corbo::SimpleOptimizationProblemWithCallbacks::setInequalityConstraint
void setInequalityConstraint(std::function< void(const Eigen::VectorXd &, Eigen::Ref< Eigen::VectorXd >)> ineq_fun, int ineq_dim)
Set inequality constraint callback.
Definition: simple_optimization_problem.cpp:93
optimization_problem_interface.h
corbo::SimpleOptimizationProblemWithCallbacks::_ineq_fun
std::function< void(const Eigen::VectorXd &, Eigen::Ref< Eigen::VectorXd >)> _ineq_fun
Definition: simple_optimization_problem.h:339


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Wed Mar 2 2022 00:06:12