-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEnvironment.py
155 lines (136 loc) · 5.34 KB
/
Environment.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
"""
Environment.py created by mohit.badwal
on 4/6/2018
"""
import random
import numpy as np
class GameEnvironment:
s = 20
iterations = s * 150
limit = s * s
def __init__(self, blockList, gamma=0.8, qMatrix=np.zeros([s, s])):
# taking 2 matrices , one for storing state and other or storing reward for that state
self.stateMatrix = np.zeros([GameEnvironment.s, GameEnvironment.s])
self.rewardMatrix = np.zeros([GameEnvironment.s, GameEnvironment.s])
self.qMatrix = qMatrix
self.gamma = gamma
self.totalStepsTaken = 1
self.totalRewards = 0
self.blockList = blockList
self.__intializeMatrix()
def clearMatrix(self):
self.stateMatrix = np.zeros([GameEnvironment.s, GameEnvironment.s])
self.rewardMatrix = np.zeros([GameEnvironment.s, GameEnvironment.s])
self.totalStepsTaken = 0
self.totalRewards = 0
def __intializeMatrix(self):
# init all rewards to -2
self.rewardMatrix.fill(-2)
# init starting reward and not to go states
count = 0
for dope in self.blockList: self.rewardMatrix[dope] = -5
self.rewardMatrix[0][GameEnvironment.s - 1] = 0
self.rewardMatrix[GameEnvironment.s - 1][0] = -2
print(self.rewardMatrix)
# defining start and final state in state matrix
self.stateMatrix[GameEnvironment.s - 1][0] = 1
self.stateMatrix[0][GameEnvironment.s - 1] = -2
def __availableActions(self, currState):
x, y = currState
directions = []
try:
if x - 1 < 0:
pass
elif self.stateMatrix[x - 1][y] == 0 or self.stateMatrix[x - 1][y] == -2:
directions.append(0)
except IndexError:
pass
try:
if self.stateMatrix[x][y + 1] == 0 or self.stateMatrix[x][y + 1] == -2:
directions.append(1)
except IndexError:
# directions.append(1)
pass
try:
if self.stateMatrix[x + 1][y] == 0 or self.stateMatrix[x + 1][y] == -2:
directions.append(2)
except IndexError:
# directions.append(2)
pass
try:
if y - 1 < 0:
pass
elif self.stateMatrix[x][y - 1] == 0 or self.stateMatrix[x][y - 1] == -2:
directions.append(3)
except IndexError:
# directions.append(3)
pass
return directions
def __getCoordinates(self, direction, currState):
x, y = currState
if direction == 0:
x, y = x - 1, y
elif direction == 1:
x, y = x, y + 1
elif direction == 2:
x, y = x + 1, y
else:
x, y = x, y - 1
return x, y
def chooseMax(self, currState):
try:
directions = self.__availableActions(currState)
# print(directions)
x, y = currState
rewards = []
directions1 = []
coordinates = []
for d in directions:
x1, y1 = self.__getCoordinates(d, (x, y))
reward = self.qMatrix[x1][y1]
coordinates.append((x1, y1))
rewards.append(reward)
maxi = max(rewards)
for i in range(len(rewards)):
if maxi == rewards[i]:
directions1.append(directions[i])
direction = random.choice(directions1)
for j in range(len(rewards)):
if direction == directions[j]:
self.__updateQMatrix(currState, rewards[j])
return direction
except ValueError:
return -1
def __updateQMatrix(self, currState, reward):
x, y = currState
self.qMatrix[x][y] = self.rewardMatrix[x][y] + self.gamma * reward
def takeAction(self, direction, currState):
# direction 0 is up , 1 is right , 2 is down and 3 is left
x, y = currState
self.totalStepsTaken = self.totalStepsTaken + 1
try:
if direction == 0:
if x - 1 < 0:
raise IndexError
self.stateMatrix[x - 1][y] = self.totalStepsTaken
self.totalRewards = self.totalRewards + self.rewardMatrix[x - 1][y]
x, y = x - 1, y
elif direction == 1:
self.stateMatrix[x][y + 1] = self.totalStepsTaken
self.totalRewards = self.totalRewards + self.rewardMatrix[x][y + 1]
x, y = x, y + 1
elif direction == 2:
self.stateMatrix[x + 1][y] = self.totalStepsTaken
self.totalRewards = self.totalRewards + self.rewardMatrix[x + 1][y]
x, y = x + 1, y
else:
if y - 1 < 0:
raise IndexError
self.stateMatrix[x][y - 1] = self.totalStepsTaken
self.totalRewards = self.totalRewards + self.rewardMatrix[x][y - 1]
x, y = x, y - 1
except IndexError:
pass
# self.stateMatrix[x][y] = self.totalStepsTaken
# self.totalRewards = self.totalRewards + self.rewardMatrix[x][y]
return x, y