-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·109 lines (82 loc) · 3.79 KB
/
main.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
#!/bin/env python3
"""
FOMO simulator example
"""
from settings import *
import init_state
import init_state.json_source
import init_state.csv_source
import target_state
import policies
import policies.fosen_haldorsen
import policies.gleditsch_hagen
import demand
import sim
import output
from helpers import timeInMinutes
from output.plots import cityTrafficStats
START_TIME = timeInMinutes(hours=7)
DURATION = timeInMinutes(hours=24)
INSTANCE = 'TD_W34'
def main():
###############################################################################
# Get initial state
# the following is for creating a new initial state from trip data
# state = init_state.get_initial_state(source=init_state.json_source,
# name="Trondheim",
# urlHistorical="https://data.urbansharing.com/trondheimbysykkel.no/trips/v1/",
# urlGbfs="https://gbfs.urbansharing.com/trondheimbysykkel.no",
# week=34)
# the following is for reading a precalculated initial state from a json file
state = init_state.read_initial_state("instances/"+INSTANCE);
state.set_seed(1)
###############################################################################
# Set up vehicles
# Each vehicle has an associated policy
# policy = policies.RandomActionPolicy()
policy = policies.GreedyPolicy()
# policy = policies.fosen_haldorsen.FosenHaldorsenPolicy(state=state, greedy=True)
# policy = policies.fosen_haldorsen.FosenHaldorsenPolicy(state=state, greedy=False, scenarios=2, branching=7, time_horizon=25)
# policy = policies.gleditsch_hagen.GleditschHagenPolicy(variant='PatternBased')
state.set_vehicles([policy]) # this creates one vehicle for each policy in the list
###############################################################################
# Set up target state
# tstate = target_state.EvenlyDistributedTargetState()
# tstate = target_state.OutflowTargetState()
# tstate = target_state.EqualProbTargetState()
# tstate = target_state.USTargetState()
tstate = target_state.HalfCapacityTargetState()
###############################################################################
# Set up demand
dmand = demand.Demand()
###############################################################################
# Set up simulator
simulator = sim.Simulator(
initial_state = state,
target_state = tstate,
demand = dmand,
start_time = START_TIME,
duration = DURATION,
verbose = True,
)
simulator.run()
# Output to console
print(f"Simulation time = {DURATION} minutes")
print(f"Total requested trips = {simulator.metrics.get_aggregate_value('trips')}")
print(f"Starvations = {simulator.metrics.get_aggregate_value('starvation')}")
print(f"Congestions = {simulator.metrics.get_aggregate_value('congestion')}")
# Output to file
output.write_csv(simulator, "output.csv", hourly = False)
# Plot to screen
output.visualize([simulator.metrics], metric="trips")
output.visualize([simulator.metrics], metric="starvation")
output.visualize([simulator.metrics], metric="congestion")
output.visualize_heatmap([simulator], metric="trips")
# show travel times for a given bike
# bikes = simulator.state.get_all_bikes()
# bikes = sorted(bikes, key=lambda bike: bike.metrics.getLen("travel_time"), reverse=True)
# print(f"Bike {bikes[11].id}: {bikes[11].metrics.getSum('travel_time')} {bikes[11].metrics.getSum('travel_time_congested')}")
# output.visualize([bikes[11].metrics], metric="travel_time")
# output.visualize([bikes[11].metrics], metric="travel_time_congested")
if __name__ == "__main__":
main()