-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathvis_agent.py
60 lines (50 loc) · 2.43 KB
/
vis_agent.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
import os
import sys
from workers.ppo_worker import PPOWorker
from workers.rule_worker import RuleBasedWorker
from models.ppo_model import NN_Model
from utils.utils import *
from environments.intersections import IntersectionsEnv, PER_AGENT_STATE_SIZE, GLOBAL_STATE_SIZE, ACTION_SIZE
from copy import deepcopy
from utils.net_scrape import *
def test_worker(worker):
worker.eval_episode({})
# Kill connection to sumo server
worker.env.connection.close()
def test_PPO_agent(constants, device, loaded_model):
_, max_neighborhood_size = get_intersection_neighborhoods(get_net_path(constants))
s_a = get_state_action_size(PER_AGENT_STATE_SIZE, GLOBAL_STATE_SIZE, ACTION_SIZE, max_neighborhood_size, constants)
env = IntersectionsEnv(constants, device, 'vis_ppo', True, get_net_path(constants), vis=True)
local_NN = NN_Model(s_a['s'], s_a['a'], constants['ppo']['hidden_layer_size'], device).to(device)
local_NN.load_state_dict(loaded_model)
worker = PPOWorker(constants, device, env, None, None, local_NN, None, 'ppo', dont_reset=True)
test_worker(worker)
# verbose means test prints at end of each batch of eps
def test_rule_based_agent(constants, device):
env = IntersectionsEnv(constants, device, 'vis_rule_based', True, get_net_path(constants), vis=True)
# Check rule set
rule_set_class = get_rule_set_class(constants['rule']['rule_set'])
rule_set_params = deepcopy(constants['rule']['rule_set_params'])
rule_set_params['phases'] = env.phases
worker = RuleBasedWorker(constants, device, env, rule_set_class(rule_set_params, get_net_path(constants), constants), None, 'rule_based')
test_worker(worker)
def run(load_model_file=None):
# Load constants
constants = load_constants('constants/constants.json')
loaded_model = None
if load_model_file:
loaded_model = torch.load('models/saved_models/' + load_model_file)
if loaded_model:
test_PPO_agent(constants, device, loaded_model)
else:
assert constants['agent']['agent_type'] == 'rule'
test_rule_based_agent(constants, device)
if __name__ == '__main__':
# we need to import python modules from the $SUMO_HOME/tools directory
if 'SUMO_HOME' in os.environ:
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
sys.path.append(tools)
else:
sys.exit("please declare environment variable 'SUMO_HOME'")
device = torch.device('cpu')
run(load_model_file='grid_1-1.pt')