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 
32 
33 namespace ifopt {
34 
36  :constraints_("constraints", false),
37  costs_("costs", true)
38 {
39  variables_ = std::make_shared<Composite>("variables", false);
40 }
41 
42 void
44 {
45  variables_->AddComponent(variable_set);
46 }
47 
48 void
50 {
51  constraint_set->LinkWithVariables(variables_);
52  constraints_.AddComponent(constraint_set);
53 }
54 
55 void
57 {
58  cost_set->LinkWithVariables(variables_);
59  costs_.AddComponent(cost_set);
60 }
61 
62 int
64 {
65  return variables_->GetRows();
66 }
67 
70 {
71  return variables_->GetBounds();
72 }
73 
76 {
77  return variables_->GetValues();
78 }
79 
80 void
81 Problem::SetVariables (const double* x)
82 {
83  variables_->SetVariables(ConvertToEigen(x));
84 }
85 
86 double
88 {
89  VectorXd g = VectorXd::Zero(1);
90  if (HasCostTerms()) {
91  SetVariables(x);
92  g = costs_.GetValues();
93  }
94  return g(0);
95 }
96 
99 {
101  if (HasCostTerms()) {
102  SetVariables(x);
103  jac = costs_.GetJacobian();
104  }
105 
106  return jac.row(0).transpose();
107 }
108 
111 {
112  return constraints_.GetBounds();
113 }
114 
115 int
117 {
118  return GetBoundsOnConstraints().size();
119 }
120 
123 {
124  SetVariables(x);
125  return constraints_.GetValues();
126 }
127 
128 bool
130 {
131  return costs_.GetRows()>0;
132 }
133 
134 void
135 Problem::EvalNonzerosOfJacobian (const double* x, double* values)
136 {
137  SetVariables(x);
139 
140  jac.makeCompressed(); // so the valuePtr() is dense and accurate
141  std::copy(jac.valuePtr(), jac.valuePtr() + jac.nonZeros(), values);
142 }
143 
146 {
147  return constraints_.GetJacobian();
148 }
149 
150 void
152 {
153  x_prev.push_back(variables_->GetValues());
154 }
155 
158 {
159  return variables_;
160 }
161 
162 void
164 {
165  variables_->SetVariables(x_prev.at(iter));
166 }
167 
168 void
170 {
171  variables_->SetVariables(x_prev.at(GetIterationCount()-1));
172 }
173 
174 void
176 {
177  variables_->Print();
178  costs_.Print();
180 };
181 
183 Problem::ConvertToEigen(const double* x) const
184 {
185  return Eigen::Map<const VectorXd>(x,GetNumberOfOptimizationVariables());
186 }
187 
188 } /* namespace opt */
189 
std::vector< VectorXd > x_prev
the pure variables for every iteration.
Definition: problem.h:209
Problem()
Creates a optimization problem with no variables, costs or constraints.
Definition: problem.cc:35
Composite::Ptr variables_
Definition: problem.h:205
VecBound GetBounds() const override
Returns the "bounds" of this component.
Definition: composite.cc:149
void SetOptVariables(int iter)
Sets the optimization variables to those at iteration iter.
Definition: problem.cc:163
int GetIterationCount() const
The number of iterations it took to solve the problem.
Definition: problem.h:197
void AddCostSet(CostTerm::Ptr cost_set)
Add a cost term to the optimization problem.
Definition: problem.cc:56
std::shared_ptr< Composite > Ptr
Definition: composite.h:170
Component::Jacobian Jacobian
Definition: problem.h:65
VecBound GetBoundsOnConstraints() const
The upper and lower bound of each individual constraint.
Definition: problem.cc:110
double EvaluateCostFunction(const double *x)
The scalar cost for current optimization variables x.
Definition: problem.cc:87
Composite constraints_
Definition: problem.h:206
VectorXd GetVariableValues() const
The current value of the optimization variables.
Definition: problem.cc:75
Component::VectorXd VectorXd
Definition: problem.h:66
VectorXd EvaluateCostFunctionGradient(const double *x)
The column-vector of derivatives of the cost w.r.t. each variable.
Definition: problem.cc:98
std::shared_ptr< Component > Ptr
Definition: composite.h:73
void SaveCurrent()
Saves the current values of the optimization variables in x_prev.
Definition: problem.cc:151
bool HasCostTerms() const
True if the optimization problem includes a cost, false if merely a feasibility problem is defined...
Definition: problem.cc:129
VecBound GetBoundsOnOptimizationVariables() const
The maximum and minimum value each optimization variable is allowed to have.
Definition: problem.cc:69
Component::VecBound VecBound
Definition: problem.h:64
void PrintCurrent() const
Prints the variables, costs and constraints.
Definition: problem.cc:175
VectorXd GetValues() const override
Returns the "values" of whatever this component represents.
Definition: composite.cc:98
void SetVariables(const double *x)
Updates the variables with the values of the raw pointer x.
Definition: problem.cc:81
void AddComponent(const Component::Ptr &)
Adds a component to this composite.
Definition: composite.cc:66
Jacobian GetJacobianOfConstraints() const
The sparse-matrix representation of Jacobian of the constraints.
Definition: problem.cc:145
std::shared_ptr< ConstraintSet > Ptr
void SetOptVariablesFinal()
Sets the optimization variables to those of the final iteration.
Definition: problem.cc:169
Definition: bounds.h:33
void Print() const override
Prints the relevant information (name, rows, values) of this component.
Definition: composite.cc:169
Jacobian GetJacobian() const override
Returns derivatives of each row w.r.t. the variables.
Definition: composite.cc:128
void EvalNonzerosOfJacobian(const double *x, double *values)
Extracts those entries from constraint Jacobian that are not zero.
Definition: problem.cc:135
VectorXd ConvertToEigen(const double *x) const
Definition: problem.cc:183
void AddConstraintSet(ConstraintSet::Ptr constraint_set)
Add a set of multiple constraints to the optimization problem.
Definition: problem.cc:49
int GetNumberOfConstraints() const
The number of individual constraints.
Definition: problem.cc:116
VectorXd EvaluateConstraints(const double *x)
Each constraint value g(x) for current optimization variables x.
Definition: problem.cc:122
void AddVariableSet(VariableSet::Ptr variable_set)
Add one individual set of variables to the optimization problem.
Definition: problem.cc:43
Composite costs_
Definition: problem.h:207
int GetRows() const
Returns the number of rows of this component.
Definition: composite.cc:43
Composite::Ptr GetOptVariables() const
Read/write access to the current optimization variables.
Definition: problem.cc:157
int GetNumberOfOptimizationVariables() const
The number of optimization variables.
Definition: problem.cc:63


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