-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparameter_optimizer.py
More file actions
274 lines (214 loc) · 7.64 KB
/
parameter_optimizer.py
File metadata and controls
274 lines (214 loc) · 7.64 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
from elevator import Elevator
from simulation import Simulation
from building import Building
import distributions
import policies
from concurrent.futures import ThreadPoolExecutor
import numpy as np
import multiprocessing as mp
import matplotlib.pyplot as plt
from itertools import product
from collections import defaultdict
import random
import csv
current_awt = 9999
current_params = []
num_epochs = 0
learning_rate = 1.03
average_of = 10
simulation_length = 60 * 60 * 6
# Enable gradient descent
gradient_descent = False
# Enable plotting
plot = False
save_plot = "plotter/plots/parameter_optimizer.pdf"
distribution = distributions.HighDensityDistribution() if num_epochs > 0 else None
load_params = "param_optimizer/save_params.txt"
store_params = "param_optimizer/save_params.txt"
def map_parameters(parameters, changed_parameter):
"""
Maps the parameters of the minimizing function to the parameters of the model
:param parameters: parameters of the minimizing function
:type parameters: list
:param changed_parameter: changed parameter
:type changed_parameter: tuple
:return: parameters of the model
:rtype: list
"""
ret = parameters + [1]
if (changed_parameter[0] >= 0):
ret[changed_parameter[0]] *= changed_parameter[1]
return ret
def create_simulation(args):
"""
Initializes a new simulation
:param args: arguments for the simulation
:type args: tuple
:return: simulation
:rtype: Simulation
"""
global current_params
global distribution
average_index, parameter_index, parameter_value = args
policy = policies.PWDPPolicy
policy_arguments = map_parameters(
current_params, (parameter_index, parameter_value))
# Start simulation at a specific time
hours, minutes, seconds = 0, 0, 0
elevators = []
# Standard scenario, set parameters automatically
floor_amount = distribution.floor_amount
amount_of_elevators = distribution.amount_of_elevators
for i in range(amount_of_elevators):
elevators.append(
Elevator(
0,
floor_amount - 1,
policy(
*policy_arguments),
distribution.elevator_capacity))
simulation = Simulation(
Building(
elevators=elevators,
floor_amount=floor_amount,
distribution=distribution
)
)
simulation.set_time(hours=hours, minutes=minutes, seconds=seconds)
return simulation
def run_simulation(args):
"""
Run a single simulation
:param args: arguments for the simulation
:type args: tuple
:return: average waiting time
:rtype: float
"""
(average_index, parameter_index, parameter_value), simulation = args
simulation.run(seconds=simulation_length, time_scale=-1)
awt = simulation.statistics.calculate_average_waiting_time()
return (average_index, parameter_index,
current_params[parameter_index] * parameter_value), awt
def update_results(results):
"""
Update the current model with new parameters if a performance increase has been detected
:param results: results of the simulations
:type results: list
"""
global current_awt
global current_params
result_dict = dict(results)
# Accumulate averages
accumulated_values = defaultdict(list)
for (_, parameter_index, parameter_value), awt in result_dict.items():
accumulated_values[(parameter_index, parameter_value)].append(awt)
mean_results = {(parameter_index, parameter_value): sum(awts) / len(awts)
for (parameter_index, parameter_value), awts in accumulated_values.items()}
# Choose min AWT of paramter
min_values = defaultdict(lambda: (float('inf'), None))
for (parameter_index, parameter_value), awt in mean_results.items():
current_min_value, _ = min_values[parameter_index]
if awt < current_min_value:
min_values[parameter_index] = (awt, parameter_value)
# Set best parameters
current_awt = min_values[-1][0]
for index, (awt, parameter_value) in min_values.items():
if index >= 0 and awt < current_awt:
current_params[index] = parameter_value
def write_result(awt, params):
"""
Write the results to text
:param awt: average waiting time
:type awt: float
:param params: parameters
:type params: list
"""
s = ','.join(map(str, [awt] + params))
with open(store_params, "a") as file:
file.write(s + "\n")
if plot or gradient_descent:
# Load parameters
with open(load_params, "r") as file:
lines = file.readlines()
if lines:
l = lines[-1].strip().split(',')
current_awt = float(l[0])
current_params = [float(value) for value in l[1:]]
else:
print("File is empty.")
min_jump = 1
max_jump = 1
for i in range(num_epochs):
if not gradient_descent:
continue
pro = product(
range(average_of), range(
len(current_params)), [
min_jump, max_jump])
# Run simulations
simulations = {}
for a in range(average_of):
p = (i, -1, 1)
simulations[p] = create_simulation(p)
for p in pro:
simulations[p] = create_simulation(p)
results = []
with ThreadPoolExecutor(max_workers=10) as executor:
results = executor.map(run_simulation, simulations.items())
# Apply simulation results
update_results(results)
min_jump = random.uniform(1 / learning_rate, 1)
max_jump = random.uniform(1, learning_rate)
print(f"Epoch: {i}\tAWT\t{current_awt}\t{current_params}")
write_result(current_awt, current_params)
# -----------------------------------------------------------------------------------------------------------
# Plot parameters and time
if not plot:
exit()
params_over_time = []
awt_over_time = []
with open(store_params, 'r') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
awt_over_time.append(float(row[0]))
# Extract the rest of the columns
params_over_time.append(map(float, row[1:]))
# Transpose the params list for easier plotting
params_transposed = list(map(list, zip(*params_over_time)))
# Create a figure and axis for the first y-axis
fig, ax1 = plt.subplots()
# Plot parameter values on the first y-axis
for param_values in params_transposed:
ax1.plot(param_values, marker='o')
# Customize the first y-axis
ax1.set_xlabel('Epochs')
ax1.set_ylabel('Parameter Values', color='tab:grey')
ax1.tick_params('y')
ax1.legend(['ElevatorButtonWeight',
'ElevatorButtonTimeWeight',
'FloorButtonWeight',
'FloorButtonTimeWeight',
'CompetitorWeight',
'DistanceWeight'])
# ax1.legend(['ElevatorButtonTimeWeight', 'FloorButtonTimeWeight', 'competitor_weight', 'distance_weight'])
# Create a secondary y-axis
ax2 = ax1.twinx()
# Plot scores on the secondary y-axis
minAWT = min(awt_over_time)
ax2.plot(awt_over_time, marker='s', alpha=0)
ax2.set_ylabel('AWT', color='tab:grey')
ax2.tick_params('y')
ax2.fill_between(
np.arange(
len(awt_over_time)),
minAWT - 2,
awt_over_time,
color='gray',
alpha=0.3)
ax2.autoscale(axis='y')
# Plot a transparent grey area for the scores
# Customize the plot
plt.title('Parameter Values Over Epochs')
fig.set_size_inches(10, 7)
plt.savefig(save_plot, dpi=300)
plt.show()