def sample_iterative(self, stack, result_stack, prev, grammar):
     stack.pop()
     wrapped_sampler = self._sampler
     restartable_sampler = pybo.IterativeSampler(wrapped_sampler,
                                                 grammar,
                                                 is_restartable=True)
     obj = restartable_sampler.sample()
     result_stack.append(obj)
 def sample_iterative(self, stack, result_stack, prev, grammar):
     # We use recursion here for now.
     stack.pop()
     set_elems_sampler = self._sampler
     k = pybo.pois(self._d, self._sampler._precomputed_eval)
     sampler = pybo.IterativeSampler(set_elems_sampler, grammar)
     set_elems = []
     for _ in range(k):
         obj = sampler.sample()
         set_elems.append(obj)
     result_stack.append(self.builder.set(set_elems))
 def sample_iterative(self, stack, result_stack, prev, grammar):
     if prev is None or self in prev.get_children():
         # Sample from lhs with substituted x.
         stack.append(self.lhs)
     else:
         stack.pop()
         # Get the object in which the l-atoms have to be replaced from the
         # result stack.
         core_object = result_stack.pop()
         # Replace the atoms and push result.
         sampler = pybo.IterativeSampler(self.rhs, grammar)
         res = core_object.replace_l_atoms(sampler)  # Recursion for now.
         result_stack.append(res)
 def sample_iterative(self, stack, result_stack, prev, grammar):
     if prev is None or self in prev.children:
         stack.append(self.lhs)
     else:
         stack.pop()
         # Get the object in which the u-atoms have to be replaced from the
         # result stack.
         core_object = result_stack.pop()
         # Recover the old y from the stack.
         # y = result_stack.pop()
         # Replace the atoms and push result.
         sampler = pybo.IterativeSampler(self.rhs, grammar)
         res = core_object.replace_u_atoms(sampler)  # Recursion for now.
         result_stack.append(res)
Example #5
0
    def sample_iterative(self, alias):
        """Samples from the rule identified by `alias` in an iterative manner.

        Parameters
        ----------
        alias : str
            The rule to be sampled from

        Traverses the decomposition tree in post-order.
        The tree may be arbitrarily large and is expanded on the fly.
        """
        try:
            sampler = self[alias]
        except KeyError:
            DecompositionGrammar._missing_rule_error(alias)
        return pybo.IterativeSampler(sampler, self).sample()