Mostly fixed up but I have to figure out why cost on actions isn't changing the priority.

This commit is contained in:
Zed A. Shaw 2025-03-31 13:51:27 -04:00
parent 862d8b4d81
commit 52f45e1d45
5 changed files with 17 additions and 29 deletions

View file

@ -111,43 +111,34 @@ namespace ai {
return distance_to_goal(start, goal);
}
using FScorePair = std::pair<int, ActionState>;
auto FScorePair_cmp = [](const FScorePair& l, const FScorePair& r) {
return l.first < r.first;
};
using FScoreQueue = std::vector<FScorePair>;
ActionState find_lowest(std::unordered_map<ActionState, int>& open_set,
FScoreQueue& f_scores)
{
ActionState find_lowest(std::unordered_map<ActionState, int>& open_set) {
check(!open_set.empty(), "open set can't be empty in find_lowest");
int found_score = SCORE_MAX;
ActionState found_as;
for(auto& [score, astate] : f_scores) {
if(open_set.contains(astate)) {
return astate;
for(auto& kv : open_set) {
if(kv.second < found_score) {
found_score = kv.second;
found_as = kv.first;
}
}
dbc::sentinel("lowest not found!");
return found_as;
}
ActionPlan plan_actions(std::vector<Action>& actions, State start, State goal) {
std::unordered_map<ActionState, int> open_set;
std::unordered_map<Action, Action> came_from;
std::unordered_map<State, int> g_score;
FScoreQueue f_score;
std::unordered_map<State, bool> closed_set;
ActionState current{FINAL_ACTION, start};
g_score.insert_or_assign(start, 0);
f_score.emplace_back(h(start, goal), current);
std::push_heap(f_score.begin(), f_score.end(), FScorePair_cmp);
open_set.insert_or_assign(current, h(start, goal));
while(!open_set.empty()) {
// current := the node in openSet having the lowest fScore[] value
current = find_lowest(open_set, f_score);
current = find_lowest(open_set);
if(is_subset(current.state, goal)) {
return {true,
@ -175,9 +166,7 @@ namespace ai {
g_score.insert_or_assign(neighbor, tentative_g_score);
ActionState neighbor_as{neighbor_action, neighbor};
int score = tentative_g_score + h(neighbor, goal);
f_score.emplace_back(score, neighbor_as);
std::push_heap(f_score.begin(), f_score.end(), FScorePair_cmp);
int score = tentative_g_score + h(neighbor, goal) + neighbor_action.cost;
// this maybe doesn't need score
open_set.insert_or_assign(neighbor_as, score);