Exemplo n.º 1
0
    def make(cls, make_h0, data, grammar, **args):
        """ This is the initializer we use to create this from a grammar, creating children for each way grammar.start can expand """
        h0 = make_h0(value=None)

        ## For each nonterminal, find out how much (in the prior) we pay for a hole that size
        hole_penalty = dict()
        dct = defaultdict(list) # make a lit of the log_probabilities below
        for _ in xrange(1000): # generate this many trees
            t = grammar.generate()
            for fn in t:
                dct[fn.returntype].append(grammar.log_probability(fn))
        hole_penalty = { nt : sum(dct[nt]) / len(dct[nt]) for nt in dct }

        #We must modify the grammar to include one more nonterminal here
        mynt = "<hsmake>"
        myr = GrammarRule(mynt, '', [grammar.start], p=1.0)
        grammar.rules[mynt].append(myr)
        return cls(make_h0(value=FunctionNode(None, mynt,  '', [grammar.start], rule=myr)), data, grammar, hole_penalty=hole_penalty, parent=None, **args) # the top state
Exemplo n.º 2
0
    def make(cls, make_h0, data, grammar, **args):
        """ This is the initializer we use to create this from a grammar, creating children for each way grammar.start can expand """
        h0 = make_h0(value=None)

        ## For each nonterminal, find out how much (in the prior) we pay for a hole that size
        hole_penalty = dict()
        dct = defaultdict(list)  # make a lit of the log_probabilities below
        for _ in xrange(1000):  # generate this many trees
            t = grammar.generate()
            for fn in t:
                dct[fn.returntype].append(grammar.log_probability(fn))
        hole_penalty = {nt: sum(dct[nt]) / len(dct[nt]) for nt in dct}

        #We must modify the grammar to include one more nonterminal here
        mynt = "<hsmake>"
        myr = GrammarRule(mynt, '', [grammar.start], p=1.0)
        grammar.rules[mynt].append(myr)
        return cls(make_h0(
            value=FunctionNode(None, mynt, '', [grammar.start], rule=myr)),
                   data,
                   grammar,
                   hole_penalty=hole_penalty,
                   parent=None,
                   **args)  # the top state
Exemplo n.º 3
0
                s = type(self)(newh, data=self.data, grammar=self.grammar, hole_penalty=self.hole_penalty, parent=self)
                children.append(s)

        return children




# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if __name__ == "__main__":

    from LOTlib import break_ctrlc

    from LOTlib.Examples.Magnetism.Simple import grammar, make_h0, data
    # USE:  C=50.0, V=1.0

    s = LOTHypothesisState.make(lambda **args: make_h0( maxnodes=100, **args), data, grammar, C=50.0, V=1.0)

    for x in break_ctrlc(s):
        print x.posterior_score, x.prior, x.likelihood, x

    print "<><><><><><><><><><><><><><><><><><><><>"
    s.show_graph(maxdepth=6)
    print "<><><><><><><><><><><><><><><><><><><><>"






Exemplo n.º 4
0
                s = type(self)(newh,
                               data=self.data,
                               grammar=self.grammar,
                               hole_penalty=self.hole_penalty,
                               parent=self)
                children.append(s)

        return children


# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if __name__ == "__main__":

    from LOTlib import break_ctrlc

    from LOTlib.Examples.Magnetism.Simple import grammar, make_h0, data
    # USE:  C=50.0, V=1.0

    s = LOTHypothesisState.make(lambda **args: make_h0(maxnodes=100, **args),
                                data,
                                grammar,
                                C=50.0,
                                V=1.0)

    for x in break_ctrlc(s):
        print x.posterior_score, x.prior, x.likelihood, x

    print "<><><><><><><><><><><><><><><><><><><><>"
    s.show_graph(maxdepth=6)
    print "<><><><><><><><><><><><><><><><><><><><>"