-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomstopcount.py
More file actions
executable file
·102 lines (91 loc) · 3.28 KB
/
randomstopcount.py
File metadata and controls
executable file
·102 lines (91 loc) · 3.28 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
#! /usr/bin/python
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 2 00:18:38 2015
@author: Damian
"""
from __future__ import division
import numpy as np
import matplotlib
from load_params import ROADLENGTH, TRIALS, REAL_LANES, \
VIRTUAL_LANES, SLOWDOWN, LANE_CHANGE_PROB
matplotlib.use("Agg")
matplotlib.rcParams.update({'font.size': 15})
matplotlib.rcParams.update({'axes.labelsize': 17})
import matplotlib.pyplot as plt
import glob
import re
import os
from subprocess import call
import h5py
POS = 0
LANE = 1
SPEED = 2
SIZE = 3
PSLOW = 4
LAST = -1
def randcount(dist_trial):
"""Computes the throughput of one trial of the simulation."""
slowsum = 0
decsum = 0
for time in xrange(len(dist_trial)-1):
for nveh in xrange(len(dist_trial[time])):
vehicle = dist_trial[time+1][nveh]
vehicle_past = dist_trial[time][nveh]
if vehicle[PSLOW] and vehicle[LANE]==1 and vehicle[SIZE]==4:
slowsum += 1
if vehicle_past[SPEED] >= vehicle[SPEED] and vehicle[SPEED]!=5:
decsum += 1
return [slowsum, decsum]
def randcount_all(dist_trial):
"""Computes the throughput of one trial of the simulation."""
slowsum = 0
decsum = 0
for time in xrange(len(dist_trial)-1):
for nveh in xrange(len(dist_trial[time])):
vehicle = dist_trial[time+1][nveh]
vehicle_past = dist_trial[time][nveh]
if vehicle[PSLOW] and vehicle[SIZE]==4:
slowsum += 1
if vehicle_past[SPEED] >= vehicle[SPEED] and vehicle[SPEED]!=5:
decsum += 1
return [slowsum, decsum]
def load(_ratio, _density):
"""Loads data from the hdf5 dataset."""
vehicledata = np.array([], dtype=np.int8)
filename = "CarRatio.%.2f_Density.%.2f.h5" % (_ratio, _density)
call(['bunzip2', filename + '.bz2'])
fid = h5py.File(filename, 'r')
for n in xrange(TRIALS):
group = "CarRatio::%.2f/Density::%.2f/" % (_ratio, _density)
_trial = "Trial::%04d" % (n + 1)
dset = fid[group + _trial]
vehicledata = np.append(vehicledata, dset)
vehicledata = np.reshape(vehicledata, (TRIALS, dset.shape[0],
dset.shape[1],
dset.shape[2]))
fid.close()
call(["bzip2", "-6", filename])
return vehicledata
if __name__ == "__main__":
import time
to = time.time()
__plot_ratios__ = [0, 0.25, 0.5, 0.75, 1]
DIRNAME = os.path.split(os.getcwd())[1]
DENSITIES = np.arange(0.01, 1.,0.01)
RATIOS = np.array([])
FILES = glob.glob("CarRatio*.npz")
for i in FILES:
car_ratio = float(re.findall(r"CarRatio.(\d.\d+)", i)[0])
if car_ratio not in RATIOS and car_ratio in __plot_ratios__:
RATIOS = np.append(RATIOS, car_ratio)
RATIOS.sort()
RANDSTOP = [[None for i in range(len(DENSITIES))]
for j in range(len(RATIOS))]
for x, ratio in enumerate(RATIOS):
for y, density in enumerate(DENSITIES):
data = np.load("CarRatio.%.2f_Density.%.2f.npz" % (ratio, density))["data"]
RANDSTOP[x][y] = [randcount_all(trial)
for trial in data]
np.savez_compressed("rand_all.npz", RANDSTOP=RANDSTOP)
print time.time()-to