test_vars_constr_cost.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 
54 #include <ifopt/variable_set.h>
55 #include <ifopt/constraint_set.h>
56 #include <ifopt/cost_term.h>
57 
58 namespace ifopt {
59 using Eigen::Vector2d;
60 
61 
62 class ExVariables : public VariableSet {
63 public:
64  // Every variable set has a name, here "var_set1". this allows the constraints
65  // and costs to define values and Jacobians specifically w.r.t this variable set.
66  ExVariables() : ExVariables("var_set1") {};
67  ExVariables(const std::string& name) : VariableSet(2, name)
68  {
69  // the initial values where the NLP starts iterating from
70  x0_ = 3.5;
71  x1_ = 1.5;
72  }
73 
74  // Here is where you can transform the Eigen::Vector into whatever
75  // internal representation of your variables you have (here two doubles, but
76  // can also be complex classes such as splines, etc..
77  void SetVariables(const VectorXd& x) override
78  {
79  x0_ = x(0);
80  x1_ = x(1);
81  };
82 
83  // Here is the reverse transformation from the internal representation to
84  // to the Eigen::Vector
85  VectorXd GetValues() const override
86  {
87  return Vector2d(x0_, x1_);
88  };
89 
90  // Each variable has an upper and lower bound set here
91  VecBound GetBounds() const override
92  {
93  VecBound bounds(GetRows());
94  bounds.at(0) = Bounds(-1.0, 1.0);
95  bounds.at(1) = NoBound;
96  return bounds;
97  }
98 
99 private:
100  double x0_, x1_;
101 };
102 
103 
104 class ExConstraint : public ConstraintSet {
105 public:
106  ExConstraint() : ExConstraint("constraint1") {}
107 
108  // This constraint set just contains 1 constraint, however generally
109  // each set can contain multiple related constraints.
110  ExConstraint(const std::string& name) : ConstraintSet(1, name) {}
111 
112  // The constraint value minus the constant value "1", moved to bounds.
113  VectorXd GetValues() const override
114  {
115  VectorXd g(GetRows());
116  Vector2d x = GetVariables()->GetComponent("var_set1")->GetValues();
117  g(0) = std::pow(x(0),2) + x(1);
118  return g;
119  };
120 
121  // The only constraint in this set is an equality constraint to 1.
122  // Constant values should always be put into GetBounds(), not GetValues().
123  // For inequality constraints (<,>), use Bounds(x, inf) or Bounds(-inf, x).
124  VecBound GetBounds() const override
125  {
126  VecBound b(GetRows());
127  b.at(0) = Bounds(1.0, 1.0);
128  return b;
129  }
130 
131  // This function provides the first derivative of the constraints.
132  // In case this is too difficult to write, you can also tell the solvers to
133  // approximate the derivatives by finite differences and not overwrite this
134  // function, e.g. in ipopt.cc::use_jacobian_approximation_ = true
135  // Attention: see the parent class function for important information on sparsity pattern.
136  void FillJacobianBlock (std::string var_set, Jacobian& jac_block) const override
137  {
138  // must fill only that submatrix of the overall Jacobian that relates
139  // to this constraint and "var_set1". even if more constraints or variables
140  // classes are added, this submatrix will always start at row 0 and column 0,
141  // thereby being independent from the overall problem.
142  if (var_set == "var_set1") {
143  Vector2d x = GetVariables()->GetComponent("var_set1")->GetValues();
144 
145  jac_block.coeffRef(0, 0) = 2.0*x(0); // derivative of first constraint w.r.t x0
146  jac_block.coeffRef(0, 1) = 1.0; // derivative of first constraint w.r.t x1
147  }
148  }
149 };
150 
151 
152 class ExCost: public CostTerm {
153 public:
154  ExCost() : ExCost("cost_term1") {}
155  ExCost(const std::string& name) : CostTerm(name) {}
156 
157  double GetCost() const override
158  {
159  Vector2d x = GetVariables()->GetComponent("var_set1")->GetValues();
160  return -std::pow(x(1)-2,2);
161  };
162 
163  void FillJacobianBlock (std::string var_set, Jacobian& jac) const override
164  {
165  if (var_set == "var_set1") {
166  Vector2d x = GetVariables()->GetComponent("var_set1")->GetValues();
167 
168  jac.coeffRef(0, 0) = 0.0; // derivative of cost w.r.t x0
169  jac.coeffRef(0, 1) = -2.0*(x(1)-2.0); // derivative of cost w.r.t x1
170  }
171  }
172 };
173 
174 } // namespace opt
175 
A container holding a single cost term.
Definition: cost_term.h:46
ExVariables(const std::string &name)
A container holding a set of related constraints.
void FillJacobianBlock(std::string var_set, Jacobian &jac_block) const override
Set individual Jacobians corresponding to each decision variable set.
VecBound GetBounds() const override
Returns the "bounds" of this component.
VectorXd GetValues() const override
Returns the "values" of whatever this component represents.
ExCost(const std::string &name)
static const Bounds NoBound
Definition: bounds.h:67
Eigen::SparseMatrix< double, Eigen::RowMajor > Jacobian
Definition: composite.h:67
Upper and lower bound for optimization variables and constraints.
Definition: bounds.h:38
VecBound GetBounds() const override
Returns the "bounds" of this component.
common namespace for all elements in this library.
Definition: bounds.h:33
double GetCost() const override
Returns the scalar cost term calculated from the variables.
A container holding a set of related optimization variables.
Definition: variable_set.h:46
ExConstraint(const std::string &name)
void FillJacobianBlock(std::string var_set, Jacobian &jac) const override
Set individual Jacobians corresponding to each decision variable set.
std::vector< Bounds > VecBound
Definition: composite.h:69
int GetRows() const
Returns the number of rows of this component.
Definition: composite.cc:44
VectorXd GetValues() const override
Returns the "values" of whatever this component represents.
Eigen::VectorXd VectorXd
Definition: composite.h:68
void SetVariables(const VectorXd &x) override
Sets the optimization variables from an Eigen vector.


ifopt
Author(s): Alexander W. Winkler
autogenerated on Fri Jan 22 2021 03:47:32