Fleet  0.0.9
Inference in the LOT
PartitionMCMC.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <thread>
4 
5 //#define DEBUG_PARTITION_MCMC 1
6 
7 #include <vector>
8 
9 #include "Errors.h"
10 #include "MCMCChain.h"
11 #include "Timing.h"
13 
23 template<typename HYP>
24 std::set<HYP> get_partitions(const HYP& h0, const size_t max_depth, const size_t max_size) {
25  assert(not (max_depth==0 and max_size==0));
26 
27  std::set<HYP> cur = {h0};
28  std::set<HYP> nxt;
29 
30  for(size_t dd=0;(max_depth==0 or dd<max_depth) and (max_size==0 or cur.size() < max_size);dd++) {
31 
32  if(CTRL_C) break;
33 
34  for(auto& h: cur) {
35  auto neigh = h.neighbors();
36  for(int n=0;n<neigh;n++) {
37  auto newh = h;
38  newh.expand_to_neighbor(n);
39  //print(h.string(), newh.string(), newh.is_evaluable(), n, dd, max_depth);
40 
41  // now we need to check here and make sure that there aren't any complete trees
42  // because if there are, we won't include them in any trees below
43  if(not newh.is_evaluable()) {
44  nxt.insert(newh);
45  }
46  }
47  }
48 
49  // if we made too many, return what we had, else keep going
50  if(max_size > 0 and nxt.size() > max_size){
51  return cur;
52  }
53  else {
54  cur = nxt;
55  }
56  }
57 
58  return cur;
59 }
60 
61 
70 template<typename HYP>
71 class PartitionMCMC : public ChainPool<HYP> {
72 public:
73 
82  PartitionMCMC(HYP& h0, size_t max_depth, typename HYP::data_t* data, size_t max_size=0) {
83 
84  assert(not h0.is_evaluable() && "*** You should not call PartitionMCMC on a complete node (usually you want it to be empty)");
85 
86  auto cur = get_partitions(h0, max_depth, max_size);
87 
88  // now make an MCMC chain on each of these
89  // (all must use the callback)
90  for(auto& h: cur) {
91  HYP x = h;
92  for(auto& n : x.get_value() ){
93  n.can_resample = false;
94  }
95 
96  x.complete(); // fill in any structural gaps
97 
98  this->pool.push_back(MCMCChain<HYP>(x, data));
100 
101  #ifdef DEBUG_PARTITION_MCMC
102  print("Starting PartitionMCMC on ", h.string(), "\t", x.string());
103  #endif
104  }
105 
106  print("# Initialized ", this->pool.size(), " partitions");
107 
108  }
109 
110 
111 };
Definition: PartitionMCMC.h:71
std::vector< MCMCChain< HYP > > pool
Definition: ChainPool.h:29
Definition: ChainPool.h:25
volatile sig_atomic_t CTRL_C
This manages multiple threads for running inference. This requires a subclass to define run_thread...
void print(FIRST f, ARGS... args)
Lock output_lock and print to std:cout.
Definition: IO.h:53
std::vector< RunningState > running
Definition: ChainPool.h:46
std::set< HYP > get_partitions(const HYP &h0, const size_t max_depth, const size_t max_size)
Compute "partitions" meaning trees with holes where we can run MCMC on the hole. This takes either a ...
Definition: PartitionMCMC.h:24
This represents an MCMC hain on a hypothesis of type HYP. It uses HYP::propose and HYP::compute_poste...
PartitionMCMC(HYP &h0, size_t max_depth, typename HYP::data_t *data, size_t max_size=0)
This specifies both a max_Size and a max_depth which are the most partitions we&#39;ll make...
Definition: PartitionMCMC.h:82