1 #ifndef OPENGM_VISITOR_HXX
2 #define OPENGM_VISITOR_HXX
22 template<
class INFERENCE>
31 void end(INFERENCE & inf){
34 void addLog(
const std::string & logName){}
35 void log(
const std::string & logName,
const double logValue){}
38 template<
class INFERENCE>
43 void begin(INFERENCE & inf,
const typename INFERENCE::ValueType value,
const typename INFERENCE::ValueType bound){}
44 size_t operator()(INFERENCE & inf,
const typename INFERENCE::ValueType value,
const typename INFERENCE::ValueType bound){
47 void end(INFERENCE & inf,
const typename INFERENCE::ValueType value,
const typename INFERENCE::ValueType bound){
51 template<
class INFERENCE>
56 visithNth_(visithNth),
57 multiline_(multiline){
60 std::cout<<
"begin: value "<<inf.value()<<
" bound "<<inf.bound()<<
"\n";
64 if((iteration_)%visithNth_==0){
65 std::cout<<
"step: "<<iteration_<<
" value "<<inf.value()<<
" bound "<<inf.bound()<<
"\n";
70 void end(INFERENCE & inf){
71 std::cout<<
"value "<<inf.value()<<
" bound "<<inf.bound()<<
"\n";
74 void addLog(
const std::string & logName){}
75 void log(
const std::string & logName,
const double logValue){
76 if((iteration_)%visithNth_==0){
77 std::cout<<logName<<
" "<<logValue<<
"\n";
88 template<
class INFERENCE>
93 visithNth_(visithNth),
94 multiline_(multiline){
96 void begin(INFERENCE & inf,
const typename INFERENCE::ValueType value,
const typename INFERENCE::ValueType bound){
97 std::cout<<
"begin: value "<< value <<
" bound "<< bound <<
"\n";
100 size_t operator()(INFERENCE & inf,
const typename INFERENCE::ValueType value,
const typename INFERENCE::ValueType bound){
101 if((iteration_)%visithNth_==0){
102 std::cout<<
"step: "<<iteration_<<
" value "<< value <<
" bound "<< bound <<
"\n";
107 void end(INFERENCE & inf,
const typename INFERENCE::ValueType value,
const typename INFERENCE::ValueType bound){
108 std::cout<<
"value "<< value <<
" bound "<< bound <<
"\n";
117 template<
class INFERENCE>
123 const size_t visithNth=1,
124 const size_t reserve=0,
125 const bool verbose=
true,
126 const bool multiline=
true,
127 const double timeLimit=std::numeric_limits<double>::infinity(),
128 const double gapLimit=0.0,
129 const size_t memLogging=0
139 visithNth_(visithNth),
141 multiline_(multiline),
142 memLogging_(memLogging),
143 timeLimit_(timeLimit),
148 ctime_ = & protocolMap_[
"ctime"] ;
149 times_ = & protocolMap_[
"times"] ;
150 values_ = & protocolMap_[
"values"] ;
151 bounds_ = & protocolMap_[
"bounds"] ;
152 iterations_ = & protocolMap_[
"iteration"];
156 times_->reserve(reserve);
157 values_->reserve(reserve);
158 bounds_->reserve(reserve);
159 iterations_->reserve(reserve);
172 const ValueType val=inf.value();
173 const ValueType bound=inf.bound();
175 times_->push_back(0);
176 values_->push_back(val);
177 bounds_->push_back(bound);
178 iterations_->push_back(
double(iteration_));
186 std::cout<<
"begin: value "<<val<<
" bound "<<bound<<
" mem "<< protocolMap_[
"mem"].back() <<
" MB\n";
188 std::cout<<
"begin: value "<<val<<
" bound "<<bound<<
"\n";
199 if(iteration_%visithNth_==0){
204 const ValueType val =inf.value();
205 const ValueType bound =inf.bound();
208 times_->push_back(totalTime_);
209 values_->push_back(val);
210 bounds_->push_back(bound);
211 iterations_->push_back(
double(iteration_));
213 for(
size_t el=0;el<extraLogs_.size();++el){
214 protocolMap_[extraLogs_[el]].push_back( std::numeric_limits<double>::quiet_NaN() );
218 protocolMap_[
"mem"].push_back(std::numeric_limits<double>::quiet_NaN());
225 std::cout<<
"step: "<<iteration_<<
" value "<<val<<
" bound "<<bound<<
" [ "<<totalTime_ <<
"]" <<
" mem "<< protocolMap_[
"mem"].back() <<
" MB\n";
227 std::cout<<
"step: "<<iteration_<<
" value "<<val<<
" bound "<<bound<<
" [ "<<totalTime_ <<
"]" <<
"\n";
231 if(std::fabs(bound - val) <= gapLimit_){
233 std::cout<<
"gap limit reached\n";
241 if(totalTime_ > timeLimit_) {
243 std::cout<<
"timeout reached\n";
258 void end(INFERENCE & inf){
262 const ValueType val=inf.value();
263 const ValueType bound=inf.bound();
266 times_->push_back(totalTime_);
267 values_->push_back(val);
268 bounds_->push_back(bound);
269 iterations_->push_back(
double(iteration_));
275 std::cout<<
"end: value "<<val<<
" bound "<<bound<<
" [ "<<totalTime_ <<
"]" <<
" mem "<< protocolMap_[
"mem"].back() <<
" MB\n";
277 std::cout<<
"end: value "<<val<<
" bound "<<bound<<
" [ "<<totalTime_ <<
"]" <<
"\n";
282 void addLog(
const std::string & logName){
283 protocolMap_[logName]=std::vector<double>();
284 extraLogs_.push_back(logName);
286 void log(
const std::string & logName,
const double logValue){
287 if((iteration_)%visithNth_==0){
290 std::cout<<logName<<
" "<<logValue<<
"\n";
292 protocolMap_[logName].back()=logValue;
300 const std::map< std::string, std::vector<double > > &
protocolMap()
const{
323 std::map< std::string, std::vector<double > > protocolMap_;
324 std::vector<std::string> extraLogs_;
325 std::vector<double > * ctime_;
326 std::vector<double > * times_;
327 std::vector<double > * values_;
328 std::vector<double > * bounds_;
329 std::vector<double > * iterations_;
343 template<
class INFERENCE>
349 const size_t visithNth=1,
350 const size_t reserve=0,
351 const bool verbose=
true,
352 const bool multiline=
true,
353 const double timeLimit=std::numeric_limits<double>::infinity(),
354 const double gapLimit=0.0,
355 const size_t memLogging=1
365 visithNth_(visithNth),
367 multiline_(multiline),
368 memLogging_(memLogging),
369 timeLimit_(timeLimit),
374 ctime_ = & protocolMap_[
"ctime"] ;
375 times_ = & protocolMap_[
"times"] ;
376 values_ = & protocolMap_[
"values"] ;
377 bounds_ = & protocolMap_[
"bounds"] ;
378 iterations_ = & protocolMap_[
"iteration"];
382 times_->reserve(reserve);
383 values_->reserve(reserve);
384 bounds_->reserve(reserve);
385 iterations_->reserve(reserve);
393 void begin(INFERENCE & inf,
const typename INFERENCE::ValueType value,
const typename INFERENCE::ValueType bound){
399 times_->push_back(0);
400 values_->push_back(value);
401 bounds_->push_back(bound);
402 iterations_->push_back(
double(iteration_));
409 std::cout<<
"begin: value "<<value<<
" bound "<<bound<<
" mem "<< protocolMap_[
"mem"].back() <<
" MB\n";
411 std::cout<<
"begin: value "<<value<<
" bound "<<bound<<
"\n";
420 size_t operator()(INFERENCE & inf,
const typename INFERENCE::ValueType value,
const typename INFERENCE::ValueType bound){
422 if(iteration_%visithNth_==0){
429 times_->push_back(totalTime_);
430 values_->push_back(value);
431 bounds_->push_back(bound);
432 iterations_->push_back(
double(iteration_));
435 protocolMap_[
"mem"].push_back(std::numeric_limits<double>::quiet_NaN());
442 std::cout<<
"step: "<<iteration_<<
" value "<<value<<
" bound "<<bound<<
" [ "<<totalTime_ <<
"]" <<
" mem"<< protocolMap_[
"mem"].back() <<
" MB\n";
444 std::cout<<
"step: "<<iteration_<<
" value "<<value<<
" bound "<<bound<<
" [ "<<totalTime_ <<
"]" <<
"\n";
448 if(std::fabs(bound - value) <= gapLimit_){
450 std::cout<<
"gap limit reached\n";
457 if(totalTime_ > timeLimit_) {
459 std::cout<<
"timeout reached\n";
474 void end(INFERENCE & inf,
const typename INFERENCE::ValueType value,
const typename INFERENCE::ValueType bound){
480 times_->push_back(totalTime_);
481 values_->push_back(value);
482 bounds_->push_back(bound);
483 iterations_->push_back(
double(iteration_));
489 std::cout<<
"end: value "<<value<<
" bound "<<bound<<
" [ "<<totalTime_ <<
"]" <<
" mem "<< protocolMap_[
"mem"].back() <<
" MB\n";
491 std::cout<<
"end: value "<<value<<
" bound "<<bound<<
" [ "<<totalTime_ <<
"]" <<
"\n";
498 const std::map< std::string, std::vector<double > > &
protocolMap()
const{
521 std::map< std::string, std::vector<double > > protocolMap_;
523 std::vector<double > * ctime_;
524 std::vector<double > * times_;
525 std::vector<double > * values_;
526 std::vector<double > * bounds_;
527 std::vector<double > * iterations_;
540 template<
class VISITOR,
class INFERENCE_TYPE>
549 :_pvisitor(pvisitor),
550 _pinference(pinference){};
551 void begin(ValueType value,ValueType bound){_pvisitor->begin(*_pinference,value,bound);}
552 void end(ValueType value,ValueType bound){_pvisitor->end(*_pinference,value,bound);}
553 size_t operator() (ValueType value,ValueType bound){
return (*_pvisitor)(*_pinference,value,bound);}
557 INFERENCE_TYPE* _pinference;
560 template<
class VISITOR,
class INFERENCE_TYPE>
569 :_pvisitor(pvisitor),
570 _pinference(pinference){};
571 void begin(){_pvisitor->begin(*_pinference);}
572 void end(){_pvisitor->end(*_pinference);}
574 void addLog(
const std::string& logName){_pvisitor->addLog(logName);}
575 void log(
const std::string& logName,
double value){_pvisitor->log(logName,value);}
578 INFERENCE_TYPE* _pinference;
585 #endif //OPENGM_VISITOR_HXX
void begin(INFERENCE &inf, const typename INFERENCE::ValueType value, const typename INFERENCE::ValueType bound)
size_t operator()(INFERENCE &inf)
InferenceType::ValueType ValueType
const std::vector< double > & getValues() const
void begin(ValueType value, ValueType bound)
const std::map< std::string, std::vector< double > > & protocolMap() const
const std::map< std::string, std::vector< double > > & protocolMap() const
TimingVisitor(const size_t visithNth=1, const size_t reserve=0, const bool verbose=true, const bool multiline=true, const double timeLimit=std::numeric_limits< double >::infinity(), const double gapLimit=0.0, const size_t memLogging=0)
INFERENCE_TYPE InferenceType
void end(INFERENCE &inf, const typename INFERENCE::ValueType value, const typename INFERENCE::ValueType bound)
void log(const std::string &logName, double value)
Platform-independent runtime measurements.
void addLog(const std::string &logName)
void addLog(const std::string &logName)
const std::vector< double > & getIterations() const
size_t operator()(INFERENCE &inf, const typename INFERENCE::ValueType value, const typename INFERENCE::ValueType bound)
void log(const std::string &logName, const double logValue)
INFERENCE_TYPE InferenceType
static const size_t StopInfBoundReached
INFERENCE::ValueType ValueType
static double usedPhysicalMemMax()
ExplicitVerboseVisitor(const size_t visithNth=1, const bool multiline=false)
size_t operator()(INFERENCE &inf, const typename INFERENCE::ValueType value, const typename INFERENCE::ValueType bound)
const std::vector< double > & getTimes() const
ExplicitVisitorWrapper(VISITOR *pvisitor, INFERENCE_TYPE *pinference)
const std::vector< double > & getConstructionTime() const
void begin(INFERENCE &inf, const typename INFERENCE::ValueType value, const typename INFERENCE::ValueType bound)
size_t operator()(INFERENCE &inf)
void begin(INFERENCE &inf, const typename INFERENCE::ValueType value, const typename INFERENCE::ValueType bound)
void begin(INFERENCE &inf)
INFERENCE::ValueType ValueType
InferenceType::ValueType ValueType
const std::vector< double > & getValues() const
VisitorWrapper(VISITOR *pvisitor, INFERENCE_TYPE *pinference)
size_t operator()(INFERENCE &inf, const typename INFERENCE::ValueType value, const typename INFERENCE::ValueType bound)
double elapsedTime() const
VerboseVisitor(const size_t visithNth=1, const bool multiline=false)
const std::vector< double > & getBounds() const
void log(const std::string &logName, const double logValue)
static const size_t StopInfTimeout
void begin(INFERENCE &inf)
void log(const std::string &logName, const double logValue)
void addLog(const std::string &logName)
const std::vector< double > & getConstructionTime() const
const std::vector< double > & getTimes() const
size_t operator()(INFERENCE &inf)
const std::vector< double > & getIterations() const
static const size_t ContinueInf
ExplicitTimingVisitor(const size_t visithNth=1, const size_t reserve=0, const bool verbose=true, const bool multiline=true, const double timeLimit=std::numeric_limits< double >::infinity(), const double gapLimit=0.0, const size_t memLogging=1)
const std::vector< double > & getBounds() const
void begin(INFERENCE &inf)
void end(INFERENCE &inf, const typename INFERENCE::ValueType value, const typename INFERENCE::ValueType bound)
void end(ValueType value, ValueType bound)
void end(INFERENCE &inf, const typename INFERENCE::ValueType value, const typename INFERENCE::ValueType bound)
void addLog(const std::string &logName)