Fleet  0.0.9
Inference in the LOT
InnerHypothesis.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "MyGrammar.h"
5 #include "Timing.h"
6 
7 #include "CachedCallHypothesis.h"
8 
9 class InnerHypothesis : public DeterministicLOTHypothesis<InnerHypothesis,BindingTree*,bool,MyGrammar,&grammar>,
10  public CachedCallHypothesis<InnerHypothesis,
11  defaultdatum_t<BindingTree*,std::string>, // need to use MyHypothesis' datum type
12  bool> {
13 public:
15  using Super::Super; // inherit the constructors
18  bool>;
19 
20  const double MAX_NODES = 40;
21 
24 
25  InnerHypothesis(const InnerHypothesis& c) : Super(c), CCH(c) {}
26  InnerHypothesis(const InnerHypothesis&& c) : Super(c), CCH(c) { }
27 
28  InnerHypothesis& operator=(const InnerHypothesis& c) {
30  CachedCallHypothesis::operator=(c);
31  return *this;
32  }
33  InnerHypothesis& operator=(const InnerHypothesis&& c) {
35  CachedCallHypothesis::operator=(c);
36  return *this;
37  }
38 
39  void set_value(Node& v) {
42  }
43  void set_value(Node&& v) {
46  }
47 
48  virtual bool cached_call_wrapper(const defaultdatum_t<BindingTree*,std::string>& di) override {
49  // override how cached_call accesses the data
50  return this->call(di.input);
51  }
52 
53  virtual double compute_prior() override {
54  /* This ends up being a really important check -- otherwise we spend tons of time on really long
55  * hypotheses */
56  if(this->value.count() > MAX_NODES) return this->prior = -infinity;
57  else return this->prior = this->get_grammar()->log_probability(value);
58  }
59 
60  [[nodiscard]] virtual std::optional<std::pair<InnerHypothesis,double>> propose() const override {
61 
62  std::optional<std::pair<Node,double>> p;
63 
64  if(flip(0.5)) p = Proposals::regenerate(&grammar, value);
66  else if(flip(0.1)) p = Proposals::swap_args(&grammar, value);
67  else if(flip()) p = Proposals::insert_tree(&grammar, value);
69 
70  if(not p) return {};
71  auto x = p.value();
72 
73  return std::make_pair(InnerHypothesis(std::move(x.first)), x.second);
74  }
75 
76 };
MyGrammar grammar
std::optional< std::pair< Node, double > > sample_function_leaving_args(GrammarType *grammar, const Node &from)
This samples functions f(a,b) -> g(a,b) (e.g. without destroying what&#39;s below). This uses a little tr...
Definition: Proposers.h:331
void set_value(Node &v)
Definition: InnerHypothesis.h:39
Definition: Node.h:22
LOTHypothesis< InnerHypothesis, BindingTree *, bool, MyGrammar, grammar, defaultdatum_t< BindingTree *, bool >, std::vector< defaultdatum_t< BindingTree *, bool > >, typename MyGrammar ::VirtualMachineState_t > Super
Definition: DeterministicLOTHypothesis.h:17
virtual double compute_prior() override
Compute the prior – defaultly just the PCFG (grammar) prior.
Definition: InnerHypothesis.h:53
Super::output_t output_t
Definition: DeterministicLOTHypothesis.h:21
virtual std::optional< std::pair< InnerHypothesis, double > > propose() const override
Default proposal is rational-rules style regeneration.
Definition: InnerHypothesis.h:60
Definition: InnerHypothesis.h:9
double prior
Definition: Bayesable.h:42
virtual output_t call(const input_t x, const output_t &err=output_t{})
A variant of call that assumes no stochasticity and therefore outputs only a single value...
Definition: DeterministicLOTHypothesis.h:32
InnerHypothesis & operator=(const InnerHypothesis &c)
Definition: InnerHypothesis.h:28
bool flip(float p=0.5)
Definition: Random.h:25
input_t input
Definition: Datum.h:22
Definition: CachedCallHypothesis.h:14
std::optional< std::pair< Node, double > > delete_tree(GrammarType *grammar, const Node &from)
Definition: Proposers.h:275
void set_value(Node &&v)
Definition: InnerHypothesis.h:43
Definition: Datum.h:15
void clear_cache()
Definition: CachedCallHypothesis.h:23
constexpr double infinity
Definition: Numerics.h:20
Definition: DeterministicLOTHypothesis.h:14
InnerHypothesis & operator=(const InnerHypothesis &&c)
Definition: InnerHypothesis.h:33
std::optional< std::pair< Node, double > > regenerate(GrammarType *grammar, const Node &from)
A little helper function that resamples everything below when we can. If we can&#39;t, then we&#39;ll recurse.
Definition: Proposers.h:107
double log_probability(const Node &n) const
This computes the expected length of productions from this grammar, counting terminals and nontermina...
Definition: Grammar.h:849
InnerHypothesis(const InnerHypothesis &&c)
Definition: InnerHypothesis.h:26
This is a hypothesis that allows you to cache an entire call on data. NOTE: This only catches std::ex...
InnerHypothesis(const InnerHypothesis &c)
Definition: InnerHypothesis.h:25
std::optional< std::pair< Node, double > > insert_tree(GrammarType *grammar, const Node &from)
Definition: Proposers.h:192
const double MAX_NODES
Definition: InnerHypothesis.h:20
Super::output_t output_t
Definition: InnerHypothesis.h:22
Bayesable< defaultdatum_t< BindingTree *, bool >, std::vector< defaultdatum_t< BindingTree *, bool > > >::data_t data_t
Definition: LOTHypothesis.h:48
virtual bool cached_call_wrapper(const defaultdatum_t< BindingTree *, std::string > &di) override
This is how we access the data before calling – needed to say how this interfaces with the data...
Definition: InnerHypothesis.h:48
std::optional< std::pair< Node, double > > swap_args(GrammarType *grammar, const Node &from)
This propose swaps around arguments of the same type.
Definition: Proposers.h:389
virtual size_t count() const
How many nodes total are below me?
Definition: BaseNode.h:358
Super::data_t data_t
Definition: InnerHypothesis.h:23