MAiNGO
babException.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 babException.h
11  *
12  * @brief File containing definition of exceptions used by the Branch-and-Bound elements.
13  *
14  **********************************************************************************/
15 
16 #pragma once
17 
18 #include "babNode.h"
19 
20 #include <exception>
21 #include <sstream>
22 #include <typeinfo>
23 
24 
25 namespace babBase {
26 
27 
38 class BranchAndBoundBaseException: public std::exception {
39 
40  protected:
41  std::string _msg{""};
43 
44  public:
50  explicit BranchAndBoundBaseException(const std::string& arg):
51  BranchAndBoundBaseException(arg, nullptr, nullptr)
52  {
53  }
54 
61  BranchAndBoundBaseException(const std::string& arg, const babBase::BabNode& node):
62  BranchAndBoundBaseException(arg, nullptr, &node)
63  {
64  }
65 
72  BranchAndBoundBaseException(const std::string& arg, const std::exception& e):
73  BranchAndBoundBaseException(arg, &e, nullptr)
74  {
75  }
76 
84  BranchAndBoundBaseException(const std::string& arg, const std::exception& e, const babBase::BabNode& node):
85  BranchAndBoundBaseException(arg, &e, &node)
86  {
87  }
88 
96  BranchAndBoundBaseException(const std::string& arg, const std::exception* e, const babBase::BabNode* node)
97  {
98  std::ostringstream message;
99  message << arg;
100  if (e) {
101  if (typeid(*e).name() != typeid(*this).name()) {
102  message << "Original std::exception: " << typeid(*e).name() << ": " << std::endl
103  << " ";
104  }
105  message << e->what();
106  }
107  if (node) {
108  std::vector<double> lowerVarBounds(node->get_lower_bounds()), upperVarBounds(node->get_upper_bounds());
109  message << std::endl
110  << "Exception was thrown while processing node no. " << node->get_ID() << ":" << std::endl;
111  for (unsigned int i = 0; i < lowerVarBounds.size(); i++) {
112  message << " x(" << i << "): " << std::setprecision(16) << lowerVarBounds[i] << ":" << upperVarBounds[i] << std::endl;
113  }
114  }
115  _msg = message.str();
116  }
117 
118 
124  const char* what() const noexcept
125  {
126  return _msg.c_str();
127  }
128 };
129 
130 
131 } // namespace babBase
int get_ID() const
Function for querying the node ID.
Definition: babNode.h:100
Class representing a node in the Branch-and-Bound tree.
Definition: babNode.h:35
namespace holding all essentials of the babBase submodule
Definition: babBrancher.h:40
BranchAndBoundBaseException(const std::string &arg, const babBase::BabNode &node)
Constructor used for forwarding.
Definition: babException.h:61
BranchAndBoundBaseException(const std::string &arg, const std::exception *e, const babBase::BabNode *node)
Constructor used printing a BranchAndBoundBase Exception.
Definition: babException.h:96
BranchAndBoundBaseException(const std::string &arg, const std::exception &e)
Constructor used for forwarding.
Definition: babException.h:72
std::vector< double > get_lower_bounds() const
Function for querying the lower bounds on the optimization variables within this node.
Definition: babNode.h:90
BranchAndBoundBaseException(const std::string &arg)
Constructor used for forwarding.
Definition: babException.h:50
BranchAndBoundBaseException(const std::string &arg, const std::exception &e, const babBase::BabNode &node)
Constructor used for forwarding.
Definition: babException.h:84
This class defines the exceptions thrown by BranchAndBoundBase.
Definition: babException.h:38
std::string _msg
Definition: babException.h:41
const char * what() const noexcept
Function to return the error message.
Definition: babException.h:124
std::vector< double > get_upper_bounds() const
Function for querying the upper bounds on the optimization variables within this node.
Definition: babNode.h:95