Skip to content

Commit

Permalink
Merge pull request #56 from srajan-kiyotaka/sracho
Browse files Browse the repository at this point in the history
made changes in the graph and tree agents methods.
  • Loading branch information
srajan-kiyotaka authored Jun 19, 2024
2 parents cfb59cc + 5e2ee6b commit e16d65a
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 51 deletions.
12 changes: 6 additions & 6 deletions docs/graphAgent.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,23 @@ The getHeatMapColor method determines and returns the color representation corre
- **Returns**:
- `tuple`: The RGB color value for the given value on the heat map.

### `checkGoalState(self, node)`
### `checkGoalState(self, nodeId)`

This method checks whether a given node within the graph represents a goal state for the agent. It takes a GraphNode object as its parameter and evaluates whether the node's isGoalState attribute is True. If the node is identified as a goal state, the method returns True; otherwise, it returns False. This check allows the agent to assess its progress towards predefined objectives within the graph.
This method checks whether a given node within the graph represents a goal state for the agent.

- **Parameters**:
- `node` (`GraphNode`): The node to be checked.
- `nodeId`: The ID of the node to be checked.

- **Returns**:
- `bool`: True if the node is a goal state, False otherwise.

### `moveAgent(self, node, delay:int=1)`
### `moveAgent(self, nodeId, delay:int=1)`

The moveAgent method directs the agent to navigate from its current node to a specified node within the graph. It includes an optional delay parameter that introduces a pause (in seconds) before the agent proceeds to the next node. During the movement process, the method updates the heat map visualization to reflect the agent's path and increments the heat map value of the current node. If successful, the method returns True; otherwise, it returns False if the specified node is None, indicating a failed attempt to move.

- **Parameters**:
- `node` (`GraphNode`): The node to which the agent should be moved.
- `delay` (`int`, optional): The delay (in seconds) before moving to the next node. Defaults to 1 second.
- `nodeId`: The ID of the node to which the agent should be moved.
- `delay` (`float`, optional): The delay (in seconds) before moving to the next node. Defaults to 1 second.

- **Returns**:
- `bool`: True if the agent was successfully moved to the node, False otherwise.
Expand Down
8 changes: 4 additions & 4 deletions docs/treeAgent.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,20 @@ This method returns the color corresponding to a given value on a heat map. It u
- **Returns**:
- `tuple`: The RGB color value for the given value on the heat map.

#### `checkGoalState(self, node)`
#### `checkGoalState(self, nodeId)`
This method checks if the specified node is a goal state.

- **Parameters**:
- `node` (`TreeNode`): The node to be checked.
- `nodeId`: The ID of the node to be checked.

- **Returns**:
- `bool`: True if the node is a goal state, False otherwise.

#### `moveAgent(self, node, delay:int=1)`
#### `moveAgent(self, nodeId, delay:int=1)`
This method moves the agent to the specified node after a given delay. It updates the heat map and the agent's current position.

- **Parameters**:
- `node` (`TreeNode`): The node to which the agent should be moved.
- `nodeId`: The ID of the node to which the agent should be moved.
- `delay` (`int`, optional): The delay (in seconds) before moving to the next node. Defaults to 1 second.

- **Returns**:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='TraverseCraft',
version='0.7.5',
version='0.8.0',
author='Srajan Chourasia, Varun Patrikar',
author_email='[email protected], [email protected]',
maintainer='Srajan Chourasia, Varun Patrikar',
Expand Down
38 changes: 24 additions & 14 deletions src/traverseCraft/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,34 +498,39 @@ def _modifyCellColor(self, node, color):
self._worldObj.changeNodeColor(node.id, color)
self._root.update()

def checkGoalState(self, node):
def checkGoalState(self, nodeId):
"""
Check if the given node is a goal state.
Parameters:
- node: The node to be checked.
- nodeId: The node Id to be checked.
Returns:
- True if the node is a goal state, False otherwise.
"""
if(node.isGoalState):
return True
if(nodeId in self._worldObj.nodeMap.keys()):
node = self._worldObj.getNode(nodeId)
if(node.isGoalState):
return True
else:
return False
else:
return False


def moveAgent(self, node, delay:int=1):
def moveAgent(self, nodeId, delay:int=1):
"""
Moves the agent to the specified node.
Args:
node: The node to which the agent should be moved.
nodeId: The node Id to which the agent should be moved.
delay (optional): The delay (in seconds) before moving to the next node. Default is 1 second.
Returns:
bool: True if the agent was successfully moved to the node, False otherwise.
"""
if(node is not None):
if(nodeId in self._worldObj.nodeMap.keys()):
node = self._worldObj.nodeMap[nodeId]
time.sleep(delay)
parent = self._currentNode
self._updateHeatMap(parent)
Expand Down Expand Up @@ -753,34 +758,39 @@ def _modifyCellColor(self, node, color):
self._worldObj.changeNodeColor(node.id, color)
self._root.update()

def checkGoalState(self, node):
def checkGoalState(self, nodeId):
"""
Check if the given node is a goal state.
Parameters:
node: The node to be checked.
node: The ID of the node to be checked.
Returns:
bool: True if the node is a goal state, False otherwise.
"""
if(node.isGoalState):
return True
if(nodeId in self._worldObj.nodeMap.keys()):
node = self._worldObj.getNode(nodeId)
if(node.isGoalState):
return True
else:
return False
else:
return False


def moveAgent(self, node, delay:int=1):
def moveAgent(self, nodeId, delay:int=1):
"""
Moves the agent to the specified node.
Args:
node: The node to which the agent should be moved.
nodeId: The ID of the node to which the agent should be moved.
delay (optional): The delay (in seconds) before moving to the next node. Default is 1 second.
Returns:
bool: True if the agent was successfully moved to the node, False otherwise.
"""
if(node is not None):
if(nodeId in self._worldObj.nodeMap.keys()):
node = self._worldObj.nodeMap[nodeId]
time.sleep(delay)
parent = self._currentNode
self._updateHeatMap(parent)
Expand Down
2 changes: 2 additions & 0 deletions src/traverseCraft/world.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ def aboutWorld(self):
about.add_row(["Cell Size", self._cellSize])
about.add_row(["Cell Padding", self._cellPadding])
about.add_row(["Window Size", f"{self._windowHeight}x{self._windowWidth}"])
about.add_row(["Goal Cells", self._goalCells])
about.add_row(["Block Cells", self._blockCells])
about.add_row(["Path Color", self._pathColor])
about.add_row(["Block Color", self._blockColor])
about.add_row(["Goal Color", self._goalColor])
Expand Down
20 changes: 6 additions & 14 deletions tests/test_graph_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,8 @@ def sample_algorithm():
self.assertTrue(True)

def test_check_goal_state(self):
goal_node = self.graphWorld.getNode("G")
non_goal_node = self.graphWorld.getNode("A")
self.assertTrue(self.agent.checkGoalState(goal_node))
self.assertFalse(self.agent.checkGoalState(non_goal_node))
self.assertTrue(self.agent.checkGoalState("G"))
self.assertFalse(self.agent.checkGoalState("A"))

def test_set_start_state(self):
self.agent.setStartState("B")
Expand All @@ -104,18 +102,12 @@ def test_set_start_state_invalid(self):
self.agent.setStartState("Z") # Assuming "Z" is not a valid node ID

def test_move_agent(self):
# Get pointers to nodes 'B', 'D', and 'E' using self.graphWorld.getNode()
node_B = self.graphWorld.getNode('B')
node_C = self.graphWorld.getNode('C')
node_D = self.graphWorld.getNode('D')
node_E = self.graphWorld.getNode('E')

# Move the agent
self.assertFalse(self.agent.moveAgent(None)) # Move to None should fail
self.assertTrue(self.agent.moveAgent(node_B)) # Move to node 'B' should succeed
self.assertTrue(self.agent.moveAgent(node_C)) # Move to node 'C' should succeed
self.assertTrue(self.agent.moveAgent(node_D)) # Move to node 'D' should succeed
self.assertTrue(self.agent.moveAgent(node_E)) # Move to node 'E' should succeed
self.assertTrue(self.agent.moveAgent("B")) # Move to node 'B' should succeed
self.assertTrue(self.agent.moveAgent("C")) # Move to node 'C' should succeed
self.assertTrue(self.agent.moveAgent("D")) # Move to node 'D' should succeed
self.assertTrue(self.agent.moveAgent("E")) # Move to node 'E' should succeed

if __name__ == '__main__':
unittest.main()
18 changes: 6 additions & 12 deletions tests/test_tree_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,16 @@ def sample_algorithm():

def test_check_goal_state(self):
# Check goal state
self.assertFalse(self.agent.checkGoalState(self.agent._treeRoot)) # The root node should be a goal state
self.assertTrue(self.agent.checkGoalState(self.treeWorld.getNode('G'))) # The node 'G' should be a goal state
self.assertFalse(self.agent.checkGoalState(self.agent._treeRoot.id)) # The root node should be a goal state
self.assertTrue(self.agent.checkGoalState("G")) # The node 'G' should be a goal state

def test_move_agent(self):
# Get pointers to nodes 'B', 'D', and 'E' using self.treeWorld.getNode()
node_B = self.treeWorld.getNode('B')
node_C = self.treeWorld.getNode('C')
node_D = self.treeWorld.getNode('D')
node_E = self.treeWorld.getNode('E')

# Move the agent
self.assertFalse(self.agent.moveAgent(None)) # Move to None should fail
self.assertTrue(self.agent.moveAgent(node_B)) # Move to node 'B' should succeed
self.assertTrue(self.agent.moveAgent(node_C)) # Move to node 'C' should succeed
self.assertTrue(self.agent.moveAgent(node_D)) # Move to node 'D' should succeed
self.assertTrue(self.agent.moveAgent(node_E)) # Move to node 'E' should succeed
self.assertTrue(self.agent.moveAgent("B")) # Move to node 'B' should succeed
self.assertTrue(self.agent.moveAgent("C")) # Move to node 'C' should succeed
self.assertTrue(self.agent.moveAgent("D")) # Move to node 'D' should succeed
self.assertTrue(self.agent.moveAgent("E")) # Move to node 'E' should succeed

if __name__ == '__main__':
unittest.main()

0 comments on commit e16d65a

Please sign in to comment.