20 #ifndef SAMPLE_RECORDER_H
21 #define SAMPLE_RECORDER_H
29 #include <boost/function.hpp>
30 #include <boost/concept_check.hpp>
31 #include <boost/concept_archetype.hpp>
32 #include <boost/lambda/lambda.hpp>
33 #include <boost/bind.hpp>
34 #include <boost/bind/apply.hpp>
35 #include <boost/any.hpp>
36 #include <boost/concept_check.hpp>
39 #include <l/l_sys_io.h>
55 template <
class Model>
66 m_current_lt(-std::numeric_limits<double>::
max())
70 m_current_model(initial_model),
71 m_current_lt(-std::numeric_limits<double>::
max())
76 assert(step_log.size() >= 1);
83 m_current_lt = result.lt;
93 return *m_current_model;
97 boost::optional<Model> m_current_model;
101 template <
class Model>
116 assert(step_log.size() >= 1);
118 m_current_log = step_log;
123 if(m_current_log.size() == 0)
128 return m_current_log;
144 template <
class Model>
155 m_best_lt(-std::numeric_limits<double>::
max())
159 m_best_model(initial_model),
160 m_best_lt(initial_lt)
166 assert(step_log.size() >= 1);
171 if(!m_best_model || result.lt > m_best_lt)
174 m_best_lt = result.lt;
186 return *m_best_model;
195 boost::optional<Model> m_best_model;
206 template <
class Model>
216 m_best_lt(-std::numeric_limits<double>::
max())
222 assert(step_log.size() >= 1);
228 if(result.lt > m_best_lt)
230 m_best_lt = result.lt;
255 template <
class Model>
285 template <
class Model>
315 template <
class Model>
339 if(m_n > 0 && Parent::size() > m_n)
358 template <
class Model>
376 if(m_n > 0 && this->size() > m_n)
400 template <
class Model,
class Value>
409 typedef boost::function1<Value_type, const Model&>
Func;
413 m_total(initial_val),
422 m_total += m_func(m);
428 return m_total / m_n;
500 template <
class Model>
503 struct null_deleter {
531 using namespace boost;
545 template <
class RecorderIn>
554 template <
class RecorderIn>
555 void add(
const RecorderIn& r_in)
567 boost::shared_ptr<RecorderIn>
r(
new RecorderIn(r_in));
575 template <
class RecorderIn>
576 void add(
const typename boost::reference_wrapper<RecorderIn>&
r)
589 template <
class RecorderIn>
590 void add(
const typename boost::shared_ptr<RecorderIn>&
r)
606 template <
class RecorderIn>
610 boost::shared_ptr<RecorderIn> r_sptr(r, null_deleter());
618 template <
class RecorderOut>
622 typedef typename boost::shared_ptr<RecorderOut> RecorderPtr;
623 typedef typename boost::reference_wrapper<RecorderOut> RecorderRef;
627 const RecorderRef result = boost::any_cast<RecorderRef>(
recorders_[
i]);
632 const RecorderPtr result = boost::any_cast<RecorderPtr>(
recorders_[
i]);
638 template <
class RecorderOut>
639 const typename RecorderOut::Value_type&
get(
size_t i)
const
641 typedef typename boost::shared_ptr<RecorderOut> RecorderPtr;
642 typedef typename boost::reference_wrapper<RecorderOut> RecorderRef;
646 const RecorderRef result = boost::any_cast<RecorderRef>(
recorders_[
i]);
647 return static_cast<const RecorderOut&
>(result).
get();
652 const RecorderPtr result = boost::any_cast<RecorderPtr>(
recorders_[
i]);
653 return result->get();
665 template <
class Recorder>
671 typedef typename Recorder::Model_type
Model_type;
726 template <
class Recorder>
732 typedef typename Recorder::Model_type
Model_type;
734 typedef boost::function2<std::ostream&, std::ostream&, const Value_type&>
Write_function;
766 const std::string& separator =
"\n",
771 write_func_(write_func)
784 write_func_(m_os,
r.get());
809 Write_function write_func_;
812 template <
class Recorder>
817 const std::string& separator =
"\n"
823 template <
class Recorder,
class Write_function>
828 const Write_function& write_func,
829 const std::string& separator =
"\n"
833 Function f = write_func;
846 template <
class Recorder_type>
860 fname_fmt_(fname_fmt),
870 std::string fname = str(boost::format(fname_fmt_) % file_counter_);
886 recorder_(model, log);
888 if(period_ == 0)
return;
893 if(it_counter_ == period_)
913 std::string fname = str(boost::format(fname_fmt_) % file_counter_);
919 Recorder_type recorder_;
920 std::string fname_fmt_;
925 size_t file_counter_;
930 template <
class Recorder>
944 template <
class Model>
959 template <
class Model>
966 template <
class Function>
973 return f_(model, step_log);
976 boost::function2<void, const Model_type&, const Step_log<Model>&> f_;
988 template <
class Recorder,
class Updater>
1011 m_recorder(model, step_log);
1014 m_updater.update(m_recorder.get());
1019 return m_recorder.get();
Current_log_recorder()
Definition: sample_recorder.h:110
void add(RecorderIn *r)
Definition: sample_recorder.h:607
double get_log_target() const
Definition: sample_recorder.h:189
Definition: sample_concept.h:123
Model Model_type
Definition: sample_recorder.h:949
virtual void operator()(const Model &m, const Step_log< Model > &step_log)
Definition: sample_recorder.h:529
Definition: sample_recorder.h:666
Int_matrix::Value_type max(const Int_matrix &mat)
Return the maximum value in this matrix.
Definition: l_int_matrix.h:1397
Serialize_recorder(const Recorder_type &r, const std::string &fname_fmt, size_t iterations)
Definition: sample_recorder.h:858
Definition: sample_recorder.h:501
Value Value_type
Definition: sample_recorder.h:407
BOOST_CONCEPT_ASSERT((BaseModel< Model >))
void operator()(const Model_type &model, const Step_log< Model_type > &log)
Definition: sample_recorder.h:883
void operator()(const Model_type &model, const Step_log< Model > &step_log)
Definition: sample_recorder.h:971
Definition: sample_recorder.h:359
Definition: sample_recorder.h:207
void add(const typename boost::shared_ptr< RecorderIn > &r)
Definition: sample_recorder.h:590
void operator()(const Model &, Step_log< Model > step_log)
Definition: sample_recorder.h:219
void operator()(const Model &m, const Step_log< Model > &)
Definition: sample_recorder.h:269
virtual void operator()(const Model_type &m, const Step_log< Model_type > &step_log)
Definition: sample_recorder.h:781
Definition: sample_recorder.h:256
Definition: sample_recorder.h:56
Recorder::Model_type Model_type
Definition: sample_recorder.h:669
const RecorderOut & get_recorder(size_t i) const
Definition: sample_recorder.h:619
void operator()(const Model &m, Step_log< Model > step_log)
Definition: sample_recorder.h:163
size_t size() const
Definition: sample_recorder.h:540
r
Definition: APPgetLargeConnectedEdges.m:127
boost::function2< std::ostream &, std::ostream &, const Value_type & > Write_function
Definition: sample_recorder.h:734
Updater::Update_type Update_type
Definition: sample_recorder.h:997
Recorder::Value_type Value_type
Definition: sample_recorder.h:995
BOOST_CONCEPT_ASSERT((BaseModel< Model >))
Current_model_recorder()
Definition: sample_recorder.h:64
Structure for returning results of a single sampling move.
Definition: sample_base.h:55
Step_log< Model > Value_type
Definition: sample_recorder.h:292
virtual ~Recent_model_recorder()
Definition: sample_recorder.h:332
Definition: sample_base.h:160
BOOST_CONCEPT_ASSERT((BaseModel< Model >))
Base::Value_type Value_type
Definition: sample_recorder.h:964
Serialize_recorder< Recorder > make_serialization_recorder(const Recorder &r, std::string fname_fmt, size_t iterations=1)
Definition: sample_recorder.h:931
Recent_log_recorder(size_t n=0)
Definition: sample_recorder.h:367
void operator()(const Model &, const Step_log< Model > &step_log)
Definition: sample_recorder.h:298
std::deque< Model > Parent
Definition: sample_recorder.h:323
BOOST_CONCEPT_ASSERT((BaseModel< Model >))
Definition: sample_recorder.h:45
BOOST_CONCEPT_ASSERT((BaseModel< Model >))
Recorder::Model_type Model_type
Definition: sample_recorder.h:996
Model Model_type
Definition: sample_recorder.h:150
Base::Model_type Model_type
Definition: sample_recorder.h:965
Expectation_recorder(const Func &f, const Value_type &initial_val=Value_type())
Definition: sample_recorder.h:411
double Value_type
Definition: sample_recorder.h:213
All_log_recorder()
Definition: sample_recorder.h:294
Step_log< Model > Value_type
Definition: sample_recorder.h:365
void operator()(const Model_type &model, const Step_log< Model_type > &step_log)
Definition: sample_recorder.h:1008
Definition: sample_recorder.h:727
Model Model_type
Definition: sample_recorder.h:406
All_model_recorder()
Definition: sample_recorder.h:265
void push_back(const RecorderIn &r)
Definition: sample_recorder.h:546
boost::function1< Value_type, const Model & > Func
Definition: sample_recorder.h:409
Definition: sample_recorder.h:286
void operator()(const Model &m, const Step_log< Model > &step_log)
Definition: sample_recorder.h:74
Model Model_type
Definition: sample_recorder.h:61
Definition: sample_recorder.h:960
Definition: sample_recorder.h:989
Recent_model_recorder(int n=0)
Definition: sample_recorder.h:328
Best_model_recorder(const Model &initial_model, double initial_lt)
Definition: sample_recorder.h:158
Definition: sample_recorder.h:102
virtual void operator()(const Model_type &m, const Step_log< Model_type > &step_log)
Definition: sample_recorder.h:690
Step_log< Model > Value_type
Definition: sample_recorder.h:108
Null_value Value_type
Definition: sample_recorder.h:524
Recorder_type::Value_type Value_type
Definition: sample_recorder.h:851
std::deque< Model > Value_type
Definition: sample_recorder.h:322
Definition: sample_concept.h:144
Model Model_type
Definition: sample_recorder.h:523
Model Value_type
Definition: sample_recorder.h:151
Model Model_type
Definition: sample_recorder.h:364
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
void write(const Data &data, const std::string &fname)
Write to file.
Definition: bbb_data.cpp:123
Definition: sample_base.h:247
void operator()(const Model &, const Step_log< Model > &step_log)
Definition: sample_recorder.h:372
BOOST_CONCEPT_ASSERT((BaseModel< Model >))
BOOST_CONCEPT_ASSERT((BaseModel< Model >))
void add(const RecorderIn &r_in)
Definition: sample_recorder.h:555
Null_value Value_type
Definition: sample_recorder.h:948
Callback_recorder(Function f)
Definition: sample_recorder.h:967
Multi_model_recorder()
Definition: sample_recorder.h:526
void save(const Edge_set &edges, const std::string &fname)
Definition: edge.h:608
void operator()(const Model &, const Step_log< Model > &step_log)
Definition: sample_recorder.h:114
Model Model_type
Definition: sample_recorder.h:212
static std::ostream & default_write(std::ostream &ost, const Value_type &v)
Definition: sample_recorder.h:800
virtual void operator()(const Model &m, const Step_log< Model > &)
Definition: sample_recorder.h:420
std::vector< boost::any > recorders_
Definition: sample_recorder.h:517
BOOST_CONCEPT_ASSERT((BaseModel< Model >))
Model_recorder< Model > Recorder
Definition: sample_recorder.h:510
Ostream_recorder(const Recorder &recorder, std::ostream &os, const std::string &separator="\n", const Write_function &write_func=Ostream_recorder::default_write)
Definition: sample_recorder.h:763
BOOST_CONCEPT_ASSERT((BaseModel< Model >))
Object thrown when an argument to a function is not acceptable.
Definition: l_exception.h:377
Recorder::Value_type Value_type
Definition: sample_recorder.h:733
Model Model_type
Definition: sample_recorder.h:107
Definition: sample_recorder.h:945
Model Model_type
Definition: sample_recorder.h:321
Modulo_recorder(const Recorder &recorder, int interval, int start=0)
Definition: sample_recorder.h:682
virtual void operator()(const Model &m, const Step_log< Model > &)
Definition: sample_recorder.h:335
Ostream_recorder< Recorder > make_ostream_recorder(const Recorder &r, std::ostream &ost, const std::string &separator="\n")
Definition: sample_recorder.h:814
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48
Model Model_type
Definition: sample_recorder.h:261
Object thrown when input or output fails.
Definition: l_exception.h:496
Definition: l_serialization.h:82
for m
Definition: APPgetLargeConnectedEdges.m:64
Recorder::Value_type Value_type
Definition: sample_recorder.h:672
std::vector< Model > Value_type
Definition: sample_recorder.h:262
void add(const typename boost::reference_wrapper< RecorderIn > &r)
Definition: sample_recorder.h:576
Definition: sample_recorder.h:401
Viewing_recorder(const Recorder &r, Updater &u)
Definition: sample_recorder.h:1003
Model Model_type
Definition: sample_recorder.h:291
Definition: sample_concept.h:41
Current_model_recorder(const Model &initial_model)
Definition: sample_recorder.h:69
Definition: sample_recorder.h:145
BOOST_CONCEPT_ASSERT((BaseModel< Model >))
Best_model_recorder()
Definition: sample_recorder.h:153
virtual ~Expectation_recorder()
Definition: sample_recorder.h:417
Model Value_type
Definition: sample_recorder.h:62
Best_target_recorder()
Definition: sample_recorder.h:215
std::vector< bool > is_reference_
Definition: sample_recorder.h:519
Recorder_type::Model_type Model_type
Definition: sample_recorder.h:850
std::vector< typename Model_recorder< Model >::Type > record_callbacks_
Definition: sample_recorder.h:513
Object thrown when computation fails somehow during execution.
Definition: l_exception.h:321
void operator()(const Model_type &, const Step_log< Model > &)
Definition: sample_recorder.h:950
Recorder::Model_type Model_type
Definition: sample_recorder.h:730
Definition: sample_recorder.h:316
Definition: sample_recorder.h:847