NOMAD Source  Version 4.0.0 Beta
AlgoStopReasons< StopType > Class Template Reference

Template class for algorithm stop reasons. More...

#include <AlgoStopReasons.hpp>

Inheritance diagram for AlgoStopReasons< StopType >:
Collaboration diagram for AlgoStopReasons< StopType >:

Public Member Functions

 AlgoStopReasons ()
 Constructor. More...
 
virtual ~AlgoStopReasons ()
 Destructor. More...
 
void set (StopType s)
 Set the algo stop reason to a specific stop type. More...
 
std::string getStopReasonAsString () const override
 Get the stop reason that requires termination as a string. More...
 
bool checkTerminate () const override
 Check among generic stop reasons and algo stop reason if the algorithm must terminate. More...
 
bool testIf (const StopType &s) const
 Test for a specific algorithm stop type. More...
 
void setStarted () override
 Reset stop reasons to their default STARTED state. More...
 
- Public Member Functions inherited from AllStopReasons
 AllStopReasons ()
 Constructor. More...
 
virtual ~AllStopReasons ()
 Destructor. More...
 
const StopReason< EvalGlobalStopType > & getEvalGlobalStopReason ()
 
const StopReason< IterStopType > & getIterStopReason () const
 
void set (const IterStopType &s)
 
bool testIf (IterStopType s)
 Test IterStopType. More...
 

Static Public Member Functions

static std::shared_ptr< AlgoStopReasons< StopType > > get (std::shared_ptr< AllStopReasons > allStopReasons)
 Access to the AlgoStopReasons. More...
 
- Static Public Member Functions inherited from AllStopReasons
static const StopReason< BaseStopType > & getBaseStopReason ()
 
static void set (const BaseStopType &s)
 
static void set (const EvalGlobalStopType &s)
 
static bool testIf (const BaseStopType &s)
 Test static BaseStopType. More...
 
static bool testIf (const EvalGlobalStopType &s)
 Test static EvalGlobalStopType. More...
 
static std::string getEvalGlobalStopReasonAsString ()
 Get the global eval stop reason as a string. More...
 
static std::string getBaseStopReasonAsString ()
 Get the base stop reason as a string. More...
 
static bool checkBaseTerminate ()
 
static bool checkEvalGlobalTerminate ()
 

Private Attributes

StopReason< StopType > _algoStopReason
 Stop reason specific to this algorithm. More...
 

Detailed Description

template<typename StopType>
class AlgoStopReasons< StopType >

Template class for algorithm stop reasons.

The class is templated with a StopType defined according to which algorithm is considered. For example, we have MadsStopType, LHStopType and NMStopType.
At some point during an algorithm a stop reason is set. It can be specific to the algorithm or generic (that is be an AllStopReasons stop type).
The stop reasons in AllStopReasons are private and not directly accessible. But the AlgoStopReasons::checkTerminate() function, checks both AllStopReasons and AlgoStopReasons.

Definition at line 65 of file AlgoStopReasons.hpp.

Constructor & Destructor Documentation

◆ AlgoStopReasons()

template<typename StopType >
AlgoStopReasons< StopType >::AlgoStopReasons ( )
inlineexplicit

Constructor.

Definition at line 71 of file AlgoStopReasons.hpp.

71  : AllStopReasons()
72  {
73  }

◆ ~AlgoStopReasons()

template<typename StopType >
virtual AlgoStopReasons< StopType >::~AlgoStopReasons ( )
inlinevirtual

Destructor.

Definition at line 76 of file AlgoStopReasons.hpp.

77  {}

Member Function Documentation

◆ checkTerminate()

template<typename StopType >
bool AlgoStopReasons< StopType >::checkTerminate ( ) const
inlineoverridevirtual

Check among generic stop reasons and algo stop reason if the algorithm must terminate.

Reimplemented from AllStopReasons.

Definition at line 116 of file AlgoStopReasons.hpp.

117  {
118  auto evc = NOMAD::EvcInterface::getEvaluatorControl();
119  return (NOMAD::AllStopReasons::checkTerminate()
121  || ((nullptr != evc) && evc->getStopReason(NOMAD::getThreadNum()).checkTerminate()));
122  }

◆ get()

template<typename StopType >
static std::shared_ptr<AlgoStopReasons<StopType> > AlgoStopReasons< StopType >::get ( std::shared_ptr< AllStopReasons allStopReasons)
inlinestatic

Access to the AlgoStopReasons.

Definition at line 126 of file AlgoStopReasons.hpp.

127  {
128  std::shared_ptr<AlgoStopReasons<StopType>> stopReasons = std::dynamic_pointer_cast<AlgoStopReasons<StopType>>( allStopReasons );
129 
130  if ( stopReasons == nullptr )
131  throw Exception(__FILE__, __LINE__, "Invalid shared pointer cast");
132  return stopReasons;
133  }

◆ getStopReasonAsString()

template<typename StopType >
std::string AlgoStopReasons< StopType >::getStopReasonAsString ( ) const
inlineoverridevirtual

Get the stop reason that requires termination as a string.

If no termination is required, an empty string is returned.

Reimplemented from AllStopReasons.

Definition at line 91 of file AlgoStopReasons.hpp.

92  {
93  std::string stopReasonStr = AllStopReasons::getStopReasonAsString();
94 
96  {
97  stopReasonStr += _algoStopReason.getStopReasonAsString() + " (Algo)";
98  }
99 
100  auto evc = NOMAD::EvcInterface::getEvaluatorControl();
101  if (nullptr != evc)
102  {
103  int mainThreadNum = NOMAD::getThreadNum(); // The code in Algos/ is always called from a main thread.
104  auto evalStopReason = evc->getStopReason(mainThreadNum);
105  if (!evalStopReason.isStarted())
106  {
107  stopReasonStr += (stopReasonStr.empty() ? "" : " ") + evalStopReason.getStopReasonAsString();
108  }
109  }
110 
111  return stopReasonStr;
112  }

◆ set()

template<typename StopType >
void AlgoStopReasons< StopType >::set ( StopType  s)
inline

Set the algo stop reason to a specific stop type.

Definition at line 85 of file AlgoStopReasons.hpp.

86  {
88  }

◆ setStarted()

template<typename StopType >
void AlgoStopReasons< StopType >::setStarted ( )
inlineoverridevirtual

Reset stop reasons to their default STARTED state.

Reimplemented from AllStopReasons.

Definition at line 147 of file AlgoStopReasons.hpp.

148  {
151  auto evc = NOMAD::EvcInterface::getEvaluatorControl();
152  if (nullptr != evc)
153  {
154  evc->setStopReason(NOMAD::getThreadNum(), NOMAD::EvalMainThreadStopType::STARTED);
155  }
156  }

◆ testIf()

template<typename StopType >
bool AlgoStopReasons< StopType >::testIf ( const StopType &  s) const
inline

Test for a specific algorithm stop type.

Used to pass a sub-algorithm stop reason to a parent algorithm stop reason.

Definition at line 140 of file AlgoStopReasons.hpp.

141  {
142  return ( _algoStopReason.get() == s );
143  }

Member Data Documentation

◆ _algoStopReason

template<typename StopType >
StopReason<StopType> AlgoStopReasons< StopType >::_algoStopReason
private

Stop reason specific to this algorithm.

Definition at line 81 of file AlgoStopReasons.hpp.


The documentation for this class was generated from the following file:
AllStopReasons::AllStopReasons
AllStopReasons()
Constructor.
Definition: AllStopReasons.hpp:68
StopReason::get
T get() const
The stop reason.
Definition: StopReason.hpp:258
AllStopReasons::getStopReasonAsString
virtual std::string getStopReasonAsString() const
Get the stop reason that requires termination as a string.
getThreadNum
int getThreadNum()
Definition: utils.cpp:533
Exception
Exception utility.
Definition: WriteAttributeDefinitionFile.cpp:86
StopReason::getStopReasonAsString
std::string getStopReasonAsString() const
Translate the stop reason into a string for display.
Definition: StopReason.hpp:287
StopReason::checkTerminate
bool checkTerminate() const
Check if the stop reason requires a termination.
AlgoStopReasons::_algoStopReason
StopReason< StopType > _algoStopReason
Stop reason specific to this algorithm.
Definition: AlgoStopReasons.hpp:81
StopReason::isStarted
bool isStarted() const
Check if it is in STARTED state.
Definition: StopReason.hpp:281
StopReason::setStarted
void setStarted()
Reset the stop reason to the default STARTED state.
Definition: StopReason.hpp:275
AllStopReasons::setStarted
virtual void setStarted()
Reset all stop reasons to their default STARTED state.
StopReason::set
void set(T s)
Set the stop reason if it is listed in dictionnary.
Definition: StopReason.hpp:264