-
Notifications
You must be signed in to change notification settings - Fork 0
/
Auto_STGCN.py
195 lines (183 loc) · 7.47 KB
/
Auto_STGCN.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import argparse
import json
import mxnet as mx
import dill
from ExperimentDataLogger import *
from Env import *
import numpy as np
from utils.utils import generate_action_dict
from collections import defaultdict
import os
from copy import *
import wandb
class QTable:
def __init__(self, config):
self.n = config['n']
# key: tuple, state value: dict:key:tuple, with all possible actions, values:Q_values
self.Qtable = defaultdict(lambda: defaultdict(lambda: 0.0))
self.actions = generate_action_dict(self.n)
def get_Q_value(self, state, action):
if isinstance(state, np.ndarray):
state = tuple(state.tolist())
if isinstance(action, np.ndarray):
action = tuple(action.tolist())
return self.Qtable[state][action]
def get_action(self, state):
# return (action, max_Q_value)
if isinstance(state, np.ndarray):
state = tuple(state.tolist())
Q_values = []
actions = self.actions[state[0]]
for action in actions:
action = tuple(action.tolist())
Q_values.append(self.get_Q_value(state, action))
Q_values = np.array(Q_values)
Q_value = np.max(Q_values)
action = np.array(list(actions[np.argmax(Q_values)]))
return action, Q_value
def set_Q_value(self, state, action, value):
if isinstance(state, np.ndarray):
state = tuple(state.tolist())
if isinstance(action, np.ndarray):
action = tuple(action.tolist())
self.Qtable[state][action] = value
def train_QTable(config, log_name):
#####################
# set up parameters #
######################
lr = config['lr']
episodes = config['episodes']
gamma = config['gamma']
n = config['n']
epsilon = config['epsilon_initial']
epsilon_decay_step = config["epsilon_decay_step"]
epsilon_decay_ratio = config["epsilon_decay_ratio"]
if isinstance(config['ctx'], list):
ctx = [mx.gpu(i) for i in config['ctx']]
elif isinstance(config['ctx'], int):
ctx = mx.gpu(config['ctx'])
else:
raise Exception("config_ctx error:" + str(config['ctx']))
logger = Logger(log_name, config, False)
#######################
# init QTable and Env #
#######################
q_table = QTable(config)
env = GNNEnv(config, ctx, logger)
##############
# training #
##############
episode = 0
exception_cnt = False
while episode < episodes or exception_cnt >= episodes:
logger.set_episode(episode)
start_time = time()
print("====================================================")
print(f"episode:{episode + 1}/{episodes}")
# S{-2}
obs = env.reset()
done = False
exception_flag = False
# store trajectory and edit the reward
local_buffer = []
while not done:
if np.random.random() >= epsilon:
action, _ = q_table.get_action(obs)
print(f"state:\n{obs}\naction:{action} QTable")
else:
action = generate_random_action(obs, n)
print(f"state:\n{obs}\naction:{action} random")
# s{-1}-S{T}, T<=n
# => len(local_buffer)<= T+2
logger(state=obs, action=action)
next_obs, reward, done, info = env.step(action)
exception_flag = info['exception_flag']
local_buffer.append([obs, action, reward, next_obs, done])
obs = next_obs
# edit reward and add into buffer
reward = local_buffer[-1][2] / len(local_buffer)
print(f" reward:{reward}")
for i in range(len(local_buffer)):
local_buffer[i][2] = reward
logger(reward=reward)
wandb.log({"reward": reward}, sync=False)
episode += 1
# training
for obs, action, reward, next_obs, done in local_buffer:
if not done:
q_S_A = q_table.get_Q_value(obs, action)
q_table.set_Q_value(obs, action,
q_S_A + lr * (reward + gamma * q_table.get_action(next_obs)[1] - q_S_A))
# epsilon decay
epsilon *= pow(epsilon_decay_ratio, episode / epsilon_decay_step)
wandb.log({"epsilon": epsilon}, sync=False)
if episode % 100 == 0:
with open(logger.log_path + 'QTable.dill', 'wb') as f:
dill.dump(q_table, f)
episode_time = time() - start_time
print(f" episode_time_cost:{episode_time}")
logger(time=episode_time)
logger.update_data_units()
logger.flush_log()
# get best model from logger
data_unit = np.array(logger.data_unit)
arr = []
for episode, data in enumerate(data_unit):
# Compatible with 'duplicate recording reward each episode' bug in the results of paper experiment
if isinstance(data[-2], list):
reward = data[-2][-1]
else:
reward = data[-2]
arr.append([data[1], reward, data[0], data[2], data[3], data[4], data[6], episode])
arr = np.array(arr)
arr = arr[np.argsort(arr[:, 1])]
print(f'action:{arr[-1, 0]} reward:{arr[-1, 1]} episode:{arr[-1, -1]} time:{np.squeeze(arr[-1, 4])[-1]}')
print(f'log file save to {logger.log_path}')
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--data', type=str, default=None)
parser.add_argument('--train_length', type=int, default=None)
parser.add_argument('--pred_length', type=int, default=None)
parser.add_argument('--split_ratio', type=list, default=None)
parser.add_argument('--time_max', type=float, default=None)
parser.add_argument('--epochs', type=int, default=None)
parser.add_argument('--epsilon_initial', type=float, default=None)
parser.add_argument('--epsilon_decay_step', type=int, default=None)
parser.add_argument('--epsilon_decay_ratio', type=float, default=None)
parser.add_argument('--gamma', type=float, default=None)
parser.add_argument('--lr', type=float, default=None)
parser.add_argument('--episodes', type=int, default=None)
parser.add_argument('--n', type=int, default=None)
parser.add_argument('--ctx', type=int, default=None)
args = parser.parse_args()
config_filename = './Config/default.json'
with open(config_filename, 'r') as f:
config = json.loads(f.read())
# override default config
dataset = args.data.upper()
if dataset == 'PEMS03':
config["id_filename"] = "data/PEMS03/PEMS03.txt"
config["num_of_vertices"] = 358
elif dataset == 'PEMS04':
config["id_filename"] = None
config["num_of_vertices"] = 307
elif dataset == 'PEMS07':
config["id_filename"] = None
config["num_of_vertices"] = 883
elif dataset == 'PEMS08':
config["id_filename"] = None
config["num_of_vertices"] = 170
else:
raise Exception(f'Input data is {args.data}, only support PEMS03/04/07/08')
config["adj_filename"] = f"data/{dataset}/{dataset}.csv"
config["graph_signal_matrix_filename"] = f"data/{dataset}/{dataset}.npz"
config["pearsonr_adj_filename"] = f"data/{dataset}/{dataset}_pearsonr.npz"
arg_dict = copy(vars(args))
for key, value in vars(args).items():
if value is None:
arg_dict.pop(key)
config.update(arg_dict)
print(json.dumps(config, sort_keys=True, indent=4))
log_name = input('log_name:\n')
wandb.init(project="GNN2", config=config, notes=log_name)
train_QTable(config, log_name)