diff --git a/dependency_repos.repos b/dependency_repos.repos index 4774724f..4adb7cfb 100644 --- a/dependency_repos.repos +++ b/dependency_repos.repos @@ -15,7 +15,7 @@ repositories: aletheia: type: git url: https://github.com/HanielUlises/del-planner.git - version: 3bae6286b01cfc62116113a424b63a2eea85c1c0 + version: 17cd874b4a0ea5f205dc2408c4d18088952eb83b # The EPDDL grounder plansys2_epddl_grounder runs. It is a plain CMake # project rather than a ROS package, so colcon builds it as one and installs # the binary into /bin, which puts `plank` on PATH once the workspace diff --git a/eplansys_docs/concepts/architecture.rst b/eplansys_docs/concepts/architecture.rst index 8e3d7007..6d2d8274 100644 --- a/eplansys_docs/concepts/architecture.rst +++ b/eplansys_docs/concepts/architecture.rst @@ -173,6 +173,13 @@ Parameters are declared under the plugin name at configure time - Factor by which the search's time bound grows between iterations. Optimality does not depend on it; ``1.0`` steps to the next lower bound. + * - ``temporal_max_expansions`` + - int + - ``0`` + - Expansion limit of the optimal search; 0 is none. Reaching it, or + three quarters of the solver timeout, the solver plans untimed and + schedules that policy with the commutation test: an admissible + temporal policy, not necessarily the shortest. * - ``consistent_beliefs`` - bool - ``true`` @@ -354,7 +361,12 @@ The solver starts an action only if the running set remains coherent. Under coherence, the model after a set of overlapping actions does not depend on the order in which they end. -The search minimises the worst-case makespan over outcomes. Its result can +The search minimises the worst-case makespan over outcomes. When it does not +finish in time (three quarters of the solver timeout), the solver plans +untimed and schedules that policy instead, each action starting as early as +the commutation test allows: the result is still a temporal policy with the +same guarantees, and no longer than the untimed one run in sequence, but not +necessarily the shortest. Its result can differ from the untimed policy: on the corridor, both robots inspect at once (35 s), where the shortest untimed policy, inspect and announce, takes 36 s. diff --git a/plansys2_bringup/params/plansys2_epistemic_params.yaml b/plansys2_bringup/params/plansys2_epistemic_params.yaml index eb31f0c1..78aebfcf 100644 --- a/plansys2_bringup/params/plansys2_epistemic_params.yaml +++ b/plansys2_bringup/params/plansys2_epistemic_params.yaml @@ -135,6 +135,11 @@ planner: # How the search's time bound grows between iterations. Optimality does # not depend on it; 1.0 steps to the next lower bound. temporal_bound_growth: 1.0 + # Expansion limit of the optimal temporal search (0: none). When it, or + # three quarters of plan_solver_timeout, runs out, the solver returns + # the untimed policy scheduled with the commutation test: admissible, + # but not necessarily the shortest. + temporal_max_expansions: 0 # The same planning is available from an external Aletheia binary rather # than in process, through plansys2_aletheia_plan_solver. It takes these diff --git a/plansys2_epistemic_planner/CMakeLists.txt b/plansys2_epistemic_planner/CMakeLists.txt index af5fe09c..9c3d8c8b 100644 --- a/plansys2_epistemic_planner/CMakeLists.txt +++ b/plansys2_epistemic_planner/CMakeLists.txt @@ -16,7 +16,7 @@ find_package(nlohmann_json 3.10 REQUIRED) # The planning core is Aletheia's, linked as a library rather than copied in, # so the search here and the epistemic_planner binary are one implementation. -find_package(aletheia 0.4 REQUIRED) +find_package(aletheia 0.5 REQUIRED) set(EPISTEMIC_SOURCES src/action_mapping.cpp diff --git a/plansys2_epistemic_planner/include/plansys2_epistemic_planner/epistemic_plan_solver.hpp b/plansys2_epistemic_planner/include/plansys2_epistemic_planner/epistemic_plan_solver.hpp index 017977e1..704df670 100644 --- a/plansys2_epistemic_planner/include/plansys2_epistemic_planner/epistemic_plan_solver.hpp +++ b/plansys2_epistemic_planner/include/plansys2_epistemic_planner/epistemic_plan_solver.hpp @@ -176,6 +176,7 @@ class EpistemicPlanSolver : public PlanSolverBase std::string max_concurrency_parameter_name_; std::string temporal_estimate_parameter_name_; std::string temporal_bound_growth_parameter_name_; + std::string temporal_max_expansions_parameter_name_; /// The last goal the epistemic state published, as text. Latched by the /// subscription rather than fetched, so reading it costs nothing and cannot diff --git a/plansys2_epistemic_planner/src/epistemic_plan_solver.cpp b/plansys2_epistemic_planner/src/epistemic_plan_solver.cpp index 7147deba..f28e729c 100644 --- a/plansys2_epistemic_planner/src/epistemic_plan_solver.cpp +++ b/plansys2_epistemic_planner/src/epistemic_plan_solver.cpp @@ -188,6 +188,7 @@ void EpistemicPlanSolver::configure( max_concurrency_parameter_name_ = plugin_name + ".max_concurrency"; temporal_estimate_parameter_name_ = plugin_name + ".temporal_estimate"; temporal_bound_growth_parameter_name_ = plugin_name + ".temporal_bound_growth"; + temporal_max_expansions_parameter_name_ = plugin_name + ".temporal_max_expansions"; const auto declare = [&](const std::string & name, const std::string & def) { if (!lc_node_->has_parameter(name)) { @@ -229,6 +230,11 @@ void EpistemicPlanSolver::configure( if (!lc_node_->has_parameter(temporal_bound_growth_parameter_name_)) { lc_node_->declare_parameter(temporal_bound_growth_parameter_name_, 1.0); } + // 0: no limit. The optimal search stops after this many expansions and the + // solver falls back as on a timeout. + if (!lc_node_->has_parameter(temporal_max_expansions_parameter_name_)) { + lc_node_->declare_parameter(temporal_max_expansions_parameter_name_, 0); + } declare_epddl_parameters(lc_node_, epddl_parameter_names_); @@ -496,7 +502,12 @@ std::optional EpistemicPlanSolver::getPlan( temporal::SearchOptions options; options.max_concurrency = static_cast( std::max(1, lc_node_->get_parameter(max_concurrency_parameter_name_).as_int())); - options.deadline = deadline; + // Three quarters of the time for the optimal search; what is left is for + // the fallback, should it be needed. + options.deadline = started + (deadline - started) * 3 / 4; + const int64_t max_expansions = + lc_node_->get_parameter(temporal_max_expansions_parameter_name_).as_int(); + options.max_expansions = static_cast(std::max(0, max_expansions)); const std::string estimate = parameter(temporal_estimate_parameter_name_); if (estimate == "critical_path") { @@ -513,17 +524,37 @@ std::optional EpistemicPlanSolver::getPlan( temporal::SearchStats stats; const auto durations = durations_from_mapping(task, mapping); - const auto policy = temporal::search_policy(task, durations, options, &stats); + auto policy = temporal::search_policy(task, durations, options, &stats); RCLCPP_INFO( lc_node_->get_logger(), "[epistemic] temporal search: %zu expanded, %zu start checks (%zu rejected), %.3f s", stats.expanded, stats.start_checks, stats.start_rejected, stats.elapsed_sec); + if (!policy && stats.exhausted) { + RCLCPP_WARN(lc_node_->get_logger(), "[epistemic] no temporal policy exists"); + return std::nullopt; + } if (!policy) { + // The optimal search did not finish. Plan untimed instead and schedule + // the result with StartCheck: an admissible temporal policy, no longer + // than the untimed one run in sequence, but not the shortest. + const auto h = make_heuristic("rpg", task); + const auto untimed = aostar::search(task, *h, 0, deadline); + if (untimed) { + policy = temporal::schedule_untimed( + task, durations, untimed->plan_tree, options.max_concurrency); + } + if (!policy) { + RCLCPP_WARN( + lc_node_->get_logger(), + "[epistemic] no temporal policy found before the timeout, and no untimed one " + "to schedule"); + return std::nullopt; + } RCLCPP_WARN( - lc_node_->get_logger(), "[epistemic] %s", - stats.exhausted ? "no temporal policy exists" : - "no temporal policy found before the timeout"); - return std::nullopt; + lc_node_->get_logger(), + "[epistemic] the optimal temporal search did not finish; returning the untimed " + "policy scheduled with StartCheck (makespan %.2f s, not necessarily the least)", + policy->makespan); } std::string why; if (!temporal::validate_policy(task, durations, *policy, &why)) { diff --git a/plansys2_epistemic_planner/test/temporal_plan_test.cpp b/plansys2_epistemic_planner/test/temporal_plan_test.cpp index 5a282d76..578f0d65 100644 --- a/plansys2_epistemic_planner/test/temporal_plan_test.cpp +++ b/plansys2_epistemic_planner/test/temporal_plan_test.cpp @@ -26,6 +26,7 @@ #include "gtest/gtest.h" #include "aletheia/bisimulation.hpp" #include "aletheia/parser.hpp" +#include "aletheia/strategy.hpp" #include "plansys2_epistemic_planner/action_mapping.hpp" #include "plansys2_epistemic_planner/policy_plan.hpp" #include "task_fixtures.hpp" @@ -164,3 +165,25 @@ TEST(TemporalPlan, TheCriticalPathSearchReturnsAPlanNoShorterThanOptimal) ASSERT_TRUE(plan.has_value()) << error; EXPECT_TRUE(plan->temporal); } + +TEST(TemporalPlan, TheScheduledUntimedPolicySerialises) +{ + // What the solver returns when the optimal search does not finish: the + // untimed policy scheduled with StartCheck, overlapping the depot's drives + // and looks where they commute. + const auto task = load_task(task_path("robot-fleet-depot")); + const auto mapping = plansys2::ActionMapping::load(mapping_path("robot-fleet-depot")); + const auto durations = plansys2::durations_from_mapping(task, mapping); + const auto h = make_heuristic("rpg", task); + const auto untimed = aostar::search(task, *h); + ASSERT_TRUE(untimed.has_value()); + + const auto scheduled = temporal::schedule_untimed(task, durations, untimed->plan_tree, 3); + ASSERT_TRUE(scheduled.has_value()); + EXPECT_LT(scheduled->makespan, 72.0); // the six steps of a branch, in sequence + + std::string error; + const auto plan = plansys2::to_temporal_plan(task, *scheduled, mapping, error); + ASSERT_TRUE(plan.has_value()) << error; + EXPECT_TRUE(plan->temporal); +} diff --git a/plansys2_tests/test_8/test_8.cpp b/plansys2_tests/test_8/test_8.cpp index 5920bd60..caf06ed3 100644 --- a/plansys2_tests/test_8/test_8.cpp +++ b/plansys2_tests/test_8/test_8.cpp @@ -80,7 +80,9 @@ struct Mission /// cannot supply, since the task designates both a blocked and a clear world /// and neither is the model's to choose; the performer reports it on finish, /// the way a real one would report what its sensor returned. -Mission run_mission(const std::string & observed, const std::string & estimate = "admissible") +Mission run_mission( + const std::string & observed, const std::string & estimate = "admissible", + int max_expansions = 0) { Mission mission; @@ -191,6 +193,8 @@ Mission run_mission(const std::string & observed, const std::string & estimate = // Plan with the mapping's durations and overlap what commutes. planner_node->set_parameter(rclcpp::Parameter("EPISTEMIC.temporal", true)); planner_node->set_parameter(rclcpp::Parameter("EPISTEMIC.temporal_estimate", estimate)); + planner_node->set_parameter( + rclcpp::Parameter("EPISTEMIC.temporal_max_expansions", max_expansions)); domain_node->trigger_transition(lifecycle_msgs::msg::Transition::TRANSITION_ACTIVATE); problem_node->trigger_transition(lifecycle_msgs::msg::Transition::TRANSITION_ACTIVATE); @@ -333,6 +337,18 @@ TEST(test_8, the_critical_path_search_plans_the_same_mission) << describe(mission); } +TEST(test_8, a_search_that_does_not_finish_falls_back_to_the_scheduled_untimed_policy) +{ + // One expansion is not enough for the optimal search, so the solver plans + // untimed and schedules that policy: r1 drives, looks and announces. It is + // still a temporal policy, admissible, and the mission still succeeds. + const auto mission = run_mission("e-inspect-blocked", "admissible", 1); + ASSERT_TRUE(mission.temporal) << "the fallback must still return a temporal policy"; + EXPECT_EQ(mission.result, plansys2_msgs::action::ExecutePlan::Result::SUCCESS) + << describe(mission); + EXPECT_TRUE(mission.ran("(report_blocked")) << describe(mission); +} + // Leave through _exit, so the DDS threads never outlive the process. // // rclcpp::shutdown() does not finalise the global context; that happens in