Skip to content

Commit

Permalink
added a few more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
devrz45 committed May 28, 2023
1 parent c6c6709 commit fb54032
Show file tree
Hide file tree
Showing 9 changed files with 526 additions and 4 deletions.
8 changes: 4 additions & 4 deletions pyreason_gym/pyreason_grid_world/graph/game_graph.graphml
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,9 @@
<edge source="3" target="end">
<data key="down">1</data>
</edge>
<edge source="3" target="blue-soldier-1">
<data key="atLoc">1</data>
</edge>
<edge source="4" target="5">
<data key="right">1</data>
</edge>
Expand All @@ -335,9 +338,6 @@
<edge source="5" target="end">
<data key="down">1</data>
</edge>
<edge source="5" target="blue-soldier-1">
<data key="atLoc">1</data>
</edge>
<edge source="6" target="7">
<data key="right">1</data>
</edge>
Expand Down Expand Up @@ -1064,7 +1064,7 @@
<edge source="blue-soldier-1" target="blue-base">
<data key="team">1</data>
</edge>
<edge source="blue-soldier-1" target="5">
<edge source="blue-soldier-1" target="3">
<data key="atLoc">1</data>
</edge>
<edge source="blue-soldier-1" target="blue-bullet-1">
Expand Down
105 changes: 105 additions & 0 deletions tests/agent_criss_cross/gen_graph_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import networkx as nx


def generate_graph(grid_dim, num_agents_per_team, base_loc, start_loc, obstacle_loc):
# Check parameters
assert len(base_loc) == 2, 'There are only two bases--supply two locations'
assert len(start_loc) == 2, 'There are only two teams--supply lists of start positions for each team in a nested list'
assert len(start_loc[0]) == num_agents_per_team and len(start_loc[1]) == num_agents_per_team, 'Supply correct number of start locations'

g = nx.DiGraph()

# Game variables
game_height = grid_dim
game_width = grid_dim

# =======================================================================
# Add a node for each grid location in the game
nodes = list(range(0, game_height * game_width))
g.add_nodes_from([str(node) for node in nodes], blocked='0,0')

# =======================================================================
# Add edges connecting each of the grid nodes. Add up, down, left, right attributes to correct edges
# Right edges
for node in g.nodes:
if (int(node) + 1) % game_width != 0:
g.add_edge(node, str(int(node) + 1), right=1)

# Left edges
for node in g.nodes:
if int(node) % game_width != 0:
g.add_edge(node, str(int(node) - 1), left=1)

# Up edges
for node in g.nodes:
if (int(node) // game_width) + 1 != game_height:
g.add_edge(node, str(int(node) + game_width), up=1)

# Down edges
for node in g.nodes:
if int(node) // game_width != 0:
g.add_edge(node, str(int(node) - game_width), down=1)

# Add edges between border nodes and end nodes to demarcate the end of the grid world. Bullets will disappear after it crosses this end
g.add_node('end', blocked=1)
# Bottom border
for i in range(game_width):
g.add_edge(f'{i}', 'end', down=1)
# Top border
for i in range(game_width):
g.add_edge(f'{i + game_width * (game_height - 1)}', 'end', up=1)
# Left border
for i in range(game_height):
g.add_edge(f'{i * game_width}', 'end', left=1)
# Right border
for i in range(game_height):
g.add_edge(f'{i * game_width + game_width - 1}', 'end', right=1)

# =======================================================================
# Add the bases and connect them to the correct location: bottom right and top left
g.add_node('red-base')
g.add_node('blue-base')
g.add_edge('red-base', str(base_loc[0]), atLoc=1)
g.add_edge('blue-base', str(base_loc[1]), atLoc=1)

# =======================================================================
# Add mountains and obstacle attributes
mountain_loc = obstacle_loc
g.add_node('mountain', isMountain=1)
for i in mountain_loc:
g.add_edge(str(i), 'mountain', atLoc=1)

# =======================================================================
# Initialize players health, action choice and team
for i in range(1, num_agents_per_team + 1):
g.add_node(f'red-soldier-{i}', health=1, moveUp=0, moveDown=0, moveLeft=0, moveRight=0, shootUpRed=0,
shootDownRed=0, shootLeftRed=0, shootRightRed=0, teamRed=1, justDied='0,0')
g.add_node(f'blue-soldier-{i}', health=1, moveUp=0, moveDown=0, moveLeft=0, moveRight=0, shootUpBlue=0,
shootDownBlue=0, shootLeftBlue=0, shootRightBlue=0, teamBlue=1, justDied='0,0')
# Teams
g.add_edge(f'red-soldier-{i}', 'red-base', team=1)
g.add_edge(f'blue-soldier-{i}', 'blue-base', team=1)
# Soldier Start Locations (dual edge)
g.add_edge(f'red-soldier-{i}', str(start_loc[0][i - 1]), atLoc=1)
g.add_edge(f'blue-soldier-{i}', str(start_loc[1][i - 1]), atLoc=1)
g.add_edge(str(start_loc[0][i - 1]), f'red-soldier-{i}', atLoc=1)
g.add_edge(str(start_loc[1][i - 1]), f'blue-soldier-{i}', atLoc=1)
# Bullets
g.add_node(f'red-bullet-{i}', teamRed=1, bullet=1)
g.add_node(f'blue-bullet-{i}', teamBlue=1, bullet=1)
g.add_edge(f'red-soldier-{i}', f'red-bullet-{i}', bullet=1)
g.add_edge(f'blue-soldier-{i}', f'blue-bullet-{i}', bullet=1)

# =======================================================================

nx.write_graphml_lxml(g, 'pyreason_gym/pyreason_grid_world/graph/game_graph.graphml', named_key_ids=True)


def main():
## Red is first then Blue
generate_graph(grid_dim=8, num_agents_per_team=1, base_loc=[7, 56], start_loc=[[2], [5]],
obstacle_loc=[26, 27, 34, 35, 36, 44])


if __name__ == '__main__':
main()
16 changes: 16 additions & 0 deletions tests/agent_criss_cross/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pyreason_gym
import gym
import time

env = gym.make('PyReasonGridWorld-v0', render_mode='human')
obs = env.reset()

for _ in range(8):
action = {
'red_team': [3],
'blue_team': [2]
}
obs = env.step(action)
print(obs)
time.sleep(1)
env.close()
105 changes: 105 additions & 0 deletions tests/agent_same_location_shoot/gen_graph_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import networkx as nx


def generate_graph(grid_dim, num_agents_per_team, base_loc, start_loc, obstacle_loc):
# Check parameters
assert len(base_loc) == 2, 'There are only two bases--supply two locations'
assert len(start_loc) == 2, 'There are only two teams--supply lists of start positions for each team in a nested list'
assert len(start_loc[0]) == num_agents_per_team and len(start_loc[1]) == num_agents_per_team, 'Supply correct number of start locations'

g = nx.DiGraph()

# Game variables
game_height = grid_dim
game_width = grid_dim

# =======================================================================
# Add a node for each grid location in the game
nodes = list(range(0, game_height * game_width))
g.add_nodes_from([str(node) for node in nodes], blocked='0,0')

# =======================================================================
# Add edges connecting each of the grid nodes. Add up, down, left, right attributes to correct edges
# Right edges
for node in g.nodes:
if (int(node) + 1) % game_width != 0:
g.add_edge(node, str(int(node) + 1), right=1)

# Left edges
for node in g.nodes:
if int(node) % game_width != 0:
g.add_edge(node, str(int(node) - 1), left=1)

# Up edges
for node in g.nodes:
if (int(node) // game_width) + 1 != game_height:
g.add_edge(node, str(int(node) + game_width), up=1)

# Down edges
for node in g.nodes:
if int(node) // game_width != 0:
g.add_edge(node, str(int(node) - game_width), down=1)

# Add edges between border nodes and end nodes to demarcate the end of the grid world. Bullets will disappear after it crosses this end
g.add_node('end', blocked=1)
# Bottom border
for i in range(game_width):
g.add_edge(f'{i}', 'end', down=1)
# Top border
for i in range(game_width):
g.add_edge(f'{i + game_width * (game_height - 1)}', 'end', up=1)
# Left border
for i in range(game_height):
g.add_edge(f'{i * game_width}', 'end', left=1)
# Right border
for i in range(game_height):
g.add_edge(f'{i * game_width + game_width - 1}', 'end', right=1)

# =======================================================================
# Add the bases and connect them to the correct location: bottom right and top left
g.add_node('red-base')
g.add_node('blue-base')
g.add_edge('red-base', str(base_loc[0]), atLoc=1)
g.add_edge('blue-base', str(base_loc[1]), atLoc=1)

# =======================================================================
# Add mountains and obstacle attributes
mountain_loc = obstacle_loc
g.add_node('mountain', isMountain=1)
for i in mountain_loc:
g.add_edge(str(i), 'mountain', atLoc=1)

# =======================================================================
# Initialize players health, action choice and team
for i in range(1, num_agents_per_team + 1):
g.add_node(f'red-soldier-{i}', health=1, moveUp=0, moveDown=0, moveLeft=0, moveRight=0, shootUpRed=0,
shootDownRed=0, shootLeftRed=0, shootRightRed=0, teamRed=1, justDied='0,0')
g.add_node(f'blue-soldier-{i}', health=1, moveUp=0, moveDown=0, moveLeft=0, moveRight=0, shootUpBlue=0,
shootDownBlue=0, shootLeftBlue=0, shootRightBlue=0, teamBlue=1, justDied='0,0')
# Teams
g.add_edge(f'red-soldier-{i}', 'red-base', team=1)
g.add_edge(f'blue-soldier-{i}', 'blue-base', team=1)
# Soldier Start Locations (dual edge)
g.add_edge(f'red-soldier-{i}', str(start_loc[0][i - 1]), atLoc=1)
g.add_edge(f'blue-soldier-{i}', str(start_loc[1][i - 1]), atLoc=1)
g.add_edge(str(start_loc[0][i - 1]), f'red-soldier-{i}', atLoc=1)
g.add_edge(str(start_loc[1][i - 1]), f'blue-soldier-{i}', atLoc=1)
# Bullets
g.add_node(f'red-bullet-{i}', teamRed=1, bullet=1)
g.add_node(f'blue-bullet-{i}', teamBlue=1, bullet=1)
g.add_edge(f'red-soldier-{i}', f'red-bullet-{i}', bullet=1)
g.add_edge(f'blue-soldier-{i}', f'blue-bullet-{i}', bullet=1)

# =======================================================================

nx.write_graphml_lxml(g, 'pyreason_gym/pyreason_grid_world/graph/game_graph.graphml', named_key_ids=True)


def main():
## Red is first then Blue
generate_graph(grid_dim=8, num_agents_per_team=1, base_loc=[7, 56], start_loc=[[2], [3]],
obstacle_loc=[26, 27, 34, 35, 36, 44])


if __name__ == '__main__':
main()
28 changes: 28 additions & 0 deletions tests/agent_same_location_shoot/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import pyreason_gym
import gym
import time

env = gym.make('PyReasonGridWorld-v0', render_mode='human')
obs = env.reset()
action = {
'red_team': [3],
'blue_team': [1]
}
obs = env.step(action)
print(obs)
time.sleep(1)
action = {
'red_team': [4],
'blue_team': [1]
}
obs = env.step(action)
print(obs)
time.sleep(1)
action = {
'red_team': [3],
'blue_team': [1]
}
obs = env.step(action)
print(obs)
time.sleep(1)
env.close()
105 changes: 105 additions & 0 deletions tests/base_done_check/gen_graph_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import networkx as nx


def generate_graph(grid_dim, num_agents_per_team, base_loc, start_loc, obstacle_loc):
# Check parameters
assert len(base_loc) == 2, 'There are only two bases--supply two locations'
assert len(start_loc) == 2, 'There are only two teams--supply lists of start positions for each team in a nested list'
assert len(start_loc[0]) == num_agents_per_team and len(start_loc[1]) == num_agents_per_team, 'Supply correct number of start locations'

g = nx.DiGraph()

# Game variables
game_height = grid_dim
game_width = grid_dim

# =======================================================================
# Add a node for each grid location in the game
nodes = list(range(0, game_height * game_width))
g.add_nodes_from([str(node) for node in nodes], blocked='0,0')

# =======================================================================
# Add edges connecting each of the grid nodes. Add up, down, left, right attributes to correct edges
# Right edges
for node in g.nodes:
if (int(node) + 1) % game_width != 0:
g.add_edge(node, str(int(node) + 1), right=1)

# Left edges
for node in g.nodes:
if int(node) % game_width != 0:
g.add_edge(node, str(int(node) - 1), left=1)

# Up edges
for node in g.nodes:
if (int(node) // game_width) + 1 != game_height:
g.add_edge(node, str(int(node) + game_width), up=1)

# Down edges
for node in g.nodes:
if int(node) // game_width != 0:
g.add_edge(node, str(int(node) - game_width), down=1)

# Add edges between border nodes and end nodes to demarcate the end of the grid world. Bullets will disappear after it crosses this end
g.add_node('end', blocked=1)
# Bottom border
for i in range(game_width):
g.add_edge(f'{i}', 'end', down=1)
# Top border
for i in range(game_width):
g.add_edge(f'{i + game_width * (game_height - 1)}', 'end', up=1)
# Left border
for i in range(game_height):
g.add_edge(f'{i * game_width}', 'end', left=1)
# Right border
for i in range(game_height):
g.add_edge(f'{i * game_width + game_width - 1}', 'end', right=1)

# =======================================================================
# Add the bases and connect them to the correct location: bottom right and top left
g.add_node('red-base')
g.add_node('blue-base')
g.add_edge('red-base', str(base_loc[0]), atLoc=1)
g.add_edge('blue-base', str(base_loc[1]), atLoc=1)

# =======================================================================
# Add mountains and obstacle attributes
mountain_loc = obstacle_loc
g.add_node('mountain', isMountain=1)
for i in mountain_loc:
g.add_edge(str(i), 'mountain', atLoc=1)

# =======================================================================
# Initialize players health, action choice and team
for i in range(1, num_agents_per_team + 1):
g.add_node(f'red-soldier-{i}', health=1, moveUp=0, moveDown=0, moveLeft=0, moveRight=0, shootUpRed=0,
shootDownRed=0, shootLeftRed=0, shootRightRed=0, teamRed=1, justDied='0,0')
g.add_node(f'blue-soldier-{i}', health=1, moveUp=0, moveDown=0, moveLeft=0, moveRight=0, shootUpBlue=0,
shootDownBlue=0, shootLeftBlue=0, shootRightBlue=0, teamBlue=1, justDied='0,0')
# Teams
g.add_edge(f'red-soldier-{i}', 'red-base', team=1)
g.add_edge(f'blue-soldier-{i}', 'blue-base', team=1)
# Soldier Start Locations (dual edge)
g.add_edge(f'red-soldier-{i}', str(start_loc[0][i - 1]), atLoc=1)
g.add_edge(f'blue-soldier-{i}', str(start_loc[1][i - 1]), atLoc=1)
g.add_edge(str(start_loc[0][i - 1]), f'red-soldier-{i}', atLoc=1)
g.add_edge(str(start_loc[1][i - 1]), f'blue-soldier-{i}', atLoc=1)
# Bullets
g.add_node(f'red-bullet-{i}', teamRed=1, bullet=1)
g.add_node(f'blue-bullet-{i}', teamBlue=1, bullet=1)
g.add_edge(f'red-soldier-{i}', f'red-bullet-{i}', bullet=1)
g.add_edge(f'blue-soldier-{i}', f'blue-bullet-{i}', bullet=1)

# =======================================================================

nx.write_graphml_lxml(g, 'pyreason_gym/pyreason_grid_world/graph/game_graph.graphml', named_key_ids=True)


def main():
## Red is first then Blue
generate_graph(grid_dim=8, num_agents_per_team=1, base_loc=[7, 56], start_loc=[[2], [15]],
obstacle_loc=[26, 27, 34, 35, 36, 44])


if __name__ == '__main__':
main()
Loading

0 comments on commit fb54032

Please sign in to comment.