problem.cc
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 #include <ifopt/problem.h>
31 #include <iomanip>
32 #include <iostream>
33 
34 namespace ifopt {
35 
37  : constraints_("constraint-sets", false), costs_("cost-terms", true)
38 {
39  variables_ = std::make_shared<Composite>("variable-sets", false);
40 }
41 
43 {
44  variables_->AddComponent(variable_set);
45 }
46 
48 {
49  constraint_set->LinkWithVariables(variables_);
50  constraints_.AddComponent(constraint_set);
51 }
52 
54 {
55  cost_set->LinkWithVariables(variables_);
56  costs_.AddComponent(cost_set);
57 }
58 
60 {
61  return variables_->GetRows();
62 }
63 
65 {
66  return variables_->GetBounds();
67 }
68 
70 {
71  return variables_->GetValues();
72 }
73 
74 void Problem::SetVariables(const double* x)
75 {
76  variables_->SetVariables(ConvertToEigen(x));
77 }
78 
79 double Problem::EvaluateCostFunction(const double* x)
80 {
81  VectorXd g = VectorXd::Zero(1);
82  if (HasCostTerms()) {
83  SetVariables(x);
84  g = costs_.GetValues();
85  }
86  return g(0);
87 }
88 
90  const double* x, bool use_finite_difference_approximation, double epsilon)
91 {
93  Jacobian jac = Jacobian(1, n);
94  if (HasCostTerms()) {
95  if (use_finite_difference_approximation) {
96  double step_size = epsilon;
97 
98  // calculate forward difference by disturbing each optimization variable
99  double g = EvaluateCostFunction(x);
100  std::vector<double> x_new(x, x + n);
101  for (int i = 0; i < n; ++i) {
102  x_new[i] += step_size; // disturb
103  double g_new = EvaluateCostFunction(x_new.data());
104  jac.coeffRef(0, i) = (g_new - g) / step_size;
105  x_new[i] = x[i]; // reset for next iteration
106  }
107  } else {
108  SetVariables(x);
109  jac = costs_.GetJacobian();
110  }
111  }
112 
113  return jac.row(0).transpose();
114 }
115 
117 {
118  return constraints_.GetBounds();
119 }
120 
122 {
123  return GetBoundsOnConstraints().size();
124 }
125 
127 {
128  SetVariables(x);
129  return constraints_.GetValues();
130 }
131 
132 bool Problem::HasCostTerms() const
133 {
134  return costs_.GetRows() > 0;
135 }
136 
137 void Problem::EvalNonzerosOfJacobian(const double* x, double* values)
138 {
139  SetVariables(x);
141 
142  jac.makeCompressed(); // so the valuePtr() is dense and accurate
143  std::copy(jac.valuePtr(), jac.valuePtr() + jac.nonZeros(), values);
144 }
145 
147 {
149 }
150 
152 {
153  return costs_.GetJacobian();
154 }
155 
157 {
158  x_prev.push_back(variables_->GetValues());
159 }
160 
162 {
163  return variables_;
164 }
165 
166 void Problem::SetOptVariables(int iter)
167 {
168  variables_->SetVariables(x_prev.at(iter));
169 }
170 
172 {
173  variables_->SetVariables(x_prev.at(GetIterationCount() - 1));
174 }
175 
176 void Problem::PrintCurrent() const
177 {
178  using namespace std;
179  cout << "\n"
180  << "************************************************************\n"
181  << " IFOPT - Interface to Nonlinear Optimizers (v2.0)\n"
182  << " \u00a9 Alexander W. Winkler\n"
183  << " https://github.com/ethz-adrl/ifopt\n"
184  << "************************************************************"
185  << "\n"
186  << "Legend:\n"
187  << "c - number of variables, constraints or cost terms" << std::endl
188  << "i - indices of this set in overall problem" << std::endl
189  << "v - number of [violated variable- or constraint-bounds] or [cost "
190  "term value]"
191  << "\n\n"
192  << std::right << std::setw(33) << "" << std::setw(5) << "c "
193  << std::setw(16) << "i " << std::setw(11) << "v " << std::left
194  << "\n";
195 
196  variables_->PrintAll();
199 };
200 
201 Problem::VectorXd Problem::ConvertToEigen(const double* x) const
202 {
203  return Eigen::Map<const VectorXd>(x, GetNumberOfOptimizationVariables());
204 }
205 
206 } // namespace ifopt
ifopt::Problem::GetJacobianOfCosts
Jacobian GetJacobianOfCosts() const
The sparse-matrix representation of Jacobian of the costs.
Definition: problem.cc:178
ifopt::Problem::GetNumberOfOptimizationVariables
int GetNumberOfOptimizationVariables() const
The number of optimization variables.
Definition: problem.cc:86
ifopt::Problem::EvaluateCostFunction
double EvaluateCostFunction(const double *x)
The scalar cost for current optimization variables x.
Definition: problem.cc:106
ifopt::Composite::PrintAll
void PrintAll() const
Definition: composite.cc:221
ifopt::Problem::SetVariables
void SetVariables(const double *x)
Updates the variables with the values of the raw pointer x.
Definition: problem.cc:101
ifopt::Problem::EvaluateCostFunctionGradient
VectorXd EvaluateCostFunctionGradient(const double *x, bool use_finite_difference_approximation=false, double epsilon=std::numeric_limits< double >::epsilon())
The column-vector of derivatives of the cost w.r.t. each variable.
Definition: problem.cc:116
ifopt::Composite::AddComponent
void AddComponent(const Component::Ptr &)
Adds a component to this composite.
Definition: composite.cc:119
ifopt::Problem::VecBound
Component::VecBound VecBound
Definition: problem.h:125
ifopt::Problem::GetBoundsOnConstraints
VecBound GetBoundsOnConstraints() const
The upper and lower bound of each individual constraint.
Definition: problem.cc:143
ifopt::Problem::HasCostTerms
bool HasCostTerms() const
True if the optimization problem includes a cost, false if merely a feasibility problem is defined.
Definition: problem.cc:159
ifopt::Problem::VectorXd
Component::VectorXd VectorXd
Definition: problem.h:127
ifopt::Problem::EvalNonzerosOfJacobian
void EvalNonzerosOfJacobian(const double *x, double *values)
Extracts those entries from constraint Jacobian that are not zero.
Definition: problem.cc:164
ifopt::Problem::AddConstraintSet
void AddConstraintSet(ConstraintSet::Ptr constraint_set)
Add a set of multiple constraints to the optimization problem.
Definition: problem.cc:74
ifopt::Problem::x_prev
std::vector< VectorXd > x_prev
the pure variables for every iteration.
Definition: problem.h:300
ifopt::Composite::GetBounds
VecBound GetBounds() const override
Returns the "bounds" of this component.
Definition: composite.cc:205
ifopt::Problem::SetOptVariablesFinal
void SetOptVariablesFinal()
Sets the optimization variables to those of the final iteration.
Definition: problem.cc:198
ifopt::Problem::Jacobian
Component::Jacobian Jacobian
Definition: problem.h:126
problem.h
ifopt::Problem::GetNumberOfConstraints
int GetNumberOfConstraints() const
The number of individual constraints.
Definition: problem.cc:148
ifopt::ConstraintSet::variables_
VariablesPtr variables_
Definition: constraint_set.h:170
ifopt::Problem::GetJacobianOfConstraints
Jacobian GetJacobianOfConstraints() const
The sparse-matrix representation of Jacobian of the constraints.
Definition: problem.cc:173
ifopt::Problem::EvaluateConstraints
VectorXd EvaluateConstraints(const double *x)
Each constraint value g(x) for current optimization variables x.
Definition: problem.cc:153
ifopt::Problem::Problem
Problem()
Creates a optimization problem with no variables, costs or constraints.
Definition: problem.cc:63
ifopt::Problem::costs_
Composite costs_
Definition: problem.h:298
ifopt::Problem::SetOptVariables
void SetOptVariables(int iter)
Sets the optimization variables to those at iteration iter.
Definition: problem.cc:193
ifopt::Problem::GetVariableValues
VectorXd GetVariableValues() const
The current value of the optimization variables.
Definition: problem.cc:96
ifopt::Problem::GetBoundsOnOptimizationVariables
VecBound GetBoundsOnOptimizationVariables() const
The maximum and minimum value each optimization variable is allowed to have.
Definition: problem.cc:91
ifopt::Problem::AddCostSet
void AddCostSet(CostTerm::Ptr cost_set)
Add a cost term to the optimization problem.
Definition: problem.cc:80
ifopt::Composite::Ptr
std::shared_ptr< Composite > Ptr
Definition: composite.h:162
ifopt::Composite::GetJacobian
Jacobian GetJacobian() const override
Returns derivatives of each row w.r.t. the variables.
Definition: composite.cc:174
ifopt::Problem::GetIterationCount
int GetIterationCount() const
The number of iterations it took to solve the problem.
Definition: problem.h:270
ifopt::Component::Ptr
std::shared_ptr< Component > Ptr
Definition: composite.h:65
ifopt::Problem::AddVariableSet
void AddVariableSet(VariableSet::Ptr variable_set)
Add one individual set of variables to the optimization problem.
Definition: problem.cc:69
ifopt::Problem::variables_
Composite::Ptr variables_
Definition: problem.h:293
ifopt
common namespace for all elements in this library.
Definition: bounds.h:33
ifopt::Problem::SaveCurrent
void SaveCurrent()
Saves the current values of the optimization variables in x_prev.
Definition: problem.cc:183
ifopt::Component::GetRows
int GetRows() const
Returns the number of rows of this component.
Definition: composite.cc:70
ifopt::ConstraintSet::Ptr
std::shared_ptr< ConstraintSet > Ptr
Definition: constraint_set.h:107
ifopt::Problem::GetOptVariables
Composite::Ptr GetOptVariables() const
Read/write access to the current optimization variables.
Definition: problem.cc:188
ifopt::Problem::PrintCurrent
void PrintCurrent() const
Prints the variables, costs and constraints.
Definition: problem.cc:203
ifopt::Composite::GetValues
VectorXd GetValues() const override
Returns the "values" of whatever this component represents.
Definition: composite.cc:148
ifopt::Problem::ConvertToEigen
VectorXd ConvertToEigen(const double *x) const
Definition: problem.cc:228
ifopt::Problem::constraints_
Composite constraints_
Definition: problem.h:297


ifopt
Author(s): Alexander W. Winkler
autogenerated on Mon Sep 18 2023 02:14:38