Skip to content

Commit

Permalink
Add documentation and tidy up
Browse files Browse the repository at this point in the history
Rename clusterFinder to dotClusterFinder
  • Loading branch information
ll-nick authored and orzechow committed Oct 4, 2024
1 parent b91d969 commit 704d91b
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 24 deletions.
5 changes: 3 additions & 2 deletions demo/include/demo/change_dot_cluster_behavior.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
namespace demo {

/**
* @brief The ChangeDotClusterBehavior makes pacman move towards another cluster of duts.
* @brief The ChangeDotClusterBehavior makes pacman move towards the closest dot cluster
* that he is not currently inside of.
*/
class ChangeDotClusterBehavior : public arbitration_graphs::Behavior<Command> {
public:
using Ptr = std::shared_ptr<ChangeDotClusterBehavior>;
using ConstPtr = std::shared_ptr<const ChangeDotClusterBehavior>;

using Cluster = utils::Cluster;
using Clusters = utils::ClusterFinder::Clusters;
using Clusters = utils::DotClusterFinder::Clusters;

explicit ChangeDotClusterBehavior(EnvironmentModel::Ptr environmentModel,
const std::string& name = "ChangeDotClusterBehavior")
Expand Down
8 changes: 4 additions & 4 deletions demo/include/demo/environment_model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace demo {
class EnvironmentModel {
public:
using Cluster = utils::Cluster;
using Clusters = utils::ClusterFinder::Clusters;
using Clusters = utils::DotClusterFinder::Clusters;
using Entities = utils::Entities;
using Maze = utils::Maze;
using Ghost = utils::Ghost;
Expand All @@ -46,7 +46,7 @@ class EnvironmentModel {
void update(const Game& game) {
maze_ = std::make_shared<Maze>(game.maze);
astar_.updateMaze(maze_);
clusterFinder_ = utils::ClusterFinder{maze_};
clusterFinder_ = utils::DotClusterFinder{maze_};
updateEntities(game.reg);
}

Expand Down Expand Up @@ -80,7 +80,7 @@ class EnvironmentModel {
* walls nor empty space.
*/
Clusters dotCluster() const {
return clusterFinder_.dotClusters();
return clusterFinder_.clusters();
}

/**
Expand Down Expand Up @@ -116,7 +116,7 @@ class EnvironmentModel {
Maze::ConstPtr maze_;

utils::AStar astar_;
utils::ClusterFinder clusterFinder_;
utils::DotClusterFinder clusterFinder_;
mutable util_caching::Cache<Time, GhostWithDistance> closestGhostCache_;
mutable util_caching::Cache<Time, GhostWithDistance> closestScaredGhostCache_;
};
Expand Down
24 changes: 15 additions & 9 deletions demo/include/utils/cluster.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,39 @@ struct ClusterCell : public BaseCell {
bool visited{false};
};

/**
* @brief A cluster is defined by a set of points that can be connected by a path which passes through neither walls nor
* empty space.
*/
struct Cluster {
explicit Cluster(const int& clusterId, const std::vector<Position>& dots)
: id(clusterId), dots(dots), center{findClusterCenter()} {
explicit Cluster(const int& clusterId, const std::vector<Position>& points)
: id(clusterId), dots(points), center{findClusterCenter()} {
}
bool isInCluster(const Position& target) const {
return std::any_of(dots.begin(), dots.end(), [target](Position dot) { return dot == target; });
}

int id;
std::vector<Position> dots;
Positions dots;

/// @brief The dot closest to the average position of all the dots of this cluster
Position center;
Position center; ///< The dot closest to the average position of all the dots of this cluster

private:
Position findClusterCenter() const;
};

class ClusterFinder {
/**
* @brief Search and store all clusters of dots (including power pellets) given the maze state.
*/
class DotClusterFinder {
public:
using Cell = ClusterCell;
using Clusters = std::vector<Cluster>;
using ClusterMazeAdapter = MazeAdapter<ClusterCell>;
using Cell = ClusterCell;

explicit ClusterFinder(Maze::ConstPtr maze) : maze_(std::move(maze)), clusters_{findDotClusters()} {
explicit DotClusterFinder(Maze::ConstPtr maze) : maze_(std::move(maze)), clusters_{findDotClusters()} {
}
Clusters dotClusters() const {
Clusters clusters() const {
return clusters_;
}

Expand Down
8 changes: 7 additions & 1 deletion demo/include/utils/maze.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,13 @@ struct BaseCell {
TileType type;
};

template <typename CellType>
/**
* @brief Adapter class for storing properties per cell of the underlying maze.
*
* The MazeAdapter is a helpful little class to put between an algorithm and the actual maze class. It can be used to
* store properties per cell of the maze grid using the templated CellType class. Cells are lazily initialized.
*/
template <typename CellType = BaseCell>
class MazeAdapter {
public:
using MazeStateConstPtr = std::shared_ptr<const MazeState>;
Expand Down
10 changes: 5 additions & 5 deletions demo/src/cluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ Position Cluster::findClusterCenter() const {
int sumX = 0;
int sumY = 0;

for (const auto& dot : dots) {
sumX += dot.x;
sumY += dot.y;
for (const auto& point : dots) {
sumX += point.x;
sumY += point.y;
}

int avgX = std::floor(sumX / dots.size());
Expand All @@ -32,7 +32,7 @@ Position Cluster::findClusterCenter() const {
return closestDot;
}

ClusterFinder::Clusters ClusterFinder::findDotClusters() const {
DotClusterFinder::Clusters DotClusterFinder::findDotClusters() const {
ClusterMazeAdapter mazeAdapter(maze_);
Clusters clusters;
int clusterId = 0;
Expand All @@ -52,7 +52,7 @@ ClusterFinder::Clusters ClusterFinder::findDotClusters() const {
return clusters;
}

Positions ClusterFinder::expandDot(const Cell& start, const ClusterMazeAdapter& mazeAdapter) const {
Positions DotClusterFinder::expandDot(const Cell& start, const ClusterMazeAdapter& mazeAdapter) const {
Positions dots;

std::queue<Position> bfsQueue;
Expand Down
4 changes: 2 additions & 2 deletions demo/test/cluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ TEST_F(ClusterTest, dotClusters) {
"#####"};
environmentModel_->setMaze({5, 5}, str);

ClusterFinder clusterFinder(environmentModel_->maze());
DotClusterFinder dotClusterFinder(environmentModel_->maze());

std::vector<Cluster> clusters = clusterFinder.dotClusters();
std::vector<Cluster> clusters = dotClusterFinder.clusters();

ASSERT_EQ(clusters.size(), 2);
EXPECT_EQ(clusters.front().dots.size(), 3);
Expand Down
2 changes: 1 addition & 1 deletion demo/test/mock_environment_model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class MockEnvironmentModel : public EnvironmentModel {
void setMaze(const Position& size, const char (&str)[Size]) {
maze_ = std::make_shared<Maze>(makeCustomMazeState({size.x, size.y}, str));
astar_ = utils::AStar(maze_);
clusterFinder_ = utils::ClusterFinder(maze_);
clusterFinder_ = utils::DotClusterFinder(maze_);
}
void setEmptyMaze() {
const char str[] = {"##########"
Expand Down

0 comments on commit 704d91b

Please sign in to comment.