22 template<
typename GrammarType>
24 auto g = grammar->generate(from.
nt());
25 return {g, grammar->log_probability(g) - grammar->log_probability(from)};
35 template<
typename GrammarType>
42 double alp = -log(a.
count());
43 double wholetree = alp + grammar->log_probability(b);
63 double lp = wholetree;
106 template<
typename GrammarType>
121 auto [s, slp] = sample<Node,Node>(ret, +[](
const Node& n) {
return 1.0*n.
can_resample;});
127 double oldgp = grammar->log_probability(*s);
129 s->
assign(grammar->generate(s->nt()));
135 double fb = (-log(from.
count()) + grammar->log_probability(*s)) -
136 (-log(ret.count()) + oldgp);
139 CERR "FORWARD" TAB slp + grammar->log_probability(*s)
ENDL;
146 return std::make_pair(ret, fb);
161 template<
typename GrammarType,
int D>
164 auto my_can_resample = +[](
const Node& n) {
165 return (n.can_resample and n.depth() <=
D )*1.0;
174 if(from.
sum<
double>(my_can_resample) == 0.0)
177 auto [s, slp] = sample<Node,Node>(ret, my_can_resample);
179 double oldgp = grammar->log_probability(*s);
181 s->
assign(grammar->generate(s.first->nt()));
183 double fb = slp + grammar->log_probability(*s.first)
184 - (log(my_can_resample(*s.first)) - log(ret.sum(my_can_resample)) + oldgp);
186 return std::make_pair(ret, fb);
191 template<
typename GrammarType>
211 DEBUG(
"INSERT-TREE", from, *s);
217 Node* captured_s = s;
218 std::function can_resample_matches_s_nt = [captured_s](
const Node& n) ->
double {
224 Node t = grammar->generate(s->nt());
233 auto [q, qlp] = sample<Node,Node>(*s, can_resample_matches_s_nt);
244 auto lpq =
lp_sample_eq(*q, *s, can_resample_matches_s_nt);
253 double forward = slp +
254 (grammar->log_probability(t)-grammar->log_probability(old_s)) +
258 double backward = lp_sample_one<Node,Node>(t, ret,
can_resample) +
259 lp_sample_eq<Node,Node>(old_s, *s, can_resample_matches_s_nt);
268 assert(not std::isinf(forward));
269 assert(not std::isinf(backward));
271 return std::make_pair(ret, forward-backward);
274 template<
typename GrammarType>
290 DEBUG(
"DELETE-TREE", from, *s);
293 Node* captured_s = s;
294 std::function can_resample_matches_s_nt = [&](
const Node& n) ->
double {
299 auto [q, qlp] =
sample(*s, can_resample_matches_s_nt);
303 double forward = slp + lp_sample_eq<Node,Node>(*q, *s, can_resample_matches_s_nt);
306 double tlp = grammar->log_probability(old_s) - grammar->log_probability(*q);
312 double backward = lp_sample_one<Node,Node>(*s,ret,
can_resample) +
314 lp_sample_eq<Node,Node>(newq,old_s,can_resample_matches_s_nt);
316 assert(not std::isinf(forward));
317 assert(not std::isinf(backward));
319 return std::make_pair(ret, forward-backward);
330 template<
typename GrammarType>
335 std::function allowed = +[](
const Node& n) {
341 auto z = sample_z<Node,Node>(ret, allowed);
342 if(z == 0.0)
return {};
344 auto [s, slp] = sample<Node,Node>(ret, z, allowed);
347 print(
"SAMPLE-LEAVING-ARGS", from, *s);
351 std::vector<Rule*> matching_rules;
352 for(
auto& r: grammar->rules[s->rule->nt]) {
353 if(r.child_types == s->rule->child_types) {
354 matching_rules.push_back(&r);
358 assert(matching_rules.size() >= 1);
359 if(matching_rules.size() == 1) {
364 std::function sampler = +[](
const Rule* r) ->
double {
return r->p; };
365 auto [newr, __rp] = sample<Rule*>(matching_rules, sampler);
366 assert(newr !=
nullptr and *newr !=
nullptr);
367 assert( (*newr)->nt == s->rule->nt);
369 const Rule* oldRule = s->rule;
373 s->lp = (*newr)->
p / grammar->Z[s->rule->nt];
376 double fb = log(sampler(*newr))-log(sampler(oldRule));
378 return std::make_pair(ret,fb);
388 template<
typename GrammarType>
394 if(z == 0.0)
return {};
396 auto [s, slp] = sample<Node,Node>(ret, z,
can_resample);
397 if(s->nchildren() <= 1) {
401 auto N = s->nchildren();
405 std::vector<int> possible_indices;
406 for(
size_t i=0;i<
N;i++) {
407 for(
size_t j=i+1;j<N;j++) {
408 if(s->child(j).rule->nt == s->child(i).rule->nt) {
409 possible_indices.push_back(i);
414 if(possible_indices.size() == 0)
return {};
422 auto x = possible_indices.at(
sample_int(possible_indices.size()).first);
423 auto y =
sample_int(N, [&](
const int v) {
return 1*(s->child(v).rule->nt == s->child(x).rule->nt and v != x); }).first;
427 s->set_child(y, std::move(tmp));
430 return std::make_pair(ret,0.0);
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's below). This uses a little tr...
Definition: Proposers.h:331
double p_regeneration_propose_to(GrammarType *grammar, const Node &a, const Node &b)
Probability of proposing from a to b under regeneration.
Definition: Proposers.h:36
#define TAB
Definition: IO.h:19
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
std::pair< int, double > sample_int(unsigned int max, const std::function< double(const int)> &f=[](const int v){return 1.0;})
Definition: Random.h:330
T sum(std::function< T(const this_t &)> &f) const
Definition: BaseNode.h:425
std::optional< std::pair< Node, double > > prior_proposal(GrammarType *grammar, const Node &from)
Definition: Proposers.h:23
void print(FIRST f, ARGS... args)
Lock output_lock and print to std:cout.
Definition: IO.h:53
std::optional< std::pair< Node, double > > delete_tree(GrammarType *grammar, const Node &from)
Definition: Proposers.h:275
T logplusexp(const T a, const T b)
Definition: Numerics.h:131
double lp_sample_eq(const t &x, const T &s, std::function< double(const t &)> &f=[](const t &v){return 1.0;})
Definition: Random.h:431
Definition: Proposers.h:11
A Node is the primary internal representation for a program – it recursively stores a rule and the a...
std::optional< std::pair< Node, double > > regenerate_shallow(GrammarType *grammar, const Node &from)
Regenerate with rational-rules style proposals, but only allow proposals to trees with a max depth of...
Definition: Proposers.h:162
void assign(Node &n)
Assign will set everything to n BUT it will not copy the parent pointer etc since we're assuming this...
Definition: Node.h:59
#define CERR
Definition: IO.h:23
nonterminal_t nt() const
Definition: Node.h:156
this_t & child(const size_t i)
Definition: BaseNode.h:175
const Rule * rule
Definition: Node.h:32
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
void set_child(const size_t i, Node &n)
Definition: Node.h:88
double p
Definition: Rule.h:30
#define ENDL
Definition: IO.h:21
std::optional< std::pair< Node, double > > insert_tree(GrammarType *grammar, const Node &from)
Definition: Proposers.h:192
void DEBUG(FIRST f, ARGS... args)
Print to std:ccout with debugging info.
Definition: IO.h:73
bool can_resample
Definition: Node.h:34
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
double can_resample(const Node &n)
Helper function for whether we can resample from a node (just accesses n.can_resample) ...
Definition: Proposers.h:18
size_t nchildren() const
Definition: BaseNode.h:208
double D
Definition: Main.cpp:15