scalar_vertex.h
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement
4  *
5  * Copyright (c) 2020,
6  * TU Dortmund - Institute of Control Theory and Systems Engineering.
7  * All rights reserved.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  *
22  * Authors: Christoph Rösmann
23  *********************************************************************/
24 
25 #ifndef SRC_OPTIMIZATION_INCLUDE_CORBO_OPTIMIZATION_HYPER_GRAPH_SCALAR_VERTEX_H_
26 #define SRC_OPTIMIZATION_INCLUDE_CORBO_OPTIMIZATION_HYPER_GRAPH_SCALAR_VERTEX_H_
27 
29 
30 #include <corbo-core/types.h>
31 
32 #include <memory>
33 #include <vector>
34 
35 namespace corbo {
36 
51 {
52  public:
53  using Ptr = std::shared_ptr<ScalarVertex>;
54  using UPtr = std::unique_ptr<ScalarVertex>;
55 
59  explicit ScalarVertex(double value) : _value(value), _lb(-CORBO_INF_DBL), _ub(CORBO_INF_DBL) {}
60 
62  explicit ScalarVertex(double value, bool fixed) : _value(value), _lb(-CORBO_INF_DBL), _ub(CORBO_INF_DBL), _fixed(fixed) {}
63 
65  explicit ScalarVertex(double value, double lb, double ub) : _value(value), _lb(lb), _ub(ub) {}
66 
68  explicit ScalarVertex(double value, double lb, double ub, bool fixed) : _value(value), _lb(lb), _ub(ub), _fixed(fixed) {}
69 
70  // implements interface method
71  int getDimension() const override { return 1; }
72  // implements interface method
73  int getDimensionUnfixed() const override { return _fixed ? 0 : 1; }
74 
75  // implements interface method
76  void plus(int /*idx*/, double inc) override { _value += inc; }
77  // implements interface method
78  void plus(const double* inc) override { _value += *inc; }
79  // implements interface method
80  void plusUnfixed(const double* inc) override { _value += *inc; }
81 
82  // implements interface method
83  const double* getData() const override { return &_value; }
84  double* getDataRaw() override { return &_value; }
85 
86  // implements interface method
87  void setData(int /*idx*/, double data) override { _value = data; }
88 
89  // directly set all properties
90  void set(double value, double lb, double ub, bool fixed)
91  {
92  _value = value;
93  _lb = lb;
94  _ub = ub;
95  _fixed = fixed;
96  }
97 
98  // implements interface method
99  bool hasFixedComponents() const override { return _fixed; }
100  // implements interface method
101  bool isFixedComponent(int /*idx*/) const override { return _fixed; }
102 
104  void setLowerBound(double lb) { _lb = lb; }
106  void setUpperBound(double ub) { _ub = ub; }
107  // implements interface method
108  void setLowerBounds(const Eigen::Ref<const Eigen::VectorXd>& lb) override { _lb = lb[0]; }
109  // implements interface method
110  void setUpperBounds(const Eigen::Ref<const Eigen::VectorXd>& ub) override { _ub = ub[0]; }
111  // implements interface method
112  void setLowerBound(int /*idx*/, double lb) override { _lb = lb; }
113  // implements interface method
114  void setUpperBound(int /*idx*/, double ub) override { _ub = ub; }
115  // implements interface method
116  bool hasFiniteBounds() const override { return _lb > -CORBO_INF_DBL || _ub < CORBO_INF_DBL; }
117  // implements interface method
118  bool hasFiniteLowerBounds() const override { return _lb > -CORBO_INF_DBL; }
119  // implements interface method
120  bool hasFiniteUpperBounds() const override { return _ub < CORBO_INF_DBL; }
121  // implements interface method
122  bool hasFiniteLowerBound(int /*idx*/) const override { return _lb > -CORBO_INF_DBL; }
123  // implements interface method
124  bool hasFiniteUpperBound(int /*idx*/) const override { return _ub < CORBO_INF_DBL; }
125  // implements interface method
126  int getNumberFiniteLowerBounds(bool unfixed_only) const override
127  {
128  if (unfixed_only && _fixed)
129  return 0;
130  else
131  return (int)hasFiniteLowerBounds();
132  }
133  // implements interface method
134  int getNumberFiniteUpperBounds(bool unfixed_only) const override
135  {
136  if (unfixed_only && _fixed)
137  return 0;
138  else
139  return (int)hasFiniteUpperBounds();
140  }
141  // implements interface method
142  int getNumberFiniteBounds(bool unfixed_only) const override
143  {
144  if (unfixed_only && _fixed)
145  return 0;
146  else
147  return (int)hasFiniteBounds();
148  }
149 
151  void setFixed(bool fixed) { _fixed = fixed; }
152 
153  // implements interface method
154  const double* getLowerBounds() const override { return &_lb; }
155  // implements interface method
156  const double* getUpperBounds() const override { return &_ub; }
157 
158  // Backup values
159  // implements interface method
160  void push() override { _backup.push_back(_value); }
161  // implements interface method
162  void pop() override
163  {
164  top();
165  _backup.pop_back();
166  }
167  // implements interface method
168  void top() override
169  {
170  assert(!_backup.empty());
171  _value = _backup.back();
172  }
173  // implements interface method
174  void discardTop() override { _backup.pop_back(); }
175  // implements interface method
176  void clear() override { _backup.clear(); }
177  // implements interface method
178  int getNumBackups() const override { return (int)_backup.size(); }
179 
181  const double& value() const { return _value; }
183  double& value() { return _value; }
184 
186  const double& values() const { return _value; }
188  double& values() { return _value; }
189 
190  protected:
191  double _value;
192  double _lb;
193  double _ub;
194 
195  bool _fixed = false;
196 
197  std::vector<double> _backup;
198 };
199 
200 } // namespace corbo
201 
202 #endif // SRC_OPTIMIZATION_INCLUDE_CORBO_OPTIMIZATION_HYPER_GRAPH_SCALAR_VERTEX_H_
double * getDataRaw() override
Get write access to the values of the vertex.
Definition: scalar_vertex.h:84
const double & values() const
Get underlying value (this method is for compatibility purposes)
std::shared_ptr< ScalarVertex > Ptr
Definition: scalar_vertex.h:53
const double * getUpperBounds() const override
Read-only raw access to upper bounds [getDimension() x 1].
bool hasFixedComponents() const override
Check if the vertex has fixed components.
Definition: scalar_vertex.h:99
void setFixed(bool fixed)
Set vertex (un)fixed.
std::vector< double > _backup
double & values()
Raw access to the underlying value (this method is for compatibility purposes)
void setLowerBounds(const Eigen::Ref< const Eigen::VectorXd > &lb) override
Define lower bounds on the vertex values [getDimension() x 1].
std::unique_ptr< ScalarVertex > UPtr
Definition: scalar_vertex.h:54
ScalarVertex(double value, bool fixed)
Construct vertex with given value and fixed flag.
Definition: scalar_vertex.h:62
Generic interface class for vertices.
bool hasFiniteUpperBounds() const override
Check if finite upper bounds are provided.
bool hasFiniteBounds() const override
Check if finite bounds (lower or upper) are provided.
int getNumberFiniteBounds(bool unfixed_only) const override
Get number of finite upper bounds (either upper or lower must be finite)
const double * getLowerBounds() const override
Read-only raw access to lower bounds [getDimension() x 1].
Vertex implementation for scalar values.
Definition: scalar_vertex.h:50
double & value()
Raw access to the underlying value.
int getNumberFiniteLowerBounds(bool unfixed_only) const override
Get number of finite lower bounds.
bool hasFiniteUpperBound(int) const override
Check if finite upper bound for a single component is provided.
ScalarVertex(double value, double lb, double ub)
Construct vertex with given value, lower and upper bound.
Definition: scalar_vertex.h:65
int getNumBackups() const override
Return the current size/number of backups of the backup stack.
int getDimension() const override
Return number of elements/values/components stored in this vertex.
Definition: scalar_vertex.h:71
void plus(const double *inc) override
Define the increment for the vertex: x += inc with dim(inc)=getDimension().
Definition: scalar_vertex.h:78
void setData(int, double data) override
Write data to to a specific component.
Definition: scalar_vertex.h:87
bool hasFiniteLowerBounds() const override
Check if finite lower bounds are provided.
constexpr const double CORBO_INF_DBL
Representation for infinity (double version)
void discardTop() override
Delete the previously made backup from the stack without restoring it.
const double & value() const
Get underlying value.
void clear() override
Clear complete backup container.
int getNumberFiniteUpperBounds(bool unfixed_only) const override
Get number of finite upper bounds.
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:192
void setUpperBounds(const Eigen::Ref< const Eigen::VectorXd > &ub) override
Define upper bounds on the vertex values [getDimension() x 1].
bool isFixedComponent(int) const override
Check if individual components are fixed or unfixed.
void setUpperBound(double ub)
Set upper bound.
void pop() override
Restore the previously stored values of the backup stack and remove them from the stack...
ScalarVertex(double value, double lb, double ub, bool fixed)
Construct vertex with given value, lower and upper bound and fixed flag.
Definition: scalar_vertex.h:68
void setLowerBound(double lb)
Set lower bound.
int getDimensionUnfixed() const override
Return number of unfixed elements (unfixed elements are skipped as parameters in the Hessian and Jaco...
Definition: scalar_vertex.h:73
ScalarVertex(double value)
Construct vertex with given value.
Definition: scalar_vertex.h:59
void plusUnfixed(const double *inc) override
Define the increment for the unfixed components of the vertex: x += inc with dim(inc)=getDimensionUnf...
Definition: scalar_vertex.h:80
void top() override
Restore the previously stored values of the backup stack WITHOUT removing them from the stack...
void setUpperBound(int, double ub) override
Define upper bound on a single component of the vertex (0 <= idx < getDimension()) ...
void push() override
Store all values into a internal backup stack.
const double * getData() const override
Get read-only raw access to the values of the vertex.
Definition: scalar_vertex.h:83
void setLowerBound(int, double lb) override
Define lower bound on a single component of the vertex (0 <= idx < getDimension()) ...
bool hasFiniteLowerBound(int) const override
Check if finite lower bound for a single component is provided.
void plus(int, double inc) override
Add value to a specific component of the vertex: x[idx] += inc.
Definition: scalar_vertex.h:76
ScalarVertex()
Default constructor.
Definition: scalar_vertex.h:57


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Mon Feb 28 2022 22:07:16