Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
build/
ideas/
*.json
*.exe
.vscode/
*.log
.DS_Store
.github/
44 changes: 40 additions & 4 deletions src/cell_tree_agent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ Dir move_to_parent(Grid<Coord> const& cell_parents, Coord a) {
throw "move_to_parent";
}


Unreachables cell_tree_unreachables(GameBase const& game, Grid<Step> const& dists) {
auto cell_parents = cell_tree_parents(game.dimensions(), game.snake);
auto can_move = [&](Coord from, Coord to, Dir dir) {
Expand All @@ -82,6 +81,38 @@ Unreachables cell_tree_unreachables(GameBase const& game, Grid<Step> const& dist
return unreachables(can_move, game, dists);
}

bool should_use_cached_path_for_move_tail(const Unreachables& unreachable, Lookahead lookahead, const std::vector<Coord>& cached_path) {
if (lookahead == Lookahead::many_move_tail) {
if ((unreachable.any) && (unreachable.dist_to_farthest >= INT_MAX) && !cached_path.empty()) {
return true;
}
}
return false;
}

Unreachables get_unreachables(
const GameBase& game,
const std::vector<Coord>& path,
Lookahead lookahead,
const Grid<Step>& dists) {
if (lookahead == Lookahead::many_move_tail) {
auto after = after_moves(game, path, Lookahead::many_move_tail);
auto unreachable = cell_tree_unreachables(after, dists);

if (!unreachable.any) {
return unreachable;
} else {
auto after = after_moves(game, path, Lookahead::many_keep_tail);
auto unreachable = cell_tree_unreachables(after, dists);
return unreachable;
}
} else {
auto after = after_moves(game, path, lookahead);
auto unreachable = cell_tree_unreachables(after, dists);
return unreachable;
}
}

enum class DetourStrategy {
none,
any,
Expand Down Expand Up @@ -156,9 +187,14 @@ struct CellTreeAgent : Agent {
}

// Heuristic 3: prevent making parts of the grid unreachable
if (detour != DetourStrategy::none) {
auto after = after_moves(game, path, lookahead);
auto unreachable = cell_tree_unreachables(after, dists);
if (detour != DetourStrategy::none) {
const Unreachables unreachable = get_unreachables(game, path, lookahead, dists);
if (should_use_cached_path_for_move_tail(unreachable, lookahead, cached_path)) {
pos2 = cached_path.back();
cached_path.pop_back();
return pos2 - pos;
}

if (unreachable.any) {
if (log) {
Grid<bool> unreachable_grid(game.dimensions());
Expand Down
8 changes: 7 additions & 1 deletion src/game_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ GameBase after_moves(GameBase const& game, std::vector<Coord> const& path, Looka
struct Unreachables {
bool any = false;
Coord nearest = {-1,-1};
Coord farthest = {-1,-1};
int dist_to_nearest = INT_MAX;
int dist_to_farthest = 0;
Grid<bool> reachable;

Unreachables(Grid<bool> const& reachable)
Expand All @@ -68,6 +70,10 @@ Unreachables unreachables(CanMove can_move, GameLike const& game, Grid<Step> con
out.nearest = a;
out.dist_to_nearest = dists[a].dist;
}
if (dists[a].dist > out.dist_to_farthest) { // track the farthest unreachable coordinate
out.farthest = a;
out.dist_to_farthest = dists[a].dist;
}
}
}
return out;
Expand Down Expand Up @@ -171,7 +177,7 @@ Grid<Coord> random_spanning_tree(CoordRange dims, RNG& rng) {
}
}
while (!queue.empty()) {
int i = rng.random(queue.size());
int i = rng.random(static_cast<int>(queue.size()));
Coord parent = queue[i].first;
Coord node = queue[i].second;
queue[i] = queue.back();
Expand Down
4 changes: 2 additions & 2 deletions src/snake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ Stats play_multiple_threaded(AgentGen make_agent, Config& config) {
t.join();
}
// done
if (!config.quiet) std::cout << "\033[K\r";
if (!config.quiet) std::cout << std::endl;
return stats;
}

Expand All @@ -467,7 +467,7 @@ Stats play_multiple(AgentGen make_agent, Config& config) {
std::cout << (i+1) << "/" << config.num_rounds << " " << stats << "\033[K\r" << std::flush;
}
}
if (!config.quiet) std::cout << "\033[K\r";
if (!config.quiet) std::cout << std::endl;
return stats;
}

Expand Down