nodes_variables.cc
Go to the documentation of this file.
00001 /******************************************************************************
00002 Copyright (c) 2018, 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 <towr/variables/nodes_variables.h>
00031 
00032 namespace towr {
00033 
00034 
00035 NodesVariables::NodesVariables (const std::string& name)
00036     : VariableSet(kSpecifyLater, name)
00037 {
00038 }
00039 
00040 int
00041 NodesVariables::GetOptIndex(const NodeValueInfo& nvi_des) const
00042 {
00043   // could also cache this as map for more efficiency, but adding complexity
00044   for (int idx=0; idx<GetRows(); ++idx)
00045     for ( NodeValueInfo nvi : GetNodeValuesInfo(idx))
00046       if ( nvi == nvi_des )
00047         return idx;
00048 
00049   return NodeValueNotOptimized; // index representing these quantities doesn't exist
00050 }
00051 
00052 Eigen::VectorXd
00053 NodesVariables::GetValues () const
00054 {
00055   VectorXd x(GetRows());
00056 
00057   for (int idx=0; idx<x.rows(); ++idx)
00058     for (auto nvi : GetNodeValuesInfo(idx))
00059       x(idx) = nodes_.at(nvi.id_).at(nvi.deriv_)(nvi.dim_);
00060 
00061   return x;
00062 }
00063 
00064 void
00065 NodesVariables::SetVariables (const VectorXd& x)
00066 {
00067   for (int idx=0; idx<x.rows(); ++idx)
00068     for (auto nvi : GetNodeValuesInfo(idx))
00069       nodes_.at(nvi.id_).at(nvi.deriv_)(nvi.dim_) = x(idx);
00070 
00071   UpdateObservers();
00072 }
00073 
00074 void
00075 NodesVariables::UpdateObservers() const
00076 {
00077   for (auto& o : observers_)
00078     o->UpdateNodes();
00079 }
00080 
00081 void
00082 NodesVariables::AddObserver(ObserverPtr const o)
00083 {
00084    observers_.push_back(o);
00085 }
00086 
00087 int
00088 NodesVariables::GetNodeId (int poly_id, Side side)
00089 {
00090   return poly_id + side;
00091 }
00092 
00093 const std::vector<Node>
00094 NodesVariables::GetBoundaryNodes(int poly_id) const
00095 {
00096   std::vector<Node> nodes;
00097   nodes.push_back(nodes_.at(GetNodeId(poly_id, Side::Start)));
00098   nodes.push_back(nodes_.at(GetNodeId(poly_id, Side::End)));
00099   return nodes;
00100 }
00101 
00102 int
00103 NodesVariables::GetDim() const
00104 {
00105   return n_dim_;
00106 }
00107 
00108 int
00109 NodesVariables::GetPolynomialCount() const
00110 {
00111   return nodes_.size() - 1;
00112 }
00113 
00114 NodesVariables::VecBound
00115 NodesVariables::GetBounds () const
00116 {
00117   return bounds_;
00118 }
00119 
00120 const std::vector<Node>
00121 NodesVariables::GetNodes() const
00122 {
00123   return nodes_;
00124 }
00125 
00126 void
00127 NodesVariables::SetByLinearInterpolation(const VectorXd& initial_val,
00128                                          const VectorXd& final_val,
00129                                          double t_total)
00130 {
00131   // only set those that are part of optimization variables,
00132   // do not overwrite phase-based parameterization
00133   VectorXd dp = final_val-initial_val;
00134   VectorXd average_velocity = dp / t_total;
00135   int num_nodes = nodes_.size();
00136 
00137   for (int idx=0; idx<GetRows(); ++idx) {
00138     for (auto nvi : GetNodeValuesInfo(idx)) {
00139 
00140       if (nvi.deriv_ == kPos) {
00141         VectorXd pos = initial_val + nvi.id_/static_cast<double>(num_nodes-1)*dp;
00142         nodes_.at(nvi.id_).at(kPos)(nvi.dim_) = pos(nvi.dim_);
00143       }
00144 
00145       if (nvi.deriv_ == kVel) {
00146         nodes_.at(nvi.id_).at(kVel)(nvi.dim_) = average_velocity(nvi.dim_);
00147       }
00148     }
00149   }
00150 }
00151 
00152 void
00153 NodesVariables::AddBounds(int node_id, Dx deriv,
00154                  const std::vector<int>& dimensions,
00155                  const VectorXd& val)
00156 {
00157   for (auto dim : dimensions)
00158     AddBound(NodeValueInfo(node_id, deriv, dim), val(dim));
00159 }
00160 
00161 void
00162 NodesVariables::AddBound (const NodeValueInfo& nvi_des, double val)
00163 {
00164   for (int idx=0; idx<GetRows(); ++idx)
00165     for (auto nvi : GetNodeValuesInfo(idx))
00166       if (nvi == nvi_des)
00167         bounds_.at(idx) = ifopt::Bounds(val, val);
00168 }
00169 
00170 void
00171 NodesVariables::AddStartBound (Dx d, const std::vector<int>& dimensions, const VectorXd& val)
00172 {
00173   AddBounds(0, d, dimensions, val);
00174 }
00175 
00176 void
00177 NodesVariables::AddFinalBound (Dx deriv, const std::vector<int>& dimensions,
00178                       const VectorXd& val)
00179 {
00180   AddBounds(nodes_.size()-1, deriv, dimensions, val);
00181 }
00182 
00183 NodesVariables::NodeValueInfo::NodeValueInfo(int node_id, Dx deriv, int node_dim)
00184 {
00185   id_    = node_id;
00186   deriv_ = deriv;
00187   dim_   = node_dim;
00188 }
00189 
00190 int
00191 NodesVariables::NodeValueInfo::operator==(const NodeValueInfo& right) const
00192 {
00193   return (id_    == right.id_)
00194       && (deriv_ == right.deriv_)
00195       && (dim_   == right.dim_);
00196 };
00197 
00198 } /* namespace towr */


towr
Author(s): Alexander W. Winkler
autogenerated on Mon Apr 15 2019 02:42:32