Beispiel #1
0
def AnBnCnGrammar():
    register_primitive(flatten2str)

    grammar = Grammar()
    grammar.add_rule('START', 'flatten2str', ['LIST', 'sep=\"\"'], 1.0)
    grammar.add_rule('LIST', 'if_', ['BOOL', 'LIST', 'LIST'], 0.09)
    grammar.add_rule('BOOL', 'empty_', ['LIST'], 0.56)
    grammar.add_rule('BOOL', 'flip_', [''], 0.43)
    grammar.add_rule('LIST', 'cons_', ['ATOM', 'LIST'], 0.203)
    grammar.add_rule('LIST', 'cdr_', ['LIST'], 0.15)
    grammar.add_rule('LIST', 'car_', ['LIST'], 0.15)
    grammar.add_rule('LIST', '\'\'', None, 0.23)
    grammar.add_rule('ATOM', q('a'), None, .33)
    grammar.add_rule('ATOM', q('b'), None, .33)
    grammar.add_rule('ATOM', q('c'), None, .33)

    return grammar
Beispiel #2
0
def AnGrammar():
    register_primitive(flatten2str)

    TERMINAL_WEIGHT = 2.
    grammar = Grammar()
    grammar.add_rule('START', 'flatten2str', ['LIST', 'sep=\"\"'], 1.0)
    grammar.add_rule('BOOL', 'empty_', ['LIST'], 1.)
    grammar.add_rule('BOOL', 'flip_', [''], 1.0)
    grammar.add_rule('LIST', 'if_', ['BOOL', 'LIST', 'LIST'], 1.)
    grammar.add_rule('LIST', 'cons_', ['ATOM', 'LIST'], 1.)
    grammar.add_rule('LIST', 'cdr_', ['LIST'], 1.)
    grammar.add_rule('LIST', 'car_', ['LIST'], 1.)
    grammar.add_rule('LIST', 'recurse_', [], 1.)
    grammar.add_rule('LIST', '[]', None, TERMINAL_WEIGHT)
    grammar.add_rule('ATOM', q('a'), None, TERMINAL_WEIGHT)

    return grammar
Beispiel #3
0
from utils import *
from LOTlib.Evaluation.Eval import register_primitive
from LOTlib.Miscellaneous import flatten2str
from LOTlib.Examples.FormalLanguageTheory.Model.Hypothesis import make_hypothesis
from LOTlib.Examples.FormalLanguageTheory.Language.An import An
import time
from mpi4py import MPI

register_primitive(flatten2str)

if __name__ == '__main__':
    comm = MPI.COMM_WORLD
    rank = comm.Get_rank()

    # ========================================================================================================
    # Process command line arguments
    # ========================================================================================================
    (options, args) = parser.parse_args()

    suffix = time.strftime('_' + options.NAME + '_%m%d_%H%M%S', time.localtime())
    prefix = '../out/simulations/transfer/'

    # ========================================================================================================
    # Running
    # ========================================================================================================
    language = An()

    show_info('running normal input case..')
    sampler = probe_MHsampler(make_hypothesis('An', terminals=['b']), language, options, prefix + 'without_prior_out_' + str(rank) + suffix, ret_sampler=True)

    show_info('running with input using different letter case..')
Beispiel #4
0
from utils import *
from LOTlib.Evaluation.Eval import register_primitive
from LOTlib.Miscellaneous import flatten2str
from LOTlib.Examples.FormalLanguageTheory.Model.Hypothesis import make_hypothesis
from LOTlib.Examples.FormalLanguageTheory.Language.An import An
import time
from mpi4py import MPI

register_primitive(flatten2str)

if __name__ == '__main__':
    comm = MPI.COMM_WORLD
    rank = comm.Get_rank()

    # ========================================================================================================
    # Process command line arguments
    # ========================================================================================================
    (options, args) = parser.parse_args()

    suffix = time.strftime('_' + options.NAME + '_%m%d_%H%M%S',
                           time.localtime())
    prefix = '../out/simulations/transfer/'

    # ========================================================================================================
    # Running
    # ========================================================================================================
    language = An()

    show_info('running normal input case..')
    sampler = probe_MHsampler(make_hypothesis('An', terminals=['b']),
                              language,
Beispiel #5
0
import LOTlib.Miscellaneous
from LOTlib.Grammar import Grammar
from LOTlib.Miscellaneous import q

# # # # # # # # # # # # # # # # # # # # # # # # # # # #
## Let's add a primitive to our evaluation.
## The alternative is to use the decorator @primitive from LOTlib.Evaluation.Eval

from LOTlib.Evaluation.Eval import register_primitive

register_primitive(LOTlib.Miscellaneous.flatten2str)

# # # # # # # # # # # # # # # # # # # # # # # # # # # #

TERMINAL_WEIGHT = 2.

grammar = Grammar()

# flattern2str lives at the top, and it takes a cons, cdr, car structure and projects it to a string
grammar.add_rule('START', 'flatten2str', ['EXPR'], 1.0)

grammar.add_rule('BOOL', 'and_', ['BOOL', 'BOOL'], 1.)
grammar.add_rule('BOOL', 'or_', ['BOOL', 'BOOL'], 1.)
grammar.add_rule('BOOL', 'not_', ['BOOL'], 1.)

grammar.add_rule('EXPR', 'if_', ['BOOL', 'EXPR', 'EXPR'], 1.)
grammar.add_rule('BOOL', 'equal_', ['EXPR', 'EXPR'], 1.)

grammar.add_rule('BOOL', 'flip_', [''], TERMINAL_WEIGHT)

# List-building operators
Beispiel #6
0
# Math rules (30-40 of these)
# ---------------------------

# Odd numbers
mix_grammar.add_rule("EXPR", "plus_", ["ODD", str(1)], 1.0)
mix_grammar.add_rule("ODD", "times_", ["X", str(2)], 1.0)

# Primes
mix_grammar.add_rule("EXPR", "isprime_", ["X"], 1.0)

# Squares, cubes
mix_grammar.add_rule("EXPR", "ipowf_", ["X", str(2)], 1.0)
mix_grammar.add_rule("EXPR", "ipowf_", ["X", str(3)], 1.0)

# { 2^n  -  32 }
register_primitive(lambda x: x if x in (2, 4, 8, 16, 64) else 0, name="pow2n_d32_")
mix_grammar.add_rule("EXPR", "pow2n_d32_", ["X"], 1.0)
# { 2^n  &  37 }
register_primitive(lambda x: x if x in (2, 4, 8, 16, 32, 37, 64) else 0, name="pow2n_u37_")
mix_grammar.add_rule("EXPR", "pow2n_u37_", ["X"], 1.0)

# [2,12] * n
for i in range(2, 13):
    mix_grammar.add_rule("EXPR", "times_", ["X", str(i)], 1.0)

# [2,10] ^ m
for i in range(2, 11):
    mix_grammar.add_rule("EXPR", "ipowf_", [str(i), "X"], 1.0)

# Ends in [0,9]
for i in range(0, 10):
Beispiel #7
0
NDATA = 50  # How many of each data point have we seen?

data = {
    'N V': NDATA,
    'D N V': NDATA,
    'D N V N': NDATA,
    'D N V D N': NDATA,
}

# # # # # # # # # # # # # # # # # # # # # # # # # # # #
## Here is an example of using define_for_evaluator

from LOTlib.Evaluation.Eval import register_primitive  # this creates new defines

# And this calls them:
register_primitive(LOTlib.Miscellaneous.flatten2str)

# # # # # # # # # # # # # # # # # # # # # # # # # # # #
TERMINAL_WEIGHT = 5.

grammar = Grammar()
grammar.add_rule('START', 'flatten2str', ['EXPR'], 1.0)

grammar.add_rule('BOOL', 'and_', ['BOOL', 'BOOL'], 1.)
grammar.add_rule('BOOL', 'or_', ['BOOL', 'BOOL'], 1.)
grammar.add_rule('BOOL', 'not_', ['BOOL'], 1.)

grammar.add_rule('EXPR', 'if_', ['BOOL', 'EXPR', 'EXPR'], 1.)
grammar.add_rule('BOOL', 'equal_', ['EXPR', 'EXPR'], 1.)

grammar.add_rule('BOOL', 'flip_', [''], TERMINAL_WEIGHT)
Beispiel #8
0
grammar.add_rule('EXPR', 'E', None, 3.0)

# To have a string terminal, it must be quoted:
#grammar.add_rule('EXPR', '\'e\'', None, 3.0)

# Then this is one way to use the variable "x" of the function.
# This gets named as the argument in evaluate_expression below
grammar.add_rule('EXPR', 'x', None, 10.0)


# We can register a new function that will be evaled via evaluate_expression
def mylambda():
    return 141.421


register_primitive(mylambda)

# A thunk function (lambdaZero is defined in Miscellaneous)
# We write these with [None] insead of []. The FunctionNode str function knows to print these with parens
# This notation keeps it simple since on a FunctionNode, the children ("to") are always a list.
grammar.add_rule('EXPR', 'mylambda', [], 1.0)
#or
grammar.add_rule('EXPR', 'mylambda()', None,
                 1.0)  # this is supported but not recommended

# EXPR -> plus_(EXPR, EXPR)
grammar.add_rule('EXPR', 'plus_', ['EXPR', 'EXPR'], 1.0)

# Or other operations
grammar.add_rule('EXPR', 'times_', ['EXPR', 'EXPR'], 1.0)
grammar.add_rule('EXPR', 'subtract_', ['EXPR', 'EXPR'], 1.0)
Beispiel #9
0
# some other simple terminals
# these are given much higher probability in order to keep the PCFG well-defined
grammar.add_rule('EXPR', '0.0', None, 3.0)
grammar.add_rule('EXPR', 'TAU', None, 3.0)
grammar.add_rule('EXPR', 'E', None, 3.0)

# To have a string terminal, it must be quoted:
#grammar.add_rule('EXPR', '\'e\'', None, 3.0)

# Then this is one way to use the variable "x" of the function.
# This gets named as the argument in evaluate_expression below
grammar.add_rule('EXPR', 'x', None, 10.0)

 # We can register a new function that will be evaled via evaluate_expression
def mylambda(): return 141.421
register_primitive(mylambda)

# A thunk function (lambdaZero is defined in Miscellaneous)
# We write these with [None] insead of []. The FunctionNode str function knows to print these with parens
# This notation keeps it simple since on a FunctionNode, the children ("to") are always a list.
grammar.add_rule('EXPR', 'mylambda', [], 1.0)
#or
grammar.add_rule('EXPR', 'mylambda()', None, 1.0)  # this is supported but not recommended

# EXPR -> plus_(EXPR, EXPR)
grammar.add_rule('EXPR', 'plus_', ['EXPR', 'EXPR'], 1.0)

# Or other operations
grammar.add_rule('EXPR', 'times_', ['EXPR', 'EXPR'], 1.0)
grammar.add_rule('EXPR', 'subtract_', ['EXPR', 'EXPR'], 1.0)
grammar.add_rule('EXPR', 'divide_', ['EXPR', 'EXPR'], 1.0)
Beispiel #10
0
# Math rules (30-40 of these)
# ---------------------------

# Odd numbers
mix_grammar.add_rule('EXPR', 'plus_', ['ODD', str(1)], 1.)
mix_grammar.add_rule('ODD', 'times_', ['X', str(2)], 1.)

# Primes
mix_grammar.add_rule('EXPR', 'isprime_', ['X'], 1.)

# Squares, cubes
mix_grammar.add_rule('EXPR', 'ipowf_', ['X', str(2)], 1.)
mix_grammar.add_rule('EXPR', 'ipowf_', ['X', str(3)], 1.)

# { 2^n  -  32 }
register_primitive(lambda x: x if x in (2, 4, 8, 16, 64) else 0, name='pow2n_d32_')
mix_grammar.add_rule('EXPR', 'pow2n_d32_', ['X'], 1.)
# { 2^n  &  37 }
register_primitive(lambda x: x if x in (2, 4, 8, 16, 32, 37, 64) else 0, name='pow2n_u37_')
mix_grammar.add_rule('EXPR', 'pow2n_u37_', ['X'], 1.)

# [2,12] * n
for i in range(2, 13):
    mix_grammar.add_rule('EXPR', 'times_', ['X', str(i)], 1.)

# [2,10] ^ m
for i in range(2, 11):
    mix_grammar.add_rule('EXPR', 'ipowf_', [str(i), 'X'], 1.)

# Ends in [0,9]
for i in range(0, 10):