-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQTable.py
More file actions
47 lines (32 loc) · 1.28 KB
/
QTable.py
File metadata and controls
47 lines (32 loc) · 1.28 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
import numpy as np
import random
from resources.GameUtils import initialize_traffic
ACTIONS = {0: "LEFT", 1: "STAY", 2: "RIGHT"}
def random_action():
return random.choice(list(ACTIONS.keys()))
class QTable:
def __init__(self):
self.q_table, self.feature_encoding = initialize_q_table()
def get_space_and_encoding():
car, obstacle = initialize_traffic()
car_space = car.get_binned_space()
obstacle_space = obstacle.get_binned_space()
space = {**car_space, **obstacle_space}
features = list(space.keys())
feature_encoding = {features[0]: 1}
prev_feature = features[0]
observation_space = space[features[0]]
for feature in features[:0:-1]: # skip first feature
space_value = space[feature]
observation_space *= space_value
feature_encoding[feature] = space[prev_feature]*feature_encoding[prev_feature]
prev_feature = feature
return observation_space+1, feature_encoding
def initialize_q_table():
observation_space, feature_encoding = get_space_and_encoding()
return np.zeros([observation_space, len(ACTIONS)]), feature_encoding
def encode(data, feature_encoding):
state = 0
for feature, encoding in feature_encoding.items():
state += encoding * data[feature]
return int(state)