Skip to content
Open

Prova #414

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions examples/modena.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from datetime import datetime

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

Missing module docstring Warning

Missing module docstring

Check warning

Code scanning / Pylint (reported by Codacy)

Missing module docstring Warning

Missing module docstring
import logging

from dsf.cartography import get_cartography
from dsf.mobility import RoadNetwork, Dynamics

from tqdm import trange

Check warning

Code scanning / Prospector (reported by Codacy)

Unable to import 'tqdm' (import-error) Warning

Unable to import 'tqdm' (import-error)

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

third party import "from tqdm import trange" should be placed before "from dsf.cartography import get_cartography" Warning

third party import "from tqdm import trange" should be placed before "from dsf.cartography import get_cartography"
import numpy as np

Check warning

Code scanning / Prospector (reported by Codacy)

Unable to import 'numpy' (import-error) Warning

Unable to import 'numpy' (import-error)

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

third party import "import numpy as np" should be placed before "from dsf.cartography import get_cartography" Warning

third party import "import numpy as np" should be placed before "from dsf.cartography import get_cartography"

SEED = 42
np.random.seed(SEED)

logging.basicConfig(level=logging.INFO)

if __name__ == "__main__":
logging.info("Getting data from OpenStreetMap...")
# Get the cartography of Modena, Italy
G, df_edges, df_nodes = get_cartography("Parma, Emilia-Romagna, Italy")

Check warning

Code scanning / Pylint (reported by Codacy)

Constant name "df_nodes" doesn't conform to UPPER_CASE naming style Warning

Constant name "df_nodes" doesn't conform to UPPER_CASE naming style

Check warning

Code scanning / Pylint (reported by Codacy)

Constant name "df_edges" doesn't conform to UPPER_CASE naming style Warning

Constant name "df_edges" doesn't conform to UPPER_CASE naming style
# set nlanes to 1 if 0
df_edges["nlanes"] = df_edges["nlanes"].replace(0, 1).fillna(1).astype(int)

df_edges.to_csv("modena_edges.csv", sep=";", index=False)
df_nodes.to_csv("modena_nodes.csv", sep=";", index=False)

nodes = G.nodes(data=False)

Check warning

Code scanning / Pylint (reported by Codacy)

Constant name "nodes" doesn't conform to UPPER_CASE naming style Warning

Constant name "nodes" doesn't conform to UPPER_CASE naming style
# Extract 10% random node ids as origins and destinations for the traffic simulation
origin_ids = np.random.choice(

Check warning

Code scanning / Pylint (reported by Codacy)

Constant name "origin_ids" doesn't conform to UPPER_CASE naming style Warning

Constant name "origin_ids" doesn't conform to UPPER_CASE naming style
list(nodes), size=int(0.1 * len(nodes)), replace=False
)
destination_ids = np.random.choice(

Check warning

Code scanning / Pylint (reported by Codacy)

Constant name "destination_ids" doesn't conform to UPPER_CASE naming style Warning

Constant name "destination_ids" doesn't conform to UPPER_CASE naming style
list(nodes), size=int(0.1 * len(nodes)), replace=False
)

del df_edges, df_nodes, G

logging.info("Creating road network and dynamics model...")

# Create a road network from the cartography
road_network = RoadNetwork()

Check warning

Code scanning / Pylint (reported by Codacy)

Constant name "road_network" doesn't conform to UPPER_CASE naming style Warning

Constant name "road_network" doesn't conform to UPPER_CASE naming style
road_network.importEdges("modena_edges.csv", ";")
road_network.importNodeProperties("modena_nodes.csv", ";")
# Adjust network parameters
road_network.adjustNodeCapacities()
road_network.autoMapStreetLanes()
road_network.autoAssignRoadPriorities()
road_network.autoInitTrafficLights()

# Generaate a random vector of integer values for vehicle input
# We want values to have a 10s entry for a whole day
vehicle_input = np.random.randint(0, 10, size=8640)

Check warning

Code scanning / Pylint (reported by Codacy)

Constant name "vehicle_input" doesn't conform to UPPER_CASE naming style Warning

Constant name "vehicle_input" doesn't conform to UPPER_CASE naming style

# Create a dynamics model for the road network
dynamics = Dynamics(road_network, seed=SEED, alpha=0.8)

Check warning

Code scanning / Pylint (reported by Codacy)

Constant name "dynamics" doesn't conform to UPPER_CASE naming style Warning

Constant name "dynamics" doesn't conform to UPPER_CASE naming style
# Get epoch time of today at midnight
epoch_time = int(

Check warning

Code scanning / Pylint (reported by Codacy)

Constant name "epoch_time" doesn't conform to UPPER_CASE naming style Warning

Constant name "epoch_time" doesn't conform to UPPER_CASE naming style
datetime.combine(datetime.today(), datetime.min.time()).timestamp()
)

dynamics.setInitTime(epoch_time)
dynamics.connectDataBase("modena.db")
dynamics.saveData(300, True, True, True)

dynamics.setOriginNodes(origin_ids)
dynamics.setDestinationNodes(destination_ids)

# Simulate traffic for 24 hours with a time step of 10 seconds
for time_step in trange(8640):
# Update paths every 5 minutes (300 seconds)
if time_step % 300 == 0:
dynamics.updatePaths()
# Add agents every 10 seconds
if time_step % 10 == 0:
dynamics.addAgentsRandomly(vehicle_input[time_step])

dynamics.summary()
Loading