BFS
This commit is contained in:
parent
cfeae8327b
commit
da2c86892d
54
agent.py
54
agent.py
@ -28,40 +28,50 @@ class Instance:
|
|||||||
def set_tank_capacity(self, fuel_units):
|
def set_tank_capacity(self, fuel_units):
|
||||||
self.tank_capacity = fuel_units
|
self.tank_capacity = fuel_units
|
||||||
|
|
||||||
def move(self):
|
def move(self, action):
|
||||||
key_pressed = pygame.key.get_pressed()
|
key_pressed = pygame.key.get_pressed()
|
||||||
height = settings.Pygame.height()
|
height = settings.Pygame.height()
|
||||||
width = settings.Pygame.width()
|
width = settings.Pygame.width()
|
||||||
tile_size = settings.Field.size()
|
tile_size = settings.Field.size()
|
||||||
|
|
||||||
if key_pressed[pygame.K_UP] and self.direction == 'west' and self.rect.x > 0:
|
if key_pressed[pygame.K_UP] and self.direction == 4 and self.rect.x > 0 and action is None:
|
||||||
self.rect.x -= tile_size
|
self.rect.x -= tile_size
|
||||||
elif key_pressed[pygame.K_UP] and self.direction == 'east' and self.rect.x < width - tile_size:
|
elif key_pressed[pygame.K_UP] and self.direction == 2 and self.rect.x < width - tile_size and action is None:
|
||||||
self.rect.x += tile_size
|
self.rect.x += tile_size
|
||||||
elif key_pressed[pygame.K_UP] and self.direction == 'north' and self.rect.y > 0:
|
elif key_pressed[pygame.K_UP] and self.direction == 1 and self.rect.y > 0 and action is None:
|
||||||
self.rect.y -= tile_size
|
self.rect.y -= tile_size
|
||||||
elif key_pressed[pygame.K_UP] and self.direction == 'south' and self.rect.y < height - tile_size:
|
elif key_pressed[pygame.K_UP] and self.direction == 3 and self.rect.y < height - tile_size and action is None:
|
||||||
|
self.rect.y += tile_size
|
||||||
|
elif action == 'f' and self.direction == 4 and self.rect.x > 0:
|
||||||
|
self.rect.x -= tile_size
|
||||||
|
elif action == 'f' and self.direction == 2 and self.rect.x < width - tile_size:
|
||||||
|
self.rect.x += tile_size
|
||||||
|
elif action == 'f' and self.direction == 1 and self.rect.y > 0:
|
||||||
|
self.rect.y -= tile_size
|
||||||
|
elif action == 'f' and self.direction == 3 and self.rect.y < height - tile_size:
|
||||||
self.rect.y += tile_size
|
self.rect.y += tile_size
|
||||||
elif key_pressed[pygame.K_SPACE]:
|
elif key_pressed[pygame.K_SPACE]:
|
||||||
return 'open_window'
|
return 'open_window'
|
||||||
return 'none'
|
return 'none'
|
||||||
|
|
||||||
def rotate(self):
|
def rotate(self, action):
|
||||||
key_pressed = pygame.key.get_pressed()
|
key_pressed = pygame.key.get_pressed()
|
||||||
if key_pressed[pygame.K_LEFT] and self.direction == 'east':
|
if key_pressed[pygame.K_LEFT] and self.direction == 2 or action == 'l' and self.direction == 2:
|
||||||
self.direction = 'north'
|
self.direction = 1
|
||||||
elif key_pressed[pygame.K_LEFT] and self.direction == 'north':
|
elif key_pressed[pygame.K_LEFT] and self.direction == 1 or action == 'l' and self.direction == 1:
|
||||||
self.direction = 'west'
|
self.direction = 4
|
||||||
elif key_pressed[pygame.K_LEFT] and self.direction == 'west':
|
elif key_pressed[pygame.K_LEFT] and self.direction == 4 or action == 'l' and self.direction == 4:
|
||||||
self.direction = 'south'
|
self.direction = 3
|
||||||
elif key_pressed[pygame.K_LEFT] and self.direction == 'south':
|
elif key_pressed[pygame.K_LEFT] and self.direction == 3 or action == 'l' and self.direction == 3:
|
||||||
self.direction = 'east'
|
self.direction = 2
|
||||||
elif key_pressed[pygame.K_RIGHT] and self.direction == 'north':
|
elif key_pressed[pygame.K_RIGHT] and self.direction == 1 or action == 'r' and self.direction == 1:
|
||||||
self.direction = 'east'
|
self.direction = 2
|
||||||
elif key_pressed[pygame.K_RIGHT] and self.direction == 'west':
|
elif key_pressed[pygame.K_RIGHT] and self.direction == 4 or action == 'r' and self.direction == 4:
|
||||||
self.direction = 'north'
|
self.direction = 1
|
||||||
elif key_pressed[pygame.K_RIGHT] and self.direction == 'south':
|
elif key_pressed[pygame.K_RIGHT] and self.direction == 3 or action == 'r' and self.direction == 3:
|
||||||
self.direction = 'west'
|
self.direction = 4
|
||||||
elif key_pressed[pygame.K_RIGHT] and self.direction == 'east':
|
elif key_pressed[pygame.K_RIGHT] and self.direction == 2 or action == 'r' and self.direction == 2:
|
||||||
self.direction = 'south'
|
self.direction = 3
|
||||||
return 'none'
|
return 'none'
|
||||||
|
|
||||||
|
|
||||||
|
207
graph.py
207
graph.py
@ -1,100 +1,151 @@
|
|||||||
|
import copy
|
||||||
import settings
|
import settings
|
||||||
import math
|
|
||||||
from collections import defaultdict
|
|
||||||
|
|
||||||
|
|
||||||
class Instance:
|
class State:
|
||||||
def __init__(self):
|
def __init__(self, direction, x, y):
|
||||||
self.graph = defaultdict(list)
|
self.direction = direction
|
||||||
self.directions_dict = defaultdict(list)
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
|
||||||
def config(self, fields):
|
def get_direction(self):
|
||||||
adjacency_list = self.__generate_graph()
|
return self.direction
|
||||||
nodes_to_visit = self.generate_target_fields(fields)
|
|
||||||
shortest_path_bfs = self.generate_shortest_path_for_all_targets_bfs(nodes_to_visit)
|
|
||||||
|
|
||||||
print("Adjacency list: " + str(dict(adjacency_list)))
|
def set_direction(self, direction):
|
||||||
print("List of targets: " + str(nodes_to_visit))
|
self.direction = direction
|
||||||
print("The shortest path found by bfs algorithm: " + str(shortest_path_bfs))
|
|
||||||
|
|
||||||
def generate_shortest_path_for_all_targets_bfs(self, nodes_to_visit):
|
def get_x(self):
|
||||||
shortest_bfs_path = []
|
return self.x
|
||||||
start_node = 0
|
|
||||||
|
|
||||||
for target_node in nodes_to_visit:
|
def set_x(self, x):
|
||||||
shortest_bfs_path.extend(self.__shortest_path_bfs(start_node, target_node)[1:])
|
self.x = x
|
||||||
start_node = target_node
|
|
||||||
|
|
||||||
return shortest_bfs_path
|
def get_y(self):
|
||||||
|
return self.y
|
||||||
|
|
||||||
# private
|
def set_y(self, y):
|
||||||
def __add_edge(self, u, v):
|
self.y = y
|
||||||
self.graph[u].append(v)
|
|
||||||
|
|
||||||
def __generate_graph(self):
|
|
||||||
width = settings.Field.horizontal_count()
|
|
||||||
height = settings.Field.vertical_count()
|
|
||||||
|
|
||||||
for i in range(height):
|
class Node:
|
||||||
for j in range(width):
|
def __init__(self, action, direction, parent, x, y):
|
||||||
point = i * width + j
|
self.action = action
|
||||||
point_left = i * width + j - 1
|
self.direction = direction
|
||||||
point_left_column = point_left - width * i
|
self.parent = parent
|
||||||
point_right = i * width + j + 1
|
self.x = x
|
||||||
point_right_column = point_right % width
|
self.y = y
|
||||||
point_up = (i - 1) * width + j
|
|
||||||
point_up_row = math.floor(point_up / width)
|
|
||||||
point_down = (i + 1) * width + j
|
|
||||||
point_down_row = math.floor(point_down / width)
|
|
||||||
|
|
||||||
if point_left_column >= 0:
|
def get_action(self):
|
||||||
self.__add_edge(point, point_left)
|
return self.action
|
||||||
if point_right_column > 0:
|
|
||||||
self.__add_edge(point, point_right)
|
|
||||||
if point_up_row >= 0:
|
|
||||||
self.__add_edge(point, point_up)
|
|
||||||
if point_down_row < height:
|
|
||||||
self.__add_edge(point, point_down)
|
|
||||||
|
|
||||||
return self.graph
|
def set_action(self, action):
|
||||||
|
self.action = action
|
||||||
|
|
||||||
def __shortest_path_bfs(self, start_node, target_node):
|
def get_direction(self):
|
||||||
path_list = [[start_node]]
|
return self.direction
|
||||||
path_index = 0
|
|
||||||
visited_nodes = {start_node}
|
|
||||||
|
|
||||||
if start_node == target_node:
|
def set_direction(self, direction):
|
||||||
return path_list[0]
|
self.direction = direction
|
||||||
|
|
||||||
while path_index < len(path_list):
|
def get_parent(self):
|
||||||
current_path = path_list[path_index]
|
return self.parent
|
||||||
prev_node = current_path[-1]
|
|
||||||
next_nodes = self.graph[prev_node]
|
|
||||||
|
|
||||||
if target_node in next_nodes:
|
def set_parent(self, parent):
|
||||||
current_path.append(target_node)
|
self.parent = parent
|
||||||
return current_path
|
|
||||||
|
|
||||||
for next_node in next_nodes:
|
def get_x(self):
|
||||||
if not next_node in visited_nodes:
|
return self.x
|
||||||
new_path = current_path[:]
|
|
||||||
new_path.append(next_node)
|
|
||||||
path_list.append(new_path)
|
|
||||||
visited_nodes.add(next_node)
|
|
||||||
|
|
||||||
path_index += 1
|
def set_x(self, x):
|
||||||
|
self.x = x
|
||||||
|
|
||||||
|
def get_y(self):
|
||||||
|
return self.y
|
||||||
|
|
||||||
|
def set_y(self, y):
|
||||||
|
self.y = y
|
||||||
|
|
||||||
|
|
||||||
|
def goal_test(goaltest,elem):
|
||||||
|
if elem.get_x() == goaltest[0] and elem.get_y() == goaltest[1]:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# static
|
|
||||||
@staticmethod
|
def graphsearch(explored, fringe, goaltest, istate, succ):
|
||||||
def generate_target_fields(fields):
|
node = Node(None, istate.get_direction(), None, istate.get_x(),
|
||||||
width = settings.Field.horizontal_count()
|
istate.get_y())
|
||||||
height = settings.Field.vertical_count()
|
fringe.append(node)
|
||||||
fields_to_visit = []
|
while True:
|
||||||
for i in range(height):
|
if not fringe:
|
||||||
for j in range(width):
|
return False
|
||||||
point = i * width + j
|
elem = fringe.pop(0)
|
||||||
if fields[j][i] == 'dirt':
|
temp = copy.copy(elem)
|
||||||
fields_to_visit.append(point)
|
if goal_test(goaltest,
|
||||||
return fields_to_visit
|
elem) is True:
|
||||||
|
return get_moves_list(elem)
|
||||||
|
explored.append(elem)
|
||||||
|
for (action, state) in succ(
|
||||||
|
temp):
|
||||||
|
fringe_tuple = []
|
||||||
|
explored_tuple = []
|
||||||
|
for x in fringe:
|
||||||
|
fringe_tuple.append((x.get_direction(), x.get_x(), x.get_y()))
|
||||||
|
for x in explored:
|
||||||
|
explored_tuple.append((x.get_direction(), x.get_x(), x.get_y()))
|
||||||
|
if state not in fringe_tuple and state not in explored_tuple:
|
||||||
|
x = Node(action, state[0], elem, state[1],
|
||||||
|
state[2])
|
||||||
|
fringe.append(x)
|
||||||
|
|
||||||
|
|
||||||
|
def get_moves_list(node):
|
||||||
|
moves_list = []
|
||||||
|
while (node.get_parent() != None):
|
||||||
|
moves_list.append(node.get_action())
|
||||||
|
node = node.get_parent()
|
||||||
|
moves_list.reverse()
|
||||||
|
return moves_list
|
||||||
|
|
||||||
|
|
||||||
|
def succ(node):
|
||||||
|
actions_list = []
|
||||||
|
direction = copy.copy(node.get_direction())
|
||||||
|
if direction == 1:
|
||||||
|
direction = 4
|
||||||
|
else:
|
||||||
|
direction = direction - 1
|
||||||
|
actions_list.append(("l", (direction, node.get_x(), node.get_y())))
|
||||||
|
direction = copy.copy(node.get_direction())
|
||||||
|
if direction == 4:
|
||||||
|
direction = 1
|
||||||
|
else:
|
||||||
|
direction = direction + 1
|
||||||
|
actions_list.append(("r", (direction, node.get_x(), node.get_y())))
|
||||||
|
temp_move_south = node.get_y() + 1
|
||||||
|
temp_move_west = node.get_x() - 1
|
||||||
|
temp_move_east = node.get_x() + 1
|
||||||
|
temp_move_north = node.get_y() - 1
|
||||||
|
if is_move_allowed(node) == "x + 1":
|
||||||
|
actions_list.append(("f", (node.get_direction(), temp_move_east, node.get_y())))
|
||||||
|
elif is_move_allowed(node) == "y - 1":
|
||||||
|
actions_list.append(("f", (node.get_direction(), node.get_x(), temp_move_north)))
|
||||||
|
elif is_move_allowed(node) == "y + 1":
|
||||||
|
actions_list.append(("f", (node.get_direction(), node.get_x(), temp_move_south)))
|
||||||
|
elif is_move_allowed(node) == "x - 1":
|
||||||
|
actions_list.append(("f", (node.get_direction(), temp_move_west, node.get_y())))
|
||||||
|
return actions_list
|
||||||
|
|
||||||
|
|
||||||
|
def is_move_allowed(node):
|
||||||
|
if node.get_direction() == 2 and node.get_x() + 1 < settings.Pygame.width():
|
||||||
|
return "x + 1"
|
||||||
|
elif node.get_direction() == 1 and node.get_y() - 1 >= 0:
|
||||||
|
return "y - 1"
|
||||||
|
elif node.get_direction() == 3 and node.get_y() + 1 < settings.Pygame.height():
|
||||||
|
return "y + 1"
|
||||||
|
elif node.get_direction() == 4 and node.get_x() - 1 >= 0:
|
||||||
|
return "x - 1"
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
41
main.py
41
main.py
@ -6,8 +6,7 @@ import settings
|
|||||||
import common
|
import common
|
||||||
import agent
|
import agent
|
||||||
import math
|
import math
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
possibleFields = {
|
possibleFields = {
|
||||||
'dirt': field.Dirt(),
|
'dirt': field.Dirt(),
|
||||||
@ -32,13 +31,13 @@ def randomize_map():
|
|||||||
for j in range(height):
|
for j in range(height):
|
||||||
# k = random.choice(list(possibleFields.keys()))
|
# k = random.choice(list(possibleFields.keys()))
|
||||||
x = random.uniform(0, 100)
|
x = random.uniform(0, 100)
|
||||||
if x < 80:
|
if x < 4:
|
||||||
field_array_small.append(possibleFields['dirt'].tile.object)
|
field_array_small.append(possibleFields['dirt'].tile.object)
|
||||||
field_array_small_2.append('dirt')
|
field_array_small_2.append('dirt')
|
||||||
elif 80 < x < 90:
|
elif 4 < x < 55:
|
||||||
field_array_small.append(possibleFields['sand'].tile.object)
|
field_array_small.append(possibleFields['sand'].tile.object)
|
||||||
field_array_small_2.append('sand')
|
field_array_small_2.append('sand')
|
||||||
elif 90 < x < 100:
|
elif 55 < x < 100:
|
||||||
field_array_small.append(possibleFields['grass'].tile.object)
|
field_array_small.append(possibleFields['grass'].tile.object)
|
||||||
field_array_small_2.append('grass')
|
field_array_small_2.append('grass')
|
||||||
field_array_big.append(field_array_small)
|
field_array_big.append(field_array_small)
|
||||||
@ -88,19 +87,19 @@ def draw_window(agent, fields):
|
|||||||
for i in range(settings.Field.horizontal_count()):
|
for i in range(settings.Field.horizontal_count()):
|
||||||
for j in range(settings.Field.vertical_count()):
|
for j in range(settings.Field.vertical_count()):
|
||||||
window.blit(fields[i][j], (i * settings.Field.size(), j * settings.Field.size()))
|
window.blit(fields[i][j], (i * settings.Field.size(), j * settings.Field.size()))
|
||||||
if agent.direction == 'east':
|
if agent.direction == 2:
|
||||||
AGENT_IMG = pygame.image.load('./assets/tracktor/tractor_east.png')
|
AGENT_IMG = pygame.image.load('./assets/tracktor/tractor_east.png')
|
||||||
AGENT = pygame.transform.scale(AGENT_IMG, (settings.Field.size(), settings.Field.size()))
|
AGENT = pygame.transform.scale(AGENT_IMG, (settings.Field.size(), settings.Field.size()))
|
||||||
window.blit(AGENT, (rect.x, rect.y))
|
window.blit(AGENT, (rect.x, rect.y))
|
||||||
elif agent.direction == 'west':
|
elif agent.direction == 4:
|
||||||
AGENT_IMG = pygame.image.load('./assets/tracktor/tractor_west.png')
|
AGENT_IMG = pygame.image.load('./assets/tracktor/tractor_west.png')
|
||||||
AGENT = pygame.transform.scale(AGENT_IMG, (settings.Field.size(), settings.Field.size()))
|
AGENT = pygame.transform.scale(AGENT_IMG, (settings.Field.size(), settings.Field.size()))
|
||||||
window.blit(AGENT, (rect.x, rect.y))
|
window.blit(AGENT, (rect.x, rect.y))
|
||||||
elif agent.direction == 'north':
|
elif agent.direction == 1:
|
||||||
AGENT_IMG = pygame.image.load('./assets/tracktor/tractor_north.png')
|
AGENT_IMG = pygame.image.load('./assets/tracktor/tractor_north.png')
|
||||||
AGENT = pygame.transform.scale(AGENT_IMG, (settings.Field.size(), settings.Field.size()))
|
AGENT = pygame.transform.scale(AGENT_IMG, (settings.Field.size(), settings.Field.size()))
|
||||||
window.blit(AGENT, (rect.x, rect.y))
|
window.blit(AGENT, (rect.x, rect.y))
|
||||||
elif agent.direction == 'south':
|
elif agent.direction == 3:
|
||||||
AGENT_IMG = pygame.image.load('./assets/tracktor/tractor_south.png')
|
AGENT_IMG = pygame.image.load('./assets/tracktor/tractor_south.png')
|
||||||
AGENT = pygame.transform.scale(AGENT_IMG, (settings.Field.size(), settings.Field.size()))
|
AGENT = pygame.transform.scale(AGENT_IMG, (settings.Field.size(), settings.Field.size()))
|
||||||
window.blit(AGENT, (rect.x, rect.y))
|
window.blit(AGENT, (rect.x, rect.y))
|
||||||
@ -108,8 +107,7 @@ def draw_window(agent, fields):
|
|||||||
|
|
||||||
|
|
||||||
common = common.Instance()
|
common = common.Instance()
|
||||||
agent = agent.Instance(1000, 'east')
|
agent = agent.Instance(1000, 2)
|
||||||
graph = graph.Instance()
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@ -122,22 +120,31 @@ def main():
|
|||||||
settings.Pygame.height())))
|
settings.Pygame.height())))
|
||||||
|
|
||||||
pygame.display.set_caption(settings.Pygame.display_name())
|
pygame.display.set_caption(settings.Pygame.display_name())
|
||||||
|
|
||||||
fields, fields_2 = randomize_map()
|
fields, fields_2 = randomize_map()
|
||||||
graph.config(fields_2)
|
state = graph.State(2, 0, 0)
|
||||||
|
move_list = (graph.graphsearch([], [], [10,10] , state, graph.succ))
|
||||||
|
print(move_list)
|
||||||
|
x = True
|
||||||
while common.get('game_running'):
|
while common.get('game_running'):
|
||||||
pygame.time.Clock().tick(settings.Pygame.fps())
|
pygame.time.Clock().tick(settings.Pygame.fps())
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
common.set('game_running', False)
|
common.set('game_running', False)
|
||||||
|
|
||||||
if common.get('state_imgShown'):
|
if common.get('state_imgShown'):
|
||||||
read_img(agent, fields)
|
read_img(agent, fields)
|
||||||
else:
|
else:
|
||||||
draw_window(agent, fields)
|
draw_window(agent, fields)
|
||||||
agent_action(agent.rotate())
|
if x:
|
||||||
agent_action(agent.move())
|
for action in move_list:
|
||||||
|
if action == 'l' or action == 'r':
|
||||||
|
agent_action(agent.rotate(action))
|
||||||
|
elif action == 'f':
|
||||||
|
agent_action(agent.move(action))
|
||||||
|
draw_window(agent, fields)
|
||||||
|
time.sleep(0.5)
|
||||||
|
x = False
|
||||||
|
agent_action(agent.rotate(None))
|
||||||
|
agent_action(agent.move(None))
|
||||||
|
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user