-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot.py
54 lines (48 loc) · 2.3 KB
/
robot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from environment import Environment
from wall import Wall
from breadthFirstSearch import BreadthFirstSearch
from depthFirstSearch import DepthFirstSearch
from greedyBestFirstSearch import GreedyBestFirstSearch
from aStarSearch import AStarSearch
from bidirectionalSearch import BidirectionalSearch
from depthFirstSearch import DepthFirstSearch
from IDASearch import IterativeDeepeningAStarSearch
class Robot:
@staticmethod
def parseFile(filename):
""""
Takes a file name and read the maze configuration
"""
with open(filename, 'r') as file:
mazeSize = file.readline().strip('][ \n').split(',')
mazeSize = [int(i) for i in mazeSize] # convert string to integer
initialLocation = [int(i) for i in file.readline().strip('() \n').split(',')]
goalStates = file.readline().strip().split(' | ')
goalStates = [state.strip(') (').split(',') for state in goalStates]
goalStates = [[int(x) for x in state] for state in goalStates]
walls = []
for line in file.readlines():
if not line.isspace():
newWall = [int(i) for i in line.strip('() \n').split(',')]
walls.append(Wall(*newWall))
environment = Environment(row = mazeSize[0],column=mazeSize[1],start=initialLocation,goals=goalStates,walls=walls)
return environment
def __init__(self, filename):
self.environment = Robot.parseFile(filename)
self.searchMethod = None
def solve(self, method):
if method == "DFS":
self.searchMethod = DepthFirstSearch(self.environment)
elif method == "BFS":
self.searchMethod = BreadthFirstSearch(self.environment)
elif method == "GBFS":
self.searchMethod = GreedyBestFirstSearch(self.environment)
elif method == "AS":
self.searchMethod = AStarSearch(self.environment)
elif method == "CUS1":
self.searchMethod = BidirectionalSearch(self.environment)
elif method == "CUS2":
self.searchMethod = IterativeDeepeningAStarSearch(self.environment)
else:
raise Exception("There is no such method. The appropriate methods are BFS, DFS, GBFS, AS, CUS1, CUS2 ")
return self.searchMethod.search()