Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip already-visited tiles during radius calculation (approx. 5% fewer tiles) #25

Merged
merged 1 commit into from
May 19, 2024

Conversation

ide
Copy link
Contributor

@ide ide commented May 19, 2024

We have found the shortest path to a given tile when it is dequeued from the priority queue for the first time. There is no shorter path because all other tiles in the priority queue have the same or greater cost from the starting tile (Djikstra).

To keep the implementation simple, this commit uses the existing closed array to mark tiles that have been visited. We can also remove the check for currentVector.equals(start) because the tile for start will already have been put into closed.

The tests pass. The number of visited tiles is slightly lower in one of the Radius test cases (added logging to measure this).

The random MapGenerator test perhaps shows a more representative impact. I ran it one time before and after this commit, and looked at the average number of tiles visited in calculateRadius during each run: before = 395, after = 372. Of course, there is randomness and it shows we can expect about 5% savings in tiles visited.

…r tiles)

We have found the shortest path to a given tile when it is dequeued from the priority queue for the first time. There is no shorter path because all other tiles in the priority queue have the same or equal cost from the starting tile (Djikstra).

To keep the implementation simple, this commit uses the existing `closed` array to mark tiles that have been visited. We can also remove the check for `currentVector.equals(start)` because the tile for `start` will already have been put into `closed`.

The tests pass. The number of visited tiles is slightly lower in one of the Radius test cases (added logging to measure this).

The random MapGenerator test perhaps shows a more representative impact. I ran it one time before and after this commit, and looked at the average number of tiles visited in `calculateRadius` during each run: before = 395, after = 372. Of course, there is randomness and it shows we can expect about 5% savings in tiles visited.
continue;
}
closed[index] = true;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only line that matters. The rest of this PR just moves around variables.

@cpojer cpojer merged commit b0d5cfc into nkzw-tech:main May 19, 2024
2 checks passed
@ide ide deleted the radius-djikstra branch May 19, 2024 19:28
@cpojer
Copy link
Contributor

cpojer commented May 19, 2024

Amazing, thanks for the PR and the improvement! Radius calculation is finicky, especially with Trenches that take movement points on traversal in/out but the tests should catch all of the edge cases these days. I believe in a past version there was an issue where a shorter path was discovered later on, but that was before I made use of the priority queue.

cpojer pushed a commit that referenced this pull request May 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants