-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotsim.py
More file actions
108 lines (97 loc) · 4.91 KB
/
plotsim.py
File metadata and controls
108 lines (97 loc) · 4.91 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
'''
A command-line interface for the simulation.
'''
import numpy as np
from numpy import random
from enum import Enum
import sys
import argparse
import matplotlib.pyplot as plt
import matplotlib
from carsharesim import run_simulation
matplotlib.use('TkAgg')
def plot(data, xLabel, yLabel, title):
fig, ax = plt.subplots()
ax.scatter(*zip(*data), s=0.5)
ax.set_xlabel(xLabel)
ax.set_ylabel(yLabel)
ax.set_title(title)
plt.show()
def main(argv):
parser = argparse.ArgumentParser(
prog = 'carsharesim',
description = 'A stochastic simulation of a station-based car sharing system.'
)
parser.add_argument('-l', '--lambda', default=1.0, type=float, dest='lam', metavar='LAMBDA',
help='the rate at which reservations occur (1.0 if unspecified).')
parser.add_argument('-nu', default=1.0, type=float, dest='nu',
help='the rate at which cars are picked up (1.0 if unspecified).')
parser.add_argument('-mu', default=1.0, type=float, dest='mu',
help='the rate at which cars are dropped off (1.0 if unspecified).')
parser.add_argument('-p', '--prob-class-1', default=0.5, type=float, dest='probabilityOfClass1', metavar='P',
help='use single reservation with probability p (or 0.5 if unspecified).')
parser.add_argument('computation',
choices=['class1-reserv', 'class2-reserv', 'class1-driving', 'class2-driving', 'waiting'], metavar='COMPUTATION',
help='''generate plots of the specified computation.
This argument can take the following values.
`class1-reserv`: plot the average number of cars reserved by users of class 1 over time.
`class2-reserv`: plot the average number of cars reserved by users of class 2 over time.
`class1-driving`: plot the average number of driving users of class 1 over time.
`class2-driving`: plot the average number of driving users of class 2 over time.
`waiting` plot the average number of users (of class 1) waiting to park over time.''')
parser.add_argument('-v', '--verbose', action='store_true', help='print a summary of the simulation to standard output.')
parser.add_argument('-r', '--runs', type=int, default=10000, metavar='R', dest='numRuns',
help='run the simulation R times.')
parser.add_argument('-n', '--num-stations', type=int, default=100, metavar='N', dest='numStations',
help='set the number of stations to N (default 100).')
parser.add_argument('-c', '--cars-per-station', required=True, type=int, metavar='cars', dest='carsPerStation',
help='start with C cars at each station.')
parser.add_argument('-dt', '--time-step', type=float, default=0.01, dest='timeStep', metavar='dt',
help='set the time step to `dt` when averaging between runs (dexfault 0.01)')
parser.add_argument('-s', '--spaces-per-station', required=True, type=int, metavar='spaces', dest='parkingSpacesPerStation',
help='use S parking spaces per station in the simulation.')
parser.add_argument('-t', '--end-time', default=10.0, type=float,metavar='TIME', dest='endTime',
help='the time at which the simulation is to be stopped.')
parser.add_argument('--version', action='version', version='1.0')
if len(argv) <= 1:
parser.print_help()
return
opts = parser.parse_args(argv)
def runColK(k):
result = run_simulation(
opts.numStations,
opts.carsPerStation,
opts.parkingSpacesPerStation,
opts.probabilityOfClass1,
opts.lam,
opts.nu,
opts.mu,
opts.numRuns,
opts.endTime,
opts.timeStep,
k
)
t = 0.0
for i in range(len(result)):
result[i] = (t, result[i])
t += opts.timeStep
return result
if opts.computation == 'class1-reserv':
data = runColK(1)
plot(data, 'Time', 'Class 1 Reservations', 'Average number of class 1 reservations over time')
elif opts.computation == 'class2-reserv':
data = runColK(2)
plot(data, 'Time', 'Class 2 Reservations', 'Average number of class 2 reservations over time')
elif opts.computation == 'class1-driving':
data = runColK(3)
plot(data, 'Time', 'Class 1 Users Driving', 'Average number of class 1 users driving')
elif opts.computation == 'class2-driving':
data = runColK(4)
plot(data, 'Time', 'Class 2 Users Driving', 'Average number of class 2 users driving')
elif opts.computation == 'waiting':
data = runColK(5)
plot(data, 'Time', 'Users Waiting to Park', 'Average number of users waiting to park')
else:
sys.stderr.write(f'Error: invalid argument `{opts.computation}`. Rerun with --help for help.')
if __name__ == '__main__':
main(sys.argv[1:])