From a5e477bd84803afb0e67e60109e1dc3b17047135 Mon Sep 17 00:00:00 2001 From: janekdererste Date: Mon, 11 Sep 2023 11:50:48 +0200 Subject: [PATCH 01/20] Remove vehicle definitions from code as first step towards vehicle types --- assets/vehicles/vehicles_v2.xml | 88 ++++++++ src/simulation/controller.rs | 9 - src/simulation/io/attributes.rs | 15 ++ src/simulation/io/mod.rs | 3 +- src/simulation/io/network.rs | 15 +- src/simulation/io/vehicle_definitions.rs | 244 ----------------------- src/simulation/io/vehicles.rs | 69 +++++++ src/simulation/messaging/messages.proto | 11 +- src/simulation/messaging/messages.rs | 17 +- src/simulation/mod.rs | 1 + src/simulation/network/global_network.rs | 3 +- src/simulation/network/link.rs | 182 +++-------------- src/simulation/network/sim_network.rs | 61 +++--- src/simulation/simulation.rs | 58 ++---- src/simulation/vehicles/Garage.rs | 32 +++ src/simulation/vehicles/VehicleType.rs | 26 +++ src/simulation/vehicles/mod.rs | 2 + 17 files changed, 316 insertions(+), 520 deletions(-) create mode 100644 assets/vehicles/vehicles_v2.xml create mode 100644 src/simulation/io/attributes.rs delete mode 100644 src/simulation/io/vehicle_definitions.rs create mode 100644 src/simulation/io/vehicles.rs create mode 100644 src/simulation/vehicles/Garage.rs create mode 100644 src/simulation/vehicles/VehicleType.rs create mode 100644 src/simulation/vehicles/mod.rs diff --git a/assets/vehicles/vehicles_v2.xml b/assets/vehicles/vehicles_v2.xml new file mode 100644 index 00000000..0cc307fd --- /dev/null +++ b/assets/vehicles/vehicles_v2.xml @@ -0,0 +1,88 @@ + + + + + + + abc + 1.3 + 23.23 + parallel + 42.42 + + abc + + + + + + + + 0.23 + EURO-5 + < 1,4L + petrol + pass. car + + + + + 0.06 + + + + + + + + + + def + 2 + + + + + + + + + + + This is a small truck + + + + + + + + EURO-6 + diesel + + + + + 0.15 + 0.05 + + + + + + + + + + 1.234 + + + + + firstVehicle + + + + + diff --git a/src/simulation/controller.rs b/src/simulation/controller.rs index ee94c841..a7755a09 100644 --- a/src/simulation/controller.rs +++ b/src/simulation/controller.rs @@ -9,7 +9,6 @@ use tracing::info; use crate::simulation::config::Config; use crate::simulation::io::proto_events::ProtoEventsWriter; -use crate::simulation::io::vehicle_definitions::{IOVehicleDefinitions, VehicleDefinitions}; use crate::simulation::messaging::events::EventsPublisher; use crate::simulation::messaging::message_broker::MpiMessageBroker; use crate::simulation::network::sim_network::SimNetworkPartition; @@ -55,20 +54,12 @@ pub fn run(world: SystemCommunicator, config: Config) { //events.add_subscriber(travel_time_collector); //events.add_subscriber(Box::new(EventsLogger {})); - let mut vehicle_definitions: Option = None; - if let Some(vehicle_definitions_file_path) = &config.vehicle_definitions_file { - let io_vehicle_definitions = - IOVehicleDefinitions::from_file(vehicle_definitions_file_path.as_ref()); - vehicle_definitions = Some(VehicleDefinitions::from_io(io_vehicle_definitions)); - } - let mut simulation = Simulation::new( &config, network_partition, population, message_broker, events, - vehicle_definitions, ); let start = Instant::now(); diff --git a/src/simulation/io/attributes.rs b/src/simulation/io/attributes.rs new file mode 100644 index 00000000..2f96bdf9 --- /dev/null +++ b/src/simulation/io/attributes.rs @@ -0,0 +1,15 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] +pub struct Attr { + pub name: String, + pub class: String, + #[serde(rename = "$value")] + pub value: String, +} + +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] +pub struct Attrs { + #[serde(rename = "attribute", default)] + pub attributes: Vec, +} diff --git a/src/simulation/io/mod.rs b/src/simulation/io/mod.rs index c94e4b15..b335496f 100644 --- a/src/simulation/io/mod.rs +++ b/src/simulation/io/mod.rs @@ -1,9 +1,10 @@ +pub mod attributes; pub mod matsim_id; pub mod network; pub mod non_blocking_io; pub mod population; pub mod proto_events; -pub mod vehicle_definitions; +pub mod vehicles; mod worker; pub mod xml_events; pub mod xml_reader; diff --git a/src/simulation/io/network.rs b/src/simulation/io/network.rs index e3679302..907f6bce 100644 --- a/src/simulation/io/network.rs +++ b/src/simulation/io/network.rs @@ -9,23 +9,10 @@ use quick_xml::se::to_writer; use serde::{Deserialize, Serialize}; use tracing::info; +use crate::simulation::io::attributes::Attrs; use crate::simulation::io::matsim_id::MatsimId; use crate::simulation::io::xml_reader; -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -pub struct Attr { - pub name: String, - pub class: String, - #[serde(rename = "$value")] - pub value: String, -} - -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -pub struct Attrs { - #[serde(rename = "attribute", default)] - pub attributes: Vec, -} - #[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] pub struct IONode { pub id: String, diff --git a/src/simulation/io/vehicle_definitions.rs b/src/simulation/io/vehicle_definitions.rs deleted file mode 100644 index 69a990cd..00000000 --- a/src/simulation/io/vehicle_definitions.rs +++ /dev/null @@ -1,244 +0,0 @@ -use crate::simulation::io::xml_reader; -use log::debug; -use serde::{Deserialize, Serialize}; - -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename = "vehicleDefinitions")] -pub struct IOVehicleDefinitions { - #[serde(rename = "vehicleType", default)] - vehicle_types: Vec, -} - -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -pub struct IOVehicleType { - pub id: String, - #[serde(rename = "$value")] - pub attributes: Vec, -} - -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename_all = "camelCase")] -pub enum IOVehicleAttribute { - Length(IOLength), - Width(IOWidth), - MaximumVelocity(IOMaximumVelocity), - AccessTime(IOAccessTime), - EgressTime(IOEgressTime), - DoorOperation(IODoorOperation), - PassengerCarEquivalents(IOPassengerCarEquivalents), - NetworkMode(IONetworkMode), - FlowEfficiencyFactor(IOFlowEfficiencyFactor), -} - -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename_all = "camelCase")] -pub struct IOLength { - meter: f32, -} - -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename_all = "camelCase")] -pub struct IOWidth { - meter: f32, -} - -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename_all = "camelCase")] -pub struct IOMaximumVelocity { - meter_per_second: f32, -} - -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename_all = "camelCase")] -pub struct IOAccessTime { - seconds_per_person: f32, -} - -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename_all = "camelCase")] -pub struct IOEgressTime { - seconds_per_person: f32, -} - -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename_all = "camelCase")] -pub struct IODoorOperation { - mode: String, -} - -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename_all = "camelCase")] -pub struct IOPassengerCarEquivalents { - pce: f32, -} - -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename_all = "camelCase")] -pub struct IONetworkMode { - network_mode: String, -} - -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename_all = "camelCase")] -pub struct IOFlowEfficiencyFactor { - network_mode: f32, -} - -#[derive(Debug, PartialEq, Clone)] -pub struct VehicleType { - pub id: String, - pub maximum_velocity: Option, - pub network_mode: String, -} - -#[derive(Debug, PartialEq, Clone)] -pub struct VehicleDefinitions { - pub vehicle_types: Vec, -} - -impl IOVehicleDefinitions { - pub fn from_file(file_path: &str) -> IOVehicleDefinitions { - xml_reader::read::(file_path) - } -} - -impl VehicleDefinitions { - pub fn new() -> VehicleDefinitions { - VehicleDefinitions { - vehicle_types: vec![], - } - } - - pub fn add_vehicle_type( - mut self, - id: String, - maximum_velocity: Option, - network_mode: String, - ) -> VehicleDefinitions { - self.vehicle_types.push(VehicleType { - id, - maximum_velocity, - network_mode, - }); - self - } - - pub fn from_io(io: IOVehicleDefinitions) -> VehicleDefinitions { - let vehicle_types = io - .vehicle_types - .into_iter() - .map(|t| VehicleDefinitions::convert_io_vehicle_type(t)) - .collect(); - VehicleDefinitions { vehicle_types } - } - - fn convert_io_vehicle_type(io: IOVehicleType) -> VehicleType { - VehicleType { - id: io.id.clone(), - maximum_velocity: Self::extract_maximum_velocity(&io), - network_mode: Self::extract_network_mode(&io).unwrap_or_else(|| { - debug!("There was no specific network mode for vehicle type {}. Using id as network mode.", io.id); - io.id - }), - } - } - - fn extract_maximum_velocity(io: &IOVehicleType) -> Option { - io.attributes - .iter() - .filter_map(|a| match a { - IOVehicleAttribute::MaximumVelocity(v) => Some(v.meter_per_second), - _ => None, - }) - .collect::>() - .get(0) - .cloned() - } - - fn extract_network_mode(io: &IOVehicleType) -> Option { - io.attributes - .iter() - .filter_map(|a| match a { - IOVehicleAttribute::NetworkMode(m) => Some(m.network_mode.clone()), - _ => None, - }) - .collect::>() - .get(0) - .cloned() - } - - pub fn get_max_speed_for_mode(&self, mode: &str) -> Option { - let mode_vehicle_type = self - .vehicle_types - .iter() - .filter(|&v| v.network_mode.eq(mode)) - .collect::>(); - - if mode_vehicle_type.len() == 0 { - panic!("There is no vehicle type definition for mode {:?} ", mode) - } else if mode_vehicle_type.len() > 1 { - panic!( - "There are multiple vehicle type definitions for mode {:?} ", - mode - ) - } - - mode_vehicle_type.get(0).unwrap().maximum_velocity - } -} - -#[cfg(test)] -mod test { - use crate::simulation::io::vehicle_definitions::{ - IOVehicleDefinitions, VehicleDefinitions, VehicleType, - }; - use quick_xml::de::from_str; - - #[test] - fn test() { - let io_definitions = from_str::( - r#" - - - - - - - - - - - - - - - - - - - - - - "# - ).unwrap(); - - let vehicle_definitions = VehicleDefinitions::from_io(io_definitions); - assert_eq!( - vehicle_definitions, - VehicleDefinitions { - vehicle_types: vec![ - VehicleType { - id: "car".to_string(), - maximum_velocity: Some(16.67), - network_mode: "car".to_string() - }, - VehicleType { - id: "bicycle".to_string(), - maximum_velocity: Some(4.17), - network_mode: "bicycle".to_string() - } - ], - } - ); - } -} diff --git a/src/simulation/io/vehicles.rs b/src/simulation/io/vehicles.rs new file mode 100644 index 00000000..1d091bda --- /dev/null +++ b/src/simulation/io/vehicles.rs @@ -0,0 +1,69 @@ +use serde::{Deserialize, Serialize}; + +use crate::simulation::io::attributes::Attrs; +use crate::simulation::io::xml_reader; + +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] +#[serde(rename = "vehicleDefinitions")] +pub struct IOVehicleDefinitions { + #[serde(rename = "vehicleType")] + veh_types: Vec, +} + +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] +#[serde(rename = "vehicleDefinitions")] +pub struct IOVehicles { + #[serde(rename = "vehicleType", default)] + vehicle_types: Vec, +} + +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] +pub struct IOVehicleType { + pub id: String, + pub descr: String, + pub capacity: IOCapacity, + pub length: f32, + pub width: f32, + pub maximum_velocity: f32, + pub engine_information: IOEngineInformation, + pub cost_information: IOCostInformation, + pub passenger_car_equivalents: f32, + pub network_mode: String, + pub flow_efficiency_factor: f32, + pub attributes: Option, +} + +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] +#[serde(rename = "capacity")] +pub struct IOCapacity { + // leave emtpy for now +} + +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] +#[serde(rename = "capacity")] +pub struct IOEngineInformation { + // leave empty for now. +} + +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] +#[serde(rename = "capacity")] +pub struct IOCostInformation { + // leave empty for now. +} + +impl IOVehicleDefinitions { + pub fn from_file(file: &str) -> Self { + xml_reader::read(file) + } +} + +#[cfg(test)] +mod test { + use crate::simulation::io::vehicles::IOVehicleDefinitions; + + #[test] + fn test_from_file() { + let io_vehicles = IOVehicleDefinitions::from_file("./assets/vehicles/vehicles_v2.xml"); + println!("{io_vehicles:#?}") + } +} diff --git a/src/simulation/messaging/messages.proto b/src/simulation/messaging/messages.proto index 6369397f..61eef18e 100644 --- a/src/simulation/messaging/messages.proto +++ b/src/simulation/messaging/messages.proto @@ -24,17 +24,12 @@ message VehicleMessage { message Vehicle { uint64 id = 1; uint32 curr_route_elem = 2; - VehicleType type = 3; + uint64 type = 3; Agent agent = 4; - string mode = 5; + uint64 mode = 5; // this must be extended once more people use one vehicle } -enum VehicleType { - TELEPORTED = 0; - NETWORK = 1; -} - message Agent { uint64 id = 1; uint32 curr_plan_elem = 2; @@ -57,7 +52,7 @@ message Activity { } message Leg { - string mode = 1; + uint64 mode = 1; optional uint32 dep_time = 2; optional uint32 trav_time = 3; oneof route { diff --git a/src/simulation/messaging/messages.rs b/src/simulation/messaging/messages.rs index 609564f7..1d1d8bfc 100644 --- a/src/simulation/messaging/messages.rs +++ b/src/simulation/messaging/messages.rs @@ -11,7 +11,7 @@ use crate::simulation::io::population::{ use crate::simulation::messaging::messages::proto::leg::Route; use crate::simulation::messaging::messages::proto::{ Activity, Agent, ExperimentalMessage, GenericRoute, Leg, NetworkRoute, Plan, - TravelTimesMessage, Vehicle, VehicleMessage, VehicleType, + TravelTimesMessage, Vehicle, VehicleMessage, }; use crate::simulation::network::global_network::Network; use crate::simulation::population::population::Population; @@ -111,13 +111,14 @@ impl Ord for VehicleMessage { } impl Vehicle { - pub fn new(id: u64, veh_type: VehicleType, mode: String, agent: Agent) -> Vehicle { + // todo, fix type and mode + pub fn new(id: u64, veh_type: u64, mode: u64, agent: Agent) -> Vehicle { Vehicle { id, agent: Some(agent), curr_route_elem: 0, - r#type: veh_type as i32, - mode, + r#type: 0, + mode: 0, } } @@ -153,10 +154,6 @@ impl Vehicle { Route::NetworkRoute(route) => self.curr_route_elem + 1 >= route.route.len() as u32, } } - - pub fn mode(&self) -> &str { - self.mode.as_str() - } } impl EndTime for Vehicle { @@ -486,7 +483,7 @@ impl Leg { let route = Route::from_io(&io_leg.route, net, pop); Self { route: Some(route), - mode: io_leg.mode.clone(), + mode: 0, //todo fix mode trav_time: parse_time_opt(&io_leg.trav_time), dep_time: parse_time_opt(&io_leg.dep_time), } @@ -495,7 +492,7 @@ impl Leg { pub fn new(route: Route, mode: &str, trav_time: Option, dep_time: Option) -> Self { Self { route: Some(route), - mode: String::from(mode), + mode: 0, //todo fix mode trav_time, dep_time, } diff --git a/src/simulation/mod.rs b/src/simulation/mod.rs index c07885e1..e002aef1 100644 --- a/src/simulation/mod.rs +++ b/src/simulation/mod.rs @@ -9,3 +9,4 @@ pub mod performance_profiling; pub mod population; mod simulation; pub mod time_queue; +mod vehicles; diff --git a/src/simulation/network/global_network.rs b/src/simulation/network/global_network.rs index cb94e919..afc5223b 100644 --- a/src/simulation/network/global_network.rs +++ b/src/simulation/network/global_network.rs @@ -1,8 +1,9 @@ use std::{collections::HashSet, path::Path}; +use crate::simulation::io::attributes::{Attr, Attrs}; use crate::simulation::{ id::{Id, IdStore}, - io::network::{Attr, Attrs, IOLink, IONetwork, IONode}, + io::network::{IOLink, IONetwork, IONode}, }; use super::metis_partitioning; diff --git a/src/simulation/network/link.rs b/src/simulation/network/link.rs index 01f9d863..1bec72ce 100644 --- a/src/simulation/network/link.rs +++ b/src/simulation/network/link.rs @@ -1,11 +1,8 @@ use std::collections::VecDeque; use std::fmt::Debug; -use tracing::warn; - use crate::simulation::id::{Id, IdImpl}; use crate::simulation::io::network::IOLink; -use crate::simulation::io::vehicle_definitions::VehicleDefinitions; use crate::simulation::messaging::messages::proto::Vehicle; use crate::simulation::network::flow_cap::Flowcap; use crate::simulation::network::global_network::Node; @@ -163,13 +160,8 @@ impl LocalLink { } } - pub fn push_vehicle( - &mut self, - vehicle: Vehicle, - now: u32, - vehicle_definitions: Option<&VehicleDefinitions>, - ) { - let speed = self.get_speed_for_vehicle(&vehicle, vehicle_definitions); + pub fn push_vehicle(&mut self, vehicle: Vehicle, now: u32) { + let speed = self.get_speed_for_vehicle(&vehicle); let duration = (self.length / speed) as u32; let earliest_exit_time = now + duration; self.q.push_back(VehicleQEntry { @@ -196,11 +188,9 @@ impl LocalLink { popped_veh } - fn get_speed_for_vehicle( - &self, - vehicle: &Vehicle, - vehicle_definitions: Option<&VehicleDefinitions>, - ) -> f32 { + fn get_speed_for_vehicle(&self, vehicle: &Vehicle) -> f32 { + panic!("not implemented"); + /* if vehicle_definitions.is_none() { return self.freespeed; } @@ -208,17 +198,19 @@ impl LocalLink { let vehicle_max_speed = vehicle_definitions .as_ref() .unwrap() - .get_max_speed_for_mode(vehicle.mode()); + .get_max_speed_for_mode(0); //todo fix mode if vehicle_max_speed.is_none() { warn!( "There is no max speed given for vehicle type {:?}. Using freespeed of links.", - vehicle.mode() + 0 // todo fix mode ); return self.freespeed; } self.freespeed.min(vehicle_max_speed.unwrap()) + + */ } } @@ -268,10 +260,9 @@ impl SplitInLink { #[cfg(test)] mod tests { use crate::simulation::id::IdImpl; - use crate::simulation::io::vehicle_definitions::VehicleDefinitions; use crate::simulation::messaging::messages::proto::leg::Route; use crate::simulation::messaging::messages::proto::{Activity, NetworkRoute}; - use crate::simulation::messaging::messages::proto::{Agent, Leg, Plan, Vehicle, VehicleType}; + use crate::simulation::messaging::messages::proto::{Agent, Leg, Plan, Vehicle}; use crate::simulation::network::link::LocalLink; #[test] @@ -288,9 +279,9 @@ mod tests { IdImpl::new_internal(0), ); let agent = create_agent(1, vec![]); - let vehicle = Vehicle::new(veh_id, VehicleType::Network, String::from("car"), agent); + let vehicle = Vehicle::new(veh_id, 0, 0, agent); - link.push_vehicle(vehicle, 0, None); + link.push_vehicle(vehicle, 0); // this should put the vehicle into the queue and update the exit time correctly let pushed_vehicle = link.q.front().unwrap(); @@ -314,12 +305,12 @@ mod tests { ); let agent1 = create_agent(1, vec![]); - let vehicle1 = Vehicle::new(id1, VehicleType::Network, String::from("car"), agent1); + let vehicle1 = Vehicle::new(id1, 0, 0, agent1); let agent2 = create_agent(1, vec![]); - let vehicle2 = Vehicle::new(id2, VehicleType::Network, String::from("car"), agent2); + let vehicle2 = Vehicle::new(id2, 0, 0, agent2); - link.push_vehicle(vehicle1, 0, None); - link.push_vehicle(vehicle2, 0, None); + link.push_vehicle(vehicle1, 0); + link.push_vehicle(vehicle2, 0); // make sure that vehicles are added ad the end of the queue assert_eq!(2, link.q.len()); @@ -350,8 +341,8 @@ mod tests { while n < 10 { let agent = create_agent(n as u64, vec![]); - let vehicle = Vehicle::new(n as u64, VehicleType::Network, String::from("car"), agent); - link.push_vehicle(vehicle, n, None); + let vehicle = Vehicle::new(n as u64, 0, 0, agent); + link.push_vehicle(vehicle, n); n += 1; } @@ -381,8 +372,8 @@ mod tests { while n < 10 { let agent = create_agent(n as u64, vec![]); - let vehicle = Vehicle::new(n as u64, VehicleType::Network, String::from("car"), agent); - link.push_vehicle(vehicle, n, None); + let vehicle = Vehicle::new(n as u64, 0, 0, agent); + link.push_vehicle(vehicle, n); n += 1; } @@ -409,11 +400,11 @@ mod tests { ); let agent1 = create_agent(1, vec![]); - let vehicle1 = Vehicle::new(1, VehicleType::Network, String::from("car"), agent1); + let vehicle1 = Vehicle::new(1, 0, 0, agent1); let agent2 = create_agent(2, vec![]); - let vehicle2 = Vehicle::new(2, VehicleType::Network, String::from("car"), agent2); - link.push_vehicle(vehicle1, 0, None); - link.push_vehicle(vehicle2, 0, None); + let vehicle2 = Vehicle::new(2, 0, 0, agent2); + link.push_vehicle(vehicle1, 0); + link.push_vehicle(vehicle2, 0); let popped = link.pop_front(10); assert_eq!(1, popped.len()); @@ -427,131 +418,6 @@ mod tests { assert_eq!(1, popped_3.len()); } - #[test] - fn local_link_with_vehicle_definitions() { - let veh_id_car = 42; - let veh_id_buggy = 43; - let veh_id_bike = 44; - let mut link = LocalLink::new( - IdImpl::new_internal(1), - 1., - 10., - 100., - vec![], - 1., - IdImpl::new_internal(0), - IdImpl::new_internal(0), - ); - - let vehicle_definitions = create_three_vehicle_definitions(); - - let agent1 = create_agent(1, vec![]); - let car = Vehicle::new( - veh_id_car, - VehicleType::Network, - String::from("car"), - agent1, - ); - let agent2 = create_agent(2, vec![]); - let buggy = Vehicle::new( - veh_id_buggy, - VehicleType::Network, - String::from("buggy"), - agent2, - ); - let agent3 = create_agent(3, vec![]); - let bike = Vehicle::new( - veh_id_bike, - VehicleType::Network, - String::from("bike"), - agent3, - ); - - link.push_vehicle(car, 0, Some(&vehicle_definitions)); - link.push_vehicle(buggy, 0, Some(&vehicle_definitions)); - link.push_vehicle(bike, 0, Some(&vehicle_definitions)); - - // this should put the vehicle into the queue and update the exit time correctly - let pushed_vehicle_car = link.q.get(0).unwrap(); - assert_eq!(veh_id_car, pushed_vehicle_car.vehicle.id); - assert_eq!(10, pushed_vehicle_car.earliest_exit_time); - - let pushed_vehicle_buggy = link.q.get(1).unwrap(); - assert_eq!(veh_id_buggy, pushed_vehicle_buggy.vehicle.id); - assert_eq!(10, pushed_vehicle_buggy.earliest_exit_time); - - let pushed_vehicle_bike = link.q.get(2).unwrap(); - assert_eq!(veh_id_bike, pushed_vehicle_bike.vehicle.id); - assert_eq!(20, pushed_vehicle_bike.earliest_exit_time); - } - - #[test] - fn local_link_pop_with_vehicle_definitions() { - let veh_id_car = 42; - let veh_id_buggy = 43; - let veh_id_bike = 44; - let mut link = LocalLink::new( - IdImpl::new_internal(1), - 3600., - 10., - 100., - vec![], - 1., - IdImpl::new_internal(0), - IdImpl::new_internal(0), - ); - - let vehicle_definitions = create_three_vehicle_definitions(); - - let agent1 = create_agent(1, vec![]); - let car = Vehicle::new( - veh_id_car, - VehicleType::Network, - String::from("car"), - agent1, - ); - let agent2 = create_agent(1, vec![]); - let buggy = Vehicle::new( - veh_id_buggy, - VehicleType::Network, - String::from("buggy"), - agent2, - ); - let agent3 = create_agent(1, vec![]); - let bike = Vehicle::new( - veh_id_bike, - VehicleType::Network, - String::from("bike"), - agent3, - ); - - link.push_vehicle(bike, 0, Some(&vehicle_definitions)); - link.push_vehicle(buggy, 0, Some(&vehicle_definitions)); - link.push_vehicle(car, 0, Some(&vehicle_definitions)); - - let popped = link.pop_front(10); - assert_eq!(0, popped.len()); - - let popped_2 = link.pop_front(20); - assert_eq!(1, popped_2.len()); - assert!(popped_2.first().unwrap().mode.eq("bike")); - - let popped_3 = link.pop_front(21); - assert_eq!(1, popped_3.len()); - assert!(popped_3.first().unwrap().mode.eq("buggy")); - - let popped_4 = link.pop_front(22); - assert_eq!(1, popped_4.len()); - assert!(popped_4.first().unwrap().mode.eq("car")); - } - - fn create_three_vehicle_definitions() -> VehicleDefinitions { - VehicleDefinitions::new() - .add_vehicle_type("car".to_string(), Some(20.), String::from("car")) - .add_vehicle_type("buggy".to_string(), Some(10.), String::from("buggy")) - .add_vehicle_type("bike".to_string(), Some(5.), String::from("bike")) - } - fn create_agent(id: u64, route: Vec) -> Agent { let route = Route::NetworkRoute(NetworkRoute::new(id, route)); let leg = Leg::new(route, "car", None, None); diff --git a/src/simulation/network/sim_network.rs b/src/simulation/network/sim_network.rs index b236954e..d19c8403 100644 --- a/src/simulation/network/sim_network.rs +++ b/src/simulation/network/sim_network.rs @@ -2,7 +2,6 @@ use std::collections::{HashMap, HashSet}; use crate::simulation::{ id::Id, - io::vehicle_definitions::VehicleDefinitions, messaging::{ events::{proto::Event, EventsPublisher}, messages::proto::Vehicle, @@ -100,12 +99,7 @@ impl<'n> SimNetworkPartition<'n> { distinct_partitions } - pub fn move_nodes( - &mut self, - events: &mut EventsPublisher, - veh_def: Option<&VehicleDefinitions>, - now: u32, - ) -> Vec { + pub fn move_nodes(&mut self, events: &mut EventsPublisher, now: u32) -> Vec { let mut exited_vehicles = Vec::new(); for node_id in &self.nodes { @@ -115,7 +109,6 @@ impl<'n> SimNetworkPartition<'n> { &mut self.links, &mut exited_vehicles, events, - veh_def, now, ); } @@ -129,7 +122,6 @@ impl<'n> SimNetworkPartition<'n> { links: &mut HashMap, SimLink>, exited_vehicles: &mut Vec, events: &mut EventsPublisher, - veh_def: Option<&VehicleDefinitions>, now: u32, ) { let node = global_network.get_node(node_id); @@ -144,7 +136,7 @@ impl<'n> SimNetworkPartition<'n> { vehicle.advance_route_index(); exited_vehicles.push(ExitReason::FinishRoute(vehicle)); } else if let Some(exit_reason) = - Self::move_vehicle(vehicle, veh_def, global_network, links, events, now) + Self::move_vehicle(vehicle, global_network, links, events, now) { exited_vehicles.push(exit_reason); } @@ -154,7 +146,6 @@ impl<'n> SimNetworkPartition<'n> { fn move_vehicle( mut vehicle: Vehicle, - veh_def: Option<&VehicleDefinitions>, global_network: &Network, links: &mut HashMap, SimLink>, events: &mut EventsPublisher, @@ -174,7 +165,7 @@ impl<'n> SimNetworkPartition<'n> { now, &Event::new_link_enter(l.id.internal as u64, vehicle.id), ); - l.push_vehicle(vehicle, now, veh_def); + l.push_vehicle(vehicle, now); None } SimLink::Out(_) => Some(ExitReason::ReachedBoundary(vehicle)), @@ -190,9 +181,7 @@ mod tests { use crate::simulation::{ messaging::{ events::EventsPublisher, - messages::proto::{ - leg::Route, Activity, Agent, Leg, NetworkRoute, Plan, Vehicle, VehicleType, - }, + messages::proto::{leg::Route, Activity, Agent, Leg, NetworkRoute, Plan, Vehicle}, }, network::{ global_network::{Link, Network, Node}, @@ -243,13 +232,13 @@ mod tests { let mut sim_network = create_single_node_sim_network(&mut network); let mut publisher = EventsPublisher::new(); let agent = create_agent(1, vec![0]); - let vehicle = Vehicle::new(1, VehicleType::Network, String::from("car"), agent); + let vehicle = Vehicle::new(1, 0, 0, agent); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { - link1.push_vehicle(vehicle, 1, None); + link1.push_vehicle(vehicle, 1); } - let exits = sim_network.move_nodes(&mut publisher, None, 11); + let exits = sim_network.move_nodes(&mut publisher, 11); assert_eq!(1, exits.len()); assert!(matches!(exits.get(0).unwrap(), ExitReason::FinishRoute(_))); @@ -261,13 +250,13 @@ mod tests { let mut sim_network = create_single_node_sim_network(&mut network); let mut publisher = EventsPublisher::new(); let agent = create_agent(1, vec![0, 1]); - let vehicle = Vehicle::new(1, VehicleType::Network, String::from("car"), agent); + let vehicle = Vehicle::new(1, 0, 0, agent); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { - link1.push_vehicle(vehicle, 1, None); + link1.push_vehicle(vehicle, 1); } - let exits = sim_network.move_nodes(&mut publisher, None, 11); + let exits = sim_network.move_nodes(&mut publisher, 11); assert_eq!(0, exits.len()); let out_id = sim_network.global_network.link_ids.get(1); @@ -284,13 +273,13 @@ mod tests { let sim_network = sim_nets.get_mut(0).unwrap(); let mut publisher = EventsPublisher::new(); let agent = create_agent(1, vec![0, 1]); - let vehicle = Vehicle::new(1, VehicleType::Network, String::from("car"), agent); + let vehicle = Vehicle::new(1, 0, 0, agent); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { - link1.push_vehicle(vehicle, 1, None); + link1.push_vehicle(vehicle, 1); } - let exits = sim_network.move_nodes(&mut publisher, None, 11); + let exits = sim_network.move_nodes(&mut publisher, 11); assert_eq!(1, exits.len()); let exit = exits.first().unwrap(); @@ -303,15 +292,15 @@ mod tests { let mut sim_network = create_single_node_sim_network(&mut network); let mut publisher = EventsPublisher::new(); let agent1 = create_agent(1, vec![0]); - let vehicle1 = Vehicle::new(1, VehicleType::Network, String::from("car"), agent1); + let vehicle1 = Vehicle::new(1, 0, 0, agent1); let agent2 = create_agent(2, vec![0]); - let vehicle2 = Vehicle::new(2, VehicleType::Network, String::from("car"), agent2); + let vehicle2 = Vehicle::new(2, 0, 0, agent2); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { - link1.push_vehicle(vehicle1, 1, None); - link1.push_vehicle(vehicle2, 1, None); + link1.push_vehicle(vehicle1, 1); + link1.push_vehicle(vehicle2, 1); } - let exited_vehicles = sim_network.move_nodes(&mut publisher, None, 11); + let exited_vehicles = sim_network.move_nodes(&mut publisher, 11); assert_eq!(1, exited_vehicles.len()); let entry = exited_vehicles.get(0).unwrap(); @@ -320,7 +309,7 @@ mod tests { assert_eq!(1, vehicle.id); } - let exited_vehicles = sim_network.move_nodes(&mut publisher, None, 21); + let exited_vehicles = sim_network.move_nodes(&mut publisher, 21); assert_eq!(1, exited_vehicles.len()); let entry = exited_vehicles.get(0).unwrap(); @@ -336,18 +325,18 @@ mod tests { let mut sim_network = create_single_node_sim_network(&mut network); let mut publisher = EventsPublisher::new(); let agent1 = create_agent(1, vec![0, 1]); - let vehicle1 = Vehicle::new(1, VehicleType::Network, String::from("car"), agent1); + let vehicle1 = Vehicle::new(1, 0, 0, agent1); let agent2 = create_agent(2, vec![0, 1]); - let vehicle2 = Vehicle::new(2, VehicleType::Network, String::from("car"), agent2); + let vehicle2 = Vehicle::new(2, 0, 0, agent2); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { - link1.push_vehicle(vehicle1, 1, None); - link1.push_vehicle(vehicle2, 1, None); + link1.push_vehicle(vehicle1, 1); + link1.push_vehicle(vehicle2, 1); } - let exited_vehicles = sim_network.move_nodes(&mut publisher, None, 11); + let exited_vehicles = sim_network.move_nodes(&mut publisher, 11); assert_eq!(0, exited_vehicles.len()); - let exited_vehicles = sim_network.move_nodes(&mut publisher, None, 12); + let exited_vehicles = sim_network.move_nodes(&mut publisher, 12); assert_eq!(0, exited_vehicles.len()); let out_link = sim_network diff --git a/src/simulation/simulation.rs b/src/simulation/simulation.rs index 6b1e0ffc..179bd3ff 100644 --- a/src/simulation/simulation.rs +++ b/src/simulation/simulation.rs @@ -1,12 +1,11 @@ use tracing::info; use crate::simulation::config::Config; -use crate::simulation::io::vehicle_definitions::VehicleDefinitions; use crate::simulation::messaging::events::proto::Event; use crate::simulation::messaging::events::EventsPublisher; use crate::simulation::messaging::message_broker::{MessageBroker, MpiMessageBroker}; use crate::simulation::messaging::messages::proto::leg::Route; -use crate::simulation::messaging::messages::proto::{Agent, GenericRoute, Vehicle, VehicleType}; +use crate::simulation::messaging::messages::proto::{Agent, GenericRoute, Vehicle}; use crate::simulation::network::link::SimLink; use crate::simulation::network::sim_network::{ExitReason, SimNetworkPartition}; use crate::simulation::time_queue::TimeQueue; @@ -17,7 +16,6 @@ pub struct Simulation<'sim> { network: SimNetworkPartition<'sim>, message_broker: MpiMessageBroker, events: EventsPublisher, - vehicle_definitions: Option, } impl<'sim> Simulation<'sim> { @@ -27,7 +25,6 @@ impl<'sim> Simulation<'sim> { population: crate::simulation::population::population::Population, message_broker: MpiMessageBroker, events: EventsPublisher, - vehicle_definitions: Option, ) -> Self { let mut activity_q = TimeQueue::new(); for agent in population.agents.into_values() { @@ -40,7 +37,6 @@ impl<'sim> Simulation<'sim> { activity_q, message_broker, events, - vehicle_definitions, } } @@ -96,24 +92,14 @@ impl<'sim> Simulation<'sim> { Route::GenericRoute(route) => { self.events.publish_event( now, - &Event::new_departure(agent_id, route.start_link, leg.mode.clone()), + &Event::new_departure(agent_id, route.start_link, String::from("")), ); if Simulation::is_local_route(route, &self.message_broker) { - let veh = Vehicle::new( - agent.id, - VehicleType::Teleported, - leg.mode.clone(), - agent, - ); + let veh = Vehicle::new(agent.id, 1, leg.mode.clone(), agent); self.teleportation_q.add(veh, now); } else { - let veh = Vehicle::new( - agent.id, - VehicleType::Teleported, - leg.mode.clone(), - agent, - ); + let veh = Vehicle::new(agent.id, 1, leg.mode.clone(), agent); self.message_broker.add_veh(veh, now); } } @@ -121,7 +107,7 @@ impl<'sim> Simulation<'sim> { let link_id = route.route.first().unwrap(); self.events.publish_event( now, - &Event::new_departure(agent_id, *link_id, leg.mode.clone()), + &Event::new_departure(agent_id, *link_id, String::from("")), ); self.events.publish_event( @@ -129,12 +115,7 @@ impl<'sim> Simulation<'sim> { &Event::new_person_enters_veh(agent_id, route.vehicle_id), ); - let veh = Vehicle::new( - route.vehicle_id, - VehicleType::Network, - leg.mode.clone(), - agent, - ); + let veh = Vehicle::new(route.vehicle_id, 0, leg.mode.clone(), agent); self.veh_onto_network(veh, true, now); } } @@ -158,12 +139,10 @@ impl<'sim> Simulation<'sim> { ); } match link { - SimLink::Local(link) => { - link.push_vehicle(vehicle, now, self.vehicle_definitions.as_ref()) - } + SimLink::Local(link) => link.push_vehicle(vehicle, now), SimLink::In(in_link) => { let local_link = in_link.local_link_mut(); - local_link.push_vehicle(vehicle, now, self.vehicle_definitions.as_ref()) + local_link.push_vehicle(vehicle, now) } SimLink::Out(_) => { panic!("Vehicles should not start on out links...") @@ -180,7 +159,7 @@ impl<'sim> Simulation<'sim> { if let Route::GenericRoute(route) = &leg.route.as_ref().unwrap() { self.events.publish_event( now, - &Event::new_travelled(agent.id, route.distance, leg.mode.clone()), + &Event::new_travelled(agent.id, route.distance, String::from("")), ); } agent.advance_plan(); @@ -189,16 +168,14 @@ impl<'sim> Simulation<'sim> { } fn move_nodes(&mut self, now: u32) { - let exited_vehicles = - self.network - .move_nodes(&mut self.events, self.vehicle_definitions.as_ref(), now); + let exited_vehicles = self.network.move_nodes(&mut self.events, now); for exit_reason in exited_vehicles { match exit_reason { ExitReason::FinishRoute(vehicle) => { let veh_id = vehicle.id; let mut agent = vehicle.agent.unwrap(); - let leg_mode = agent.curr_leg().mode.clone(); + let leg_mode = 0; // todo fix mode self.events .publish_event(now, &Event::new_person_leaves_veh(agent.id, veh_id)); @@ -206,8 +183,10 @@ impl<'sim> Simulation<'sim> { agent.advance_plan(); let act = agent.curr_act(); - self.events - .publish_event(now, &Event::new_arrival(agent.id, act.link_id, leg_mode)); + self.events.publish_event( + now, + &Event::new_arrival(agent.id, act.link_id, String::from("some mode")), + ); //todo fix mode self.events.publish_event( now, @@ -231,13 +210,14 @@ impl<'sim> Simulation<'sim> { .collect::>(); for vehicle in vehicles { - match vehicle.r#type() { - VehicleType::Teleported => { + match vehicle.r#type { + 1 => { self.teleportation_q.add(vehicle, now); } - VehicleType::Network => { + 0 => { self.veh_onto_network(vehicle, false, now); } + _ => {} } } } diff --git a/src/simulation/vehicles/Garage.rs b/src/simulation/vehicles/Garage.rs new file mode 100644 index 00000000..cb29c75c --- /dev/null +++ b/src/simulation/vehicles/Garage.rs @@ -0,0 +1,32 @@ +use std::collections::HashMap; + +use crate::simulation::id::{Id, IdStore}; +use crate::simulation::messaging::messages::proto::Vehicle; +use crate::simulation::vehicles::VehicleType::VehicleType; + +#[derive(Debug)] +pub struct Garage<'g> { + vehicles: HashMap, Vehicle>, + vehicle_ids: IdStore<'g, Vehicle>, + vehicle_types: Vec, + vehicle_type_ids: IdStore<'g, VehicleType>, + modes: IdStore<'g, String>, +} + +impl<'g> Default for Garage<'g> { + fn default() -> Self { + Garage::new() + } +} + +impl<'g> Garage<'g> { + pub fn new() -> Self { + Garage { + vehicles: Default::default(), + vehicle_ids: Default::default(), + vehicle_types: vec![], + vehicle_type_ids: IdStore::new(), + modes: Default::default(), + } + } +} diff --git a/src/simulation/vehicles/VehicleType.rs b/src/simulation/vehicles/VehicleType.rs new file mode 100644 index 00000000..95f87f19 --- /dev/null +++ b/src/simulation/vehicles/VehicleType.rs @@ -0,0 +1,26 @@ +use crate::simulation::id::Id; + +#[derive(Debug)] +pub struct VehicleType { + id: Id, + length: f32, + width: f32, + max_v: f32, + pce: f32, + fef: f32, + net_mode: Id, +} + +impl VehicleType { + pub fn new(id: Id, net_mode: Id) -> Self { + VehicleType { + id, + length: 0.0, + width: 0.0, + max_v: 0.0, + pce: 0.0, + fef: 0.0, + net_mode, + } + } +} diff --git a/src/simulation/vehicles/mod.rs b/src/simulation/vehicles/mod.rs new file mode 100644 index 00000000..b7ec6cc8 --- /dev/null +++ b/src/simulation/vehicles/mod.rs @@ -0,0 +1,2 @@ +mod Garage; +mod VehicleType; \ No newline at end of file From cccfcc89591f235c61c8e61854269729fc6de7fe Mon Sep 17 00:00:00 2001 From: janekdererste Date: Tue, 12 Sep 2023 18:06:21 +0200 Subject: [PATCH 02/20] replicate crazy vehicles file structure for serde, remove loading of entire xml file in xml_reader.rs..., fix clippy warnings --- assets/vehicles/vehicles_v2.xml | 3 +- src/simulation/io/vehicles.rs | 126 ++++++++++++++++++++++++++++---- src/simulation/io/xml_reader.rs | 18 ++--- 3 files changed, 121 insertions(+), 26 deletions(-) diff --git a/assets/vehicles/vehicles_v2.xml b/assets/vehicles/vehicles_v2.xml index 0cc307fd..78615be3 100644 --- a/assets/vehicles/vehicles_v2.xml +++ b/assets/vehicles/vehicles_v2.xml @@ -1,7 +1,6 @@ - + diff --git a/src/simulation/io/vehicles.rs b/src/simulation/io/vehicles.rs index 1d091bda..006b7632 100644 --- a/src/simulation/io/vehicles.rs +++ b/src/simulation/io/vehicles.rs @@ -18,35 +18,60 @@ pub struct IOVehicles { } #[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] +#[serde(rename_all = "camelCase")] pub struct IOVehicleType { pub id: String, - pub descr: String, - pub capacity: IOCapacity, - pub length: f32, - pub width: f32, - pub maximum_velocity: f32, - pub engine_information: IOEngineInformation, - pub cost_information: IOCostInformation, - pub passenger_car_equivalents: f32, - pub network_mode: String, - pub flow_efficiency_factor: f32, + pub description: Option, + pub capacity: Option, + pub length: Option, + pub width: Option, + pub maximum_velocity: Option, + pub engine_information: Option, + pub cost_information: Option, + pub passenger_car_equivalents: Option, + pub network_mode: Option, + pub flow_efficiency_factor: Option, pub attributes: Option, } #[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename = "capacity")] pub struct IOCapacity { // leave emtpy for now } #[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename = "capacity")] +pub struct IODimension { + meter: f32, +} + +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] +#[serde(rename_all = "camelCase")] +pub struct IOVelocity { + meter_per_second: f32, +} + +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] +pub struct IOPassengerCarEquivalents { + pce: f32, +} + +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] +#[serde(rename_all = "camelCase")] +pub struct IONetworkMode { + network_mode: String, +} + +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] +pub struct IOFowEfficiencyFactor { + factor: f32, +} + +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] pub struct IOEngineInformation { // leave empty for now. } #[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename = "capacity")] pub struct IOCostInformation { // leave empty for now. } @@ -59,11 +84,82 @@ impl IOVehicleDefinitions { #[cfg(test)] mod test { + use quick_xml::de::from_str; + use crate::simulation::io::vehicles::IOVehicleDefinitions; + #[test] + fn from_string_empty_type() { + let xml = "\ + \ + \ + \ + \ + "; + let veh_def: IOVehicleDefinitions = from_str(xml).unwrap(); + + assert_eq!(1, veh_def.veh_types.len()); + + let veh_type = veh_def.veh_types.first().unwrap(); + assert_eq!("some-vehicle-id", veh_type.id.as_str()); + } + + #[test] + fn from_string_full_type() { + let xml = "\ + \ + \ + some-description\ + \ + \ + \ + \ + \ + \ + \ + \ + "; + + let veh_def: IOVehicleDefinitions = from_str(xml).unwrap(); + + assert_eq!(1, veh_def.veh_types.len()); + + let veh_type = veh_def.veh_types.first().unwrap(); + assert_eq!("some-vehicle-id", veh_type.id.as_str()); + assert_eq!( + "some-description", + veh_type.description.as_ref().unwrap().as_str() + ); + assert_eq!( + "some-network-mode", + veh_type + .network_mode + .as_ref() + .unwrap() + .network_mode + .as_str() + ); + assert_eq!(9.5, veh_type.length.as_ref().unwrap().meter); + assert_eq!(9.5, veh_type.width.as_ref().unwrap().meter); + assert_eq!( + 9.5, + veh_type.maximum_velocity.as_ref().unwrap().meter_per_second + ); + assert_eq!( + 9.5, + veh_type.passenger_car_equivalents.as_ref().unwrap().pce + ); + assert_eq!( + 9.5, + veh_type.flow_efficiency_factor.as_ref().unwrap().factor + ); + } + #[test] fn test_from_file() { - let io_vehicles = IOVehicleDefinitions::from_file("./assets/vehicles/vehicles_v2.xml"); - println!("{io_vehicles:#?}") + let veh_def = IOVehicleDefinitions::from_file("./assets/vehicles/vehicles_v2.xml"); + assert_eq!(3, veh_def.veh_types.len()); + // no further assertions here, as the tests above test the individual properties. + // so, this test mainly tests whether the vehicles implementation calls the xml_reader } } diff --git a/src/simulation/io/xml_reader.rs b/src/simulation/io/xml_reader.rs index c31ddee1..9f38f8c7 100644 --- a/src/simulation/io/xml_reader.rs +++ b/src/simulation/io/xml_reader.rs @@ -1,9 +1,9 @@ -use quick_xml::de::from_reader; -use serde::de::DeserializeOwned; -use std::fs; use std::fs::File; use std::io::BufReader; -use tracing::{debug, info}; + +use quick_xml::de::from_reader; +use serde::de::DeserializeOwned; +use tracing::info; pub fn read(file_path: &str) -> T where @@ -15,26 +15,26 @@ where let buffered_reader = BufReader::new(file); // I guess this could be prettier, but I don't know how to achieve this in Rust yet :-/ - return if file_path.ends_with(".xml.gz") { + if file_path.ends_with(".xml.gz") { // use full name, to avoid ambiguity let decoder = flate2::read::GzDecoder::new(buffered_reader); let buffered_decoder = BufReader::new(decoder); let result: T = from_reader(buffered_decoder).unwrap(); result } else if file_path.ends_with(".xml") { - let s = fs::read_to_string(file_path).expect("Couldn't find file."); - debug!("File content of {}:\n{}", file_path, s); let result: Result = from_reader(buffered_reader); match result { Ok(x) => x, - Err(e) => panic!("Problem reading file: {:?}", e), + Err(_e) => { + panic!("Problem reading file: {_e:?}") + } } } else { panic!( "xml_reader::read: Can't open file path: {}. Only files with endings '.xml' or '.xml.gz' are supported.", file_path ); - }; + } } #[cfg(test)] From de942b58db00491df09c46b960989d1004815697 Mon Sep 17 00:00:00 2001 From: janekdererste Date: Thu, 14 Sep 2023 12:31:29 +0200 Subject: [PATCH 03/20] * add default implementations for vehicles, so that creation in garage.rs becomes easier * rename files, to snake case, as we are not in Java, * add 'add_veh' logic for garage --- src/simulation/io/vehicles.rs | 44 +++++- src/simulation/vehicles/Garage.rs | 32 ----- src/simulation/vehicles/garage.rs | 132 ++++++++++++++++++ src/simulation/vehicles/mod.rs | 4 +- .../{VehicleType.rs => vehicle_type.rs} | 14 +- 5 files changed, 180 insertions(+), 46 deletions(-) delete mode 100644 src/simulation/vehicles/Garage.rs create mode 100644 src/simulation/vehicles/garage.rs rename src/simulation/vehicles/{VehicleType.rs => vehicle_type.rs} (63%) diff --git a/src/simulation/io/vehicles.rs b/src/simulation/io/vehicles.rs index 006b7632..a7dca59a 100644 --- a/src/simulation/io/vehicles.rs +++ b/src/simulation/io/vehicles.rs @@ -41,29 +41,63 @@ pub struct IOCapacity { #[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] pub struct IODimension { - meter: f32, + pub(crate) meter: f32, +} + +impl Default for IODimension { + fn default() -> Self { + Self { meter: 1. } + } } #[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] #[serde(rename_all = "camelCase")] pub struct IOVelocity { - meter_per_second: f32, + pub(crate) meter_per_second: f32, +} + +impl Default for IOVelocity { + fn default() -> Self { + Self { + meter_per_second: f32::MAX, + } + } } #[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] pub struct IOPassengerCarEquivalents { - pce: f32, + pub(crate) pce: f32, +} + +impl Default for IOPassengerCarEquivalents { + fn default() -> Self { + Self { pce: 1. } + } } #[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] #[serde(rename_all = "camelCase")] pub struct IONetworkMode { - network_mode: String, + pub(crate) network_mode: String, +} + +impl Default for IONetworkMode { + fn default() -> Self { + Self { + network_mode: String::from("car"), + } + } } #[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] pub struct IOFowEfficiencyFactor { - factor: f32, + pub(crate) factor: f32, +} + +impl Default for IOFowEfficiencyFactor { + fn default() -> Self { + Self { factor: 1. } + } } #[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] diff --git a/src/simulation/vehicles/Garage.rs b/src/simulation/vehicles/Garage.rs deleted file mode 100644 index cb29c75c..00000000 --- a/src/simulation/vehicles/Garage.rs +++ /dev/null @@ -1,32 +0,0 @@ -use std::collections::HashMap; - -use crate::simulation::id::{Id, IdStore}; -use crate::simulation::messaging::messages::proto::Vehicle; -use crate::simulation::vehicles::VehicleType::VehicleType; - -#[derive(Debug)] -pub struct Garage<'g> { - vehicles: HashMap, Vehicle>, - vehicle_ids: IdStore<'g, Vehicle>, - vehicle_types: Vec, - vehicle_type_ids: IdStore<'g, VehicleType>, - modes: IdStore<'g, String>, -} - -impl<'g> Default for Garage<'g> { - fn default() -> Self { - Garage::new() - } -} - -impl<'g> Garage<'g> { - pub fn new() -> Self { - Garage { - vehicles: Default::default(), - vehicle_ids: Default::default(), - vehicle_types: vec![], - vehicle_type_ids: IdStore::new(), - modes: Default::default(), - } - } -} diff --git a/src/simulation/vehicles/garage.rs b/src/simulation/vehicles/garage.rs new file mode 100644 index 00000000..669ef4fc --- /dev/null +++ b/src/simulation/vehicles/garage.rs @@ -0,0 +1,132 @@ +use std::collections::HashMap; + +use crate::simulation::id::{Id, IdStore}; +use crate::simulation::io::vehicles::IOVehicleType; +use crate::simulation::messaging::messages::proto::Vehicle; +use crate::simulation::vehicles::vehicle_type::VehicleType; + +#[derive(Debug)] +pub struct Garage<'g> { + vehicles: HashMap, Vehicle>, + vehicle_ids: IdStore<'g, Vehicle>, + vehicle_types: Vec, + vehicle_type_ids: IdStore<'g, VehicleType>, + modes: IdStore<'g, String>, +} + +impl<'g> Default for Garage<'g> { + fn default() -> Self { + Garage::new() + } +} + +impl<'g> Garage<'g> { + pub fn new() -> Self { + Garage { + vehicles: Default::default(), + vehicle_ids: Default::default(), + vehicle_types: vec![], + vehicle_type_ids: IdStore::new(), + modes: Default::default(), + } + } + + pub fn add_io_veh_type(&mut self, io_veh_type: IOVehicleType) { + let id = self.vehicle_type_ids.create_id(&io_veh_type.id); + let net_mode = self + .modes + .create_id(&io_veh_type.network_mode.unwrap_or_default().network_mode); + + let veh_type = VehicleType { + id, + length: io_veh_type.length.unwrap_or_default().meter, + width: io_veh_type.width.unwrap_or_default().meter, + max_v: io_veh_type + .maximum_velocity + .unwrap_or_default() + .meter_per_second, + pce: io_veh_type + .passenger_car_equivalents + .unwrap_or_default() + .pce, + fef: io_veh_type + .flow_efficiency_factor + .unwrap_or_default() + .factor, + net_mode, + }; + self.add_veh_type(veh_type); + } + + pub fn add_veh_type(&mut self, veh_type: VehicleType) { + assert_eq!( + veh_type.id.internal, + self.vehicle_types.len(), + "internal id {} and slot in node vec {} were note the same. Probably, node id {} already exsists.", + veh_type.id.internal, + self.vehicle_types.len(), + veh_type.id.external + ); + + self.vehicle_types.push(veh_type); + } +} + +#[cfg(test)] +mod tests { + use crate::simulation::io::vehicles::IOVehicleType; + use crate::simulation::vehicles::garage::Garage; + use crate::simulation::vehicles::vehicle_type::VehicleType; + + #[test] + fn add_veh_type() { + let mut garage = Garage::new(); + let type_id = garage.vehicle_type_ids.create_id("some-type"); + let mode = garage.modes.create_id("default-mode"); + let veh_type = VehicleType::new(type_id, mode); + + garage.add_veh_type(veh_type); + + assert_eq!(1, garage.vehicle_types.len()); + } + + #[test] + #[should_panic] + fn add_veh_type_reject_duplicate() { + let mut garage = Garage::new(); + let type_id = garage.vehicle_type_ids.create_id("some-type"); + let mode = garage.modes.create_id("default-mode"); + let veh_type1 = VehicleType::new(type_id.clone(), mode.clone()); + let veh_type2 = VehicleType::new(type_id.clone(), mode.clone()); + + garage.add_veh_type(veh_type1); + garage.add_veh_type(veh_type2); + } + + #[test] + fn add_io_veh_type() { + let io_veh_type = IOVehicleType { + id: "some-id".to_string(), + description: None, + capacity: None, + length: None, + width: None, + maximum_velocity: None, + engine_information: None, + cost_information: None, + passenger_car_equivalents: None, + network_mode: None, + flow_efficiency_factor: None, + attributes: None, + }; + let mut garage = Garage::new(); + + garage.add_io_veh_type(io_veh_type); + + assert_eq!(1, garage.vehicle_types.len()); + assert_eq!(0, garage.modes.get_from_ext("car").internal); + assert_eq!(0, garage.vehicle_type_ids.get_from_ext("some-id").internal); + + assert!(garage.vehicle_types.get(0).is_some()); + } +} diff --git a/src/simulation/vehicles/mod.rs b/src/simulation/vehicles/mod.rs index b7ec6cc8..d4655f55 100644 --- a/src/simulation/vehicles/mod.rs +++ b/src/simulation/vehicles/mod.rs @@ -1,2 +1,2 @@ -mod Garage; -mod VehicleType; \ No newline at end of file +mod garage; +mod vehicle_type; \ No newline at end of file diff --git a/src/simulation/vehicles/VehicleType.rs b/src/simulation/vehicles/vehicle_type.rs similarity index 63% rename from src/simulation/vehicles/VehicleType.rs rename to src/simulation/vehicles/vehicle_type.rs index 95f87f19..cd174d9e 100644 --- a/src/simulation/vehicles/VehicleType.rs +++ b/src/simulation/vehicles/vehicle_type.rs @@ -2,13 +2,13 @@ use crate::simulation::id::Id; #[derive(Debug)] pub struct VehicleType { - id: Id, - length: f32, - width: f32, - max_v: f32, - pce: f32, - fef: f32, - net_mode: Id, + pub(crate) id: Id, + pub(crate) length: f32, + pub(crate) width: f32, + pub(crate) max_v: f32, + pub(crate) pce: f32, + pub(crate) fef: f32, + pub(crate) net_mode: Id, } impl VehicleType { From 620b0f8cadead0a38e04a77650ee901de297c407 Mon Sep 17 00:00:00 2001 From: janekdererste Date: Thu, 14 Sep 2023 14:18:14 +0200 Subject: [PATCH 04/20] place vehicles into garage when reading in scenario --- src/simulation/config.rs | 10 ++++++++ src/simulation/controller.rs | 11 ++++++-- src/simulation/id.rs | 4 +++ src/simulation/io/vehicles.rs | 2 +- src/simulation/messaging/messages.proto | 3 +-- src/simulation/messaging/messages.rs | 22 +++++++--------- src/simulation/network/link.rs | 16 ++++++------ src/simulation/network/sim_network.rs | 20 +++++++-------- src/simulation/population/population.rs | 34 ++++++++++++++++--------- src/simulation/simulation.rs | 27 +++++++++++++++----- src/simulation/vehicles/garage.rs | 21 ++++++++++++++- src/simulation/vehicles/mod.rs | 4 +-- 12 files changed, 115 insertions(+), 59 deletions(-) diff --git a/src/simulation/config.rs b/src/simulation/config.rs index 02083c4a..97c6f790 100644 --- a/src/simulation/config.rs +++ b/src/simulation/config.rs @@ -14,6 +14,8 @@ pub struct Config { #[arg(long)] pub population_file: String, #[arg(long)] + pub vehicles_file: String, + #[arg(long)] pub vehicle_definitions_file: Option, #[arg(long, default_value = "./")] pub output_dir: String, @@ -35,6 +37,7 @@ pub struct ConfigBuilder { num_parts: usize, network_file: String, population_file: String, + vehicles_file: String, vehicle_definitions_file: Option, output_dir: String, events_mode: String, @@ -46,6 +49,7 @@ impl ConfigBuilder { ConfigBuilder { network_file: String::from(""), population_file: String::from(""), + vehicles_file: String::from(""), vehicle_definitions_file: None, output_dir: String::from("./"), events_mode: String::from("file"), @@ -81,6 +85,11 @@ impl ConfigBuilder { self } + pub fn vehicles_file(mut self, file: String) -> Self { + self.vehicles_file = file; + self + } + pub fn output_dir(mut self, dir: String) -> Self { self.output_dir = dir; self @@ -111,6 +120,7 @@ impl ConfigBuilder { num_parts: self.num_parts, network_file: self.network_file, population_file: self.population_file, + vehicles_file: self.vehicles_file, vehicle_definitions_file: self.vehicle_definitions_file, output_dir: self.output_dir, events_mode: self.events_mode, diff --git a/src/simulation/controller.rs b/src/simulation/controller.rs index a7755a09..62434e1d 100644 --- a/src/simulation/controller.rs +++ b/src/simulation/controller.rs @@ -14,6 +14,7 @@ use crate::simulation::messaging::message_broker::MpiMessageBroker; use crate::simulation::network::sim_network::SimNetworkPartition; use crate::simulation::population::population::Population; use crate::simulation::simulation::Simulation; +use crate::simulation::vehicles::garage::Garage; pub fn run(world: SystemCommunicator, config: Config) { let rank = world.rank(); @@ -34,8 +35,14 @@ pub fn run(world: SystemCommunicator, config: Config) { network.to_file(&output_path.join("output_network.xml.gz")); } - let population = - Population::from_file(config.population_file.as_ref(), &network, rank as usize); + let mut garage = Garage::from_file(config.vehicles_file.as_ref()); + + let population = Population::from_file( + config.population_file.as_ref(), + &network, + &mut garage, + rank as usize, + ); let network_partition = SimNetworkPartition::from_network(&network, rank as usize); info!( "Partition #{rank} network has: {} nodes and {} links. Population has {} agents", diff --git a/src/simulation/id.rs b/src/simulation/id.rs index e5e1f3fc..99cba5c7 100644 --- a/src/simulation/id.rs +++ b/src/simulation/id.rs @@ -89,6 +89,10 @@ impl<'ext, T> IdStore<'ext, T> { .clone() } + pub fn get_from_wire(&self, internal: u64) -> Id { + self.get(internal as usize) + } + pub fn get_from_ext(&self, external: &str) -> Id { let index = self .mapping diff --git a/src/simulation/io/vehicles.rs b/src/simulation/io/vehicles.rs index a7dca59a..43841c76 100644 --- a/src/simulation/io/vehicles.rs +++ b/src/simulation/io/vehicles.rs @@ -7,7 +7,7 @@ use crate::simulation::io::xml_reader; #[serde(rename = "vehicleDefinitions")] pub struct IOVehicleDefinitions { #[serde(rename = "vehicleType")] - veh_types: Vec, + pub veh_types: Vec, } #[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] diff --git a/src/simulation/messaging/messages.proto b/src/simulation/messaging/messages.proto index 61eef18e..87758685 100644 --- a/src/simulation/messaging/messages.proto +++ b/src/simulation/messaging/messages.proto @@ -26,7 +26,6 @@ message Vehicle { uint32 curr_route_elem = 2; uint64 type = 3; Agent agent = 4; - uint64 mode = 5; // this must be extended once more people use one vehicle } @@ -42,7 +41,7 @@ message Plan { } message Activity { - string act_type = 1; + uint64 act_type = 1; uint64 link_id = 2; float x = 3; float y = 4; diff --git a/src/simulation/messaging/messages.rs b/src/simulation/messaging/messages.rs index 1d1d8bfc..f364e8b6 100644 --- a/src/simulation/messaging/messages.rs +++ b/src/simulation/messaging/messages.rs @@ -112,13 +112,12 @@ impl Ord for VehicleMessage { impl Vehicle { // todo, fix type and mode - pub fn new(id: u64, veh_type: u64, mode: u64, agent: Agent) -> Vehicle { + pub fn new(id: u64, veh_type: u64, agent: Option) -> Vehicle { Vehicle { - id, - agent: Some(agent), + id: id, + agent, curr_route_elem: 0, - r#type: 0, - mode: 0, + r#type: veh_type, } } @@ -402,7 +401,7 @@ impl Plan { for element in &io_plan.elements { match element { IOPlanElement::Activity(io_act) => { - let act = Activity::from_io(io_act, net); + let act = Activity::from_io(io_act, net, pop); result.acts.push(act); } IOPlanElement::Leg(io_leg) => { @@ -429,12 +428,13 @@ impl Plan { } impl Activity { - fn from_io(io_act: &IOActivity, net: &Network) -> Self { + fn from_io(io_act: &IOActivity, net: &Network, pop: &Population) -> Self { let link_id = net.link_ids.get_from_ext(&io_act.link); + let act_type = pop.act_types.get_from_ext(&io_act.r#type); Activity { x: io_act.x, y: io_act.y, - act_type: io_act.r#type.clone(), + act_type: act_type.internal as u64, link_id: link_id.internal as u64, start_time: parse_time_opt(&io_act.start_time), end_time: parse_time_opt(&io_act.end_time), @@ -445,7 +445,7 @@ impl Activity { pub fn new( x: f32, y: f32, - act_type: String, + act_type: u64, link_id: u64, start_time: Option, end_time: Option, @@ -472,10 +472,6 @@ impl Activity { u32::MAX } } - - pub fn is_interaction(&self) -> bool { - self.act_type.contains("interaction") - } } impl Leg { diff --git a/src/simulation/network/link.rs b/src/simulation/network/link.rs index 1bec72ce..79b3d32b 100644 --- a/src/simulation/network/link.rs +++ b/src/simulation/network/link.rs @@ -279,7 +279,7 @@ mod tests { IdImpl::new_internal(0), ); let agent = create_agent(1, vec![]); - let vehicle = Vehicle::new(veh_id, 0, 0, agent); + let vehicle = Vehicle::new(veh_id, 0, Some(agent)); link.push_vehicle(vehicle, 0); @@ -305,9 +305,9 @@ mod tests { ); let agent1 = create_agent(1, vec![]); - let vehicle1 = Vehicle::new(id1, 0, 0, agent1); + let vehicle1 = Vehicle::new(id1, 0, Some(agent1)); let agent2 = create_agent(1, vec![]); - let vehicle2 = Vehicle::new(id2, 0, 0, agent2); + let vehicle2 = Vehicle::new(id2, 0, Some(agent2)); link.push_vehicle(vehicle1, 0); link.push_vehicle(vehicle2, 0); @@ -341,7 +341,7 @@ mod tests { while n < 10 { let agent = create_agent(n as u64, vec![]); - let vehicle = Vehicle::new(n as u64, 0, 0, agent); + let vehicle = Vehicle::new(n as u64, 0, Some(agent)); link.push_vehicle(vehicle, n); n += 1; } @@ -372,7 +372,7 @@ mod tests { while n < 10 { let agent = create_agent(n as u64, vec![]); - let vehicle = Vehicle::new(n as u64, 0, 0, agent); + let vehicle = Vehicle::new(n as u64, 0, Some(agent)); link.push_vehicle(vehicle, n); n += 1; } @@ -400,9 +400,9 @@ mod tests { ); let agent1 = create_agent(1, vec![]); - let vehicle1 = Vehicle::new(1, 0, 0, agent1); + let vehicle1 = Vehicle::new(1, 0, Some(agent1)); let agent2 = create_agent(2, vec![]); - let vehicle2 = Vehicle::new(2, 0, 0, agent2); + let vehicle2 = Vehicle::new(2, 0, Some(agent2)); link.push_vehicle(vehicle1, 0); link.push_vehicle(vehicle2, 0); @@ -421,7 +421,7 @@ mod tests { fn create_agent(id: u64, route: Vec) -> Agent { let route = Route::NetworkRoute(NetworkRoute::new(id, route)); let leg = Leg::new(route, "car", None, None); - let act = Activity::new(0., 0., String::from("some-type"), 1, None, None, None); + let act = Activity::new(0., 0., 0, 1, None, None, None); let mut plan = Plan::new(); plan.add_act(act); plan.add_leg(leg); diff --git a/src/simulation/network/sim_network.rs b/src/simulation/network/sim_network.rs index d19c8403..ac37e1dc 100644 --- a/src/simulation/network/sim_network.rs +++ b/src/simulation/network/sim_network.rs @@ -156,9 +156,7 @@ impl<'n> SimNetworkPartition<'n> { &Event::new_link_leave(vehicle.curr_route_elem as u64, vehicle.id), ); vehicle.advance_route_index(); - let link_id = global_network - .link_ids - .get(vehicle.curr_route_elem as usize); + let link_id = global_network.link_ids.get(vehicle.curr_link_id().unwrap()); match links.get_mut(&link_id).unwrap() { SimLink::Local(l) => { events.publish_event( @@ -232,7 +230,7 @@ mod tests { let mut sim_network = create_single_node_sim_network(&mut network); let mut publisher = EventsPublisher::new(); let agent = create_agent(1, vec![0]); - let vehicle = Vehicle::new(1, 0, 0, agent); + let vehicle = Vehicle::new(1, 0, Some(agent)); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { link1.push_vehicle(vehicle, 1); @@ -250,7 +248,7 @@ mod tests { let mut sim_network = create_single_node_sim_network(&mut network); let mut publisher = EventsPublisher::new(); let agent = create_agent(1, vec![0, 1]); - let vehicle = Vehicle::new(1, 0, 0, agent); + let vehicle = Vehicle::new(1, 0, Some(agent)); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { link1.push_vehicle(vehicle, 1); @@ -273,7 +271,7 @@ mod tests { let sim_network = sim_nets.get_mut(0).unwrap(); let mut publisher = EventsPublisher::new(); let agent = create_agent(1, vec![0, 1]); - let vehicle = Vehicle::new(1, 0, 0, agent); + let vehicle = Vehicle::new(1, 0, Some(agent)); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { link1.push_vehicle(vehicle, 1); @@ -292,9 +290,9 @@ mod tests { let mut sim_network = create_single_node_sim_network(&mut network); let mut publisher = EventsPublisher::new(); let agent1 = create_agent(1, vec![0]); - let vehicle1 = Vehicle::new(1, 0, 0, agent1); + let vehicle1 = Vehicle::new(1, 0, Some(agent1)); let agent2 = create_agent(2, vec![0]); - let vehicle2 = Vehicle::new(2, 0, 0, agent2); + let vehicle2 = Vehicle::new(2, 0, Some(agent2)); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { link1.push_vehicle(vehicle1, 1); @@ -325,9 +323,9 @@ mod tests { let mut sim_network = create_single_node_sim_network(&mut network); let mut publisher = EventsPublisher::new(); let agent1 = create_agent(1, vec![0, 1]); - let vehicle1 = Vehicle::new(1, 0, 0, agent1); + let vehicle1 = Vehicle::new(1, 0, Some(agent1)); let agent2 = create_agent(2, vec![0, 1]); - let vehicle2 = Vehicle::new(2, 0, 0, agent2); + let vehicle2 = Vehicle::new(2, 0, Some(agent2)); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { link1.push_vehicle(vehicle1, 1); @@ -451,7 +449,7 @@ mod tests { fn create_agent(id: u64, route: Vec) -> Agent { let route = Route::NetworkRoute(NetworkRoute::new(id, route)); let leg = Leg::new(route, "car", None, None); - let act = Activity::new(0., 0., String::from("some-type"), 1, None, None, None); + let act = Activity::new(0., 0., 0, 1, None, None, None); let mut plan = Plan::new(); plan.add_act(act); plan.add_leg(leg); diff --git a/src/simulation/population/population.rs b/src/simulation/population/population.rs index a8604897..66447892 100644 --- a/src/simulation/population/population.rs +++ b/src/simulation/population/population.rs @@ -4,6 +4,7 @@ use crate::simulation::id::{Id, IdStore}; use crate::simulation::io::population::{IOPerson, IOPlanElement, IOPopulation, IORoute}; use crate::simulation::messaging::messages::proto::{Agent, Vehicle}; use crate::simulation::network::global_network::{Link, Network}; +use crate::simulation::vehicles::garage::Garage; type ActType = (); @@ -26,17 +27,22 @@ impl<'p> Population<'p> { } } - pub fn from_file(file: &str, net: &Network, partition: usize) -> Self { + pub fn from_file(file: &str, net: &Network, garage: &mut Garage, partition: usize) -> Self { let io_population = IOPopulation::from_file(file); - Self::from_io(&io_population, net, partition) + Self::from_io(&io_population, net, garage, partition) } - pub fn from_io(io_population: &IOPopulation, network: &Network, partition: usize) -> Self { + pub fn from_io( + io_population: &IOPopulation, + network: &Network, + garage: &mut Garage, + partition: usize, + ) -> Self { let mut result = Population::new(); // first pass to set ids globally for io in io_population.persons.iter() { - Self::agent_id(io, &mut result); + Self::agent_id(io, &mut result, garage); } // then copy the agents on this partition @@ -63,7 +69,7 @@ impl<'p> Population<'p> { panic!("First element should be activity."); } - fn agent_id(io: &IOPerson, pop: &mut Population) { + fn agent_id(io: &IOPerson, pop: &mut Population, garage: &mut Garage) { pop.agent_ids.create_id(&io.id); for io_plan in io.plans.iter() { for element in io_plan.elements.iter() { @@ -72,14 +78,14 @@ impl<'p> Population<'p> { pop.act_types.create_id(&a.r#type); } IOPlanElement::Leg(l) => { - Self::route_ids(&l.route, pop); + Self::route_ids(&l.route, garage); } } } } } - fn route_ids(io: &IORoute, pop: &mut Population) { + fn route_ids(io: &IORoute, garage: &mut Garage) { match io.r#type.as_str() { "links" => { let veh_id = io @@ -89,7 +95,7 @@ impl<'p> Population<'p> { match veh_id.as_str() { "null" => (), _ => { - pop.vehicle_ids.create_id(veh_id); + garage.add_veh_id(veh_id); } }; } @@ -103,11 +109,13 @@ mod tests { use crate::simulation::messaging::messages::proto::leg::Route; use crate::simulation::network::global_network::Network; use crate::simulation::population::population::Population; + use crate::simulation::vehicles::garage::Garage; #[test] fn from_io_1_plan() { let net = Network::from_file("./assets/equil/equil-network.xml", 1); - let pop = Population::from_file("./assets/equil/equil-1-plan.xml", &net, 0); + let mut garage = Garage::new(); + let pop = Population::from_file("./assets/equil/equil-1-plan.xml", &net, &mut garage, 0); assert_eq!(1, pop.agents.len()); @@ -119,7 +127,8 @@ mod tests { assert_eq!(3, plan.legs.len()); let home_act = plan.acts.first().unwrap(); - assert_eq!("h", home_act.act_type.as_str()); + let act_type = pop.act_types.get_from_wire(home_act.act_type); + assert_eq!("h", act_type.external.as_str()); assert_eq!( net.link_ids.get_from_ext("1").internal as u64, home_act.link_id @@ -156,8 +165,9 @@ mod tests { #[test] fn from_io() { let net = Network::from_file("./assets/equil/equil-network.xml", 2); - let pop1 = Population::from_file("./assets/equil/equil-plans.xml.gz", &net, 0); - let pop2 = Population::from_file("./assets/equil/equil-plans.xml.gz", &net, 1); + let mut garage = Garage::new(); + let pop1 = Population::from_file("./assets/equil/equil-plans.xml.gz", &net, &mut garage, 0); + let pop2 = Population::from_file("./assets/equil/equil-plans.xml.gz", &net, &mut garage, 1); // metis produces unstable results on small networks so, make sure that one of the populations // has all the agents and the other doesn't diff --git a/src/simulation/simulation.rs b/src/simulation/simulation.rs index 179bd3ff..4bf3d466 100644 --- a/src/simulation/simulation.rs +++ b/src/simulation/simulation.rs @@ -8,12 +8,14 @@ use crate::simulation::messaging::messages::proto::leg::Route; use crate::simulation::messaging::messages::proto::{Agent, GenericRoute, Vehicle}; use crate::simulation::network::link::SimLink; use crate::simulation::network::sim_network::{ExitReason, SimNetworkPartition}; +use crate::simulation::population::population::Population; use crate::simulation::time_queue::TimeQueue; pub struct Simulation<'sim> { activity_q: TimeQueue, teleportation_q: TimeQueue, network: SimNetworkPartition<'sim>, + population: Population<'sim>, message_broker: MpiMessageBroker, events: EventsPublisher, } @@ -22,17 +24,23 @@ impl<'sim> Simulation<'sim> { pub fn new( config: &Config, network: SimNetworkPartition<'sim>, - population: crate::simulation::population::population::Population, + mut population: Population<'sim>, message_broker: MpiMessageBroker, events: EventsPublisher, ) -> Self { let mut activity_q = TimeQueue::new(); - for agent in population.agents.into_values() { + + // take agents and copy them into queues. This way we can keep population around to tranlate + // ids for events processing... + let agents = std::mem::take(&mut population.agents); + + for agent in agents.into_values() { activity_q.add(agent, config.start_time); } Simulation { network, + population, teleportation_q: TimeQueue::new(), activity_q, message_broker, @@ -72,12 +80,16 @@ impl<'sim> Simulation<'sim> { for mut agent in agents { let agent_id = agent.id; + let act_type = self + .population + .act_types + .get_from_wire(agent.curr_act().act_type); self.events.publish_event( now, &Event::new_act_end( agent_id, agent.curr_act().link_id, - agent.curr_act().act_type.clone(), + act_type.external.clone(), ), ); @@ -96,10 +108,10 @@ impl<'sim> Simulation<'sim> { ); if Simulation::is_local_route(route, &self.message_broker) { - let veh = Vehicle::new(agent.id, 1, leg.mode.clone(), agent); + let veh = Vehicle::new(agent.id, 1, Some(agent)); self.teleportation_q.add(veh, now); } else { - let veh = Vehicle::new(agent.id, 1, leg.mode.clone(), agent); + let veh = Vehicle::new(agent.id, 1, Some(agent)); self.message_broker.add_veh(veh, now); } } @@ -115,7 +127,7 @@ impl<'sim> Simulation<'sim> { &Event::new_person_enters_veh(agent_id, route.vehicle_id), ); - let veh = Vehicle::new(route.vehicle_id, 0, leg.mode.clone(), agent); + let veh = Vehicle::new(route.vehicle_id, 0, Some(agent)); self.veh_onto_network(veh, true, now); } } @@ -188,9 +200,10 @@ impl<'sim> Simulation<'sim> { &Event::new_arrival(agent.id, act.link_id, String::from("some mode")), ); //todo fix mode + let act_type = self.population.act_types.get_from_wire(act.act_type); self.events.publish_event( now, - &Event::new_act_start(agent.id, act.link_id, act.act_type.clone()), + &Event::new_act_start(agent.id, act.link_id, act_type.external.clone()), ); self.activity_q.add(agent, now); } diff --git a/src/simulation/vehicles/garage.rs b/src/simulation/vehicles/garage.rs index 669ef4fc..953be158 100644 --- a/src/simulation/vehicles/garage.rs +++ b/src/simulation/vehicles/garage.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use crate::simulation::id::{Id, IdStore}; -use crate::simulation::io::vehicles::IOVehicleType; +use crate::simulation::io::vehicles::{IOVehicleDefinitions, IOVehicleType}; use crate::simulation::messaging::messages::proto::Vehicle; use crate::simulation::vehicles::vehicle_type::VehicleType; @@ -31,6 +31,15 @@ impl<'g> Garage<'g> { } } + pub fn from_file(file_path: &str) -> Self { + let io_veh_definition = IOVehicleDefinitions::from_file(file_path); + let mut result = Self::new(); + for io_veh_type in io_veh_definition.veh_types { + result.add_io_veh_type(io_veh_type); + } + result + } + pub fn add_io_veh_type(&mut self, io_veh_type: IOVehicleType) { let id = self.vehicle_type_ids.create_id(&io_veh_type.id); let net_mode = self @@ -70,6 +79,10 @@ impl<'g> Garage<'g> { self.vehicle_types.push(veh_type); } + + pub fn add_veh_id(&mut self, external_id: &str) -> Id { + self.vehicle_ids.create_id(external_id) + } } #[cfg(test)] @@ -129,4 +142,10 @@ mod tests { assert!(garage.vehicle_types.get(0).is_some()); } + + #[test] + fn from_file() { + let garage = Garage::from_file("./assets/vehicles/vehicles_v2.xml"); + assert_eq!(3, garage.vehicle_types.len()); + } } diff --git a/src/simulation/vehicles/mod.rs b/src/simulation/vehicles/mod.rs index d4655f55..86f9b104 100644 --- a/src/simulation/vehicles/mod.rs +++ b/src/simulation/vehicles/mod.rs @@ -1,2 +1,2 @@ -mod garage; -mod vehicle_type; \ No newline at end of file +pub mod garage; +pub mod vehicle_type; \ No newline at end of file From d5d982aa549edf06d66f1decb4a394082b9d76de Mon Sep 17 00:00:00 2001 From: janekdererste Date: Thu, 14 Sep 2023 14:43:14 +0200 Subject: [PATCH 05/20] remove vehicle ids from garage --- src/simulation/messaging/messages.rs | 35 ++++++++++++++++--------- src/simulation/population/population.rs | 8 +++--- src/simulation/vehicles/garage.rs | 10 +++---- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/simulation/messaging/messages.rs b/src/simulation/messaging/messages.rs index f364e8b6..03449ab4 100644 --- a/src/simulation/messaging/messages.rs +++ b/src/simulation/messaging/messages.rs @@ -16,6 +16,7 @@ use crate::simulation::messaging::messages::proto::{ use crate::simulation::network::global_network::Network; use crate::simulation::population::population::Population; use crate::simulation::time_queue::EndTime; +use crate::simulation::vehicles::garage::Garage; // Include the `messages` module, which is generated from messages.proto. pub mod proto { @@ -162,8 +163,13 @@ impl EndTime for Vehicle { } impl Agent { - pub fn from_io(io_person: &IOPerson, net: &Network, pop: &Population) -> Agent { - let plan = Plan::from_io(io_person.selected_plan(), net, pop); + pub fn from_io( + io_person: &IOPerson, + net: &Network, + pop: &Population, + garage: &Garage, + ) -> Agent { + let plan = Plan::from_io(io_person.selected_plan(), net, pop, garage); if plan.acts.is_empty() { debug!("There is an empty plan for person {:?}", io_person.id); @@ -386,16 +392,21 @@ impl Plan { } } - fn from_io(io_plan: &IOPlan, net: &Network, pop: &Population) -> Plan { + fn from_io(io_plan: &IOPlan, net: &Network, pop: &Population, garage: &Garage) -> Plan { assert!(!io_plan.elements.is_empty()); if let IOPlanElement::Leg(_leg) = io_plan.elements.get(0).unwrap() { panic!("First plan element must be an activity! But was a leg."); }; - Plan::get_full_plan_no_routing(io_plan, net, pop) + Plan::get_full_plan_no_routing(io_plan, net, pop, garage) } - fn get_full_plan_no_routing(io_plan: &IOPlan, net: &Network, pop: &Population) -> Plan { + fn get_full_plan_no_routing( + io_plan: &IOPlan, + net: &Network, + pop: &Population, + garage: &Garage, + ) -> Plan { let mut result = Plan::new(); for element in &io_plan.elements { @@ -405,7 +416,7 @@ impl Plan { result.acts.push(act); } IOPlanElement::Leg(io_leg) => { - let leg = Leg::from_io(io_leg, net, pop); + let leg = Leg::from_io(io_leg, net, garage); result.legs.push(leg); } } @@ -475,8 +486,8 @@ impl Activity { } impl Leg { - fn from_io(io_leg: &IOLeg, net: &Network, pop: &Population) -> Self { - let route = Route::from_io(&io_leg.route, net, pop); + fn from_io(io_leg: &IOLeg, net: &Network, garage: &Garage) -> Self { + let route = Route::from_io(&io_leg.route, net, garage); Self { route: Some(route), mode: 0, //todo fix mode @@ -496,7 +507,7 @@ impl Leg { } impl Route { - fn from_io(io_route: &IORoute, net: &Network, pop: &Population) -> Self { + fn from_io(io_route: &IORoute, net: &Network, garage: &Garage) -> Self { match io_route.r#type.as_str() { "generic" => Route::GenericRoute(GenericRoute::from_io(io_route, net)), "links" => { @@ -506,7 +517,7 @@ impl Route { if vehicle_id.eq("null") { Route::GenericRoute(GenericRoute::from_io(io_route, net)) } else { - Route::NetworkRoute(NetworkRoute::from_io(io_route, net, pop)) + Route::NetworkRoute(NetworkRoute::from_io(io_route, net, garage)) } } else { panic!("vehicle id is expected to be set. ") @@ -533,12 +544,12 @@ impl GenericRoute { } impl NetworkRoute { - fn from_io(io_route: &IORoute, net: &Network, pop: &Population) -> Self { + fn from_io(io_route: &IORoute, net: &Network, garage: &Garage) -> Self { let matsim_veh_id = io_route .vehicle .as_ref() .unwrap_or_else(|| panic!("Couldn't find veh-id for route {io_route:?} ")); - let veh_id = pop.vehicle_ids.get_from_ext(matsim_veh_id); + let veh_id = garage.vehicle_ids.get_from_ext(matsim_veh_id); let link_ids = match &io_route.route { None => Vec::new(), diff --git a/src/simulation/population/population.rs b/src/simulation/population/population.rs index 66447892..92f8dfe1 100644 --- a/src/simulation/population/population.rs +++ b/src/simulation/population/population.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use crate::simulation::id::{Id, IdStore}; use crate::simulation::io::population::{IOPerson, IOPlanElement, IOPopulation, IORoute}; -use crate::simulation::messaging::messages::proto::{Agent, Vehicle}; +use crate::simulation::messaging::messages::proto::Agent; use crate::simulation::network::global_network::{Link, Network}; use crate::simulation::vehicles::garage::Garage; @@ -12,7 +12,6 @@ type ActType = (); pub struct Population<'p> { pub agents: HashMap, Agent>, pub agent_ids: IdStore<'p, Agent>, - pub vehicle_ids: IdStore<'p, Vehicle>, // TODO this should probably go somewhere else pub act_types: IdStore<'p, ActType>, } @@ -22,7 +21,6 @@ impl<'p> Population<'p> { Population { agents: HashMap::default(), agent_ids: IdStore::new(), - vehicle_ids: IdStore::new(), act_types: IdStore::new(), } } @@ -49,7 +47,7 @@ impl<'p> Population<'p> { for io in io_population.persons.iter() { let link = Self::link_first_act(io, network); if partition == link.partition { - let agent = Agent::from_io(io, network, &result); + let agent = Agent::from_io(io, network, &result, garage); result .agents .insert(result.agent_ids.get(agent.id as usize), agent); @@ -145,7 +143,7 @@ mod tests { assert!(leg.route.is_some()); if let Route::NetworkRoute(net_route) = leg.route.as_ref().unwrap() { assert_eq!( - pop.vehicle_ids.get_from_ext("1").internal as u64, + garage.vehicle_ids.get_from_ext("1").internal as u64, net_route.vehicle_id ); assert_eq!( diff --git a/src/simulation/vehicles/garage.rs b/src/simulation/vehicles/garage.rs index 953be158..0514ba37 100644 --- a/src/simulation/vehicles/garage.rs +++ b/src/simulation/vehicles/garage.rs @@ -7,11 +7,11 @@ use crate::simulation::vehicles::vehicle_type::VehicleType; #[derive(Debug)] pub struct Garage<'g> { - vehicles: HashMap, Vehicle>, - vehicle_ids: IdStore<'g, Vehicle>, - vehicle_types: Vec, - vehicle_type_ids: IdStore<'g, VehicleType>, - modes: IdStore<'g, String>, + pub vehicles: HashMap, Vehicle>, + pub vehicle_ids: IdStore<'g, Vehicle>, + pub vehicle_types: Vec, + pub vehicle_type_ids: IdStore<'g, VehicleType>, + pub modes: IdStore<'g, String>, } impl<'g> Default for Garage<'g> { From abecd70905bae82afebfae432a33b936e5758d8d Mon Sep 17 00:00:00 2001 From: janekdererste Date: Thu, 14 Sep 2023 15:10:29 +0200 Subject: [PATCH 06/20] move attributes relevant for qsim into vehicle to make calculation of link dynamics easier. --- src/simulation/controller.rs | 5 ++-- src/simulation/messaging/messages.proto | 4 +++- src/simulation/messaging/messages.rs | 4 +++- src/simulation/network/global_network.rs | 29 +++++++++++++----------- src/simulation/network/link.rs | 14 ++++++------ src/simulation/network/sim_network.rs | 14 ++++++------ src/simulation/population/population.rs | 4 ++-- src/simulation/simulation.rs | 6 ++--- 8 files changed, 44 insertions(+), 36 deletions(-) diff --git a/src/simulation/controller.rs b/src/simulation/controller.rs index 62434e1d..c8a4263b 100644 --- a/src/simulation/controller.rs +++ b/src/simulation/controller.rs @@ -25,9 +25,12 @@ pub fn run(world: SystemCommunicator, config: Config) { let output_path = PathBuf::from(&config.output_dir); fs::create_dir_all(&output_path).expect("Failed to create output path"); + let mut garage = Garage::from_file(config.vehicles_file.as_ref()); + let network = crate::simulation::network::global_network::Network::from_file( config.network_file.as_ref(), config.num_parts, + &mut garage, ); // write network with new ids to output but only once. @@ -35,8 +38,6 @@ pub fn run(world: SystemCommunicator, config: Config) { network.to_file(&output_path.join("output_network.xml.gz")); } - let mut garage = Garage::from_file(config.vehicles_file.as_ref()); - let population = Population::from_file( config.population_file.as_ref(), &network, diff --git a/src/simulation/messaging/messages.proto b/src/simulation/messaging/messages.proto index 87758685..5c86ff90 100644 --- a/src/simulation/messaging/messages.proto +++ b/src/simulation/messaging/messages.proto @@ -25,7 +25,9 @@ message Vehicle { uint64 id = 1; uint32 curr_route_elem = 2; uint64 type = 3; - Agent agent = 4; + float max_v = 4; + float pce = 5; + Agent agent = 6; // this must be extended once more people use one vehicle } diff --git a/src/simulation/messaging/messages.rs b/src/simulation/messaging/messages.rs index 03449ab4..45afb084 100644 --- a/src/simulation/messaging/messages.rs +++ b/src/simulation/messaging/messages.rs @@ -113,12 +113,14 @@ impl Ord for VehicleMessage { impl Vehicle { // todo, fix type and mode - pub fn new(id: u64, veh_type: u64, agent: Option) -> Vehicle { + pub fn new(id: u64, veh_type: u64, max_v: f32, pce: f32, agent: Option) -> Vehicle { Vehicle { id: id, agent, curr_route_elem: 0, r#type: veh_type, + max_v, + pce, } } diff --git a/src/simulation/network/global_network.rs b/src/simulation/network/global_network.rs index afc5223b..1783499c 100644 --- a/src/simulation/network/global_network.rs +++ b/src/simulation/network/global_network.rs @@ -1,6 +1,7 @@ use std::{collections::HashSet, path::Path}; use crate::simulation::io::attributes::{Attr, Attrs}; +use crate::simulation::vehicles::garage::Garage; use crate::simulation::{ id::{Id, IdStore}, io::network::{IOLink, IONetwork, IONode}, @@ -16,7 +17,7 @@ pub struct Network<'a> { // we make sure to store each mode only once. This could be optimized further if we'd // cache the HashSets which we store in the links. I.e. each combination of modes is only // one hash set. - pub modes: IdStore<'a, String>, + // pub modes: IdStore<'a, String>, pub nodes: Vec, pub links: Vec, } @@ -57,16 +58,15 @@ impl<'a> Network<'a> { Network { node_ids: IdStore::new(), link_ids: IdStore::new(), - modes: IdStore::new(), nodes: Vec::new(), links: Vec::new(), } } - pub fn from_file(file_path: &str, num_parts: usize) -> Self { + pub fn from_file(file_path: &str, num_parts: usize, garage: &mut Garage) -> Self { let io_network = IONetwork::from_file(file_path); let mut result = Network::new(); - Self::init_nodes_and_links(&mut result, io_network); + Self::init_nodes_and_links(&mut result, io_network, garage); Self::partition_network(&mut result, num_parts); result } @@ -170,7 +170,7 @@ impl<'a> Network<'a> { self.links.push(link); } - pub fn add_io_link(&mut self, io_link: IOLink) { + pub fn add_io_link(&mut self, io_link: IOLink, garage: &mut Garage) { let id = self.link_ids.create_id(&io_link.id); assert_eq!( id.internal, @@ -188,7 +188,7 @@ impl<'a> Network<'a> { .modes .split(',') .map(|s| s.trim()) - .map(|mode| self.modes.create_id(mode)) + .map(|mode| garage.modes.create_id(mode)) .collect(); let from_id = self.node_ids.get_from_ext(&io_link.from); let to_id = self.node_ids.get_from_ext(&io_link.to); @@ -215,13 +215,13 @@ impl<'a> Network<'a> { self.links.get(id.internal).unwrap() } - fn init_nodes_and_links(network: &mut Network, io_network: IONetwork) { + fn init_nodes_and_links(network: &mut Network, io_network: IONetwork, garage: &mut Garage) { for node in io_network.nodes.nodes { network.add_io_node(node) } for link in io_network.links.links { - network.add_io_link(link) + network.add_io_link(link, garage) } } @@ -301,6 +301,7 @@ impl Link { #[cfg(test)] mod tests { use crate::simulation::io::network::{IOLink, IONode}; + use crate::simulation::vehicles::garage::Garage; use super::{Link, Network, Node}; @@ -430,10 +431,11 @@ mod tests { attributes: None, }; + let mut garage = Garage::new(); let mut network = Network::new(); network.add_io_node(io_from); network.add_io_node(io_to); - network.add_io_link(io_link.clone()); + network.add_io_link(io_link.clone(), &mut garage); let from = network.get_node(&network.node_ids.get_from_ext(&ext_from_id)); let to = network.get_node(&network.node_ids.get_from_ext(&ext_to_id)); @@ -447,14 +449,15 @@ mod tests { assert_eq!(io_link.freespeed, link.freespeed); assert_eq!(io_link.permlanes, link.permlanes); - assert!(link.modes.contains(&network.modes.get_from_ext("car"))); - assert!(link.modes.contains(&network.modes.get_from_ext("ride"))); - assert!(link.modes.contains(&network.modes.get_from_ext("bike"))); + assert!(link.modes.contains(&garage.modes.get_from_ext("car"))); + assert!(link.modes.contains(&garage.modes.get_from_ext("ride"))); + assert!(link.modes.contains(&garage.modes.get_from_ext("bike"))); } #[test] fn from_file() { - let network = Network::from_file("./assets/equil/equil-network.xml", 2); + let mut garage = Garage::default(); + let network = Network::from_file("./assets/equil/equil-network.xml", 2, &mut garage); // check partitioning let expected_partitions = [0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0]; diff --git a/src/simulation/network/link.rs b/src/simulation/network/link.rs index 79b3d32b..004168f8 100644 --- a/src/simulation/network/link.rs +++ b/src/simulation/network/link.rs @@ -279,7 +279,7 @@ mod tests { IdImpl::new_internal(0), ); let agent = create_agent(1, vec![]); - let vehicle = Vehicle::new(veh_id, 0, Some(agent)); + let vehicle = Vehicle::new(veh_id, 0, 10., 1., Some(agent)); link.push_vehicle(vehicle, 0); @@ -305,9 +305,9 @@ mod tests { ); let agent1 = create_agent(1, vec![]); - let vehicle1 = Vehicle::new(id1, 0, Some(agent1)); + let vehicle1 = Vehicle::new(id1, 0, 10., 1., Some(agent1)); let agent2 = create_agent(1, vec![]); - let vehicle2 = Vehicle::new(id2, 0, Some(agent2)); + let vehicle2 = Vehicle::new(id2, 0, 10., 1., Some(agent2)); link.push_vehicle(vehicle1, 0); link.push_vehicle(vehicle2, 0); @@ -341,7 +341,7 @@ mod tests { while n < 10 { let agent = create_agent(n as u64, vec![]); - let vehicle = Vehicle::new(n as u64, 0, Some(agent)); + let vehicle = Vehicle::new(n as u64, 0, 10., 1., Some(agent)); link.push_vehicle(vehicle, n); n += 1; } @@ -372,7 +372,7 @@ mod tests { while n < 10 { let agent = create_agent(n as u64, vec![]); - let vehicle = Vehicle::new(n as u64, 0, Some(agent)); + let vehicle = Vehicle::new(n as u64, 0, 10., 1., Some(agent)); link.push_vehicle(vehicle, n); n += 1; } @@ -400,9 +400,9 @@ mod tests { ); let agent1 = create_agent(1, vec![]); - let vehicle1 = Vehicle::new(1, 0, Some(agent1)); + let vehicle1 = Vehicle::new(1, 0, 10., 1., Some(agent1)); let agent2 = create_agent(2, vec![]); - let vehicle2 = Vehicle::new(2, 0, Some(agent2)); + let vehicle2 = Vehicle::new(2, 0, 10., 1., Some(agent2)); link.push_vehicle(vehicle1, 0); link.push_vehicle(vehicle2, 0); diff --git a/src/simulation/network/sim_network.rs b/src/simulation/network/sim_network.rs index ac37e1dc..7928fb2c 100644 --- a/src/simulation/network/sim_network.rs +++ b/src/simulation/network/sim_network.rs @@ -230,7 +230,7 @@ mod tests { let mut sim_network = create_single_node_sim_network(&mut network); let mut publisher = EventsPublisher::new(); let agent = create_agent(1, vec![0]); - let vehicle = Vehicle::new(1, 0, Some(agent)); + let vehicle = Vehicle::new(1, 0, 10., 1., Some(agent)); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { link1.push_vehicle(vehicle, 1); @@ -248,7 +248,7 @@ mod tests { let mut sim_network = create_single_node_sim_network(&mut network); let mut publisher = EventsPublisher::new(); let agent = create_agent(1, vec![0, 1]); - let vehicle = Vehicle::new(1, 0, Some(agent)); + let vehicle = Vehicle::new(1, 0, 10., 1., Some(agent)); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { link1.push_vehicle(vehicle, 1); @@ -271,7 +271,7 @@ mod tests { let sim_network = sim_nets.get_mut(0).unwrap(); let mut publisher = EventsPublisher::new(); let agent = create_agent(1, vec![0, 1]); - let vehicle = Vehicle::new(1, 0, Some(agent)); + let vehicle = Vehicle::new(1, 0, 10., 1., Some(agent)); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { link1.push_vehicle(vehicle, 1); @@ -290,9 +290,9 @@ mod tests { let mut sim_network = create_single_node_sim_network(&mut network); let mut publisher = EventsPublisher::new(); let agent1 = create_agent(1, vec![0]); - let vehicle1 = Vehicle::new(1, 0, Some(agent1)); + let vehicle1 = Vehicle::new(1, 0, 10., 1., Some(agent1)); let agent2 = create_agent(2, vec![0]); - let vehicle2 = Vehicle::new(2, 0, Some(agent2)); + let vehicle2 = Vehicle::new(2, 0, 10., 1., Some(agent2)); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { link1.push_vehicle(vehicle1, 1); @@ -323,9 +323,9 @@ mod tests { let mut sim_network = create_single_node_sim_network(&mut network); let mut publisher = EventsPublisher::new(); let agent1 = create_agent(1, vec![0, 1]); - let vehicle1 = Vehicle::new(1, 0, Some(agent1)); + let vehicle1 = Vehicle::new(1, 0, 10., 1., Some(agent1)); let agent2 = create_agent(2, vec![0, 1]); - let vehicle2 = Vehicle::new(2, 0, Some(agent2)); + let vehicle2 = Vehicle::new(2, 0, 10., 1., Some(agent2)); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { link1.push_vehicle(vehicle1, 1); diff --git a/src/simulation/population/population.rs b/src/simulation/population/population.rs index 92f8dfe1..1ba3c89b 100644 --- a/src/simulation/population/population.rs +++ b/src/simulation/population/population.rs @@ -111,8 +111,8 @@ mod tests { #[test] fn from_io_1_plan() { - let net = Network::from_file("./assets/equil/equil-network.xml", 1); let mut garage = Garage::new(); + let net = Network::from_file("./assets/equil/equil-network.xml", 1, &mut garage); let pop = Population::from_file("./assets/equil/equil-1-plan.xml", &net, &mut garage, 0); assert_eq!(1, pop.agents.len()); @@ -162,8 +162,8 @@ mod tests { #[test] fn from_io() { - let net = Network::from_file("./assets/equil/equil-network.xml", 2); let mut garage = Garage::new(); + let net = Network::from_file("./assets/equil/equil-network.xml", 2, &mut garage); let pop1 = Population::from_file("./assets/equil/equil-plans.xml.gz", &net, &mut garage, 0); let pop2 = Population::from_file("./assets/equil/equil-plans.xml.gz", &net, &mut garage, 1); diff --git a/src/simulation/simulation.rs b/src/simulation/simulation.rs index 4bf3d466..cf3838f5 100644 --- a/src/simulation/simulation.rs +++ b/src/simulation/simulation.rs @@ -108,10 +108,10 @@ impl<'sim> Simulation<'sim> { ); if Simulation::is_local_route(route, &self.message_broker) { - let veh = Vehicle::new(agent.id, 1, Some(agent)); + let veh = Vehicle::new(agent.id, 1, 10., 1., Some(agent)); self.teleportation_q.add(veh, now); } else { - let veh = Vehicle::new(agent.id, 1, Some(agent)); + let veh = Vehicle::new(agent.id, 1, 10., 1., Some(agent)); self.message_broker.add_veh(veh, now); } } @@ -127,7 +127,7 @@ impl<'sim> Simulation<'sim> { &Event::new_person_enters_veh(agent_id, route.vehicle_id), ); - let veh = Vehicle::new(route.vehicle_id, 0, Some(agent)); + let veh = Vehicle::new(route.vehicle_id, 0, 10., 1., Some(agent)); self.veh_onto_network(veh, true, now); } } From b843e095c18d7f8696ebb314811763f289724249 Mon Sep 17 00:00:00 2001 From: janekdererste Date: Thu, 14 Sep 2023 15:43:53 +0200 Subject: [PATCH 07/20] add speed from vehicle again --- src/simulation/network/link.rs | 27 +-------------------------- src/simulation/network/sim_network.rs | 6 +++--- 2 files changed, 4 insertions(+), 29 deletions(-) diff --git a/src/simulation/network/link.rs b/src/simulation/network/link.rs index 004168f8..1c4ce519 100644 --- a/src/simulation/network/link.rs +++ b/src/simulation/network/link.rs @@ -161,7 +161,7 @@ impl LocalLink { } pub fn push_vehicle(&mut self, vehicle: Vehicle, now: u32) { - let speed = self.get_speed_for_vehicle(&vehicle); + let speed = self.freespeed.min(vehicle.max_v); let duration = (self.length / speed) as u32; let earliest_exit_time = now + duration; self.q.push_back(VehicleQEntry { @@ -187,31 +187,6 @@ impl LocalLink { popped_veh } - - fn get_speed_for_vehicle(&self, vehicle: &Vehicle) -> f32 { - panic!("not implemented"); - /* - if vehicle_definitions.is_none() { - return self.freespeed; - } - - let vehicle_max_speed = vehicle_definitions - .as_ref() - .unwrap() - .get_max_speed_for_mode(0); //todo fix mode - - if vehicle_max_speed.is_none() { - warn!( - "There is no max speed given for vehicle type {:?}. Using freespeed of links.", - 0 // todo fix mode - ); - return self.freespeed; - } - - self.freespeed.min(vehicle_max_speed.unwrap()) - - */ - } } #[derive(Debug, Clone)] diff --git a/src/simulation/network/sim_network.rs b/src/simulation/network/sim_network.rs index 7928fb2c..95c65a8c 100644 --- a/src/simulation/network/sim_network.rs +++ b/src/simulation/network/sim_network.rs @@ -59,14 +59,14 @@ impl<'n> SimNetworkPartition<'n> { let from_part = all_nodes.get(link.from.internal).unwrap().partition; let to_part = all_nodes.get(link.to.internal).unwrap().partition; - return if from_part == to_part { + if from_part == to_part { SimLink::Local(LocalLink::from_link(link, 1.0)) } else if to_part == partition { let local_link = LocalLink::from_link(&link, 1.0); SimLink::In(SplitInLink::new(from_part, local_link)) } else { SimLink::Out(SplitOutLink::new(link.id.clone(), to_part)) - }; + } } pub fn new( @@ -105,7 +105,7 @@ impl<'n> SimNetworkPartition<'n> { for node_id in &self.nodes { Self::move_node( node_id, - &self.global_network, + self.global_network, &mut self.links, &mut exited_vehicles, events, From e62fb51efcbc15ead22afdb7007052de218458cf Mon Sep 17 00:00:00 2001 From: janekdererste Date: Mon, 11 Sep 2023 11:50:48 +0200 Subject: [PATCH 08/20] Remove vehicle definitions from code as first step towards vehicle types --- assets/vehicles/vehicles_v2.xml | 88 ++++++++ src/simulation/controller.rs | 9 - src/simulation/io/attributes.rs | 15 ++ src/simulation/io/mod.rs | 3 +- src/simulation/io/network.rs | 15 +- src/simulation/io/vehicle_definitions.rs | 244 ----------------------- src/simulation/io/vehicles.rs | 69 +++++++ src/simulation/messaging/messages.proto | 11 +- src/simulation/messaging/messages.rs | 17 +- src/simulation/mod.rs | 1 + src/simulation/network/global_network.rs | 3 +- src/simulation/network/link.rs | 182 +++-------------- src/simulation/network/sim_network.rs | 61 +++--- src/simulation/simulation.rs | 58 ++---- src/simulation/vehicles/Garage.rs | 32 +++ src/simulation/vehicles/VehicleType.rs | 26 +++ src/simulation/vehicles/mod.rs | 2 + 17 files changed, 316 insertions(+), 520 deletions(-) create mode 100644 assets/vehicles/vehicles_v2.xml create mode 100644 src/simulation/io/attributes.rs delete mode 100644 src/simulation/io/vehicle_definitions.rs create mode 100644 src/simulation/io/vehicles.rs create mode 100644 src/simulation/vehicles/Garage.rs create mode 100644 src/simulation/vehicles/VehicleType.rs create mode 100644 src/simulation/vehicles/mod.rs diff --git a/assets/vehicles/vehicles_v2.xml b/assets/vehicles/vehicles_v2.xml new file mode 100644 index 00000000..0cc307fd --- /dev/null +++ b/assets/vehicles/vehicles_v2.xml @@ -0,0 +1,88 @@ + + + + + + + abc + 1.3 + 23.23 + parallel + 42.42 + + abc + + + + + + + + 0.23 + EURO-5 + < 1,4L + petrol + pass. car + + + + + 0.06 + + + + + + + + + + def + 2 + + + + + + + + + + + This is a small truck + + + + + + + + EURO-6 + diesel + + + + + 0.15 + 0.05 + + + + + + + + + + 1.234 + + + + + firstVehicle + + + + + diff --git a/src/simulation/controller.rs b/src/simulation/controller.rs index ee94c841..a7755a09 100644 --- a/src/simulation/controller.rs +++ b/src/simulation/controller.rs @@ -9,7 +9,6 @@ use tracing::info; use crate::simulation::config::Config; use crate::simulation::io::proto_events::ProtoEventsWriter; -use crate::simulation::io::vehicle_definitions::{IOVehicleDefinitions, VehicleDefinitions}; use crate::simulation::messaging::events::EventsPublisher; use crate::simulation::messaging::message_broker::MpiMessageBroker; use crate::simulation::network::sim_network::SimNetworkPartition; @@ -55,20 +54,12 @@ pub fn run(world: SystemCommunicator, config: Config) { //events.add_subscriber(travel_time_collector); //events.add_subscriber(Box::new(EventsLogger {})); - let mut vehicle_definitions: Option = None; - if let Some(vehicle_definitions_file_path) = &config.vehicle_definitions_file { - let io_vehicle_definitions = - IOVehicleDefinitions::from_file(vehicle_definitions_file_path.as_ref()); - vehicle_definitions = Some(VehicleDefinitions::from_io(io_vehicle_definitions)); - } - let mut simulation = Simulation::new( &config, network_partition, population, message_broker, events, - vehicle_definitions, ); let start = Instant::now(); diff --git a/src/simulation/io/attributes.rs b/src/simulation/io/attributes.rs new file mode 100644 index 00000000..2f96bdf9 --- /dev/null +++ b/src/simulation/io/attributes.rs @@ -0,0 +1,15 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] +pub struct Attr { + pub name: String, + pub class: String, + #[serde(rename = "$value")] + pub value: String, +} + +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] +pub struct Attrs { + #[serde(rename = "attribute", default)] + pub attributes: Vec, +} diff --git a/src/simulation/io/mod.rs b/src/simulation/io/mod.rs index c94e4b15..b335496f 100644 --- a/src/simulation/io/mod.rs +++ b/src/simulation/io/mod.rs @@ -1,9 +1,10 @@ +pub mod attributes; pub mod matsim_id; pub mod network; pub mod non_blocking_io; pub mod population; pub mod proto_events; -pub mod vehicle_definitions; +pub mod vehicles; mod worker; pub mod xml_events; pub mod xml_reader; diff --git a/src/simulation/io/network.rs b/src/simulation/io/network.rs index e3679302..907f6bce 100644 --- a/src/simulation/io/network.rs +++ b/src/simulation/io/network.rs @@ -9,23 +9,10 @@ use quick_xml::se::to_writer; use serde::{Deserialize, Serialize}; use tracing::info; +use crate::simulation::io::attributes::Attrs; use crate::simulation::io::matsim_id::MatsimId; use crate::simulation::io::xml_reader; -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -pub struct Attr { - pub name: String, - pub class: String, - #[serde(rename = "$value")] - pub value: String, -} - -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -pub struct Attrs { - #[serde(rename = "attribute", default)] - pub attributes: Vec, -} - #[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] pub struct IONode { pub id: String, diff --git a/src/simulation/io/vehicle_definitions.rs b/src/simulation/io/vehicle_definitions.rs deleted file mode 100644 index 69a990cd..00000000 --- a/src/simulation/io/vehicle_definitions.rs +++ /dev/null @@ -1,244 +0,0 @@ -use crate::simulation::io::xml_reader; -use log::debug; -use serde::{Deserialize, Serialize}; - -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename = "vehicleDefinitions")] -pub struct IOVehicleDefinitions { - #[serde(rename = "vehicleType", default)] - vehicle_types: Vec, -} - -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -pub struct IOVehicleType { - pub id: String, - #[serde(rename = "$value")] - pub attributes: Vec, -} - -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename_all = "camelCase")] -pub enum IOVehicleAttribute { - Length(IOLength), - Width(IOWidth), - MaximumVelocity(IOMaximumVelocity), - AccessTime(IOAccessTime), - EgressTime(IOEgressTime), - DoorOperation(IODoorOperation), - PassengerCarEquivalents(IOPassengerCarEquivalents), - NetworkMode(IONetworkMode), - FlowEfficiencyFactor(IOFlowEfficiencyFactor), -} - -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename_all = "camelCase")] -pub struct IOLength { - meter: f32, -} - -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename_all = "camelCase")] -pub struct IOWidth { - meter: f32, -} - -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename_all = "camelCase")] -pub struct IOMaximumVelocity { - meter_per_second: f32, -} - -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename_all = "camelCase")] -pub struct IOAccessTime { - seconds_per_person: f32, -} - -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename_all = "camelCase")] -pub struct IOEgressTime { - seconds_per_person: f32, -} - -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename_all = "camelCase")] -pub struct IODoorOperation { - mode: String, -} - -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename_all = "camelCase")] -pub struct IOPassengerCarEquivalents { - pce: f32, -} - -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename_all = "camelCase")] -pub struct IONetworkMode { - network_mode: String, -} - -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename_all = "camelCase")] -pub struct IOFlowEfficiencyFactor { - network_mode: f32, -} - -#[derive(Debug, PartialEq, Clone)] -pub struct VehicleType { - pub id: String, - pub maximum_velocity: Option, - pub network_mode: String, -} - -#[derive(Debug, PartialEq, Clone)] -pub struct VehicleDefinitions { - pub vehicle_types: Vec, -} - -impl IOVehicleDefinitions { - pub fn from_file(file_path: &str) -> IOVehicleDefinitions { - xml_reader::read::(file_path) - } -} - -impl VehicleDefinitions { - pub fn new() -> VehicleDefinitions { - VehicleDefinitions { - vehicle_types: vec![], - } - } - - pub fn add_vehicle_type( - mut self, - id: String, - maximum_velocity: Option, - network_mode: String, - ) -> VehicleDefinitions { - self.vehicle_types.push(VehicleType { - id, - maximum_velocity, - network_mode, - }); - self - } - - pub fn from_io(io: IOVehicleDefinitions) -> VehicleDefinitions { - let vehicle_types = io - .vehicle_types - .into_iter() - .map(|t| VehicleDefinitions::convert_io_vehicle_type(t)) - .collect(); - VehicleDefinitions { vehicle_types } - } - - fn convert_io_vehicle_type(io: IOVehicleType) -> VehicleType { - VehicleType { - id: io.id.clone(), - maximum_velocity: Self::extract_maximum_velocity(&io), - network_mode: Self::extract_network_mode(&io).unwrap_or_else(|| { - debug!("There was no specific network mode for vehicle type {}. Using id as network mode.", io.id); - io.id - }), - } - } - - fn extract_maximum_velocity(io: &IOVehicleType) -> Option { - io.attributes - .iter() - .filter_map(|a| match a { - IOVehicleAttribute::MaximumVelocity(v) => Some(v.meter_per_second), - _ => None, - }) - .collect::>() - .get(0) - .cloned() - } - - fn extract_network_mode(io: &IOVehicleType) -> Option { - io.attributes - .iter() - .filter_map(|a| match a { - IOVehicleAttribute::NetworkMode(m) => Some(m.network_mode.clone()), - _ => None, - }) - .collect::>() - .get(0) - .cloned() - } - - pub fn get_max_speed_for_mode(&self, mode: &str) -> Option { - let mode_vehicle_type = self - .vehicle_types - .iter() - .filter(|&v| v.network_mode.eq(mode)) - .collect::>(); - - if mode_vehicle_type.len() == 0 { - panic!("There is no vehicle type definition for mode {:?} ", mode) - } else if mode_vehicle_type.len() > 1 { - panic!( - "There are multiple vehicle type definitions for mode {:?} ", - mode - ) - } - - mode_vehicle_type.get(0).unwrap().maximum_velocity - } -} - -#[cfg(test)] -mod test { - use crate::simulation::io::vehicle_definitions::{ - IOVehicleDefinitions, VehicleDefinitions, VehicleType, - }; - use quick_xml::de::from_str; - - #[test] - fn test() { - let io_definitions = from_str::( - r#" - - - - - - - - - - - - - - - - - - - - - - "# - ).unwrap(); - - let vehicle_definitions = VehicleDefinitions::from_io(io_definitions); - assert_eq!( - vehicle_definitions, - VehicleDefinitions { - vehicle_types: vec![ - VehicleType { - id: "car".to_string(), - maximum_velocity: Some(16.67), - network_mode: "car".to_string() - }, - VehicleType { - id: "bicycle".to_string(), - maximum_velocity: Some(4.17), - network_mode: "bicycle".to_string() - } - ], - } - ); - } -} diff --git a/src/simulation/io/vehicles.rs b/src/simulation/io/vehicles.rs new file mode 100644 index 00000000..1d091bda --- /dev/null +++ b/src/simulation/io/vehicles.rs @@ -0,0 +1,69 @@ +use serde::{Deserialize, Serialize}; + +use crate::simulation::io::attributes::Attrs; +use crate::simulation::io::xml_reader; + +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] +#[serde(rename = "vehicleDefinitions")] +pub struct IOVehicleDefinitions { + #[serde(rename = "vehicleType")] + veh_types: Vec, +} + +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] +#[serde(rename = "vehicleDefinitions")] +pub struct IOVehicles { + #[serde(rename = "vehicleType", default)] + vehicle_types: Vec, +} + +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] +pub struct IOVehicleType { + pub id: String, + pub descr: String, + pub capacity: IOCapacity, + pub length: f32, + pub width: f32, + pub maximum_velocity: f32, + pub engine_information: IOEngineInformation, + pub cost_information: IOCostInformation, + pub passenger_car_equivalents: f32, + pub network_mode: String, + pub flow_efficiency_factor: f32, + pub attributes: Option, +} + +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] +#[serde(rename = "capacity")] +pub struct IOCapacity { + // leave emtpy for now +} + +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] +#[serde(rename = "capacity")] +pub struct IOEngineInformation { + // leave empty for now. +} + +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] +#[serde(rename = "capacity")] +pub struct IOCostInformation { + // leave empty for now. +} + +impl IOVehicleDefinitions { + pub fn from_file(file: &str) -> Self { + xml_reader::read(file) + } +} + +#[cfg(test)] +mod test { + use crate::simulation::io::vehicles::IOVehicleDefinitions; + + #[test] + fn test_from_file() { + let io_vehicles = IOVehicleDefinitions::from_file("./assets/vehicles/vehicles_v2.xml"); + println!("{io_vehicles:#?}") + } +} diff --git a/src/simulation/messaging/messages.proto b/src/simulation/messaging/messages.proto index 6369397f..61eef18e 100644 --- a/src/simulation/messaging/messages.proto +++ b/src/simulation/messaging/messages.proto @@ -24,17 +24,12 @@ message VehicleMessage { message Vehicle { uint64 id = 1; uint32 curr_route_elem = 2; - VehicleType type = 3; + uint64 type = 3; Agent agent = 4; - string mode = 5; + uint64 mode = 5; // this must be extended once more people use one vehicle } -enum VehicleType { - TELEPORTED = 0; - NETWORK = 1; -} - message Agent { uint64 id = 1; uint32 curr_plan_elem = 2; @@ -57,7 +52,7 @@ message Activity { } message Leg { - string mode = 1; + uint64 mode = 1; optional uint32 dep_time = 2; optional uint32 trav_time = 3; oneof route { diff --git a/src/simulation/messaging/messages.rs b/src/simulation/messaging/messages.rs index 609564f7..1d1d8bfc 100644 --- a/src/simulation/messaging/messages.rs +++ b/src/simulation/messaging/messages.rs @@ -11,7 +11,7 @@ use crate::simulation::io::population::{ use crate::simulation::messaging::messages::proto::leg::Route; use crate::simulation::messaging::messages::proto::{ Activity, Agent, ExperimentalMessage, GenericRoute, Leg, NetworkRoute, Plan, - TravelTimesMessage, Vehicle, VehicleMessage, VehicleType, + TravelTimesMessage, Vehicle, VehicleMessage, }; use crate::simulation::network::global_network::Network; use crate::simulation::population::population::Population; @@ -111,13 +111,14 @@ impl Ord for VehicleMessage { } impl Vehicle { - pub fn new(id: u64, veh_type: VehicleType, mode: String, agent: Agent) -> Vehicle { + // todo, fix type and mode + pub fn new(id: u64, veh_type: u64, mode: u64, agent: Agent) -> Vehicle { Vehicle { id, agent: Some(agent), curr_route_elem: 0, - r#type: veh_type as i32, - mode, + r#type: 0, + mode: 0, } } @@ -153,10 +154,6 @@ impl Vehicle { Route::NetworkRoute(route) => self.curr_route_elem + 1 >= route.route.len() as u32, } } - - pub fn mode(&self) -> &str { - self.mode.as_str() - } } impl EndTime for Vehicle { @@ -486,7 +483,7 @@ impl Leg { let route = Route::from_io(&io_leg.route, net, pop); Self { route: Some(route), - mode: io_leg.mode.clone(), + mode: 0, //todo fix mode trav_time: parse_time_opt(&io_leg.trav_time), dep_time: parse_time_opt(&io_leg.dep_time), } @@ -495,7 +492,7 @@ impl Leg { pub fn new(route: Route, mode: &str, trav_time: Option, dep_time: Option) -> Self { Self { route: Some(route), - mode: String::from(mode), + mode: 0, //todo fix mode trav_time, dep_time, } diff --git a/src/simulation/mod.rs b/src/simulation/mod.rs index c07885e1..e002aef1 100644 --- a/src/simulation/mod.rs +++ b/src/simulation/mod.rs @@ -9,3 +9,4 @@ pub mod performance_profiling; pub mod population; mod simulation; pub mod time_queue; +mod vehicles; diff --git a/src/simulation/network/global_network.rs b/src/simulation/network/global_network.rs index cb94e919..afc5223b 100644 --- a/src/simulation/network/global_network.rs +++ b/src/simulation/network/global_network.rs @@ -1,8 +1,9 @@ use std::{collections::HashSet, path::Path}; +use crate::simulation::io::attributes::{Attr, Attrs}; use crate::simulation::{ id::{Id, IdStore}, - io::network::{Attr, Attrs, IOLink, IONetwork, IONode}, + io::network::{IOLink, IONetwork, IONode}, }; use super::metis_partitioning; diff --git a/src/simulation/network/link.rs b/src/simulation/network/link.rs index 01f9d863..1bec72ce 100644 --- a/src/simulation/network/link.rs +++ b/src/simulation/network/link.rs @@ -1,11 +1,8 @@ use std::collections::VecDeque; use std::fmt::Debug; -use tracing::warn; - use crate::simulation::id::{Id, IdImpl}; use crate::simulation::io::network::IOLink; -use crate::simulation::io::vehicle_definitions::VehicleDefinitions; use crate::simulation::messaging::messages::proto::Vehicle; use crate::simulation::network::flow_cap::Flowcap; use crate::simulation::network::global_network::Node; @@ -163,13 +160,8 @@ impl LocalLink { } } - pub fn push_vehicle( - &mut self, - vehicle: Vehicle, - now: u32, - vehicle_definitions: Option<&VehicleDefinitions>, - ) { - let speed = self.get_speed_for_vehicle(&vehicle, vehicle_definitions); + pub fn push_vehicle(&mut self, vehicle: Vehicle, now: u32) { + let speed = self.get_speed_for_vehicle(&vehicle); let duration = (self.length / speed) as u32; let earliest_exit_time = now + duration; self.q.push_back(VehicleQEntry { @@ -196,11 +188,9 @@ impl LocalLink { popped_veh } - fn get_speed_for_vehicle( - &self, - vehicle: &Vehicle, - vehicle_definitions: Option<&VehicleDefinitions>, - ) -> f32 { + fn get_speed_for_vehicle(&self, vehicle: &Vehicle) -> f32 { + panic!("not implemented"); + /* if vehicle_definitions.is_none() { return self.freespeed; } @@ -208,17 +198,19 @@ impl LocalLink { let vehicle_max_speed = vehicle_definitions .as_ref() .unwrap() - .get_max_speed_for_mode(vehicle.mode()); + .get_max_speed_for_mode(0); //todo fix mode if vehicle_max_speed.is_none() { warn!( "There is no max speed given for vehicle type {:?}. Using freespeed of links.", - vehicle.mode() + 0 // todo fix mode ); return self.freespeed; } self.freespeed.min(vehicle_max_speed.unwrap()) + + */ } } @@ -268,10 +260,9 @@ impl SplitInLink { #[cfg(test)] mod tests { use crate::simulation::id::IdImpl; - use crate::simulation::io::vehicle_definitions::VehicleDefinitions; use crate::simulation::messaging::messages::proto::leg::Route; use crate::simulation::messaging::messages::proto::{Activity, NetworkRoute}; - use crate::simulation::messaging::messages::proto::{Agent, Leg, Plan, Vehicle, VehicleType}; + use crate::simulation::messaging::messages::proto::{Agent, Leg, Plan, Vehicle}; use crate::simulation::network::link::LocalLink; #[test] @@ -288,9 +279,9 @@ mod tests { IdImpl::new_internal(0), ); let agent = create_agent(1, vec![]); - let vehicle = Vehicle::new(veh_id, VehicleType::Network, String::from("car"), agent); + let vehicle = Vehicle::new(veh_id, 0, 0, agent); - link.push_vehicle(vehicle, 0, None); + link.push_vehicle(vehicle, 0); // this should put the vehicle into the queue and update the exit time correctly let pushed_vehicle = link.q.front().unwrap(); @@ -314,12 +305,12 @@ mod tests { ); let agent1 = create_agent(1, vec![]); - let vehicle1 = Vehicle::new(id1, VehicleType::Network, String::from("car"), agent1); + let vehicle1 = Vehicle::new(id1, 0, 0, agent1); let agent2 = create_agent(1, vec![]); - let vehicle2 = Vehicle::new(id2, VehicleType::Network, String::from("car"), agent2); + let vehicle2 = Vehicle::new(id2, 0, 0, agent2); - link.push_vehicle(vehicle1, 0, None); - link.push_vehicle(vehicle2, 0, None); + link.push_vehicle(vehicle1, 0); + link.push_vehicle(vehicle2, 0); // make sure that vehicles are added ad the end of the queue assert_eq!(2, link.q.len()); @@ -350,8 +341,8 @@ mod tests { while n < 10 { let agent = create_agent(n as u64, vec![]); - let vehicle = Vehicle::new(n as u64, VehicleType::Network, String::from("car"), agent); - link.push_vehicle(vehicle, n, None); + let vehicle = Vehicle::new(n as u64, 0, 0, agent); + link.push_vehicle(vehicle, n); n += 1; } @@ -381,8 +372,8 @@ mod tests { while n < 10 { let agent = create_agent(n as u64, vec![]); - let vehicle = Vehicle::new(n as u64, VehicleType::Network, String::from("car"), agent); - link.push_vehicle(vehicle, n, None); + let vehicle = Vehicle::new(n as u64, 0, 0, agent); + link.push_vehicle(vehicle, n); n += 1; } @@ -409,11 +400,11 @@ mod tests { ); let agent1 = create_agent(1, vec![]); - let vehicle1 = Vehicle::new(1, VehicleType::Network, String::from("car"), agent1); + let vehicle1 = Vehicle::new(1, 0, 0, agent1); let agent2 = create_agent(2, vec![]); - let vehicle2 = Vehicle::new(2, VehicleType::Network, String::from("car"), agent2); - link.push_vehicle(vehicle1, 0, None); - link.push_vehicle(vehicle2, 0, None); + let vehicle2 = Vehicle::new(2, 0, 0, agent2); + link.push_vehicle(vehicle1, 0); + link.push_vehicle(vehicle2, 0); let popped = link.pop_front(10); assert_eq!(1, popped.len()); @@ -427,131 +418,6 @@ mod tests { assert_eq!(1, popped_3.len()); } - #[test] - fn local_link_with_vehicle_definitions() { - let veh_id_car = 42; - let veh_id_buggy = 43; - let veh_id_bike = 44; - let mut link = LocalLink::new( - IdImpl::new_internal(1), - 1., - 10., - 100., - vec![], - 1., - IdImpl::new_internal(0), - IdImpl::new_internal(0), - ); - - let vehicle_definitions = create_three_vehicle_definitions(); - - let agent1 = create_agent(1, vec![]); - let car = Vehicle::new( - veh_id_car, - VehicleType::Network, - String::from("car"), - agent1, - ); - let agent2 = create_agent(2, vec![]); - let buggy = Vehicle::new( - veh_id_buggy, - VehicleType::Network, - String::from("buggy"), - agent2, - ); - let agent3 = create_agent(3, vec![]); - let bike = Vehicle::new( - veh_id_bike, - VehicleType::Network, - String::from("bike"), - agent3, - ); - - link.push_vehicle(car, 0, Some(&vehicle_definitions)); - link.push_vehicle(buggy, 0, Some(&vehicle_definitions)); - link.push_vehicle(bike, 0, Some(&vehicle_definitions)); - - // this should put the vehicle into the queue and update the exit time correctly - let pushed_vehicle_car = link.q.get(0).unwrap(); - assert_eq!(veh_id_car, pushed_vehicle_car.vehicle.id); - assert_eq!(10, pushed_vehicle_car.earliest_exit_time); - - let pushed_vehicle_buggy = link.q.get(1).unwrap(); - assert_eq!(veh_id_buggy, pushed_vehicle_buggy.vehicle.id); - assert_eq!(10, pushed_vehicle_buggy.earliest_exit_time); - - let pushed_vehicle_bike = link.q.get(2).unwrap(); - assert_eq!(veh_id_bike, pushed_vehicle_bike.vehicle.id); - assert_eq!(20, pushed_vehicle_bike.earliest_exit_time); - } - - #[test] - fn local_link_pop_with_vehicle_definitions() { - let veh_id_car = 42; - let veh_id_buggy = 43; - let veh_id_bike = 44; - let mut link = LocalLink::new( - IdImpl::new_internal(1), - 3600., - 10., - 100., - vec![], - 1., - IdImpl::new_internal(0), - IdImpl::new_internal(0), - ); - - let vehicle_definitions = create_three_vehicle_definitions(); - - let agent1 = create_agent(1, vec![]); - let car = Vehicle::new( - veh_id_car, - VehicleType::Network, - String::from("car"), - agent1, - ); - let agent2 = create_agent(1, vec![]); - let buggy = Vehicle::new( - veh_id_buggy, - VehicleType::Network, - String::from("buggy"), - agent2, - ); - let agent3 = create_agent(1, vec![]); - let bike = Vehicle::new( - veh_id_bike, - VehicleType::Network, - String::from("bike"), - agent3, - ); - - link.push_vehicle(bike, 0, Some(&vehicle_definitions)); - link.push_vehicle(buggy, 0, Some(&vehicle_definitions)); - link.push_vehicle(car, 0, Some(&vehicle_definitions)); - - let popped = link.pop_front(10); - assert_eq!(0, popped.len()); - - let popped_2 = link.pop_front(20); - assert_eq!(1, popped_2.len()); - assert!(popped_2.first().unwrap().mode.eq("bike")); - - let popped_3 = link.pop_front(21); - assert_eq!(1, popped_3.len()); - assert!(popped_3.first().unwrap().mode.eq("buggy")); - - let popped_4 = link.pop_front(22); - assert_eq!(1, popped_4.len()); - assert!(popped_4.first().unwrap().mode.eq("car")); - } - - fn create_three_vehicle_definitions() -> VehicleDefinitions { - VehicleDefinitions::new() - .add_vehicle_type("car".to_string(), Some(20.), String::from("car")) - .add_vehicle_type("buggy".to_string(), Some(10.), String::from("buggy")) - .add_vehicle_type("bike".to_string(), Some(5.), String::from("bike")) - } - fn create_agent(id: u64, route: Vec) -> Agent { let route = Route::NetworkRoute(NetworkRoute::new(id, route)); let leg = Leg::new(route, "car", None, None); diff --git a/src/simulation/network/sim_network.rs b/src/simulation/network/sim_network.rs index b236954e..d19c8403 100644 --- a/src/simulation/network/sim_network.rs +++ b/src/simulation/network/sim_network.rs @@ -2,7 +2,6 @@ use std::collections::{HashMap, HashSet}; use crate::simulation::{ id::Id, - io::vehicle_definitions::VehicleDefinitions, messaging::{ events::{proto::Event, EventsPublisher}, messages::proto::Vehicle, @@ -100,12 +99,7 @@ impl<'n> SimNetworkPartition<'n> { distinct_partitions } - pub fn move_nodes( - &mut self, - events: &mut EventsPublisher, - veh_def: Option<&VehicleDefinitions>, - now: u32, - ) -> Vec { + pub fn move_nodes(&mut self, events: &mut EventsPublisher, now: u32) -> Vec { let mut exited_vehicles = Vec::new(); for node_id in &self.nodes { @@ -115,7 +109,6 @@ impl<'n> SimNetworkPartition<'n> { &mut self.links, &mut exited_vehicles, events, - veh_def, now, ); } @@ -129,7 +122,6 @@ impl<'n> SimNetworkPartition<'n> { links: &mut HashMap, SimLink>, exited_vehicles: &mut Vec, events: &mut EventsPublisher, - veh_def: Option<&VehicleDefinitions>, now: u32, ) { let node = global_network.get_node(node_id); @@ -144,7 +136,7 @@ impl<'n> SimNetworkPartition<'n> { vehicle.advance_route_index(); exited_vehicles.push(ExitReason::FinishRoute(vehicle)); } else if let Some(exit_reason) = - Self::move_vehicle(vehicle, veh_def, global_network, links, events, now) + Self::move_vehicle(vehicle, global_network, links, events, now) { exited_vehicles.push(exit_reason); } @@ -154,7 +146,6 @@ impl<'n> SimNetworkPartition<'n> { fn move_vehicle( mut vehicle: Vehicle, - veh_def: Option<&VehicleDefinitions>, global_network: &Network, links: &mut HashMap, SimLink>, events: &mut EventsPublisher, @@ -174,7 +165,7 @@ impl<'n> SimNetworkPartition<'n> { now, &Event::new_link_enter(l.id.internal as u64, vehicle.id), ); - l.push_vehicle(vehicle, now, veh_def); + l.push_vehicle(vehicle, now); None } SimLink::Out(_) => Some(ExitReason::ReachedBoundary(vehicle)), @@ -190,9 +181,7 @@ mod tests { use crate::simulation::{ messaging::{ events::EventsPublisher, - messages::proto::{ - leg::Route, Activity, Agent, Leg, NetworkRoute, Plan, Vehicle, VehicleType, - }, + messages::proto::{leg::Route, Activity, Agent, Leg, NetworkRoute, Plan, Vehicle}, }, network::{ global_network::{Link, Network, Node}, @@ -243,13 +232,13 @@ mod tests { let mut sim_network = create_single_node_sim_network(&mut network); let mut publisher = EventsPublisher::new(); let agent = create_agent(1, vec![0]); - let vehicle = Vehicle::new(1, VehicleType::Network, String::from("car"), agent); + let vehicle = Vehicle::new(1, 0, 0, agent); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { - link1.push_vehicle(vehicle, 1, None); + link1.push_vehicle(vehicle, 1); } - let exits = sim_network.move_nodes(&mut publisher, None, 11); + let exits = sim_network.move_nodes(&mut publisher, 11); assert_eq!(1, exits.len()); assert!(matches!(exits.get(0).unwrap(), ExitReason::FinishRoute(_))); @@ -261,13 +250,13 @@ mod tests { let mut sim_network = create_single_node_sim_network(&mut network); let mut publisher = EventsPublisher::new(); let agent = create_agent(1, vec![0, 1]); - let vehicle = Vehicle::new(1, VehicleType::Network, String::from("car"), agent); + let vehicle = Vehicle::new(1, 0, 0, agent); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { - link1.push_vehicle(vehicle, 1, None); + link1.push_vehicle(vehicle, 1); } - let exits = sim_network.move_nodes(&mut publisher, None, 11); + let exits = sim_network.move_nodes(&mut publisher, 11); assert_eq!(0, exits.len()); let out_id = sim_network.global_network.link_ids.get(1); @@ -284,13 +273,13 @@ mod tests { let sim_network = sim_nets.get_mut(0).unwrap(); let mut publisher = EventsPublisher::new(); let agent = create_agent(1, vec![0, 1]); - let vehicle = Vehicle::new(1, VehicleType::Network, String::from("car"), agent); + let vehicle = Vehicle::new(1, 0, 0, agent); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { - link1.push_vehicle(vehicle, 1, None); + link1.push_vehicle(vehicle, 1); } - let exits = sim_network.move_nodes(&mut publisher, None, 11); + let exits = sim_network.move_nodes(&mut publisher, 11); assert_eq!(1, exits.len()); let exit = exits.first().unwrap(); @@ -303,15 +292,15 @@ mod tests { let mut sim_network = create_single_node_sim_network(&mut network); let mut publisher = EventsPublisher::new(); let agent1 = create_agent(1, vec![0]); - let vehicle1 = Vehicle::new(1, VehicleType::Network, String::from("car"), agent1); + let vehicle1 = Vehicle::new(1, 0, 0, agent1); let agent2 = create_agent(2, vec![0]); - let vehicle2 = Vehicle::new(2, VehicleType::Network, String::from("car"), agent2); + let vehicle2 = Vehicle::new(2, 0, 0, agent2); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { - link1.push_vehicle(vehicle1, 1, None); - link1.push_vehicle(vehicle2, 1, None); + link1.push_vehicle(vehicle1, 1); + link1.push_vehicle(vehicle2, 1); } - let exited_vehicles = sim_network.move_nodes(&mut publisher, None, 11); + let exited_vehicles = sim_network.move_nodes(&mut publisher, 11); assert_eq!(1, exited_vehicles.len()); let entry = exited_vehicles.get(0).unwrap(); @@ -320,7 +309,7 @@ mod tests { assert_eq!(1, vehicle.id); } - let exited_vehicles = sim_network.move_nodes(&mut publisher, None, 21); + let exited_vehicles = sim_network.move_nodes(&mut publisher, 21); assert_eq!(1, exited_vehicles.len()); let entry = exited_vehicles.get(0).unwrap(); @@ -336,18 +325,18 @@ mod tests { let mut sim_network = create_single_node_sim_network(&mut network); let mut publisher = EventsPublisher::new(); let agent1 = create_agent(1, vec![0, 1]); - let vehicle1 = Vehicle::new(1, VehicleType::Network, String::from("car"), agent1); + let vehicle1 = Vehicle::new(1, 0, 0, agent1); let agent2 = create_agent(2, vec![0, 1]); - let vehicle2 = Vehicle::new(2, VehicleType::Network, String::from("car"), agent2); + let vehicle2 = Vehicle::new(2, 0, 0, agent2); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { - link1.push_vehicle(vehicle1, 1, None); - link1.push_vehicle(vehicle2, 1, None); + link1.push_vehicle(vehicle1, 1); + link1.push_vehicle(vehicle2, 1); } - let exited_vehicles = sim_network.move_nodes(&mut publisher, None, 11); + let exited_vehicles = sim_network.move_nodes(&mut publisher, 11); assert_eq!(0, exited_vehicles.len()); - let exited_vehicles = sim_network.move_nodes(&mut publisher, None, 12); + let exited_vehicles = sim_network.move_nodes(&mut publisher, 12); assert_eq!(0, exited_vehicles.len()); let out_link = sim_network diff --git a/src/simulation/simulation.rs b/src/simulation/simulation.rs index 6b1e0ffc..179bd3ff 100644 --- a/src/simulation/simulation.rs +++ b/src/simulation/simulation.rs @@ -1,12 +1,11 @@ use tracing::info; use crate::simulation::config::Config; -use crate::simulation::io::vehicle_definitions::VehicleDefinitions; use crate::simulation::messaging::events::proto::Event; use crate::simulation::messaging::events::EventsPublisher; use crate::simulation::messaging::message_broker::{MessageBroker, MpiMessageBroker}; use crate::simulation::messaging::messages::proto::leg::Route; -use crate::simulation::messaging::messages::proto::{Agent, GenericRoute, Vehicle, VehicleType}; +use crate::simulation::messaging::messages::proto::{Agent, GenericRoute, Vehicle}; use crate::simulation::network::link::SimLink; use crate::simulation::network::sim_network::{ExitReason, SimNetworkPartition}; use crate::simulation::time_queue::TimeQueue; @@ -17,7 +16,6 @@ pub struct Simulation<'sim> { network: SimNetworkPartition<'sim>, message_broker: MpiMessageBroker, events: EventsPublisher, - vehicle_definitions: Option, } impl<'sim> Simulation<'sim> { @@ -27,7 +25,6 @@ impl<'sim> Simulation<'sim> { population: crate::simulation::population::population::Population, message_broker: MpiMessageBroker, events: EventsPublisher, - vehicle_definitions: Option, ) -> Self { let mut activity_q = TimeQueue::new(); for agent in population.agents.into_values() { @@ -40,7 +37,6 @@ impl<'sim> Simulation<'sim> { activity_q, message_broker, events, - vehicle_definitions, } } @@ -96,24 +92,14 @@ impl<'sim> Simulation<'sim> { Route::GenericRoute(route) => { self.events.publish_event( now, - &Event::new_departure(agent_id, route.start_link, leg.mode.clone()), + &Event::new_departure(agent_id, route.start_link, String::from("")), ); if Simulation::is_local_route(route, &self.message_broker) { - let veh = Vehicle::new( - agent.id, - VehicleType::Teleported, - leg.mode.clone(), - agent, - ); + let veh = Vehicle::new(agent.id, 1, leg.mode.clone(), agent); self.teleportation_q.add(veh, now); } else { - let veh = Vehicle::new( - agent.id, - VehicleType::Teleported, - leg.mode.clone(), - agent, - ); + let veh = Vehicle::new(agent.id, 1, leg.mode.clone(), agent); self.message_broker.add_veh(veh, now); } } @@ -121,7 +107,7 @@ impl<'sim> Simulation<'sim> { let link_id = route.route.first().unwrap(); self.events.publish_event( now, - &Event::new_departure(agent_id, *link_id, leg.mode.clone()), + &Event::new_departure(agent_id, *link_id, String::from("")), ); self.events.publish_event( @@ -129,12 +115,7 @@ impl<'sim> Simulation<'sim> { &Event::new_person_enters_veh(agent_id, route.vehicle_id), ); - let veh = Vehicle::new( - route.vehicle_id, - VehicleType::Network, - leg.mode.clone(), - agent, - ); + let veh = Vehicle::new(route.vehicle_id, 0, leg.mode.clone(), agent); self.veh_onto_network(veh, true, now); } } @@ -158,12 +139,10 @@ impl<'sim> Simulation<'sim> { ); } match link { - SimLink::Local(link) => { - link.push_vehicle(vehicle, now, self.vehicle_definitions.as_ref()) - } + SimLink::Local(link) => link.push_vehicle(vehicle, now), SimLink::In(in_link) => { let local_link = in_link.local_link_mut(); - local_link.push_vehicle(vehicle, now, self.vehicle_definitions.as_ref()) + local_link.push_vehicle(vehicle, now) } SimLink::Out(_) => { panic!("Vehicles should not start on out links...") @@ -180,7 +159,7 @@ impl<'sim> Simulation<'sim> { if let Route::GenericRoute(route) = &leg.route.as_ref().unwrap() { self.events.publish_event( now, - &Event::new_travelled(agent.id, route.distance, leg.mode.clone()), + &Event::new_travelled(agent.id, route.distance, String::from("")), ); } agent.advance_plan(); @@ -189,16 +168,14 @@ impl<'sim> Simulation<'sim> { } fn move_nodes(&mut self, now: u32) { - let exited_vehicles = - self.network - .move_nodes(&mut self.events, self.vehicle_definitions.as_ref(), now); + let exited_vehicles = self.network.move_nodes(&mut self.events, now); for exit_reason in exited_vehicles { match exit_reason { ExitReason::FinishRoute(vehicle) => { let veh_id = vehicle.id; let mut agent = vehicle.agent.unwrap(); - let leg_mode = agent.curr_leg().mode.clone(); + let leg_mode = 0; // todo fix mode self.events .publish_event(now, &Event::new_person_leaves_veh(agent.id, veh_id)); @@ -206,8 +183,10 @@ impl<'sim> Simulation<'sim> { agent.advance_plan(); let act = agent.curr_act(); - self.events - .publish_event(now, &Event::new_arrival(agent.id, act.link_id, leg_mode)); + self.events.publish_event( + now, + &Event::new_arrival(agent.id, act.link_id, String::from("some mode")), + ); //todo fix mode self.events.publish_event( now, @@ -231,13 +210,14 @@ impl<'sim> Simulation<'sim> { .collect::>(); for vehicle in vehicles { - match vehicle.r#type() { - VehicleType::Teleported => { + match vehicle.r#type { + 1 => { self.teleportation_q.add(vehicle, now); } - VehicleType::Network => { + 0 => { self.veh_onto_network(vehicle, false, now); } + _ => {} } } } diff --git a/src/simulation/vehicles/Garage.rs b/src/simulation/vehicles/Garage.rs new file mode 100644 index 00000000..cb29c75c --- /dev/null +++ b/src/simulation/vehicles/Garage.rs @@ -0,0 +1,32 @@ +use std::collections::HashMap; + +use crate::simulation::id::{Id, IdStore}; +use crate::simulation::messaging::messages::proto::Vehicle; +use crate::simulation::vehicles::VehicleType::VehicleType; + +#[derive(Debug)] +pub struct Garage<'g> { + vehicles: HashMap, Vehicle>, + vehicle_ids: IdStore<'g, Vehicle>, + vehicle_types: Vec, + vehicle_type_ids: IdStore<'g, VehicleType>, + modes: IdStore<'g, String>, +} + +impl<'g> Default for Garage<'g> { + fn default() -> Self { + Garage::new() + } +} + +impl<'g> Garage<'g> { + pub fn new() -> Self { + Garage { + vehicles: Default::default(), + vehicle_ids: Default::default(), + vehicle_types: vec![], + vehicle_type_ids: IdStore::new(), + modes: Default::default(), + } + } +} diff --git a/src/simulation/vehicles/VehicleType.rs b/src/simulation/vehicles/VehicleType.rs new file mode 100644 index 00000000..95f87f19 --- /dev/null +++ b/src/simulation/vehicles/VehicleType.rs @@ -0,0 +1,26 @@ +use crate::simulation::id::Id; + +#[derive(Debug)] +pub struct VehicleType { + id: Id, + length: f32, + width: f32, + max_v: f32, + pce: f32, + fef: f32, + net_mode: Id, +} + +impl VehicleType { + pub fn new(id: Id, net_mode: Id) -> Self { + VehicleType { + id, + length: 0.0, + width: 0.0, + max_v: 0.0, + pce: 0.0, + fef: 0.0, + net_mode, + } + } +} diff --git a/src/simulation/vehicles/mod.rs b/src/simulation/vehicles/mod.rs new file mode 100644 index 00000000..b7ec6cc8 --- /dev/null +++ b/src/simulation/vehicles/mod.rs @@ -0,0 +1,2 @@ +mod Garage; +mod VehicleType; \ No newline at end of file From e9aa74c3432842424d4143d1ecee49f1cdd3e021 Mon Sep 17 00:00:00 2001 From: janekdererste Date: Tue, 12 Sep 2023 18:06:21 +0200 Subject: [PATCH 09/20] replicate crazy vehicles file structure for serde, remove loading of entire xml file in xml_reader.rs..., fix clippy warnings --- assets/vehicles/vehicles_v2.xml | 3 +- src/simulation/io/vehicles.rs | 126 ++++++++++++++++++++++++++++---- src/simulation/io/xml_reader.rs | 18 ++--- 3 files changed, 121 insertions(+), 26 deletions(-) diff --git a/assets/vehicles/vehicles_v2.xml b/assets/vehicles/vehicles_v2.xml index 0cc307fd..78615be3 100644 --- a/assets/vehicles/vehicles_v2.xml +++ b/assets/vehicles/vehicles_v2.xml @@ -1,7 +1,6 @@ - + diff --git a/src/simulation/io/vehicles.rs b/src/simulation/io/vehicles.rs index 1d091bda..006b7632 100644 --- a/src/simulation/io/vehicles.rs +++ b/src/simulation/io/vehicles.rs @@ -18,35 +18,60 @@ pub struct IOVehicles { } #[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] +#[serde(rename_all = "camelCase")] pub struct IOVehicleType { pub id: String, - pub descr: String, - pub capacity: IOCapacity, - pub length: f32, - pub width: f32, - pub maximum_velocity: f32, - pub engine_information: IOEngineInformation, - pub cost_information: IOCostInformation, - pub passenger_car_equivalents: f32, - pub network_mode: String, - pub flow_efficiency_factor: f32, + pub description: Option, + pub capacity: Option, + pub length: Option, + pub width: Option, + pub maximum_velocity: Option, + pub engine_information: Option, + pub cost_information: Option, + pub passenger_car_equivalents: Option, + pub network_mode: Option, + pub flow_efficiency_factor: Option, pub attributes: Option, } #[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename = "capacity")] pub struct IOCapacity { // leave emtpy for now } #[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename = "capacity")] +pub struct IODimension { + meter: f32, +} + +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] +#[serde(rename_all = "camelCase")] +pub struct IOVelocity { + meter_per_second: f32, +} + +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] +pub struct IOPassengerCarEquivalents { + pce: f32, +} + +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] +#[serde(rename_all = "camelCase")] +pub struct IONetworkMode { + network_mode: String, +} + +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] +pub struct IOFowEfficiencyFactor { + factor: f32, +} + +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] pub struct IOEngineInformation { // leave empty for now. } #[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] -#[serde(rename = "capacity")] pub struct IOCostInformation { // leave empty for now. } @@ -59,11 +84,82 @@ impl IOVehicleDefinitions { #[cfg(test)] mod test { + use quick_xml::de::from_str; + use crate::simulation::io::vehicles::IOVehicleDefinitions; + #[test] + fn from_string_empty_type() { + let xml = "\ + \ + \ + \ + \ + "; + let veh_def: IOVehicleDefinitions = from_str(xml).unwrap(); + + assert_eq!(1, veh_def.veh_types.len()); + + let veh_type = veh_def.veh_types.first().unwrap(); + assert_eq!("some-vehicle-id", veh_type.id.as_str()); + } + + #[test] + fn from_string_full_type() { + let xml = "\ + \ + \ + some-description\ + \ + \ + \ + \ + \ + \ + \ + \ + "; + + let veh_def: IOVehicleDefinitions = from_str(xml).unwrap(); + + assert_eq!(1, veh_def.veh_types.len()); + + let veh_type = veh_def.veh_types.first().unwrap(); + assert_eq!("some-vehicle-id", veh_type.id.as_str()); + assert_eq!( + "some-description", + veh_type.description.as_ref().unwrap().as_str() + ); + assert_eq!( + "some-network-mode", + veh_type + .network_mode + .as_ref() + .unwrap() + .network_mode + .as_str() + ); + assert_eq!(9.5, veh_type.length.as_ref().unwrap().meter); + assert_eq!(9.5, veh_type.width.as_ref().unwrap().meter); + assert_eq!( + 9.5, + veh_type.maximum_velocity.as_ref().unwrap().meter_per_second + ); + assert_eq!( + 9.5, + veh_type.passenger_car_equivalents.as_ref().unwrap().pce + ); + assert_eq!( + 9.5, + veh_type.flow_efficiency_factor.as_ref().unwrap().factor + ); + } + #[test] fn test_from_file() { - let io_vehicles = IOVehicleDefinitions::from_file("./assets/vehicles/vehicles_v2.xml"); - println!("{io_vehicles:#?}") + let veh_def = IOVehicleDefinitions::from_file("./assets/vehicles/vehicles_v2.xml"); + assert_eq!(3, veh_def.veh_types.len()); + // no further assertions here, as the tests above test the individual properties. + // so, this test mainly tests whether the vehicles implementation calls the xml_reader } } diff --git a/src/simulation/io/xml_reader.rs b/src/simulation/io/xml_reader.rs index c31ddee1..9f38f8c7 100644 --- a/src/simulation/io/xml_reader.rs +++ b/src/simulation/io/xml_reader.rs @@ -1,9 +1,9 @@ -use quick_xml::de::from_reader; -use serde::de::DeserializeOwned; -use std::fs; use std::fs::File; use std::io::BufReader; -use tracing::{debug, info}; + +use quick_xml::de::from_reader; +use serde::de::DeserializeOwned; +use tracing::info; pub fn read(file_path: &str) -> T where @@ -15,26 +15,26 @@ where let buffered_reader = BufReader::new(file); // I guess this could be prettier, but I don't know how to achieve this in Rust yet :-/ - return if file_path.ends_with(".xml.gz") { + if file_path.ends_with(".xml.gz") { // use full name, to avoid ambiguity let decoder = flate2::read::GzDecoder::new(buffered_reader); let buffered_decoder = BufReader::new(decoder); let result: T = from_reader(buffered_decoder).unwrap(); result } else if file_path.ends_with(".xml") { - let s = fs::read_to_string(file_path).expect("Couldn't find file."); - debug!("File content of {}:\n{}", file_path, s); let result: Result = from_reader(buffered_reader); match result { Ok(x) => x, - Err(e) => panic!("Problem reading file: {:?}", e), + Err(_e) => { + panic!("Problem reading file: {_e:?}") + } } } else { panic!( "xml_reader::read: Can't open file path: {}. Only files with endings '.xml' or '.xml.gz' are supported.", file_path ); - }; + } } #[cfg(test)] From c8ba918f2d3a9cccd3d2907fa8ecf41afe6a8798 Mon Sep 17 00:00:00 2001 From: janekdererste Date: Thu, 14 Sep 2023 12:31:29 +0200 Subject: [PATCH 10/20] * add default implementations for vehicles, so that creation in garage.rs becomes easier * rename files, to snake case, as we are not in Java, * add 'add_veh' logic for garage --- src/simulation/io/vehicles.rs | 44 +++++- src/simulation/vehicles/Garage.rs | 32 ----- src/simulation/vehicles/garage.rs | 132 ++++++++++++++++++ src/simulation/vehicles/mod.rs | 4 +- .../{VehicleType.rs => vehicle_type.rs} | 14 +- 5 files changed, 180 insertions(+), 46 deletions(-) delete mode 100644 src/simulation/vehicles/Garage.rs create mode 100644 src/simulation/vehicles/garage.rs rename src/simulation/vehicles/{VehicleType.rs => vehicle_type.rs} (63%) diff --git a/src/simulation/io/vehicles.rs b/src/simulation/io/vehicles.rs index 006b7632..a7dca59a 100644 --- a/src/simulation/io/vehicles.rs +++ b/src/simulation/io/vehicles.rs @@ -41,29 +41,63 @@ pub struct IOCapacity { #[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] pub struct IODimension { - meter: f32, + pub(crate) meter: f32, +} + +impl Default for IODimension { + fn default() -> Self { + Self { meter: 1. } + } } #[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] #[serde(rename_all = "camelCase")] pub struct IOVelocity { - meter_per_second: f32, + pub(crate) meter_per_second: f32, +} + +impl Default for IOVelocity { + fn default() -> Self { + Self { + meter_per_second: f32::MAX, + } + } } #[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] pub struct IOPassengerCarEquivalents { - pce: f32, + pub(crate) pce: f32, +} + +impl Default for IOPassengerCarEquivalents { + fn default() -> Self { + Self { pce: 1. } + } } #[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] #[serde(rename_all = "camelCase")] pub struct IONetworkMode { - network_mode: String, + pub(crate) network_mode: String, +} + +impl Default for IONetworkMode { + fn default() -> Self { + Self { + network_mode: String::from("car"), + } + } } #[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] pub struct IOFowEfficiencyFactor { - factor: f32, + pub(crate) factor: f32, +} + +impl Default for IOFowEfficiencyFactor { + fn default() -> Self { + Self { factor: 1. } + } } #[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] diff --git a/src/simulation/vehicles/Garage.rs b/src/simulation/vehicles/Garage.rs deleted file mode 100644 index cb29c75c..00000000 --- a/src/simulation/vehicles/Garage.rs +++ /dev/null @@ -1,32 +0,0 @@ -use std::collections::HashMap; - -use crate::simulation::id::{Id, IdStore}; -use crate::simulation::messaging::messages::proto::Vehicle; -use crate::simulation::vehicles::VehicleType::VehicleType; - -#[derive(Debug)] -pub struct Garage<'g> { - vehicles: HashMap, Vehicle>, - vehicle_ids: IdStore<'g, Vehicle>, - vehicle_types: Vec, - vehicle_type_ids: IdStore<'g, VehicleType>, - modes: IdStore<'g, String>, -} - -impl<'g> Default for Garage<'g> { - fn default() -> Self { - Garage::new() - } -} - -impl<'g> Garage<'g> { - pub fn new() -> Self { - Garage { - vehicles: Default::default(), - vehicle_ids: Default::default(), - vehicle_types: vec![], - vehicle_type_ids: IdStore::new(), - modes: Default::default(), - } - } -} diff --git a/src/simulation/vehicles/garage.rs b/src/simulation/vehicles/garage.rs new file mode 100644 index 00000000..669ef4fc --- /dev/null +++ b/src/simulation/vehicles/garage.rs @@ -0,0 +1,132 @@ +use std::collections::HashMap; + +use crate::simulation::id::{Id, IdStore}; +use crate::simulation::io::vehicles::IOVehicleType; +use crate::simulation::messaging::messages::proto::Vehicle; +use crate::simulation::vehicles::vehicle_type::VehicleType; + +#[derive(Debug)] +pub struct Garage<'g> { + vehicles: HashMap, Vehicle>, + vehicle_ids: IdStore<'g, Vehicle>, + vehicle_types: Vec, + vehicle_type_ids: IdStore<'g, VehicleType>, + modes: IdStore<'g, String>, +} + +impl<'g> Default for Garage<'g> { + fn default() -> Self { + Garage::new() + } +} + +impl<'g> Garage<'g> { + pub fn new() -> Self { + Garage { + vehicles: Default::default(), + vehicle_ids: Default::default(), + vehicle_types: vec![], + vehicle_type_ids: IdStore::new(), + modes: Default::default(), + } + } + + pub fn add_io_veh_type(&mut self, io_veh_type: IOVehicleType) { + let id = self.vehicle_type_ids.create_id(&io_veh_type.id); + let net_mode = self + .modes + .create_id(&io_veh_type.network_mode.unwrap_or_default().network_mode); + + let veh_type = VehicleType { + id, + length: io_veh_type.length.unwrap_or_default().meter, + width: io_veh_type.width.unwrap_or_default().meter, + max_v: io_veh_type + .maximum_velocity + .unwrap_or_default() + .meter_per_second, + pce: io_veh_type + .passenger_car_equivalents + .unwrap_or_default() + .pce, + fef: io_veh_type + .flow_efficiency_factor + .unwrap_or_default() + .factor, + net_mode, + }; + self.add_veh_type(veh_type); + } + + pub fn add_veh_type(&mut self, veh_type: VehicleType) { + assert_eq!( + veh_type.id.internal, + self.vehicle_types.len(), + "internal id {} and slot in node vec {} were note the same. Probably, node id {} already exsists.", + veh_type.id.internal, + self.vehicle_types.len(), + veh_type.id.external + ); + + self.vehicle_types.push(veh_type); + } +} + +#[cfg(test)] +mod tests { + use crate::simulation::io::vehicles::IOVehicleType; + use crate::simulation::vehicles::garage::Garage; + use crate::simulation::vehicles::vehicle_type::VehicleType; + + #[test] + fn add_veh_type() { + let mut garage = Garage::new(); + let type_id = garage.vehicle_type_ids.create_id("some-type"); + let mode = garage.modes.create_id("default-mode"); + let veh_type = VehicleType::new(type_id, mode); + + garage.add_veh_type(veh_type); + + assert_eq!(1, garage.vehicle_types.len()); + } + + #[test] + #[should_panic] + fn add_veh_type_reject_duplicate() { + let mut garage = Garage::new(); + let type_id = garage.vehicle_type_ids.create_id("some-type"); + let mode = garage.modes.create_id("default-mode"); + let veh_type1 = VehicleType::new(type_id.clone(), mode.clone()); + let veh_type2 = VehicleType::new(type_id.clone(), mode.clone()); + + garage.add_veh_type(veh_type1); + garage.add_veh_type(veh_type2); + } + + #[test] + fn add_io_veh_type() { + let io_veh_type = IOVehicleType { + id: "some-id".to_string(), + description: None, + capacity: None, + length: None, + width: None, + maximum_velocity: None, + engine_information: None, + cost_information: None, + passenger_car_equivalents: None, + network_mode: None, + flow_efficiency_factor: None, + attributes: None, + }; + let mut garage = Garage::new(); + + garage.add_io_veh_type(io_veh_type); + + assert_eq!(1, garage.vehicle_types.len()); + assert_eq!(0, garage.modes.get_from_ext("car").internal); + assert_eq!(0, garage.vehicle_type_ids.get_from_ext("some-id").internal); + + assert!(garage.vehicle_types.get(0).is_some()); + } +} diff --git a/src/simulation/vehicles/mod.rs b/src/simulation/vehicles/mod.rs index b7ec6cc8..d4655f55 100644 --- a/src/simulation/vehicles/mod.rs +++ b/src/simulation/vehicles/mod.rs @@ -1,2 +1,2 @@ -mod Garage; -mod VehicleType; \ No newline at end of file +mod garage; +mod vehicle_type; \ No newline at end of file diff --git a/src/simulation/vehicles/VehicleType.rs b/src/simulation/vehicles/vehicle_type.rs similarity index 63% rename from src/simulation/vehicles/VehicleType.rs rename to src/simulation/vehicles/vehicle_type.rs index 95f87f19..cd174d9e 100644 --- a/src/simulation/vehicles/VehicleType.rs +++ b/src/simulation/vehicles/vehicle_type.rs @@ -2,13 +2,13 @@ use crate::simulation::id::Id; #[derive(Debug)] pub struct VehicleType { - id: Id, - length: f32, - width: f32, - max_v: f32, - pce: f32, - fef: f32, - net_mode: Id, + pub(crate) id: Id, + pub(crate) length: f32, + pub(crate) width: f32, + pub(crate) max_v: f32, + pub(crate) pce: f32, + pub(crate) fef: f32, + pub(crate) net_mode: Id, } impl VehicleType { From c85fb94c54dcbd317ed86ebf3cf83130d8292052 Mon Sep 17 00:00:00 2001 From: janekdererste Date: Thu, 14 Sep 2023 14:18:14 +0200 Subject: [PATCH 11/20] place vehicles into garage when reading in scenario --- src/simulation/config.rs | 10 ++++++++ src/simulation/controller.rs | 11 ++++++-- src/simulation/id.rs | 4 +++ src/simulation/io/vehicles.rs | 2 +- src/simulation/messaging/messages.proto | 3 +-- src/simulation/messaging/messages.rs | 22 +++++++--------- src/simulation/network/link.rs | 16 ++++++------ src/simulation/network/sim_network.rs | 20 +++++++-------- src/simulation/population/population.rs | 34 ++++++++++++++++--------- src/simulation/simulation.rs | 27 +++++++++++++++----- src/simulation/vehicles/garage.rs | 21 ++++++++++++++- src/simulation/vehicles/mod.rs | 4 +-- 12 files changed, 115 insertions(+), 59 deletions(-) diff --git a/src/simulation/config.rs b/src/simulation/config.rs index 02083c4a..97c6f790 100644 --- a/src/simulation/config.rs +++ b/src/simulation/config.rs @@ -14,6 +14,8 @@ pub struct Config { #[arg(long)] pub population_file: String, #[arg(long)] + pub vehicles_file: String, + #[arg(long)] pub vehicle_definitions_file: Option, #[arg(long, default_value = "./")] pub output_dir: String, @@ -35,6 +37,7 @@ pub struct ConfigBuilder { num_parts: usize, network_file: String, population_file: String, + vehicles_file: String, vehicle_definitions_file: Option, output_dir: String, events_mode: String, @@ -46,6 +49,7 @@ impl ConfigBuilder { ConfigBuilder { network_file: String::from(""), population_file: String::from(""), + vehicles_file: String::from(""), vehicle_definitions_file: None, output_dir: String::from("./"), events_mode: String::from("file"), @@ -81,6 +85,11 @@ impl ConfigBuilder { self } + pub fn vehicles_file(mut self, file: String) -> Self { + self.vehicles_file = file; + self + } + pub fn output_dir(mut self, dir: String) -> Self { self.output_dir = dir; self @@ -111,6 +120,7 @@ impl ConfigBuilder { num_parts: self.num_parts, network_file: self.network_file, population_file: self.population_file, + vehicles_file: self.vehicles_file, vehicle_definitions_file: self.vehicle_definitions_file, output_dir: self.output_dir, events_mode: self.events_mode, diff --git a/src/simulation/controller.rs b/src/simulation/controller.rs index a7755a09..62434e1d 100644 --- a/src/simulation/controller.rs +++ b/src/simulation/controller.rs @@ -14,6 +14,7 @@ use crate::simulation::messaging::message_broker::MpiMessageBroker; use crate::simulation::network::sim_network::SimNetworkPartition; use crate::simulation::population::population::Population; use crate::simulation::simulation::Simulation; +use crate::simulation::vehicles::garage::Garage; pub fn run(world: SystemCommunicator, config: Config) { let rank = world.rank(); @@ -34,8 +35,14 @@ pub fn run(world: SystemCommunicator, config: Config) { network.to_file(&output_path.join("output_network.xml.gz")); } - let population = - Population::from_file(config.population_file.as_ref(), &network, rank as usize); + let mut garage = Garage::from_file(config.vehicles_file.as_ref()); + + let population = Population::from_file( + config.population_file.as_ref(), + &network, + &mut garage, + rank as usize, + ); let network_partition = SimNetworkPartition::from_network(&network, rank as usize); info!( "Partition #{rank} network has: {} nodes and {} links. Population has {} agents", diff --git a/src/simulation/id.rs b/src/simulation/id.rs index e5e1f3fc..99cba5c7 100644 --- a/src/simulation/id.rs +++ b/src/simulation/id.rs @@ -89,6 +89,10 @@ impl<'ext, T> IdStore<'ext, T> { .clone() } + pub fn get_from_wire(&self, internal: u64) -> Id { + self.get(internal as usize) + } + pub fn get_from_ext(&self, external: &str) -> Id { let index = self .mapping diff --git a/src/simulation/io/vehicles.rs b/src/simulation/io/vehicles.rs index a7dca59a..43841c76 100644 --- a/src/simulation/io/vehicles.rs +++ b/src/simulation/io/vehicles.rs @@ -7,7 +7,7 @@ use crate::simulation::io::xml_reader; #[serde(rename = "vehicleDefinitions")] pub struct IOVehicleDefinitions { #[serde(rename = "vehicleType")] - veh_types: Vec, + pub veh_types: Vec, } #[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] diff --git a/src/simulation/messaging/messages.proto b/src/simulation/messaging/messages.proto index 61eef18e..87758685 100644 --- a/src/simulation/messaging/messages.proto +++ b/src/simulation/messaging/messages.proto @@ -26,7 +26,6 @@ message Vehicle { uint32 curr_route_elem = 2; uint64 type = 3; Agent agent = 4; - uint64 mode = 5; // this must be extended once more people use one vehicle } @@ -42,7 +41,7 @@ message Plan { } message Activity { - string act_type = 1; + uint64 act_type = 1; uint64 link_id = 2; float x = 3; float y = 4; diff --git a/src/simulation/messaging/messages.rs b/src/simulation/messaging/messages.rs index 1d1d8bfc..f364e8b6 100644 --- a/src/simulation/messaging/messages.rs +++ b/src/simulation/messaging/messages.rs @@ -112,13 +112,12 @@ impl Ord for VehicleMessage { impl Vehicle { // todo, fix type and mode - pub fn new(id: u64, veh_type: u64, mode: u64, agent: Agent) -> Vehicle { + pub fn new(id: u64, veh_type: u64, agent: Option) -> Vehicle { Vehicle { - id, - agent: Some(agent), + id: id, + agent, curr_route_elem: 0, - r#type: 0, - mode: 0, + r#type: veh_type, } } @@ -402,7 +401,7 @@ impl Plan { for element in &io_plan.elements { match element { IOPlanElement::Activity(io_act) => { - let act = Activity::from_io(io_act, net); + let act = Activity::from_io(io_act, net, pop); result.acts.push(act); } IOPlanElement::Leg(io_leg) => { @@ -429,12 +428,13 @@ impl Plan { } impl Activity { - fn from_io(io_act: &IOActivity, net: &Network) -> Self { + fn from_io(io_act: &IOActivity, net: &Network, pop: &Population) -> Self { let link_id = net.link_ids.get_from_ext(&io_act.link); + let act_type = pop.act_types.get_from_ext(&io_act.r#type); Activity { x: io_act.x, y: io_act.y, - act_type: io_act.r#type.clone(), + act_type: act_type.internal as u64, link_id: link_id.internal as u64, start_time: parse_time_opt(&io_act.start_time), end_time: parse_time_opt(&io_act.end_time), @@ -445,7 +445,7 @@ impl Activity { pub fn new( x: f32, y: f32, - act_type: String, + act_type: u64, link_id: u64, start_time: Option, end_time: Option, @@ -472,10 +472,6 @@ impl Activity { u32::MAX } } - - pub fn is_interaction(&self) -> bool { - self.act_type.contains("interaction") - } } impl Leg { diff --git a/src/simulation/network/link.rs b/src/simulation/network/link.rs index 1bec72ce..79b3d32b 100644 --- a/src/simulation/network/link.rs +++ b/src/simulation/network/link.rs @@ -279,7 +279,7 @@ mod tests { IdImpl::new_internal(0), ); let agent = create_agent(1, vec![]); - let vehicle = Vehicle::new(veh_id, 0, 0, agent); + let vehicle = Vehicle::new(veh_id, 0, Some(agent)); link.push_vehicle(vehicle, 0); @@ -305,9 +305,9 @@ mod tests { ); let agent1 = create_agent(1, vec![]); - let vehicle1 = Vehicle::new(id1, 0, 0, agent1); + let vehicle1 = Vehicle::new(id1, 0, Some(agent1)); let agent2 = create_agent(1, vec![]); - let vehicle2 = Vehicle::new(id2, 0, 0, agent2); + let vehicle2 = Vehicle::new(id2, 0, Some(agent2)); link.push_vehicle(vehicle1, 0); link.push_vehicle(vehicle2, 0); @@ -341,7 +341,7 @@ mod tests { while n < 10 { let agent = create_agent(n as u64, vec![]); - let vehicle = Vehicle::new(n as u64, 0, 0, agent); + let vehicle = Vehicle::new(n as u64, 0, Some(agent)); link.push_vehicle(vehicle, n); n += 1; } @@ -372,7 +372,7 @@ mod tests { while n < 10 { let agent = create_agent(n as u64, vec![]); - let vehicle = Vehicle::new(n as u64, 0, 0, agent); + let vehicle = Vehicle::new(n as u64, 0, Some(agent)); link.push_vehicle(vehicle, n); n += 1; } @@ -400,9 +400,9 @@ mod tests { ); let agent1 = create_agent(1, vec![]); - let vehicle1 = Vehicle::new(1, 0, 0, agent1); + let vehicle1 = Vehicle::new(1, 0, Some(agent1)); let agent2 = create_agent(2, vec![]); - let vehicle2 = Vehicle::new(2, 0, 0, agent2); + let vehicle2 = Vehicle::new(2, 0, Some(agent2)); link.push_vehicle(vehicle1, 0); link.push_vehicle(vehicle2, 0); @@ -421,7 +421,7 @@ mod tests { fn create_agent(id: u64, route: Vec) -> Agent { let route = Route::NetworkRoute(NetworkRoute::new(id, route)); let leg = Leg::new(route, "car", None, None); - let act = Activity::new(0., 0., String::from("some-type"), 1, None, None, None); + let act = Activity::new(0., 0., 0, 1, None, None, None); let mut plan = Plan::new(); plan.add_act(act); plan.add_leg(leg); diff --git a/src/simulation/network/sim_network.rs b/src/simulation/network/sim_network.rs index d19c8403..ac37e1dc 100644 --- a/src/simulation/network/sim_network.rs +++ b/src/simulation/network/sim_network.rs @@ -156,9 +156,7 @@ impl<'n> SimNetworkPartition<'n> { &Event::new_link_leave(vehicle.curr_route_elem as u64, vehicle.id), ); vehicle.advance_route_index(); - let link_id = global_network - .link_ids - .get(vehicle.curr_route_elem as usize); + let link_id = global_network.link_ids.get(vehicle.curr_link_id().unwrap()); match links.get_mut(&link_id).unwrap() { SimLink::Local(l) => { events.publish_event( @@ -232,7 +230,7 @@ mod tests { let mut sim_network = create_single_node_sim_network(&mut network); let mut publisher = EventsPublisher::new(); let agent = create_agent(1, vec![0]); - let vehicle = Vehicle::new(1, 0, 0, agent); + let vehicle = Vehicle::new(1, 0, Some(agent)); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { link1.push_vehicle(vehicle, 1); @@ -250,7 +248,7 @@ mod tests { let mut sim_network = create_single_node_sim_network(&mut network); let mut publisher = EventsPublisher::new(); let agent = create_agent(1, vec![0, 1]); - let vehicle = Vehicle::new(1, 0, 0, agent); + let vehicle = Vehicle::new(1, 0, Some(agent)); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { link1.push_vehicle(vehicle, 1); @@ -273,7 +271,7 @@ mod tests { let sim_network = sim_nets.get_mut(0).unwrap(); let mut publisher = EventsPublisher::new(); let agent = create_agent(1, vec![0, 1]); - let vehicle = Vehicle::new(1, 0, 0, agent); + let vehicle = Vehicle::new(1, 0, Some(agent)); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { link1.push_vehicle(vehicle, 1); @@ -292,9 +290,9 @@ mod tests { let mut sim_network = create_single_node_sim_network(&mut network); let mut publisher = EventsPublisher::new(); let agent1 = create_agent(1, vec![0]); - let vehicle1 = Vehicle::new(1, 0, 0, agent1); + let vehicle1 = Vehicle::new(1, 0, Some(agent1)); let agent2 = create_agent(2, vec![0]); - let vehicle2 = Vehicle::new(2, 0, 0, agent2); + let vehicle2 = Vehicle::new(2, 0, Some(agent2)); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { link1.push_vehicle(vehicle1, 1); @@ -325,9 +323,9 @@ mod tests { let mut sim_network = create_single_node_sim_network(&mut network); let mut publisher = EventsPublisher::new(); let agent1 = create_agent(1, vec![0, 1]); - let vehicle1 = Vehicle::new(1, 0, 0, agent1); + let vehicle1 = Vehicle::new(1, 0, Some(agent1)); let agent2 = create_agent(2, vec![0, 1]); - let vehicle2 = Vehicle::new(2, 0, 0, agent2); + let vehicle2 = Vehicle::new(2, 0, Some(agent2)); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { link1.push_vehicle(vehicle1, 1); @@ -451,7 +449,7 @@ mod tests { fn create_agent(id: u64, route: Vec) -> Agent { let route = Route::NetworkRoute(NetworkRoute::new(id, route)); let leg = Leg::new(route, "car", None, None); - let act = Activity::new(0., 0., String::from("some-type"), 1, None, None, None); + let act = Activity::new(0., 0., 0, 1, None, None, None); let mut plan = Plan::new(); plan.add_act(act); plan.add_leg(leg); diff --git a/src/simulation/population/population.rs b/src/simulation/population/population.rs index a8604897..66447892 100644 --- a/src/simulation/population/population.rs +++ b/src/simulation/population/population.rs @@ -4,6 +4,7 @@ use crate::simulation::id::{Id, IdStore}; use crate::simulation::io::population::{IOPerson, IOPlanElement, IOPopulation, IORoute}; use crate::simulation::messaging::messages::proto::{Agent, Vehicle}; use crate::simulation::network::global_network::{Link, Network}; +use crate::simulation::vehicles::garage::Garage; type ActType = (); @@ -26,17 +27,22 @@ impl<'p> Population<'p> { } } - pub fn from_file(file: &str, net: &Network, partition: usize) -> Self { + pub fn from_file(file: &str, net: &Network, garage: &mut Garage, partition: usize) -> Self { let io_population = IOPopulation::from_file(file); - Self::from_io(&io_population, net, partition) + Self::from_io(&io_population, net, garage, partition) } - pub fn from_io(io_population: &IOPopulation, network: &Network, partition: usize) -> Self { + pub fn from_io( + io_population: &IOPopulation, + network: &Network, + garage: &mut Garage, + partition: usize, + ) -> Self { let mut result = Population::new(); // first pass to set ids globally for io in io_population.persons.iter() { - Self::agent_id(io, &mut result); + Self::agent_id(io, &mut result, garage); } // then copy the agents on this partition @@ -63,7 +69,7 @@ impl<'p> Population<'p> { panic!("First element should be activity."); } - fn agent_id(io: &IOPerson, pop: &mut Population) { + fn agent_id(io: &IOPerson, pop: &mut Population, garage: &mut Garage) { pop.agent_ids.create_id(&io.id); for io_plan in io.plans.iter() { for element in io_plan.elements.iter() { @@ -72,14 +78,14 @@ impl<'p> Population<'p> { pop.act_types.create_id(&a.r#type); } IOPlanElement::Leg(l) => { - Self::route_ids(&l.route, pop); + Self::route_ids(&l.route, garage); } } } } } - fn route_ids(io: &IORoute, pop: &mut Population) { + fn route_ids(io: &IORoute, garage: &mut Garage) { match io.r#type.as_str() { "links" => { let veh_id = io @@ -89,7 +95,7 @@ impl<'p> Population<'p> { match veh_id.as_str() { "null" => (), _ => { - pop.vehicle_ids.create_id(veh_id); + garage.add_veh_id(veh_id); } }; } @@ -103,11 +109,13 @@ mod tests { use crate::simulation::messaging::messages::proto::leg::Route; use crate::simulation::network::global_network::Network; use crate::simulation::population::population::Population; + use crate::simulation::vehicles::garage::Garage; #[test] fn from_io_1_plan() { let net = Network::from_file("./assets/equil/equil-network.xml", 1); - let pop = Population::from_file("./assets/equil/equil-1-plan.xml", &net, 0); + let mut garage = Garage::new(); + let pop = Population::from_file("./assets/equil/equil-1-plan.xml", &net, &mut garage, 0); assert_eq!(1, pop.agents.len()); @@ -119,7 +127,8 @@ mod tests { assert_eq!(3, plan.legs.len()); let home_act = plan.acts.first().unwrap(); - assert_eq!("h", home_act.act_type.as_str()); + let act_type = pop.act_types.get_from_wire(home_act.act_type); + assert_eq!("h", act_type.external.as_str()); assert_eq!( net.link_ids.get_from_ext("1").internal as u64, home_act.link_id @@ -156,8 +165,9 @@ mod tests { #[test] fn from_io() { let net = Network::from_file("./assets/equil/equil-network.xml", 2); - let pop1 = Population::from_file("./assets/equil/equil-plans.xml.gz", &net, 0); - let pop2 = Population::from_file("./assets/equil/equil-plans.xml.gz", &net, 1); + let mut garage = Garage::new(); + let pop1 = Population::from_file("./assets/equil/equil-plans.xml.gz", &net, &mut garage, 0); + let pop2 = Population::from_file("./assets/equil/equil-plans.xml.gz", &net, &mut garage, 1); // metis produces unstable results on small networks so, make sure that one of the populations // has all the agents and the other doesn't diff --git a/src/simulation/simulation.rs b/src/simulation/simulation.rs index 179bd3ff..4bf3d466 100644 --- a/src/simulation/simulation.rs +++ b/src/simulation/simulation.rs @@ -8,12 +8,14 @@ use crate::simulation::messaging::messages::proto::leg::Route; use crate::simulation::messaging::messages::proto::{Agent, GenericRoute, Vehicle}; use crate::simulation::network::link::SimLink; use crate::simulation::network::sim_network::{ExitReason, SimNetworkPartition}; +use crate::simulation::population::population::Population; use crate::simulation::time_queue::TimeQueue; pub struct Simulation<'sim> { activity_q: TimeQueue, teleportation_q: TimeQueue, network: SimNetworkPartition<'sim>, + population: Population<'sim>, message_broker: MpiMessageBroker, events: EventsPublisher, } @@ -22,17 +24,23 @@ impl<'sim> Simulation<'sim> { pub fn new( config: &Config, network: SimNetworkPartition<'sim>, - population: crate::simulation::population::population::Population, + mut population: Population<'sim>, message_broker: MpiMessageBroker, events: EventsPublisher, ) -> Self { let mut activity_q = TimeQueue::new(); - for agent in population.agents.into_values() { + + // take agents and copy them into queues. This way we can keep population around to tranlate + // ids for events processing... + let agents = std::mem::take(&mut population.agents); + + for agent in agents.into_values() { activity_q.add(agent, config.start_time); } Simulation { network, + population, teleportation_q: TimeQueue::new(), activity_q, message_broker, @@ -72,12 +80,16 @@ impl<'sim> Simulation<'sim> { for mut agent in agents { let agent_id = agent.id; + let act_type = self + .population + .act_types + .get_from_wire(agent.curr_act().act_type); self.events.publish_event( now, &Event::new_act_end( agent_id, agent.curr_act().link_id, - agent.curr_act().act_type.clone(), + act_type.external.clone(), ), ); @@ -96,10 +108,10 @@ impl<'sim> Simulation<'sim> { ); if Simulation::is_local_route(route, &self.message_broker) { - let veh = Vehicle::new(agent.id, 1, leg.mode.clone(), agent); + let veh = Vehicle::new(agent.id, 1, Some(agent)); self.teleportation_q.add(veh, now); } else { - let veh = Vehicle::new(agent.id, 1, leg.mode.clone(), agent); + let veh = Vehicle::new(agent.id, 1, Some(agent)); self.message_broker.add_veh(veh, now); } } @@ -115,7 +127,7 @@ impl<'sim> Simulation<'sim> { &Event::new_person_enters_veh(agent_id, route.vehicle_id), ); - let veh = Vehicle::new(route.vehicle_id, 0, leg.mode.clone(), agent); + let veh = Vehicle::new(route.vehicle_id, 0, Some(agent)); self.veh_onto_network(veh, true, now); } } @@ -188,9 +200,10 @@ impl<'sim> Simulation<'sim> { &Event::new_arrival(agent.id, act.link_id, String::from("some mode")), ); //todo fix mode + let act_type = self.population.act_types.get_from_wire(act.act_type); self.events.publish_event( now, - &Event::new_act_start(agent.id, act.link_id, act.act_type.clone()), + &Event::new_act_start(agent.id, act.link_id, act_type.external.clone()), ); self.activity_q.add(agent, now); } diff --git a/src/simulation/vehicles/garage.rs b/src/simulation/vehicles/garage.rs index 669ef4fc..953be158 100644 --- a/src/simulation/vehicles/garage.rs +++ b/src/simulation/vehicles/garage.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use crate::simulation::id::{Id, IdStore}; -use crate::simulation::io::vehicles::IOVehicleType; +use crate::simulation::io::vehicles::{IOVehicleDefinitions, IOVehicleType}; use crate::simulation::messaging::messages::proto::Vehicle; use crate::simulation::vehicles::vehicle_type::VehicleType; @@ -31,6 +31,15 @@ impl<'g> Garage<'g> { } } + pub fn from_file(file_path: &str) -> Self { + let io_veh_definition = IOVehicleDefinitions::from_file(file_path); + let mut result = Self::new(); + for io_veh_type in io_veh_definition.veh_types { + result.add_io_veh_type(io_veh_type); + } + result + } + pub fn add_io_veh_type(&mut self, io_veh_type: IOVehicleType) { let id = self.vehicle_type_ids.create_id(&io_veh_type.id); let net_mode = self @@ -70,6 +79,10 @@ impl<'g> Garage<'g> { self.vehicle_types.push(veh_type); } + + pub fn add_veh_id(&mut self, external_id: &str) -> Id { + self.vehicle_ids.create_id(external_id) + } } #[cfg(test)] @@ -129,4 +142,10 @@ mod tests { assert!(garage.vehicle_types.get(0).is_some()); } + + #[test] + fn from_file() { + let garage = Garage::from_file("./assets/vehicles/vehicles_v2.xml"); + assert_eq!(3, garage.vehicle_types.len()); + } } diff --git a/src/simulation/vehicles/mod.rs b/src/simulation/vehicles/mod.rs index d4655f55..86f9b104 100644 --- a/src/simulation/vehicles/mod.rs +++ b/src/simulation/vehicles/mod.rs @@ -1,2 +1,2 @@ -mod garage; -mod vehicle_type; \ No newline at end of file +pub mod garage; +pub mod vehicle_type; \ No newline at end of file From b045e8a229167baae408160fad70f6f16f839461 Mon Sep 17 00:00:00 2001 From: janekdererste Date: Thu, 14 Sep 2023 14:43:14 +0200 Subject: [PATCH 12/20] remove vehicle ids from garage --- src/simulation/messaging/messages.rs | 35 ++++++++++++++++--------- src/simulation/population/population.rs | 8 +++--- src/simulation/vehicles/garage.rs | 10 +++---- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/simulation/messaging/messages.rs b/src/simulation/messaging/messages.rs index f364e8b6..03449ab4 100644 --- a/src/simulation/messaging/messages.rs +++ b/src/simulation/messaging/messages.rs @@ -16,6 +16,7 @@ use crate::simulation::messaging::messages::proto::{ use crate::simulation::network::global_network::Network; use crate::simulation::population::population::Population; use crate::simulation::time_queue::EndTime; +use crate::simulation::vehicles::garage::Garage; // Include the `messages` module, which is generated from messages.proto. pub mod proto { @@ -162,8 +163,13 @@ impl EndTime for Vehicle { } impl Agent { - pub fn from_io(io_person: &IOPerson, net: &Network, pop: &Population) -> Agent { - let plan = Plan::from_io(io_person.selected_plan(), net, pop); + pub fn from_io( + io_person: &IOPerson, + net: &Network, + pop: &Population, + garage: &Garage, + ) -> Agent { + let plan = Plan::from_io(io_person.selected_plan(), net, pop, garage); if plan.acts.is_empty() { debug!("There is an empty plan for person {:?}", io_person.id); @@ -386,16 +392,21 @@ impl Plan { } } - fn from_io(io_plan: &IOPlan, net: &Network, pop: &Population) -> Plan { + fn from_io(io_plan: &IOPlan, net: &Network, pop: &Population, garage: &Garage) -> Plan { assert!(!io_plan.elements.is_empty()); if let IOPlanElement::Leg(_leg) = io_plan.elements.get(0).unwrap() { panic!("First plan element must be an activity! But was a leg."); }; - Plan::get_full_plan_no_routing(io_plan, net, pop) + Plan::get_full_plan_no_routing(io_plan, net, pop, garage) } - fn get_full_plan_no_routing(io_plan: &IOPlan, net: &Network, pop: &Population) -> Plan { + fn get_full_plan_no_routing( + io_plan: &IOPlan, + net: &Network, + pop: &Population, + garage: &Garage, + ) -> Plan { let mut result = Plan::new(); for element in &io_plan.elements { @@ -405,7 +416,7 @@ impl Plan { result.acts.push(act); } IOPlanElement::Leg(io_leg) => { - let leg = Leg::from_io(io_leg, net, pop); + let leg = Leg::from_io(io_leg, net, garage); result.legs.push(leg); } } @@ -475,8 +486,8 @@ impl Activity { } impl Leg { - fn from_io(io_leg: &IOLeg, net: &Network, pop: &Population) -> Self { - let route = Route::from_io(&io_leg.route, net, pop); + fn from_io(io_leg: &IOLeg, net: &Network, garage: &Garage) -> Self { + let route = Route::from_io(&io_leg.route, net, garage); Self { route: Some(route), mode: 0, //todo fix mode @@ -496,7 +507,7 @@ impl Leg { } impl Route { - fn from_io(io_route: &IORoute, net: &Network, pop: &Population) -> Self { + fn from_io(io_route: &IORoute, net: &Network, garage: &Garage) -> Self { match io_route.r#type.as_str() { "generic" => Route::GenericRoute(GenericRoute::from_io(io_route, net)), "links" => { @@ -506,7 +517,7 @@ impl Route { if vehicle_id.eq("null") { Route::GenericRoute(GenericRoute::from_io(io_route, net)) } else { - Route::NetworkRoute(NetworkRoute::from_io(io_route, net, pop)) + Route::NetworkRoute(NetworkRoute::from_io(io_route, net, garage)) } } else { panic!("vehicle id is expected to be set. ") @@ -533,12 +544,12 @@ impl GenericRoute { } impl NetworkRoute { - fn from_io(io_route: &IORoute, net: &Network, pop: &Population) -> Self { + fn from_io(io_route: &IORoute, net: &Network, garage: &Garage) -> Self { let matsim_veh_id = io_route .vehicle .as_ref() .unwrap_or_else(|| panic!("Couldn't find veh-id for route {io_route:?} ")); - let veh_id = pop.vehicle_ids.get_from_ext(matsim_veh_id); + let veh_id = garage.vehicle_ids.get_from_ext(matsim_veh_id); let link_ids = match &io_route.route { None => Vec::new(), diff --git a/src/simulation/population/population.rs b/src/simulation/population/population.rs index 66447892..92f8dfe1 100644 --- a/src/simulation/population/population.rs +++ b/src/simulation/population/population.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use crate::simulation::id::{Id, IdStore}; use crate::simulation::io::population::{IOPerson, IOPlanElement, IOPopulation, IORoute}; -use crate::simulation::messaging::messages::proto::{Agent, Vehicle}; +use crate::simulation::messaging::messages::proto::Agent; use crate::simulation::network::global_network::{Link, Network}; use crate::simulation::vehicles::garage::Garage; @@ -12,7 +12,6 @@ type ActType = (); pub struct Population<'p> { pub agents: HashMap, Agent>, pub agent_ids: IdStore<'p, Agent>, - pub vehicle_ids: IdStore<'p, Vehicle>, // TODO this should probably go somewhere else pub act_types: IdStore<'p, ActType>, } @@ -22,7 +21,6 @@ impl<'p> Population<'p> { Population { agents: HashMap::default(), agent_ids: IdStore::new(), - vehicle_ids: IdStore::new(), act_types: IdStore::new(), } } @@ -49,7 +47,7 @@ impl<'p> Population<'p> { for io in io_population.persons.iter() { let link = Self::link_first_act(io, network); if partition == link.partition { - let agent = Agent::from_io(io, network, &result); + let agent = Agent::from_io(io, network, &result, garage); result .agents .insert(result.agent_ids.get(agent.id as usize), agent); @@ -145,7 +143,7 @@ mod tests { assert!(leg.route.is_some()); if let Route::NetworkRoute(net_route) = leg.route.as_ref().unwrap() { assert_eq!( - pop.vehicle_ids.get_from_ext("1").internal as u64, + garage.vehicle_ids.get_from_ext("1").internal as u64, net_route.vehicle_id ); assert_eq!( diff --git a/src/simulation/vehicles/garage.rs b/src/simulation/vehicles/garage.rs index 953be158..0514ba37 100644 --- a/src/simulation/vehicles/garage.rs +++ b/src/simulation/vehicles/garage.rs @@ -7,11 +7,11 @@ use crate::simulation::vehicles::vehicle_type::VehicleType; #[derive(Debug)] pub struct Garage<'g> { - vehicles: HashMap, Vehicle>, - vehicle_ids: IdStore<'g, Vehicle>, - vehicle_types: Vec, - vehicle_type_ids: IdStore<'g, VehicleType>, - modes: IdStore<'g, String>, + pub vehicles: HashMap, Vehicle>, + pub vehicle_ids: IdStore<'g, Vehicle>, + pub vehicle_types: Vec, + pub vehicle_type_ids: IdStore<'g, VehicleType>, + pub modes: IdStore<'g, String>, } impl<'g> Default for Garage<'g> { From 6ea3069079b7eff8055dca9309b4fe5b77a3663f Mon Sep 17 00:00:00 2001 From: janekdererste Date: Thu, 14 Sep 2023 15:10:29 +0200 Subject: [PATCH 13/20] move attributes relevant for qsim into vehicle to make calculation of link dynamics easier. --- src/simulation/controller.rs | 5 ++-- src/simulation/messaging/messages.proto | 4 +++- src/simulation/messaging/messages.rs | 4 +++- src/simulation/network/global_network.rs | 29 +++++++++++++----------- src/simulation/network/link.rs | 14 ++++++------ src/simulation/network/sim_network.rs | 14 ++++++------ src/simulation/population/population.rs | 4 ++-- src/simulation/simulation.rs | 6 ++--- 8 files changed, 44 insertions(+), 36 deletions(-) diff --git a/src/simulation/controller.rs b/src/simulation/controller.rs index 62434e1d..c8a4263b 100644 --- a/src/simulation/controller.rs +++ b/src/simulation/controller.rs @@ -25,9 +25,12 @@ pub fn run(world: SystemCommunicator, config: Config) { let output_path = PathBuf::from(&config.output_dir); fs::create_dir_all(&output_path).expect("Failed to create output path"); + let mut garage = Garage::from_file(config.vehicles_file.as_ref()); + let network = crate::simulation::network::global_network::Network::from_file( config.network_file.as_ref(), config.num_parts, + &mut garage, ); // write network with new ids to output but only once. @@ -35,8 +38,6 @@ pub fn run(world: SystemCommunicator, config: Config) { network.to_file(&output_path.join("output_network.xml.gz")); } - let mut garage = Garage::from_file(config.vehicles_file.as_ref()); - let population = Population::from_file( config.population_file.as_ref(), &network, diff --git a/src/simulation/messaging/messages.proto b/src/simulation/messaging/messages.proto index 87758685..5c86ff90 100644 --- a/src/simulation/messaging/messages.proto +++ b/src/simulation/messaging/messages.proto @@ -25,7 +25,9 @@ message Vehicle { uint64 id = 1; uint32 curr_route_elem = 2; uint64 type = 3; - Agent agent = 4; + float max_v = 4; + float pce = 5; + Agent agent = 6; // this must be extended once more people use one vehicle } diff --git a/src/simulation/messaging/messages.rs b/src/simulation/messaging/messages.rs index 03449ab4..45afb084 100644 --- a/src/simulation/messaging/messages.rs +++ b/src/simulation/messaging/messages.rs @@ -113,12 +113,14 @@ impl Ord for VehicleMessage { impl Vehicle { // todo, fix type and mode - pub fn new(id: u64, veh_type: u64, agent: Option) -> Vehicle { + pub fn new(id: u64, veh_type: u64, max_v: f32, pce: f32, agent: Option) -> Vehicle { Vehicle { id: id, agent, curr_route_elem: 0, r#type: veh_type, + max_v, + pce, } } diff --git a/src/simulation/network/global_network.rs b/src/simulation/network/global_network.rs index afc5223b..1783499c 100644 --- a/src/simulation/network/global_network.rs +++ b/src/simulation/network/global_network.rs @@ -1,6 +1,7 @@ use std::{collections::HashSet, path::Path}; use crate::simulation::io::attributes::{Attr, Attrs}; +use crate::simulation::vehicles::garage::Garage; use crate::simulation::{ id::{Id, IdStore}, io::network::{IOLink, IONetwork, IONode}, @@ -16,7 +17,7 @@ pub struct Network<'a> { // we make sure to store each mode only once. This could be optimized further if we'd // cache the HashSets which we store in the links. I.e. each combination of modes is only // one hash set. - pub modes: IdStore<'a, String>, + // pub modes: IdStore<'a, String>, pub nodes: Vec, pub links: Vec, } @@ -57,16 +58,15 @@ impl<'a> Network<'a> { Network { node_ids: IdStore::new(), link_ids: IdStore::new(), - modes: IdStore::new(), nodes: Vec::new(), links: Vec::new(), } } - pub fn from_file(file_path: &str, num_parts: usize) -> Self { + pub fn from_file(file_path: &str, num_parts: usize, garage: &mut Garage) -> Self { let io_network = IONetwork::from_file(file_path); let mut result = Network::new(); - Self::init_nodes_and_links(&mut result, io_network); + Self::init_nodes_and_links(&mut result, io_network, garage); Self::partition_network(&mut result, num_parts); result } @@ -170,7 +170,7 @@ impl<'a> Network<'a> { self.links.push(link); } - pub fn add_io_link(&mut self, io_link: IOLink) { + pub fn add_io_link(&mut self, io_link: IOLink, garage: &mut Garage) { let id = self.link_ids.create_id(&io_link.id); assert_eq!( id.internal, @@ -188,7 +188,7 @@ impl<'a> Network<'a> { .modes .split(',') .map(|s| s.trim()) - .map(|mode| self.modes.create_id(mode)) + .map(|mode| garage.modes.create_id(mode)) .collect(); let from_id = self.node_ids.get_from_ext(&io_link.from); let to_id = self.node_ids.get_from_ext(&io_link.to); @@ -215,13 +215,13 @@ impl<'a> Network<'a> { self.links.get(id.internal).unwrap() } - fn init_nodes_and_links(network: &mut Network, io_network: IONetwork) { + fn init_nodes_and_links(network: &mut Network, io_network: IONetwork, garage: &mut Garage) { for node in io_network.nodes.nodes { network.add_io_node(node) } for link in io_network.links.links { - network.add_io_link(link) + network.add_io_link(link, garage) } } @@ -301,6 +301,7 @@ impl Link { #[cfg(test)] mod tests { use crate::simulation::io::network::{IOLink, IONode}; + use crate::simulation::vehicles::garage::Garage; use super::{Link, Network, Node}; @@ -430,10 +431,11 @@ mod tests { attributes: None, }; + let mut garage = Garage::new(); let mut network = Network::new(); network.add_io_node(io_from); network.add_io_node(io_to); - network.add_io_link(io_link.clone()); + network.add_io_link(io_link.clone(), &mut garage); let from = network.get_node(&network.node_ids.get_from_ext(&ext_from_id)); let to = network.get_node(&network.node_ids.get_from_ext(&ext_to_id)); @@ -447,14 +449,15 @@ mod tests { assert_eq!(io_link.freespeed, link.freespeed); assert_eq!(io_link.permlanes, link.permlanes); - assert!(link.modes.contains(&network.modes.get_from_ext("car"))); - assert!(link.modes.contains(&network.modes.get_from_ext("ride"))); - assert!(link.modes.contains(&network.modes.get_from_ext("bike"))); + assert!(link.modes.contains(&garage.modes.get_from_ext("car"))); + assert!(link.modes.contains(&garage.modes.get_from_ext("ride"))); + assert!(link.modes.contains(&garage.modes.get_from_ext("bike"))); } #[test] fn from_file() { - let network = Network::from_file("./assets/equil/equil-network.xml", 2); + let mut garage = Garage::default(); + let network = Network::from_file("./assets/equil/equil-network.xml", 2, &mut garage); // check partitioning let expected_partitions = [0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0]; diff --git a/src/simulation/network/link.rs b/src/simulation/network/link.rs index 79b3d32b..004168f8 100644 --- a/src/simulation/network/link.rs +++ b/src/simulation/network/link.rs @@ -279,7 +279,7 @@ mod tests { IdImpl::new_internal(0), ); let agent = create_agent(1, vec![]); - let vehicle = Vehicle::new(veh_id, 0, Some(agent)); + let vehicle = Vehicle::new(veh_id, 0, 10., 1., Some(agent)); link.push_vehicle(vehicle, 0); @@ -305,9 +305,9 @@ mod tests { ); let agent1 = create_agent(1, vec![]); - let vehicle1 = Vehicle::new(id1, 0, Some(agent1)); + let vehicle1 = Vehicle::new(id1, 0, 10., 1., Some(agent1)); let agent2 = create_agent(1, vec![]); - let vehicle2 = Vehicle::new(id2, 0, Some(agent2)); + let vehicle2 = Vehicle::new(id2, 0, 10., 1., Some(agent2)); link.push_vehicle(vehicle1, 0); link.push_vehicle(vehicle2, 0); @@ -341,7 +341,7 @@ mod tests { while n < 10 { let agent = create_agent(n as u64, vec![]); - let vehicle = Vehicle::new(n as u64, 0, Some(agent)); + let vehicle = Vehicle::new(n as u64, 0, 10., 1., Some(agent)); link.push_vehicle(vehicle, n); n += 1; } @@ -372,7 +372,7 @@ mod tests { while n < 10 { let agent = create_agent(n as u64, vec![]); - let vehicle = Vehicle::new(n as u64, 0, Some(agent)); + let vehicle = Vehicle::new(n as u64, 0, 10., 1., Some(agent)); link.push_vehicle(vehicle, n); n += 1; } @@ -400,9 +400,9 @@ mod tests { ); let agent1 = create_agent(1, vec![]); - let vehicle1 = Vehicle::new(1, 0, Some(agent1)); + let vehicle1 = Vehicle::new(1, 0, 10., 1., Some(agent1)); let agent2 = create_agent(2, vec![]); - let vehicle2 = Vehicle::new(2, 0, Some(agent2)); + let vehicle2 = Vehicle::new(2, 0, 10., 1., Some(agent2)); link.push_vehicle(vehicle1, 0); link.push_vehicle(vehicle2, 0); diff --git a/src/simulation/network/sim_network.rs b/src/simulation/network/sim_network.rs index ac37e1dc..7928fb2c 100644 --- a/src/simulation/network/sim_network.rs +++ b/src/simulation/network/sim_network.rs @@ -230,7 +230,7 @@ mod tests { let mut sim_network = create_single_node_sim_network(&mut network); let mut publisher = EventsPublisher::new(); let agent = create_agent(1, vec![0]); - let vehicle = Vehicle::new(1, 0, Some(agent)); + let vehicle = Vehicle::new(1, 0, 10., 1., Some(agent)); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { link1.push_vehicle(vehicle, 1); @@ -248,7 +248,7 @@ mod tests { let mut sim_network = create_single_node_sim_network(&mut network); let mut publisher = EventsPublisher::new(); let agent = create_agent(1, vec![0, 1]); - let vehicle = Vehicle::new(1, 0, Some(agent)); + let vehicle = Vehicle::new(1, 0, 10., 1., Some(agent)); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { link1.push_vehicle(vehicle, 1); @@ -271,7 +271,7 @@ mod tests { let sim_network = sim_nets.get_mut(0).unwrap(); let mut publisher = EventsPublisher::new(); let agent = create_agent(1, vec![0, 1]); - let vehicle = Vehicle::new(1, 0, Some(agent)); + let vehicle = Vehicle::new(1, 0, 10., 1., Some(agent)); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { link1.push_vehicle(vehicle, 1); @@ -290,9 +290,9 @@ mod tests { let mut sim_network = create_single_node_sim_network(&mut network); let mut publisher = EventsPublisher::new(); let agent1 = create_agent(1, vec![0]); - let vehicle1 = Vehicle::new(1, 0, Some(agent1)); + let vehicle1 = Vehicle::new(1, 0, 10., 1., Some(agent1)); let agent2 = create_agent(2, vec![0]); - let vehicle2 = Vehicle::new(2, 0, Some(agent2)); + let vehicle2 = Vehicle::new(2, 0, 10., 1., Some(agent2)); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { link1.push_vehicle(vehicle1, 1); @@ -323,9 +323,9 @@ mod tests { let mut sim_network = create_single_node_sim_network(&mut network); let mut publisher = EventsPublisher::new(); let agent1 = create_agent(1, vec![0, 1]); - let vehicle1 = Vehicle::new(1, 0, Some(agent1)); + let vehicle1 = Vehicle::new(1, 0, 10., 1., Some(agent1)); let agent2 = create_agent(2, vec![0, 1]); - let vehicle2 = Vehicle::new(2, 0, Some(agent2)); + let vehicle2 = Vehicle::new(2, 0, 10., 1., Some(agent2)); let in_link_id = sim_network.global_network.link_ids.get(0); if let SimLink::Local(link1) = sim_network.links.get_mut(&in_link_id).unwrap() { link1.push_vehicle(vehicle1, 1); diff --git a/src/simulation/population/population.rs b/src/simulation/population/population.rs index 92f8dfe1..1ba3c89b 100644 --- a/src/simulation/population/population.rs +++ b/src/simulation/population/population.rs @@ -111,8 +111,8 @@ mod tests { #[test] fn from_io_1_plan() { - let net = Network::from_file("./assets/equil/equil-network.xml", 1); let mut garage = Garage::new(); + let net = Network::from_file("./assets/equil/equil-network.xml", 1, &mut garage); let pop = Population::from_file("./assets/equil/equil-1-plan.xml", &net, &mut garage, 0); assert_eq!(1, pop.agents.len()); @@ -162,8 +162,8 @@ mod tests { #[test] fn from_io() { - let net = Network::from_file("./assets/equil/equil-network.xml", 2); let mut garage = Garage::new(); + let net = Network::from_file("./assets/equil/equil-network.xml", 2, &mut garage); let pop1 = Population::from_file("./assets/equil/equil-plans.xml.gz", &net, &mut garage, 0); let pop2 = Population::from_file("./assets/equil/equil-plans.xml.gz", &net, &mut garage, 1); diff --git a/src/simulation/simulation.rs b/src/simulation/simulation.rs index 4bf3d466..cf3838f5 100644 --- a/src/simulation/simulation.rs +++ b/src/simulation/simulation.rs @@ -108,10 +108,10 @@ impl<'sim> Simulation<'sim> { ); if Simulation::is_local_route(route, &self.message_broker) { - let veh = Vehicle::new(agent.id, 1, Some(agent)); + let veh = Vehicle::new(agent.id, 1, 10., 1., Some(agent)); self.teleportation_q.add(veh, now); } else { - let veh = Vehicle::new(agent.id, 1, Some(agent)); + let veh = Vehicle::new(agent.id, 1, 10., 1., Some(agent)); self.message_broker.add_veh(veh, now); } } @@ -127,7 +127,7 @@ impl<'sim> Simulation<'sim> { &Event::new_person_enters_veh(agent_id, route.vehicle_id), ); - let veh = Vehicle::new(route.vehicle_id, 0, Some(agent)); + let veh = Vehicle::new(route.vehicle_id, 0, 10., 1., Some(agent)); self.veh_onto_network(veh, true, now); } } From 2a4dc87d0093e8fd0ee72fa3dd25de929ced1e9d Mon Sep 17 00:00:00 2001 From: janekdererste Date: Thu, 14 Sep 2023 15:43:53 +0200 Subject: [PATCH 14/20] add speed from vehicle again --- src/simulation/network/link.rs | 27 +-------------------------- src/simulation/network/sim_network.rs | 6 +++--- 2 files changed, 4 insertions(+), 29 deletions(-) diff --git a/src/simulation/network/link.rs b/src/simulation/network/link.rs index 004168f8..1c4ce519 100644 --- a/src/simulation/network/link.rs +++ b/src/simulation/network/link.rs @@ -161,7 +161,7 @@ impl LocalLink { } pub fn push_vehicle(&mut self, vehicle: Vehicle, now: u32) { - let speed = self.get_speed_for_vehicle(&vehicle); + let speed = self.freespeed.min(vehicle.max_v); let duration = (self.length / speed) as u32; let earliest_exit_time = now + duration; self.q.push_back(VehicleQEntry { @@ -187,31 +187,6 @@ impl LocalLink { popped_veh } - - fn get_speed_for_vehicle(&self, vehicle: &Vehicle) -> f32 { - panic!("not implemented"); - /* - if vehicle_definitions.is_none() { - return self.freespeed; - } - - let vehicle_max_speed = vehicle_definitions - .as_ref() - .unwrap() - .get_max_speed_for_mode(0); //todo fix mode - - if vehicle_max_speed.is_none() { - warn!( - "There is no max speed given for vehicle type {:?}. Using freespeed of links.", - 0 // todo fix mode - ); - return self.freespeed; - } - - self.freespeed.min(vehicle_max_speed.unwrap()) - - */ - } } #[derive(Debug, Clone)] diff --git a/src/simulation/network/sim_network.rs b/src/simulation/network/sim_network.rs index 7928fb2c..95c65a8c 100644 --- a/src/simulation/network/sim_network.rs +++ b/src/simulation/network/sim_network.rs @@ -59,14 +59,14 @@ impl<'n> SimNetworkPartition<'n> { let from_part = all_nodes.get(link.from.internal).unwrap().partition; let to_part = all_nodes.get(link.to.internal).unwrap().partition; - return if from_part == to_part { + if from_part == to_part { SimLink::Local(LocalLink::from_link(link, 1.0)) } else if to_part == partition { let local_link = LocalLink::from_link(&link, 1.0); SimLink::In(SplitInLink::new(from_part, local_link)) } else { SimLink::Out(SplitOutLink::new(link.id.clone(), to_part)) - }; + } } pub fn new( @@ -105,7 +105,7 @@ impl<'n> SimNetworkPartition<'n> { for node_id in &self.nodes { Self::move_node( node_id, - &self.global_network, + self.global_network, &mut self.links, &mut exited_vehicles, events, From 88a01fd848fcab05a42e1c41b1ad9c82a594d9fc Mon Sep 17 00:00:00 2001 From: janekdererste Date: Wed, 4 Oct 2023 12:46:04 +0200 Subject: [PATCH 15/20] Add Level of detail proerty to vehicle types. This is represented in the vehicle type xml as attribute. --- Cargo.lock | 72 +++++++++++----------- src/simulation/io/attributes.rs | 12 +++- src/simulation/vehicles/garage.rs | 79 +++++++++++++++++++++++-- src/simulation/vehicles/vehicle_type.rs | 44 ++++++++++++++ 4 files changed, 164 insertions(+), 43 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 08ba25dc..21073b51 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -28,9 +28,9 @@ dependencies = [ [[package]] name = "anstream" -version = "0.5.0" +version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1f58811cfac344940f1a400b6e6231ce35171f614f26439e80f8c1465c5cc0c" +checksum = "2ab91ebe16eb252986481c5b62f6098f3b698a45e34b5b98200cf20dd2484a44" dependencies = [ "anstyle", "anstyle-parse", @@ -42,15 +42,15 @@ dependencies = [ [[package]] name = "anstyle" -version = "1.0.3" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b84bf0a05bbb2a83e5eb6fa36bb6e87baa08193c35ff52bbf6b38d8af2890e46" +checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87" [[package]] name = "anstyle-parse" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "938874ff5980b03a87c5524b3ae5b59cf99b1d6bc836848df7bc5ada9643c333" +checksum = "317b9a89c1868f5ea6ff1d9539a69f45dffc21ce321ac1fd1160dfa48c8e2140" dependencies = [ "utf8parse", ] @@ -66,9 +66,9 @@ dependencies = [ [[package]] name = "anstyle-wincon" -version = "2.1.0" +version = "3.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "58f54d10c6dfa51283a066ceab3ec1ab78d13fae00aa49243a45e4571fb79dfd" +checksum = "f0699d10d2f4d628a98ee7b57b289abbc98ff3bad977cb3152709d4bf2330628" dependencies = [ "anstyle", "windows-sys", @@ -213,9 +213,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.4.4" +version = "4.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1d7b8d5ec32af0fadc644bf1fd509a688c2103b185644bb1e29d164e0703136" +checksum = "d04704f56c2cde07f43e8e2c154b43f216dc5c92fc98ada720177362f953b956" dependencies = [ "clap_builder", "clap_derive", @@ -223,9 +223,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.4.4" +version = "4.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5179bb514e4d7c2051749d8fcefa2ed6d06a9f4e6d69faf3805f5d80b8cf8d56" +checksum = "0e231faeaca65ebd1ea3c737966bf858971cd38c3849107aa3ea7de90a804e45" dependencies = [ "anstream", "anstyle", @@ -337,9 +337,9 @@ checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] name = "errno" -version = "0.3.3" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "136526188508e25c6fef639d7927dfb3e0e3084488bf202267829cf7fc23dbdd" +checksum = "add4f07d43996f76ef320709726a556a9d4f965d9410d8d0271132d2f8293480" dependencies = [ "errno-dragonfly", "libc", @@ -358,9 +358,9 @@ dependencies = [ [[package]] name = "fastrand" -version = "2.0.0" +version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6999dc1837253364c2ebb0704ba97994bd874e8f195d665c50b7548f6ea92764" +checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" [[package]] name = "fixedbitset" @@ -493,9 +493,9 @@ checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" [[package]] name = "hashbrown" -version = "0.14.0" +version = "0.14.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a" +checksum = "7dfda62a12f55daeae5015f81b0baea145391cb4520f86c248fc615d72640d12" [[package]] name = "heck" @@ -523,9 +523,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.0.0" +version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5477fe2230a79769d8dc68e0eabf5437907c0457a5614a9e8dddb67f65eb65d" +checksum = "8adf3ddd720272c6ea8bf59463c04e0f93d0bbf7c5439b691bca2987e0270897" dependencies = [ "equivalent", "hashbrown", @@ -595,9 +595,9 @@ dependencies = [ [[package]] name = "linux-raw-sys" -version = "0.4.7" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a9bad9f94746442c783ca431b22403b519cd7fbeed0533fdd6328b2f2212128" +checksum = "3852614a3bd9ca9804678ba6be5e3b8ce76dfc902cae004e3e0c44051b6e88db" [[package]] name = "lock_api" @@ -617,9 +617,9 @@ checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" [[package]] name = "memchr" -version = "2.6.3" +version = "2.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f232d6ef707e1956a43342693d2a31e72989554d58299d7a88738cc95b0d35c" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" [[package]] name = "memoffset" @@ -950,9 +950,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.9.5" +version = "1.9.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "697061221ea1b4a94a624f67d0ae2bfe4e22b8a17b6a192afb11046542cc8c47" +checksum = "ebee201405406dbf528b8b672104ae6d6d63e6d118cb10e4d51abbc7b58044ff" dependencies = [ "aho-corasick", "memchr", @@ -962,9 +962,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.3.8" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2f401f4955220693b56f8ec66ee9c78abffd8d1c4f23dc41a23839eb88f0795" +checksum = "59b23e92ee4318893fa3fe3e6fb365258efbfe6ac6ab30f090cdcbb7aa37efa9" dependencies = [ "aho-corasick", "memchr", @@ -1011,9 +1011,9 @@ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" [[package]] name = "rustix" -version = "0.38.14" +version = "0.38.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "747c788e9ce8e92b12cd485c49ddf90723550b654b32508f979b71a7b1ecda4f" +checksum = "d2f9da0cbd88f9f09e7814e388301c8414c51c62aa6ce1e4b5c551d49d96e531" dependencies = [ "bitflags 2.4.0", "errno", @@ -1098,9 +1098,9 @@ dependencies = [ [[package]] name = "sharded-slab" -version = "0.1.4" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31" +checksum = "c1b21f559e07218024e7e9f90f96f601825397de0e25420135f7f952453fed0b" dependencies = [ "lazy_static", ] @@ -1175,18 +1175,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.48" +version = "1.0.49" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d6d7a740b8a666a7e828dd00da9c0dc290dff53154ea77ac109281de90589b7" +checksum = "1177e8c6d7ede7afde3585fd2513e611227efd6481bd78d2e82ba1ce16557ed4" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.48" +version = "1.0.49" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49922ecae66cc8a249b77e68d1d0623c1b2c514f0060c27cdc68bd62a1219d35" +checksum = "10712f02019e9288794769fba95cd6847df9874d49d871d062172f9dd41bc4cc" dependencies = [ "proc-macro2", "quote", diff --git a/src/simulation/io/attributes.rs b/src/simulation/io/attributes.rs index 2f96bdf9..d6761c23 100644 --- a/src/simulation/io/attributes.rs +++ b/src/simulation/io/attributes.rs @@ -8,7 +8,17 @@ pub struct Attr { pub value: String, } -#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] +impl Attr { + pub fn new(name: String, value: String) -> Self { + Attr { + name, + class: "".to_string(), + value, + } + } +} + +#[derive(Debug, Deserialize, Serialize, PartialEq, Clone, Default)] pub struct Attrs { #[serde(rename = "attribute", default)] pub attributes: Vec, diff --git a/src/simulation/vehicles/garage.rs b/src/simulation/vehicles/garage.rs index 0514ba37..5e618b40 100644 --- a/src/simulation/vehicles/garage.rs +++ b/src/simulation/vehicles/garage.rs @@ -3,17 +3,23 @@ use std::collections::HashMap; use crate::simulation::id::{Id, IdStore}; use crate::simulation::io::vehicles::{IOVehicleDefinitions, IOVehicleType}; use crate::simulation::messaging::messages::proto::Vehicle; -use crate::simulation::vehicles::vehicle_type::VehicleType; +use crate::simulation::vehicles::vehicle_type::{LevelOfDetail, VehicleType}; #[derive(Debug)] pub struct Garage<'g> { - pub vehicles: HashMap, Vehicle>, + pub vehicles: HashMap, GarageVehicle>, pub vehicle_ids: IdStore<'g, Vehicle>, pub vehicle_types: Vec, pub vehicle_type_ids: IdStore<'g, VehicleType>, pub modes: IdStore<'g, String>, } +#[derive(Debug)] +pub struct GarageVehicle { + pub id: Id, + pub veh_type: Id, +} + impl<'g> Default for Garage<'g> { fn default() -> Self { Garage::new() @@ -45,6 +51,17 @@ impl<'g> Garage<'g> { let net_mode = self .modes .create_id(&io_veh_type.network_mode.unwrap_or_default().network_mode); + let lod = if let Some(attr) = io_veh_type + .attributes + .unwrap_or_default() + .attributes + .iter() + .find(|&attr| attr.name.eq("lod")) + { + LevelOfDetail::from(attr.value.as_str()) + } else { + LevelOfDetail::from("network") + }; let veh_type = VehicleType { id, @@ -63,6 +80,7 @@ impl<'g> Garage<'g> { .unwrap_or_default() .factor, net_mode, + lod, }; self.add_veh_type(veh_type); } @@ -87,9 +105,13 @@ impl<'g> Garage<'g> { #[cfg(test)] mod tests { - use crate::simulation::io::vehicles::IOVehicleType; + use crate::simulation::io::attributes::{Attr, Attrs}; + use crate::simulation::io::vehicles::{ + IODimension, IOFowEfficiencyFactor, IONetworkMode, IOPassengerCarEquivalents, + IOVehicleType, IOVelocity, + }; use crate::simulation::vehicles::garage::Garage; - use crate::simulation::vehicles::vehicle_type::VehicleType; + use crate::simulation::vehicles::vehicle_type::{LevelOfDetail, VehicleType}; #[test] fn add_veh_type() { @@ -117,7 +139,7 @@ mod tests { } #[test] - fn add_io_veh_type() { + fn add_empty_io_veh_type() { let io_veh_type = IOVehicleType { id: "some-id".to_string(), description: None, @@ -140,7 +162,52 @@ mod tests { assert_eq!(0, garage.modes.get_from_ext("car").internal); assert_eq!(0, garage.vehicle_type_ids.get_from_ext("some-id").internal); - assert!(garage.vehicle_types.get(0).is_some()); + let veh_type_opt = garage.vehicle_types.first(); + assert!(veh_type_opt.is_some()); + let veh_type = veh_type_opt.unwrap(); + assert!(matches!(veh_type.lod, LevelOfDetail::Network)); + } + + #[test] + fn add_io_veh_type() { + let io_veh_type = IOVehicleType { + id: "some-id".to_string(), + description: None, + capacity: None, + length: Some(IODimension { meter: 10. }), + width: Some(IODimension { meter: 5. }), + maximum_velocity: Some(IOVelocity { + meter_per_second: 100., + }), + engine_information: None, + cost_information: None, + passenger_car_equivalents: Some(IOPassengerCarEquivalents { pce: 21.0 }), + network_mode: Some(IONetworkMode { + network_mode: "some_mode".to_string(), + }), + flow_efficiency_factor: Some(IOFowEfficiencyFactor { factor: 2. }), + attributes: Some(Attrs { + attributes: vec![Attr::new(String::from("lod"), String::from("teleported"))], + }), + }; + let mut garage = Garage::new(); + + garage.add_io_veh_type(io_veh_type); + + let expected_id = garage.vehicle_type_ids.get_from_ext("some-id"); + let expected_mode = garage.modes.get_from_ext("some_mode"); + + let veh_type_opt = garage.vehicle_types.first(); + assert!(veh_type_opt.is_some()); + let veh_type = veh_type_opt.unwrap(); + assert!(matches!(veh_type.lod, LevelOfDetail::Teleported)); + assert_eq!(veh_type.max_v, 100.); + assert_eq!(veh_type.width, 5.0); + assert_eq!(veh_type.length, 10.); + assert_eq!(veh_type.pce, 21.); + assert_eq!(veh_type.fef, 2.); + assert_eq!(veh_type.id, expected_id); + assert_eq!(veh_type.net_mode, expected_mode) } #[test] diff --git a/src/simulation/vehicles/vehicle_type.rs b/src/simulation/vehicles/vehicle_type.rs index cd174d9e..21b70c89 100644 --- a/src/simulation/vehicles/vehicle_type.rs +++ b/src/simulation/vehicles/vehicle_type.rs @@ -9,6 +9,23 @@ pub struct VehicleType { pub(crate) pce: f32, pub(crate) fef: f32, pub(crate) net_mode: Id, + pub(crate) lod: LevelOfDetail, +} + +#[derive(Debug)] +pub enum LevelOfDetail { + Network, + Teleported, +} + +impl From<&str> for LevelOfDetail { + fn from(value: &str) -> Self { + match value { + "network" => LevelOfDetail::Network, + "teleported" => LevelOfDetail::Teleported, + _ => panic!("&{value} is not yet implemented as level of detail"), + } + } } impl VehicleType { @@ -21,6 +38,33 @@ impl VehicleType { pce: 0.0, fef: 0.0, net_mode, + lod: LevelOfDetail::Network, } } } + +#[cfg(test)] +mod tests { + use crate::simulation::vehicles::vehicle_type::LevelOfDetail; + + #[test] + fn lod_from_network() { + let value = "network"; + let veh_type = LevelOfDetail::from(value); + assert!(matches!(veh_type, LevelOfDetail::Network)); + } + + #[test] + fn lod_from_teleported() { + let value = "teleported"; + let veh_type = LevelOfDetail::from(value); + assert!(matches!(veh_type, LevelOfDetail::Teleported)); + } + + #[test] + #[should_panic] + fn lod_from_not_supported() { + let value = "something-invalid"; + let _ = LevelOfDetail::from(value); + } +} From 573f021709dd62cc773a40252c35f7e02cca55f1 Mon Sep 17 00:00:00 2001 From: janekdererste Date: Wed, 4 Oct 2023 17:32:19 +0200 Subject: [PATCH 16/20] WIP: Add vehicles and vehicle ids to population creation. Add meaningfull test to test how vehicles are created from population. --- Cargo.lock | 610 ++++++++++++------------ assets/3-links/3-agent.xml | 2 +- assets/3-links/vehicle_definitions.xml | 9 - assets/3-links/vehicles.xml | 38 ++ assets/equil/equil-1-plan.xml | 142 +++--- assets/equil/equil-plans.xml.gz | Bin 5479 -> 5494 bytes assets/equil/equil-vehicles.xml | 27 ++ src/simulation/id.rs | 11 +- src/simulation/population/population.rs | 158 ++++-- src/simulation/vehicles/garage.rs | 10 +- tests/three_link_it.rs | 6 +- 11 files changed, 576 insertions(+), 437 deletions(-) delete mode 100644 assets/3-links/vehicle_definitions.xml create mode 100644 assets/3-links/vehicles.xml create mode 100644 assets/equil/equil-vehicles.xml diff --git a/Cargo.lock b/Cargo.lock index 21073b51..a9406a76 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,7 +14,7 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ea5d730647d4fadd988536d06fecce94b7b4f2a7efdae548f1cf4b63205518ab" dependencies = [ - "memchr", + "memchr", ] [[package]] @@ -23,7 +23,7 @@ version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" dependencies = [ - "winapi", + "winapi", ] [[package]] @@ -32,12 +32,12 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2ab91ebe16eb252986481c5b62f6098f3b698a45e34b5b98200cf20dd2484a44" dependencies = [ - "anstyle", - "anstyle-parse", - "anstyle-query", - "anstyle-wincon", - "colorchoice", - "utf8parse", + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", ] [[package]] @@ -52,7 +52,7 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "317b9a89c1868f5ea6ff1d9539a69f45dffc21ce321ac1fd1160dfa48c8e2140" dependencies = [ - "utf8parse", + "utf8parse", ] [[package]] @@ -61,7 +61,7 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" dependencies = [ - "windows-sys", + "windows-sys", ] [[package]] @@ -70,8 +70,8 @@ version = "3.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0699d10d2f4d628a98ee7b57b289abbc98ff3bad977cb3152709d4bf2330628" dependencies = [ - "anstyle", - "windows-sys", + "anstyle", + "windows-sys", ] [[package]] @@ -86,9 +86,9 @@ version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" dependencies = [ - "hermit-abi", - "libc", - "winapi", + "hermit-abi", + "libc", + "winapi", ] [[package]] @@ -103,7 +103,7 @@ version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aef8da1805e028a172334c3b680f93e71126f2327622faef2ec3d893c0a4ad77" dependencies = [ - "cc", + "cc", ] [[package]] @@ -112,21 +112,21 @@ version = "0.65.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cfdf7b466f9a4903edc73f95d6d2bcd5baf8ae620638762244d3f60143643cc5" dependencies = [ - "bitflags 1.3.2", - "cexpr", - "clang-sys", - "lazy_static", - "lazycell", - "log", - "peeking_take_while", - "prettyplease 0.2.15", - "proc-macro2", - "quote", - "regex", - "rustc-hash", - "shlex", - "syn 2.0.37", - "which", + "bitflags 1.3.2", + "cexpr", + "clang-sys", + "lazy_static", + "lazycell", + "log", + "peeking_take_while", + "prettyplease 0.2.15", + "proc-macro2", + "quote", + "regex", + "rustc-hash", + "shlex", + "syn 2.0.37", + "which", ] [[package]] @@ -135,18 +135,18 @@ version = "0.66.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2b84e06fc203107bfbad243f4aba2af864eb7db3b1cf46ea0a023b0b433d2a7" dependencies = [ - "bitflags 2.4.0", - "cexpr", - "clang-sys", - "lazy_static", - "lazycell", - "peeking_take_while", - "proc-macro2", - "quote", - "regex", - "rustc-hash", - "shlex", - "syn 2.0.37", + "bitflags 2.4.0", + "cexpr", + "clang-sys", + "lazy_static", + "lazycell", + "peeking_take_while", + "proc-macro2", + "quote", + "regex", + "rustc-hash", + "shlex", + "syn 2.0.37", ] [[package]] @@ -166,8 +166,8 @@ name = "build-probe-mpi" version = "0.1.2" source = "git+https://github.com/rsmpi/rsmpi?rev=1622101c9fc3a78620e9649f7f895616289d3558#1622101c9fc3a78620e9649f7f895616289d3558" dependencies = [ - "pkg-config", - "shell-words", + "pkg-config", + "shell-words", ] [[package]] @@ -182,7 +182,7 @@ version = "1.0.83" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" dependencies = [ - "libc", + "libc", ] [[package]] @@ -191,7 +191,7 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" dependencies = [ - "nom", + "nom", ] [[package]] @@ -206,9 +206,9 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c688fc74432808e3eb684cae8830a86be1d66a2bd58e1f248ed0960a590baf6f" dependencies = [ - "glob", - "libc", - "libloading", + "glob", + "libc", + "libloading", ] [[package]] @@ -217,8 +217,8 @@ version = "4.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d04704f56c2cde07f43e8e2c154b43f216dc5c92fc98ada720177362f953b956" dependencies = [ - "clap_builder", - "clap_derive", + "clap_builder", + "clap_derive", ] [[package]] @@ -227,10 +227,10 @@ version = "4.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0e231faeaca65ebd1ea3c737966bf858971cd38c3849107aa3ea7de90a804e45" dependencies = [ - "anstream", - "anstyle", - "clap_lex", - "strsim", + "anstream", + "anstyle", + "clap_lex", + "strsim", ] [[package]] @@ -239,10 +239,10 @@ version = "4.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0862016ff20d69b84ef8247369fabf5c008a7417002411897d40ee1f4532b873" dependencies = [ - "heck", - "proc-macro2", - "quote", - "syn 2.0.37", + "heck", + "proc-macro2", + "quote", + "syn 2.0.37", ] [[package]] @@ -263,7 +263,7 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ff10625fd0ac447827aa30ea8b861fead473bb60aeb73af6c1c58caf0d1299" dependencies = [ - "custom_derive", + "custom_derive", ] [[package]] @@ -272,7 +272,7 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" dependencies = [ - "cfg-if", + "cfg-if", ] [[package]] @@ -281,8 +281,8 @@ version = "0.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200" dependencies = [ - "cfg-if", - "crossbeam-utils", + "cfg-if", + "crossbeam-utils", ] [[package]] @@ -291,8 +291,8 @@ version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d1cfb3ea8a53f37c40dea2c7bedcbd88bdfae54f5e2175d6ecaff1c988353add" dependencies = [ - "cfg-if", - "crossbeam-utils", + "cfg-if", + "crossbeam-utils", ] [[package]] @@ -301,7 +301,7 @@ version = "0.8.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294" dependencies = [ - "cfg-if", + "cfg-if", ] [[package]] @@ -316,11 +316,11 @@ version = "5.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856" dependencies = [ - "cfg-if", - "hashbrown", - "lock_api", - "once_cell", - "parking_lot_core", + "cfg-if", + "hashbrown", + "lock_api", + "once_cell", + "parking_lot_core", ] [[package]] @@ -341,9 +341,9 @@ version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "add4f07d43996f76ef320709726a556a9d4f965d9410d8d0271132d2f8293480" dependencies = [ - "errno-dragonfly", - "libc", - "windows-sys", + "errno-dragonfly", + "libc", + "windows-sys", ] [[package]] @@ -352,8 +352,8 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" dependencies = [ - "cc", - "libc", + "cc", + "libc", ] [[package]] @@ -374,8 +374,8 @@ version = "1.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c6c98ee8095e9d1dcbf2fcc6d95acccb90d1c81db1e44725c6a984b1dbdfb010" dependencies = [ - "crc32fast", - "miniz_oxide", + "crc32fast", + "miniz_oxide", ] [[package]] @@ -384,17 +384,17 @@ version = "0.22.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c76a80dd14a27fc3d8bc696502132cb52b3f227256fd8601166c3a35e45f409" dependencies = [ - "ansi_term", - "atty", - "crossbeam-channel", - "crossbeam-queue", - "glob", - "lazy_static", - "log", - "regex", - "rustversion", - "thiserror", - "time", + "ansi_term", + "atty", + "crossbeam-channel", + "crossbeam-queue", + "glob", + "lazy_static", + "log", + "regex", + "rustversion", + "thiserror", + "time", ] [[package]] @@ -403,13 +403,13 @@ version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23342abe12aba583913b2e62f22225ff9c950774065e4bfb61a19cd9770fec40" dependencies = [ - "futures-channel", - "futures-core", - "futures-executor", - "futures-io", - "futures-sink", - "futures-task", - "futures-util", + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", ] [[package]] @@ -418,8 +418,8 @@ version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "955518d47e09b25bbebc7a18df10b81f0c766eaf4c4f1cccef2fca5f2a4fb5f2" dependencies = [ - "futures-core", - "futures-sink", + "futures-core", + "futures-sink", ] [[package]] @@ -434,9 +434,9 @@ version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ccecee823288125bd88b4d7f565c9e58e41858e47ab72e8ea2d64e93624386e0" dependencies = [ - "futures-core", - "futures-task", - "futures-util", + "futures-core", + "futures-task", + "futures-util", ] [[package]] @@ -463,15 +463,15 @@ version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533" dependencies = [ - "futures-channel", - "futures-core", - "futures-io", - "futures-sink", - "futures-task", - "memchr", - "pin-project-lite", - "pin-utils", - "slab", + "futures-channel", + "futures-core", + "futures-io", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", ] [[package]] @@ -480,9 +480,9 @@ version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" dependencies = [ - "cfg-if", - "libc", - "wasi", + "cfg-if", + "libc", + "wasi", ] [[package]] @@ -509,7 +509,7 @@ version = "0.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" dependencies = [ - "libc", + "libc", ] [[package]] @@ -518,7 +518,7 @@ version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5444c27eef6923071f7ebcc33e3444508466a76f7a2b93da00ed6e19f30c1ddb" dependencies = [ - "windows-sys", + "windows-sys", ] [[package]] @@ -527,8 +527,8 @@ version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8adf3ddd720272c6ea8bf59463c04e0f93d0bbf7c5439b691bca2987e0270897" dependencies = [ - "equivalent", - "hashbrown", + "equivalent", + "hashbrown", ] [[package]] @@ -537,7 +537,7 @@ version = "0.10.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" dependencies = [ - "either", + "either", ] [[package]] @@ -570,8 +570,8 @@ version = "3.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce826c243048e3d5cec441799724de52e2d42f820468431fc3fceee2341871e2" dependencies = [ - "libc", - "libffi-sys", + "libc", + "libffi-sys", ] [[package]] @@ -580,7 +580,7 @@ version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f36115160c57e8529781b4183c2bb51fdc1f6d6d1ed345591d84be7703befb3c" dependencies = [ - "cc", + "cc", ] [[package]] @@ -589,8 +589,8 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b67380fd3b2fbe7527a606e18729d21c6f3951633d0500574c4dc22d2d638b9f" dependencies = [ - "cfg-if", - "winapi", + "cfg-if", + "winapi", ] [[package]] @@ -605,8 +605,8 @@ version = "0.4.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1cc9717a20b1bb222f333e6a92fd32f7d8a18ddc5a3191a11af45dcbf4dcd16" dependencies = [ - "autocfg", - "scopeguard", + "autocfg", + "scopeguard", ] [[package]] @@ -627,7 +627,7 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d61c719bcfbcf5d62b3a09efa6088de8c54bc0bfcd3ea7ae39fcc186108b8de1" dependencies = [ - "autocfg", + "autocfg", ] [[package]] @@ -636,7 +636,7 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "62d65169fd55c134e5498444453fe968349421e7248e4d6547c6846e0082c397" dependencies = [ - "metis-sys", + "metis-sys", ] [[package]] @@ -645,7 +645,7 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cc898b11f9da2e428afa54b5f092dac7edd3f87281084d789b9a9a15b6f81853" dependencies = [ - "bindgen 0.66.1", + "bindgen 0.66.1", ] [[package]] @@ -660,7 +660,7 @@ version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e7810e0be55b428ada41041c41f32c9f1a42817901b4ccf45fa3d4b6561e74c7" dependencies = [ - "adler", + "adler", ] [[package]] @@ -668,14 +668,14 @@ name = "mpi" version = "0.6.0" source = "git+https://github.com/rsmpi/rsmpi?rev=1622101c9fc3a78620e9649f7f895616289d3558#1622101c9fc3a78620e9649f7f895616289d3558" dependencies = [ - "build-probe-mpi", - "conv", - "libffi", - "memoffset", - "mpi-sys", - "once_cell", - "smallvec", - "thiserror", + "build-probe-mpi", + "conv", + "libffi", + "memoffset", + "mpi-sys", + "once_cell", + "smallvec", + "thiserror", ] [[package]] @@ -683,9 +683,9 @@ name = "mpi-sys" version = "0.2.0" source = "git+https://github.com/rsmpi/rsmpi?rev=1622101c9fc3a78620e9649f7f895616289d3558#1622101c9fc3a78620e9649f7f895616289d3558" dependencies = [ - "bindgen 0.65.1", - "build-probe-mpi", - "cc", + "bindgen 0.65.1", + "build-probe-mpi", + "cc", ] [[package]] @@ -700,8 +700,8 @@ version = "7.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" dependencies = [ - "memchr", - "minimal-lexical", + "memchr", + "minimal-lexical", ] [[package]] @@ -710,8 +710,8 @@ version = "0.46.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" dependencies = [ - "overload", - "winapi", + "overload", + "winapi", ] [[package]] @@ -720,7 +720,7 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44" dependencies = [ - "libc", + "libc", ] [[package]] @@ -741,8 +741,8 @@ version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" dependencies = [ - "lock_api", - "parking_lot_core", + "lock_api", + "parking_lot_core", ] [[package]] @@ -751,11 +751,11 @@ version = "0.9.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "93f00c865fe7cabf650081affecd3871070f26767e7b2070a3ffae14c654b447" dependencies = [ - "cfg-if", - "libc", - "redox_syscall", - "smallvec", - "windows-targets", + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets", ] [[package]] @@ -770,8 +770,8 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1d3afd2628e69da2be385eb6f2fd57c8ac7977ceeff6dc166ff1657b0e386a9" dependencies = [ - "fixedbitset", - "indexmap", + "fixedbitset", + "indexmap", ] [[package]] @@ -804,8 +804,8 @@ version = "0.1.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c8646e95016a7a6c4adea95bafa8a16baab64b583356217f2c85db4a39d9a86" dependencies = [ - "proc-macro2", - "syn 1.0.109", + "proc-macro2", + "syn 1.0.109", ] [[package]] @@ -814,8 +814,8 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae005bd773ab59b4725093fd7df83fd7892f7d8eafb48dbd7de6e024e4215f9d" dependencies = [ - "proc-macro2", - "syn 2.0.37", + "proc-macro2", + "syn 2.0.37", ] [[package]] @@ -824,7 +824,7 @@ version = "1.0.67" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d433d9f1a3e8c1263d9456598b16fec66f4acc9a74dacffd35c7bb09b3a1328" dependencies = [ - "unicode-ident", + "unicode-ident", ] [[package]] @@ -833,8 +833,8 @@ version = "0.11.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b82eaa1d779e9a4bc1c3217db8ffbeabaae1dca241bf70183242128d48681cd" dependencies = [ - "bytes", - "prost-derive", + "bytes", + "prost-derive", ] [[package]] @@ -843,20 +843,20 @@ version = "0.11.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "119533552c9a7ffacc21e099c24a0ac8bb19c2a2a3f363de84cd9b844feab270" dependencies = [ - "bytes", - "heck", - "itertools", - "lazy_static", - "log", - "multimap", - "petgraph", - "prettyplease 0.1.25", - "prost", - "prost-types", - "regex", - "syn 1.0.109", - "tempfile", - "which", + "bytes", + "heck", + "itertools", + "lazy_static", + "log", + "multimap", + "petgraph", + "prettyplease 0.1.25", + "prost", + "prost-types", + "regex", + "syn 1.0.109", + "tempfile", + "which", ] [[package]] @@ -865,11 +865,11 @@ version = "0.11.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5d2d8d10f3c6ded6da8b05b5fb3b8a5082514344d56c9f871412d29b4e075b4" dependencies = [ - "anyhow", - "itertools", - "proc-macro2", - "quote", - "syn 1.0.109", + "anyhow", + "itertools", + "proc-macro2", + "quote", + "syn 1.0.109", ] [[package]] @@ -878,7 +878,7 @@ version = "0.11.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "213622a1460818959ac1181aaeb2dc9c7f63df720db7d788b3e24eacd1983e13" dependencies = [ - "prost", + "prost", ] [[package]] @@ -887,7 +887,7 @@ version = "1.1.0+21.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c7ac8852baeb3cc6fb83b93646fb93c0ffe5d14bf138c945ceb4b9948ee0e3c1" dependencies = [ - "autotools", + "autotools", ] [[package]] @@ -896,8 +896,8 @@ version = "0.23.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "11bafc859c6815fbaffbbbf4229ecb767ac913fecb27f9ad4343662e9ef099ea" dependencies = [ - "memchr", - "serde", + "memchr", + "serde", ] [[package]] @@ -906,7 +906,7 @@ version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" dependencies = [ - "proc-macro2", + "proc-macro2", ] [[package]] @@ -915,9 +915,9 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ - "libc", - "rand_chacha", - "rand_core", + "libc", + "rand_chacha", + "rand_core", ] [[package]] @@ -926,8 +926,8 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" dependencies = [ - "ppv-lite86", - "rand_core", + "ppv-lite86", + "rand_core", ] [[package]] @@ -936,7 +936,7 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" dependencies = [ - "getrandom", + "getrandom", ] [[package]] @@ -945,7 +945,7 @@ version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29" dependencies = [ - "bitflags 1.3.2", + "bitflags 1.3.2", ] [[package]] @@ -954,10 +954,10 @@ version = "1.9.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ebee201405406dbf528b8b672104ae6d6d63e6d118cb10e4d51abbc7b58044ff" dependencies = [ - "aho-corasick", - "memchr", - "regex-automata", - "regex-syntax", + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", ] [[package]] @@ -966,9 +966,9 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "59b23e92ee4318893fa3fe3e6fb365258efbfe6ac6ab30f090cdcbb7aa37efa9" dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", + "aho-corasick", + "memchr", + "regex-syntax", ] [[package]] @@ -981,26 +981,26 @@ checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da" name = "rust_q_sim" version = "0.1.0" dependencies = [ - "bytes", - "clap", - "flate2", - "flexi_logger", - "itertools", - "log", - "metis", - "mpi", - "prost", - "prost-build", - "protobuf-src", - "quick-xml", - "rand", - "serde", - "serde_json", - "serial_test", - "tracing", - "tracing-appender", - "tracing-subscriber", - "wait-timeout", + "bytes", + "clap", + "flate2", + "flexi_logger", + "itertools", + "log", + "metis", + "mpi", + "prost", + "prost-build", + "protobuf-src", + "quick-xml", + "rand", + "serde", + "serde_json", + "serial_test", + "tracing", + "tracing-appender", + "tracing-subscriber", + "wait-timeout", ] [[package]] @@ -1015,11 +1015,11 @@ version = "0.38.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2f9da0cbd88f9f09e7814e388301c8414c51c62aa6ce1e4b5c551d49d96e531" dependencies = [ - "bitflags 2.4.0", - "errno", - "libc", - "linux-raw-sys", - "windows-sys", + "bitflags 2.4.0", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", ] [[package]] @@ -1046,7 +1046,7 @@ version = "1.0.188" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e" dependencies = [ - "serde_derive", + "serde_derive", ] [[package]] @@ -1055,9 +1055,9 @@ version = "1.0.188" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2" dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.37", + "proc-macro2", + "quote", + "syn 2.0.37", ] [[package]] @@ -1066,9 +1066,9 @@ version = "1.0.107" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b420ce6e3d8bd882e9b243c6eed35dbc9a6110c9769e74b584e0d68d1f20c65" dependencies = [ - "itoa", - "ryu", - "serde", + "itoa", + "ryu", + "serde", ] [[package]] @@ -1077,12 +1077,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "538c30747ae860d6fb88330addbbd3e0ddbe46d662d032855596d8a8ca260611" dependencies = [ - "dashmap", - "futures", - "lazy_static", - "log", - "parking_lot", - "serial_test_derive", + "dashmap", + "futures", + "lazy_static", + "log", + "parking_lot", + "serial_test_derive", ] [[package]] @@ -1091,9 +1091,9 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "079a83df15f85d89a68d64ae1238f142f172b1fa915d0d76b26a7cba1b659a69" dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", + "proc-macro2", + "quote", + "syn 1.0.109", ] [[package]] @@ -1102,7 +1102,7 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1b21f559e07218024e7e9f90f96f601825397de0e25420135f7f952453fed0b" dependencies = [ - "lazy_static", + "lazy_static", ] [[package]] @@ -1123,7 +1123,7 @@ version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" dependencies = [ - "autocfg", + "autocfg", ] [[package]] @@ -1144,9 +1144,9 @@ version = "1.0.109" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", + "proc-macro2", + "quote", + "unicode-ident", ] [[package]] @@ -1155,9 +1155,9 @@ version = "2.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7303ef2c05cd654186cb250d29049a24840ca25d2747c25c0381c8d9e2f582e8" dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", + "proc-macro2", + "quote", + "unicode-ident", ] [[package]] @@ -1166,11 +1166,11 @@ version = "3.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb94d2f3cc536af71caac6b6fcebf65860b347e7ce0cc9ebe8f70d3e521054ef" dependencies = [ - "cfg-if", - "fastrand", - "redox_syscall", - "rustix", - "windows-sys", + "cfg-if", + "fastrand", + "redox_syscall", + "rustix", + "windows-sys", ] [[package]] @@ -1179,7 +1179,7 @@ version = "1.0.49" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1177e8c6d7ede7afde3585fd2513e611227efd6481bd78d2e82ba1ce16557ed4" dependencies = [ - "thiserror-impl", + "thiserror-impl", ] [[package]] @@ -1188,9 +1188,9 @@ version = "1.0.49" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "10712f02019e9288794769fba95cd6847df9874d49d871d062172f9dd41bc4cc" dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.37", + "proc-macro2", + "quote", + "syn 2.0.37", ] [[package]] @@ -1199,8 +1199,8 @@ version = "1.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152" dependencies = [ - "cfg-if", - "once_cell", + "cfg-if", + "once_cell", ] [[package]] @@ -1209,10 +1209,10 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2702e08a7a860f005826c6815dcac101b19b5eb330c27fe4a5928fec1d20ddd" dependencies = [ - "itoa", - "libc", - "num_threads", - "time-macros", + "itoa", + "libc", + "num_threads", + "time-macros", ] [[package]] @@ -1227,10 +1227,10 @@ version = "0.1.37" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8" dependencies = [ - "cfg-if", - "pin-project-lite", - "tracing-attributes", - "tracing-core", + "cfg-if", + "pin-project-lite", + "tracing-attributes", + "tracing-core", ] [[package]] @@ -1239,9 +1239,9 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09d48f71a791638519505cefafe162606f706c25592e4bde4d97600c0195312e" dependencies = [ - "crossbeam-channel", - "time", - "tracing-subscriber", + "crossbeam-channel", + "time", + "tracing-subscriber", ] [[package]] @@ -1250,9 +1250,9 @@ version = "0.1.26" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab" dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.37", + "proc-macro2", + "quote", + "syn 2.0.37", ] [[package]] @@ -1261,8 +1261,8 @@ version = "0.1.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0955b8137a1df6f1a2e9a37d8a6656291ff0297c1a97c24e0d8425fe2312f79a" dependencies = [ - "once_cell", - "valuable", + "once_cell", + "valuable", ] [[package]] @@ -1271,9 +1271,9 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ddad33d2d10b1ed7eb9d1f518a5674713876e97e5bb9b7345a7984fbb4f922" dependencies = [ - "lazy_static", - "log", - "tracing-core", + "lazy_static", + "log", + "tracing-core", ] [[package]] @@ -1282,8 +1282,8 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc6b213177105856957181934e4920de57730fc69bf42c37ee5bb664d406d9e1" dependencies = [ - "serde", - "tracing-core", + "serde", + "tracing-core", ] [[package]] @@ -1292,15 +1292,15 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "30a651bc37f915e81f087d86e62a18eec5f79550c7faff886f7090b4ea757c77" dependencies = [ - "nu-ansi-term", - "serde", - "serde_json", - "sharded-slab", - "smallvec", - "thread_local", - "tracing-core", - "tracing-log", - "tracing-serde", + "nu-ansi-term", + "serde", + "serde_json", + "sharded-slab", + "smallvec", + "thread_local", + "tracing-core", + "tracing-log", + "tracing-serde", ] [[package]] @@ -1327,7 +1327,7 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6" dependencies = [ - "libc", + "libc", ] [[package]] @@ -1342,10 +1342,10 @@ version = "4.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7" dependencies = [ - "either", - "home", - "once_cell", - "rustix", + "either", + "home", + "once_cell", + "rustix", ] [[package]] @@ -1354,8 +1354,8 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", ] [[package]] @@ -1376,7 +1376,7 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" dependencies = [ - "windows-targets", + "windows-targets", ] [[package]] @@ -1385,13 +1385,13 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", ] [[package]] diff --git a/assets/3-links/3-agent.xml b/assets/3-links/3-agent.xml index d411e391..ce5e7c73 100644 --- a/assets/3-links/3-agent.xml +++ b/assets/3-links/3-agent.xml @@ -18,7 +18,7 @@ link1 link2 link3 + distance="1200" vehicleRefId="200_bike">link1 link2 link3 diff --git a/assets/3-links/vehicle_definitions.xml b/assets/3-links/vehicle_definitions.xml deleted file mode 100644 index 7b191b5f..00000000 --- a/assets/3-links/vehicle_definitions.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/assets/3-links/vehicles.xml b/assets/3-links/vehicles.xml new file mode 100644 index 00000000..ac2e7a6a --- /dev/null +++ b/assets/3-links/vehicles.xml @@ -0,0 +1,38 @@ + + + + + + abc + + + + + + + + + + + This is a bike + + + + + + + + + + teleported + + This is a pair of shoes + + + + + + + + \ No newline at end of file diff --git a/assets/equil/equil-1-plan.xml b/assets/equil/equil-1-plan.xml index e3a0ad79..29036d70 100644 --- a/assets/equil/equil-1-plan.xml +++ b/assets/equil/equil-1-plan.xml @@ -3,75 +3,87 @@ - - Atlantis - + + Atlantis + - + - - - {"car":"1"} - - - - - - - car - - 1 6 15 20 - - - - - - car - - 20 - - - - - - car - - 20 21 22 23 1 - - - - + + + {"car":"1"} + + + + + + + car + + 1 6 15 20 + + + + + + + car + + 20 + + + + + + + car + + 20 21 22 23 1 + + + + + - - - - - - car - - 1 8 17 20 - - - - - - car - - 20 - - - - - - car - - 20 21 22 23 1 - - - - + + + + + + car + + 1 8 17 20 + + + + + + + car + + 20 + + + + + + + car + + 20 21 22 23 1 + + + + + - + diff --git a/assets/equil/equil-plans.xml.gz b/assets/equil/equil-plans.xml.gz index cfcf554de7475addd100f720e9516d176e88e866..82353d994b3fccc8c6ec41cd37727590a1143015 100644 GIT binary patch literal 5494 zcmXw-c|4Te|HhTwQ?{aPjh(C^sYb?HjU}>-iLwk*)+`~$z6_@9`(F03B~3(QB$Ry} zLX0iD#+0#)G4q?A=li?=yzlcm=RTivo%eN}13sY>fY9@29j&5X6sKsa;?9GqraQHg!?&t)V;OVe~@2Xl<~9JjccS8A{WoorAB z$>fdc)$NUwk0*KxpwT)B*(ge3~mZ*d-y5XFF2ImAbFgtvPPrwLW4DE5`YQ9z=_KF|Iq;8s$eW9JA=_cV2J7+}M7TlU5Y!*TvVp~6Rap*8q52G5N*EAippoXja~aLWUnn z-e+?>eh-JYpJ_H7obDey+K1_CvA?FaJ^ax=Hg2l?{P<@+X{&p-zi9_ygQpH2w72Nk ze>AYpCTNhHPK-`^z2U1QDNx%5`}tSo!X0E?c-y-j7o1Qy<_2cw)2Ftcou5C#Qa58g z>hFOl0nZ7_LzAFF``Y0wWYZtA9tm6w=V@wZQ>6y> zuzA+pyUW^}q+IMp+Siy05&7cUOy1dOP#&JQ$2D|?tQ-3Eq8Cgfpg;Bg@1S8K1Grpnm}^sDmX*!uUp@zo4*cOwrSS6W*p&OSso zk=m5Qetn*s3#%+h`3_fV=UeI0u{bDOnfKiK##9o^7Gr9#`?CXy$Xs%ma-EzYHtA2Z zZMY-B>C@lK50)&Nl)?662w}Ae|-AG&$A@1>&+7e0spZ3vMrZD%;rvyBUbL;d}GFG zDVeRX^t{V|sQAYbx&U+*V5}{XqwRO47b<=`{?uuUJvkT(dis*ldl);kT3r0}-EXmC zcmAk%9k%gY3Dt-v+J_$)SlTQGRSN^c%I`LE@Ha99dVc*AOBJc>6X$obDcsNvSFYQX+9*pcvpAk9 za5$qYtbaOMX2U{iV+Gs_NxKJugII`T%0sC>{Ifb(}V4uCJnQHU|!|XMW=@V^S!LZ0%B zqHntX{Z|7Cu<{_dHAdK6Oo3fIs2AjzXQyd0WjNlyJF0*6O~TbX=T=&71qvmUZ5M42 z(`YG;`kGl!x_MPB46c6~w&W9w(>QXqG1wbJsc}*Wy!A|Y)FGlGfJru6IKm6XcGphJnD1@@t(qUa{T7?NHL~a3|J%Oj07gX9HYqeGw$ww~0dXWfa z9^dM4UdN90TcP9S8}~Y_jmE#?)5k>@Dg#cpm*p*8`R6C}Eukl}SBB#7xr^t}?Xz=G zeZbg)TV+7~H5f|sZ-E2TwmFZ$KA$ zojuH6dmCjHy>q=Dr_2`rSmIz}u71)68>edFi<^10k@#w1wxCLq2j(Q6e(LbsV#Z(vKG>d1XkbSYF_+X-t|Jho{tP z**x{16OWeooDE&t?%r^j4$U}Z1f=3yUu?JteG^o<>pw}@GgaRL6j5`pt`|S$B3DW> zeB&-*I9gojg(ZxGaIgdcvMUozG(Lw{EJR=w^OBpe(*(JFCYd&IU2GyqJAOPd6;_d^ z>QpCFu@Igm7rU%ps&_8FA?k9S|?WMsuWBi#DHSvg;hZlb@jz4YK zvo!$)m3t485NqL4EVOeoN5#7LuN&R7)%Dq&9dh{14$2_{PgxEgB3LscS*p(LGi0VZ zcjT?F?9|7xruRlA`1MUh_3jp^noY+(SohiLXacblEH~q!w2dqQ6ov`2e{4Sc%dIMV}xjyJp_Fn_2p*9 z%SPbqcV4Eb9NkT$_9S~zCSQgJ845fx>nkZVhqTyeRa(v!pKBqSx=@lXTd0Mpt(QvV z8(gDskA?7%xy|K!K$>K>t0Q;bvXW(ubQH7;825j^%Y}Dvtx}S-%>Af~qjyRowW0#{ z?)MI8pTFYsa{im<0|lS`R2v1AFIvIyo=9t+M<});GDEsGPn}iQ?zY5;13(9O{4EzD z*G^cFP{$LPwSG_E?g>1LBna9xg#v9%(0g^=%D45`H>2xuB6T;5CeY*?MlE;Q8z0v( zjXkgKD{qysiVpEr!VQ_1x7?CL&W6fqC6+fdy0Yf(<6aDLZc0Ni<;rKxl;1}O$L8vu zu6hA7v+s)UWu-VE?eA?`*-`{AQLB9@6~sN0*O^_Oe-xY8*hJbdAW)gtDSpQrrXt|9 zJ86IMAv&tf0Rx(warhahvg^tAHl_9ICa5vvV7P;f7lO~NQY?tPv_}#EUsQh6$_~Qygj4)E$7hl|WP&iq$ zdi?kaX4bKQSuJYMGoKVtAi^y8W3l)taA>#6ZbF*`1EMr8$VLf66VJQunXZ!QCH6b9 zE1P%1Ulqn5HZ8h^SNz4|ovBxh*8ndG(A;S0x-0`{CmRfgWeSCH@h`Ozf%DD1YZ&t9 z3qADVt5T^yJ^R(_N?b>dWc&)lqv=*@z;3kFgjR!Y-J1_;4W~uMJ$%>CkA~s`g^=ReC zY+&+CiN>;K#-=6aIxWU>Z5pWt$7(*kvRhpkSlcdWJe{0DeRRAJNM=La-E;Kew(;bk~*x_sEgGzWn&}wdpIQ?r$m*k zmrU#Zh;-(2Yi>2n=Y(E$&i>RpV;knVAp3xP6CEhF9Y69y6w((@w2f6CnVCqM++UnF z93psls;@OiGmz{YZHK&br+^k~fTB}|f@jR!Tq2g_7@T$E(R;&7Cy4PIST${2L3mKD z=u-#gr(Ct7y1m@7zMMR_a*X+nh2^XGjpM#@q_IF!W*VcIUD|j)3&(u3&67q-CGlAz zr2^hYoo7s2(O2I%@UcJ1mh94ys8s}?N#>jQj{NQO{=7>p&z+$gu9nvHtaG9k<$>NftZ?nY7cUsjXiVMAYgMP#&Id+lFBRnW)7sDhX?a{rDKyW-hTjk&t%Mtufebx@p!fbFqM1?td zcK>U`{%H1U?R2|H^(EZ*jzyUeygN?xgKJSKrtPVfvcZ%#X%%9rT(6r{z~P}WqVws3 zYpd|zv~Yt=_x@VnrGR9K=7FdJ^S+K=Q@k<15oMr`?j8Q?lZ(X_a@1`;ELyHJ35xL< zs(_q@Q0RFk-T$na_dMf7J~PK-Ca~Qv)2uhZa%)8cK+#yfx-8~n=GZ^-2`NS=7Jda0 zqli%Y`WKAV@EZr;4;CUHVPe+r>z?;sYli54AKjF|T(6&~HzdLEndG^eb$;~Cux7Z* zr+BS#uNOd@x&0&wwRigOl5<6h9iPiUT%m)HYJ=VF1@&Bfx6A|{#xlic$nq^Ne9!I8 zYKK?7oEgh5e64y19ikPMrW96&%OlK=y46)#lypR1LCS2*rTY^l=ekh^W$ym1+oS89 z{~kY=>Q^nyWVs)w}f4@x^>Ipg>g4}W*B9zKa6kw;S5*yyP^z5(* z>h`kF9UTK{{|}NvY$cRbVDD6arge`p*$-e%N`Jg zlZIhCn??EaW04>YxwSl%WA(YNNe)H4@V^t8UUcu;u|p4`Np6CkD;Tug8FoL?bMC-- zUhPh%3DT32znN zPYZ_^zbGj1Teeew9wlH+zhd~+#o`DPH1I#b}%IOOl?1z(Em@euGTRJ2I< zcPvv-Fmu)bAJ9$Z@rIV|ZABx%rOAH5w{~6#lAw4(KD?8QF1AU!*y0czZj#bFFA~J& ztLYUod)+n8TX$C#4l%!466L~GwNMH-$0M#gOZrrm*vK|A@@d}eWcUuh+V13eE>Hi- zGgFc> zna?3OP|WFzucHk98|9w#RH78;zf3HN`il0xPEBJXr^TS968_g_=5^HL91dv!A^NDD z9)#fGL3{T{wE>xsz0`wCZr!|h0M*^Rf7T`N7=!f( z!KQzu&f)05)fJISVoWzZ`0Rl{#YkUA}1=0yi%m2fXj&m4K~E7LZ{9nqWLnjm(DMg&MoG`cvmKZbd}RPgbV9 z0h*X88W0T52|IXtZ9Bma#4`*O=o9{W_&3$>tB(}FVa&;d6v`ckCiB$^KBV~8akd_o ztJLbwAgm7SvxkQA;sFUKGUDFvLQQ09J&J>LWm-^QU{) zoMwFd5P?`gan#Llq5I!=Ldt*SYllgTtCjDF{@M0> zy{588`b!{SZ3)b;8J+JGWzYVRufG&=Ly(&!__+QAmPg1=Qzt&~53Yp6yj1DIDPkG% zv>$fkk7#2J>POp4djuL~X@8rtdU&&@>v?^9NHEMVcyIM^dv2*;!tF4gAAgrh>p0oN zA0MZ5&^)*HDWukkp=(Y_V)Q5HtLS; zpWE9}wZFp}j#Kth{^U`(C-4zXb06?h{4$P(_<1$Q;1>e{pu7#M3xNBH_KyhgN!%D6FfHR!)c9biM^!3 z(~g3;KDf}iVy+o!z4=85q=nF)H`LS~bni27I1mQ0zo>)Q_}-=N8OkH|8~n90wa3k{ zdvM1BnL^QXAfJbW@ymmCc8dJ!KGJAkabg2twPUb%UE(f{TpH-u?$BZTxw(BZykhSc zo;J_VxpF;aWr;FZ9{wQ$g+D~#J|7R@3VPp~mL3o9$5%(dJZ8|ALPw;fBln%|(Aq3ib%o2RF`1# zuFs;c`t;GlITPnJ0{Fddv~JjJ#xd4uE3==n_FGB+$>OrdPswUV7s*JFxp2EsbxqBS zm~ny&Z6ChTdfzjQ&6@ z=-$q!E;5o$_K8G%q4dY`b9Y56J$5RbcP8icG*~WO0~sLhKiz>$skUIfCsl3h4uzB7 z#GW;zO(5D^M@rFL);B{gg5T@cpX05k3wlzxgP^wQxpBee5W@kB?eThh>3-k!`%p04 zeOV?D2RQ0W#uh5wZ`;*gL{NfFO^9JRd1!zebIeL3V{Qv7QgXx>#dUzsi59~2`~cIw zR*6O8BZZm=6BRH7aQDH)O5|DpA6K*eLy>uAiRU#9JG-Xr=ESe(G`t-GiuqqrNvhMk z@N-}O8J_{GpczZc87pJy_0nFzzCa0=_N3OQg_);)jdPQ@&KsF*;yRQ~k|JD;W~^Wc zPpBDb_Og%>lmTL1dYbf!5mV1O8vwo-QCLT}2)~>3R2<@7gG;z{%aoIZ`$@D3ebHZ| ztx68xh2-NB`T+<5uz2O!_{E$`zMd5j&A2M|3ea*VnEku1$V$n#q#UCPCC95vix1CP zsFO1q3m-Qhm1V<;;vErR8TvWd8{NI`c|XU{uhiH^65YBi=zE%TCAoT-w5cDTc5vXY zHt`gGQE@InMC<9~Rw>De=s%qBk6J&wn*Z6KufhRbLyt5eJ@ZNybCwoR2BdosAZ`Q> zq!p&;ekba&sD4zJmHAZATI zLs?)C!3(UFKZtpvP%}c`;K#1?Aru{6V)y_}**%Iua4P9OpfeZU` z6G{@ibPgR#YiJ1O0kYHO09<;qV}~XNU!X4htpMLh;X{4Vrp)~bO5@0cd)N6w?oh3S zxgT89j12oqeKplo>TCJmR`CehfB}{_J%UzV1{e`?f7E68O0a=U+AUc@N=3OWV%is` z4F@r3+O72|Xua$xf?ABqHHfLq;VANQA=;~_b=B)^y`KO5VIGw$X{D}UDeq~fI7)AE z-+(R^q(Q%J)RNH)Kzdd70C+60EIk?kIWevM=f$DT!t-zsl)V733gzdI+&Y~PuSl=4 zm|LIiE#oiaQkG0eZ;k%#LxT;H4*HM=Szp`6zZsLexy+|K7nDgmz+#EcUmfQ8e$AaI zkk6B)u-_Kr+pU>_L_4SizGN)o?-I#CHGg%X|3B?9Z%~?mBd=&9cNrB-R0g z&JEi%oKGL*#Evtv*NA`gSBx!rmi4Z&2S0|s{DruxmOFCC2GY9{&lDZS1USz@7oFzX znL9}&U5jN-!$c?SbXr6Yd{dP*Q?`UIM~z<+73NPePK9+lR{{tMvYCMIC)Ph-Z|2{A zd+9cxYLv~LMPKFF#1uZRP&f{KDJ0PXx2%{7`}TTm5MUn1ZQaNlbCG7Dx6z}xOrX)#y8(==l0w)>g^A{W9_QOuJ! zrFisgIG-GL8_8QL1XN@MR)B8PDZY6x%qKidPu<4-6}@s>X`IEoaOfBSS`=4wS<&LA zJm9SHrP5oMO!>6mDVA|-8`s5MDd1Wd*Lv#}h2UV#_WUHP;LW07t^I8n_`JL*33mso z-@6s@xm6@cg%59?RT zI(6#TAB%$(t#7vKGY|DC(v44YRMh;`HYsr;+R-{^|(OQR+onJmp? zj)}5PyXoQo2@}Y6nZhm%5q~UH?S7pJx$k?boz}~y3=wb=*-l!S4%81$on1z52P5&7 z!p+`})O(M@GAN+TpwG4}Hqw4^jX zCM=lbjxX4W1o~g8doj#Vqa{oLi^Zj@&4D%pr%F}YOhe54D8-oE3e6uq3B*6lH-l!4w)TZ1>=be1f*8I3!hlu|sr6_xG&&=2uo1d3kK`J)M>AGK6(*+n?#Y80aigBgpxX4r1C4=*X1tz8 zmyKxeg--dc^&E0OdDTn5t#=u@6L`w?_dIYE*;v8rndz*eiqp*+Ie4tCNBx4N4#AvG zPQsme`2Jf2RhBQ)Ckz^=`fnFUwvgdHo@AVBga_jhPqS|8nEZEqU3#Tob zWfcl@706>_Uy;XF-E9|Xa)|k@F-bimU4WzVY6(YQ7uzR?ljl9nvN~zcm9KF!miS{ z-1$-O!Qa)}z8smYH;90yAFP_GMSmhe8P}=SN*l~7DVwF)|93k&LM0u*XqST=gLfd#Ay)cPUhsYBICbU%NDdo|T&V z%$`^j?LXw~dKgcr7-G;jXNNxN)*#oe@&>3Ybi zGsQG9k(Gq#+OcQO%14XJEK3;m=7&_JCvo0y!ve=pEi)Ak)Ccdzm$p=azQd3~U}QCENup|@f~;v5X0CuJZm|$bO4y{20ap|C*JB z07*%_pxh0=DINZ4EFCxV4O*QIDe8=pg-a~XW%zBmhOL7wjrtT-EI6QM&L9JBHc>yH zKLO26=n7~VK|83Tu1|pP>E&*7ZT)}v>a04%fD=-jAFzVN^;F5RJa)_YJ6nQM%ge#X zbGl_AdbM(dD7$;AovSsEY5-PwDV_64DmkOn1ZH03h8*GN_38@0P(Ym7bFCoCu}OqP zdkfIqcdL2yfE!gX>OJKA^M|t3r!st;NP?Dfe8SURh+rGe`GH)Bi2N0kuPgED#rQ;= zlYu&phawFgmk&V*a?YwO76V~v+%5^R8WbhbZ2Ose@*#y=MJ~JA3`83SVH}Ar*ttBF zj$6E_=HcGIRKPq?)dN@z@XF}zBDY3EKSG|Bw?=C(qHGvUvCIo7`kSDh1UWe8tGuC= ze3NRI-McGe?5)v!wWQ>Y9oK-%3*~T%zNM&{ zepPaSd%o*zziNf(q7uutTH}bhbOB=e$k>0Yn11`i^ScL;)aLMJlinp-cuf-aps9%Z z9xERw0GWJ#n?gX&E+0($7orI}N(v#i{7(IN{|3qLIqS`c`z_zHd%LO|ti5hZ|4m~_ zVY$hnt!ob&{0e(Z(h;2O9d7L88|ZFdcCP08HS48kxlSKS##4w4VZXq9O_`NlouF_tQXVAIz zJ=_!Ae{lW1WCw=nhqlGAd5t2s-Ql%UXLfN&?X|wvg&0xs9X<6hW=%i$!fT~yka>rV z$EiY)qb+*O#(j!Sx$pC(5x09RDd!CT!4y1kBqzLVtqb7cvc=8%^q7|m4VybX$d^FDOjAp z!^2Z&_MQN|UH6=hV+i7xX)$~1mrzaYV*rn@s7PUbgC#`xkJ*daT;qkwLFpeY$KV}B73uJ%n?!2pBnsn><`%dZs~-E*H?P|`|{N=@rnJBPuzz6xv=uK2*uGRU>M;K?Yg0j?0uj^=-Tuod4sFS&7#hfHP@1Oml5N-Br=sUA_plvF>GP~rr~dnq SIY`2tsOJ_c+c@wu(ET3^gZ}aW diff --git a/assets/equil/equil-vehicles.xml b/assets/equil/equil-vehicles.xml new file mode 100644 index 00000000..cc482218 --- /dev/null +++ b/assets/equil/equil-vehicles.xml @@ -0,0 +1,27 @@ + + + + + + abc + + + + + + + + + + teleported + + This is a pair of shoes + + + + + + + + \ No newline at end of file diff --git a/src/simulation/id.rs b/src/simulation/id.rs index 99cba5c7..26347098 100644 --- a/src/simulation/id.rs +++ b/src/simulation/id.rs @@ -1,3 +1,4 @@ +use std::slice::Iter; use std::{collections::HashMap, hash::Hash, hash::Hasher, marker::PhantomData, rc::Rc}; pub type Id = Rc>; @@ -100,10 +101,18 @@ impl<'ext, T> IdStore<'ext, T> { .unwrap_or_else(|| panic!("Could not find id for external id: {external}")); self.ids.get(*index).unwrap().clone() } + + pub fn exists(&self, id: Id) -> bool { + self.ids.get(id.internal).is_some() + } + + pub fn iter(&self) -> Iter<'_, Id> { + self.ids.iter() + } } #[cfg(test)] -mod tets { +mod tests { use super::{Id, IdImpl, IdStore}; #[test] diff --git a/src/simulation/population/population.rs b/src/simulation/population/population.rs index 1ba3c89b..21f686bd 100644 --- a/src/simulation/population/population.rs +++ b/src/simulation/population/population.rs @@ -1,10 +1,11 @@ -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use crate::simulation::id::{Id, IdStore}; -use crate::simulation::io::population::{IOPerson, IOPlanElement, IOPopulation, IORoute}; +use crate::simulation::io::population::{IOPerson, IOPlanElement, IOPopulation}; use crate::simulation::messaging::messages::proto::Agent; use crate::simulation::network::global_network::{Link, Network}; use crate::simulation::vehicles::garage::Garage; +use crate::simulation::vehicles::vehicle_type::VehicleType; type ActType = (); @@ -38,23 +39,100 @@ impl<'p> Population<'p> { ) -> Self { let mut result = Population::new(); - // first pass to set ids globally - for io in io_population.persons.iter() { - Self::agent_id(io, &mut result, garage); + // create person ids, and vehicles for each person + Self::create_ids(&mut result, io_population, garage); + // create the actual persons for this partition + Self::create_persons(&mut result, io_population, network, garage, partition); + // create a vehicles for all modes for persons belonging to this partition + Self::create_vehicles(garage, &result); + + result + } + + fn create_ids(pop: &mut Population, io: &IOPopulation, garage: &mut Garage) { + // create person ids and collect strings for vehicle ids + let raw_veh: Vec<_> = io + .persons + .iter() + .map(|p| pop.agent_ids.create_id(p.id.as_str())) + .flat_map(|p_id| { + garage + .vehicle_types + .iter() + .map(move |veh_type| Self::create_veh_id_string(&p_id, &veh_type.id)) + }) + .collect(); + + // have this in a separate loop because we are iterating over garage's vehicle types and we + // can't borrow vehicle types while using a &mut in add_veh. + for veh_id in raw_veh { + garage.add_veh_id(veh_id.as_str()); } - // then copy the agents on this partition - for io in io_population.persons.iter() { - let link = Self::link_first_act(io, network); - if partition == link.partition { - let agent = Agent::from_io(io, network, &result, garage); - result - .agents - .insert(result.agent_ids.get(agent.id as usize), agent); - } + // now iterate over all plans to extract activity ids + let types: HashSet<_> = io + .persons + .iter() + .flat_map(|person| person.plans.iter()) + .flat_map(|plan| plan.elements.iter()) + .filter_map(|element| match element { + IOPlanElement::Activity(a) => Some(a), + IOPlanElement::Leg(_) => None, + }) + .map(|act| &act.r#type) + .collect(); + + for act_type in types { + pop.act_types.create_id(act_type.as_str()); } + } - result + fn create_persons( + result: &mut Population, + io_population: &IOPopulation, + net: &Network, + garage: &Garage, + part: usize, + ) { + let persons: Vec<_> = io_population + .persons + .iter() + .filter(|io_p| Self::is_partition(io_p, net, part)) + .map(|io_p| Agent::from_io(io_p, net, result, garage)) + .collect(); + + for person in persons { + let person_id = result.agent_ids.get_from_wire(person.id); + result.agents.insert(person_id, person); + } + } + + fn create_vehicles(garage: &mut Garage, pop: &Population) { + let person_mode: Vec<_> = pop + .agents + .keys() + .flat_map(|p_id| { + garage + .vehicle_types + .iter() + .map(move |veh_type| (p_id, veh_type.id.clone())) + }) + .collect(); + + for (person_id, veh_type) in person_mode { + let vehicle_id_ext = Self::create_veh_id_string(&person_id, &veh_type); + let vehicle_id = garage.vehicle_ids.get_from_ext(vehicle_id_ext.as_str()); + garage.add_veh(vehicle_id, veh_type); + } + } + + fn create_veh_id_string(person_id: &Id, veh_type: &Id) -> String { + format!("{}_{}", person_id.external, veh_type.external) + } + + fn is_partition(io_person: &IOPerson, net: &Network, partition: usize) -> bool { + let link = Self::link_first_act(io_person, net); + link.partition == partition } fn link_first_act<'n>(io: &IOPerson, net: &'n Network) -> &'n Link { @@ -66,40 +144,6 @@ impl<'p> Population<'p> { panic!("First element should be activity."); } - - fn agent_id(io: &IOPerson, pop: &mut Population, garage: &mut Garage) { - pop.agent_ids.create_id(&io.id); - for io_plan in io.plans.iter() { - for element in io_plan.elements.iter() { - match element { - IOPlanElement::Activity(a) => { - pop.act_types.create_id(&a.r#type); - } - IOPlanElement::Leg(l) => { - Self::route_ids(&l.route, garage); - } - } - } - } - } - - fn route_ids(io: &IORoute, garage: &mut Garage) { - match io.r#type.as_str() { - "links" => { - let veh_id = io - .vehicle - .as_ref() - .expect("Vehicle id is expected to be set for network route"); - match veh_id.as_str() { - "null" => (), - _ => { - garage.add_veh_id(veh_id); - } - }; - } - _t => panic!("Unsupported route type: '{_t}'"), - }; - } } #[cfg(test)] @@ -111,7 +155,7 @@ mod tests { #[test] fn from_io_1_plan() { - let mut garage = Garage::new(); + let mut garage = Garage::from_file("./assets/equil/equil-vehicles.xml"); let net = Network::from_file("./assets/equil/equil-network.xml", 1, &mut garage); let pop = Population::from_file("./assets/equil/equil-1-plan.xml", &net, &mut garage, 0); @@ -143,7 +187,7 @@ mod tests { assert!(leg.route.is_some()); if let Route::NetworkRoute(net_route) = leg.route.as_ref().unwrap() { assert_eq!( - garage.vehicle_ids.get_from_ext("1").internal as u64, + garage.vehicle_ids.get_from_ext("1_car").internal as u64, net_route.vehicle_id ); assert_eq!( @@ -160,9 +204,19 @@ mod tests { } } + #[test] + fn from_io_multi_mode() { + let mut garage = Garage::from_file("./assets/3-links/vehicles.xml"); + let net = Network::from_file("./assets/3-links/3-links-network.xml", 1, &mut garage); + let pop = Population::from_file("./assets/3-links/3-agent.xml", &net, &mut garage, 0); + + println!("{garage:#?}"); + println!("{pop:#?}"); + } + #[test] fn from_io() { - let mut garage = Garage::new(); + let mut garage = Garage::from_file("./assets/equil/equil-vehicles.xml"); let net = Network::from_file("./assets/equil/equil-network.xml", 2, &mut garage); let pop1 = Population::from_file("./assets/equil/equil-plans.xml.gz", &net, &mut garage, 0); let pop2 = Population::from_file("./assets/equil/equil-plans.xml.gz", &net, &mut garage, 1); diff --git a/src/simulation/vehicles/garage.rs b/src/simulation/vehicles/garage.rs index 5e618b40..ddbd0b18 100644 --- a/src/simulation/vehicles/garage.rs +++ b/src/simulation/vehicles/garage.rs @@ -89,7 +89,7 @@ impl<'g> Garage<'g> { assert_eq!( veh_type.id.internal, self.vehicle_types.len(), - "internal id {} and slot in node vec {} were note the same. Probably, node id {} already exsists.", + "internal id {} and slot in node vec {} were note the same. Probably, vehicle type {} already exists.", veh_type.id.internal, self.vehicle_types.len(), veh_type.id.external @@ -101,6 +101,14 @@ impl<'g> Garage<'g> { pub fn add_veh_id(&mut self, external_id: &str) -> Id { self.vehicle_ids.create_id(external_id) } + + pub fn add_veh(&mut self, veh_id: Id, veh_type: Id) { + let vehicle = GarageVehicle { + id: veh_id, + veh_type, + }; + self.vehicles.insert(vehicle.id.clone(), vehicle); + } } #[cfg(test)] diff --git a/tests/three_link_it.rs b/tests/three_link_it.rs index 90b3cc85..510022c2 100644 --- a/tests/three_link_it.rs +++ b/tests/three_link_it.rs @@ -59,7 +59,7 @@ fn test_three_link_with_routing_legs_in_plans() { fn test_three_link_one_agent_with_vehicle_definitions() { test_three_link( "assets/3-links/1-agent.xml", - Some("assets/3-links/vehicle_definitions.xml"), + Some("assets/3-links/vehicles.xml"), "static/vehicle_definitions", "tests/resources/three_link", ) @@ -75,7 +75,7 @@ fn test_three_link_one_agent_with_vehicle_definitions() { fn test_three_link_multiple_agents_with_vehicle_definitions() { test_three_link( "assets/3-links/3-agent.xml", - Some("assets/3-links/vehicle_definitions.xml"), + Some("assets/3-links/vehicles.xml"), "static/multiple_agents/no_jam", "tests/resources/three_link/multiple_agents/no_jam", ) @@ -91,7 +91,7 @@ fn test_three_link_multiple_agents_with_vehicle_definitions() { fn test_three_link_multiple_agents_with_jam_by_vehicle_definitions() { test_three_link( "assets/3-links/3-agent_jam.xml", - Some("assets/3-links/vehicle_definitions.xml"), + Some("assets/3-links/vehicles.xml"), "static/multiple_agents/with_jam", "tests/resources/three_link/multiple_agents/with_jam", ) From 4d51212d61cd11ba8dba3fec9165c542e0519c76 Mon Sep 17 00:00:00 2001 From: janekdererste Date: Thu, 5 Oct 2023 13:38:14 +0200 Subject: [PATCH 17/20] Add test for population with vehicles creation --- assets/3-links/3-agent.xml | 12 ++-- assets/vehicles/vehicles_v2.xml | 87 ------------------------- src/simulation/io/vehicles.rs | 2 +- src/simulation/population/population.rs | 30 ++++++++- src/simulation/vehicles/garage.rs | 2 +- 5 files changed, 37 insertions(+), 96 deletions(-) delete mode 100644 assets/vehicles/vehicles_v2.xml diff --git a/assets/3-links/3-agent.xml b/assets/3-links/3-agent.xml index ce5e7c73..85780ded 100644 --- a/assets/3-links/3-agent.xml +++ b/assets/3-links/3-agent.xml @@ -4,35 +4,35 @@ - + link1 link2 link3 - + - + link1 link2 link3 - + - + link1 link2 link3 - + \ No newline at end of file diff --git a/assets/vehicles/vehicles_v2.xml b/assets/vehicles/vehicles_v2.xml deleted file mode 100644 index 78615be3..00000000 --- a/assets/vehicles/vehicles_v2.xml +++ /dev/null @@ -1,87 +0,0 @@ - - - - - - - abc - 1.3 - 23.23 - parallel - 42.42 - - abc - - - - - - - - 0.23 - EURO-5 - < 1,4L - petrol - pass. car - - - - - 0.06 - - - - - - - - - - def - 2 - - - - - - - - - - - This is a small truck - - - - - - - - EURO-6 - diesel - - - - - 0.15 - 0.05 - - - - - - - - - - 1.234 - - - - - firstVehicle - - - - - diff --git a/src/simulation/io/vehicles.rs b/src/simulation/io/vehicles.rs index 43841c76..c3b6d998 100644 --- a/src/simulation/io/vehicles.rs +++ b/src/simulation/io/vehicles.rs @@ -191,7 +191,7 @@ mod test { #[test] fn test_from_file() { - let veh_def = IOVehicleDefinitions::from_file("./assets/vehicles/vehicles_v2.xml"); + let veh_def = IOVehicleDefinitions::from_file("./assets/3-links/vehicles.xml"); assert_eq!(3, veh_def.veh_types.len()); // no further assertions here, as the tests above test the individual properties. // so, this test mainly tests whether the vehicles implementation calls the xml_reader diff --git a/src/simulation/population/population.rs b/src/simulation/population/population.rs index 21f686bd..98530c69 100644 --- a/src/simulation/population/population.rs +++ b/src/simulation/population/population.rs @@ -148,6 +148,8 @@ impl<'p> Population<'p> { #[cfg(test)] mod tests { + use std::collections::HashSet; + use crate::simulation::messaging::messages::proto::leg::Route; use crate::simulation::network::global_network::Network; use crate::simulation::population::population::Population; @@ -210,7 +212,33 @@ mod tests { let net = Network::from_file("./assets/3-links/3-links-network.xml", 1, &mut garage); let pop = Population::from_file("./assets/3-links/3-agent.xml", &net, &mut garage, 0); - println!("{garage:#?}"); + // check that we have all three vehicle types + let expected_veh_types = HashSet::from(["car", "bike", "walk"]); + assert_eq!(3, garage.vehicle_types.len()); + assert!(garage + .vehicle_types + .iter() + .all(|veh_type| expected_veh_types.contains(veh_type.id.external.as_str()))); + + // check that we have a vehicle for each mode and for each person + assert_eq!(9, garage.vehicles.len()); + + // check population + // activity types should be done as id. If id is not present this will crash + assert_eq!("home", pop.act_types.get_from_ext("home").external.as_str()); + assert_eq!( + "errands", + pop.act_types.get_from_ext("errands").external.as_str() + ); + + // agents should also have ids + assert_eq!("100", pop.agent_ids.get_from_ext("100").external.as_str()); + assert_eq!("200", pop.agent_ids.get_from_ext("200").external.as_str()); + assert_eq!("300", pop.agent_ids.get_from_ext("300").external.as_str()); + + // we expect three agents overall + assert_eq!(3, pop.agents.len()); + println!("{pop:#?}"); } diff --git a/src/simulation/vehicles/garage.rs b/src/simulation/vehicles/garage.rs index ddbd0b18..553c89d3 100644 --- a/src/simulation/vehicles/garage.rs +++ b/src/simulation/vehicles/garage.rs @@ -220,7 +220,7 @@ mod tests { #[test] fn from_file() { - let garage = Garage::from_file("./assets/vehicles/vehicles_v2.xml"); + let garage = Garage::from_file("./assets/3-links/vehicles.xml"); assert_eq!(3, garage.vehicle_types.len()); } } From 0d7561568240d44647d1a6c2eac80528fb6ae93c Mon Sep 17 00:00:00 2001 From: janekdererste Date: Thu, 5 Oct 2023 16:25:11 +0200 Subject: [PATCH 18/20] WIP: put new vehicle definition into simulation as well --- src/simulation/controller.rs | 1 + src/simulation/io/attributes.rs | 16 ++ src/simulation/io/population.rs | 2 + src/simulation/messaging/messages.proto | 25 +-- src/simulation/messaging/messages.rs | 210 +++++++++++------------- src/simulation/network/link.rs | 3 +- src/simulation/population/population.rs | 40 +++-- src/simulation/simulation.rs | 104 ++++++------ src/simulation/vehicles/garage.rs | 59 +++++-- 9 files changed, 244 insertions(+), 216 deletions(-) diff --git a/src/simulation/controller.rs b/src/simulation/controller.rs index c8a4263b..567fb0bf 100644 --- a/src/simulation/controller.rs +++ b/src/simulation/controller.rs @@ -65,6 +65,7 @@ pub fn run(world: SystemCommunicator, config: Config) { let mut simulation = Simulation::new( &config, network_partition, + garage, population, message_broker, events, diff --git a/src/simulation/io/attributes.rs b/src/simulation/io/attributes.rs index d6761c23..a0d68005 100644 --- a/src/simulation/io/attributes.rs +++ b/src/simulation/io/attributes.rs @@ -23,3 +23,19 @@ pub struct Attrs { #[serde(rename = "attribute", default)] pub attributes: Vec, } + +impl Attrs { + pub fn find_or_else<'a, F>(&self, name: &str, f: F) -> &str + where + F: FnOnce() -> &'a str, + { + let opt_attr = self.attributes.iter().find(|&attr| attr.name.eq(name)); + let value = if let Some(&attr) = opt_attr { + attr.value.as_str() + } else { + f() + }; + + value + } +} diff --git a/src/simulation/io/population.rs b/src/simulation/io/population.rs index feedd163..98fb0f54 100644 --- a/src/simulation/io/population.rs +++ b/src/simulation/io/population.rs @@ -1,6 +1,7 @@ use serde::Deserialize; use tracing::info; +use crate::simulation::io::attributes::Attrs; use crate::simulation::io::matsim_id::MatsimId; use crate::simulation::io::xml_reader; @@ -42,6 +43,7 @@ pub struct IOLeg { pub dep_time: Option, pub trav_time: Option, pub route: IORoute, + pub attributes: Option, } #[derive(Debug, Deserialize, PartialEq)] diff --git a/src/simulation/messaging/messages.proto b/src/simulation/messaging/messages.proto index 5c86ff90..dd8ee880 100644 --- a/src/simulation/messaging/messages.proto +++ b/src/simulation/messaging/messages.proto @@ -54,23 +54,14 @@ message Activity { message Leg { uint64 mode = 1; - optional uint32 dep_time = 2; - optional uint32 trav_time = 3; - oneof route { - GenericRoute generic_route = 4; - NetworkRoute network_route = 5; - } + uint64 routing_mode = 2; + optional uint32 dep_time = 3; + optional uint32 trav_time = 4; + Route route = 5; } -message NetworkRoute { - // is the vehicle id important? So far this is sent inside a vehicle - uint64 vehicle_id = 1; - repeated uint64 route = 2; -} - -message GenericRoute { - uint64 start_link = 1; - uint64 end_link = 2; - uint32 trav_time = 3; - float distance = 4; +message Route { + uint64 veh_id = 1; + float distance = 2; + repeated uint64 route = 3; } \ No newline at end of file diff --git a/src/simulation/messaging/messages.rs b/src/simulation/messaging/messages.rs index 45afb084..2167ca1b 100644 --- a/src/simulation/messaging/messages.rs +++ b/src/simulation/messaging/messages.rs @@ -5,13 +5,13 @@ use std::io::Cursor; use log::debug; use prost::Message; +use crate::simulation::id::Id; use crate::simulation::io::population::{ IOActivity, IOLeg, IOPerson, IOPlan, IOPlanElement, IORoute, }; -use crate::simulation::messaging::messages::proto::leg::Route; use crate::simulation::messaging::messages::proto::{ - Activity, Agent, ExperimentalMessage, GenericRoute, Leg, NetworkRoute, Plan, - TravelTimesMessage, Vehicle, VehicleMessage, + Activity, Agent, ExperimentalMessage, Leg, Plan, Route, TravelTimesMessage, Vehicle, + VehicleMessage, }; use crate::simulation::network::global_network::Network; use crate::simulation::population::population::Population; @@ -136,25 +136,20 @@ impl Vehicle { self.curr_route_elem += 1; } + // todo I have changed the way this works. Probably one needs to call + // advance route index for teleported legs now, once the person is woken up from the activity queue pub fn curr_link_id(&self) -> Option { let leg = self.agent().curr_leg(); let route = leg.route.as_ref().unwrap(); - match route { - Route::GenericRoute(route) => Some(route.end_link as usize), - Route::NetworkRoute(route) => { - let index = self.curr_route_elem as usize; - route.route.get(index).map(|id| *id as usize) - } - } + let index = self.curr_route_elem as usize; + route.route.get(index).map(|link_id| *link_id as usize) } + // todo same as above pub fn is_current_link_last(&self) -> bool { let leg = self.agent().curr_leg(); let route = leg.route.as_ref().unwrap(); - match route { - Route::GenericRoute(_) => true, - Route::NetworkRoute(route) => self.curr_route_elem + 1 >= route.route.len() as u32, - } + self.curr_route_elem + 1 >= route.route.len() as u32 } } @@ -171,7 +166,9 @@ impl Agent { pop: &Population, garage: &Garage, ) -> Agent { - let plan = Plan::from_io(io_person.selected_plan(), net, pop, garage); + let person_id = pop.agent_ids.get_from_ext(&io_person.id); + + let plan = Plan::from_io(io_person.selected_plan(), &person_id, net, pop, garage); if plan.acts.is_empty() { debug!("There is an empty plan for person {:?}", io_person.id); @@ -184,9 +181,8 @@ impl Agent { ); } - let id = pop.agent_ids.get_from_ext(&io_person.id); Agent { - id: id.internal as u64, + id: person_id.internal as u64, plan: Some(plan), curr_plan_elem: 0, } @@ -322,38 +318,6 @@ impl Agent { .unwrap() } - pub fn update_next_leg( - &mut self, - dep_time: Option, - travel_time: Option, - route: Vec, - distance: Option, - start_link: u64, - end_link: u64, - ) { - //info!("Leg update for agent {:?}. Departure {:?}, travel time {:?}, route {:?}, distance {:?}, start_link {:?}, end_link {:?}", - // self, dep_time, travel_time, route,distance, start_link, end_link); - - let simulation_route = match route.is_empty() { - true => Route::GenericRoute(GenericRoute { - start_link, - end_link, - trav_time: travel_time.expect("No travel time set for walking leg."), - distance: distance.expect("No distance set for walking leg."), - }), - false => Route::NetworkRoute(NetworkRoute { - vehicle_id: self.id, - route, - }), - }; - - let next_leg = self.next_leg_mut(); - - next_leg.dep_time = dep_time; - next_leg.trav_time = travel_time; - next_leg.route = Some(simulation_route); - } - pub fn advance_plan(&mut self) { let next = self.curr_plan_elem + 1; if self.plan.as_ref().unwrap().acts.len() + self.plan.as_ref().unwrap().legs.len() @@ -373,13 +337,7 @@ impl EndTime for Agent { return if self.curr_plan_elem % 2 == 0 { self.curr_act().cmp_end_time(now) } else { - let route = self.curr_leg().route.as_ref().unwrap(); - match route { - Route::GenericRoute(gen_route) => now + gen_route.trav_time, - Route::NetworkRoute(_) => { - panic!("End time not supported for network route") - } - } + self.curr_leg().trav_time.unwrap() + now }; } } @@ -394,17 +352,24 @@ impl Plan { } } - fn from_io(io_plan: &IOPlan, net: &Network, pop: &Population, garage: &Garage) -> Plan { + fn from_io( + io_plan: &IOPlan, + person_id: &Id, + net: &Network, + pop: &Population, + garage: &Garage, + ) -> Plan { assert!(!io_plan.elements.is_empty()); if let IOPlanElement::Leg(_leg) = io_plan.elements.get(0).unwrap() { panic!("First plan element must be an activity! But was a leg."); }; - Plan::get_full_plan_no_routing(io_plan, net, pop, garage) + Plan::get_full_plan_no_routing(io_plan, person_id, net, pop, garage) } fn get_full_plan_no_routing( io_plan: &IOPlan, + person_id: &Id, net: &Network, pop: &Population, garage: &Garage, @@ -418,7 +383,7 @@ impl Plan { result.acts.push(act); } IOPlanElement::Leg(io_leg) => { - let leg = Leg::from_io(io_leg, net, garage); + let leg = Leg::from_io(io_leg, person_id, net, garage); result.legs.push(leg); } } @@ -488,88 +453,107 @@ impl Activity { } impl Leg { - fn from_io(io_leg: &IOLeg, net: &Network, garage: &Garage) -> Self { - let route = Route::from_io(&io_leg.route, net, garage); + fn from_io(io_leg: &IOLeg, person_id: &Id, net: &Network, garage: &Garage) -> Self { + let attrs = io_leg.attributes.as_ref().unwrap_or_default(); + let routing_mode_ext = attrs.find_or_else("routingMode", || "car"); + + let routing_mode = garage.modes.get_from_ext(routing_mode_ext); + let mode = garage.modes.get_from_ext(io_leg.mode.as_str()); + let route = Route::from_io(&io_leg.route, person_id, &mode, net, garage); + Self { route: Some(route), - mode: 0, //todo fix mode + mode: mode.internal as u64, trav_time: parse_time_opt(&io_leg.trav_time), dep_time: parse_time_opt(&io_leg.dep_time), + routing_mode: routing_mode.internal as u64, } } + // todo fix mode!! pub fn new(route: Route, mode: &str, trav_time: Option, dep_time: Option) -> Self { Self { route: Some(route), mode: 0, //todo fix mode trav_time, dep_time, + routing_mode: 0, } } } impl Route { - fn from_io(io_route: &IORoute, net: &Network, garage: &Garage) -> Self { - match io_route.r#type.as_str() { - "generic" => Route::GenericRoute(GenericRoute::from_io(io_route, net)), - "links" => { - if let Some(vehicle_id) = &io_route.vehicle { - // catch this special case because we have "null" as vehicle ids for modes which are - // routed but not simulated on the network. - if vehicle_id.eq("null") { - Route::GenericRoute(GenericRoute::from_io(io_route, net)) - } else { - Route::NetworkRoute(NetworkRoute::from_io(io_route, net, garage)) - } - } else { - panic!("vehicle id is expected to be set. ") - } - } + pub fn start_link(&self) -> u64 { + *self.route.first().unwrap() + } + + pub fn end_link(&self) -> u64 { + *self.route.last().unwrap() + } + + fn from_io( + io_route: &IORoute, + person_id: &Id, + mode: &Id, + net: &Network, + garage: &Garage, + ) -> Self { + let route = match io_route.r#type.as_str() { + "generic" => Self::from_io_generic(io_route, person_id, mode, net, garage), + "links" => Self::from_io_net_route(io_route, person_id, mode, net, garage), _t => panic!("Unsupported route type: '{_t}'"), - } + }; + + route } -} -impl GenericRoute { - fn from_io(io_route: &IORoute, net: &Network) -> Self { + fn from_io_generic( + io_route: &IORoute, + person_id: &Id, + mode: &Id, + net: &Network, + garage: &Garage, + ) -> Self { let start_link = net.link_ids.get_from_ext(&io_route.start_link); let end_link = net.link_ids.get_from_ext(&io_route.end_link); - let trav_time = parse_time_opt(&io_route.trav_time).unwrap(); + let veh_id: Id = garage.get_veh_id(person_id, mode); - Self { - start_link: start_link.internal as u64, - end_link: end_link.internal as u64, - trav_time, + Route { distance: io_route.distance, + veh_id: veh_id.internal as u64, + route: vec![start_link.internal as u64, end_link.internal as u64], } } -} -impl NetworkRoute { - fn from_io(io_route: &IORoute, net: &Network, garage: &Garage) -> Self { - let matsim_veh_id = io_route - .vehicle - .as_ref() - .unwrap_or_else(|| panic!("Couldn't find veh-id for route {io_route:?} ")); - let veh_id = garage.vehicle_ids.get_from_ext(matsim_veh_id); - - let link_ids = match &io_route.route { - None => Vec::new(), - Some(encoded_links) => encoded_links - .split(' ') - .map(|matsim_id| net.link_ids.get_from_ext(matsim_id).internal as u64) - .collect(), - }; - Self { - route: link_ids, - vehicle_id: veh_id.internal as u64, - } - } - - pub fn new(vehicle_id: u64, link_ids: Vec) -> Self { - Self { - vehicle_id, - route: link_ids, + fn from_io_net_route( + io_route: &IORoute, + person_id: &Id, + mode: &Id, + net: &Network, + garage: &Garage, + ) -> Self { + if let Some(veh_id_ext) = &io_route.vehicle { + // catch this special case because we have "null" as vehicle ids for modes which are + // routed but not simulated on the network. + if veh_id_ext.eq("null") { + Self::from_io_generic(io_route, person_id, mode, net, garage) + } else { + let veh_id = garage.vehicle_ids.get_from_ext(veh_id_ext.as_str()); + let link_ids = match &io_route.route { + None => Vec::new(), + Some(encoded_links) => encoded_links + .split(' ') + .map(|matsim_id| net.link_ids.get_from_ext(matsim_id).internal as u64) + .collect(), + }; + Route { + distance: io_route.distance, + veh_id: veh_id.internal as u64, + route: link_ids, + } + } + } else { + panic!("vehicle id is expected to be set. ") } } } diff --git a/src/simulation/network/link.rs b/src/simulation/network/link.rs index 1c4ce519..57209448 100644 --- a/src/simulation/network/link.rs +++ b/src/simulation/network/link.rs @@ -235,8 +235,7 @@ impl SplitInLink { #[cfg(test)] mod tests { use crate::simulation::id::IdImpl; - use crate::simulation::messaging::messages::proto::leg::Route; - use crate::simulation::messaging::messages::proto::{Activity, NetworkRoute}; + use crate::simulation::messaging::messages::proto::Activity; use crate::simulation::messaging::messages::proto::{Agent, Leg, Plan, Vehicle}; use crate::simulation::network::link::LocalLink; diff --git a/src/simulation/population/population.rs b/src/simulation/population/population.rs index 98530c69..1f5c4714 100644 --- a/src/simulation/population/population.rs +++ b/src/simulation/population/population.rs @@ -59,7 +59,7 @@ impl<'p> Population<'p> { garage .vehicle_types .iter() - .map(move |veh_type| Self::create_veh_id_string(&p_id, &veh_type.id)) + .map(move |(type_id, _veh_type)| Self::create_veh_id_string(&p_id, &type_id)) }) .collect(); @@ -115,14 +115,14 @@ impl<'p> Population<'p> { garage .vehicle_types .iter() - .map(move |veh_type| (p_id, veh_type.id.clone())) + .map(move |(type_id, _veh_type)| (p_id, type_id.clone())) }) .collect(); for (person_id, veh_type) in person_mode { let vehicle_id_ext = Self::create_veh_id_string(&person_id, &veh_type); let vehicle_id = garage.vehicle_ids.get_from_ext(vehicle_id_ext.as_str()); - garage.add_veh(vehicle_id, veh_type); + garage.add_veh(vehicle_id, person_id.clone(), veh_type); } } @@ -150,7 +150,6 @@ impl<'p> Population<'p> { mod tests { use std::collections::HashSet; - use crate::simulation::messaging::messages::proto::leg::Route; use crate::simulation::network::global_network::Network; use crate::simulation::population::population::Population; use crate::simulation::vehicles::garage::Garage; @@ -187,23 +186,20 @@ mod tests { assert_eq!(None, leg.trav_time); assert_eq!(None, leg.dep_time); assert!(leg.route.is_some()); - if let Route::NetworkRoute(net_route) = leg.route.as_ref().unwrap() { - assert_eq!( - garage.vehicle_ids.get_from_ext("1_car").internal as u64, - net_route.vehicle_id - ); - assert_eq!( - vec![ - net.link_ids.get_from_ext("1").internal as u64, - net.link_ids.get_from_ext("6").internal as u64, - net.link_ids.get_from_ext("15").internal as u64, - net.link_ids.get_from_ext("20").internal as u64, - ], - net_route.route - ); - } else { - panic!("Expected network route as first leg.") - } + let net_route = leg.route.as_ref().unwrap(); + assert_eq!( + garage.vehicle_ids.get_from_ext("1_car").internal as u64, + net_route.veh_id + ); + assert_eq!( + vec![ + net.link_ids.get_from_ext("1").internal as u64, + net.link_ids.get_from_ext("6").internal as u64, + net.link_ids.get_from_ext("15").internal as u64, + net.link_ids.get_from_ext("20").internal as u64, + ], + net_route.route + ); } #[test] @@ -239,6 +235,8 @@ mod tests { // we expect three agents overall assert_eq!(3, pop.agents.len()); + // todo test bookkeeping of garage person_2_vehicle + println!("{pop:#?}"); } diff --git a/src/simulation/simulation.rs b/src/simulation/simulation.rs index cf3838f5..c9686e9b 100644 --- a/src/simulation/simulation.rs +++ b/src/simulation/simulation.rs @@ -4,18 +4,20 @@ use crate::simulation::config::Config; use crate::simulation::messaging::events::proto::Event; use crate::simulation::messaging::events::EventsPublisher; use crate::simulation::messaging::message_broker::{MessageBroker, MpiMessageBroker}; -use crate::simulation::messaging::messages::proto::leg::Route; -use crate::simulation::messaging::messages::proto::{Agent, GenericRoute, Vehicle}; +use crate::simulation::messaging::messages::proto::{Agent, Vehicle}; use crate::simulation::network::link::SimLink; use crate::simulation::network::sim_network::{ExitReason, SimNetworkPartition}; use crate::simulation::population::population::Population; use crate::simulation::time_queue::TimeQueue; +use crate::simulation::vehicles::garage::Garage; +use crate::simulation::vehicles::vehicle_type::LevelOfDetail; pub struct Simulation<'sim> { activity_q: TimeQueue, teleportation_q: TimeQueue, network: SimNetworkPartition<'sim>, population: Population<'sim>, + garage: Garage<'sim>, message_broker: MpiMessageBroker, events: EventsPublisher, } @@ -24,6 +26,7 @@ impl<'sim> Simulation<'sim> { pub fn new( config: &Config, network: SimNetworkPartition<'sim>, + garage: Garage<'sim>, mut population: Population<'sim>, message_broker: MpiMessageBroker, events: EventsPublisher, @@ -41,6 +44,7 @@ impl<'sim> Simulation<'sim> { Simulation { network, population, + garage, teleportation_q: TimeQueue::new(), activity_q, message_broker, @@ -93,47 +97,48 @@ impl<'sim> Simulation<'sim> { ), ); - //here, current element counter is going to be increased - agent.advance_plan(); - - assert_ne!(agent.curr_plan_elem % 2, 0); - - let leg = agent.curr_leg(); - - match leg.route.as_ref().unwrap() { - Route::GenericRoute(route) => { - self.events.publish_event( - now, - &Event::new_departure(agent_id, route.start_link, String::from("")), - ); + let mut vehicle = self.departure(agent, now); + let veh_type_id = self.garage.vehicle_type_ids.get_from_wire(vehicle.r#type); + let veh_type = self.garage.vehicle_types.get(&veh_type_id).unwrap(); - if Simulation::is_local_route(route, &self.message_broker) { - let veh = Vehicle::new(agent.id, 1, 10., 1., Some(agent)); - self.teleportation_q.add(veh, now); + match veh_type.lod { + LevelOfDetail::Network => { + self.events + .publish_event(now, &Event::new_person_enters_veh(agent_id, vehicle.id)); + self.veh_onto_network(vehicle, true, now); + } + LevelOfDetail::Teleported => { + if Simulation::is_local_route(&vehicle, &self.message_broker) { + self.teleportation_q.add(vehicle, now); } else { - let veh = Vehicle::new(agent.id, 1, 10., 1., Some(agent)); - self.message_broker.add_veh(veh, now); + // we need to call advance here, so that the vehicle's current link index + // points to the end link of the route array. + vehicle.advance_route_index(); + self.message_broker.add_veh(vehicle, now); } } - Route::NetworkRoute(route) => { - let link_id = route.route.first().unwrap(); - self.events.publish_event( - now, - &Event::new_departure(agent_id, *link_id, String::from("")), - ); - - self.events.publish_event( - now, - &Event::new_person_enters_veh(agent_id, route.vehicle_id), - ); - - let veh = Vehicle::new(route.vehicle_id, 0, 10., 1., Some(agent)); - self.veh_onto_network(veh, true, now); - } } } } + fn departure(&mut self, mut agent: Agent, now: u32) -> Vehicle { + //here, current element counter is going to be increased + agent.advance_plan(); + + assert_ne!(agent.curr_plan_elem % 2, 0); + + let leg = agent.curr_leg(); + let route = leg.route.as_ref().unwrap(); + let leg_mode = self.garage.modes.get_from_wire(leg.mode); + self.events.publish_event( + now, + &Event::new_departure(agent.id, route.start_link(), leg_mode.external.clone()), + ); + + let veh_id = self.garage.vehicle_ids.get_from_wire(route.veh_id); + self.garage.unpark_veh(agent, &veh_id) + } + fn veh_onto_network(&mut self, vehicle: Vehicle, from_act: bool, now: u32) { let link_id_internal = vehicle.curr_link_id().unwrap(); // in this case there should always be a link id. let link_id = self.network.global_network.link_ids.get(link_id_internal); @@ -144,6 +149,7 @@ impl<'sim> Simulation<'sim> { ) }); + // todo, can we do this differently maybe... if !from_act { self.events.publish_event( now, @@ -168,12 +174,12 @@ impl<'sim> Simulation<'sim> { // handle travelled let mut agent = vehicle.agent.unwrap(); let leg = agent.curr_leg(); - if let Route::GenericRoute(route) = &leg.route.as_ref().unwrap() { - self.events.publish_event( - now, - &Event::new_travelled(agent.id, route.distance, String::from("")), - ); - } + let route = leg.route.as_ref().unwrap(); + let mode = self.garage.modes.get_from_wire(leg.mode); + self.events.publish_event( + now, + &Event::new_travelled(agent.id, route.distance, mode.external.clone()), + ); agent.advance_plan(); self.activity_q.add(agent, now); } @@ -235,17 +241,11 @@ impl<'sim> Simulation<'sim> { } } - fn is_local_route(route: &GenericRoute, message_broker: &MpiMessageBroker) -> bool { - let (from, to) = Simulation::process_ids_for_generic_route(route, message_broker); + fn is_local_route(veh: &Vehicle, message_broker: &MpiMessageBroker) -> bool { + let leg = veh.agent.as_ref().unwrap().curr_leg(); + let route = leg.route.as_ref().unwrap(); + let from = message_broker.rank_for_link(route.start_link()); + let to = message_broker.rank_for_link(route.end_link()); from == to } - - fn process_ids_for_generic_route( - route: &GenericRoute, - message_broker: &MpiMessageBroker, - ) -> (u64, u64) { - let from_rank = message_broker.rank_for_link(route.start_link); - let to_rank = message_broker.rank_for_link(route.end_link); - (from_rank, to_rank) - } } diff --git a/src/simulation/vehicles/garage.rs b/src/simulation/vehicles/garage.rs index 553c89d3..62dbf9ab 100644 --- a/src/simulation/vehicles/garage.rs +++ b/src/simulation/vehicles/garage.rs @@ -2,14 +2,17 @@ use std::collections::HashMap; use crate::simulation::id::{Id, IdStore}; use crate::simulation::io::vehicles::{IOVehicleDefinitions, IOVehicleType}; -use crate::simulation::messaging::messages::proto::Vehicle; +use crate::simulation::messaging::messages::proto::{Agent, Vehicle}; use crate::simulation::vehicles::vehicle_type::{LevelOfDetail, VehicleType}; #[derive(Debug)] pub struct Garage<'g> { pub vehicles: HashMap, GarageVehicle>, + // don't know whether this is a good place to do this kind of book keeping + // we need this information to extract vehicles from io::Plans though. + pub person_2_vehicle: HashMap, HashMap, Id>>, pub vehicle_ids: IdStore<'g, Vehicle>, - pub vehicle_types: Vec, + pub vehicle_types: HashMap, VehicleType>, pub vehicle_type_ids: IdStore<'g, VehicleType>, pub modes: IdStore<'g, String>, } @@ -30,8 +33,9 @@ impl<'g> Garage<'g> { pub fn new() -> Self { Garage { vehicles: Default::default(), + person_2_vehicle: Default::default(), vehicle_ids: Default::default(), - vehicle_types: vec![], + vehicle_types: Default::default(), vehicle_type_ids: IdStore::new(), modes: Default::default(), } @@ -95,19 +99,52 @@ impl<'g> Garage<'g> { veh_type.id.external ); - self.vehicle_types.push(veh_type); + self.vehicle_types.insert(veh_type.id.clone(), veh_type); } pub fn add_veh_id(&mut self, external_id: &str) -> Id { self.vehicle_ids.create_id(external_id) } - pub fn add_veh(&mut self, veh_id: Id, veh_type: Id) { + pub fn add_veh( + &mut self, + veh_id: Id, + person_id: Id, + veh_type_id: Id, + ) { let vehicle = GarageVehicle { - id: veh_id, - veh_type, + id: veh_id.clone(), + veh_type: veh_type_id.clone(), }; self.vehicles.insert(vehicle.id.clone(), vehicle); + let veh_type = self.vehicle_types.get(&veh_type_id).unwrap(); + self.person_2_vehicle + .entry(person_id) + .or_default() + .insert(veh_type.net_mode.clone(), veh_id); + } + + pub fn get_veh_id(&self, person_id: &Id, mode: &Id) -> Id { + self.person_2_vehicle + .get(person_id) + .unwrap() + .get(mode) + .unwrap() + .clone() + } + + pub fn unpark_veh(&mut self, person: Agent, id: &Id) -> Vehicle { + let garage_veh = self.vehicles.remove(id).unwrap(); + let veh_type = self.vehicle_types.get(&garage_veh.veh_type).unwrap(); + + Vehicle { + id: id.internal as u64, + curr_route_elem: 0, + r#type: veh_type.id.internal as u64, + max_v: veh_type.max_v, + pce: veh_type.pce, + agent: Some(person), + } } } @@ -170,9 +207,9 @@ mod tests { assert_eq!(0, garage.modes.get_from_ext("car").internal); assert_eq!(0, garage.vehicle_type_ids.get_from_ext("some-id").internal); - let veh_type_opt = garage.vehicle_types.first(); + let veh_type_opt = garage.vehicle_types.iter().next(); assert!(veh_type_opt.is_some()); - let veh_type = veh_type_opt.unwrap(); + let (type_id, veh_type) = veh_type_opt.unwrap(); assert!(matches!(veh_type.lod, LevelOfDetail::Network)); } @@ -205,9 +242,9 @@ mod tests { let expected_id = garage.vehicle_type_ids.get_from_ext("some-id"); let expected_mode = garage.modes.get_from_ext("some_mode"); - let veh_type_opt = garage.vehicle_types.first(); + let veh_type_opt = garage.vehicle_types.iter().next(); assert!(veh_type_opt.is_some()); - let veh_type = veh_type_opt.unwrap(); + let (type_id, veh_type) = veh_type_opt.unwrap(); assert!(matches!(veh_type.lod, LevelOfDetail::Teleported)); assert_eq!(veh_type.max_v, 100.); assert_eq!(veh_type.width, 5.0); From 7882c3542f91fcac6184724f9fb5168921e8d243 Mon Sep 17 00:00:00 2001 From: janekdererste Date: Fri, 6 Oct 2023 11:34:09 +0200 Subject: [PATCH 19/20] put new vehicle definition into simulation, project compiles all (unignored) tests pass --- src/simulation/io/attributes.rs | 18 ++++++++--- src/simulation/messaging/messages.rs | 37 ++++++---------------- src/simulation/network/link.rs | 10 ++++-- src/simulation/network/sim_network.rs | 15 ++++++--- src/simulation/population/population.rs | 14 ++++----- src/simulation/simulation.rs | 41 ++++++++++++++----------- src/simulation/vehicles/garage.rs | 17 +++++++--- src/simulation/vehicles/vehicle_type.rs | 1 + 8 files changed, 85 insertions(+), 68 deletions(-) diff --git a/src/simulation/io/attributes.rs b/src/simulation/io/attributes.rs index a0d68005..8c41625f 100644 --- a/src/simulation/io/attributes.rs +++ b/src/simulation/io/attributes.rs @@ -25,17 +25,27 @@ pub struct Attrs { } impl Attrs { - pub fn find_or_else<'a, F>(&self, name: &str, f: F) -> &str + #[allow(clippy::needless_lifetimes)] // lifetimes are in fact needed here i think + pub fn find_or_else<'a, F>(&'a self, name: &str, f: F) -> &'a str where F: FnOnce() -> &'a str, { let opt_attr = self.attributes.iter().find(|&attr| attr.name.eq(name)); - let value = if let Some(&attr) = opt_attr { + if let Some(&attr) = opt_attr.as_ref() { attr.value.as_str() } else { f() - }; + } + } - value + pub fn find_or_else_opt<'a, F>(attrs_opt: &'a Option, name: &str, f: F) -> &'a str + where + F: FnOnce() -> &'a str, + { + if let Some(attrs) = attrs_opt.as_ref() { + attrs.find_or_else(name, f) + } else { + f() + } } } diff --git a/src/simulation/messaging/messages.rs b/src/simulation/messaging/messages.rs index 2167ca1b..53f64fd8 100644 --- a/src/simulation/messaging/messages.rs +++ b/src/simulation/messaging/messages.rs @@ -6,6 +6,7 @@ use log::debug; use prost::Message; use crate::simulation::id::Id; +use crate::simulation::io::attributes::Attrs; use crate::simulation::io::population::{ IOActivity, IOLeg, IOPerson, IOPlan, IOPlanElement, IORoute, }; @@ -115,7 +116,7 @@ impl Vehicle { // todo, fix type and mode pub fn new(id: u64, veh_type: u64, max_v: f32, pce: f32, agent: Option) -> Vehicle { Vehicle { - id: id, + id, agent, curr_route_elem: 0, r#type: veh_type, @@ -124,7 +125,7 @@ impl Vehicle { } } - fn agent(&self) -> &Agent { + pub fn agent(&self) -> &Agent { self.agent.as_ref().unwrap() } @@ -227,7 +228,7 @@ impl Agent { } fn next_act_index(&self) -> u32 { - let act_index = match self.curr_plan_elem % 2 { + match self.curr_plan_elem % 2 { //current element is an activity => two elements after is the next activity 0 => (self.curr_plan_elem + 2) / 2, //current element is a leg => one element after is the next activity @@ -238,8 +239,7 @@ impl Agent { self.id ) } - }; - act_index + } } pub fn curr_leg(&self) -> &Leg { @@ -261,13 +261,8 @@ impl Agent { self.get_leg_at_index(next_leg_index) } - fn next_leg_mut(&mut self) -> &mut Leg { - let next_leg_index = self.next_leg_index(); - self.get_leg_at_index_mut(next_leg_index) - } - fn next_leg_index(&self) -> u32 { - let next_leg_index = match self.curr_plan_elem % 2 { + match self.curr_plan_elem % 2 { //current element is an activity => one element after is the next leg 0 => (self.curr_plan_elem + 1) / 2, //current element is a leg => two elements after is the next leg @@ -278,8 +273,7 @@ impl Agent { self.id ) } - }; - next_leg_index + } } fn get_act_at_index(&self, index: u32) -> &Activity { @@ -309,15 +303,6 @@ impl Agent { .unwrap() } - fn get_leg_at_index_mut(&mut self, index: u32) -> &mut Leg { - self.plan - .as_mut() - .unwrap() - .legs - .get_mut(index as usize) - .unwrap() - } - pub fn advance_plan(&mut self) { let next = self.curr_plan_elem + 1; if self.plan.as_ref().unwrap().acts.len() + self.plan.as_ref().unwrap().legs.len() @@ -454,8 +439,7 @@ impl Activity { impl Leg { fn from_io(io_leg: &IOLeg, person_id: &Id, net: &Network, garage: &Garage) -> Self { - let attrs = io_leg.attributes.as_ref().unwrap_or_default(); - let routing_mode_ext = attrs.find_or_else("routingMode", || "car"); + let routing_mode_ext = Attrs::find_or_else_opt(&io_leg.attributes, "routingMode", || "car"); let routing_mode = garage.modes.get_from_ext(routing_mode_ext); let mode = garage.modes.get_from_ext(io_leg.mode.as_str()); @@ -470,11 +454,10 @@ impl Leg { } } - // todo fix mode!! - pub fn new(route: Route, mode: &str, trav_time: Option, dep_time: Option) -> Self { + pub fn new(route: Route, mode: u64, trav_time: Option, dep_time: Option) -> Self { Self { route: Some(route), - mode: 0, //todo fix mode + mode, trav_time, dep_time, routing_mode: 0, diff --git a/src/simulation/network/link.rs b/src/simulation/network/link.rs index 57209448..b32ed8c6 100644 --- a/src/simulation/network/link.rs +++ b/src/simulation/network/link.rs @@ -235,7 +235,7 @@ impl SplitInLink { #[cfg(test)] mod tests { use crate::simulation::id::IdImpl; - use crate::simulation::messaging::messages::proto::Activity; + use crate::simulation::messaging::messages::proto::{Activity, Route}; use crate::simulation::messaging::messages::proto::{Agent, Leg, Plan, Vehicle}; use crate::simulation::network::link::LocalLink; @@ -393,8 +393,12 @@ mod tests { } fn create_agent(id: u64, route: Vec) -> Agent { - let route = Route::NetworkRoute(NetworkRoute::new(id, route)); - let leg = Leg::new(route, "car", None, None); + let route = Route { + veh_id: id, + distance: 0.0, + route, + }; + let leg = Leg::new(route, 0, None, None); let act = Activity::new(0., 0., 0, 1, None, None, None); let mut plan = Plan::new(); plan.add_act(act); diff --git a/src/simulation/network/sim_network.rs b/src/simulation/network/sim_network.rs index 95c65a8c..f63de7c6 100644 --- a/src/simulation/network/sim_network.rs +++ b/src/simulation/network/sim_network.rs @@ -55,14 +55,14 @@ impl<'n> SimNetworkPartition<'n> { Self::new(Vec::from_iter(nodes), links, global_network) } - fn create_sim_link(link: &Link, partition: usize, all_nodes: &Vec) -> SimLink { + fn create_sim_link(link: &Link, partition: usize, all_nodes: &[Node]) -> SimLink { let from_part = all_nodes.get(link.from.internal).unwrap().partition; let to_part = all_nodes.get(link.to.internal).unwrap().partition; if from_part == to_part { SimLink::Local(LocalLink::from_link(link, 1.0)) } else if to_part == partition { - let local_link = LocalLink::from_link(&link, 1.0); + let local_link = LocalLink::from_link(link, 1.0); SimLink::In(SplitInLink::new(from_part, local_link)) } else { SimLink::Out(SplitOutLink::new(link.id.clone(), to_part)) @@ -176,10 +176,11 @@ impl<'n> SimNetworkPartition<'n> { #[cfg(test)] mod tests { + use crate::simulation::messaging::messages::proto::Route; use crate::simulation::{ messaging::{ events::EventsPublisher, - messages::proto::{leg::Route, Activity, Agent, Leg, NetworkRoute, Plan, Vehicle}, + messages::proto::{Activity, Agent, Leg, Plan, Vehicle}, }, network::{ global_network::{Link, Network, Node}, @@ -447,8 +448,12 @@ mod tests { } fn create_agent(id: u64, route: Vec) -> Agent { - let route = Route::NetworkRoute(NetworkRoute::new(id, route)); - let leg = Leg::new(route, "car", None, None); + let route = Route { + veh_id: id, + distance: 0.0, + route, + }; + let leg = Leg::new(route, 0, None, None); let act = Activity::new(0., 0., 0, 1, None, None, None); let mut plan = Plan::new(); plan.add_act(act); diff --git a/src/simulation/population/population.rs b/src/simulation/population/population.rs index 1f5c4714..801cb204 100644 --- a/src/simulation/population/population.rs +++ b/src/simulation/population/population.rs @@ -58,8 +58,8 @@ impl<'p> Population<'p> { .flat_map(|p_id| { garage .vehicle_types - .iter() - .map(move |(type_id, _veh_type)| Self::create_veh_id_string(&p_id, &type_id)) + .keys() + .map(move |type_id| Self::create_veh_id_string(&p_id, type_id)) }) .collect(); @@ -114,13 +114,13 @@ impl<'p> Population<'p> { .flat_map(|p_id| { garage .vehicle_types - .iter() - .map(move |(type_id, _veh_type)| (p_id, type_id.clone())) + .keys() + .map(move |type_id| (p_id, type_id.clone())) }) .collect(); for (person_id, veh_type) in person_mode { - let vehicle_id_ext = Self::create_veh_id_string(&person_id, &veh_type); + let vehicle_id_ext = Self::create_veh_id_string(person_id, &veh_type); let vehicle_id = garage.vehicle_ids.get_from_ext(vehicle_id_ext.as_str()); garage.add_veh(vehicle_id, person_id.clone(), veh_type); } @@ -213,8 +213,8 @@ mod tests { assert_eq!(3, garage.vehicle_types.len()); assert!(garage .vehicle_types - .iter() - .all(|veh_type| expected_veh_types.contains(veh_type.id.external.as_str()))); + .keys() + .all(|type_id| expected_veh_types.contains(type_id.external.as_str()))); // check that we have a vehicle for each mode and for each person assert_eq!(9, garage.vehicles.len()); diff --git a/src/simulation/simulation.rs b/src/simulation/simulation.rs index c9686e9b..9e26429f 100644 --- a/src/simulation/simulation.rs +++ b/src/simulation/simulation.rs @@ -82,8 +82,7 @@ impl<'sim> Simulation<'sim> { fn wakeup(&mut self, now: u32) { let agents = self.activity_q.pop(now); - for mut agent in agents { - let agent_id = agent.id; + for agent in agents { let act_type = self .population .act_types @@ -91,7 +90,7 @@ impl<'sim> Simulation<'sim> { self.events.publish_event( now, &Event::new_act_end( - agent_id, + agent.id, agent.curr_act().link_id, act_type.external.clone(), ), @@ -103,8 +102,10 @@ impl<'sim> Simulation<'sim> { match veh_type.lod { LevelOfDetail::Network => { - self.events - .publish_event(now, &Event::new_person_enters_veh(agent_id, vehicle.id)); + self.events.publish_event( + now, + &Event::new_person_enters_veh(vehicle.agent().id, vehicle.id), + ); self.veh_onto_network(vehicle, true, now); } LevelOfDetail::Teleported => { @@ -171,8 +172,10 @@ impl<'sim> Simulation<'sim> { fn terminate_teleportation(&mut self, now: u32) { let teleportation_vehicles = self.teleportation_q.pop(now); for vehicle in teleportation_vehicles { + // park the vehice - get the agent out of the vehicle + let mut agent = self.garage.park_veh(vehicle); + // handle travelled - let mut agent = vehicle.agent.unwrap(); let leg = agent.curr_leg(); let route = leg.route.as_ref().unwrap(); let mode = self.garage.modes.get_from_wire(leg.mode); @@ -180,6 +183,8 @@ impl<'sim> Simulation<'sim> { now, &Event::new_travelled(agent.id, route.distance, mode.external.clone()), ); + + // advance plan to activity and put agent into activity q. agent.advance_plan(); self.activity_q.add(agent, now); } @@ -191,21 +196,21 @@ impl<'sim> Simulation<'sim> { for exit_reason in exited_vehicles { match exit_reason { ExitReason::FinishRoute(vehicle) => { - let veh_id = vehicle.id; - let mut agent = vehicle.agent.unwrap(); - let leg_mode = 0; // todo fix mode - - self.events - .publish_event(now, &Event::new_person_leaves_veh(agent.id, veh_id)); - - agent.advance_plan(); - let act = agent.curr_act(); - + // finish driving - get agent out of vehicle, remember mode for arrival event self.events.publish_event( now, - &Event::new_arrival(agent.id, act.link_id, String::from("some mode")), - ); //todo fix mode + &Event::new_person_leaves_veh(vehicle.agent().id, vehicle.id), + ); + let veh_type_id = self.garage.vehicle_type_ids.get_from_wire(vehicle.id); + let veh_type = self.garage.vehicle_types.get(&veh_type_id).unwrap(); + let mode = veh_type.net_mode.external.clone(); + let mut agent = self.garage.park_veh(vehicle); + // move to next activity + agent.advance_plan(); + let act = agent.curr_act(); + self.events + .publish_event(now, &Event::new_arrival(agent.id, act.link_id, mode)); let act_type = self.population.act_types.get_from_wire(act.act_type); self.events.publish_event( now, diff --git a/src/simulation/vehicles/garage.rs b/src/simulation/vehicles/garage.rs index 62dbf9ab..9dea6f58 100644 --- a/src/simulation/vehicles/garage.rs +++ b/src/simulation/vehicles/garage.rs @@ -133,6 +133,15 @@ impl<'g> Garage<'g> { .clone() } + pub(crate) fn park_veh(&mut self, vehicle: Vehicle) -> Agent { + let id = self.vehicle_ids.get_from_wire(vehicle.id); + let veh_type = self.vehicle_type_ids.get_from_wire(vehicle.r#type); + let garage_veh = GarageVehicle { id, veh_type }; + self.vehicles.insert(garage_veh.id.clone(), garage_veh); + + vehicle.agent.unwrap() + } + pub fn unpark_veh(&mut self, person: Agent, id: &Id) -> Vehicle { let garage_veh = self.vehicles.remove(id).unwrap(); let veh_type = self.vehicle_types.get(&garage_veh.veh_type).unwrap(); @@ -207,9 +216,9 @@ mod tests { assert_eq!(0, garage.modes.get_from_ext("car").internal); assert_eq!(0, garage.vehicle_type_ids.get_from_ext("some-id").internal); - let veh_type_opt = garage.vehicle_types.iter().next(); + let veh_type_opt = garage.vehicle_types.values().next(); assert!(veh_type_opt.is_some()); - let (type_id, veh_type) = veh_type_opt.unwrap(); + let veh_type = veh_type_opt.unwrap(); assert!(matches!(veh_type.lod, LevelOfDetail::Network)); } @@ -242,9 +251,9 @@ mod tests { let expected_id = garage.vehicle_type_ids.get_from_ext("some-id"); let expected_mode = garage.modes.get_from_ext("some_mode"); - let veh_type_opt = garage.vehicle_types.iter().next(); + let veh_type_opt = garage.vehicle_types.values().next(); assert!(veh_type_opt.is_some()); - let (type_id, veh_type) = veh_type_opt.unwrap(); + let veh_type = veh_type_opt.unwrap(); assert!(matches!(veh_type.lod, LevelOfDetail::Teleported)); assert_eq!(veh_type.max_v, 100.); assert_eq!(veh_type.width, 5.0); diff --git a/src/simulation/vehicles/vehicle_type.rs b/src/simulation/vehicles/vehicle_type.rs index 21b70c89..afe7c45e 100644 --- a/src/simulation/vehicles/vehicle_type.rs +++ b/src/simulation/vehicles/vehicle_type.rs @@ -1,6 +1,7 @@ use crate::simulation::id::Id; #[derive(Debug)] +#[allow(dead_code)] // allow dead code here, as we have some props which are not used yet. pub struct VehicleType { pub(crate) id: Id, pub(crate) length: f32, From b4d70cd5f09e859f38e516da7bb2a68a1c2b2f74 Mon Sep 17 00:00:00 2001 From: janekdererste Date: Mon, 9 Oct 2023 11:38:24 +0200 Subject: [PATCH 20/20] use veh type id when fetching the vehicle type. Prdouce output network with internal ids, until all output is written with external ids --- src/simulation/network/global_network.rs | 12 ++++++++---- src/simulation/simulation.rs | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/simulation/network/global_network.rs b/src/simulation/network/global_network.rs index 1783499c..e43debd1 100644 --- a/src/simulation/network/global_network.rs +++ b/src/simulation/network/global_network.rs @@ -83,7 +83,8 @@ impl<'a> Network<'a> { }], }; let io_node = IONode { - id: node.id.external.clone(), + //id: node.id.external.clone(), + id: node.id.internal.to_string(), // todo replace this with external id, once all output is written using external ids x: node.x, y: node.y, attributes: Some(attributes), @@ -107,9 +108,12 @@ impl<'a> Network<'a> { }; let io_link = IOLink { - id: link.id.external.clone(), - from: link.from.external.clone(), - to: link.to.external.clone(), + //id: link.id.external.clone(), + id: link.id.internal.to_string(), // todo replace with external id again, once all output translates to external ids + //from: link.from.external.clone(), + from: link.from.internal.to_string(), + //to: link.to.external.clone(), + to: link.to.internal.to_string(), length: link.length, capacity: link.capacity, freespeed: link.freespeed, diff --git a/src/simulation/simulation.rs b/src/simulation/simulation.rs index 9e26429f..47a4ba9b 100644 --- a/src/simulation/simulation.rs +++ b/src/simulation/simulation.rs @@ -201,7 +201,7 @@ impl<'sim> Simulation<'sim> { now, &Event::new_person_leaves_veh(vehicle.agent().id, vehicle.id), ); - let veh_type_id = self.garage.vehicle_type_ids.get_from_wire(vehicle.id); + let veh_type_id = self.garage.vehicle_type_ids.get_from_wire(vehicle.r#type); let veh_type = self.garage.vehicle_types.get(&veh_type_id).unwrap(); let mode = veh_type.net_mode.external.clone(); let mut agent = self.garage.park_veh(vehicle);