MAiNGO
babNode.h
Go to the documentation of this file.
1 /**********************************************************************************
2  * Copyright (c) 2019 Process Systems Engineering (AVT.SVT), RWTH Aachen University
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License 2.0 which is available at
6  * http://www.eclipse.org/legal/epl-2.0.
7  *
8  * SPDX-License-Identifier: EPL-2.0
9  *
10  * @file babNode.h
11  *
12  * @brief File defining the class for Branch-and-Bound nodes
13  *
14  **********************************************************************************/
15 
16 #pragma once
17 
18 #include "babOptVar.h"
19 #include "babUtils.h"
20 
21 #include <limits>
22 #include <string>
23 
24 
25 namespace babBase {
26 
27 
35 class BabNode {
36 
37  public:
48  BabNode(double pruningScoreIn, const std::vector<double>& lbdsIn, const std::vector<double>& ubdsIn, const int idIn, const unsigned depthIn, const bool holdsIncumbent):
49  _pruningScore(pruningScoreIn), _lowerBounds(lbdsIn), _upperBounds(ubdsIn), _idNumber(idIn), _depth(depthIn), _holdsIncumbent(holdsIncumbent) {}
50 
60  BabNode(double pruningScoreIn, const std::vector<OptimizationVariable>& variablesIn, const int idIn, const unsigned depthIn, const bool holdsIncumbent):
61  _pruningScore(pruningScoreIn), _idNumber(idIn), _depth(depthIn), _holdsIncumbent(holdsIncumbent)
62  {
63  size_t nVar = variablesIn.size();
64  _lowerBounds.resize(nVar);
65  _upperBounds.resize(nVar);
66  for (size_t iVar = 0; iVar < nVar; iVar++) {
67  _lowerBounds[iVar] = variablesIn[iVar].get_lower_bound();
68  _upperBounds[iVar] = variablesIn[iVar].get_upper_bound();
69  }
70  }
71 
75  BabNode() {}
76 
80  double get_pruning_score() const { return _pruningScore; }
81 
85  void set_pruning_score(double pruningScoreIn) { _pruningScore = pruningScoreIn; }
86 
90  std::vector<double> get_lower_bounds() const { return _lowerBounds; }
91 
95  std::vector<double> get_upper_bounds() const { return _upperBounds; }
96 
100  int get_ID() const { return _idNumber; };
101 
105  int get_depth() const { return _depth; };
106 
110  bool holds_incumbent() const { return _holdsIncumbent; }
111 
115  void set_holds_incumbent(const bool holdsIncumbent) { _holdsIncumbent = holdsIncumbent; }
116 
120  void set_upper_bound(const std::vector<double> upperBounds) { _upperBounds = upperBounds; }
121 
125  void set_upper_bound(const unsigned iVar, const double value) { _upperBounds[iVar] = value; }
126 
130  void set_lower_bound(const std::vector<double> lowerBounds) { _lowerBounds = lowerBounds; }
131 
135  void set_lower_bound(const unsigned iVar, const double value) { _lowerBounds[iVar] = value; }
136 
141  friend std::ostream& operator<<(std::ostream& out, const BabNode& node);
142 
143  private:
148  std::vector<double> _lowerBounds;
149  std::vector<double> _upperBounds;
150  int _idNumber;
151  unsigned _depth;
152  double _pruningScore;
155 };
156 
163 inline std::ostream&
164 operator<<(std::ostream& out, const BabNode& node)
165 {
166  std::string str;
167  if (node.holds_incumbent()) {
168  str = "yes";
169  }
170  else {
171  str = "no";
172  }
173  out << "BabNode id: " << node.get_ID() << ", pruning score: " << node.get_pruning_score() << ", hold incumbent: " << str << "\n";
174  for (unsigned int i = 0; i < node.get_lower_bounds().size(); i++) {
175  out << "lb[" << i << "]: " << std::setprecision(16) << node.get_lower_bounds()[i] << " .. " << node.get_upper_bounds()[i] << " :[" << i << "]ub\n";
176  }
177  return out;
178 }
179 
180 
181 } // end namespace babBase
int _idNumber
Definition: babNode.h:150
void set_lower_bound(const std::vector< double > lowerBounds)
Function for setting the whole upper bound vector.
Definition: babNode.h:130
int get_ID() const
Function for querying the node ID.
Definition: babNode.h:100
friend std::ostream & operator<<(std::ostream &out, const BabNode &node)
Overloaded operator for easier output. Definition of this operator is in bab.cpp.
Definition: babNode.h:164
Class representing a node in the Branch-and-Bound tree.
Definition: babNode.h:35
BabNode()
Default constructor.
Definition: babNode.h:75
namespace holding all essentials of the babBase submodule
Definition: babBrancher.h:40
bool _holdsIncumbent
Definition: babNode.h:153
std::vector< double > get_lower_bounds() const
Function for querying the lower bounds on the optimization variables within this node.
Definition: babNode.h:90
std::vector< double > _lowerBounds
Definition: babNode.h:148
std::vector< double > _upperBounds
Definition: babNode.h:149
void set_upper_bound(const std::vector< double > upperBounds)
Function for setting the whole upper bound vector.
Definition: babNode.h:120
int get_depth() const
Function for querying the node depth.
Definition: babNode.h:105
unsigned _depth
Definition: babNode.h:151
std::ostream & operator<<(std::ostream &out, const BabNode &node)
operator << overloaded for BabNode for easier output
Definition: babNode.h:164
bool holds_incumbent() const
Function obtaining information whether the node holds the incumbent.
Definition: babNode.h:110
void set_upper_bound(const unsigned iVar, const double value)
Function for setting the whole upper bound vector.
Definition: babNode.h:125
BabNode(double pruningScoreIn, const std::vector< OptimizationVariable > &variablesIn, const int idIn, const unsigned depthIn, const bool holdsIncumbent)
Constructor for initializing a BabNode using a vector of OptimizationVariable (each of which contains...
Definition: babNode.h:60
void set_pruning_score(double pruningScoreIn)
Function for setting the pruning score within this node.
Definition: babNode.h:85
double _pruningScore
Definition: babNode.h:152
void set_lower_bound(const unsigned iVar, const double value)
Function for setting the whole upper bound vector.
Definition: babNode.h:135
std::vector< double > get_upper_bounds() const
Function for querying the upper bounds on the optimization variables within this node.
Definition: babNode.h:95
BabNode(double pruningScoreIn, const std::vector< double > &lbdsIn, const std::vector< double > &ubdsIn, const int idIn, const unsigned depthIn, const bool holdsIncumbent)
Constructor for initializing a BabNode using separate vectors containing the bounds.
Definition: babNode.h:48
double get_pruning_score() const
Function for querying the pruning score within this node.
Definition: babNode.h:80
void set_holds_incumbent(const bool holdsIncumbent)
Function for setting the _holdsIncumbent variable.
Definition: babNode.h:115