-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q_table.py
35 lines (30 loc) · 1.05 KB
/
Q_table.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
import json
class Q_table:
def __init__(self,filename, defaut_len):
self.filename = filename
self.__table, self.epsilon = self.load()
self.default_len = defaut_len
def get(self, key:str) -> list:
if key in self.__table:
return self.__table[key]
else:
return [0 for i in range(self.default_len)]
def set(self, key:str, data:list):
if sum(data) == 0:
return
self.__table[key] = data
def save(self):
with open(self.filename, "w") as f:
json.dump({"Epsilon":self.epsilon,"Q_table":self.__table}, f)
def load(self):
try:
with open(self.filename, "r") as f:
data = json.load(f)
epsilon = data["Epsilon"]
Q_table = data["Q_table"]
except FileNotFoundError:
Q_table = {}
epsilon = 1.0
with open(self.filename, "w") as f:
json.dump({"Epsilon":epsilon,"Q_table":Q_table}, f)
return Q_table, epsilon