vector_bound_box.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, The Regents of the University of California (Regents).
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following
14  * disclaimer in the documentation and/or other materials provided
15  * with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Please contact the author(s) of this library if you have any questions.
34  * Authors: David Fridovich-Keil ( dfk@eecs.berkeley.edu )
35  */
36 
38 //
39 // Class to specify a box constraint on a vector-valued control variable.
40 //
42 
43 #ifndef FASTRACK_CONTROL_VECTOR_BOUND_BOX_H
44 #define FASTRACK_CONTROL_VECTOR_BOUND_BOX_H
45 
48 
49 namespace fastrack {
50 namespace control {
51 
52 class VectorBoundBox : public ControlBound<VectorXd> {
53  public:
55  explicit VectorBoundBox(const VectorXd& min, const VectorXd& max)
56  : min_(min), max_(max) {
57  if (min_.size() != max_.size())
58  throw std::runtime_error("Inconsistent bound dimensions.");
59  }
60 
61  // Assume 'params' is laid out such that the first half of the parameters
62  // form the 'min_' and the second form the 'max_'.
63  explicit VectorBoundBox(const std::vector<double>& params) {
64  if (!params.size() || params.size() & 1)
65  throw std::runtime_error("Incorrect number of parameters.");
66 
67  const size_t dimension = params.size() >> 1;
68 
69  min_.resize(dimension);
70  for (size_t ii = 0; ii < dimension; ii++) min_(ii) = params[ii];
71 
72  max_.resize(dimension);
73  for (size_t ii = 0; ii < dimension; ii++) max_(ii) = params[dimension + ii];
74  }
75 
76  // Accessors.
77  inline const VectorXd& Min() const { return min_; }
78  inline const VectorXd& Max() const { return max_; }
79 
80  // Custom definition of copy-assign operator.
82  if (&other == this) return *this;
83 
84  min_ = other.min_;
85  max_ = other.max_;
86  return *this;
87  }
88 
89  // Derived classes must be able to check whether a query is inside the bound.
90  inline bool Contains(const VectorXd& query) const {
91  if (min_.size() != query.size())
92  throw std::runtime_error("Incorrect query dimension.");
93 
94  for (size_t ii = 0; ii < min_.size(); ii++) {
95  if (min_(ii) > query(ii) || query(ii) > max_(ii)) return false;
96  }
97 
98  return true;
99  }
100 
101  // Derived classes must be able to compute the projection of a vector
102  // (represented as the templated type) onto the surface of the bound.
103  // NOTE: this is basically solving an LP with the bound as the feasible
104  // set and the query as the coefficients.
105  inline VectorXd ProjectToSurface(const VectorXd& query) const {
106  if (min_.size() != query.size())
107  throw std::runtime_error("Incorrect query dimension.");
108 
109  VectorXd projection(min_.size());
110  for (size_t ii = 0; ii < min_.size(); ii++)
111  projection(ii) = (query(ii) >= 0.0) ? max_(ii) : min_(ii);
112 
113  return projection;
114  }
115 
116  private:
117  // Lower and upper bounds..
118  VectorXd min_, max_;
119 }; //\class ControlBound
120 
121 } // namespace control
122 } // namespace fastrack
123 
124 #endif
VectorXd ProjectToSurface(const VectorXd &query) const
VectorBoundBox & operator=(const VectorBoundBox &other)
Definition: box.h:53
const VectorXd & Min() const
VectorBoundBox(const VectorXd &min, const VectorXd &max)
bool Contains(const VectorXd &query) const
const VectorXd & Max() const
VectorBoundBox(const std::vector< double > &params)


fastrack
Author(s): David Fridovich-Keil
autogenerated on Mon Aug 3 2020 21:28:37