Symphony 1.0
Loading...
Searching...
No Matches
MazeProblem Class Reference

#include <simple_maze.h>

Inheritance diagram for MazeProblem:
Collaboration diagram for MazeProblem:

Public Member Functions

 MazeProblem ()
 
 ~MazeProblem ()
 
bool goal_test (State *state) override
 Tests if a given state satisfies the goal condition.
 
std::vector< std::shared_ptr< Action > > actions (std::shared_ptr< State > state) override
 Retrieves the set of actions applicable to a given state.
 
double heuristic (State *state) override
 Computes a heuristic estimate for a given state.
 
- Public Member Functions inherited from Problem
 Problem ()
 Constructor for the Problem class.
 
virtual ~Problem ()
 Virtual destructor for the Problem class.
 
virtual Stateinitial_state ()
 Retrieves the initial state of the problem.
 

Additional Inherited Members

- Public Attributes inherited from Problem
Stateinitial_state_
 Pointer to the initial state of the problem.
 

Detailed Description

Definition at line 57 of file simple_maze.h.

Constructor & Destructor Documentation

◆ MazeProblem()

MazeProblem::MazeProblem ( )
inline

Definition at line 59 of file simple_maze.h.

59 {
61 }
State * initial_state_
Pointer to the initial state of the problem.

References Problem::initial_state_.

◆ ~MazeProblem()

MazeProblem::~MazeProblem ( )
inline

Definition at line 62 of file simple_maze.h.

62 {
63 }

Member Function Documentation

◆ actions()

std::vector< std::shared_ptr< Action > > MazeProblem::actions ( std::shared_ptr< State state)
inlineoverridevirtual

Retrieves the set of actions applicable to a given state.

This method must be implemented by the user to define the available actions for a specific state.

Parameters
stateThe current state.
Returns
A vector of actions applicable to the given state.

Implements Problem.

Definition at line 69 of file simple_maze.h.

69 {
70 auto maze_state = std::dynamic_pointer_cast<MazeState>(state);
71 std::vector<std::shared_ptr<Action>> actions;
72
73 auto create_state = [&](int x, int y) {
74 return std::make_shared<MazeState>(maze_state->maze, x, y);
75 };
76
77 // Add possible actions with smart pointers
78 if (maze_state->x > 0 && maze_state->maze[maze_state->x - 1][maze_state->y] != 1) {
79 actions.push_back(std::make_shared<Action>("Up", 1, state, create_state(maze_state->x - 1, maze_state->y)));
80 }
81 if (maze_state->x < maze_state->maze.size() - 1 && maze_state->maze[maze_state->x + 1][maze_state->y] != 1) {
82 actions.push_back(std::make_shared<Action>("Down", 1, state, create_state(maze_state->x + 1, maze_state->y)));
83 }
84 if (maze_state->y > 0 && maze_state->maze[maze_state->x][maze_state->y - 1] != 1) {
85 actions.push_back(std::make_shared<Action>("Left", 1, state, create_state(maze_state->x, maze_state->y - 1)));
86 }
87 if (maze_state->y < maze_state->maze[0].size() - 1 && maze_state->maze[maze_state->x][maze_state->y + 1] != 1) {
88 actions.push_back(std::make_shared<Action>("Right", 1, state, create_state(maze_state->x, maze_state->y + 1)));
89 }
90
91 return actions;
92 }
std::vector< std::shared_ptr< Action > > actions(std::shared_ptr< State > state) override
Retrieves the set of actions applicable to a given state.
Definition simple_maze.h:69

References actions().

Referenced by actions().

◆ goal_test()

bool MazeProblem::goal_test ( State state)
inlineoverridevirtual

Tests if a given state satisfies the goal condition.

This method must be implemented by the user to define the goal state criteria.

Parameters
stateThe state to test.
Returns
True if the state satisfies the goal condition, false otherwise.

Implements Problem.

Definition at line 64 of file simple_maze.h.

64 {
65 auto *maze_state = dynamic_cast<MazeState *>(state);
66 return maze_state->maze[maze_state->x][maze_state->y] == -1;
67 }
std::vector< std::vector< int > > maze
Definition simple_maze.h:28

References MazeState::maze.

◆ heuristic()

double MazeProblem::heuristic ( State state)
inlineoverridevirtual

Computes a heuristic estimate for a given state.

This method must be implemented by the user to define a heuristic function for the problem.

Parameters
stateThe state for which to compute the heuristic.
Returns
A heuristic estimate of the cost to reach the goal from the given state.

Implements Problem.

Definition at line 96 of file simple_maze.h.

96 {
97 auto *maze_state = dynamic_cast<MazeState *>(state);
98 return abs(maze_state->x - 3) + abs(maze_state->y - 3);
99 }

The documentation for this class was generated from the following file: