-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgrid_game.py
240 lines (190 loc) · 6.17 KB
/
grid_game.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
# Dumbed down version of car simulator
# For first tests of Q-learning techniques
# 5x5 grid world
# Our car starts in one of the 10 blocks in the left-most 2 columns
# There are two or three red squares with a reward of -10 (obstacles)
# There is a region on the right-most side with reward 10
# Our car can take one of 3 actions each turn:
# drive straight - car moves one square forward
# drive left
# drive right
import random
import numpy as np
X_DIM, Y_DIM = (5, 5)
class World():
def __init__(self, grid, car, goal_reward, step_cost, printing):
self.grid = grid
self.car = car
self.goal_reward = goal_reward
self.step_cost = step_cost
self.state = grid.grid
self.cost = 0
self.goal = [X_DIM-1,2]
self.printing = printing
self.car.printing = printing
self.grid.printing = printing
def updateState(self, action):
try:
if(self.cost==-self.goal_reward):
self.restartGame()
return (-500,self.grid.getState())
action = int(action)
if action in self.car.getAvailableActions():
if self.printing:
print('taking action: '+str(action))
self.car.takeAction(action)
if self.printing:
self.grid.printGrid()
if self.checkGoal():
reward = self.cost + self.goal_reward - self.step_cost
print('## reached goal with reward: ',reward,' ##')
self.restartGame()
return(reward,self.grid.getState())
else:
self.cost -= self.step_cost
return(self.cost, self.grid.getState())
else: # action not available - penalize -500
if self.printing:
print('not in available actions')
# self.restartGame()
# self.cost -= self.step_cost
# return(self.cost, self.grid.getState())
return (-500, self.grid.getState())
except ValueError as e:
print('input value error')
return(0,0)
def checkGoal(self):
if self.car.x == self.goal[0] and self.car.y == self.goal[1]:
if self.printing:
print('reached goal!')
return True
return False
def restartGame(self):
self.grid = Grid(self.grid.w, self.grid.h)
self.car = Car(self.grid,0,random.randint(0,self.grid.h-1))
self.state = self.grid.grid
self.cost = 0
self.goal = [4,2]
class Grid():
def __init__(self, w, h):
self.w = w
self.h = h
self.state_length = w*h
self.grid = [[0 for x in range(w)] for y in range(h)]
def getGrid(self):
return self.grid.copy()
def getState(self):
state = []
for row in self.grid:
state.extend(row)
state = np.transpose(np.array(state))
state = state.reshape(1, self.state_length)
# print(state.shape)
return state
def addCar(self, car):
self.car = car
self.grid[car.y][car.x] = 1
self.carPos = (car.x, car.y)
def update(self, car):
self.grid[self.carPos[1]][self.carPos[0]] = 0
self.grid[car.y][car.x] = 1
self.carPos = (car.x, car.y)
def clear(self, x, y):
if x < 0 or y < 0:
return False
try:
b = self.grid[y][x]
if(b == 0):
return True
return False
except IndexError:
return False
def printGrid(self):
printDivide()
for i in range(self.w):
line = ' '
for j in range(self.h):
if (j, i) == self.carPos:
line += '[ ]'
else:
line += ' 0 '
print(line)
# for i in self.grid:
# print(i)
printDivide()
class Car():
def __init__(self, grid, x, y):
self.x = x
self.y = y
self.grid = grid
self.grid.addCar(self)
self.printing = False #default value
def getAvailableActions(self):
actions = []
if(self.grid.clear(self.x + 1, self.y)):
actions.append(0)
if(self.grid.clear(self.x, self.y - 1)):
actions.append(1)
if(self.grid.clear(self.x, self.y + 1)):
actions.append(2)
return actions
def takeAction(self, action):
if action == 0:
self.driveForward()
elif action == 1:
self.moveLeft()
elif action == 2:
self.moveRight()
def moveRight(self):
if self.printing:
print('move Right')
newx = self.x
newy = self.y + 1
if(self.grid.clear(newx, newy)):
self.x, self.y = (newx, newy)
else:
print("ERROR - next position not clear")
self.grid.update(self)
def moveLeft(self):
if self.printing:
print('move Left')
newx = self.x
newy = self.y - 1
if(self.grid.clear(newx, newy)):
self.x, self.y = (newx, newy)
else:
print("ERROR - next position not clear")
self.grid.update(self)
def driveForward(self):
if self.printing:
print('drive forward')
newx = self.x + 1
newy = self.y
if(self.grid.clear(newx, newy)):
self.x, self.y = (newx, newy)
else:
print("ERROR - next position not clear")
self.grid.update(self)
def printDivide():
print('***********')
def printGrid(grid):
printDivide()
for row in grid:
line = ' '
for i in row:
line += str(i) + ' '
print(line)
printDivide()
if __name__ == "__main__":
grid = Grid(X_DIM, Y_DIM)
car = Car(grid, 0, 0)
grid.printGrid()
print(grid.getState())
print(car.getAvailableActions())
game_state = World(grid, car, 500, 10)
gameMode = True
while gameMode:
cmd = input("Next action? [0,1,2] \n")
s,c = game_state.updateState(cmd)
print(s)
print(c)