-
Notifications
You must be signed in to change notification settings - Fork 5
Prova #414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Prova #414
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| from datetime import datetime | ||
Check warningCode 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 warningCode scanning / Prospector (reported by Codacy) Unable to import 'tqdm' (import-error) Warning
Unable to import 'tqdm' (import-error)
Check warningCode 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 warningCode scanning / Prospector (reported by Codacy) Unable to import 'numpy' (import-error) Warning
Unable to import 'numpy' (import-error)
Check warningCode 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 warningCode 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 warningCode 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 warningCode 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 warningCode 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 warningCode 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 warningCode 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 warningCode 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 warningCode 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 warningCode 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() | ||
Check warning
Code scanning / Pylintpython3 (reported by Codacy)
Missing module docstring Warning