2021-04-14 00:22:27 +02:00
|
|
|
import sys
|
|
|
|
from collections import deque
|
|
|
|
from os import path
|
|
|
|
import queue
|
2021-04-14 03:55:35 +02:00
|
|
|
import numpy
|
2021-04-14 00:22:27 +02:00
|
|
|
|
|
|
|
from utils import *
|
|
|
|
from settings import *
|
|
|
|
|
|
|
|
|
|
|
|
class Problem:
|
2021-04-14 14:50:44 +02:00
|
|
|
def __init__(self, initial, goal):
|
2021-04-14 00:22:27 +02:00
|
|
|
self.initial = initial
|
|
|
|
self.goal = goal
|
|
|
|
|
|
|
|
def actions(self, state):
|
|
|
|
moves = []
|
2021-04-14 03:55:35 +02:00
|
|
|
if self.turn_left(state):
|
2021-04-14 00:22:27 +02:00
|
|
|
moves.append('Left')
|
2021-04-14 03:55:35 +02:00
|
|
|
if self.turn_right(state):
|
2021-04-14 00:22:27 +02:00
|
|
|
moves.append('Right')
|
2021-04-14 03:55:35 +02:00
|
|
|
if self.move_forward(state):
|
2021-04-14 00:22:27 +02:00
|
|
|
moves.append('Forward')
|
|
|
|
|
2021-04-14 03:55:35 +02:00
|
|
|
#print(moves)
|
2021-04-14 00:22:27 +02:00
|
|
|
return moves
|
|
|
|
|
|
|
|
|
|
|
|
def turn_left(self, state):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def turn_right(self, state):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def move_forward(self, state):
|
|
|
|
|
|
|
|
a_row = 0
|
|
|
|
a_column = 0
|
|
|
|
|
|
|
|
for row in range(MAP_SIZE):
|
|
|
|
for column, pos in enumerate(state[row]):
|
|
|
|
if pos == ">":
|
|
|
|
a_row = row
|
|
|
|
a_column = column
|
|
|
|
|
|
|
|
if a_column == MAP_SIZE-1:
|
|
|
|
return False
|
|
|
|
elif state[a_row][a_column+1] == '.':
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
if pos == "<":
|
|
|
|
a_row = row
|
|
|
|
a_column = column
|
|
|
|
|
|
|
|
if a_column == 0:
|
|
|
|
return False
|
|
|
|
elif state[a_row][a_column-1] == '.':
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
if pos == "v":
|
|
|
|
a_row = row
|
|
|
|
a_column = column
|
|
|
|
|
|
|
|
if a_row == MAP_SIZE-1:
|
|
|
|
return False
|
|
|
|
elif state[a_row+1][a_column] == '.':
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
if pos == "^":
|
|
|
|
a_row = row
|
|
|
|
a_column = column
|
|
|
|
|
|
|
|
if row == 0:
|
|
|
|
return False
|
|
|
|
elif state[a_row-1][a_column] == '.':
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def turn_me_or_move(self, state, do_it):
|
2021-04-14 03:55:35 +02:00
|
|
|
|
|
|
|
temp_map = [list(item) for item in state]
|
|
|
|
|
|
|
|
#print(temp_map)
|
|
|
|
|
|
|
|
#a_row = 0
|
|
|
|
#a_column = 0
|
2021-04-14 00:22:27 +02:00
|
|
|
|
|
|
|
for row in range(MAP_SIZE):
|
2021-04-14 03:55:35 +02:00
|
|
|
for column, pos in enumerate(temp_map[row]):
|
2021-04-14 00:22:27 +02:00
|
|
|
if pos == ">":
|
|
|
|
a_row = row
|
|
|
|
a_column = column
|
2021-04-14 03:55:35 +02:00
|
|
|
#print("a_row:" + str(a_row))
|
|
|
|
#print("a_column" + str(a_column))
|
2021-04-14 00:22:27 +02:00
|
|
|
|
|
|
|
if(do_it == 'Left'):
|
2021-04-14 03:55:35 +02:00
|
|
|
temp_map[a_row][a_column] = "^"
|
2021-04-14 00:22:27 +02:00
|
|
|
if(do_it == 'Right'):
|
2021-04-14 03:55:35 +02:00
|
|
|
temp_map[a_row][a_column] = 'v'
|
2021-04-14 00:22:27 +02:00
|
|
|
if(do_it == 'Forward'):
|
2021-04-14 03:55:35 +02:00
|
|
|
temp_map[a_row][a_column] = '.'
|
|
|
|
temp_map[a_row][a_column+1] = '>'
|
|
|
|
return temp_map
|
2021-04-14 00:22:27 +02:00
|
|
|
|
|
|
|
if pos == "<":
|
|
|
|
a_row = row
|
|
|
|
a_column = column
|
|
|
|
if(do_it == 'Left'):
|
2021-04-14 03:55:35 +02:00
|
|
|
temp_map[a_row][a_column] = 'v'
|
2021-04-14 00:22:27 +02:00
|
|
|
if(do_it == 'Right'):
|
2021-04-14 03:55:35 +02:00
|
|
|
temp_map[a_row][a_column] = '^'
|
2021-04-14 00:22:27 +02:00
|
|
|
if(do_it == 'Forward'):
|
2021-04-14 03:55:35 +02:00
|
|
|
temp_map[a_row][a_column] = '.'
|
|
|
|
temp_map[a_row][a_column-1] = '<'
|
|
|
|
return temp_map
|
2021-04-14 00:22:27 +02:00
|
|
|
if pos == "v":
|
|
|
|
a_row = row
|
|
|
|
a_column = column
|
|
|
|
if(do_it == 'Left'):
|
2021-04-14 03:55:35 +02:00
|
|
|
temp_map[a_row][a_column] = '>'
|
2021-04-14 00:22:27 +02:00
|
|
|
if(do_it == 'Right'):
|
2021-04-14 03:55:35 +02:00
|
|
|
temp_map[a_row][a_column] = '<'
|
2021-04-14 00:22:27 +02:00
|
|
|
if(do_it == 'Forward'):
|
2021-04-14 03:55:35 +02:00
|
|
|
temp_map[a_row][a_column] = '.'
|
|
|
|
temp_map[a_row+1][a_column] = 'v'
|
|
|
|
return temp_map
|
2021-04-14 00:22:27 +02:00
|
|
|
if pos == "^":
|
|
|
|
a_row = row
|
|
|
|
a_column = column
|
|
|
|
if(do_it == 'Left'):
|
2021-04-14 03:55:35 +02:00
|
|
|
temp_map[a_row][a_column] = '<'
|
2021-04-14 00:22:27 +02:00
|
|
|
if(do_it == 'Right'):
|
2021-04-14 03:55:35 +02:00
|
|
|
temp_map[a_row][a_column] = '>'
|
2021-04-14 00:22:27 +02:00
|
|
|
if(do_it == 'Forward'):
|
2021-04-14 03:55:35 +02:00
|
|
|
temp_map[a_row][a_column] = '.'
|
|
|
|
temp_map[a_row-1][a_column] = '^'
|
|
|
|
return temp_map
|
|
|
|
return temp_map
|
2021-04-14 00:22:27 +02:00
|
|
|
|
|
|
|
def result(self, state, action):
|
|
|
|
new_state = []
|
|
|
|
|
|
|
|
if action == 'Left':
|
|
|
|
new_state = self.turn_me_or_move(state, 'Left')
|
|
|
|
elif action == 'Right':
|
|
|
|
new_state = self.turn_me_or_move(state, 'Right')
|
|
|
|
elif action == 'Forward':
|
|
|
|
new_state = self.turn_me_or_move(state, 'Forward')
|
|
|
|
|
2021-04-14 03:55:35 +02:00
|
|
|
super_new_state = tuple(map(tuple,new_state))
|
|
|
|
|
|
|
|
return super_new_state
|
2021-04-14 00:22:27 +02:00
|
|
|
|
|
|
|
def goal_test(self, state):
|
2021-04-14 03:55:35 +02:00
|
|
|
if self.goal == state:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-04-14 00:22:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Node:
|
|
|
|
def __init__(self, state, parent=None, action=None):
|
|
|
|
"""Create a search tree Node, derived from a parent by an action."""
|
|
|
|
self.state = state
|
|
|
|
self.parent = parent
|
|
|
|
self.action = action
|
2021-04-14 03:55:35 +02:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<Node {}>".format(self.state)
|
2021-04-14 00:22:27 +02:00
|
|
|
|
|
|
|
def expand(self, problem):
|
|
|
|
"""List the nodes reachable in one step from this node."""
|
|
|
|
return [self.child_node(problem, action)
|
|
|
|
for action in problem.actions(self.state)]
|
|
|
|
|
|
|
|
def child_node(self, problem, action):
|
|
|
|
next_state = problem.result(self.state, action)
|
|
|
|
next_node = Node(next_state, self, action)
|
|
|
|
return next_node
|
|
|
|
|
2021-04-14 03:55:35 +02:00
|
|
|
def __eq__(self, other):
|
|
|
|
return isinstance(other, Node) and self.state == other.state
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
# We use the hash value of the state
|
|
|
|
# stored in the node instead of the node
|
|
|
|
# object itself to quickly search a node
|
|
|
|
# with the same state in a Hash Table
|
|
|
|
return hash(self.state)
|
|
|
|
|
2021-04-14 00:22:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
class BFS:
|
|
|
|
@staticmethod
|
|
|
|
def breadth_first_graph_search(problem):
|
|
|
|
node = Node(problem.initial)
|
|
|
|
if problem.goal_test(node.state):
|
|
|
|
return node
|
|
|
|
frontier = deque([node])
|
2021-04-14 03:55:35 +02:00
|
|
|
final_child = node
|
|
|
|
history = []
|
2021-04-14 00:22:27 +02:00
|
|
|
explored = set()
|
|
|
|
while frontier:
|
|
|
|
node = frontier.popleft()
|
|
|
|
explored.add(node.state)
|
|
|
|
for child in node.expand(problem):
|
|
|
|
if child.state not in explored and child not in frontier:
|
|
|
|
if problem.goal_test(child.state):
|
2021-04-14 03:55:35 +02:00
|
|
|
while(child.parent != None):
|
|
|
|
history.append(child.action)
|
|
|
|
child = child.parent
|
|
|
|
#return child
|
|
|
|
history.reverse()
|
|
|
|
#print(history)
|
|
|
|
return history
|
2021-04-14 00:22:27 +02:00
|
|
|
frontier.append(child)
|
2021-04-14 03:55:35 +02:00
|
|
|
#BFS.print_node_state(child.state)
|
|
|
|
|
2021-04-14 00:22:27 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def loadMap(map_name=''):
|
|
|
|
maze = []
|
|
|
|
map_folder = path.dirname(__file__)
|
|
|
|
with open(path.join(map_folder, map_name), 'rt') as f:
|
|
|
|
for line in f:
|
2021-04-14 03:55:35 +02:00
|
|
|
maze.append(line.rstrip('\n'))
|
|
|
|
|
|
|
|
#print(maze)
|
2021-04-14 00:22:27 +02:00
|
|
|
return maze
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def run():
|
2021-04-14 03:55:35 +02:00
|
|
|
initial_map = tuple(map(tuple,BFS.loadMap('map.txt')))
|
|
|
|
goal_map = tuple(map(tuple,BFS.loadMap('goal_map.txt')))
|
2021-04-14 00:22:27 +02:00
|
|
|
problem = Problem(initial_map, goal_map)
|
|
|
|
|
2021-04-14 03:55:35 +02:00
|
|
|
#BFS.print_node_state(initial_map)
|
|
|
|
#BFS.print_node_state(goal_map)
|
|
|
|
|
2021-04-14 00:22:27 +02:00
|
|
|
result = BFS.breadth_first_graph_search(problem)
|
|
|
|
print(result)
|
2021-04-14 15:30:36 +02:00
|
|
|
return result
|
2021-04-14 03:55:35 +02:00
|
|
|
#print(BFS.print_node_state(result))
|
2021-04-14 00:22:27 +02:00
|
|
|
|
2021-04-14 03:55:35 +02:00
|
|
|
@staticmethod
|
|
|
|
def print_node_state(node_state):
|
|
|
|
print(numpy.matrix(node_state))
|
|
|
|
|