31 template<
typename this_t,
37 typename _data_t=std::vector<_datum_t>,
38 typename _VirtualMachineState_t=
typename _Grammar_t::VirtualMachineState_t
115 total_instruction_count_last_call = c.total_instruction_count_last_call;
116 total_vms_steps = c.total_vms_steps;
119 if(c.program.loader == &c)
137 return std::make_pair(this_t(std::move(x.first)), x.second);
148 [[nodiscard]]
virtual this_t
restart()
const override {
201 this->program.
clear();
202 value.template linearize<VirtualMachineState_t, Grammar_t>(this->
program);
203 this->program.
loader =
this;
214 for(
auto it = this->program.
begin(); it != this->program.
end(); it++) {
219 virtual std::string
string(std::string prefix=
"")
const override {
220 return this->
string(prefix,
true);
222 virtual std::string
string(std::string prefix,
bool usedot)
const {
223 return prefix + std::string(
"\u03BBx.") + value.
string(usedot);
230 virtual size_t hash()
const override {
240 return this->value == h.value;
248 auto nt =
grammar->template nt<output_t>();
264 auto nt =
grammar->template nt<output_t>();
278 auto nt =
grammar->template nt<output_t>();
293 auto nt =
grammar->template nt<output_t>();
318 for(
auto& n : value) {
319 cnt += n.rule->is_recursive();
330 return str(this->
prior) + SerializationDelimiter +
347 h.
prior = string_to<double>(pr);
348 h.likelihood = string_to<double>(li);
349 h.posterior = string_to<double>(po);
LOTHypothesis(const LOTHypothesis &&c)
Definition: LOTHypothesis.h:96
virtual std::string string(std::string prefix="") const override
Definition: LOTHypothesis.h:219
Node & get_value()
Definition: LOTHypothesis.h:161
static this_t deserialize(const std::string &s)
Convert this from a string which was in a file.
Definition: LOTHypothesis.h:341
virtual this_t restart() const override
This is used to restart chains, sampling from prior but ONLY for nodes that are can_resample.
Definition: LOTHypothesis.h:148
virtual int neighbors() const override
Count the number of neighbors that are possible. (This should be size_t but int is more convenient...
Definition: LOTHypothesis.h:262
double neighbor_prior(const Node &node, int &which) const
Definition: Grammar.h:958
LOTHypothesis(const LOTHypothesis &c)
Definition: LOTHypothesis.h:92
Node generate(const nonterminal_t ntfrom=nt< output_t >(), unsigned long depth=0) const
A wrapper to catch DepthExcpetions and retry. This means that defaultly we try to generate GENERATE_D...
Definition: Grammar.h:693
void push(const Instruction &val)
Push val onto the stack.
Definition: Stack.h:49
virtual std::string string(std::string prefix, bool usedot) const
Definition: LOTHypothesis.h:222
virtual void complete() override
Modify this hypothesis's value by (randomly) filling in all the gaps.
Definition: LOTHypothesis.h:246
double likelihood
Definition: Bayesable.h:43
void clear()
Definition: Stack.h:41
virtual size_t hash() const override
Definition: LOTHypothesis.h:230
size_t recursion_count()
Count up how many times I use recursion – we keep a list of recursion here.
Definition: LOTHypothesis.h:316
LOTHypothesis()
Definition: LOTHypothesis.h:76
void set_value(Node &v, bool should_compile=true)
Set the value to v. (NOTE: This compiles into a program)
Definition: LOTHypothesis.h:168
virtual std::string string(bool usedot=true) const override
Definition: Node.h:205
Node copy_resample(const Node &node, bool f(const Node &n)) const
Definition: Grammar.h:702
Definition: MCMCable.h:14
LOTHypothesis & operator=(const LOTHypothesis &&c)
Definition: LOTHypothesis.h:112
void compile()
Definition: LOTHypothesis.h:200
std::pair< t *, double > sample(const T &s, double z, const std::function< double(const t &)> &f=[](const t &v){return 1.0;})
Definition: Random.h:258
virtual std::string parseable() const
Definition: Node.h:269
virtual std::string serialize() const override
Convert this into a string which can be written to a file.
Definition: LOTHypothesis.h:328
Node value
Definition: LOTHypothesis.h:72
virtual void push_program(Program< VirtualMachineState_t > &s) override
This puts the code from my node onto s. Used internally in e.g. recursion.
Definition: LOTHypothesis.h:211
virtual bool is_evaluable() const override
A node is "evaluable" if it is complete (meaning no null subnodes)
Definition: LOTHypothesis.h:306
set input_t
Definition: LOTHypothesis.h:50
typename MyGrammar ::VirtualMachineState_t VirtualMachineState_t
Definition: LOTHypothesis.h:52
A class is searchable if permits us to enumerate and make its neighbors. This class is used by MCTS a...
Definition: Serializable.h:4
Program< VirtualMachineState_t > program
Definition: LOTHypothesis.h:68
double prior
Definition: Bayesable.h:42
Node makeNode(const Rule *r) const
Definition: Grammar.h:633
auto begin()
These are for iterating through the underlying vector.
Definition: Stack.h:138
virtual bool operator==(const this_t &h) const override
Equality is checked on equality of values; note that greater-than is still on posteriors.
Definition: LOTHypothesis.h:239
Node from_parseable(std::deque< std::string > &q) const
Definition: Grammar.h:871
const Rule * NullRule
Definition: Rule.h:186
std::string str(BindingTree *t)
Definition: BindingTree.h:195
double posterior
Definition: Bayesable.h:44
virtual bool is_complete() const
Definition: Node.h:188
ProgramLoader< VirtualMachineState_t > * loader
Definition: Program.h:48
bool is_null() const
Definition: Node.h:165
unsigned long total_vms_steps
Definition: LOTHypothesis.h:65
constexpr double infinity
Definition: Numerics.h:20
A Node is the primary internal representation for a program – it recursively stores a rule and the a...
Bayesable< _datum_t, _data_t >::datum_t datum_t
Definition: LOTHypothesis.h:47
static this_t sample()
Definition: LOTHypothesis.h:140
word output_t
Definition: LOTHypothesis.h:51
void expand_to_neighbor(Node &node, int &which)
Definition: Grammar.h:937
size_t neighbors(const Node &node) const
Definition: Grammar.h:923
Grammar_t * get_grammar() const
Definition: LOTHypothesis.h:179
LOTHypothesis(Node &x)
Definition: LOTHypothesis.h:79
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't, then we'll recurse.
Definition: Proposers.h:107
_datum_t datum_t
Definition: Bayesable.h:38
static const char SerializationDelimiter
Definition: LOTHypothesis.h:58
double rule_normalizer(const nonterminal_t nt) const
Definition: Grammar.h:601
std::optional< std::pair< MyHypothesis, double > > ProposalType
Definition: LOTHypothesis.h:55
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
const Node & get_value() const
Definition: LOTHypothesis.h:162
LOTHypothesis & operator=(const LOTHypothesis &c)
Definition: LOTHypothesis.h:100
void set_value(Node &&v, bool should_compile=true)
Definition: LOTHypothesis.h:173
The Bayesable class provides an interface for hypotheses that support Bayesian inference (e...
Definition: LOTHypothesis.h:40
Definition: Searchable.h:13
unsigned long total_instruction_count_last_call
Definition: LOTHypothesis.h:64
virtual void expand_to_neighbor(int k) override
Modify this hypothesis to become the k'th neighbor. NOTE This does not compile since it might not be ...
Definition: LOTHypothesis.h:274
Definition: MyGrammar.h:72
virtual double compute_prior() override
Compute the prior – defaultly just the PCFG (grammar) prior.
Definition: LOTHypothesis.h:185
This stores a distribution from values of T to log probabilities. It is used as the return value from...
static this_t from_string(Grammar_t *g, std::string s)
Definition: LOTHypothesis.h:226
size_t count_rules(const nonterminal_t nt) const
Definition: Grammar.h:189
_data_t data_t
Definition: Bayesable.h:39
LOTHypothesis(std::string s)
Definition: LOTHypothesis.h:88
auto end()
Definition: Stack.h:139
static const size_t MAX_NODES
Definition: LOTHypothesis.h:60
Bayesable< _datum_t, _data_t >::data_t data_t
Definition: LOTHypothesis.h:48
virtual ProposalType propose() const override
Default proposal is rational-rules style regeneration.
Definition: LOTHypothesis.h:128
void complete(Node &node)
Definition: Grammar.h:985
bool was_called
Definition: Program.h:23
A program here stores just a stack of instructions which can be executed by the VirtualMachineState_t...
virtual size_t count() const
How many nodes total are below me?
Definition: BaseNode.h:358
virtual double neighbor_prior(int k) override
What is the prior of the k'th neighbor? This does not need to return the full prior, only relative (among ks)
Definition: LOTHypothesis.h:290
LOTHypothesis(Node &&x)
Definition: LOTHypothesis.h:83
virtual Rule * get_rule(const nonterminal_t nt, size_t k) const
Definition: Grammar.h:489
virtual double compute_single_likelihood(const datum_t &datum) override
Definition: LOTHypothesis.h:195
A class is MCMCable if it is Bayesable and lets us propose, restart, and check equality (which MCMC d...
virtual size_t hash(size_t depth=0) const
Definition: Node.h:391