Skip to content

Commit

Permalink
AStar returns optional to clearly indicate whether path was found
Browse files Browse the repository at this point in the history
  • Loading branch information
ll-nick committed Sep 24, 2024
1 parent de69a19 commit abe8f0c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
6 changes: 3 additions & 3 deletions demo/include/demo/eat_closest_dot_behavior.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ class EatClosestDotBehavior : public arbitration_graphs::Behavior<Command> {

Command getCommand(const Time& /*time*/) override {
auto pacmanPosition = environmentModel_->pacmanPosition();
Path pathToClosestDot = environmentModel_->pathToClosestDot(pacmanPosition);
std::optional<Path> pathToClosestDot = environmentModel_->pathToClosestDot(pacmanPosition);

if (pathToClosestDot.empty()) {
if (!pathToClosestDot) {
throw std::runtime_error("Failed to compute path to closest dot. Can not provide a sensible command.");
}

return Command{pathToClosestDot};
return Command{pathToClosestDot.value()};
}

bool checkInvocationCondition(const Time& /*time*/) const override {
Expand Down
2 changes: 1 addition & 1 deletion demo/include/demo/environment_model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class EnvironmentModel {
return astar_.mazeDistance(start, goal);
}

Path pathToClosestDot(const Position& position) const {
std::optional<Path> pathToClosestDot(const Position& position) const {
return astar_.pathToClosestDot(position);
}

Expand Down
2 changes: 1 addition & 1 deletion demo/src/astar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Path AStar::shortestPath(const Position& start, const Position& goal) const {
return {};
}

Path AStar::pathToClosestDot(const Position& start) const {
std::optional<Path> AStar::pathToClosestDot(const Position& start) const {
// There is a "virtual" position outside of the maze that entities are on when entering the tunnel. We accept a
// small error in the distance computation by neglecting this and wrapping the position to be on either end of the
// tunnel.
Expand Down
11 changes: 7 additions & 4 deletions demo/test/astar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,18 @@ TEST_F(AStarTest, pathToClosestDot) {
environmentModel_->setMaze({5, 6}, str);

AStar astar(environmentModel_->maze());
Path path = astar.pathToClosestDot({1, 2});
std::optional<Path> path = astar.pathToClosestDot({1, 2});
ASSERT_TRUE(path.has_value());

Path targetPath = {Direction::UP, Direction::RIGHT, Direction::RIGHT};
ASSERT_EQ(path.size(), targetPath.size());
ASSERT_EQ(path->size(), targetPath.size());
for (int i = 0; i < targetPath.size(); i++) {
EXPECT_EQ(path.at(i), targetPath.at(i));
EXPECT_EQ(path->at(i), targetPath.at(i));
}

path = astar.pathToClosestDot({1, 3});
ASSERT_EQ(path.size(), 3);
ASSERT_TRUE(path.has_value());
ASSERT_EQ(path->size(), 3);
}

} // namespace utils

0 comments on commit abe8f0c

Please sign in to comment.