-
Notifications
You must be signed in to change notification settings - Fork 8
/
environments.py
174 lines (144 loc) · 5.21 KB
/
environments.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
'''
Environment definitions for the RC-NFQ algorithm.
'''
import numpy as np
from scipy import stats
class SimpleWorld:
"""Basic test world.
"""
def __init__(self, max_steps):
self.actions = [0, 1]
self.nb_actions = len(self.actions)
self.states = [0, 1, 2, 3]
self.nb_states = len(self.states)
self.terminal_states = [0, 0, 0, 1]
self.start_state = 1
self.state = self.start_state
self.rewards = [-1, -0.25, -0.25, 1]
self.max_steps = max_steps
stochastic = True
if stochastic:
self.T = {
0: np.array(((0.9, 0.1, 0, 0),
(0.9, 0, 0.1, 0),
(0, 0.9, 0.1, 0),
(0, 0, 0, 1))),
1: np.array(((0.1, 0.9, 0, 0),
(0.1, 0, 0.9, 0),
(0, 0.1, 0, 0.9),
(0, 0, 0, 1)))
}
else:
self.T = {
0: np.array(((1, 0, 0, 0),
(1, 0, 0, 0),
(0, 1, 0, 0),
(0, 0, 0, 1))),
1: np.array(((0, 1, 0, 0),
(0, 0, 1, 0),
(0, 0, 0, 1),
(0, 0, 0, 1)))
}
def reset(self):
self.state = self.start_state
def terminal(self):
return self.terminal_states[self.state]
def act(self, action):
try:
assert not self.terminal()
except AssertionError as e:
e.args += ('Further action not permitted: terminal state ' +
' reached. Episode is over.',)
raise
probs = self.T[action][self.state, :]
pmf = stats.rv_discrete(name='pmf',
values=(self.states, probs))
successor_state = pmf.rvs()
self.state = successor_state
r = self.rewards[successor_state]
return r
class GridWorld:
"""Defines a simple MDP environment with 3 states.
"""
def __init__(self, max_steps=50, start_state=0):
"""Initializes the environment.
"""
# Define the actions:
# 0=left, 1=right, 2=up, 3=down
self.actions = [0, 1, 2, 3]
self.nb_actions = len(self.actions)
self.max_steps = max_steps
# Define the transition matrix for each action
# Format of entries in the transition probabilities dictionary:
# a: probability_matrix
# where a is the action, and probability_matrix consists of row
# vectors of probabilities where the row index corresponds to
# the current state and the column index corresponds to the successor
# state.
self.T = {}
for a in np.arange(self.nb_actions):
self.T[a] = np.load('gridworld_{}.npy'.format(a))
# Format of entries in the rewards dictionary:
# s: r
self.rewards = np.load('gridworld_r.npy')
# Load the states and terminal states
self.terminal_states = np.load('gridworld_terminal.npy')
self.terminal_state_indices = np.where(self.terminal_states == 1)
self.states = np.arange(self.terminal_states.shape[0])
self.nb_states = len(self.states)
self.state_dim = self.nb_states
# Define the start state and set the current state to it
self.start_state = start_state
self.state = self.start_state
def act(self, action):
try:
assert not self.terminal()
except AssertionError as e:
e.args += ('Further action not permitted: terminal state ' +
' reached. Episode is over.',)
raise
probs = self.T[action][self.state, :]
pmf = stats.rv_discrete(name='pmf',
values=(self.states, probs))
successor_state = pmf.rvs()
self.state = successor_state
r = self.rewards[successor_state]
return r
def terminal(self):
return self.terminal_states[self.state]
def reset(self):
self.state = self.start_state
class SimpleRobot:
"""Defines a simple robot environment
"""
def __init__(self, max_steps=50):
"""Initializes the environment.
"""
# Actions: stop, forward, backward, left, right
self.actions = np.arange(5)
self.nb_actions = len(self.actions)
self.max_steps = 50
# States: a discretization of sonar distances into 10 buckets
self.states = np.arange(10)
self.state_dim = len(self.states)
def terminal(self):
return False
def reset(self):
pass
class VisionRobot:
"""Defines a robot environment where the state input is a video camera
stream
"""
def __init__(self, max_steps=50):
"""Initializes the environment.
"""
# Actions: forward, backward, left, right
self.actions = np.arange(4)
self.nb_actions = len(self.actions)
self.max_steps = 50
# States: grayscale 64x64 camera images
self.state_dim = (1, 64, 64)
def terminal(self):
return False
def reset(self):
pass