-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner_1.py
132 lines (101 loc) · 3.37 KB
/
runner_1.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
from __future__ import division
import math
import bus_line
import bus_stop
import lane
import simulator as sim_module
import scipy.stats as st
from config import *
from config_pygame import *
print 'Tiempo, warmup, velocidad autos, velocidad buses, transportadas auto, transportadas buses'
condition = False
class StatisticalSignificanceAchiever(object):
def __init__(self, alpha, margin):
self.values = []
self.alpha = alpha
self.margin = margin
def add_value(self, value):
self.values.append(value)
def is_achieved(self):
n = len(self.values)
if n < 4:
return False
x_sum = 0
x2_sum = 0
for x in self.values:
x_sum += x
x2_sum += x*x
average = x_sum / n
variance = (x2_sum - (x_sum*x_sum)/n)/(n - 1)
std = math.sqrt(variance)
zeta = float(st.norm.ppf(1 - self.alpha/2))
print 'n = %d, zeta = %f, std = %f, average = %f, needed_n = %f' % (
n, zeta, std, average,
math.pow(zeta * std / (self.margin * average), 2)
)
return n > math.pow(zeta * std / (self.margin * average), 2)
stats = [
StatisticalSignificanceAchiever(0.05, 0.02),
StatisticalSignificanceAchiever(0.05, 0.02),
StatisticalSignificanceAchiever(0.05, 0.02),
StatisticalSignificanceAchiever(0.05, 0.02),
]
def _update_histograms(old_histograms, current_histograms, lines):
histograms = {}
for line in lines:
new_histogram = []
old_histogram = old_histograms[line]
current_histogram = current_histograms[line]
for i in range(len(old_histogram)):
new_histogram.append(old_histogram[i] + current_histogram[i])
histograms[line] = new_histogram
return histograms
histograms = None
line_1 = bus_line.BusLine([
bus_stop.BusStop(i) for i in range(40, ROAD_LENGTH, 200)
], 70)
line_2 = bus_line.BusLine([
bus_stop.BusStop(i) for i in range(140, ROAD_LENGTH, 200)
], 120)
line_3 = bus_line.BusLine([
bus_stop.BusStop(i) for i in range(80, ROAD_LENGTH, 200)
], 110)
line_4 = bus_line.BusLine([
bus_stop.BusStop(i) for i in range(180, ROAD_LENGTH, 200)
], 230)
config_lines = [line_1, line_2, line_3, line_4]
while not condition:
config_lanes = [
lane.Lane(True),
lane.Lane(),
lane.Lane(),
lane.Lane(),
]
config_lanes[0].exclusive = True
line_1.last_time_appeared = 0
line_2.last_time_appeared = 0
line_3.last_time_appeared = 0
line_4.last_time_appeared = 0
sim = sim_module.Simulator(config_lanes, config_lines)
if not histograms:
histograms = {}
for line in config_lines:
histograms[line] = sim.build_histogram()
while not sim.has_finished():
sim.loop()
round_results = (
sim.current_time,
sim.warmup_time,
sim.private_speed(),
sim.public_speed(),
sim.private_transported(),
sim.public_transported()
)
stats[0].add_value(round_results[2])
stats[1].add_value(round_results[3])
stats[2].add_value(round_results[4])
stats[3].add_value(round_results[5])
histograms = _update_histograms(histograms, sim.arrival_histograms, config_lines)
condition = all(stat.is_achieved() for stat in stats)
print '%0.2f, %0.2f, %0.4f, %0.4f, %d, %d' % round_results
print histograms