Skip to content

Commit

Permalink
use network mode in vehicle definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
paulheinr committed Jun 12, 2023
1 parent 0476955 commit 39e47e9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 14 deletions.
56 changes: 47 additions & 9 deletions src/simulation/io/vehicle_definitions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::simulation::io::xml_reader;
use log::debug;
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)]
Expand All @@ -25,6 +26,8 @@ pub enum IOVehicleAttribute {
EgressTime(IOEgressTime),
DoorOperation(IODoorOperation),
PassengerCarEquivalents(IOPassengerCarEquivalents),
NetworkMode(IONetworkMode),
FlowEfficiencyFactor(IOFlowEfficiencyFactor),
}

#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)]
Expand Down Expand Up @@ -69,10 +72,23 @@ 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<f32>,
pub network_mode: String,
}

#[derive(Debug, PartialEq, Clone)]
Expand All @@ -97,10 +113,12 @@ impl VehicleDefinitions {
mut self,
id: String,
maximum_velocity: Option<f32>,
network_mode: String,
) -> VehicleDefinitions {
self.vehicle_types.push(VehicleType {
id,
maximum_velocity,
network_mode,
});
self
}
Expand All @@ -115,28 +133,45 @@ impl VehicleDefinitions {
}

fn convert_io_vehicle_type(io: IOVehicleType) -> VehicleType {
let maximum_velocity = io
.attributes
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<f32> {
io.attributes
.iter()
.filter_map(|a| match a {
IOVehicleAttribute::MaximumVelocity(v) => Some(v.meter_per_second),
_ => None,
})
.collect::<Vec<f32>>()
.get(0)
.cloned();
.cloned()
}

VehicleType {
id: io.id,
maximum_velocity,
}
fn extract_network_mode(io: &IOVehicleType) -> Option<String> {
io.attributes
.iter()
.filter_map(|a| match a {
IOVehicleAttribute::NetworkMode(m) => Some(m.network_mode.clone()),
_ => None,
})
.collect::<Vec<String>>()
.get(0)
.cloned()
}

pub fn get_max_speed_for_mode(&self, mode: &str) -> Option<f32> {
let mode_vehicle_type = self
.vehicle_types
.iter()
.filter(|&v| v.id.eq(mode))
.filter(|&v| v.network_mode.eq(mode))
.collect::<Vec<&VehicleType>>();

if mode_vehicle_type.len() == 0 {
Expand Down Expand Up @@ -172,6 +207,7 @@ mod test {
<egressTime secondsPerPerson="1.0"/>
<doorOperation mode="serial"/>
<passengerCarEquivalents pce="1.0"/>
<networkMode networkMode="car"/>
</vehicleType>
<vehicleType id="bicycle">
<length meter="7.5"/>
Expand All @@ -194,10 +230,12 @@ mod test {
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)
maximum_velocity: Some(4.17),
network_mode: "bicycle".to_string()
}
],
}
Expand Down
6 changes: 3 additions & 3 deletions src/simulation/network/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,9 @@ mod tests {

fn create_three_vehicle_definitions() -> VehicleDefinitions {
VehicleDefinitions::new()
.add_vehicle_type("car".to_string(), Some(20.))
.add_vehicle_type("buggy".to_string(), Some(10.))
.add_vehicle_type("bike".to_string(), Some(5.))
.add_vehicle_type("car".to_string(), Some(20.), "car".to_string())
.add_vehicle_type("buggy".to_string(), Some(10.), "buggy".to_string())
.add_vehicle_type("bike".to_string(), Some(5.), "bike".to_string())
}

fn create_agent(id: u64, route: Vec<u64>) -> Agent {
Expand Down
4 changes: 2 additions & 2 deletions src/simulation/routing/network_converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ mod test {
#[test]
fn test_simple_network_with_modes() {
let vehicle_definitions = VehicleDefinitions::new()
.add_vehicle_type("car".to_string(), Some(5.))
.add_vehicle_type("bike".to_string(), Some(2.));
.add_vehicle_type("car".to_string(), Some(5.), "car".to_string())
.add_vehicle_type("bike".to_string(), Some(2.), "bike".to_string());

let io_network = IONetwork::from_file("./assets/routing_tests/network_different_modes.xml");
let io_population = IOPopulation::empty();
Expand Down

0 comments on commit 39e47e9

Please sign in to comment.