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 
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 
108  Eigen::VectorXd& getXRef() { return _x; }
109  const Eigen::VectorXd& getX() const { return _x; }
110 
112  Eigen::VectorXd& getLowerBoundsRef() { return _lb; }
113  const Eigen::VectorXd& getLowerBounds() const { return _lb; }
114 
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 
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:
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_
Simple optimization problem formulation.
void setX(const Eigen::Ref< const Eigen::VectorXd > &x)
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) ...
void setParameterVector(const Eigen::Ref< const Eigen::VectorXd > &x) override
Same as setX() (overrides interface method)
int getEqualityDimension() override
Total dimension of equality constraints.
void computeValuesLsqObjective(Eigen::Ref< Eigen::VectorXd > values) override=0
Compute the objective function values f(x) for the current parameter set.
int getLsqObjectiveDimension() override=0
Total dimension of least-squares objective function terms.
void setParameterValue(int idx, double x) override
Set specific value of the parameter vector.
Scalar * x
std::vector< Eigen::VectorXd > _x_backup
void computeValuesEquality(Eigen::Ref< Eigen::VectorXd > values) override=0
Compute the equality constraint values ceq(x) for the current parameter set.
double getUpperBound(int idx) override
Return specific upper bound of a parameter.
int getNonLsqObjectiveDimension() override
Total dimension of objective function terms.
bool isLeastSquaresProblem() const override
Check if the underlying problem is defined in the least squares form.
void setBounds(const Eigen::Ref< const Eigen::VectorXd > &lb, const Eigen::Ref< const Eigen::VectorXd > &ub) override
Set lower and upper bound vector.
int getNonLsqObjectiveDimension() override=0
Total dimension of objective function terms.
void applyIncrement(const Eigen::Ref< const Eigen::VectorXd > &increment) override
Apply increment to the current parameter set.
Simple optimization problem formulation (callback based configuration)
void discardBackupParameters(bool all=false) override
void computeValuesLsqObjective(Eigen::Ref< Eigen::VectorXd > values) override
Compute the objective function values f(x) for the current parameter set.
void resizeParameterVector(int parameter_dim)
Resize the dimension of the parameter vector.
void backupParameters() override
Restore parameter set from the last backup and keep backup if desired.
int getObjectiveDimension() override
Get dimension of the objective (should be zero or one, includes Lsq objectives if present) ...
int getInequalityDimension() override
Total dimension of general inequality constraints.
int getLsqObjectiveDimension() override
Total dimension of least-squares objective function terms.
void computeValuesInequality(Eigen::Ref< Eigen::VectorXd > values) override=0
Compute the inequality constraint values c(x) for the current parameter set.
double getParameterValue(int idx) override
Return specific value of the parameter vector.
Generic interface for optimization problem definitions.
int getEqualityDimension() override=0
Total dimension of equality constraints.
int getInequalityDimension() override=0
Total dimension of general inequality constraints.
void getBounds(Eigen::Ref< Eigen::VectorXd > lb, Eigen::Ref< Eigen::VectorXd > ub) override
Get lower and upper bound vector.
const Eigen::VectorXd & getLowerBounds() const
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:192
void computeValuesEquality(Eigen::Ref< Eigen::VectorXd > values) override
Compute the equality constraint values ceq(x) for the current parameter set.
bool isLeastSquaresProblem() const override=0
Check if the underlying problem is defined in the least squares form.
void setLowerBound(int idx, double lb) override
Set specific lower bound of a parameter.
int getObjectiveDimension() override=0
Get dimension of the objective (should be zero or one, includes Lsq objectives if present) ...
const Eigen::VectorXd & getUpperBounds() const
double computeValueNonLsqObjective() override=0
void setUpperBounds(const Eigen::Ref< const Eigen::VectorXd > &ub)
SimpleOptimizationProblemWithCallbacks(int param_dim)
Construct Optimization Problem with a given parameter vector dimension.
SimpleOptimizationProblemWithCallbacks()
Default constructor (do not forget to initialize the parameter vector dimension manually) ...
void restoreBackupParameters(bool keep_backup) override
Discard last backup (or all)
double getLowerBound(int idx) override
Return specific lower bound value of a parameter.
void setLowerBounds(const Eigen::Ref< const Eigen::VectorXd > &lb)
const Eigen::VectorXd & getX() const
void setUpperBound(int idx, double ub) override
Set specific upper bound of a parameter.
void computeValuesInequality(Eigen::Ref< Eigen::VectorXd > values) override
Compute the inequality constraint values c(x) for the current parameter set.
int getParameterDimension() override
Effictive dimension of the optimization parameter set (changeable, non-fixed part) ...
void applyIncrement(int idx, double increment) override
Apply increment to the current parameter set (single element overload)


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Mon Feb 28 2022 22:07:18