forked from cyberpvn7/Python_Lang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagic Grid.py
More file actions
153 lines (135 loc) · 5.1 KB
/
Copy pathMagic Grid.py
File metadata and controls
153 lines (135 loc) · 5.1 KB
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
import numpy as np
import matplotlib.pyplot as plt
class GridWorld(object):
def __init__(self, m, n, magicSquares):
self.grid = np.zeros((m,n))
self.m = m
self.n = n
self.stateSpace = [i for i in range(self.m*self.n)]
self.stateSpace.remove(80)
self.stateSpacePlus = [i for i in range(self.m*self.n)]
self.actionSpace = {'U': -self.m, 'D': self.m,
'L': -1, 'R': 1}
self.possibleActions = ['U', 'D', 'L', 'R']
# dict with magic squares and resulting squares
self.addMagicSquares(magicSquares)
self.agentPosition = 0
def isTerminalState(self, state):
return state in self.stateSpacePlus and state not in self.stateSpace
def addMagicSquares(self, magicSquares):
self.magicSquares = magicSquares
i = 2
for square in self.magicSquares:
x = square // self.m
y = square % self.n
self.grid[x][y] = i
i += 1
x = magicSquares[square] // self.m
y = magicSquares[square] % self.n
self.grid[x][y] = i
i += 1
def getAgentRowAndColumn(self):
x = self.agentPosition // self.m
y = self.agentPosition % self.n
return x, y
def setState(self, state):
x, y = self.getAgentRowAndColumn()
self.grid[x][y] = 0
self.agentPosition = state
x, y = self.getAgentRowAndColumn()
self.grid[x][y] = 1
def offGridMove(self, newState, oldState):
# if we move into a row not in the grid
if newState not in self.stateSpacePlus:
return True
# if we're trying to wrap around to next row
elif oldState % self.m == 0 and newState % self.m == self.m - 1:
return True
elif oldState % self.m == self.m - 1 and newState % self.m == 0:
return True
else:
return False
def step(self, action):
agentX, agentY = self.getAgentRowAndColumn()
resultingState = self.agentPosition + self.actionSpace[action]
if resultingState in self.magicSquares.keys():
resultingState = magicSquares[resultingState]
reward = -1 if not self.isTerminalState(resultingState) else 0
if not self.offGridMove(resultingState, self.agentPosition):
self.setState(resultingState)
return resultingState, reward, \
self.isTerminalState(resultingState), None
else:
return self.agentPosition, reward, \
self.isTerminalState(self.agentPosition), None
def reset(self):
self.agentPosition = 0
self.grid = np.zeros((self.m,self.n))
self.addMagicSquares(self.magicSquares)
return self.agentPosition
def render(self):
print('------------------------------------------')
for row in self.grid:
for col in row:
if col == 0:
print('-', end='\t')
elif col == 1:
print('X', end='\t')
elif col == 2:
print('Ain', end='\t')
elif col == 3:
print('Aout', end='\t')
elif col == 4:
print('Bin', end='\t')
elif col == 5:
print('Bout', end='\t')
print('\n')
print('------------------------------------------')
def actionSpaceSample(self):
return np.random.choice(self.possibleActions)
def maxAction(Q, state, actions):
values = np.array([Q[state,a] for a in actions])
action = np.argmax(values)
return actions[action]
if __name__ == '__main__':
# map magic squares to their connecting square
magicSquares = {18: 54, 63: 14}
env = GridWorld(9, 9, magicSquares)
# model hyperparameters
ALPHA = 0.1
GAMMA = 1.0
EPS = 1.0
Q = {}
for state in env.stateSpacePlus:
for action in env.possibleActions:
Q[state, action] = 0
numGames = 50000
totalRewards = np.zeros(numGames)
env.render()
for i in range(numGames):
if i % 5000 == 0:
print('starting game ', i)
done = False
epRewards = 0
observation = env.reset()
while not done:
rand = np.random.random()
action = maxAction(Q,observation, env.possibleActions) if rand < (1-EPS) \
else env.actionSpaceSample()
observation_, reward, done, info = env.step(action)
epRewards += reward
action_ = maxAction(Q, observation_, env.possibleActions)
Q[observation,action] = Q[observation,action] + ALPHA*(reward + \
GAMMA*Q[observation_,action_] - Q[observation,action])
observation = observation_
if EPS - 2 / numGames > 0:
EPS -= 2 / numGames #end of each episode decrease epsilon
else:
EPS = 0
totalRewards[i] = epRewards
# print('Rewards:\n')
# for r in totalRewards:
# print(r)
print(Q)
plt.plot(totalRewards)
plt.show()