-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrid.py
248 lines (201 loc) · 7.96 KB
/
Grid.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
'''
File name: Grid.py
Author: Eosandra Grund
Date created: 20.04.2022
Date last modified: 20.07.2022
Python Version: 3.10.4
'''
import numpy as np
import os
import Gridworlds
def clearConsole():
'''
clears the console
'''
command = 'clear'
# If Machine is running on Windows, use cls
if os.name in ('nt', 'dos'):
command = 'cls'
os.system(command)
# try new output type
class Gridworld:
"""
This methods creates a gridworld for a RL algorithm
### Reward function:
10 for the terminal state
user input for neg reward field
-0.5 for invalid move (against barrier or outside of the gridworld)
-0.1 for each move where you do not get any other reward
### State transition function:
take random action with probabilitiy epsilon
take given action with probabilitiy 1- epsilon
### Attributes:
x_dim (int>0) : x dimension of gridworld
y_dim (int>0) : y dimension of gridworld
epsilon (0<float<1) : for epsilon-greedy state transition function
agent (list) : [y,x] coordinates of the current agent
initial_agent (list) : [y,x] coordinates of the starting state
terminal (list) : [y,x] coordinates of the terminal state
action (list) : list of all the possible actions in order as strings
world (2D list) : [y][x] with values for all states being int for rewards and np.NaN for barriers
"""
def __init__(self,gridworld = Gridworlds.Gridworlds.GRIDWORLD1):
"""
Initializes a gridworld with all parameters
gives one positive reward in the terminal state
all states as [y,x] in the code, in constructor parameters as [x,y] for convenience
create an empty grid world default hardcoded some examples in Gridworlds
e.g. GRIDWORLD0 :
s 0 X 0 10
0 0 -1 0 0
0 X 0 0 0
0 0 X 0 0
-1 0 0 0 -1
### Keys in the dictionary gridworld:
x_dim (int>0) : x dimension of gridworld
y_dim (int>0) : y dimension of gridworld
epsilon (0<float<1) : for epsilon-greedy state transition function
start [x,y] = starting state of agent for each episode
terminal [x,y] = terminal state with a positive reward
neg_rewards [[x,y,reward],[x,y,reward],...] = list of fields with negative rewards
barrier [[x,y],[x,y],...] = list of fields that are barriers
"""
self.x_dim = gridworld["x_dim"]
self.y_dim = gridworld["x_dim"]
self.epsilon = gridworld["epsilon"]
self.agent = gridworld["start"].copy()
self.agent.reverse() # [y,x]
self.initial_agent = self.agent # needed for reset
self.terminal = gridworld["terminal"].copy()
self.terminal.reverse()# [y,x]
self.action = ['up', 'down' , 'left' , 'right']
# create empty gridworld
world =np.zeros(shape=(self.y_dim,self.x_dim))
# put terminal
world[self.terminal[0],self.terminal[1]] = 10 # [y,x]
# put negative rewards in gridworld
for r in gridworld["neg_reward"]:
world[r[1],r[0]] = r[2]
# put barrier in gridworld
for b in gridworld["barrier"]:
world[b[1],b[0]] = np.NaN
self.world = world
# getter and setter
def getXdim(self):
return self.x_dim
def getYdim(self):
return self.y_dim
def getActions(self):
return self.action
def getTerminal(self):
return self.terminal # [y,x]
def getState(self):
return self.agent # [y,x]
# methods
def isValid(self,x,y):
"""
checks whether coordinates x,y are in the gridworld and not on a barrier
"""
# check whether in the Gridworld
if(x>=0 and x<self.x_dim and y>= 0 and y<self.y_dim):
# check whether the state is a barrier
if not np.array_equal(self.world[y,x], np.NaN, equal_nan=True):
return True
return False
def inTerminal(self):
"""
checks whether the current agent is in the terminal state
"""
if self.agent == self.terminal: # [y,x]
return True
return False
def reset(self):
"""
resets the gridworld to its initial state
"""
self.agent = self.initial_agent
return self.initial_agent
def step(self, action):
"""
applies the state transition dynamics and reward dynamics
based on the state of the environment and the action argument
Arguments:
action int : [0,1,2,3] for ['up', 'down' , 'left' , 'right'] =
returns:
the new state
reward of this step
a boolean indication whether this state is terminal
"""
# state transition policy
# check whether action or for epsilon random other one
take_greedy_action = np.random.choice([True,False],p=[1-self.epsilon, self.epsilon])
# take random action
if (not take_greedy_action):
action = np.random.choice(len(self.action))
# get new place after action
y,x = self.agent[0], self.agent[1] # [y,x]
if action == 0: # up
y -= 1
elif action == 1: # down
y += 1
elif action == 2: # left
x -= 1
elif action == 3: # right
x += 1
# basic reward in case no other for each step
reward = -0.1 # for eachs step done
# check if action is valid, then do action
if self.isValid(x,y):
self.agent = [y,x]
reward = self.world[self.agent[0],self.agent[1]]
else:
# invalid
reward = -0.5
return self.agent, reward , self.inTerminal()
def visualize(self):
"""
visualizes the current state
"""
clearConsole()
print("")
for y in range(self.y_dim):
# left side y values
firstLine = " ||"
thisLine = (" " + str(y) + " ||")[-6:]
nextLine = "____||"
for x in range(self.x_dim):
val = self.world[y,x]
# print one field if agent is there or if barrier on bottom line
if ([y,x]==self.agent):
nextLine += "__A__|"
elif np.array_equal(val, np.NaN, equal_nan=True): # if it is a barrier
nextLine += "XXXXX|"
else:
nextLine += "_____|"
# print vlaues on field in middle line and first line
if (val==0.0): # if
firstLine += " |"
thisLine += " "
elif np.array_equal(val, np.NaN, equal_nan=True): # if it is a barrier
firstLine += "XXXXX|"
thisLine += "XXXXX"
else: # if it has a reward
firstLine += " |"
thisLine += ( " " + str(int(val)) )[-5:]
thisLine += "|"
print(firstLine)
print(thisLine)
print(nextLine)
# at the bottom of the print, pritn x coordinates
topLine = "____||"
line = " ||"
middleLine = " ||"
for x in range(self.x_dim):
line += " |"
middleLine += (" " + str(x) + " |")[-6:]
topLine += "_____|"
print(topLine)
print(line)
print(middleLine)
print(line)
print("")