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 <iostream>
33 
34 namespace ifopt {
35 
36 Component::Component (int num_rows, const std::string& name)
37 {
38  num_rows_ = num_rows;
39  name_ = name;
40 }
41 
42 int
44 {
45  return num_rows_;
46 }
47 
48 void
49 Component::SetRows (int num_rows)
50 {
51  num_rows_ = num_rows;
52 }
53 
54 std::string
56 {
57  return name_;
58 }
59 
60 Composite::Composite (const std::string& name, bool is_cost) :Component(0, name)
61 {
62  is_cost_ = is_cost;
63 }
64 
65 void
67 {
68  // at this point the number of rows must be specified.
69  assert(c->GetRows() != kSpecifyLater);
70 
71  components_.push_back(c);
72 
73  if (is_cost_)
74  SetRows(1);
75  else
76  SetRows(GetRows()+ c->GetRows());
77 }
78 
79 void
81 {
82  components_.clear();
83  SetRows(0);
84 }
85 
86 const Component::Ptr
87 Composite::GetComponent (std::string name) const
88 {
89  for (const auto& c : components_)
90  if (c->GetName() == name)
91  return c;
92 
93  assert(false); // component with name doesn't exist, abort program
94  return Component::Ptr();
95 }
96 
99 {
100  VectorXd g_all = VectorXd::Zero(GetRows());
101 
102  int row = 0;
103  for (const auto& c : components_) {
104 
105  int n_rows = c->GetRows();
106  VectorXd g = c->GetValues();
107  g_all.middleRows(row, n_rows) += g;
108 
109  if (!is_cost_)
110  row += n_rows;
111  }
112  return g_all;
113 }
114 
115 void
117 {
118  int row = 0;
119  for (auto& c : components_) {
120 
121  int n_rows = c->GetRows();
122  c->SetVariables(x.middleRows(row,n_rows));
123  row += n_rows;
124  }
125 }
126 
129 {
130  int n_var = components_.front()->GetJacobian().cols();
131  Jacobian jacobian(GetRows(), n_var);
132 
133  int row = 0;
134  for (const auto& c : components_) {
135 
136  const Jacobian& jac = c->GetJacobian();
137  for (int k=0; k<jac.outerSize(); ++k)
138  for (Jacobian::InnerIterator it(jac,k); it; ++it)
139  jacobian.coeffRef(row+it.row(), it.col()) += it.value();
140 
141  if (!is_cost_)
142  row += c->GetRows();
143  }
144 
145  return jacobian;
146 }
147 
150 {
151  VecBound bounds_;
152  for (const auto& c : components_) {
153  VecBound b = c->GetBounds();
154  bounds_.insert(bounds_.end(), b.begin(), b.end());
155  }
156 
157  return bounds_;
158 }
159 
162 {
163  return components_;
164 }
165 
166 // some printouts for convenience
167 static int print_counter = 0;
168 void
170 {
171  print_counter = 0;
172 
173  std::cout << GetName() << ":\n";
174  for (auto c : components_) {
175  std::cout << " "; // indent components
176  c->Print();
177  }
178  std::cout << std::endl;
179 }
180 
181 void Component::Print () const
182 {
183  int print_rows = 3;
184  std::string end_string = ", ...";
185 
186  if (num_rows_ < print_rows) {
187  print_rows = num_rows_;
188  end_string.clear(); // all variables printed
189  }
190 
191  // calculate squared bound violation
192  VectorXd x = GetValues();
193  VecBound bounds = GetBounds();
194 
195  std::vector<int> viol_idx;
196  double eps = 0.001; // from ipopt config file
197  for (uint i=0; i<bounds.size(); ++i) {
198  double lower = bounds.at(i).lower_;
199  double upper = bounds.at(i).upper_;
200  double val = x(i);
201  if (val < lower-eps || upper+eps < val)
202  viol_idx.push_back(i); // constraint out of bounds
203  }
204 
205 
206  std::cout.precision(2);
207  std::cout << std::fixed;
208  // https://stackoverflow.com/questions/2616906/how-do-i-output-coloured-text-to-a-linux-terminal
209  std::string black = "\033[0m";
210  std::string red = "\033[31m";
211  std::string color = viol_idx.empty()? black : red;
212  std::cout << name_ << "\t(";
213  std::cout << num_rows_ << ", " << print_counter << "-" << print_counter+num_rows_;
214  std::cout << ", " << color << "nr_violated=" << viol_idx.size() << " ( ";
215  uint i_print = 4;
216  int nr_indices_print = viol_idx.size()<i_print? viol_idx.size() : i_print;
217  for (int i=0; i<nr_indices_print; ++i)
218  std::cout << viol_idx.at(i) << ", ";
219  std::cout << ")";
220  std::cout << black;
221  std::cout << ":\t";
222 
223  print_counter += num_rows_;
224 
225  VectorXd val = GetValues().topRows(print_rows);
226  if (val.rows() > 0)
227  std::cout << val(0);
228  for (int i=1; i<val.rows(); ++i)
229  std::cout << ",\t" << val(i);
230 
231  std::cout << end_string << std::endl;
232 }
233 
234 } /* namespace opt */
VecBound GetBounds() const override
Returns the "bounds" of this component.
Definition: composite.cc:149
static int print_counter
Definition: composite.cc:167
const Component::Ptr GetComponent(std::string name) const
Access generic component with the specified name.
Definition: composite.cc:87
std::string name_
Definition: composite.h:153
static const int kSpecifyLater
Definition: composite.h:149
const ComponentVec GetComponents() const
Returns read access to the components.
Definition: composite.cc:161
Component(int num_rows, const std::string &name)
Creates a component.
Definition: composite.cc:36
std::vector< Component::Ptr > ComponentVec
Definition: composite.h:171
void ClearComponents()
Removes all component from this composite.
Definition: composite.cc:80
std::string GetName() const
Returns the name (id) of this component.
Definition: composite.cc:55
std::shared_ptr< Component > Ptr
Definition: composite.h:73
Declares the classes Composite and Component used as variables, costs and constraints.
VectorXd GetValues() const override
Returns the "values" of whatever this component represents.
Definition: composite.cc:98
void SetRows(int num_rows)
Sets the number of rows of this component.
Definition: composite.cc:49
ComponentVec components_
Definition: composite.h:223
void AddComponent(const Component::Ptr &)
Adds a component to this composite.
Definition: composite.cc:66
Eigen::SparseMatrix< double, Eigen::RowMajor > Jacobian
Definition: composite.h:75
Definition: bounds.h:33
Interface representing either Variable, Cost or Constraint.
Definition: composite.h:71
void Print() const override
Prints the relevant information (name, rows, values) of this component.
Definition: composite.cc:169
void SetVariables(const VectorXd &x) override
Sets the optimization variables from an Eigen vector.
Definition: composite.cc:116
Jacobian GetJacobian() const override
Returns derivatives of each row w.r.t. the variables.
Definition: composite.cc:128
virtual void Print() const
Prints the relevant information (name, rows, values) of this component.
Definition: composite.cc:181
Composite(const std::string &name, bool is_cost)
Creates a Composite holding either variables, costs or constraints.
Definition: composite.cc:60
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 Fri Apr 20 2018 02:27:34