13 extern std::atomic<bool>
CTRL_C;
25 template<
typename HYP>
30 const unsigned long time_resolution_ms = 25;
50 ChainPool<HYP>(h0, d, t.size()), temperatures(t), terminate(false) {
52 swap_history.reserve(temperatures.size());
54 for(
size_t i=0;i<temperatures.size();i++) {
55 this->
pool[i].temperature = temperatures[i];
56 swap_history.emplace_back();
62 ChainPool<HYP>(h0, d, n),terminate(false) {
64 swap_history.reserve(n);
67 this->
pool[0].temperature = 1.0;
70 for(
size_t i=0;i<n;i++) {
71 this->
pool[i].temperature = exp(i * log(maxT)/(n-1));
72 swap_history.emplace_back();
80 while(not (terminate or
CTRL_C) ) {
85 while(time_since(last) < show_every and (not CTRL_C) and (not terminate))
86 std::this_thread::sleep_for(std::chrono::milliseconds(time_resolution_ms));
88 if(CTRL_C or terminate)
return;
99 while(!(terminate or
CTRL_C)) {
103 while(time_since(last) < swap_every and (not CTRL_C) and (not terminate))
104 std::this_thread::sleep_for(std::chrono::milliseconds(time_resolution_ms));
107 std::lock_guard og(overall_mutex);
108 for(
size_t k=this->
pool.size()-1;
k>=1;
k--) {
109 if(CTRL_C or terminate)
break;
112 std::lock_guard guard1(this->
pool[
k-1].current_mutex);
113 std::lock_guard guard2(this->
pool[
k ].current_mutex);
116 double Tnow = this->
pool[
k-1].at_temperature(this->
pool[
k-1].temperature) + this->
pool[
k].at_temperature(this->
pool[
k].temperature);
117 double Tswp = this->
pool[
k-1].at_temperature(this->
pool[
k].temperature) + this->
pool[
k].at_temperature(this->
pool[
k-1].temperature);
118 double R = Tswp-Tnow;
122 if(R >= 0 or
flip(exp(R))) {
124 #ifdef PARALLEL_TEMPERING_SHOW_DETAIL 125 COUT "# Swapping " <<
k<<
" and " <<(
k-1)<<
"." TAB Tnow
TAB Tswp
TAB this->
pool[
k].current.likelihood
TAB this->pool[
k-1].current.likelihood
ENDL;
137 swap_history.at(
k) <<
true;
140 swap_history.at(
k) <<
false;
149 while(not (terminate or
CTRL_C) ) {
154 while(time_since(last) < adapt_every and (not CTRL_C) and (not terminate))
155 std::this_thread::sleep_for(std::chrono::milliseconds(time_resolution_ms));
157 if(CTRL_C or terminate)
return;
161 #ifdef PARALLEL_TEMPERING_SHOW_DETAIL 178 #ifdef PARALLEL_TEMPERING_SHOW_DETAIL 193 #ifdef PARALLEL_TEMPERING_SHOW_DETAIL 208 COUT "# Pool info: \n";
209 for(
size_t i=0;i<this->
pool.size();i++) {
213 auto cpy = this->
pool[i].current;
217 double(this->
pool[i].temperature),
219 this->pool[i].acceptance_ratio(),
220 swap_history[i].mean(),
221 int(swap_history[i].
N),
222 this->
pool[i].samples,
228 double k(
unsigned long t,
double v,
double t0) {
229 return (1.0/v) * t0/(t+t0);
232 void adapt(
double v=3,
double t0=1000000) {
233 std::lock_guard og(overall_mutex);
236 std::vector<double> sw(this->
pool.size());
238 for(
size_t i=1;i<this->
pool.size()-1;i++) {
239 sw[i] = log(this->
pool[i].temperature - this->
pool[i-1].temperature);
241 if( swap_history[i].
N>0 && swap_history[i+1].
N>0 ) {
242 sw[i] +=
k(this->
pool[i].samples, v, t0) * (swap_history[i].mean()-swap_history[i+1].mean());
256 for(
size_t i=1;i<this->
pool.size();i++) {
257 this->
pool[i].temperature = this->
pool[i-1].temperature + exp(sw[i]);
272 template<
typename HYP>
279 DataTempering(HYP& h0, std::vector<typename HYP::data_t>& datas) : terminate(false) {
281 swap_history.reserve(datas.size());
284 for(
size_t i=0;i<datas.size();i++) {
286 this->
pool[i].temperature = 1.0;
287 swap_history.emplace_back();
309 void adapt(
double v=3,
double t0=1000000) { assert(
false &&
"*** You should not call adapt for DataTempering");}
Definition: OrderedLock.h:16
generator< HYP & > run(Control ctl)
Definition: ParallelTempering.h:170
A ChainPool stores a bunch of MCMCChains and allows you to run them serially or in parallel...
ParallelTempering(HYP &h0, typename HYP::data_t *d, unsigned long n, double maxT)
Definition: ParallelTempering.h:61
std::atomic< bool > CTRL_C
void __swapper_thread()
Definition: ParallelTempering.h:95
void __adapter_thread()
Definition: ParallelTempering.h:147
#define TAB
Definition: IO.h:19
std::vector< MCMCChain< HYP > > pool
Definition: ChainPool.h:29
time_ms adapt_every
Definition: ParallelTempering.h:35
std::atomic< bool > terminate
Definition: ParallelTempering.h:277
time_ms swap_every
Definition: ParallelTempering.h:34
void __shower_thread()
Definition: ParallelTempering.h:78
Definition: ParallelTempering.h:273
bool flip(float p=0.5)
Definition: Random.h:25
Definition: ChainPool.h:25
DataTempering(HYP &h0, std::vector< typename HYP::data_t > &datas)
Definition: ParallelTempering.h:279
double k(unsigned long t, double v, double t0)
Definition: ParallelTempering.h:228
OrderedLock overall_mutex
Definition: ParallelTempering.h:38
void print(FIRST f, ARGS... args)
Lock output_lock and print to std:cout.
Definition: IO.h:53
Definition: ParallelTempering.h:26
std::atomic< bool > terminate
Definition: ParallelTempering.h:47
Definition: generator.hpp:21
void adapt(double v=3, double t0=1000000)
Definition: ParallelTempering.h:309
void show_statistics()
Definition: ParallelTempering.h:200
std::vector< double > temperatures
Definition: ParallelTempering.h:40
std::vector< FiniteHistory< bool > > swap_history
Definition: ParallelTempering.h:43
void swap(generator< T > &a, generator< T > &b)
Definition: generator.hpp:238
std::vector< FiniteHistory< bool > > swap_history
Definition: ParallelTempering.h:276
#define ENDL
Definition: IO.h:21
generator< HYP & > run(Control ctl, time_ms swap_every, time_ms adapt_every)
Definition: ParallelTempering.h:293
time_ms show_every
Definition: ParallelTempering.h:36
void adapt(double v=3, double t0=1000000)
Definition: ParallelTempering.h:232
#define COUT
Definition: IO.h:24
ParallelTempering(HYP &h0, typename HYP::data_t *d, std::initializer_list< double > t)
Definition: ParallelTempering.h:49
bool is_temperature
Definition: ParallelTempering.h:45