def test_neg_precondition_compilation_on_problem2(): problem = generate_strips_blocksworld_problem() lang = problem.language b1, clear, on, ontable, handempty, holding = lang.get( 'b1', 'clear', 'on', 'ontable', 'handempty', 'holding') # Change the goal to include some negated preconditions, this should trigger # the rewriting of some action effects problem.goal = ~ontable(b1) & ~handempty() compiled = compile_negated_preconditions_away(problem) # Check that indeed new effects have been added of the appropriate type and appropriate predicate assert str(compiled.get_action( 'unstack').effects[-1]) == '(T -> ADD(_not_handempty()))' assert str(compiled.get_action( 'stack').effects[-1]) == '(T -> DEL(_not_handempty()))' # Check the goal has been correctly rewritten assert str(compiled.goal) == '(_not_ontable(b1) and _not_handempty())' # Check the initial state has been correctly updated init = compiled.init nhe, nont = lang.get('_not_handempty', '_not_ontable') assert init[nont(b1)] or init[ontable(b1)] assert init[neg(nhe())] or init[neg(handempty())]
def test_neg_precondition_compilation_on_formulas(): problem = generate_strips_blocksworld_problem() lang = problem.language clear, on, ontable, handempty, holding = lang.get('clear', 'on', 'ontable', 'handempty', 'holding') x = lang.variable('x', 'object') y = lang.variable('y', 'object') negpreds = dict() comp = compile_away_formula_negated_literals(clear(x) & (x != y), negpreds) assert comp == clear(x) & (x != y) and not negpreds # No change was made comp = compile_away_formula_negated_literals(neg(x == y), negpreds) assert comp == neg(x == y) and not negpreds # No change was made comp = compile_away_formula_negated_literals( clear(x) & ontable(x), negpreds) assert comp == clear(x) & ontable(x) and not negpreds # No change was made comp = compile_away_formula_negated_literals( clear(x) & ~ontable(x), negpreds) assert str(comp) == '(clear(x) and _not_ontable(x))' # Compile again to check that predicate is not declared twice, which would raise an error comp = compile_away_formula_negated_literals( clear(x) & ~ontable(x), negpreds) assert str(comp) == '(clear(x) and _not_ontable(x))'
def create_small_bw_task(): lang = generate_small_fstrips_bw_language() init = tarski.model.create(lang) b1, b2, b3, b4, clear, loc, table = lang.get('b1', 'b2', 'b3', 'b4', 'clear', 'loc', 'table') block, place = lang.get('block', 'place') init.set(loc, b1, b2) # loc(b1) := b2 init.set(loc, b2, b3) # loc(b2) := b3 init.set(loc, b3, table) # loc(b3) := table init.set(loc, b4, table) # loc(b4) := table init.add(clear, b1) # clear(b1) init.add(clear, b4) # clear(b4) init.add(clear, table) # clear(table) src = lang.variable('src', block) dest = lang.variable('dest', place) x = lang.variable('x', block) y = lang.variable('y', block) clear_constraint = forall( x, equiv(neg(clear(x)), land(x != table, exists(y, loc(y) == x)))) G = land(loc(b1) == b2, loc(b2) == b3, loc(b3) == b4, loc(b4) == table) problem = fs.Problem("tower4", "blocksworld") problem.language = lang problem.init = init problem.goal = G problem.constraints += [clear_constraint] problem.action('move', [src, dest], land(clear(src), clear(dest)), [fs.FunctionalEffect(loc(src), dest)]) return problem
def test_variables_classification(): tw = tarskiworld.create_small_world() x = tw.variable('x', tw.Object) y = tw.variable('y', tw.Object) s = neg(land(tw.Cube(x), exists(y, land(tw.Tet(x), tw.LeftOf(x, y))))) free = free_variables(s) assert len(free) == 1 and symref(free[0]) == symref(x) assert len(all_variables(s)) == 2
def test_ground_actions_on_negated_preconditions(): problem = create_sample_problem() # We negate all atoms in the precondition, which makes all 8^3 combinations of objects be a valid grounding, # since all precondition atoms are ignored pick_prec = problem.actions['pick'].precondition pick_prec.subformulas = tuple(neg(f) for f in pick_prec.subformulas) actions = compute_action_groundings(problem) assert len(actions['pick']) == 512
def test_simplifier(): problem = generate_fstrips_counters_problem(ncounters=3) lang = problem.language value, max_int, counter, val_t, c1 = lang.get('value', 'max_int', 'counter', 'val', 'c1') x = lang.variable('x', counter) two, three, six = [lang.constant(c, val_t) for c in (2, 3, 6)] s = Simplify(problem, problem.init) assert symref(s.simplify_expression(x)) == symref(x) assert symref(s.simplify_expression(value(c1) < max_int())) == symref( value(c1) < six) # max_int evaluates to 6 assert s.simplify_expression(two < max_int()) is True assert s.simplify_expression(two > three) is False # conjunction evaluates to false because of first conjunct: falseconj = land(two > three, value(c1) < max_int()) assert s.simplify_expression(falseconj) is False assert s.simplify_expression(neg(falseconj)) is True # first conjunct gets removed: assert str(s.simplify_expression(land( two < three, value(c1) < max_int()))) == '<(value(c1),6)' # first disjunct gets removed because it is false assert str(s.simplify_expression(lor( two > three, value(c1) < max_int()))) == '<(value(c1),6)' assert str(s.simplify_expression(forall( x, value(x) < max_int()))) == 'forall x : (<(value(x),6))' assert s.simplify_expression(forall(x, two + three <= 6)) is True inc = problem.get_action('increment') simp = s.simplify_action(inc) assert str(simp.precondition) == '<(value(c),6)' assert str(simp.effects) == str(inc.effects) eff = UniversalEffect(x, [value(x) << three]) assert str( s.simplify_effect(eff)) == '(T -> forall (x) : ((T -> value(x) := 3)))' simp = s.simplify() assert str(simp.get_action('increment').precondition) == '<(value(c),6)' # Make sure there is no mention to the compiled away "max_int" symbol in the language assert not simp.language.has_function("max_int") # Make sure there is no mention to the compiled away "max_int" symbol in the initial state exts = list(simp.init.list_all_extensions().keys()) assert ('max_int', 'val') not in exts
def test_simplification_of_negation(): problem = tarski.benchmarks.blocksworld.generate_strips_blocksworld_problem( ) lang = problem.language b1, clear, on, ontable, handempty, holding = lang.get( 'b1', 'clear', 'on', 'ontable', 'handempty', 'holding') s = Simplify(problem, problem.init) cb1 = clear(b1) assert str(s.simplify_expression(land(cb1, neg(bot)))) == 'clear(b1)' assert str(s.simplify_expression(cb1)) == 'clear(b1)' # No evaluation made assert str(s.simplify_expression(neg( neg(cb1)))) == 'clear(b1)' # Double negation gets removed assert s.simplify_expression(land(neg(bot), neg(bot))) is True assert s.simplify_expression(lor(neg(top), neg(bot))) is True assert s.simplify_expression(lor(neg(top), neg(top))) is False act = problem.get_action('unstack') simp = s.simplify_action(act) assert simp
def create_simple_problem(): lang = create_simple_untyped_language() problem = fs.create_fstrips_problem(lang, problem_name="simple", domain_name="simple") p, q, a = lang.get("p", "q", "a") init = tarski.model.create(lang) problem.init = init init.add(p, a) # p(a) init.add(q, a) # q(a) problem.goal = neg(p(a)) x = lang.variable("x", lang.Object) problem.action("negate", [x], precondition=p(a), effects=[fs.DelEffect(p(a))]) return problem
def test_detect_free_variables(): tw = tarskiworld.create_small_world() x = tw.variable('x', tw.Object) y = tw.variable('y', tw.Object) s = neg(land(tw.Cube(x), exists(y, land(tw.Tet(x), tw.LeftOf(x, y))))) assert len(free_variables(s)) == 1