-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment.py
207 lines (154 loc) · 7.21 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
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
"""
This file emulates the environment behavior for different scenarios.
The grid is an m x m grid, m is either 8 or 4. Certain columns of this grid world push the agent up by some offset
upon entering this column. The agent needs to learn this and move accordingly.
"""
import numpy as np
class GridWorld8x8:
def __init__(self):
self.m = 8
self.n = 8
self.grid = np.zeros((self.m, self.n)) # making grid
self.starting_point = [3, 0] # defining starting state
self.terminal_state = [4, 5] # defining terminal state
# defining windy behavior
self.push_up = {0: 0, 1: 1, 2: 1, 3: 2, 4: 1, 5: 2, 6: 0, 7: 0}
# defining actions
self.actions = {0: "up", 1: "down", 2: "right", 3: "left"}
"""given are the current state(m, n) and the
action taken(0, 1, 2, 3) to return: reward and next state and is_terminal"""
def take_action(self, m, n, action):
# reward: if the terminal state is reached, return 100.
# for all other states, return 0
# we can modify this reward function
reward = 0
# next state:
next_state = []
if action == 0: # up
next_state.append(max(0, m - 1))
next_state.append(n)
if next_state[0] == self.terminal_state[0] and next_state[1] == self.terminal_state[1]:
reward = 100
return reward, next_state, True
else:
return reward, next_state, False
elif action == 1: # down
next_state.append(min(self.m - 1, m + 1))
next_state.append(n)
if next_state[0] == self.terminal_state[0] and next_state[1] == self.terminal_state[1]:
reward = 100
return reward, next_state, True
else:
return reward, next_state, False
elif action == 2: # right : this action is affected by the windy nature
next_state.append(max(0, m - self.push_up[min(self.n - 1, n + 1)]))
next_state.append(min(self.n - 1, n + 1))
if next_state[0] == self.terminal_state[0] and next_state[1] == self.terminal_state[1]:
reward = 100
return reward, next_state, True
else:
return reward, next_state, False
elif action == 3: # left : this action is affected by the windy nature
next_state.append(max(0, m - self.push_up[max(0, n - 1)]))
next_state.append(max(0, n - 1))
if next_state[0] == self.terminal_state[0] and next_state[1] == self.terminal_state[1]:
reward = 100
return reward, next_state, True
else:
return reward, next_state, False
class GridWorld4x4:
def __init__(self):
self.m = 4
self.n = 4
self.grid = np.zeros((self.m, self.n)) # making grid
self.starting_point = [1, 0] # defining starting state
self.terminal_state = [2, 2] # defining terminal state
# defining windy behavior
self.push_up = {0: 0, 1: 1, 2: 1, 3: 0}
# defining actions
self.actions = {0: "up", 1: "down", 2: "right", 3: "left"}
"""given are the current state(m, n) and the
action taken(0, 1, 2, 3) to return: reward and next state and is_terminal
"""
def take_action(self, m, n, action):
# reward: if the terminal state is reached, return 100.
# for all other states, return 0
reward = 0
# next state:
next_state = []
if action == 0: # up
next_state.append(max(0, m - 1))
next_state.append(n)
if next_state[0] == self.terminal_state[0] and next_state[1] == self.terminal_state[1]:
reward = 100
return reward, next_state, True
else:
return reward, next_state, False
elif action == 1: # down
next_state.append(min(self.m - 1, m + 1))
next_state.append(n)
if next_state[0] == self.terminal_state[0] and next_state[1] == self.terminal_state[1]:
reward = 100
return reward, next_state, True
else:
return reward, next_state, False
elif action == 2: # right : this action is affected by the windy nature
next_state.append(max(0, m - self.push_up[min(self.n - 1, n + 1)]))
next_state.append(min(self.n - 1, n + 1))
if next_state[0] == self.terminal_state[0] and next_state[1] == self.terminal_state[1]:
reward = 100
return reward, next_state, True
else:
return reward, next_state, False
elif action == 3: # left : this action is affected by the windy nature
next_state.append(max(0, m - self.push_up[max(0, n - 1)]))
next_state.append(max(0, n - 1))
if next_state[0] == self.terminal_state[0] and next_state[1] == self.terminal_state[1]:
reward = 100
return reward, next_state, True
else:
return reward, next_state, False
"""
This is the classic Tic-Tac-Toe game.
The agent( the one to be trained) plays "X". The opponent plays "O".
The reward function is simple( rather naive): 100 if the agent wins and -100 if the opponent wins, 0 for every
other case
"""
class TicTacToe:
def __init__(self):
self.tic_tac_toe = np.zeros(shape=(3, 3))
self.available_slots = [[i, j] for i in range(3) for j in range(3)]
def play(self, i, j, item):
for t in range(len(self.available_slots)):
if self.available_slots[t][0] == i and self.available_slots[t][1] == j:
del(self.available_slots[t])
break
if item == 'O': # opponent's play
self.tic_tac_toe[i][j] = 1
return self.is_game_over(i, j, item)
else: # agent's play
self.tic_tac_toe[i][j] = 4
return self.is_game_over(i, j, item)
def get_available_slots(self):
return self.available_slots
def is_game_over(self, i, j, item):
# row
if self.tic_tac_toe[i][j] == self.tic_tac_toe[i][(j + 1) % 3] == self.tic_tac_toe[i][(j + 2) % 3]:
return True, 100 if item == 'X' else -100
# column
if self.tic_tac_toe[i][j] == self.tic_tac_toe[(i + 1) % 3][j] == self.tic_tac_toe[(i + 2) % 3][j]:
return True, 100 if item == 'X' else -100
# diagonals
if self.tic_tac_toe[0][0] == self.tic_tac_toe[1][1] == self.tic_tac_toe[2][2] != 0:
return True, 100 if item == 'X' else -100
if self.tic_tac_toe[0][2] == self.tic_tac_toe[1][1] == self.tic_tac_toe[2][0] != 0:
return True, 100 if item == 'X' else -100
# if all slots are done
if len(self.available_slots) == 0:
return True, 0
return False, 0
def clear_game(self):
self.tic_tac_toe = np.zeros(shape=(3, 3))
self.available_slots = [[i, j] for i in range(3) for j in range(3)]
"""
"""