-
Notifications
You must be signed in to change notification settings - Fork 2
/
optimization.py
122 lines (96 loc) · 5.96 KB
/
optimization.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
import pysmac
import pickle
from experiment import create_map, experiment
def save_obj(obj, name):
with open('results/' + name + '.pkl', 'wb') as f:
pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
def load_obj(name):
with open('results/' + name + '.pkl', 'rb') as f:
return pickle.load(f)
def generate_simulation_result(x21, x22, x23, x24, x25, x26,
x31, x32, x33, x34, x35, x36,
x41, x42, x43, x44, x45, x46,
x51, x52, x53, x54, x55, x56,
x61, x62, x63, x64, x65, x66,
x71, x72, x73, x74, x75, x76):
b1 = [1, 1, 1, 1, 1, 1]
b2 = [x21, x22, x23, x24, x25, x26]
b3 = [x31, x32, x33, x34, x35, x36]
b4 = [x41, x42, x43, x44, x45, x46]
b5 = [x51, x52, x53, x54, x55, x56]
b6 = [x61, x62, x63, x64, x65, x66]
b7 = [x71, x72, x73, x74, x75, x76]
model = create_map(routes_per_bus=[b1, b2, b3, b4, b5, b6, b7], name='model')
return experiment([model], 60*18, 10, output_report=False, printing=False)
def avg_waiting_time(x21, x22, x23, x24, x25, x26,
x31, x32, x33, x34, x35, x36,
x41, x42, x43, x44, x45, x46,
x51, x52, x53, x54, x55, x56,
x61, x62, x63, x64, x65, x66,
x71, x72, x73, x74, x75, x76):
stats = generate_simulation_result(x21, x22, x23, x24, x25, x26,
x31, x32, x33, x34, x35, x36,
x41, x42, x43, x44, x45, x46,
x51, x52, x53, x54, x55, x56,
x61, x62, x63, x64, x65, x66,
x71, x72, x73, x74, x75, x76)
return stats[stats.keys()[stats.keys().str.contains('waiting time total')]].mean().values.mean()
def avg_queue_length(x21, x22, x23, x24, x25, x26,
x31, x32, x33, x34, x35, x36,
x41, x42, x43, x44, x45, x46,
x51, x52, x53, x54, x55, x56,
x61, x62, x63, x64, x65, x66,
x71, x72, x73, x74, x75, x76):
stats = generate_simulation_result(x21, x22, x23, x24, x25, x26,
x31, x32, x33, x34, x35, x36,
x41, x42, x43, x44, x45, x46,
x51, x52, x53, x54, x55, x56,
x61, x62, x63, x64, x65, x66,
x71, x72, x73, x74, x75, x76)
return stats[stats.keys()[stats.keys().str.contains('avg people waiting') & ~stats.keys().str.contains('Depot') & ~stats.keys().str.contains('Wegmans-Westbound')]].mean().values.mean()
def avg_occupancy(x21, x22, x23, x24, x25, x26,
x31, x32, x33, x34, x35, x36,
x41, x42, x43, x44, x45, x46,
x51, x52, x53, x54, x55, x56,
x61, x62, x63, x64, x65, x66,
x71, x72, x73, x74, x75, x76):
stats = generate_simulation_result(x21, x22, x23, x24, x25, x26,
x31, x32, x33, x34, x35, x36,
x41, x42, x43, x44, x45, x46,
x51, x52, x53, x54, x55, x56,
x61, x62, x63, x64, x65, x66,
x71, x72, x73, x74, x75, x76)
return stats[stats.keys()[stats.keys().str.contains('avg occupancy') & stats.keys().str.contains('Bus')]].mean().values.mean()
def dead_people(x21, x22, x23, x24, x25, x26,
x31, x32, x33, x34, x35, x36,
x41, x42, x43, x44, x45, x46,
x51, x52, x53, x54, x55, x56,
x61, x62, x63, x64, x65, x66,
x71, x72, x73, x74, x75, x76):
stats = generate_simulation_result(x21, x22, x23, x24, x25, x26,
x31, x32, x33, x34, x35, x36,
x41, x42, x43, x44, x45, x46,
x51, x52, x53, x54, x55, x56,
x61, x62, x63, x64, x65, x66,
x71, x72, x73, x74, x75, x76)
return stats['total dead people'].values.mean()
if __name__ == "__main__":
parameters = dict(
x21=('categorical', [1, 2, 3], 2), x22=('categorical', [1, 2, 3], 2), x23=('categorical', [1, 2, 3], 1),
x24=('categorical', [1, 2, 3], 1), x25=('categorical', [1, 2, 3], 3), x26=('categorical', [1, 2, 3], 1),
x31=('categorical', [1, 2, 3], 2), x32=('categorical', [1, 2, 3], 1), x33=('categorical', [1, 2, 3], 1),
x34=('categorical', [1, 2, 3], 1), x35=('categorical', [1, 2, 3], 2), x36=('categorical', [1, 2, 3], 3),
x41=('categorical', [1, 2, 3], 1), x42=('categorical', [1, 2, 3], 2), x43=('categorical', [1, 2, 3], 2),
x44=('categorical', [1, 2, 3], 1), x45=('categorical', [1, 2, 3], 1), x46=('categorical', [1, 2, 3], 1),
x51=('categorical', [1, 2, 3], 3), x52=('categorical', [1, 2, 3], 3), x53=('categorical', [1, 2, 3], 2),
x54=('categorical', [1, 2, 3], 2), x55=('categorical', [1, 2, 3], 3), x56=('categorical', [1, 2, 3], 2),
x61=('categorical', [1, 2, 3], 1), x62=('categorical', [1, 2, 3], 1), x63=('categorical', [1, 2, 3], 2),
x64=('categorical', [1, 2, 3], 3), x65=('categorical', [1, 2, 3], 2), x66=('categorical', [1, 2, 3], 1),
x71=('categorical', [1, 2, 3], 1), x72=('categorical', [1, 2, 3], 2), x73=('categorical', [1, 2, 3], 2),
x74=('categorical', [1, 2, 3], 1), x75=('categorical', [1, 2, 3], 1), x76=('categorical', [1, 2, 3], 1),
)
opt = pysmac.SMAC_optimizer()
value, parameters = opt.minimize(avg_queue_length, 1000, parameters)
print(('Lowest function value found: %f' % value))
print(('Parameter setting %s' % parameters))
save_obj(parameters, 'dead_people')