-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.py
197 lines (196 loc) · 8.02 KB
/
actions.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
def move_agent(board, direction):
result_square = ""
for row in range(0, len(board)):
for column in range(0, len(board[row])):
# our agent could be in an empty square (4) or on a goal (1)
if board[row][column] == "4" or board[row][column] == "1":
if board[row][column] == "4":
is_goal = False
else:
is_goal = True
agent_row = row
agent_column = column
if direction == "L":
result_square = board[row][column-1]
target_row = row
target_column = column - 1
elif direction == "R":
result_square = board[row][column+1]
target_row = row
target_column = column + 1
elif direction == "U":
result_square = board[row-1][column]
target_row = row - 1
target_column = column
elif direction == "D":
result_square = board[row+1][column]
target_row = row + 1
target_column = column
else:
raise ValueError("Bad direction!")
# result_square has what is there now!
if result_square == "-1":
# there is a wall, we cannot move!
return False
# we are moving the agent to the goal
elif result_square == "0":
board[target_row][target_column] = "1"
# is the agent leaving a goal?
if is_goal:
board[agent_row][agent_column] = "0"
else:
board[agent_row][agent_column] = "5"
return board
elif result_square == "5":
board[target_row][target_column] = "4"
if is_goal:
board[agent_row][agent_column] = "0"
else:
board[agent_row][agent_column] = "5"
return board
elif result_square == "3" or result_square == "2":
if result_square == "3":
is_box_goal = False
else:
is_box_goal = True
# we also have to check if we can move the box or not ... ugh
if direction == "L":
box_result_square = board[agent_row][agent_column-2]
box_target_row = agent_row
box_target_column = agent_column - 2
elif direction == "R":
box_result_square = board[agent_row][agent_column+2]
box_target_row = agent_row
box_target_column = agent_column + 2
elif direction == "U":
box_result_square = board[agent_row-2][agent_column]
box_target_row = agent_row - 2
box_target_column = agent_column
elif direction == "D":
box_result_square = board[agent_row+2][agent_column]
box_target_row = agent_row + 2
box_target_column = agent_column
# now we have the box result, let us see if we can move it to that square!
if box_result_square == "5":
board[box_target_row][box_target_column] = "3"
if is_box_goal:
board[target_row][target_column] = "1"
else:
board[target_row][target_column] = "4"
if is_goal:
board[agent_row][agent_column] = "0"
else:
board[agent_row][agent_column] = "5"
return board
elif box_result_square == "0":
board[box_target_row][box_target_column] = "2"
if is_box_goal:
board[target_row][target_column] = "1"
else:
board[target_row][target_column] = "4"
if is_goal:
board[agent_row][agent_column] = "0"
else:
board[agent_row][agent_column] = "5"
return board
elif box_result_square == "-1" or box_result_square == "2" or box_result_square == "3":
# we hit a wall, a box, or a goal that has a box on it, so we cannot move this specific box!
return False
def move_agent_mcts(board, direction):
result_square = ""
for row in range(0, len(board)):
for column in range(0, len(board[row])):
# our agent could be in an empty square (4) or on a goal (1)
if board[row][column] == "4" or board[row][column] == "1":
if board[row][column] == "4":
is_goal = False
else:
is_goal = True
agent_row = row
agent_column = column
if direction == "L":
result_square = board[row][column-1]
target_row = row
target_column = column - 1
elif direction == "R":
result_square = board[row][column+1]
target_row = row
target_column = column + 1
elif direction == "U":
result_square = board[row-1][column]
target_row = row - 1
target_column = column
elif direction == "D":
result_square = board[row+1][column]
target_row = row + 1
target_column = column
else:
raise ValueError("Bad direction!")
# result_square has what is there now!
if result_square == "-1":
# there is a wall, we cannot move!
return board
# we are moving the agent to the goal
elif result_square == "0":
board[target_row][target_column] = "1"
# is the agent leaving a goal?
if is_goal:
board[agent_row][agent_column] = "0"
else:
board[agent_row][agent_column] = "5"
return board
elif result_square == "5":
board[target_row][target_column] = "4"
if is_goal:
board[agent_row][agent_column] = "0"
else:
board[agent_row][agent_column] = "5"
return board
elif result_square == "3" or result_square == "2":
if result_square == "3":
is_box_goal = False
else:
is_box_goal = True
# we also have to check if we can move the box or not ... ugh
if direction == "L":
box_result_square = board[agent_row][agent_column-2]
box_target_row = agent_row
box_target_column = agent_column - 2
elif direction == "R":
box_result_square = board[agent_row][agent_column+2]
box_target_row = agent_row
box_target_column = agent_column + 2
elif direction == "U":
box_result_square = board[agent_row-2][agent_column]
box_target_row = agent_row - 2
box_target_column = agent_column
elif direction == "D":
box_result_square = board[agent_row+2][agent_column]
box_target_row = agent_row + 2
box_target_column = agent_column
# now we have the box result, let us see if we can move it to that square!
if box_result_square == "5":
board[box_target_row][box_target_column] = "3"
if is_box_goal:
board[target_row][target_column] = "1"
else:
board[target_row][target_column] = "4"
if is_goal:
board[agent_row][agent_column] = "0"
else:
board[agent_row][agent_column] = "5"
return board
elif box_result_square == "0":
board[box_target_row][box_target_column] = "2"
if is_box_goal:
board[target_row][target_column] = "1"
else:
board[target_row][target_column] = "4"
if is_goal:
board[agent_row][agent_column] = "0"
else:
board[agent_row][agent_column] = "5"
return board
elif box_result_square == "-1" or box_result_square == "2" or box_result_square == "3":
# we hit a wall, a box, or a goal that has a box on it, so we cannot move this specific box!
return board