-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid_world.py
More file actions
151 lines (134 loc) · 4.45 KB
/
grid_world.py
File metadata and controls
151 lines (134 loc) · 4.45 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
# From The School of AI's Move 37 Course https://www.theschool.ai/courses/move-37-course/
# Coding demo by Colin Skow
# Forked from https://github.com/lazyprogrammer/machine_learning_examples/tree/master/rl
# Credit goes to LazyProgrammer
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future
import numpy as np
class Grid: # Environment
def __init__(self, width, height, start):
# i is vertical axis, j is horizontal
self.width = width
self.height = height
self.i = start[0]
self.j = start[1]
def set(self, rewards, actions, obey_prob):
# rewards should be a dict of: (i, j): r (row, col): reward
# actions should be a dict of: (i, j): A (row, col): list of possible actions
self.rewards = rewards
self.actions = actions
self.obey_prob = obey_prob
def non_terminal_states(self):
return self.actions.keys()
def set_state(self, s):
self.i = s[0]
self.j = s[1]
def current_state(self):
return (self.i, self.j)
def is_terminal(self, s):
return s not in self.actions
def stochastic_move(self, action):
p = np.random.random()
if p <= self.obey_prob:
return action
if action == 'U' or action == 'D':
return np.random.choice(['L', 'R'])
elif action == 'L' or action == 'R':
return np.random.choice(['U', 'D'])
def move(self, action):
actual_action = self.stochastic_move(action)
if actual_action in self.actions[(self.i, self.j)]:
if actual_action == 'U':
self.i -= 1
elif actual_action == 'D':
self.i += 1
elif actual_action == 'R':
self.j += 1
elif actual_action == 'L':
self.j -= 1
return self.rewards.get((self.i, self.j), 0)
def check_move(self, action):
i = self.i
j = self.j
# check if legal move first
if action in self.actions[(self.i, self.j)]:
if action == 'U':
i -= 1
elif action == 'D':
i += 1
elif action == 'R':
j += 1
elif action == 'L':
j -= 1
# return a reward (if any)
reward = self.rewards.get((i, j), 0)
return ((i, j), reward)
def get_transition_probs(self, action):
# returns a list of (probability, reward, s') transition tuples
probs = []
state, reward = self.check_move(action)
probs.append((self.obey_prob, reward, state))
disobey_prob = 1 - self.obey_prob
if not (disobey_prob > 0.0):
return probs
if action == 'U' or action == 'D':
state, reward = self.check_move('L')
probs.append((disobey_prob / 2, reward, state))
state, reward = self.check_move('R')
probs.append((disobey_prob / 2, reward, state))
elif action == 'L' or action == 'R':
state, reward = self.check_move('U')
probs.append((disobey_prob / 2, reward, state))
state, reward = self.check_move('D')
probs.append((disobey_prob / 2, reward, state))
return probs
def game_over(self):
# returns true if game is over, else false
# true if we are in a state where no actions are possible
return (self.i, self.j) not in self.actions
def all_states(self):
# possibly buggy but simple way to get all states
# either a position that has possible next actions
# or a position that yields a reward
return set(self.actions.keys()) | set(self.rewards.keys())
def standard_grid(obey_prob=1.0, step_cost=None):
# define a grid that describes the reward for arriving at each state
# and possible actions at each state
# the grid looks like this
# x means you can't go there
# s means start position
# number means reward at that state
# . . . 1
# . x . -1
# s . . .
# obey_brob (float): the probability of obeying the command
# step_cost (float): a penalty applied each step to minimize the number of moves (-0.1)
g = Grid(3, 4, (2, 0))
rewards = {(0, 3): 1, (1, 3): -1}
actions = {
(0, 0): ('D', 'R'),
(0, 1): ('L', 'R'),
(0, 2): ('L', 'D', 'R'),
(1, 0): ('U', 'D'),
(1, 2): ('U', 'D', 'R'),
(2, 0): ('U', 'R'),
(2, 1): ('L', 'R'),
(2, 2): ('L', 'R', 'U'),
(2, 3): ('L', 'U'),
}
g.set(rewards, actions, obey_prob)
if step_cost is not None:
g.rewards.update({
(0, 0): step_cost,
(0, 1): step_cost,
(0, 2): step_cost,
(1, 0): step_cost,
(1, 2): step_cost,
(2, 0): step_cost,
(2, 1): step_cost,
(2, 2): step_cost,
(2, 3): step_cost,
})
return g