composite.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/composite.h>
31 
32 #include <iomanip>
33 #include <iostream>
34 
35 namespace ifopt {
36 
37 Component::Component(int num_rows, const std::string& name)
38 {
39  num_rows_ = num_rows;
40  name_ = name;
41 }
42 
43 int Component::GetRows() const
44 {
45  return num_rows_;
46 }
47 
48 void Component::SetRows(int num_rows)
49 {
50  num_rows_ = num_rows;
51 }
52 
53 std::string Component::GetName() const
54 {
55  return name_;
56 }
57 
58 void Component::Print(double tol, int& index) const
59 {
60  // calculate squared bound violation
61  VectorXd x = GetValues();
62  VecBound bounds = GetBounds();
63 
64  std::vector<int> viol_idx;
65  for (std::size_t i = 0; i < bounds.size(); ++i) {
66  double lower = bounds.at(i).lower_;
67  double upper = bounds.at(i).upper_;
68  double val = x(i);
69  if (val < lower - tol || upper + tol < val)
70  viol_idx.push_back(i); // constraint out of bounds
71  }
72 
73  std::string black = "\033[0m";
74  std::string red = "\033[31m";
75  std::string color = viol_idx.empty() ? black : red;
76 
77  std::cout.precision(2);
78  std::cout << std::fixed << std::left << std::setw(30) << name_ << std::right
79  << std::setw(4) << num_rows_ << std::setw(9) << index
80  << std::setfill('.') << std::setw(7) << index + num_rows_ - 1
81  << std::setfill(' ') << color << std::setw(12) << viol_idx.size()
82  << black << std::endl;
83 
84  index += num_rows_;
85 }
86 
87 Composite::Composite(const std::string& name, bool is_cost) : Component(0, name)
88 {
89  is_cost_ = is_cost;
90 }
91 
93 {
94  // at this point the number of rows must be specified.
95  assert(c->GetRows() != kSpecifyLater);
96 
97  components_.push_back(c);
98 
99  if (is_cost_)
100  SetRows(1);
101  else
102  SetRows(GetRows() + c->GetRows());
103 }
104 
106 {
107  components_.clear();
108  SetRows(0);
109 }
110 
111 const Component::Ptr Composite::GetComponent(std::string name) const
112 {
113  for (const auto& c : components_)
114  if (c->GetName() == name)
115  return c;
116 
117  assert(false); // component with name doesn't exist, abort program
118  return Component::Ptr();
119 }
120 
122 {
123  VectorXd g_all = VectorXd::Zero(GetRows());
124 
125  int row = 0;
126  for (const auto& c : components_) {
127  int n_rows = c->GetRows();
128  VectorXd g = c->GetValues();
129  g_all.middleRows(row, n_rows) += g;
130 
131  if (!is_cost_)
132  row += n_rows;
133  }
134  return g_all;
135 }
136 
137 void Composite::SetVariables(const VectorXd& x)
138 {
139  int row = 0;
140  for (auto& c : components_) {
141  int n_rows = c->GetRows();
142  c->SetVariables(x.middleRows(row, n_rows));
143  row += n_rows;
144  }
145 }
146 
148 { // set number of variables only the first time this function is called,
149  // since number doesn't change during the optimization. Improves efficiency.
150  if (n_var == -1)
151  n_var = components_.empty() ? 0 : components_.front()->GetJacobian().cols();
152 
153  Jacobian jacobian(GetRows(), n_var);
154 
155  if (n_var == 0)
156  return jacobian;
157 
158  int row = 0;
159  std::vector<Eigen::Triplet<double>> triplet_list;
160 
161  for (const auto& c : components_) {
162  const Jacobian& jac = c->GetJacobian();
163  triplet_list.reserve(triplet_list.size() + jac.nonZeros());
164 
165  for (int k = 0; k < jac.outerSize(); ++k)
166  for (Jacobian::InnerIterator it(jac, k); it; ++it)
167  triplet_list.push_back(
168  Eigen::Triplet<double>(row + it.row(), it.col(), it.value()));
169 
170  if (!is_cost_)
171  row += c->GetRows();
172  }
173 
174  jacobian.setFromTriplets(triplet_list.begin(), triplet_list.end());
175  return jacobian;
176 }
177 
179 {
180  VecBound bounds_;
181  for (const auto& c : components_) {
182  VecBound b = c->GetBounds();
183  bounds_.insert(bounds_.end(), b.begin(), b.end());
184  }
185 
186  return bounds_;
187 }
188 
190 {
191  return components_;
192 }
193 
194 void Composite::PrintAll() const
195 {
196  int index = 0;
197  double tol =
198  0.001;
199 
200  std::cout << GetName() << ":\n";
201  for (auto c : components_) {
202  std::cout << " "; // indent components
203  c->Print(tol, index);
204  }
205  std::cout << std::endl;
206 }
207 
208 } // namespace ifopt
ifopt::Composite::Composite
Composite(const std::string &name, bool is_cost)
Creates a Composite holding either variables, costs or constraints.
Definition: composite.cc:114
ifopt::Component
Interface representing either Variable, Cost or Constraint.
Definition: composite.h:63
ifopt::Component::SetRows
void SetRows(int num_rows)
Sets the number of rows of this component.
Definition: composite.cc:75
ifopt::Component::kSpecifyLater
static const int kSpecifyLater
Definition: composite.h:143
ifopt::Composite::PrintAll
void PrintAll() const
Definition: composite.cc:221
ifopt::Composite::AddComponent
void AddComponent(const Component::Ptr &)
Adds a component to this composite.
Definition: composite.cc:119
ifopt::Component::VecBound
std::vector< Bounds > VecBound
Definition: composite.h:69
ifopt::Composite::GetComponents
const ComponentVec GetComponents() const
Returns read access to the components.
Definition: composite.cc:216
ifopt::Composite::GetBounds
VecBound GetBounds() const override
Returns the "bounds" of this component.
Definition: composite.cc:205
ifopt::Composite::n_var
size_t n_var
Definition: composite.h:219
ifopt::Component::name_
std::string name_
Definition: composite.h:147
ifopt::Component::Print
virtual void Print(double tolerance, int &index_start) const
Prints the relevant information (name, rows, values) of this component.
Definition: composite.cc:85
ifopt::Composite::components_
ComponentVec components_
Definition: composite.h:215
ifopt::Composite::GetJacobian
Jacobian GetJacobian() const override
Returns derivatives of each row w.r.t. the variables.
Definition: composite.cc:174
ifopt::Component::Ptr
std::shared_ptr< Component > Ptr
Definition: composite.h:65
ifopt::Component::GetName
std::string GetName() const
Returns the name (id) of this component.
Definition: composite.cc:80
ifopt::Composite::SetVariables
void SetVariables(const VectorXd &x) override
Sets the optimization variables from an Eigen vector.
Definition: composite.cc:164
ifopt
common namespace for all elements in this library.
Definition: bounds.h:33
ifopt::Composite::is_cost_
bool is_cost_
Definition: composite.h:216
composite.h
Declares the classes Composite and Component used as variables, costs and constraints.
ifopt::Component::GetBounds
virtual VecBound GetBounds() const =0
Returns the "bounds" of this component.
ifopt::Component::Component
Component(int num_rows, const std::string &name)
Creates a component.
Definition: composite.cc:64
ifopt::Composite::ComponentVec
std::vector< Component::Ptr > ComponentVec
Definition: composite.h:163
ifopt::Component::VectorXd
Eigen::VectorXd VectorXd
Definition: composite.h:68
ifopt::Composite::GetComponent
const Component::Ptr GetComponent(std::string name) const
Access generic component with the specified name.
Definition: composite.cc:138
ifopt::Component::GetRows
int GetRows() const
Returns the number of rows of this component.
Definition: composite.cc:70
ifopt::Component::Jacobian
Eigen::SparseMatrix< double, Eigen::RowMajor > Jacobian
Definition: composite.h:67
ifopt::Composite::GetValues
VectorXd GetValues() const override
Returns the "values" of whatever this component represents.
Definition: composite.cc:148
ifopt::Composite::ClearComponents
void ClearComponents()
Removes all component from this composite.
Definition: composite.cc:132
ifopt::Component::num_rows_
int num_rows_
Definition: composite.h:146
ifopt::Component::GetValues
virtual VectorXd GetValues() const =0
Returns the "values" of whatever this component represents.


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