Fleet  0.0.9
Inference in the LOT
Batch.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "HumanDatum.h"
4 #include "Control.h"
5 #include "TopN.h"
6 #include "MCMCChain.h"
7 #include "Vectors.h"
8 
15 template<typename HYP>
16 std::vector<std::set<HYP>> get_hypotheses_from_mcmc(const HYP& h0,
17  const std::vector<typename HYP::data_t*>& mcmc_data,
18  Control c,
19  const std::vector<size_t> ntop) {
20  assert(not ntop.empty());
21 
22  // find the largest top
23  auto maxntop = *std::max_element(ntop.begin(), ntop.end());
24 
25  std::vector<std::set<HYP>> out(ntop.size()); // one output for each ntop
26 
27  #pragma omp parallel for
28  for(size_t vi=0; vi<mcmc_data.size();vi++) {
29  if(CTRL_C) std::terminate();
30 
31  #pragma omp critical
32  {
33  print("# Running ", vi, " of ", mcmc_data.size(), mcmc_data[vi]);
34  }
35 
36  // start at di=0 so we actually always include prior samples
37  // maybe this isn't a good way to do it
38  for(size_t di=0;di<=mcmc_data[vi]->size() and !CTRL_C;di++) {
39  #pragma omp critical
40  {
41  COUT "# Running " TAB vi TAB " of " TAB mcmc_data.size() TAB mcmc_data[vi] TAB di ENDL;
42  }
43  TopN<HYP> top(maxntop);
44  HYP myh0 = h0.restart();
45  auto givendata = slice(*(mcmc_data[vi]), 0, di); // slices [0,di]
46 
47  MCMCChain chain(myh0, &givendata);
48  for(auto& h : chain.run(Control(c)) | top | printer(FleetArgs::print) ) { UNUSED(h); }
49 
50  #pragma omp critical
51  {
52  COUT "# Done " TAB vi TAB " of " TAB mcmc_data.size() TAB mcmc_data[vi] TAB di ENDL;
53  }
54 
55  #pragma omp critical
56  {
57  // now make data of each size
58  for(size_t t=0;t<ntop.size();t++) {
59 
60  TopN<HYP> mytop(ntop[t]);
61  mytop << top; // choose the top ntop
62 
63  for(auto h : mytop.values()) {
64  h.clear_bayes(); // zero and insert
65  out[t].insert(h);
66  }
67 
68  }
69  }
70  }
71  }
72 
73  return out;
74 }
75 
76 template<typename HYP>
77 std::set<HYP> get_hypotheses_from_mcmc(const HYP& h0, const std::vector<typename HYP::data_t*>& mcmc_data, Control c, const size_t ntop) {
78  auto v = get_hypotheses_from_mcmc(h0, mcmc_data, c, std::vector<size_t>(1,ntop));
79  assert(v.size() == 1);
80  return *v.begin();
81 }
unsigned long print
Definition: FleetArgs.h:34
void UNUSED(const T &x)
Definition: Miscellaneous.h:38
#define TAB
Definition: IO.h:19
std::vector< std::set< HYP > > get_hypotheses_from_mcmc(const HYP &h0, const std::vector< typename HYP::data_t *> &mcmc_data, Control c, const std::vector< size_t > ntop)
Runs MCMC on hypotheses, resampling when the data stops being incremental and returns a unioned vecto...
Definition: Batch.h:16
Definition: SampleStreams.h:66
const std::set< T > & values() const
Definition: TopN.h:112
unsigned long ntop
Definition: FleetArgs.h:15
Definition: MCMCChain.h:23
volatile sig_atomic_t CTRL_C
std::vector< T > slice(const std::vector< T > &v, const size_t start, const int len)
Definition: Vectors.h:6
void print(FIRST f, ARGS... args)
Lock output_lock and print to std:cout.
Definition: IO.h:53
Definition: Control.h:23
#define ENDL
Definition: IO.h:21
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
#define COUT
Definition: IO.h:24
This represents an MCMC hain on a hypothesis of type HYP. It uses HYP::propose and HYP::compute_poste...
A human data series contains a list of human data points, some of which may be presented at the same ...
This class has all the information for running MCMC or MCTS in a little package. It defaultly constru...