Skip to content

Commit

Permalink
remove options object from LandmarHeuristic.
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonDold committed Mar 6, 2024
1 parent 4e99bb6 commit 503d5db
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 63 deletions.
17 changes: 5 additions & 12 deletions src/search/landmarks/landmark_cost_partitioning_heuristic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ using namespace std;

namespace landmarks {
LandmarkCostPartitioningHeuristic::LandmarkCostPartitioningHeuristic(
const plugins::Options &lm_factory_option,
const shared_ptr<LandmarkFactory> &lm_factory,
bool use_preferred_operators,
bool prog_goal,
bool prog_gn,
Expand All @@ -33,22 +33,19 @@ LandmarkCostPartitioningHeuristic::LandmarkCostPartitioningHeuristic(
if (log.is_at_least_normal()) {
log << "Initializing landmark cost partitioning heuristic..." << endl;
}
check_unsupported_features(lm_factory_option);
initialize(lm_factory_option, prog_goal, prog_gn, prog_r);
check_unsupported_features(lm_factory);
initialize(lm_factory, prog_goal, prog_gn, prog_r);
set_cost_partitioning_algorithm(cost_partitioning, lpsolver, alm);
}

void LandmarkCostPartitioningHeuristic::check_unsupported_features(
const plugins::Options &lm_factory_option) {
shared_ptr<LandmarkFactory> lm_graph_factory =
lm_factory_option.get<shared_ptr<LandmarkFactory>>("lm_factory");

const shared_ptr<LandmarkFactory> &lm_factory) {
if (task_properties::has_axioms(task_proxy)) {
cerr << "Cost partitioning does not support axioms." << endl;
utils::exit_with(utils::ExitCode::SEARCH_UNSUPPORTED);
}

if (!lm_graph_factory->supports_conditional_effects()
if (!lm_factory->supports_conditional_effects()
&& task_properties::has_conditional_effects(task_proxy)) {
cerr << "Conditional effects not supported by the landmark "
<< "generation method." << endl;
Expand Down Expand Up @@ -177,11 +174,7 @@ class LandmarkCostPartitioningHeuristicFeature : public plugins::TypedFeature<Ev

virtual shared_ptr<LandmarkCostPartitioningHeuristic> create_component(
const plugins::Options &opts, const utils::Context &) const override {
plugins::Options lm_factory_options;
lm_factory_options.set<shared_ptr<LandmarkFactory>>(
"lm_factory", opts.get<shared_ptr<LandmarkFactory>>("lm_factory"));
return plugins::make_shared_from_arg_tuples<LandmarkCostPartitioningHeuristic>(
lm_factory_options,
get_landmark_heuristic_arguments_from_options(opts),
opts.get<CostPartitioningMethod>("cost_partitioning"),
opts.get<bool>("alm"),
Expand Down
13 changes: 2 additions & 11 deletions src/search/landmarks/landmark_cost_partitioning_heuristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,15 @@ enum class CostPartitioningMethod {
class LandmarkCostPartitioningHeuristic : public LandmarkHeuristic {
std::unique_ptr<CostPartitioningAlgorithm> cost_partitioning_algorithm;

void check_unsupported_features(const plugins::Options &lm_factory_option); // TODO issue1082 this needs Options to construct the lm_factory later.
void check_unsupported_features(const std::shared_ptr<LandmarkFactory> &lm_factory);
void set_cost_partitioning_algorithm(CostPartitioningMethod cost_partitioning,
lp::LPSolverType lpsolver,
bool alm);

int get_heuristic_value(const State &ancestor_state) override;
public:
/*
TODO: issue1082 aimed to remove the options object from constructors.
This is not possible here because we need to wait with initializing the
landmark factory until the task is given (e.g., cost transformation).
Therefore, we can only extract the landmark factory from the options
after this happened, so we allow the landmark heuristics to keep a
(small) options object around for that purpose.
This should be handled by issue559 eventually.
*/
LandmarkCostPartitioningHeuristic(
const plugins::Options &lm_factory_option,
const std::shared_ptr<LandmarkFactory> &lm_factory,
bool use_preferred_operators,
bool prog_goal,
bool prog_gn,
Expand Down
32 changes: 13 additions & 19 deletions src/search/landmarks/landmark_heuristic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ LandmarkHeuristic::LandmarkHeuristic(
}

void LandmarkHeuristic::initialize(
const plugins::Options &lm_factory_option,
const shared_ptr<LandmarkFactory> &lm_factory,
bool prog_goal,
bool prog_gn,
bool prog_r) {
Expand All @@ -42,7 +42,7 @@ void LandmarkHeuristic::initialize(
utils::exit_with(utils::ExitCode::SEARCH_UNSUPPORTED);
}

compute_landmark_graph(lm_factory_option);
compute_landmark_graph(lm_factory);
lm_status_manager = utils::make_unique_ptr<LandmarkStatusManager>(
*lm_graph,
prog_goal,
Expand Down Expand Up @@ -100,16 +100,14 @@ bool LandmarkHeuristic::depth_first_search_for_cycle_of_natural_orderings(
return false;
}

void LandmarkHeuristic::compute_landmark_graph(const plugins::Options &lm_factory_option) {
void LandmarkHeuristic::compute_landmark_graph(const shared_ptr<LandmarkFactory> &lm_factory) {
utils::Timer lm_graph_timer;
if (log.is_at_least_normal()) {
log << "Generating landmark graph..." << endl;
}

shared_ptr<LandmarkFactory> lm_graph_factory =
lm_factory_option.get<shared_ptr<LandmarkFactory>>("lm_factory");
lm_graph = lm_graph_factory->compute_lm_graph(task);
assert(lm_graph_factory->achievers_are_calculated());
lm_graph = lm_factory->compute_lm_graph(task);
assert(lm_factory->achievers_are_calculated());

if (log.is_at_least_normal()) {
log << "Landmark graph generation time: " << lm_graph_timer << endl;
Expand Down Expand Up @@ -237,20 +235,16 @@ void add_landmark_heuristic_options_to_feature(plugins::Feature &feature,
"yes (if enabled; see ``pref`` option)");
}

tuple<bool, bool, bool, bool, shared_ptr<AbstractTask>, bool, string, utils::Verbosity>
get_landmark_heuristic_arguments_from_options(const plugins::Options &options) {
tuple<shared_ptr<LandmarkFactory>, bool, bool, bool, bool, shared_ptr<AbstractTask>, bool, string, utils::Verbosity>
get_landmark_heuristic_arguments_from_options(const plugins::Options &opts) {
return tuple_cat(
make_tuple(
options.get<bool>("pref"),
/*
TODO: add_landmark_heuristic_options_to_feature also adds "lm_factory".
Here we do not extract it to put it in the argument tuple because we want the lm_factory to be
created later.
*/
options.get<bool>("prog_goal"),
options.get<bool>("prog_gn"),
options.get<bool>("prog_r")
opts.get<shared_ptr<LandmarkFactory>>("lm_factory"),
opts.get<bool>("pref"),
opts.get<bool>("prog_goal"),
opts.get<bool>("prog_gn"),
opts.get<bool>("prog_r")
),
get_heuristic_arguments_from_options(options));
get_heuristic_arguments_from_options(opts));
}
}
8 changes: 3 additions & 5 deletions src/search/landmarks/landmark_heuristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ class LandmarkHeuristic : public Heuristic {
std::unique_ptr<LandmarkStatusManager> lm_status_manager;
std::unique_ptr<successor_generator::SuccessorGenerator> successor_generator;

// TODO this needs lm_factory_options to construct the lm_factory later.
// This should be handled by issue559 eventually.
void initialize(const plugins::Options &lm_factory_option, bool prog_goal, bool prog_gn, bool prog_r);
void compute_landmark_graph(const plugins::Options &lm_factory_option);
void initialize(const std::shared_ptr<LandmarkFactory> &lm_factory, bool prog_goal, bool prog_gn, bool prog_r);
void compute_landmark_graph(const std::shared_ptr<LandmarkFactory> &lm_factory);

virtual int get_heuristic_value(const State &ancestor_state) = 0;

Expand All @@ -61,7 +59,7 @@ class LandmarkHeuristic : public Heuristic {

extern void add_landmark_heuristic_options_to_feature(
plugins::Feature &feature, const std::string &description);
extern std::tuple<bool, bool, bool, bool, std::shared_ptr<AbstractTask>, bool, std::string, utils::Verbosity> get_landmark_heuristic_arguments_from_options(
extern std::tuple<std::shared_ptr<LandmarkFactory>, bool, bool, bool, bool, std::shared_ptr<AbstractTask>, bool, std::string, utils::Verbosity> get_landmark_heuristic_arguments_from_options(
const plugins::Options &options);
}

Expand Down
10 changes: 3 additions & 7 deletions src/search/landmarks/landmark_sum_heuristic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static bool are_dead_ends_reliable(
}

LandmarkSumHeuristic::LandmarkSumHeuristic(
const plugins::Options &lm_factory_option,
const shared_ptr<LandmarkFactory> &lm_factory,
bool use_preferred_operators,
bool prog_goal,
bool prog_gn,
Expand All @@ -44,12 +44,12 @@ LandmarkSumHeuristic::LandmarkSumHeuristic(
description, verbosity),
dead_ends_reliable(
are_dead_ends_reliable(
lm_factory_option.get<shared_ptr<LandmarkFactory>>("lm_factory"),
lm_factory,
task_proxy)) {
if (log.is_at_least_normal()) {
log << "Initializing landmark sum heuristic..." << endl;
}
initialize(lm_factory_option, prog_goal, prog_gn, prog_r);
initialize(lm_factory, prog_goal, prog_gn, prog_r);
compute_landmark_costs();
}

Expand Down Expand Up @@ -202,11 +202,7 @@ class LandmarkSumHeuristicFeature : public plugins::TypedFeature<Evaluator, Land

virtual shared_ptr<LandmarkSumHeuristic> create_component(
const plugins::Options &opts, const utils::Context &) const override {
plugins::Options lm_factory_options;
lm_factory_options.set<shared_ptr<LandmarkFactory>>(
"lm_factory", opts.get<shared_ptr<LandmarkFactory>>("lm_factory"));
return plugins::make_shared_from_arg_tuples<LandmarkSumHeuristic>(
lm_factory_options,
get_landmark_heuristic_arguments_from_options(opts));
}
};
Expand Down
10 changes: 1 addition & 9 deletions src/search/landmarks/landmark_sum_heuristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,7 @@ class LandmarkSumHeuristic : public LandmarkHeuristic {

int get_heuristic_value(const State &ancestor_state) override;
public:
/*
TODO: issue1082 aimed to remove the options object from constructors.
This is not possible here because we need to wait with initializing the
landmark factory until the task is given (e.g., cost transformation).
Therefore, we can only extract the landmark factory from the options
after this happened, so we allow the landmark heuristics to keep a
(small) options object around for that purpose.
*/
LandmarkSumHeuristic(const plugins::Options &lm_factory_option,
LandmarkSumHeuristic(const std::shared_ptr<LandmarkFactory> &lm_factory,
bool use_preferred_operators,
bool prog_goal,
bool prog_gn,
Expand Down

0 comments on commit 503d5db

Please sign in to comment.