-
Notifications
You must be signed in to change notification settings - Fork 0
/
RescueRobot.py
109 lines (94 loc) · 3.96 KB
/
RescueRobot.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from SearchAlgorithms.ClassicSearchAlgorithms import *
from SearchAlgorithms.BeyondClassicSearchAlgorithm import *
from copy import deepcopy
class Problem():
n = 5
m = 5
size = [n, m]
number_of_blocks = 4
list_of_blocks = [
((3, 2), (4, 2)),
((3, 3), (4, 3)),
((2, 3), (2, 4)),
((3, 3), (3, 4)),
]
def initialState(self):
return (1, 1)
def goal(self):
return (self.n, self.m)
def isGoalTest(self, state):
return state == self.goal()
def actions(self, state):
actions = []
if state[0] > 1:
actions.append('U')
if state[0] < self.n:
actions.append('D')
if state[1] > 1:
actions.append('L')
if state[1] < self.m:
actions.append('R')
return actions
def results(self, actions, state):
def is_safe_to_move(direction, location, block_list):
for block_row in block_list:
if list(block_row[0]) == location:
if 'U' in direction and block_row[0][0] == block_row[1][0] + 1:
return False
elif 'D' in direction and block_row[0][0] == block_row[1][0] - 1:
return False
elif 'R' in direction and block_row[0][1] == block_row[1][1] - 1:
return False
elif 'L' in direction and block_row[0][1] == block_row[1][1] + 1:
return False
if list(block_row[1]) == location:
if 'U' in direction and block_row[0][0] == block_row[1][0] - 1:
return False
elif 'D' in direction and block_row[0][0] == block_row[1][0] + 1:
return False
elif 'R' in direction and block_row[0][1] == block_row[1][1] + 1:
return False
elif 'L' in direction and block_row[0][1] == block_row[1][1] - 1:
return False
return True
states = []
for action in actions:
temp_state = deepcopy(state)
temp_state = list(temp_state)
state = list(state)
if is_safe_to_move(action, state, self.list_of_blocks):
if 'U' in action:
temp_state[0], temp_state[1] = state[0] - 1, state[1]
elif 'D' in action:
temp_state[0], temp_state[1] = state[0] + 1, state[1]
elif 'L' in action:
temp_state[0], temp_state[1] = state[0], state[1] - 1
elif 'R' in action:
temp_state[0], temp_state[1] = state[0], state[1] + 1
states.append(tuple(temp_state))
return states
def step_cost(self, current_state, next_state):
return 1
def heuristic(self, state):
return list(state)[0] + list(state)[1] - 2
def print_path(self, path):
def find_direction(current_state, next_state):
if current_state[0] == next_state[0] and current_state[1] > next_state[1]:
print('L', end=" ")
elif current_state[0] == next_state[0] and current_state[1] < next_state[1]:
print('R', end=" ")
elif current_state[0] > next_state[0] and current_state[1] == next_state[1]:
print('U', end=" ")
elif current_state[0] < next_state[0] and current_state[1] == next_state[1]:
print('D', end=" ")
print("Path:", end=" ")
for current_state, next_state in zip(path, path[1:]):
find_direction(current_state, next_state)
p = Problem()
csa = ClassicSearchAlgorithm(p)
bcsa = BeyondClassicSearchAlgorithm(p)
# csa.graph_depth_first_search(p.initialState())
# csa.graph_breadth_first_search(p.initialState())
# csa.graph_bidirectional_search(p.initialState(), p.goal())
# csa.graph_uniform_cost_search(p.initialState())
# bcsa.graph_a_star(p.initialState())