|
| 1 | +/****************************************************************************** |
| 2 | +Copyright (c) 2018, 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 | +#ifndef TRAJOPT_IFOPT_NODES_VARIABLES_H |
| 31 | +#define TRAJOPT_IFOPT_NODES_VARIABLES_H |
| 32 | + |
| 33 | +#include <trajopt_common/macros.h> |
| 34 | +TRAJOPT_IGNORE_WARNINGS_PUSH |
| 35 | +#include <ifopt/variable_set.h> |
| 36 | +#include <ifopt/bounds.h> |
| 37 | +#include <Eigen/Core> |
| 38 | +TRAJOPT_IGNORE_WARNINGS_POP |
| 39 | + |
| 40 | +#include <trajopt_ifopt/variable_sets/node.h> |
| 41 | + |
| 42 | +namespace trajopt_ifopt |
| 43 | +{ |
| 44 | +class NodesObserver; |
| 45 | + |
| 46 | +class NodesVariables : public ifopt::VariableSet |
| 47 | +{ |
| 48 | +public: |
| 49 | + using Ptr = std::shared_ptr<NodesVariables>; |
| 50 | + |
| 51 | + /** |
| 52 | + * @brief Add node to the variable set |
| 53 | + * @param node The node to append |
| 54 | + */ |
| 55 | + void AddNode(std::unique_ptr<Node> node); |
| 56 | + |
| 57 | + /** |
| 58 | + * @brief Get node based on index |
| 59 | + * @param opt_idx The node index |
| 60 | + * @return The node |
| 61 | + */ |
| 62 | + std::shared_ptr<const Node> GetNode(std::size_t opt_idx) const; |
| 63 | + |
| 64 | + /** |
| 65 | + * @brief Pure optimization variables that define the nodes. |
| 66 | + * |
| 67 | + * Not all node position and velocities are independent or optimized over, so |
| 68 | + * usually the number of optimization variables is less than all nodes' pos/vel. |
| 69 | + * |
| 70 | + * @sa GetNodeInfoAtOptIndex() |
| 71 | + */ |
| 72 | + VectorXd GetValues() const override; |
| 73 | + |
| 74 | + /** |
| 75 | + * @brief Sets some node positions and velocity from the optimization variables. |
| 76 | + * @param x The optimization variables. |
| 77 | + * |
| 78 | + * Not all node position and velocities are independent or optimized over, so |
| 79 | + * usually the number of optimization variables is less than |
| 80 | + * all nodes pos/vel. |
| 81 | + * |
| 82 | + * @sa GetNodeValuesInfo() |
| 83 | + */ |
| 84 | + void SetVariables(const VectorXd& x) override; |
| 85 | + |
| 86 | + /** |
| 87 | + * @returns the bounds on position and velocity of each node and dimension. |
| 88 | + */ |
| 89 | + VecBound GetBounds() const override; |
| 90 | + |
| 91 | + /** |
| 92 | + * @returns All the nodes that can be used to reconstruct the spline. |
| 93 | + */ |
| 94 | + std::vector<std::shared_ptr<const Node>> GetNodes() const; |
| 95 | + |
| 96 | + /** |
| 97 | + * @brief Adds a dependent observer that gets notified when the nodes change. |
| 98 | + * @param spline Usually a pointer to a spline which uses the node values. |
| 99 | + */ |
| 100 | + void AddObserver(std::shared_ptr<NodesObserver> observer); |
| 101 | + |
| 102 | + /** |
| 103 | + * @returns The dimensions (x,y,z) of every node. |
| 104 | + */ |
| 105 | + Eigen::Index GetDim() const; |
| 106 | + |
| 107 | +protected: |
| 108 | + /** |
| 109 | + * @param n_dim The number of dimensions (x,y,..) each node has. |
| 110 | + * @param variable_name The name of the variables in the optimization problem. |
| 111 | + */ |
| 112 | + NodesVariables(const std::string& variable_name); |
| 113 | + virtual ~NodesVariables() = default; |
| 114 | + |
| 115 | + Eigen::VectorXd values_; |
| 116 | + VecBound bounds_; ///< the bounds on the node values. |
| 117 | + std::vector<std::shared_ptr<Node>> nodes_; |
| 118 | + Eigen::Index n_dim_{ 0 }; |
| 119 | + std::vector<std::shared_ptr<NodesObserver>> observers_; |
| 120 | + |
| 121 | + /** @brief Notifies the subscribed observers that the node values changes. */ |
| 122 | + void UpdateObservers(); |
| 123 | + |
| 124 | + // /** |
| 125 | + // * @brief Bounds a specific node variables. |
| 126 | + // * @param node_id The ID of the node to bound. |
| 127 | + // * @param deriv The derivative of the node to set. |
| 128 | + // * @param dim The dimension of the node to bound. |
| 129 | + // * @param values The values to set the bounds to. |
| 130 | + // */ |
| 131 | + // void AddBounds(int node_id, Dx deriv, const std::vector<int>& dim, |
| 132 | + // const VectorXd& values); |
| 133 | + // /** |
| 134 | + // * @brief Restricts a specific optimization variables. |
| 135 | + // * @param node_info The specs of the optimization variables to restrict. |
| 136 | + // * @param value The value to set the bounds to. |
| 137 | + // */ |
| 138 | + // void AddBound(const NodeValueInfo& node_info, double value); |
| 139 | +}; |
| 140 | + |
| 141 | +} // namespace trajopt_ifopt |
| 142 | + |
| 143 | +#endif // TRAJOPT_IFOPT_NODES_VARIABLES_H |
0 commit comments