Now have a cycle and repeated action mitigation technique in the AI algorithm called 'delete shit you've seen'.
This commit is contained in:
parent
6fa7b0a418
commit
862d8b4d81
3 changed files with 44 additions and 21 deletions
54
goap.cpp
54
goap.cpp
|
@ -54,24 +54,50 @@ namespace ai {
|
|||
return count;
|
||||
}
|
||||
|
||||
Script reconstruct_path(std::unordered_map<Action, Action>& came_from, Action& current) {
|
||||
Script total_path{current};
|
||||
bool final_found = false;
|
||||
inline void dump_came_from(std::string msg, std::unordered_map<Action, Action>& came_from, Action& current) {
|
||||
fmt::println("{}: {}", msg, current.name);
|
||||
|
||||
for(auto& [from, to] : came_from) {
|
||||
fmt::println("from={}; to={}", from.name, to.name);
|
||||
}
|
||||
}
|
||||
|
||||
inline void path_invariant(std::unordered_map<Action, Action>& came_from, Action& current) {
|
||||
#if defined(NDEBUG)
|
||||
(void)came_from; // disable errors about unused
|
||||
(void)current;
|
||||
#else
|
||||
bool final_found = current == FINAL_ACTION;
|
||||
|
||||
for(size_t i = 0; i <= came_from.size() && came_from.contains(current); i++) {
|
||||
current = came_from.at(current);
|
||||
if(current != FINAL_ACTION) {
|
||||
total_path.push_front(current);
|
||||
} else {
|
||||
final_found = true;
|
||||
}
|
||||
final_found = current == FINAL_ACTION;
|
||||
}
|
||||
|
||||
// this here temporarily while I figure out cycle detects/prevention
|
||||
if(!final_found && total_path[0] != FINAL_ACTION) {
|
||||
auto error = fmt::format("!!!!! You may have a cycle in your json. No FINAL found. Here's the path: ");
|
||||
for(auto& action : total_path) error += fmt::format("{} ", action.name);
|
||||
dbc::sentinel(error);
|
||||
if(!final_found) {
|
||||
dump_came_from("CYCLE DETECTED!", came_from, current);
|
||||
dbc::sentinel("AI CYCLE FOUND!");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
Script reconstruct_path(std::unordered_map<Action, Action>& came_from, Action& current) {
|
||||
Script total_path{current};
|
||||
|
||||
path_invariant(came_from, current);
|
||||
|
||||
for(size_t i = 0; i <= came_from.size() && came_from.contains(current); i++) {
|
||||
auto next = came_from.at(current);
|
||||
|
||||
if(next != FINAL_ACTION) {
|
||||
// remove the previous node to avoid cycles and repeated actions
|
||||
total_path.push_front(next);
|
||||
came_from.erase(current);
|
||||
current = next;
|
||||
} else {
|
||||
// found the terminator, done
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return total_path;
|
||||
|
@ -147,11 +173,9 @@ namespace ai {
|
|||
came_from.insert_or_assign(neighbor_action, current.action);
|
||||
|
||||
g_score.insert_or_assign(neighbor, tentative_g_score);
|
||||
// open_set gets the fScore
|
||||
ActionState neighbor_as{neighbor_action, neighbor};
|
||||
|
||||
int score = tentative_g_score + h(neighbor, goal);
|
||||
// could maintain lowest here and avoid searching all things
|
||||
f_score.emplace_back(score, neighbor_as);
|
||||
std::push_heap(f_score.begin(), f_score.end(), FScorePair_cmp);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue