-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmytaxi_stage2.py
142 lines (127 loc) · 4.89 KB
/
mytaxi_stage2.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
import sys
from six import StringIO
from gym import utils
from gym.envs.toy_text import discrete
import numpy as np
MAP = [
"+---------+",
"|R: | : :G|",
"| : | : : |",
"| : : : : |",
"| | : : : |",
"|Y| : |B: |",
"+---------+",
]
class MyTaxiEnv(discrete.DiscreteEnv):
"""
The Taxi Problem
from "Hierarchical Reinforcement Learning with the MAXQ Value Function Decomposition"
by Tom Dietterich
rendering:
- blue: passenger
- magenta: destination
- yellow: empty taxi
- green: full taxi
- other letters: locations
Modified by Takashi Nagata
- Added get_idx function
"""
metadata = {'render.modes': ['human', 'ansi']}
def __init__(self):
self.desc = np.asarray(MAP,dtype='c')
self.locs = locs = [(0,0), (0,4), (4,0), (4,3)]
nS = 500
nR = 5
nC = 5
maxR = nR-1
maxC = nC-1
isd = np.zeros(nS)
nA = 6
P = {s : {a : [] for a in range(nA)} for s in range(nS)}
for row in range(5):
for col in range(5):
for passidx in range(5):
for destidx in range(4):
state = self.encode(row, col, passidx, destidx)
if passidx < 4 and passidx != destidx:
isd[state] += 1
for a in range(nA):
# defaults
newrow, newcol, newpassidx = row, col, passidx
reward = -1
done = False
taxiloc = (row, col)
if a==0:
newrow = min(row+1, maxR)
elif a==1:
newrow = max(row-1, 0)
if a==2 and self.desc[1+row,2*col+2]==b":":
newcol = min(col+1, maxC)
elif a==3 and self.desc[1+row,2*col]==b":":
newcol = max(col-1, 0)
elif a==4: # pickup
if (passidx < 4 and taxiloc == locs[passidx]):
newpassidx = 4
else:
reward = -10
elif a==5: # dropoff
if (taxiloc == locs[destidx]) and passidx==4:
done = True
reward = 20
elif (taxiloc in locs) and passidx==4:
newpassidx = locs.index(taxiloc)
else:
reward = -10
newstate = self.encode(newrow, newcol, newpassidx, destidx)
P[state][a].append((1.0, newstate, reward, done))
isd /= isd.sum()
discrete.DiscreteEnv.__init__(self, nS, nA, P, isd)
def encode(self, taxirow, taxicol, passloc, destidx):
# (5) 5, 5, 4
i = taxirow
i *= 5
i += taxicol
i *= 5
i += passloc
i *= 4
i += destidx
return i
def decode(self, i):
out = []
out.append(i % 4)
i = i // 4
out.append(i % 5)
i = i // 5
out.append(i % 5)
i = i // 5
out.append(i)
assert 0 <= i < 5
return reversed(out)
def render(self, mode='human'):
outfile = StringIO() if mode == 'ansi' else sys.stdout
out = self.desc.copy().tolist()
out = [[c.decode('utf-8') for c in line] for line in out]
taxirow, taxicol, passidx, destidx = self.decode(self.s)
def ul(x): return "_" if x == " " else x
if passidx < 4:
out[1+taxirow][2*taxicol+1] = utils.colorize(out[1+taxirow][2*taxicol+1], 'yellow', highlight=True)
pi, pj = self.locs[passidx]
out[1+pi][2*pj+1] = utils.colorize(out[1+pi][2*pj+1], 'blue', bold=True)
else: # passenger in taxi
out[1+taxirow][2*taxicol+1] = utils.colorize(ul(out[1+taxirow][2*taxicol+1]), 'green', highlight=True)
di, dj = self.locs[destidx]
out[1+di][2*dj+1] = utils.colorize(out[1+di][2*dj+1], 'magenta')
outfile.write("\n".join(["".join(row) for row in out])+"\n")
if self.lastaction is not None:
outfile.write(" ({})\n".format(["South", "North", "East", "West", "Pickup", "Dropoff"][self.lastaction]))
else: outfile.write("\n")
# No need to return anything for human
if mode != 'human':
return outfile
def get_idx(self):
"""return a list contains index
"""
taxirow, taxicol, passidx, destidx = self.decode(self.s)
pi, pj = self.locs[passidx]
di, dj = self.locs[destidx]
return [taxirow, taxicol, di, dj, pi, pj]