19 template<
typename this_t,
25 typename _VirtualMachineState_t=
typename INNER::Grammar_t::VirtualMachineState_t
28 public Searchable<this_t, _input_t, _output_t>,
53 return factors.size();
60 INNER&
at(
const key_t& k) {
return factors.at(k); }
61 const INNER&
at(
const key_t& k)
const {
return factors.at(k); }
67 return factors.contains(key);
74 return factors.begin()->second.get_grammar();
83 [[nodiscard]]
static this_t
sample(std::initializer_list<key_t> lst) {
92 [[nodiscard]]
static this_t
sample(
const std::vector<key_t>& lst) {
102 virtual std::string
string(std::string prefix=
"")
const override {
108 std::string s = prefix +
"[";
109 for(
auto& [k,f] : factors) {
110 s +=
"F("+
str(k)+
",x):=" + f.string() +
". ";
117 virtual size_t hash()
const override {
124 size_t out = h(factors.size());
126 for(
auto& [k,f] : factors){
127 hash_combine(out, f.hash(), k);
139 return factors == l.factors;
239 factors.at(k).push_program(s);
247 for(
auto& [k, f] : factors){
255 this->
prior = log(0.5)*(factors.size()+1);
257 for(
auto& [k,f] : factors) {
258 this->
prior += f.compute_prior();
269 [[nodiscard]]
virtual std::optional<std::pair<this_t,double>>
propose()
const override {
277 this_t x;
double fb = 0.0;
279 for(
auto& [k,f] : factors) {
280 if(should_propose[idx]) {
281 auto p = f.propose();
283 auto [h, _fb] = p.value();
295 assert(x.factors.size() == factors.size());
297 return std::make_pair(x,fb);
301 [[nodiscard]]
virtual this_t
restart()
const override {
303 for(
auto& [k,f] : factors) {
309 template<
typename...
A>
310 [[nodiscard]]
static this_t
make(
A... a) {
311 auto h = this_t(a...);
328 return tmp.neighbors();
332 return factors.rbegin()->second.neighbors();
345 assert(k < factors.rbegin()->second.neighbors());
346 return factors.rbegin()->second.neighbor_prior(k);
350 for(
auto& [k,f]: factors) {
351 if(not f.is_evaluable())
return false;
374 for(
auto& [k,f] : factors) {
375 out +=
str(k) + Lexicon::FactorDelimiter +
378 out.erase(out.size()-1);
393 h.prior = string_to<double>(q.front()); q.pop_front();
394 h.likelihood = string_to<double>(q.front()); q.pop_front();
395 h.posterior = string_to<double>(q.front()); q.pop_front();
397 while(not q.empty()){
398 key_t k = string_to<key_t>(q.front()); q.pop_front();
399 auto v = INNER::deserialize(q.front()); q.pop_front();
407 template<
typename this_t,
413 typename _VirtualMachineState_t
std::vector< bool > random_nonempty_subset(const size_t n, const double p)
Returns a random nonempty subset of n elements, as a vector<bool> of length n with trues for elements...
Definition: Random.h:220
virtual size_t hash() const override
Definition: Lexicon.h:117
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
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: Lexicon.h:344
double likelihood
Definition: Bayesable.h:42
bool is_evaluable() const override
Check if we can evaluate this node (meaning compute a prior and posterior). NOTE that this is not the...
Definition: Lexicon.h:349
typename InnerHypothesis ::Grammar_t::VirtualMachineState_t VirtualMachineState_t
Definition: Lexicon.h:37
const INNER & at(const key_t &k) const
Definition: Lexicon.h:61
Definition: MCMCable.h:14
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
A class is searchable if permits us to enumerate and make its neighbors. This class is used by MCTS a...
Definition: Serializable.h:4
std::deque< std::string > split(const std::string &s, const char delimiter)
Split is returns a deque of s split up at the character delimiter. It handles these special cases: sp...
Definition: str.h:44
double prior
Definition: Bayesable.h:41
Definition: DiscreteDistribution.h:25
virtual DiscreteDistribution< output_t > call(const key_t k, const input_t x, const output_t &err=output_t{})
Definition: Lexicon.h:361
static double p_factor_propose
Definition: Lexicon.h:42
Grammar_t * get_grammar()
Definition: Lexicon.h:70
int neighbors() const override
Count the number of neighbors that are possible. (This should be size_t but int is more convenient...
Definition: Lexicon.h:324
const INNER & operator[](const key_t &k) const
Definition: Lexicon.h:64
bool contains(const key_t &key)
Definition: Lexicon.h:66
std::string str(BindingTree *t)
Definition: BindingTree.h:195
double posterior
Definition: Bayesable.h:43
const auto & get_value() const
Definition: Lexicon.h:58
virtual std::optional< std::pair< this_t, double > > propose() const override
This proposal guarantees that there will be at least one factor that is proposed to. Each individual factor is proposed to with p_factor_propose.
Definition: Lexicon.h:269
static this_t sample(const std::vector< key_t > &lst)
Definition: Lexicon.h:92
typename InnerHypothesis ::Grammar_t Grammar_t
Definition: Lexicon.h:33
std::string key_t
Definition: Lexicon.h:36
auto & get_value()
Definition: Lexicon.h:57
virtual std::string serialize() const override
Definition: Lexicon.h:365
static const char FactorDelimiter
Definition: Lexicon.h:40
Definition: MyHypothesis.h:80
Args... datum_t
Definition: Bayesable.h:37
std::map< key_t, INNER > factors
Definition: Lexicon.h:44
static this_t make(A... a)
Definition: Lexicon.h:310
Lexicon()
Definition: Lexicon.h:46
The Bayesable class provides an interface for hypotheses that support Bayesian inference (e...
Definition: Searchable.h:13
virtual std::string string(std::string prefix="") const override
Definition: Lexicon.h:102
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: Lexicon.h:336
static this_t sample(std::initializer_list< key_t > lst)
Sample with n factors.
Definition: Lexicon.h:83
size_t nfactors() const
Return the number of factors.
Definition: Lexicon.h:52
static this_t deserialize(const std::string s)
Definition: Lexicon.h:382
virtual this_t restart() const override
Definition: Lexicon.h:301
virtual bool operator==(const this_t &l) const override
Equality checks equality on each part.
Definition: Lexicon.h:138
bool was_called
Definition: Program.h:23
INNER & at(const key_t &k)
Definition: Lexicon.h:60
virtual void push_program(Program< VirtualMachineState_t > &s, const key_t k) override
A lexicon has valid indices if calls to op_RECURSE, op_MEM_RECURSE, op_SAFE_RECURSE, and op_SAFE_MEM_RECURSE all have arguments that are less than the size. (So this places no restrictions on the calling earlier factors)
Definition: Lexicon.h:236
A class is MCMCable if it is Bayesable and lets us propose, restart, and check equality (which MCMC d...
virtual double compute_prior() override
Definition: Lexicon.h:252
INNER & operator[](const key_t &k)
Definition: Lexicon.h:63