ex_problem.h
Go to the documentation of this file.
1 /******************************************************************************
2 Copyright (c) 2017, Alexander W Winkler. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6 
7 * Redistributions of source code must retain the above copyright notice, this
8  list of conditions and the following disclaimer.
9 
10 * Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation
12  and/or other materials provided with the distribution.
13 
14 * Neither the name of the copyright holder nor the names of its
15  contributors may be used to endorse or promote products derived from
16  this software without specific prior written permission.
17 
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 ******************************************************************************/
29 
30 
48 #include <ifopt/variable_set.h>
49 
50 namespace ifopt {
51 using Eigen::Vector2d;
52 
53 
54 class ExVariables : public VariableSet {
55 public:
56  // Every variable set has a name, here "var_set1". this allows the constraints
57  // and costs to define values and Jacobians specifically w.r.t this variable set.
58  ExVariables() : ExVariables("var_set1") {};
59  ExVariables(const std::string& name) : VariableSet(2, name)
60  {
61  // the initial values where the NLP starts iterating from
62  x0_ = 0.5;
63  x1_ = 1.5;
64  }
65 
66  // Here is where you can transform the Eigen::Vector into whatever
67  // internal representation of your variables you have (here two doubles, but
68  // can also be complex classes such as splines, etc..
69  virtual void SetVariables(const VectorXd& x) override
70  {
71  x0_ = x(0);
72  x1_ = x(1);
73  };
74 
75  // Here is the reverse transformation from the internal representation to
76  // to the Eigen::Vector
77  virtual VectorXd GetValues() const override
78  {
79  return Vector2d(x0_, x1_);
80  };
81 
82  // Each variable has an upper and lower bound set here
83  VecBound GetBounds() const override
84  {
85  VecBound bounds(GetRows());
86  bounds.at(0) = Bounds(-1.0, 1.0);
87  bounds.at(1) = NoBound;
88  return bounds;
89  }
90 
91 private:
92  double x0_, x1_;
93 };
94 
95 
96 class ExConstraint : public ConstraintSet {
97 public:
98  ExConstraint() : ExConstraint("constraint1") {}
99 
100  // This constraint set just contains 1 constraint, however generally
101  // each set can contain multiple related constraints.
102  ExConstraint(const std::string& name) : ConstraintSet(1, name) {}
103 
104  // The constraint value minus the constant value "1", moved to bounds.
105  virtual VectorXd GetValues() const override
106  {
107  VectorXd g(GetRows());
108  Vector2d x = GetVariables()->GetComponent("var_set1")->GetValues();
109  g(0) = std::pow(x(0),2) + x(1);
110  return g;
111  };
112 
113  // The only constraint in this set is an equality constraint to 1.
114  // Constant values should always be put into GetBounds(), not GetValues().
115  // For inequality constraints (<,>), use Bounds(x, inf) or Bounds(-inf, x).
116  VecBound GetBounds() const override
117  {
118  VecBound b(GetRows());
119  b.at(0) = Bounds(1.0, 1.0);
120  return b;
121  }
122 
123  // This function should provides the derivative of the constraint set.
124  // can also tell the solvers to approximate the derivatives by finite
125  // differences and simply leave this function empty by setting the solver
126  // option in e.g. ipopt_adapter.cc::SetOptions():
127  // SetStringValue("jacobian_approximation", "finite-difference-values");
128  void FillJacobianBlock (std::string var_set, Jacobian& jac_block) const override
129  {
130  // must fill only that submatrix of the overall Jacobian that relates
131  // to this constraint and "var_set1". even if more constraints or variables
132  // classes are added, this submatrix will always start at row 0 and column 0,
133  // thereby being independent from the overall problem.
134  if (var_set == "var_set1") {
135 
136  Vector2d x = GetVariables()->GetComponent("var_set1")->GetValues();
137 
138  jac_block.coeffRef(0, 0) = 2.0*x(0); // derivative of first constraint w.r.t x0
139  jac_block.coeffRef(0, 1) = 1.0; // derivative of first constraint w.r.t x1
140  }
141  }
142 };
143 
144 
145 class ExCost: public CostTerm {
146 public:
147  ExCost() : ExCost("cost_term1") {}
148  ExCost(const std::string& name) : CostTerm(name) {}
149 
150  virtual double GetCost() const override
151  {
152  Vector2d x = GetVariables()->GetComponent("var_set1")->GetValues();
153  return -std::pow(x(1)-2,2);
154  };
155 
156  void FillJacobianBlock (std::string var_set, Jacobian& jac) const override
157  {
158  if (var_set == "var_set1") {
159 
160  Vector2d x = GetVariables()->GetComponent("var_set1")->GetValues();
161 
162  jac.coeffRef(0, 0) = 0.0; // derivative of cost w.r.t x0
163  jac.coeffRef(0, 1) = -2.0*(x(1)-2.0); // derivative of cost w.r.t x1
164  }
165  }
166 };
167 
168 } // namespace opt
169 
A container holding a single cost term.
Definition: cost_term.h:45
ExVariables(const std::string &name)
Definition: ex_problem.h:59
A container holding a set of related constraints.
virtual VectorXd GetValues() const override
Returns the "values" of whatever this component represents.
Definition: ex_problem.h:77
virtual void SetVariables(const VectorXd &x) override
Sets the optimization variables from an Eigen vector.
Definition: ex_problem.h:69
void FillJacobianBlock(std::string var_set, Jacobian &jac_block) const override
Set individual Jacobians corresponding to each decision variable set.
Definition: ex_problem.h:128
VecBound GetBounds() const override
Returns the "bounds" of this component.
Definition: ex_problem.h:116
ExCost(const std::string &name)
Definition: ex_problem.h:148
static const Bounds NoBound
Definition: bounds.h:68
virtual VectorXd GetValues() const override
Returns the "values" of whatever this component represents.
Definition: ex_problem.h:105
virtual double GetCost() const override
Returns the scalar cost term calculated from the variables.
Definition: ex_problem.h:150
Eigen::SparseMatrix< double, Eigen::RowMajor > Jacobian
Definition: composite.h:75
Upper and lower bound for optimization variables and constraints.
Definition: bounds.h:38
VecBound GetBounds() const override
Returns the "bounds" of this component.
Definition: ex_problem.h:83
Definition: bounds.h:33
A container holding a set of related optimization variables.
Definition: variable_set.h:45
ExConstraint(const std::string &name)
Definition: ex_problem.h:102
void FillJacobianBlock(std::string var_set, Jacobian &jac) const override
Set individual Jacobians corresponding to each decision variable set.
Definition: ex_problem.h:156
std::vector< Bounds > VecBound
Definition: composite.h:77
int GetRows() const
Returns the number of rows of this component.
Definition: composite.cc:43
Eigen::VectorXd VectorXd
Definition: composite.h:76


ifopt_core
Author(s): Alexander W. Winkler
autogenerated on Thu Apr 19 2018 02:47:37