Skip to content

Commit

Permalink
update potentials
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonDold committed Feb 16, 2024
1 parent 1ce8c7a commit 1845863
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 46 deletions.
42 changes: 33 additions & 9 deletions src/search/potentials/diverse_potential_heuristics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,23 @@
using namespace std;

namespace potentials {
DiversePotentialHeuristics::DiversePotentialHeuristics(const plugins::Options &opts)
: optimizer(opts),
max_num_heuristics(opts.get<int>("max_num_heuristics")),
num_samples(opts.get<int>("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<AbstractTask> &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
Expand Down Expand Up @@ -166,9 +177,22 @@ class DiversePotentialMaxHeuristicFeature : public plugins::TypedFeature<Evaluat
utils::add_rng_options_to_feature(*this);
}

virtual shared_ptr<PotentialMaxHeuristic> create_component(const plugins::Options &options, const utils::Context &) const override {
DiversePotentialHeuristics factory(options);
return make_shared<PotentialMaxHeuristic>(options, factory.find_functions());
virtual shared_ptr<PotentialMaxHeuristic> create_component(const plugins::Options &opts, const utils::Context &) const override {
return make_shared<PotentialMaxHeuristic>( // TODO issue1082 use make_shared_from_arg_tuples
DiversePotentialHeuristics(
opts.get<int>("num_samples"),
opts.get<int>("max_num_heuristics"),
opts.get<double>("max_potential"),
opts.get<lp::LPSolverType>("lpsolver"),
opts.get<shared_ptr<AbstractTask>>("transform"),
opts.get<int>("random_seed"),
opts.get<utils::Verbosity>("verbosity")
).find_functions(),
opts.get<shared_ptr<AbstractTask>>("transform"),
opts.get<bool>("cache_estimates"),
opts.get<string>("description"),
opts.get<utils::Verbosity>("verbosity")
);
}
};

Expand Down
10 changes: 9 additions & 1 deletion src/search/potentials/diverse_potential_heuristics.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<AbstractTask> &transform,
int random_seed,
utils::Verbosity verbosity
);
~DiversePotentialHeuristics() = default;

// Sample states, then cover them.
Expand Down
11 changes: 7 additions & 4 deletions src/search/potentials/potential_heuristic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ using namespace std;

namespace potentials {
PotentialHeuristic::PotentialHeuristic(
const plugins::Options &opts, unique_ptr<PotentialFunction> function)
: Heuristic(opts),
unique_ptr<PotentialFunction> function,
const shared_ptr<AbstractTask> &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);
Expand Down
10 changes: 7 additions & 3 deletions src/search/potentials/potential_heuristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ class PotentialHeuristic : public Heuristic {

public:
explicit PotentialHeuristic(
const plugins::Options &opts, std::unique_ptr<PotentialFunction> function);
// Define in .cc file to avoid include in header.
~PotentialHeuristic();
std::unique_ptr<PotentialFunction> function,
const std::shared_ptr<AbstractTask> &transform,
bool cache_estimates,
const std::string &description,
utils::Verbosity verbosity
);

};
}

Expand Down
9 changes: 6 additions & 3 deletions src/search/potentials/potential_max_heuristic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ using namespace std;

namespace potentials {
PotentialMaxHeuristic::PotentialMaxHeuristic(
const plugins::Options &opts,
vector<unique_ptr<PotentialFunction>> &&functions)
: Heuristic(opts),
vector<unique_ptr<PotentialFunction>> &&functions,
const shared_ptr<AbstractTask> &transform,
bool cache_estimates,
const string &description,
utils::Verbosity verbosity)
: Heuristic(transform, cache_estimates, description, verbosity),
functions(move(functions)) {
}

Expand Down
10 changes: 6 additions & 4 deletions src/search/potentials/potential_max_heuristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::unique_ptr<PotentialFunction>> &&functions);
~PotentialMaxHeuristic() = default;
PotentialMaxHeuristic(
std::vector<std::unique_ptr<PotentialFunction>> &&functions,
const std::shared_ptr<AbstractTask> &transform,
bool cache_estimates,
const std::string &description,
utils::Verbosity verbosity);
};
}

Expand Down
16 changes: 10 additions & 6 deletions src/search/potentials/potential_optimizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<shared_ptr<AbstractTask>>("transform")),
task_proxy(*task),
lp_solver(opts.get<lp::LPSolverType>("lpsolver")),
max_potential(opts.get<double>("max_potential")),
num_lp_vars(0) {
PotentialOptimizer::PotentialOptimizer(
const shared_ptr<AbstractTask> &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();
Expand Down
6 changes: 5 additions & 1 deletion src/search/potentials/potential_optimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ class PotentialOptimizer {
void extract_lp_solution();

public:
explicit PotentialOptimizer(const plugins::Options &opts);
PotentialOptimizer(
const std::shared_ptr<AbstractTask> &transform,
lp::LPSolverType lpsolver,
double max_potential
);
~PotentialOptimizer() = default;

std::shared_ptr<AbstractTask> get_task() const;
Expand Down
36 changes: 28 additions & 8 deletions src/search/potentials/sample_based_potential_heuristics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,20 @@ static void optimize_for_samples(
sets of samples.
*/
static vector<unique_ptr<PotentialFunction>> create_sample_based_potential_functions(
const plugins::Options &opts) {
int num_samples,
int num_heuristics,
double max_potential,
lp::LPSolverType lpsolver,
const shared_ptr<AbstractTask> &transform,
int random_seed
) {
vector<unique_ptr<PotentialFunction>> functions;
PotentialOptimizer optimizer(opts);
shared_ptr<utils::RandomNumberGenerator> rng(utils::parse_rng_from_options(opts));
for (int i = 0; i < opts.get<int>("num_heuristics"); ++i) {
optimize_for_samples(optimizer, opts.get<int>("num_samples"), *rng);
PotentialOptimizer optimizer(transform,
lpsolver,
max_potential);
shared_ptr<utils::RandomNumberGenerator> 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;
Expand Down Expand Up @@ -74,9 +82,21 @@ class SampleBasedPotentialMaxHeuristicFeature : public plugins::TypedFeature<Eva
utils::add_rng_options_to_feature(*this);
}

virtual shared_ptr<PotentialMaxHeuristic> create_component(const plugins::Options &options, const utils::Context &) const override {
return make_shared<PotentialMaxHeuristic>(
options, create_sample_based_potential_functions(options));
virtual shared_ptr<PotentialMaxHeuristic> create_component(const plugins::Options &opts, const utils::Context &) const override {
return make_shared<PotentialMaxHeuristic>( // TODO issue1082 use make_shared_from_arg_tuples
create_sample_based_potential_functions(
opts.get<int>("num_samples"),
opts.get<int>("num_heuristics"),
opts.get<double>("max_potential"),
opts.get<lp::LPSolverType>("lpsolver"),
opts.get<shared_ptr<AbstractTask>>("transform"),
opts.get<int>("random_seed")
),
opts.get<shared_ptr<AbstractTask>>("transform"),
opts.get<bool>("cache_estimates"),
opts.get<string>("description"),
opts.get<utils::Verbosity>("verbosity")
);
}
};

Expand Down
39 changes: 32 additions & 7 deletions src/search/potentials/single_potential_heuristics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ enum class OptimizeFor {
};

static unique_ptr<PotentialFunction> create_potential_function(
const plugins::Options &opts, OptimizeFor opt_func) {
PotentialOptimizer optimizer(opts);
const AbstractTask &task = *opts.get<shared_ptr<AbstractTask>>("transform");
const shared_ptr<AbstractTask> &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:
Expand All @@ -42,8 +48,18 @@ class InitialStatePotentialHeuristicFeature : public plugins::TypedFeature<Evalu
add_admissible_potentials_options_to_feature(*this, "initial_state_potential");
}

virtual shared_ptr<PotentialHeuristic> create_component(const plugins::Options &options, const utils::Context &) const override {
return make_shared<PotentialHeuristic>(options, create_potential_function(options, OptimizeFor::INITIAL_STATE));
virtual shared_ptr<PotentialHeuristic> create_component(const plugins::Options &opts, const utils::Context &) const override {
return make_shared<PotentialHeuristic>(
create_potential_function(
opts.get<shared_ptr<AbstractTask>>("transform"),
opts.get<lp::LPSolverType>("lpsolver"),
opts.get<double>("max_potential"),
OptimizeFor::INITIAL_STATE),
opts.get<shared_ptr<AbstractTask>>("transform"),
opts.get<bool>("cache_estimates"),
opts.get<string>("description"),
opts.get<utils::Verbosity>("verbosity")
);
}
};

Expand All @@ -59,8 +75,17 @@ class AllStatesPotentialHeuristicFeature : public plugins::TypedFeature<Evaluato
add_admissible_potentials_options_to_feature(*this, "all_states_potential");
}

virtual shared_ptr<PotentialHeuristic> create_component(const plugins::Options &options, const utils::Context &) const override {
return make_shared<PotentialHeuristic>(options, create_potential_function(options, OptimizeFor::ALL_STATES));
virtual shared_ptr<PotentialHeuristic> create_component(const plugins::Options &opts, const utils::Context &) const override {
return make_shared<PotentialHeuristic>(create_potential_function(
opts.get<shared_ptr<AbstractTask>>("transform"),
opts.get<lp::LPSolverType>("lpsolver"),
opts.get<double>("max_potential"),
OptimizeFor::ALL_STATES),
opts.get<shared_ptr<AbstractTask>>("transform"),
opts.get<bool>("cache_estimates"),
opts.get<string>("description"),
opts.get<utils::Verbosity>("verbosity")
);
}
};

Expand Down
9 changes: 9 additions & 0 deletions src/search/potentials/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<double, lp::LPSolverType, shared_ptr<AbstractTask>, bool, string, utils::Verbosity> get_admissible_potential_arguments_from_options(const plugins::Options &opts) {
return tuple_cat(
make_tuple(opts.get<double>("max_potential")),
lp::get_lp_solver_arguments_from_options(opts),
get_heuristic_arguments_from_options(opts)
);
}
}
6 changes: 6 additions & 0 deletions src/search/potentials/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
#include <memory>
#include <string>
#include <vector>
#include "../lp/lp_solver.h"
#include "../utils/logging.h"


class AbstractTask;
class State;

namespace plugins {
class Feature;
class Options;
}

namespace utils {
Expand All @@ -25,6 +30,7 @@ std::vector<State> 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<double, lp::LPSolverType, std::shared_ptr<AbstractTask>, bool, std::string, utils::Verbosity>get_admissible_potential_arguments_from_options(const plugins::Options &opts);
}

#endif

0 comments on commit 1845863

Please sign in to comment.