-
Notifications
You must be signed in to change notification settings - Fork 1
/
driving.py
57 lines (43 loc) · 1.47 KB
/
driving.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
from cmp import ControlledMarkovProcess
import random
class Driving(ControlledMarkovProcess):
def __init__(self, responseTime, horizon, terminalReward, length=10, lanes=5):
self.noise = 0
self.lanes = lanes
self.length = length
# horizon is assumed to be finite in this domain
ControlledMarkovProcess.__init__(self, responseTime, horizon, terminalReward)
def cost(self, q):
return 0
def reset(self):
self.state = (0, self.lanes / 2)
def getStates(self):
# in principle, the state space is continuous
# here are the states that are reachable from the start state, which is sufficient!
i = 0
states = []
while i < self.length:
for j in range(self.lanes):
states.append((i, j))
i += 0.1
return states
def sampleState(self):
loc = random.random() * self.length
lane = random.randint(0, self.lanes - 1)
return (loc, lane)
def getStateDistance(self, s1, s2):
return abs(s1[0] - s2[0]) + abs(s1[1] - s2[1])
def getPossibleActions(self, state=None):
return ['W', 'E', 'N']
def isTerminal(self, state):
# only depends on the task horizon
return state[0] >= self.length
def getTransitionStatesAndProbs(self, state, action):
loc = state[0] + 0.1
if action == 'W':
lane = max(state[1] - 1, 0)
elif action == 'E':
lane = min(state[1] + 1, self.lanes - 1)
else:
lane = state[1]
return [((loc, lane), 1)]