diff --git a/src/search/potentials/diverse_potential_heuristics.cc b/src/search/potentials/diverse_potential_heuristics.cc index 49ac88c789..c292528a5f 100644 --- a/src/search/potentials/diverse_potential_heuristics.cc +++ b/src/search/potentials/diverse_potential_heuristics.cc @@ -15,12 +15,23 @@ using namespace std; namespace potentials { -DiversePotentialHeuristics::DiversePotentialHeuristics(const plugins::Options &opts) - : optimizer(opts), - max_num_heuristics(opts.get("max_num_heuristics")), - num_samples(opts.get("num_samples")), - rng(utils::parse_rng_from_options(opts)), - log(utils::get_log_from_options(opts)) { +DiversePotentialHeuristics::DiversePotentialHeuristics( + int num_samples, + int max_num_heuristics, + double max_potential, + lp::LPSolverType lpsolver, + const shared_ptr &transform, + int random_seed, + utils::Verbosity verbosity + ) + : optimizer( + transform, + lpsolver, + max_potential), + max_num_heuristics(max_num_heuristics), + num_samples(num_samples), + rng(utils::get_rng(random_seed)), + log(utils::get_log_for_verbosity(verbosity)) { } SamplesToFunctionsMap @@ -166,9 +177,22 @@ class DiversePotentialMaxHeuristicFeature : public plugins::TypedFeature create_component(const plugins::Options &options, const utils::Context &) const override { - DiversePotentialHeuristics factory(options); - return make_shared(options, factory.find_functions()); + virtual shared_ptr create_component(const plugins::Options &opts, const utils::Context &) const override { + return make_shared( // TODO issue1082 use make_shared_from_arg_tuples + DiversePotentialHeuristics( + opts.get("num_samples"), + opts.get("max_num_heuristics"), + opts.get("max_potential"), + opts.get("lpsolver"), + opts.get>("transform"), + opts.get("random_seed"), + opts.get("verbosity") + ).find_functions(), + opts.get>("transform"), + opts.get("cache_estimates"), + opts.get("description"), + opts.get("verbosity") + ); } }; diff --git a/src/search/potentials/diverse_potential_heuristics.h b/src/search/potentials/diverse_potential_heuristics.h index 4069f9e8e5..eee8b9fd42 100644 --- a/src/search/potentials/diverse_potential_heuristics.h +++ b/src/search/potentials/diverse_potential_heuristics.h @@ -54,7 +54,15 @@ class DiversePotentialHeuristics { void cover_samples(SamplesToFunctionsMap &samples_to_functions); public: - explicit DiversePotentialHeuristics(const plugins::Options &opts); + DiversePotentialHeuristics( + int num_samples, + int max_num_heuristics, + double max_potential, + lp::LPSolverType lpsolver, + const std::shared_ptr &transform, + int random_seed, + utils::Verbosity verbosity + ); ~DiversePotentialHeuristics() = default; // Sample states, then cover them. diff --git a/src/search/potentials/potential_heuristic.cc b/src/search/potentials/potential_heuristic.cc index 4c4377214d..1a05aee796 100644 --- a/src/search/potentials/potential_heuristic.cc +++ b/src/search/potentials/potential_heuristic.cc @@ -8,13 +8,16 @@ using namespace std; namespace potentials { PotentialHeuristic::PotentialHeuristic( - const plugins::Options &opts, unique_ptr function) - : Heuristic(opts), + unique_ptr function, + const shared_ptr &transform, + bool cache_estimates, + const string &description, + utils::Verbosity verbosity +) + : Heuristic(transform, cache_estimates, description, verbosity), function(move(function)) { } -PotentialHeuristic::~PotentialHeuristic() { -} int PotentialHeuristic::compute_heuristic(const State &ancestor_state) { State state = convert_ancestor_state(ancestor_state); diff --git a/src/search/potentials/potential_heuristic.h b/src/search/potentials/potential_heuristic.h index 387788a44b..d6a6758cbb 100644 --- a/src/search/potentials/potential_heuristic.h +++ b/src/search/potentials/potential_heuristic.h @@ -19,9 +19,13 @@ class PotentialHeuristic : public Heuristic { public: explicit PotentialHeuristic( - const plugins::Options &opts, std::unique_ptr function); - // Define in .cc file to avoid include in header. - ~PotentialHeuristic(); + std::unique_ptr function, + const std::shared_ptr &transform, + bool cache_estimates, + const std::string &description, + utils::Verbosity verbosity + ); + }; } diff --git a/src/search/potentials/potential_max_heuristic.cc b/src/search/potentials/potential_max_heuristic.cc index 912f4fab11..b5cd5b136f 100644 --- a/src/search/potentials/potential_max_heuristic.cc +++ b/src/search/potentials/potential_max_heuristic.cc @@ -8,9 +8,12 @@ using namespace std; namespace potentials { PotentialMaxHeuristic::PotentialMaxHeuristic( - const plugins::Options &opts, - vector> &&functions) - : Heuristic(opts), + vector> &&functions, + const shared_ptr &transform, + bool cache_estimates, + const string &description, + utils::Verbosity verbosity) + : Heuristic(transform, cache_estimates, description, verbosity), functions(move(functions)) { } diff --git a/src/search/potentials/potential_max_heuristic.h b/src/search/potentials/potential_max_heuristic.h index 66c5737328..cae6c44614 100644 --- a/src/search/potentials/potential_max_heuristic.h +++ b/src/search/potentials/potential_max_heuristic.h @@ -19,10 +19,12 @@ class PotentialMaxHeuristic : public Heuristic { virtual int compute_heuristic(const State &ancestor_state) override; public: - explicit PotentialMaxHeuristic( - const plugins::Options &opts, - std::vector> &&functions); - ~PotentialMaxHeuristic() = default; + PotentialMaxHeuristic( + std::vector> &&functions, + const std::shared_ptr &transform, + bool cache_estimates, + const std::string &description, + utils::Verbosity verbosity); }; } diff --git a/src/search/potentials/potential_optimizer.cc b/src/search/potentials/potential_optimizer.cc index e6eb2e2df0..c866160e2e 100644 --- a/src/search/potentials/potential_optimizer.cc +++ b/src/search/potentials/potential_optimizer.cc @@ -19,12 +19,16 @@ static int get_undefined_value(VariableProxy var) { return var.get_domain_size(); } -PotentialOptimizer::PotentialOptimizer(const plugins::Options &opts) - : task(opts.get>("transform")), - task_proxy(*task), - lp_solver(opts.get("lpsolver")), - max_potential(opts.get("max_potential")), - num_lp_vars(0) { +PotentialOptimizer::PotentialOptimizer( + const shared_ptr &transform, + lp::LPSolverType lpsolver, + double max_potential + ) + : task(transform), + task_proxy(*task), + lp_solver(lpsolver), + max_potential(max_potential), + num_lp_vars(0) { task_properties::verify_no_axioms(task_proxy); task_properties::verify_no_conditional_effects(task_proxy); initialize(); diff --git a/src/search/potentials/potential_optimizer.h b/src/search/potentials/potential_optimizer.h index fe5dca83d5..49234fe6a5 100644 --- a/src/search/potentials/potential_optimizer.h +++ b/src/search/potentials/potential_optimizer.h @@ -57,7 +57,11 @@ class PotentialOptimizer { void extract_lp_solution(); public: - explicit PotentialOptimizer(const plugins::Options &opts); + PotentialOptimizer( + const std::shared_ptr &transform, + lp::LPSolverType lpsolver, + double max_potential + ); ~PotentialOptimizer() = default; std::shared_ptr get_task() const; diff --git a/src/search/potentials/sample_based_potential_heuristics.cc b/src/search/potentials/sample_based_potential_heuristics.cc index 869562f049..72381b7fe7 100644 --- a/src/search/potentials/sample_based_potential_heuristics.cc +++ b/src/search/potentials/sample_based_potential_heuristics.cc @@ -41,12 +41,20 @@ static void optimize_for_samples( sets of samples. */ static vector> create_sample_based_potential_functions( - const plugins::Options &opts) { + int num_samples, + int num_heuristics, + double max_potential, + lp::LPSolverType lpsolver, + const shared_ptr &transform, + int random_seed + ) { vector> functions; - PotentialOptimizer optimizer(opts); - shared_ptr rng(utils::parse_rng_from_options(opts)); - for (int i = 0; i < opts.get("num_heuristics"); ++i) { - optimize_for_samples(optimizer, opts.get("num_samples"), *rng); + PotentialOptimizer optimizer(transform, + lpsolver, + max_potential); + shared_ptr rng(utils::get_rng(random_seed)); + for (int i = 0; i < num_heuristics; ++i) { + optimize_for_samples(optimizer, num_samples, *rng); functions.push_back(optimizer.get_potential_function()); } return functions; @@ -74,9 +82,21 @@ class SampleBasedPotentialMaxHeuristicFeature : public plugins::TypedFeature create_component(const plugins::Options &options, const utils::Context &) const override { - return make_shared( - options, create_sample_based_potential_functions(options)); + virtual shared_ptr create_component(const plugins::Options &opts, const utils::Context &) const override { + return make_shared( // TODO issue1082 use make_shared_from_arg_tuples + create_sample_based_potential_functions( + opts.get("num_samples"), + opts.get("num_heuristics"), + opts.get("max_potential"), + opts.get("lpsolver"), + opts.get>("transform"), + opts.get("random_seed") + ), + opts.get>("transform"), + opts.get("cache_estimates"), + opts.get("description"), + opts.get("verbosity") + ); } }; diff --git a/src/search/potentials/single_potential_heuristics.cc b/src/search/potentials/single_potential_heuristics.cc index 9f6a348ade..ee7ca13aec 100644 --- a/src/search/potentials/single_potential_heuristics.cc +++ b/src/search/potentials/single_potential_heuristics.cc @@ -15,9 +15,15 @@ enum class OptimizeFor { }; static unique_ptr create_potential_function( - const plugins::Options &opts, OptimizeFor opt_func) { - PotentialOptimizer optimizer(opts); - const AbstractTask &task = *opts.get>("transform"); + const shared_ptr &transform, + lp::LPSolverType lpsolver, + double max_potential, + OptimizeFor opt_func) { + PotentialOptimizer optimizer( + transform, + lpsolver, + max_potential); + const AbstractTask &task = *transform; TaskProxy task_proxy(task); switch (opt_func) { case OptimizeFor::INITIAL_STATE: @@ -42,8 +48,18 @@ class InitialStatePotentialHeuristicFeature : public plugins::TypedFeature create_component(const plugins::Options &options, const utils::Context &) const override { - return make_shared(options, create_potential_function(options, OptimizeFor::INITIAL_STATE)); + virtual shared_ptr create_component(const plugins::Options &opts, const utils::Context &) const override { + return make_shared( + create_potential_function( + opts.get>("transform"), + opts.get("lpsolver"), + opts.get("max_potential"), + OptimizeFor::INITIAL_STATE), + opts.get>("transform"), + opts.get("cache_estimates"), + opts.get("description"), + opts.get("verbosity") + ); } }; @@ -59,8 +75,17 @@ class AllStatesPotentialHeuristicFeature : public plugins::TypedFeature create_component(const plugins::Options &options, const utils::Context &) const override { - return make_shared(options, create_potential_function(options, OptimizeFor::ALL_STATES)); + virtual shared_ptr create_component(const plugins::Options &opts, const utils::Context &) const override { + return make_shared(create_potential_function( + opts.get>("transform"), + opts.get("lpsolver"), + opts.get("max_potential"), + OptimizeFor::ALL_STATES), + opts.get>("transform"), + opts.get("cache_estimates"), + opts.get("description"), + opts.get("verbosity") + ); } }; diff --git a/src/search/potentials/util.cc b/src/search/potentials/util.cc index 960fd594d3..6159d5df2f 100644 --- a/src/search/potentials/util.cc +++ b/src/search/potentials/util.cc @@ -65,4 +65,13 @@ void add_admissible_potentials_options_to_feature(plugins::Feature &feature, con lp::add_lp_solver_option_to_feature(feature); add_heuristic_options_to_feature(feature, description); } + + +tuple, bool, string, utils::Verbosity> get_admissible_potential_arguments_from_options(const plugins::Options &opts) { + return tuple_cat( + make_tuple(opts.get("max_potential")), + lp::get_lp_solver_arguments_from_options(opts), + get_heuristic_arguments_from_options(opts) + ); +} } diff --git a/src/search/potentials/util.h b/src/search/potentials/util.h index 656301a320..04133d2fdf 100644 --- a/src/search/potentials/util.h +++ b/src/search/potentials/util.h @@ -4,11 +4,16 @@ #include #include #include +#include "../lp/lp_solver.h" +#include "../utils/logging.h" + +class AbstractTask; class State; namespace plugins { class Feature; +class Options; } namespace utils { @@ -25,6 +30,7 @@ std::vector sample_without_dead_end_detection( std::string get_admissible_potentials_reference(); void add_admissible_potentials_options_to_feature(plugins::Feature &feature, const std::string &description); +std::tuple, bool, std::string, utils::Verbosity>get_admissible_potential_arguments_from_options(const plugins::Options &opts); } #endif