Skip to content

Commit

Permalink
fix(interpreted functions): fixed support in usertype walker, fixed b…
Browse files Browse the repository at this point in the history
…ug in IF remover
  • Loading branch information
Samuel Gobbi committed Nov 12, 2024
1 parent dec25bb commit 1800320
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,12 @@ def _compile(
if tuple(ifun_exp.args) in new_objects:
o = new_objects[tuple(ifun_exp.args)]
else:
if_known[ifun].append(tuple(ifun_exp.args))
o = Object(get_fresh_name(new_problem, f"_o"), kNum)
new_objects[tuple(ifun_exp.args)] = o
new_problem.add_object(o)
if tuple(ifun_exp.args) not in if_known[ifun]:
if_known[ifun].append(tuple(ifun_exp.args))

new_problem.set_initial_value(f(o), val)

assignment_tracking_fluents: dict = {}
Expand Down
29 changes: 29 additions & 0 deletions unified_planning/model/walkers/usertype_fluents_walker.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,35 @@ def walk_plus(
exp_args, variables, fluents = self._process_exp_args(args)
return (self.manager.Plus(*exp_args), None, variables, None, fluents)

def walk_interpreted_function_exp(
self,
expression: FNode,
args: List[
Tuple[
FNode,
Optional["up.model.variable.Variable"],
Set["up.model.variable.Variable"],
Optional[FNode],
Set[FNode],
]
],
) -> Tuple[
FNode,
Optional["up.model.variable.Variable"],
Set["up.model.variable.Variable"],
Optional[FNode],
Set[FNode],
]:
exp_args, variables, fluents = self._process_exp_args(args)
i_f = expression.interpreted_function()
return (

Check warning on line 567 in unified_planning/model/walkers/usertype_fluents_walker.py

View check run for this annotation

Codecov / codecov/patch

unified_planning/model/walkers/usertype_fluents_walker.py#L565-L567

Added lines #L565 - L567 were not covered by tests
self.manager.InterpretedFunctionExp(i_f, exp_args),
None,
variables,
None,
fluents,
)

def walk_minus(
self,
expression: FNode,
Expand Down
22 changes: 16 additions & 6 deletions unified_planning/test/examples/complex_interpreted_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,25 @@ def wet(israining, haveumbrella):
problems["go_home_with_rain_and_interpreted_functions"] = ifproblem

def if_cut_a_slice(in_val):
out_val = Fraction(0, 1)
if in_val >= Fraction(1, 8):
out_val = Fraction(in_val - Fraction(1, 8))
else:
out_val = Fraction(0, 1)
return out_val

signature_if_cut = OrderedDict()
signature_if_cut["i_one"] = RealType()
if_cut = InterpretedFunction("if_cut", RealType(), signature_if_cut, if_cut_a_slice)
def if_is_there_enough_pizza(in_val):
if in_val >= Fraction(1, 8):
return True
else:
return False

signature_if_both = OrderedDict()
signature_if_both["i_one"] = RealType()
if_cut = InterpretedFunction(
"if_cut", RealType(), signature_if_both, if_cut_a_slice
)
if_available = InterpretedFunction(
"if_available", BoolType(), signature_if_both, if_is_there_enough_pizza
)

pizza = Fluent("pizza", RealType())
hunger = Fluent("hunger", IntType())
Expand All @@ -219,7 +229,7 @@ def if_cut_a_slice(in_val):
cut = InstantaneousAction("cut")
cut.add_effect(pizza, if_cut(pizza))
cut.add_effect(slices, Plus(slices, 1))
cut.add_precondition(GE(pizza, Fraction(1, 8)))
cut.add_precondition(if_available(pizza))

eat = InstantaneousAction("eat")
eat.add_effect(hunger, Minus(hunger, 1))
Expand Down
18 changes: 8 additions & 10 deletions unified_planning/test/test_infix_notation.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,15 @@ def test_infix_with_parameters(self):
self._test_helper_function(i_1, r_1, i_2, r_2, b_1, b_2, b_3)

def test_infix_with_interpreted_functions(self):
def if_placeholder():
return 0

placeholder = lambda *args: None
signature_empty: OrderedDict = OrderedDict()
i_1 = InterpretedFunction("i_1", IntType(), signature_empty, if_placeholder)
r_1 = InterpretedFunction("r_1", RealType(), signature_empty, if_placeholder)
i_2 = InterpretedFunction("i_2", IntType(), signature_empty, if_placeholder)
r_2 = InterpretedFunction("r_2", RealType(), signature_empty, if_placeholder)
b_1 = InterpretedFunction("b_1", BoolType(), signature_empty, if_placeholder)
b_2 = InterpretedFunction("b_2", BoolType(), signature_empty, if_placeholder)
b_3 = InterpretedFunction("b_3", BoolType(), signature_empty, if_placeholder)
i_1 = InterpretedFunction("i_1", IntType(), signature_empty, placeholder)
r_1 = InterpretedFunction("r_1", RealType(), signature_empty, placeholder)
i_2 = InterpretedFunction("i_2", IntType(), signature_empty, placeholder)
r_2 = InterpretedFunction("r_2", RealType(), signature_empty, placeholder)
b_1 = InterpretedFunction("b_1", BoolType(), signature_empty, placeholder)
b_2 = InterpretedFunction("b_2", BoolType(), signature_empty, placeholder)
b_3 = InterpretedFunction("b_3", BoolType(), signature_empty, placeholder)
self._test_helper_function(i_1, r_1, i_2, r_2, b_1, b_2, b_3)

def test_infix_on_objects(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ def test_interpreted_functions_in_preconditions_planner_complex(self):

@skipIfEngineNotAvailable("opt-pddl-planner")
def test_interpreted_functions_reals(self):
# BUG - check example file "complex_interpreted_functions.py"
testproblem = self.problems["if_reals_condition_effect_pizza"]
problem = testproblem.problem
with OneshotPlanner(
Expand Down

0 comments on commit 1800320

Please sign in to comment.