Exemple #1
0
def is_conservative(h,testing_set):
    """Check if a hypothesis (funciton node) is conservative or not."""
    f = evaluate_expression(h, ['context'])
    for x in testing_set:
        a,b,s = x
        if f(a,b,s) != f(a, b.intersection(a), s.intersection(a) ): # HMM: is this right? We intersect s with a?
            return False
    return True
Exemple #2
0
def is_conservative(h, testing_set):
    """Check if a hypothesis (funciton node) is conservative or not."""
    f = evaluate_expression(h, ['context'])
    for x in testing_set:
        a, b, s = x
        if f(a, b, s) != f(a, b.intersection(a), s.intersection(
                a)):  # HMM: is this right? We intersect s with a?
            return False
    return True
    def value2function(self, value):
        """ How we convert a value into a function. Default is LOTlib.Miscellaneous.evaluate_expression """

        # Risky here to catch all exceptions, but we'll do it and warn on failure
        try:
            return evaluate_expression(value, args=self.args)
        except Exception as e:
            print "# Warning: failed to execute evaluate_expression on " + str(value)
            print "# ", e
            return lambdaNone
Exemple #4
0
 def compile_function(self):
     """Called in set_value to compile into a function."""
     if self.value.count_nodes() > self.maxnodes:
         return lambda *args: raise_exception(TooBigException)
     else:
         try:
             return evaluate_expression(str(self))
         except Exception as e:
             print "# Warning: failed to execute evaluate_expression on " + str(self)
             print "# ", e
             return lambda *args: raise_exception(EvaluationException)
Exemple #5
0
    def value2function(self, value):
        """ How we convert a value into a function. Default is LOTlib.Miscellaneous.evaluate_expression """

        # Risky here to catch all exceptions, but we'll do it and warn on failure
        try:
            return evaluate_expression(value, args=self.args)
        except Exception as e:
            print "# Warning: failed to execute evaluate_expression on " + str(
                value)
            print "# ", e
            return lambdaNone
Exemple #6
0
 def compile_function(self):
     """
     Overwrite this from FunctionHypothesis. Here, we add args for the constants so we can use them
     """
     return evaluate_expression(str(self))
Exemple #7
0
#grammar.add_rule('FUNCTION', 'lambda', ['EXPR'], 1.0,  bv_type='BOOL', bv_args=['EXPR'])

# Etc.

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Conditional:
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# if_ gets printed specially (see LOTlib.FunctionNode.__str__). Here COND is a name that is made up
# here for conditional expressions
grammar.add_rule('EXPR', 'if_', ['COND', 'EXPR', 'EXPR'], 1.0)
grammar.add_rule('COND', 'gt_', ['EXPR', 'EXPR'], 1.0)
grammar.add_rule('COND', 'eq_', ['EXPR', 'EXPR'], 1.0)
# Note that because if_ prints specially, it is correctly handled (via short circuit evaluation)
# so that we don't eval both branches unnecessarily

for _ in xrange(1000):

    t = grammar.generate(
    )  # Default is to generate from 'START'; else use 'START=t' to generate from type t

    # Now x is a FunctionNode

    # We can compile it via LOTlib.Miscellaneous.evaluate_expression
    # This says that t is a *function* with arguments 'x' (allowed via the grammar above)
    # The alternative way to do this would be to put a lambda at the top of each tree
    f = evaluate_expression(t, args=['x'])

    print t  # will call x.__str__ and display as a pythonesque string
    print map(f, range(0, 10))
Exemple #8
0
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# if_ gets printed specially (see LOTlib.FunctionNode.__str__). Here COND is a name that is made up
# here for conditional expressions
grammar.add_rule('EXPR', 'if_', ['COND', 'EXPR', 'EXPR'], 1.0)
grammar.add_rule('COND', 'gt_', ['EXPR', 'EXPR'], 1.0)
grammar.add_rule('COND', 'eq_', ['EXPR', 'EXPR'], 1.0)

# Note that because if_ prints specially in FunctionNode, it is correctly handled (via short circuit evaluation)
# so that we don't eval both branches unnecessarily

if __name__ == "__main__":

    for _ in xrange(1000):

        t = grammar.generate(
        )  # Default is to generate from 'START'; else use 'START=t' to generate from type t

        # We can make this into a function by adding a lambda and a variable name, corresponding to
        # the argument "x" that we built into the grammar. This step is defaultly done by a a LOTHypothesis (see below)

        f = evaluate_expression('lambda x:%s' % t)

        print t  # will call x.__str__ and display as a pythonesque string
        print map(f, range(0, 10))

        # Alternatively, we can just make a LOTHypothesis, which is typically the only place in LOTlib we use trees
        from LOTlib.Hypotheses.LOTHypothesis import LOTHypothesis
        h = LOTHypothesis(grammar, value=t, args=['x'])
        print map(h, range(0, 10))
Exemple #9
0

# Etc.

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Conditional:
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# if_ gets printed specially (see LOTlib.FunctionNode.__str__). Here COND is a name that is made up
# here for conditional expressions
grammar.add_rule('EXPR', 'if_', ['COND', 'EXPR', 'EXPR'], 1.0)
grammar.add_rule('COND', 'gt_', ['EXPR', 'EXPR'], 1.0)
grammar.add_rule('COND', 'eq_', ['EXPR', 'EXPR'], 1.0)
# Note that because if_ prints specially, it is correctly handled (via short circuit evaluation)
# so that we don't eval both branches unnecessarily


for _ in xrange(1000):

    t = grammar.generate() # Default is to generate from 'START'; else use 'START=t' to generate from type t

    # Now x is a FunctionNode

    # We can compile it via LOTlib.Miscellaneous.evaluate_expression
    # This says that t is a *function* with arguments 'x' (allowed via the grammar above)
    # The alternative way to do this would be to put a lambda at the top of each tree
    f = evaluate_expression(t, args=['x'])

    print t # will call x.__str__ and display as a pythonesque string
    print map(f, range(0,10))
Exemple #10
0
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# if_ gets printed specially (see LOTlib.FunctionNode.__str__). Here COND is a name that is made up
# here for conditional expressions
grammar.add_rule("EXPR", "if_", ["COND", "EXPR", "EXPR"], 1.0)
grammar.add_rule("COND", "gt_", ["EXPR", "EXPR"], 1.0)
grammar.add_rule("COND", "eq_", ["EXPR", "EXPR"], 1.0)

# Note that because if_ prints specially in FunctionNode, it is correctly handled (via short circuit evaluation)
# so that we don't eval both branches unnecessarily

if __name__ == "__main__":

    for _ in xrange(1000):

        t = grammar.generate()  # Default is to generate from 'START'; else use 'START=t' to generate from type t

        # We can make this into a function by adding a lambda and a variable name, corresponding to
        # the argument "x" that we built into the grammar. This step is defaultly done by a a LOTHypothesis (see below)

        f = evaluate_expression("lambda x:%s" % t)

        print t  # will call x.__str__ and display as a pythonesque string
        print map(f, range(0, 10))

        # Alternatively, we can just make a LOTHypothesis, which is typically the only place in LOTlib we use trees
        from LOTlib.Hypotheses.LOTHypothesis import LOTHypothesis

        h = LOTHypothesis(grammar, value=t, args=["x"])
        print map(h, range(0, 10))