Fleet  0.0.9
Inference in the LOT
StochasticLOTHypothesis.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "LOTHypothesis.h"
4 
5 template<typename this_t,
6  typename _input_t,
7  typename _output_t,
8  typename _Grammar_t,
9  _Grammar_t* grammar,
10  typename _datum_t=defaultdatum_t<_input_t, _output_t>,
11  typename _data_t=std::vector<_datum_t>,
12  typename _VirtualMachineState_t=typename _Grammar_t::VirtualMachineState_t
13  >
14 class StochasticLOTHypothesis : public LOTHypothesis<this_t, _input_t, _output_t, _Grammar_t, grammar, _datum_t, _data_t, _VirtualMachineState_t> {
15 public:
17  using Super::Super;
18 
19  using Grammar_t = _Grammar_t;
20  using input_t = _input_t;
21  using output_t = _output_t;
22  using call_output_t = DiscreteDistribution<output_t>; // what does call output?
23  using VirtualMachineState_t = _VirtualMachineState_t;
24 
34  virtual DiscreteDistribution<output_t> call(const input_t x, const output_t& err=output_t{}) {
35 
36  // The below condition is needed in case we create e.g. a lexicon whose input_t and output_t differ from the VirtualMachineState
37  // in that case, these functions are all overwritten and must be called on their own.
38  if constexpr (std::is_same<typename VirtualMachineState_t::input_t, input_t>::value and
39  std::is_same<typename VirtualMachineState_t::output_t, output_t>::value) {
40  assert(not this->program.empty());
41 
43 
44  VirtualMachineState_t* vms = new VirtualMachineState_t(x, err, &pool);
45 
46  vms->program = this->program; // copy our program into vms
47 
48  // Ok this is a little odd -- in the original FLT we did not have this set was_called=true
49  // since it was not called by another factor; in retrospect, this is odd because it means the first one called
50  // is never was_called unless it was called by another factor (e.g. not counting the original function call)
51  // But probably it makes sense to make anything in here set was_called=true
52  this->was_called = true;
53 
54  pool.push(vms); // put vms into the pool
55 
56  const auto out = pool.run();
57 
58  // update some stats
59  this->total_instruction_count_last_call = pool.total_instruction_count;
60  this->total_vms_steps = pool.total_vms_steps;
61 
62  return out;
63 
64  } else { UNUSED(x); UNUSED(err); assert(false && "*** Cannot use call when VirtualMachineState_t has different input_t or output_t."); }
65  }
66 };
MyGrammar grammar
void UNUSED(const T &x)
Definition: Miscellaneous.h:38
virtual DiscreteDistribution< output_t > call(const input_t x, const output_t &err=output_t{})
Run the virtual machine on input x, and marginalize over execution paths to return a distribution on ...
Definition: StochasticLOTHypothesis.h:34
Program< VirtualMachineState_t > program
Definition: LOTHypothesis.h:68
Definition: DiscreteDistribution.h:25
Definition: Datum.h:15
unsigned long total_vms_steps
Definition: LOTHypothesis.h:65
_VirtualMachineState_t VirtualMachineState_t
Definition: StochasticLOTHypothesis.h:23
Definition: LOTHypothesis.h:40
A LOTHypothesis is the basic unit for doing LOT models. It store a Node as its value, and handles all of the proposing and computing priors, likelihoods, etc. It compiles this Node into a "program" which is used to make function calls, which means that the value should only be changed via LOTHypothesis::set_value.
unsigned long total_instruction_count_last_call
Definition: LOTHypothesis.h:64
Definition: MyGrammar.h:72
Definition: StochasticLOTHypothesis.h:14
bool was_called
Definition: Program.h:23
Definition: VirtualMachinePool.h:42