forked from Flenji/traffic_light_optimization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sumoAgent_rand_train_test.py
199 lines (149 loc) · 6.55 KB
/
sumoAgent_rand_train_test.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
196
197
198
199
# -*- coding: utf-8 -*-
"""
This module is used to train a single agent over a specific network design with a variety of random flows and test it in a specific scenario.
Authors: AAU CS (IT) 07 - 03
"""
import sumo_rl
from pettingzoo import AECEnv
import matplotlib.pyplot as plt
import numpy as np
import ddqn
import utility
import os
import observation_spaces
import time
import reward_fncs
from lxml import etree as ET
import random
def generate_random_partition(total_sum, num_elements):
"""
"""
# Generate num_elements random numbers between 0 and total_sum
#random_numbers = random.sample(range(total_sum), num_elements)
random_numbers = random.choices(range(3, 31), k=num_elements)
# Ensure that the total sum of numbers adds up to 100
while sum(random_numbers) > total_sum:
index_to_adjust = random.randint(0, num_elements - 1)
if random_numbers[index_to_adjust] > 3:
random_numbers[index_to_adjust] -= 1
while sum(random_numbers) < total_sum:
index_to_adjust = random.randint(0, num_elements - 1)
random_numbers[index_to_adjust] += 1
return random_numbers
def define_new_flows(route_file, number_routes):
"""
"""
# Read XML file
tree = ET.parse(route_file)
root = tree.getroot()
# Generate random values
vehsPerHour = round(float(random.randint(400, 2300)), 2)
route_probabilities = generate_random_partition(100, number_routes)
# Modify attributes in the route file with the random values
root.find('.//flow[@id="random"]').set('vehsPerHour', str(vehsPerHour))
for i in range(len(route_probabilities)):
route = './/route[@id="r_' + str(i) + '"]'
root.find(route).set('probability', str(route_probabilities[i]))
# Write updated XML back to the file
tree.write(route_file)
start_time = time.time()
net_file = 'Networks/single_agent_networks/1w/1w.net.xml'
train_route_file = 'Networks/single_agent_networks/1w/1w_random.rou.xml'
test_suffix='_low'
test_route_file = 'Networks/single_agent_networks/1w/1w'+test_suffix+'.rou.xml'
observation_class = observation_spaces.ObservationFunction2
reward_function = reward_fncs._combined_reward3
num_seconds = 7200
### SETTING HYPERPARAMETERS
learning_rate = 0.0001
mem_size = 3000000
eps_dec = 1.5e-6
batch_size = 36
gamma = 0.9
eps_min = 0.1
replace = 1000
checkpoint_dir = "model_checkpoint"
#Load or Save model?
SAVE = False
LOAD = True
agent_suffix = "_reward3_randtraining"
epsilons = []
scores = []
name = "ddqn" + agent_suffix
ddqn_agent = ddqn.Agent(learning_rate=learning_rate, input_dim= (21,), n_actions=4,\
mem_size=mem_size, eps_dec=eps_dec, eps_min = eps_min, gamma = gamma,\
batch_size= batch_size, name = name, checkpoint_dir= checkpoint_dir,\
replace = replace, deeper=True)
if LOAD:
ddqn_agent.load_model()
num_simulations = 450
def train(num_simulations):
"""
Trains the agents for minimum min_learning_steps. If the one learning episode ends (the simulations ends)
and the ammount of learning steps taken is >= min_learning_steps the training is done.
"""
for n in range(num_simulations):
define_new_flows(train_route_file, 12)
env = sumo_rl.parallel_env(net_file= net_file,
route_file=train_route_file,
reward_fn=reward_function,
observation_class=observation_class,
use_gui=False,
num_seconds=num_seconds)
observations = env.reset()[0]
print(f"Generation: {n}")
while env.agents: #contains agents as long simulation is running
actions = {agent: ddqn_agent.get_action(observations[agent]) for agent in env.agents}
observations_, rewards, terminations, truncations, infos = env.step(actions)
for agent in env.agents:
obs = observations[agent] #current observation of agent
action = actions[agent]
obs_, reward, termination, truncation, info = observations_[agent],\
rewards[agent], terminations[agent], truncations[agent], infos[agent]
done = termination or truncation #TODO: see if this is needed for SUMO
ddqn_agent.learn(obs, action, reward, obs_, done)
scores.append(reward)
epsilons.append(ddqn_agent.epsilon)
observations = observations_ #setting new observation as current observation
if n % 10 == 0:
if SAVE:
ddqn_agent.save_model()
utility.save_object(scores, "scores"+agent_suffix, "results")
utility.save_object(epsilons, "epsilons"+ agent_suffix, "results")
print(f"current epsilon: {ddqn_agent.epsilon}")
env.close()
utility.plot_learning_curve(scores, epsilons, filename = "model_"+agent_suffix, path="results", mean_over=2400)
def test(random = False, metrics = False, use_gui = True):
"""
Function test the agent. If random = True, agents choose just random actions.
"""
if metrics:
additional_sumo_cmd = "--additional-files additional.xml"
else:
additional_sumo_cmd = ""
env = sumo_rl.parallel_env(net_file=net_file,
route_file=test_route_file,
use_gui=use_gui,
num_seconds=num_seconds,
observation_class = observation_class,
reward_fn = reward_function,
additional_sumo_cmd = additional_sumo_cmd,
sumo_seed = 0
)
observations = env.reset()[0]
while env.agents:
if random:
actions = {agent: env.action_space(agent).sample() for agent in env.agents}
else:
actions = {agent: ddqn_agent.get_test_action(observations[agent]) for agent in env.agents}
observations_, rewards, terminations, truncations, infos = env.step(actions)
observations = observations_ #setting new observation as current observation
env.close()
if metrics:
file_name_old = utility.createPath("metrics","metrics.xml")
file_name_new = utility.createPath("metrics","metrics"+agent_suffix+test_suffix+".xml")
os.rename(file_name_old,file_name_new)
#train(num_simulations)
end_time = time.time()
print(f"Runtime {utility.get_time_formatted(end_time-start_time)}")
test(metrics=True,use_gui= True)