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):
|
||||
self.tank_capacity = fuel_units
|
||||
|
||||
def move(self):
|
||||
def move(self, action):
|
||||
key_pressed = pygame.key.get_pressed()
|
||||
height = settings.Pygame.height()
|
||||
width = settings.Pygame.width()
|
||||
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
|
||||
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
|
||||
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
|
||||
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
|
||||
elif key_pressed[pygame.K_SPACE]:
|
||||
return 'open_window'
|
||||
return 'none'
|
||||
|
||||
def rotate(self):
|
||||
def rotate(self, action):
|
||||
key_pressed = pygame.key.get_pressed()
|
||||
if key_pressed[pygame.K_LEFT] and self.direction == 'east':
|
||||
self.direction = 'north'
|
||||
elif key_pressed[pygame.K_LEFT] and self.direction == 'north':
|
||||
self.direction = 'west'
|
||||
elif key_pressed[pygame.K_LEFT] and self.direction == 'west':
|
||||
self.direction = 'south'
|
||||
elif key_pressed[pygame.K_LEFT] and self.direction == 'south':
|
||||
self.direction = 'east'
|
||||
elif key_pressed[pygame.K_RIGHT] and self.direction == 'north':
|
||||
self.direction = 'east'
|
||||
elif key_pressed[pygame.K_RIGHT] and self.direction == 'west':
|
||||
self.direction = 'north'
|
||||
elif key_pressed[pygame.K_RIGHT] and self.direction == 'south':
|
||||
self.direction = 'west'
|
||||
elif key_pressed[pygame.K_RIGHT] and self.direction == 'east':
|
||||
self.direction = 'south'
|
||||
if key_pressed[pygame.K_LEFT] and self.direction == 2 or action == 'l' and self.direction == 2:
|
||||
self.direction = 1
|
||||
elif key_pressed[pygame.K_LEFT] and self.direction == 1 or action == 'l' and self.direction == 1:
|
||||
self.direction = 4
|
||||
elif key_pressed[pygame.K_LEFT] and self.direction == 4 or action == 'l' and self.direction == 4:
|
||||
self.direction = 3
|
||||
elif key_pressed[pygame.K_LEFT] and self.direction == 3 or action == 'l' and self.direction == 3:
|
||||
self.direction = 2
|
||||
elif key_pressed[pygame.K_RIGHT] and self.direction == 1 or action == 'r' and self.direction == 1:
|
||||
self.direction = 2
|
||||
elif key_pressed[pygame.K_RIGHT] and self.direction == 4 or action == 'r' and self.direction == 4:
|
||||
self.direction = 1
|
||||
elif key_pressed[pygame.K_RIGHT] and self.direction == 3 or action == 'r' and self.direction == 3:
|
||||
self.direction = 4
|
||||
elif key_pressed[pygame.K_RIGHT] and self.direction == 2 or action == 'r' and self.direction == 2:
|
||||
self.direction = 3
|
||||
return 'none'
|
||||
|
||||
|
||||
|
207
graph.py
207
graph.py
@ -1,100 +1,151 @@
|
||||
import copy
|
||||
import settings
|
||||
import math
|
||||
from collections import defaultdict
|
||||
|
||||
|
||||
class Instance:
|
||||
def __init__(self):
|
||||
self.graph = defaultdict(list)
|
||||
self.directions_dict = defaultdict(list)
|
||||
class State:
|
||||
def __init__(self, direction, x, y):
|
||||
self.direction = direction
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
def config(self, fields):
|
||||
adjacency_list = self.__generate_graph()
|
||||
nodes_to_visit = self.generate_target_fields(fields)
|
||||
shortest_path_bfs = self.generate_shortest_path_for_all_targets_bfs(nodes_to_visit)
|
||||
def get_direction(self):
|
||||
return self.direction
|
||||
|
||||
print("Adjacency list: " + str(dict(adjacency_list)))
|
||||
print("List of targets: " + str(nodes_to_visit))
|
||||
print("The shortest path found by bfs algorithm: " + str(shortest_path_bfs))
|
||||
def set_direction(self, direction):
|
||||
self.direction = direction
|
||||
|
||||
def generate_shortest_path_for_all_targets_bfs(self, nodes_to_visit):
|
||||
shortest_bfs_path = []
|
||||
start_node = 0
|
||||
def get_x(self):
|
||||
return self.x
|
||||
|
||||
for target_node in nodes_to_visit:
|
||||
shortest_bfs_path.extend(self.__shortest_path_bfs(start_node, target_node)[1:])
|
||||
start_node = target_node
|
||||
def set_x(self, x):
|
||||
self.x = x
|
||||
|
||||
return shortest_bfs_path
|
||||
def get_y(self):
|
||||
return self.y
|
||||
|
||||
# private
|
||||
def __add_edge(self, u, v):
|
||||
self.graph[u].append(v)
|
||||
def set_y(self, y):
|
||||
self.y = y
|
||||
|
||||
def __generate_graph(self):
|
||||
width = settings.Field.horizontal_count()
|
||||
height = settings.Field.vertical_count()
|
||||
|
||||
for i in range(height):
|
||||
for j in range(width):
|
||||
point = i * width + j
|
||||
point_left = i * width + j - 1
|
||||
point_left_column = point_left - width * i
|
||||
point_right = i * width + j + 1
|
||||
point_right_column = point_right % width
|
||||
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)
|
||||
class Node:
|
||||
def __init__(self, action, direction, parent, x, y):
|
||||
self.action = action
|
||||
self.direction = direction
|
||||
self.parent = parent
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
if point_left_column >= 0:
|
||||
self.__add_edge(point, point_left)
|
||||
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)
|
||||
def get_action(self):
|
||||
return self.action
|
||||
|
||||
return self.graph
|
||||
def set_action(self, action):
|
||||
self.action = action
|
||||
|
||||
def __shortest_path_bfs(self, start_node, target_node):
|
||||
path_list = [[start_node]]
|
||||
path_index = 0
|
||||
visited_nodes = {start_node}
|
||||
def get_direction(self):
|
||||
return self.direction
|
||||
|
||||
if start_node == target_node:
|
||||
return path_list[0]
|
||||
def set_direction(self, direction):
|
||||
self.direction = direction
|
||||
|
||||
while path_index < len(path_list):
|
||||
current_path = path_list[path_index]
|
||||
prev_node = current_path[-1]
|
||||
next_nodes = self.graph[prev_node]
|
||||
def get_parent(self):
|
||||
return self.parent
|
||||
|
||||
if target_node in next_nodes:
|
||||
current_path.append(target_node)
|
||||
return current_path
|
||||
def set_parent(self, parent):
|
||||
self.parent = parent
|
||||
|
||||
for next_node in next_nodes:
|
||||
if not next_node in visited_nodes:
|
||||
new_path = current_path[:]
|
||||
new_path.append(next_node)
|
||||
path_list.append(new_path)
|
||||
visited_nodes.add(next_node)
|
||||
def get_x(self):
|
||||
return self.x
|
||||
|
||||
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
|
||||
|
||||
# static
|
||||
@staticmethod
|
||||
def generate_target_fields(fields):
|
||||
width = settings.Field.horizontal_count()
|
||||
height = settings.Field.vertical_count()
|
||||
fields_to_visit = []
|
||||
for i in range(height):
|
||||
for j in range(width):
|
||||
point = i * width + j
|
||||
if fields[j][i] == 'dirt':
|
||||
fields_to_visit.append(point)
|
||||
return fields_to_visit
|
||||
|
||||
def graphsearch(explored, fringe, goaltest, istate, succ):
|
||||
node = Node(None, istate.get_direction(), None, istate.get_x(),
|
||||
istate.get_y())
|
||||
fringe.append(node)
|
||||
while True:
|
||||
if not fringe:
|
||||
return False
|
||||
elem = fringe.pop(0)
|
||||
temp = copy.copy(elem)
|
||||
if goal_test(goaltest,
|
||||
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 agent
|
||||
import math
|
||||
|
||||
|
||||
import time
|
||||
|
||||
possibleFields = {
|
||||
'dirt': field.Dirt(),
|
||||
@ -32,13 +31,13 @@ def randomize_map():
|
||||
for j in range(height):
|
||||
# k = random.choice(list(possibleFields.keys()))
|
||||
x = random.uniform(0, 100)
|
||||
if x < 80:
|
||||
if x < 4:
|
||||
field_array_small.append(possibleFields['dirt'].tile.object)
|
||||
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_2.append('sand')
|
||||
elif 90 < x < 100:
|
||||
elif 55 < x < 100:
|
||||
field_array_small.append(possibleFields['grass'].tile.object)
|
||||
field_array_small_2.append('grass')
|
||||
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 j in range(settings.Field.vertical_count()):
|
||||
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 = pygame.transform.scale(AGENT_IMG, (settings.Field.size(), settings.Field.size()))
|
||||
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 = pygame.transform.scale(AGENT_IMG, (settings.Field.size(), settings.Field.size()))
|
||||
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 = pygame.transform.scale(AGENT_IMG, (settings.Field.size(), settings.Field.size()))
|
||||
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 = pygame.transform.scale(AGENT_IMG, (settings.Field.size(), settings.Field.size()))
|
||||
window.blit(AGENT, (rect.x, rect.y))
|
||||
@ -108,8 +107,7 @@ def draw_window(agent, fields):
|
||||
|
||||
|
||||
common = common.Instance()
|
||||
agent = agent.Instance(1000, 'east')
|
||||
graph = graph.Instance()
|
||||
agent = agent.Instance(1000, 2)
|
||||
|
||||
|
||||
def main():
|
||||
@ -122,22 +120,31 @@ def main():
|
||||
settings.Pygame.height())))
|
||||
|
||||
pygame.display.set_caption(settings.Pygame.display_name())
|
||||
|
||||
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'):
|
||||
pygame.time.Clock().tick(settings.Pygame.fps())
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
common.set('game_running', False)
|
||||
|
||||
if common.get('state_imgShown'):
|
||||
read_img(agent, fields)
|
||||
else:
|
||||
draw_window(agent, fields)
|
||||
agent_action(agent.rotate())
|
||||
agent_action(agent.move())
|
||||
if x:
|
||||
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()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user