Fleet  0.0.9
Inference in the LOT
MyMCTS.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "PartialMCTSNode.h"
4 #include "ParallelTempering.h"
5 #include "MCMCChain.h"
6 
7 class MyMCTS : public PartialMCTSNode<MyMCTS, MyHypothesis> {
9  using Super::Super;
10 
11 public:
12  MyMCTS(MyMCTS&&) { assert(false); } // must be defined but not used
13 
14  virtual generator<MyHypothesis&> playout(MyHypothesis& current) override {
15  // define our own playout here to call our callback and add sample to the MCTS
16 
17  MyHypothesis h0 = current; // need a copy to change resampling on
18  for(auto& n : h0.get_value() ){
19  n.can_resample = false;
20  }
21 
22  h0.complete(); // fill in any structural gaps
23 
24  // check to see if we have no gaps and no resamples, then we just run one sample.
25  std::function no_resamples = +[](const Node& n) -> bool { return not n.can_resample;};
26  if(h0.count_constants() == 0 and h0.get_value().all(no_resamples)) {
27  this->process_evaluable(current);
28  co_yield h0;
29  }
30  else {
31 
32  double best_posterior = -infinity;
33  long steps_since_best = 0;
34 
35  // else we run vanilla MCMC
36  MCMCChain chain(h0, data);
37 // ParallelTempering chain(h0, &mydata, 10, 100.0 );
38  for(auto& h : chain.run(InnerControl()) | burn(BURN_N) | thin(FleetArgs::inner_thin) ) {
39 
40  // return if we haven't improved in howevermany samples.
41  if(h.posterior > best_posterior) {
42  best_posterior = h.posterior;
43  steps_since_best = 0;
44  }
45 
46  this->add_sample(h.posterior);
47  co_yield h;
48 
49  //
50  if(steps_since_best > 100000) break;
51  }
52 
53  }
54  }
55 
56 };
unsigned long burn
Definition: FleetArgs.h:14
virtual void complete() override
Fill in all the holes in this hypothesis, at random, modifying self. NOTE for LOTHypotheses this will...
Definition: Lexicon.h:246
Definition: Node.h:22
unsigned long inner_thin
Definition: FleetArgs.h:28
Definition: MCMCChain.h:23
we don&#39;t need inputs/outputs for out MyHypothesis
Definition: MyHypothesis.h:6
virtual void process_evaluable(MyHypothesis &current)
If we can evaluate this current node (usually: compute a posterior and add_sample) ...
Definition: MCTSBase.h:193
constexpr double infinity
Definition: Numerics.h:20
Definition: generator.hpp:21
auto & get_value()
Definition: Lexicon.h:57
MyMCTS(MyMCTS &&)
Definition: MyMCTS.h:12
Definition: PartialMCTSNode.h:13
size_t BURN_N
Definition: Main.cpp:21
This is a chain pool that runs multiple chains on a ladder of different temperatures and adjusts temp...
Control InnerControl(unsigned long st=FleetArgs::inner_steps, unsigned long t=FleetArgs::inner_runtime, size_t thr=1, unsigned long re=FleetArgs::inner_restart)
Make a Control object (NOTE it&#39;s a Control object not an InnerControl one) that has default parameter...
Definition: Control.h:102
void add_sample(const float v)
Definition: MCTSBase.h:163
Definition: MyMCTS.h:7
generator< HYP & > run(Control ctl)
Run MCMC according to the control parameters passed in. NOTE: ctl cannot be passed by reference...
Definition: MCMCChain.h:153
virtual generator< MyHypothesis & > playout(MyHypothesis &current) override
This gets called on a child that is unvisited. Typically it would consist of filling in h some number...
Definition: MyMCTS.h:14
This represents an MCMC hain on a hypothesis of type HYP. It uses HYP::propose and HYP::compute_poste...
static data_t * data
Definition: MCTSBase.h:57
unsigned long thin
Definition: FleetArgs.h:33