Skip to content

Commit

Permalink
optimize goal_count (callgrind ir/call : 816 -> 126)
Browse files Browse the repository at this point in the history
  • Loading branch information
guicho271828 committed Jan 12, 2025
1 parent c7d6a4d commit 8008590
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/search/heuristics/goal_count_heuristic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@ GoalCountHeuristic::GoalCountHeuristic(
if (log.is_at_least_normal()) {
log << "Initializing goal count heuristic..." << endl;
}
goals.reserve(task_proxy.get_goals().size());
for (FactProxy goal : task_proxy.get_goals()) {
int var = goal.get_variable().get_id();
int val = goal.get_value();
goals.push_back({var,val});
}
}

int GoalCountHeuristic::compute_heuristic(const State &ancestor_state) {
State state = convert_ancestor_state(ancestor_state);
int unsatisfied_goal_count = 0;

for (FactProxy goal : task_proxy.get_goals()) {
const VariableProxy var = goal.get_variable();
if (state[var] != goal) {
state.unpack();
const vector<int> &unpacked_state = state.get_unpacked_values();
for (const auto& it : goals) {
const int& var = it.first;
const int& val = it.second;
if (unpacked_state[var] != val) {
++unsatisfied_goal_count;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/search/heuristics/goal_count_heuristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace goal_count_heuristic {
class GoalCountHeuristic : public Heuristic {
std::vector<std::pair<int,int>> goals;
protected:
virtual int compute_heuristic(const State &ancestor_state) override;
public:
Expand Down

0 comments on commit 8008590

Please sign in to comment.