-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpd_main.py
409 lines (307 loc) · 13.2 KB
/
pd_main.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
from agent import Agent
from cell_methods import *
from grid_cells import *
from policies import *
import copy
import enum
import numpy as np
# creating enumerations using class
class actions(enum.Enum):
NORTH = 0
EAST = 1
SOUTH = 2
WEST = 3
PICKUP = 4
DROP = 5
RESET = 6
# Environment matrix
environment = [[[None, -1, -1, None], [None, -1, -1, -1], [None, -1, -1, -1], [None, -1, -1, -1], [None, None, -1, -1]],
[[-1, -1, -1, None], [-1, -1, -1, -1], [-1, -1, -1, -1], [-1, -1, -1, -1], [-1, None, -1, -1]],
[[-1, -1, -1, None], [-1, -1, -1, -1], [-1, -1, -1, -1], [-1, -1, -1, -1], [-1, None, -1, -1]],
[[-1, -1, -1, None], [-1, -1, -1, -1], [-1, -1, -1, -1], [-1, -1, -1, -1], [-1, None, -1, -1]],
[[-1, -1, None, None], [-1, -1, None, -1], [-1, -1, None, -1], [-1, -1, None, -1], [-1, None, None, -1]]]
# Pickup q table
pickup_q_table = [ [[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]],
[[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]],
[[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]],
[[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]],
[[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]]]
# Dropoff q table
dropoff_q_table = [ [[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]],
[[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]],
[[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]],
[[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]],
[[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]]]
bank_account_list = []
num_operator_list = []
# agent object
agent = Agent()
pickup_cells = [] # list of pickup cells
dropoff_cells = [] # list of drop off cells
def clear_lists():
bank_account_list.clear()
num_operator_list.clear()
# swaps the values in the q table
def swap_q_tables():
global pickup_q_table, dropoff_q_table
pickup_q_table, dropoff_q_table = dropoff_q_table, pickup_q_table
# random policy #
# returns a random number in the possible actions list
def PRandom(possible_actions):
return random.choice(possible_actions)
# exploit policy #
def PExploit(possible_actions, agent, row, col):
duplicate = []
q_table = dropoff_q_table if agent.hasBlock() else pickup_q_table
# choose action with best q-value 80% of the time
if random.random() <= 0.8:
max_action = possible_actions[0]
for num in possible_actions:
q_value = q_table[row][col][num]
max_q_value = q_table[row][col][max_action]
if q_value > max_q_value:
max_action = num
duplicate.clear()
duplicate.append(num)
if q_value == max_q_value:
duplicate.append(num)
exploit_choice = random.choice(duplicate) if len(duplicate) > 1 else max_action
else:
exploit_choice = random.choice(possible_actions)
return exploit_choice
# greedy policy #
def PGreedy(possible_actions, agent, row, col):
duplicate = []
q_table = dropoff_q_table if agent.hasBlock() else pickup_q_table
# choose action with best q-value 100% of the time
max_action = possible_actions[0]
for num in possible_actions:
q_value = q_table[row][col][num]
max_q_value = q_table[row][col][max_action]
if q_value > max_q_value:
max_action = num
duplicate.clear()
duplicate.append(num)
if q_value == max_q_value:
duplicate.append(num)
greedy_choice = random.choice(duplicate) if len(duplicate) > 1 else max_action
return greedy_choice
# ------------------------------ HELPER FUNCTIONS ------------------------------- #
# decrement blocks on cell #
def decrementNumBlocksInCell(pos):
cell = getCellFromPosition(pos, pickup_cells)
cell.num_of_blocks -= 1
# increment blocks on cell #
def incrementNumBlocksInCell(pos):
cell = getCellFromPosition(pos, dropoff_cells)
cell.num_of_blocks += 1
# Initialize environment to original #
def initializeCells(pickup_states, dropoff_states):
pickup_cells.clear()
dropoff_cells.clear()
for pos in pickup_states:
pickup_cells.append(Cells(pos, 5))
for pos in dropoff_states:
dropoff_cells.append(Cells(pos, 0))
for i in range(len(pickup_states)):
print("Pickup cell ", i, " = ", pickup_cells[i].position)
print()
for i in range(len(dropoff_states)):
print("dropoff cell ", i, " = ", dropoff_cells[i].position)
# returns the cell object in the given position #
# pos = [x,y]
def getCellFromPosition(pos, cell_list):
for cell in cell_list:
if cell.position == pos:
return cell
# function to initialize Q table #
def initialize_Q_table():
global pickup_q_table, dropoff_q_table
for row in range(5):
for col in range(5):
for i in range(6):
pickup_q_table[row][col][i] = 0
dropoff_q_table[row][col][i] = 0
# calculates reward from action #
def calculateRewardFromAction(action):
negative_reward = ["NORTH", "EAST", "SOUTH", "WEST"]
positive_reward = ["PICKUP", "DROP"]
operator = action.name
if operator in negative_reward:
reward = -1
if operator in positive_reward:
reward = 13
return reward
def isPickup(pos, pickup_states):
return pos in pickup_states
def isDropOff(pos, dropoff_states):
return pos in dropoff_states
# returns action enum given agent's policy and possible actions #
def getPolicyAction(agent, state, possible_actions, pickup_states, dropoff_states):
row = state[0]
col = state[1]
pos = [row, col]
# finds pickup or dropoff cell if position is a pickup or dropoff state
if pos in pickup_states:
cell = getCellFromPosition(pos, pickup_cells)
elif pos in dropoff_states:
cell = getCellFromPosition(pos, dropoff_cells)
if pos in pickup_states and state[2] == 0 and not cell.is_empty(): # pos is pickup state, agent has no block, and cell not empty
action = actions.PICKUP
decrementNumBlocksInCell(pos)
if cell.is_empty():
cell.isActive = False
print('\nAgent picked up a block')
print(f'Pickup cell {cell.position} = {cell.num_of_blocks}')
elif pos in dropoff_states and state[2] == 1 and not cell.is_full():
action = actions.DROP
incrementNumBlocksInCell(pos)
if cell.is_full():
cell.isActive = False
print('\nAgent dropped off a block')
print(f'Drop off cell {cell.position} = {cell.num_of_blocks}')
else: # directional move
if agent.policy == "PRandom":
action = actions(PRandom(possible_actions))
if agent.policy == "PExploit":
action = actions(PExploit(possible_actions, agent, row, col))
if agent.policy == "PGreedy":
action = actions(PGreedy(possible_actions, agent, row, col))
agent.num_operators += 1
return action
# returns new state when action is applied to old state #
def getNextState(old_state, action):
new_state = copy.deepcopy(old_state)
if action.name == "NORTH":
new_state[0] -= 1
elif action.name == "EAST":
new_state[1] += 1
elif action.name == "SOUTH":
new_state[0] += 1
elif action.name == "WEST":
new_state[1] -= 1
elif action.name == "PICKUP":
new_state[2] = 1
elif action.name == "DROP":
new_state[2] = 0
return new_state
# returns max q value in list of possible actions #
def getMaxQvalue(possible_actions, state):
hasBlock = True if state[2] == 1 else False
q_table = dropoff_q_table if hasBlock else pickup_q_table
row = state[0]
col = state[1]
possible_q_values = []
for i in possible_actions:
possible_q_values.append(q_table[row][col][i]) # Appends to list all possible q_value
return np.max(possible_q_values)
# returns list of all possible action in a given position #
def getAllPossibleNextAction(state, pickup_states, dropoff_states):
row = state[0]
col = state[1]
pos = [row, col]
hasBlock = True if state[2] == 1 else False
possible_actions = []
for i in range(4):
if environment[row][col][i] != None:
possible_actions.append(i)
if pos in pickup_states:
cell = getCellFromPosition(pos, pickup_cells)
elif pos in dropoff_states:
cell = getCellFromPosition(pos, dropoff_cells)
# if pos in pickup_states and not hasBlock and not cell.is_empty():
if pos in pickup_states and not hasBlock and not cell.is_empty():
possible_actions.append(actions.PICKUP.value)
elif pos in dropoff_states and hasBlock and not cell.is_full():
possible_actions.append(actions.DROP.value)
return (possible_actions)
# returns number of times terminal state reached
def Q_learning(learning_rate, discount_rate, agent, pickup_states, dropoff_states):
position = agent.position
row = position[0]
col = position[1]
possible_actions = getAllPossibleNextAction(agent.state, pickup_states, dropoff_states) # possible actions in state
action = getPolicyAction(agent, agent.state, possible_actions, pickup_states, dropoff_states) # a = action chosen in state
agent.action = action
next_state = getNextState(agent.state, action) # s' = next state after action is applied
reward = calculateRewardFromAction(action) # calculate agent's rewards & bank account
agent.updateRewards(reward)
new_row = next_state[0]
new_col = next_state[1]
q_table = dropoff_q_table if agent.hasBlock() else pickup_q_table
old_value = q_table[row][col][action.value]
new_pos = [new_row, new_col]
next_possible_action = getAllPossibleNextAction(next_state, pickup_states, dropoff_states)
# next_action = getPolicyAction(agent, next_state, next_possible_action, pickup_states, dropoff_states)
# if pickup or drop off
if isPickup(new_pos, pickup_states):
cell = getCellFromPosition(new_pos, pickup_cells)
elif isDropOff(new_pos, dropoff_states):
cell = getCellFromPosition(new_pos, dropoff_cells)
# if (isPickup(new_pos, pickup_states) or isDropOff(new_pos, dropoff_states)) and not cell.isActive and not cell.available:
# next_max = np.max(q_table[new_row][new_col][0:4])
# else:
# next_max = np.max(q_table[new_row][new_col])
next_max = getMaxQvalue(next_possible_action, next_state)
new_q_value = ((1 - learning_rate) * old_value) + (learning_rate * (agent.reward + discount_rate * next_max))
q_table[row][col][action.value] = round(new_q_value, 2)
print(f'Q({action.name},{agent.state}) = {new_q_value} = (1 - {learning_rate}) * {old_value} + {learning_rate} * ({agent.reward} + {discount_rate} + {next_max})')
print("Q-table: ", q_table[row][col])
agent.updateState(next_state)
agent.updatePosition()
is_terminal = False
val = 0
for cell in dropoff_cells:
val += cell.num_of_blocks
if val == 15:
is_terminal = True
if is_terminal:
bank_account_list.append(agent.bank_account)
num_operator_list.append(agent.num_operators)
agent.initialize()
agent.terminal_states_reached += 1
agent.action = actions.RESET
initializeCells(pickup_states, dropoff_states)
print("\n-----------INITIALIZED----------")
# SARSA update, returns next action #
def SARSA_update(learning_rate, discount_rate, next_action, agent, pickup_states, dropoff_states):
is_terminal = False
val = 0
for cell in dropoff_cells:
val += cell.num_of_blocks
if val == 15:
is_terminal = True
if is_terminal:
bank_account_list.append(agent.bank_account)
num_operator_list.append(agent.num_operators)
agent.initialize()
agent.terminal_states_reached += 1
initializeCells(pickup_states, dropoff_states)
print("\n-----------INITIALIZED----------")
agent.action = actions.RESET
return None
position = agent.position
row = position[0]
col = position[1]
if next_action is None:
possible_actions = getAllPossibleNextAction(agent.state, pickup_states, dropoff_states) # possible actions in state
action = getPolicyAction(agent, agent.state, possible_actions, pickup_states, dropoff_states) # a = action chosen in state
else:
action = next_action # we know what our action is, so we chose it
reward = calculateRewardFromAction(action) # calculate agent's rewards & bank account
agent.action = action
agent.updateRewards(reward)
next_state = getNextState(agent.state, action) # s' = next state after action is applied
new_row = next_state[0]
new_col = next_state[1]
new_pos = [new_row, new_col]
next_possible_actions = getAllPossibleNextAction(next_state, pickup_states, dropoff_states) # all possible actions in s'
next_action = getPolicyAction(agent, next_state, next_possible_actions, pickup_states, dropoff_states) # a' = next action in s'
q_table = dropoff_q_table if agent.hasBlock() else pickup_q_table
old_value = q_table[row][col][action.value]
new_q_value = old_value + learning_rate * (reward + discount_rate *
q_table[next_state[0]][next_state[1]][next_action.value] - old_value)
q_table[row][col][action.value] = round(new_q_value, 2)
agent.updateState(next_state)
agent.updatePosition()
return next_action