Skip to content

Commit

Permalink
added alternating option for bidirectional search
Browse files Browse the repository at this point in the history
  • Loading branch information
speckdavid committed Jan 30, 2024
1 parent f6661b8 commit 6000975
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ void SymbolicUniformCostSearch::initialize() {

if (fw && bw) {
search = unique_ptr<BidirectionalSearch>(new BidirectionalSearch(
this, searchParams, move(fw_search), move(bw_search)));
this, searchParams, move(fw_search), move(bw_search), alternating));
} else {
search.reset(fw ? fw_search.release() : bw_search.release());
}
}

SymbolicUniformCostSearch::SymbolicUniformCostSearch(
const options::Options &opts, bool fw, bool bw)
: SymbolicSearch(opts), fw(fw), bw(bw) {}
const options::Options &opts, bool fw, bool bw, bool alternating)
: SymbolicSearch(opts), fw(fw), bw(bw), alternating(alternating) {}

void SymbolicUniformCostSearch::new_solution(const SymSolutionCut &sol) {
if (!solution_registry->found_all_plans() && sol.get_f() < upper_bound) {
Expand Down Expand Up @@ -104,12 +104,14 @@ _parse_bidirectional_ucs(OptionParser &parser) {
symbolic::SymbolicSearch::add_options_to_parser(parser);
parser.add_option<shared_ptr<symbolic::PlanSelector>>(
"plan_selection", "plan selection strategy", "top_k(num_plans=1)");
parser.add_option<bool>("alternating", "alternating", "false");
Options opts = parser.parse();

shared_ptr<symbolic::SymbolicSearch> engine = nullptr;
if (!parser.dry_run()) {
bool alternating = opts.get<bool>("alternating");
engine =
make_shared<symbolic::SymbolicUniformCostSearch>(opts, true, true);
make_shared<symbolic::SymbolicUniformCostSearch>(opts, true, true, alternating);
utils::g_log << "Symbolic Bidirectional Uniform Cost Search" << endl;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ class SymbolicUniformCostSearch : public SymbolicSearch {
protected:
bool fw;
bool bw;
bool alternating;

virtual void initialize() override;

virtual SearchStatus step() override {return SymbolicSearch::step();}

public:
SymbolicUniformCostSearch(const options::Options &opts, bool fw, bool bw);
SymbolicUniformCostSearch(const options::Options &opts, bool fw, bool bw, bool alternating = false);
virtual ~SymbolicUniformCostSearch() = default;

virtual void new_solution(const SymSolutionCut &sol) override;
Expand Down
38 changes: 26 additions & 12 deletions src/search/symbolic/searches/bidirectional_search.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ namespace symbolic {
BidirectionalSearch::BidirectionalSearch(SymbolicSearch *eng,
const SymParamsSearch &params,
shared_ptr<UniformCostSearch> _fw,
shared_ptr<UniformCostSearch> _bw)
: SymSearch(eng, params), fw(_fw), bw(_bw), cur_dir(nullptr) {
shared_ptr<UniformCostSearch> _bw,
bool alternating)
: SymSearch(eng, params), fw(_fw), bw(_bw), cur_dir(nullptr), alternating(alternating) {
assert(fw->getStateSpace() == bw->getStateSpace());
mgr = fw->getStateSpaceShared();
}
Expand All @@ -21,17 +22,30 @@ string BidirectionalSearch::get_last_dir() const {
}

UniformCostSearch *BidirectionalSearch::selectBestDirection() {
Estimation &fw_est = *fw->get_step_estimator();
Estimation &bw_est = *bw->get_step_estimator();
if (fw_est.get_failed() && bw_est.get_failed()) {
p.increase_bound();
bw_est.set_data(bw_est.get_time(), bw_est.get_nodes(), false);
return fw.get();
if (alternating) {
if (!cur_dir) {
cur_dir = fw;
} else {
if (cur_dir == fw) {
cur_dir = bw;
} else {
cur_dir = fw;
}
}
} else {
Estimation &fw_est = *fw->get_step_estimator();
Estimation &bw_est = *bw->get_step_estimator();
if (fw_est.get_failed() && bw_est.get_failed()) {
p.increase_bound();
bw_est.set_data(bw_est.get_time(), bw_est.get_nodes(), false);
return fw.get();
}
/*utils::g_log << "FWD: " << fw_est << endl;
utils::g_log << "BWD: " << bw_est << endl;
utils::g_log << ((bw_est < fw_est) ? "bw" : "fw") << endl;*/
cur_dir = (bw_est < fw_est) ? bw : fw;
}
/*utils::g_log << "FWD: " << fw_est << endl;
utils::g_log << "BWD: " << bw_est << endl;
utils::g_log << ((bw_est < fw_est) ? "bw" : "fw") << endl;*/
cur_dir = (bw_est < fw_est) ? bw : fw;

return cur_dir.get();
}

Expand Down
4 changes: 3 additions & 1 deletion src/search/symbolic/searches/bidirectional_search.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ class BidirectionalSearch : public SymSearch {
private:
std::shared_ptr<UniformCostSearch> fw, bw;
std::shared_ptr<UniformCostSearch> cur_dir;
bool alternating;

// Returns the best direction to search the bd exp
UniformCostSearch *selectBestDirection();

public:
BidirectionalSearch(SymbolicSearch *eng, const SymParamsSearch &params,
std::shared_ptr<UniformCostSearch> fw,
std::shared_ptr<UniformCostSearch> bw);
std::shared_ptr<UniformCostSearch> bw,
bool alternating = false);

virtual bool finished() const override;

Expand Down

0 comments on commit 6000975

Please sign in to comment.