From b7a4a68f406782a808b50d5875d4a5266f47146d Mon Sep 17 00:00:00 2001 From: Toki Migimatsu Date: Tue, 26 Apr 2022 16:15:39 -0700 Subject: [PATCH] feat: Implement fast method to apply sequence of actions --- include/symbolic/pddl.h | 16 +++++++++++++++- src/pddl.cc | 14 ++++++++++++++ src/python/symbolic.cc | 20 ++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/include/symbolic/pddl.h b/include/symbolic/pddl.h index 5466525..133d22f 100644 --- a/include/symbolic/pddl.h +++ b/include/symbolic/pddl.h @@ -82,13 +82,27 @@ class Pddl { * derived predicates. * * @param state Current state. - * @param action Action call in the form of `"action(obj_a, obj_b)"`. + * @param action_call Action call in the form of `"action(obj_a, obj_b)"`. * @returns Next state. * * @seepython{symbolic.Pddl,next_state} */ State NextState(const State& state, const std::string& action_call) const; + /** + * Execute a sequence of actions from the given state. + * + * The action preconditionss are not checked. The resulting state includes + * derived predicates. + * + * @param state Current state. + * @param action_call Action calls in the form of `"action(obj_a, obj_b)"`. + * @returns Final state. + * + * @seepython{symbolic.Pddl.execute} + */ + State ApplyActions(const State& state, const std::vector& action_calls) const; + /** * Apply the derived predicates to the given state. */ diff --git a/src/pddl.cc b/src/pddl.cc index e5221de..39fc401 100644 --- a/src/pddl.cc +++ b/src/pddl.cc @@ -334,6 +334,20 @@ TEST_CASE_FIXTURE(testing::Fixture, "Pddl.NextState") { REQUIRE(pddl.NextState(state, "pick(hook)") == next_state); } +State Pddl::ApplyActions(const State& state, + const std::vector& action_calls) const { + State next_state(state); + for (const std::string& action_call : action_calls) { + const std::pair> action_args = + Action::Parse(*this, action_call); + const Action& action = action_args.first; + const std::vector& arguments = action_args.second; + + Apply(action, arguments, derived_predicates(), &next_state); + } + return next_state; +} + State Pddl::DerivedState(const State& state) const { return DerivedPredicate::Apply(state, derived_predicates()); } diff --git a/src/python/symbolic.cc b/src/python/symbolic.cc index 216f3fe..2812f92 100644 --- a/src/python/symbolic.cc +++ b/src/python/symbolic.cc @@ -157,6 +157,26 @@ PYBIND11_MODULE(pysymbolic, m) { .. seealso:: C++: :symbolic:`symbolic::Pddl::NextState`. )pbdoc") + .def( + "apply_actions", + [](const Pddl& pddl, const std::unordered_set& state, + const std::vector& actions) { + return pddl.ApplyActions(State(pddl, state), actions).Stringify(); + }, + "state"_a, "action"_a, R"pbdoc( + Execute a sequence of actions from the given state + + The action preconditions are not checked. The resulting state includes + derived predicates. + + Args: + state: Current state. + actions: Action calls in the form of :code:`"action(obj_a, obj_b)"`. + Returns: + Final state. + + .. seealso:: C++: :symbolic:`symbolic::Pddl::Execute`. + )pbdoc") .def( "derived_state", [](const Pddl& pddl, const std::unordered_set& state) {