leaves.cc
Go to the documentation of this file.
00001 /******************************************************************************
00002 Copyright (c) 2017, Alexander W Winkler. All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 
00010 * Redistributions in binary form must reproduce the above copyright notice,
00011   this list of conditions and the following disclaimer in the documentation
00012   and/or other materials provided with the distribution.
00013 
00014 * Neither the name of the copyright holder nor the names of its
00015   contributors may be used to endorse or promote products derived from
00016   this software without specific prior written permission.
00017 
00018 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00021 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00022 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00023 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00024 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00025 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00027 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 ******************************************************************************/
00029 
00030 #include <ifopt/variable_set.h>
00031 #include <ifopt/constraint_set.h>
00032 #include <ifopt/cost_term.h>
00033 
00034 #include <iostream>
00035 #include <iomanip>
00036 
00037 
00038 namespace ifopt {
00039 
00040 VariableSet::VariableSet(int n_var, const std::string& name)
00041     : Component(n_var, name)
00042 {
00043 }
00044 
00045 ConstraintSet::ConstraintSet (int row_count, const std::string& name)
00046     : Component(row_count, name)
00047 {
00048 }
00049 
00050 ConstraintSet::Jacobian
00051 ConstraintSet::GetJacobian () const
00052 {
00053   Jacobian jacobian(GetRows(), variables_->GetRows());
00054 
00055   int col = 0;
00056   Jacobian jac;
00057   std::vector< Eigen::Triplet<double> > triplet_list;
00058 
00059   for (const auto& vars : variables_->GetComponents()) {
00060     int n = vars->GetRows();
00061     jac.resize(GetRows(), n);
00062 
00063     FillJacobianBlock(vars->GetName(), jac);
00064     // reserve space for the new elements to reduce memory allocation
00065     triplet_list.reserve(triplet_list.size()+jac.nonZeros());
00066 
00067     // create triplets for the derivative at the correct position in the overall Jacobian
00068     for (int k=0; k<jac.outerSize(); ++k)
00069       for (Jacobian::InnerIterator it(jac,k); it; ++it)
00070         triplet_list.push_back(Eigen::Triplet<double>(it.row(), col+it.col(), it.value()));
00071     col += n;
00072   }
00073 
00074   // transform triplet vector into sparse matrix
00075   jacobian.setFromTriplets(triplet_list.begin(), triplet_list.end());
00076   return jacobian;
00077 }
00078 
00079 void
00080 ConstraintSet::LinkWithVariables(const VariablesPtr& x)
00081 {
00082   variables_ = x;
00083   InitVariableDependedQuantities(x);
00084 }
00085 
00086 CostTerm::CostTerm (const std::string& name) :ConstraintSet(1, name)
00087 {
00088 }
00089 
00090 CostTerm::VectorXd
00091 CostTerm::GetValues() const
00092 {
00093   VectorXd cost(1);
00094   cost(0) = GetCost();
00095   return cost;
00096 }
00097 
00098 CostTerm::VecBound
00099 CostTerm::GetBounds() const
00100 {
00101   return VecBound(GetRows(), NoBound);
00102 }
00103 
00104 void
00105 CostTerm::Print (double tol, int& index) const
00106 {
00107   // only one scalar cost value
00108   double cost = GetValues()(0);
00109 
00110   std::cout.precision(2);
00111   std::cout << std::fixed
00112             << std::left
00113             << std::setw(30) << GetName()
00114             << std::right
00115             << std::setw(4) << GetRows()
00116             << std::setw(9) << index
00117             << std::setfill ('.')
00118             << std::setw(7) << index+GetRows()-1
00119             << std::setfill (' ')
00120             << std::setw(12) << cost
00121             << std::endl;
00122 }
00123 
00124 } /* namespace opt */


ifopt
Author(s): Alexander W. Winkler
autogenerated on Sat May 18 2019 02:43:08