From bb1c0f6943ced42b6c0f459c8cf68e58765d33da Mon Sep 17 00:00:00 2001 From: Javier Segovia Date: Thu, 25 May 2023 13:42:29 +0200 Subject: [PATCH] Generate PDDL plans in root folder when validating GP plans --- src/action.h | 11 +++++++++++ src/program.h | 25 +++++++++++++++++++++++-- src/runner.h | 16 +++++++++++++--- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/action.h b/src/action.h index d5995b208..e2606f772 100644 --- a/src/action.h +++ b/src/action.h @@ -237,6 +237,17 @@ class Action{ } return ret; } + + [[nodiscard]] virtual std::string to_pddl_grounded() const{ + auto pddl_grounded = std::string("(") + _name; + if(not _pointer_parameters.empty()){ + // Action grounded over pointer parameters + for (const auto &ptr_param: _pointer_parameters) + pddl_grounded += " " + ptr_param->get_object()->get_name(); + } + pddl_grounded += ")"; + return pddl_grounded; + } protected: std::string _name; diff --git a/src/program.h b/src/program.h index 7246b70c6..67343926f 100644 --- a/src/program.h +++ b/src/program.h @@ -193,8 +193,13 @@ class Program{ return ptr_assigned; } - std::vector< std::unique_ptr > run(GeneralizedPlanningProblem *gpp){ + std::vector< std::unique_ptr > run(GeneralizedPlanningProblem *gpp, + bool save_pddl_plans = false){ reset_performance_variables(); + if(save_pddl_plans){ + _pddl_plans.clear(); + _pddl_plans.resize(gpp->get_num_instances()+1); + } auto actions_theory = gpp->is_actions_theory(); auto infinite_detection = gpp->get_infinite_detection(); @@ -268,7 +273,15 @@ class Program{ line = ps->get_line(); auto ins_type = _instructions[line]->get_type(); if( ins_type == ActionType::Math ) _num_of_math_planning_actions++; - else if(ins_type == ActionType::Memory ) _num_of_mem_planning_actions++; + else if(ins_type == ActionType::Memory ) { + _num_of_mem_planning_actions++; + // Memory planning actions are saved in PDDL plans + if(save_pddl_plans) { + auto act = dynamic_cast(_instructions[line]); + if(act and act->is_applicable(ins, ps)) + _pddl_plans[ins->get_instance_id()].emplace_back(act->get_action()->to_pddl_grounded()); + } + } // Applying current instruction //std::cout << _instructions[line]->to_string(false) << "\n"; @@ -493,6 +506,11 @@ class Program{ return _failed_instance_idx; } + [[nodiscard]] vec_str_t get_plan(size_t instance_id) const{ + assert(instance_id < _pddl_plans.size()); + return _pddl_plans[instance_id]; + } + [[nodiscard]] std::string to_string(bool full_info) const{ std::string ret; for(size_t line = 0; line < _instructions.size(); line++ ){ @@ -514,6 +532,9 @@ class Program{ long long _num_of_mem_planning_actions; long long _failed_instance_idx; + /// PDDL plans + std::vector _pddl_plans; + /// Landmarks data //std::vector< std::shared_ptr > _landmark_graphs; // accessible from gpp //value_t _landmark_count; diff --git a/src/runner.h b/src/runner.h index b7a20741e..a09ac83b8 100644 --- a/src/runner.h +++ b/src/runner.h @@ -72,8 +72,18 @@ namespace runner{ void run_program(Program *prog, GeneralizedPlanningProblem *gpp, - StatsInfo *stats_info){ - auto vps = prog->run( gpp ); + StatsInfo *stats_info, + bool save_pddl_plans = false){ + auto vps = prog->run( gpp, save_pddl_plans ); + // ToDo: print pddl_plans to files here + if(save_pddl_plans){ + for(size_t instance_id = 1; instance_id <= gpp->get_num_instances(); instance_id++){ + std::ofstream ofs("plan." + std::to_string(instance_id)); + for(const auto& str_act : prog->get_plan(instance_id)) + ofs << str_act << "\n"; + ofs.close(); + } + } stats_info->add_info_msg("Number of instances: " + std::to_string(gpp->get_num_instances())); if( !vps.empty() ) stats_info->add_info_msg("GOAL ACHIEVED!"); else stats_info->add_info_msg("INVALID GENERAL PLAN :("); @@ -180,7 +190,7 @@ namespace runner{ if(arg_parser->get_mode() == "validation-prog") { auto th_name = arg_parser->get_theory_name(); - run_program(prog.get(), gpp.get(), stats_info.get()); + run_program(prog.get(), gpp.get(), stats_info.get(), true); } else if(arg_parser->get_mode() == "validation-cpp"){ generate_cpp_code(gpp.get(), prog.get(), dest_folder_file.first, dest_folder_file.second, stats_info.get(), arg_parser.get());