Skip to content

Commit

Permalink
Pass arguments to search algorithms directly.
Browse files Browse the repository at this point in the history
  • Loading branch information
jendrikseipp committed Nov 29, 2024
1 parent 67270ad commit 851ed3f
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 82 deletions.
5 changes: 3 additions & 2 deletions misc/tests/test-parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@
"initial_state_potential",
"all_states_potential",
"diverse_potentials",
"dump_reachable_search_space",
"brfs",
]

PERMANENT_EXCEPTIONS = [
"adapt_costs",
"brfs",
"canonical_heuristic",
"dump_reachable_search_space",
"gzocp",
"idastar",
"pho",
"ocp",
"scp",
Expand Down
30 changes: 19 additions & 11 deletions src/search/search_algorithms/breadth_first_search.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
using namespace std;

namespace breadth_first_search {
BreadthFirstSearch::BreadthFirstSearch(const plugins::Options &opts)
: SearchAlgorithm(opts),
single_plan(opts.get<bool>("single_plan")),
write_plan(opts.get<bool>("write_plan")),
BreadthFirstSearch::BreadthFirstSearch(
bool single_plan, bool write_plan, const shared_ptr<PruningMethod> &pruning,
const string &description, utils::Verbosity verbosity)
: SearchAlgorithm(
ONE, numeric_limits<int>::max(),
numeric_limits<double>::infinity(), description, verbosity),
single_plan(single_plan),
write_plan(write_plan),
last_plan_cost(-1),
pruning_method(opts.get<shared_ptr<PruningMethod>>("pruning")) {
assert(cost_type == ONE);
pruning_method(pruning) {
}

void BreadthFirstSearch::initialize() {
Expand Down Expand Up @@ -136,16 +139,21 @@ class BreadthFirstSearchFeature
"each state and thereby influence the number and order of successor states "
"that are considered.",
"null()");
add_option<string>(
"description",
"description used to identify search algorithm in logs",
"\"brfs\"");
utils::add_log_options_to_feature(*this);
}

virtual shared_ptr<BreadthFirstSearch> create_component(
const plugins::Options &options, const utils::Context &) const override {
plugins::Options opts = options;
opts.set<OperatorCost>("cost_type", ONE);
opts.set<int>("bound", numeric_limits<int>::max());
opts.set<double>("max_time", numeric_limits<double>::infinity());
return make_shared<BreadthFirstSearch>(opts);
return plugins::make_shared_from_arg_tuples<BreadthFirstSearch>(
options.get<bool>("single_plan"),
options.get<bool>("write_plan"),
options.get<shared_ptr<PruningMethod>>("pruning"),
options.get<string>("description"),
utils::get_log_arguments_from_options(options));
}
};

Expand Down
8 changes: 3 additions & 5 deletions src/search/search_algorithms/breadth_first_search.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@

class PruningMethod;

namespace options {
class Options;
}

namespace breadth_first_search {
struct Parent {
StateID state_id;
Expand Down Expand Up @@ -47,7 +43,9 @@ class BreadthFirstSearch : public SearchAlgorithm {
virtual SearchStatus step() override;

public:
explicit BreadthFirstSearch(const plugins::Options &opts);
BreadthFirstSearch(
bool single_plan, bool write_plan, const std::shared_ptr<PruningMethod> &pruning,
const std::string &description, utils::Verbosity verbosity);

virtual void save_plan_if_necessary() override;

Expand Down
17 changes: 12 additions & 5 deletions src/search/search_algorithms/depth_first_search.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include "../task_utils/task_properties.h"

#include "../utils/logging.h"
#include "../utils/memory.h"
#include "../utils/timer.h"

#include <cassert>
#include <cstdlib>
Expand All @@ -17,9 +15,11 @@ namespace depth_first_search {
static const int INF = numeric_limits<int>::max();


DepthFirstSearch::DepthFirstSearch(const plugins::Options &opts)
: SearchAlgorithm(opts),
single_plan(opts.get<bool>("single_plan")),
DepthFirstSearch::DepthFirstSearch(
bool single_plan, OperatorCost cost_type, int bound, double max_time,
const string &description, utils::Verbosity verbosity)
: SearchAlgorithm(cost_type, bound, max_time, description, verbosity),
single_plan(single_plan),
max_depth(0),
cheapest_plan_cost(INF) {
if (max_time != numeric_limits<double>::infinity()) {
Expand Down Expand Up @@ -125,6 +125,13 @@ class DepthFirstSearchFeature
"false");
add_search_algorithm_options_to_feature(*this, "dfs");
}

virtual shared_ptr<DepthFirstSearch> create_component(
const plugins::Options &options, const utils::Context &) const override {
return plugins::make_shared_from_arg_tuples<DepthFirstSearch>(
options.get<bool>("single_plan"),
get_search_algorithm_arguments_from_options(options));
}
};

static plugins::FeaturePlugin<DepthFirstSearchFeature> _plugin;
Expand Down
12 changes: 3 additions & 9 deletions src/search/search_algorithms/depth_first_search.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@

#include "../search_algorithm.h"

#include <memory>

class Evaluator;

namespace options {
class Options;
}

namespace depth_first_search {
struct DFSNode {
State state;
Expand Down Expand Up @@ -39,7 +31,9 @@ class DepthFirstSearch : public SearchAlgorithm {
virtual SearchStatus step() override;

public:
explicit DepthFirstSearch(const plugins::Options &opts);
DepthFirstSearch(
bool single_plan, OperatorCost cost_type, int bound, double max_time,
const std::string &description, utils::Verbosity verbosity);

virtual void save_plan_if_necessary() override;

Expand Down
14 changes: 5 additions & 9 deletions src/search/search_algorithms/exhaustive_search.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ static vector<vector<int>> construct_and_dump_fact_mapping(
return mapping;
}

ExhaustiveSearch::ExhaustiveSearch(const plugins::Options &opts)
: SearchAlgorithm(opts) {
assert(cost_type == ONE);
ExhaustiveSearch::ExhaustiveSearch()
: SearchAlgorithm(
ONE, numeric_limits<int>::max(), numeric_limits<double>::infinity(),
"dump_reachable_search_space", utils::Verbosity::NORMAL) {
}

void ExhaustiveSearch::initialize() {
Expand Down Expand Up @@ -114,12 +115,7 @@ class ExhaustiveSearchFeature

virtual shared_ptr<ExhaustiveSearch> create_component(
const plugins::Options &, const utils::Context &) const override {
plugins::Options opts;
opts.set<OperatorCost>("cost_type", ONE);
opts.set<int>("bound", numeric_limits<int>::max());
opts.set<double>("max_time", numeric_limits<double>::infinity());
opts.set<utils::Verbosity>("verbosity", utils::Verbosity::NORMAL);
return make_shared<ExhaustiveSearch>(opts);
return plugins::make_shared_from_arg_tuples<ExhaustiveSearch>();
}
};

Expand Down
6 changes: 1 addition & 5 deletions src/search/search_algorithms/exhaustive_search.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@

#include "../search_algorithm.h"

namespace options {
class Options;
}

namespace exhaustive_search {
class ExhaustiveSearch : public SearchAlgorithm {
int current_state_id;
Expand All @@ -19,7 +15,7 @@ class ExhaustiveSearch : public SearchAlgorithm {
virtual SearchStatus step() override;

public:
explicit ExhaustiveSearch(const plugins::Options &opts);
ExhaustiveSearch();

virtual void print_statistics() const override;
};
Expand Down
24 changes: 18 additions & 6 deletions src/search/search_algorithms/idastar_search.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,15 @@ void FifoCache::clear() {
queue<State>().swap(states);
}

IDAstarSearch::IDAstarSearch(const plugins::Options &opts)
: SearchAlgorithm(opts),
h_evaluator(opts.get<shared_ptr<Evaluator>>("eval")),
single_plan(opts.get<bool>("single_plan")),
IDAstarSearch::IDAstarSearch(
const shared_ptr<Evaluator> &h_evaluator, int initial_f_limit, int cache_size,
bool single_plan, OperatorCost cost_type, int bound, double max_time,
const string &description, utils::Verbosity verbosity)
: SearchAlgorithm(cost_type, bound, max_time, description, verbosity),
h_evaluator(h_evaluator),
single_plan(single_plan),
iteration(0),
f_limit(opts.get<int>("initial_f_limit")),
f_limit(initial_f_limit),
cheapest_plan_cost(numeric_limits<int>::max()),
num_cache_hits(0),
num_expansions(0),
Expand All @@ -75,7 +78,6 @@ IDAstarSearch::IDAstarSearch(const plugins::Options &opts)
cerr << "Error: set cache_estimates=false for IDA* heuristics." << endl;
utils::exit_with(utils::ExitCode::SEARCH_INPUT_ERROR);
}
int cache_size = opts.get<int>("cache_size");
if (cache_size > 0) {
cache = utils::make_unique_ptr<FifoCache>(cache_size);
}
Expand Down Expand Up @@ -201,6 +203,16 @@ class IDAstarSearchFeature
"true");
add_search_algorithm_options_to_feature(*this, "idastar");
}

virtual shared_ptr<IDAstarSearch> create_component(
const plugins::Options &options, const utils::Context &) const override {
return plugins::make_shared_from_arg_tuples<IDAstarSearch>(
options.get<shared_ptr<Evaluator>>("eval"),
options.get<int>("initial_f_limit"),
options.get<int>("cache_size"),
options.get<bool>("single_plan"),
get_search_algorithm_arguments_from_options(options));
}
};

static plugins::FeaturePlugin<IDAstarSearchFeature> _plugin;
Expand Down
10 changes: 4 additions & 6 deletions src/search/search_algorithms/idastar_search.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@

#include <memory>
#include <queue>
#include <vector>

class Evaluator;

namespace options {
class Options;
}

namespace idastar_search {
struct IDAstarNode {
State state;
Expand Down Expand Up @@ -66,7 +61,10 @@ class IDAstarSearch : public SearchAlgorithm {
virtual SearchStatus step() override;

public:
explicit IDAstarSearch(const plugins::Options &opts);
IDAstarSearch(
const std::shared_ptr<Evaluator> &h_evaluator, int initial_f_limit,
int cache_size, bool single_plan, OperatorCost cost_type, int bound,
double max_time, const std::string &description, utils::Verbosity verbosity);

void save_plan_if_necessary() override;

Expand Down
15 changes: 12 additions & 3 deletions src/search/search_algorithms/iterative_deepening_search.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
using namespace std;

namespace iterative_deepening_search {
IterativeDeepeningSearch::IterativeDeepeningSearch(const plugins::Options &opts)
: SearchAlgorithm(opts),
single_plan(opts.get<bool>("single_plan")),
IterativeDeepeningSearch::IterativeDeepeningSearch(
bool single_plan, OperatorCost cost_type, int bound, double max_time,
const string &description, utils::Verbosity verbosity)
: SearchAlgorithm(cost_type, bound, max_time, description, verbosity),
single_plan(single_plan),
sg(task_proxy),
last_plan_cost(-1) {
if (!task_properties::is_unit_cost(task_proxy)) {
Expand Down Expand Up @@ -101,6 +103,13 @@ class IterativeDeepeningSearchFeature
"true");
add_search_algorithm_options_to_feature(*this, "ids");
}

virtual shared_ptr<IterativeDeepeningSearch> create_component(
const plugins::Options &options, const utils::Context &) const override {
return plugins::make_shared_from_arg_tuples<IterativeDeepeningSearch>(
options.get<bool>("single_plan"),
get_search_algorithm_arguments_from_options(options));
}
};

static plugins::FeaturePlugin<IterativeDeepeningSearchFeature> _plugin;
Expand Down
13 changes: 3 additions & 10 deletions src/search/search_algorithms/iterative_deepening_search.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@

#include "../task_utils/incremental_successor_generator.h"

#include <memory>
#include <vector>

class Evaluator;

namespace options {
class Options;
}

namespace iterative_deepening_search {
class IterativeDeepeningSearch : public SearchAlgorithm {
const bool single_plan;
Expand All @@ -29,7 +20,9 @@ class IterativeDeepeningSearch : public SearchAlgorithm {
virtual SearchStatus step() override;

public:
explicit IterativeDeepeningSearch(const plugins::Options &opts);
IterativeDeepeningSearch(
bool single_plan, OperatorCost cost_type, int bound, double max_time,
const std::string &description, utils::Verbosity verbosity);

void save_plan_if_necessary() override;

Expand Down
17 changes: 13 additions & 4 deletions src/search/search_algorithms/iterative_width_search.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
using namespace std;

namespace iterative_width_search {
IterativeWidthSearch::IterativeWidthSearch(const plugins::Options &opts)
: SearchAlgorithm(opts),
width(opts.get<int>("width")),
debug(opts.get<utils::Verbosity>("verbosity") == utils::Verbosity::DEBUG),
IterativeWidthSearch::IterativeWidthSearch(
int width, OperatorCost cost_type, int bound, double max_time,
const string &description, utils::Verbosity verbosity)
: SearchAlgorithm(cost_type, bound, max_time, description, verbosity),
width(width),
debug(verbosity == utils::Verbosity::DEBUG),
novelty_table(task_proxy, width) {
utils::g_log << "Setting up iterative width search." << endl;
}
Expand Down Expand Up @@ -100,6 +102,13 @@ class IterativeWidthSearchFeature
"width", "maximum conjunction size", "2", plugins::Bounds("1", "2"));
add_search_algorithm_options_to_feature(*this, "iw");
}

virtual shared_ptr<IterativeWidthSearch> create_component(
const plugins::Options &options, const utils::Context &) const override {
return plugins::make_shared_from_arg_tuples<IterativeWidthSearch>(
options.get<int>("width"),
get_search_algorithm_arguments_from_options(options));
}
};

static plugins::FeaturePlugin<IterativeWidthSearchFeature> _plugin;
Expand Down
10 changes: 3 additions & 7 deletions src/search/search_algorithms/iterative_width_search.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
#include "../novelty/novelty_table.h"

#include <deque>
#include <memory>
#include <vector>

namespace options {
class Options;
}

// TODO: rename to IteratedWidthSearch.
namespace iterative_width_search {
Expand All @@ -30,7 +24,9 @@ class IterativeWidthSearch : public SearchAlgorithm {
virtual SearchStatus step() override;

public:
explicit IterativeWidthSearch(const plugins::Options &opts);
IterativeWidthSearch(
int width, OperatorCost cost_type, int bound, double max_time,
const std::string &description, utils::Verbosity verbosity);

virtual void print_statistics() const override;

Expand Down

0 comments on commit 851ed3f

Please sign in to comment.