Skip to content

Commit df6dffe

Browse files
committed
Merge branch '205-rename-protofiles-to-inputfiles' of github.com:matsim-vsp/parallel_qsim_rust into 205-rename-protofiles-to-inputfiles
2 parents 07faae9 + c2c28f7 commit df6dffe

File tree

28 files changed

+1242
-557
lines changed

28 files changed

+1242
-557
lines changed

rust_qsim/assets/equil/equil-1-plan.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<attribute name="routingMode" class="java.lang.String">car</attribute>
2323
</attributes>
2424
<route type="links" start_link="1" end_link="20" trav_time="undefined" distance="25000.0"
25-
vehicleRefId="1_car">1 6 15 20
25+
vehicleRefId="1">1 6 15 20
2626
</route>
2727
</leg>
2828
<activity type="w" link="20" x="3456.0" y="4242.0" max_dur="00:10:00">
@@ -32,7 +32,7 @@
3232
<attribute name="routingMode" class="java.lang.String">car</attribute>
3333
</attributes>
3434
<route type="links" start_link="20" end_link="20" trav_time="undefined" distance="0.0"
35-
vehicleRefId="1_car">20
35+
vehicleRefId="1">20
3636
</route>
3737
</leg>
3838
<activity type="w" link="20" x="10000.0" y="0.0" max_dur="03:30:00">
@@ -42,7 +42,7 @@
4242
<attribute name="routingMode" class="java.lang.String">car</attribute>
4343
</attributes>
4444
<route type="links" start_link="20" end_link="1" trav_time="undefined" distance="65000.0"
45-
vehicleRefId="1_car">20 21 22 23 1
45+
vehicleRefId="1">20 21 22 23 1
4646
</route>
4747
</leg>
4848
<activity type="h" link="1" x="-25000.0" y="0.0">

rust_qsim/assets/equil/equil-vehicles.xml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<vehicleDefinitions xmlns="http://www.matsim.org/files/dtd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://www.matsim.org/files/dtd http://www.matsim.org/files/dtd/vehicleDefinitions_v1.0.xsd">
2+
<vehicleDefinitions xmlns="http://www.matsim.org/files/dtd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.matsim.org/files/dtd http://www.matsim.org/files/dtd/vehicleDefinitions_v2.0.xsd">
43
<vehicleType id="car">
5-
<attributes>
6-
</attributes>
74
<description>abc</description>
85
<length meter="9.5"/>
96
<width meter="3.0"/>
@@ -13,9 +10,6 @@
1310
<flowEfficiencyFactor factor="1.5"/>
1411
</vehicleType>
1512
<vehicleType id="walk">
16-
<attributes>
17-
<attribute name="lod" class="java.lang.String">teleported</attribute>
18-
</attributes>
1913
<description>This is a pair of shoes</description>
2014
<length meter="0.5"/>
2115
<width meter="1.0"/>

rust_qsim/src/bin/local_qsim_routing.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,44 @@ fn main() {
2727
let total_thread_count = config.partitioning().num_parts + 1;
2828
let barrier = Arc::new(Barrier::new(total_thread_count as usize));
2929

30+
// Configuring the routing adapter. We need
31+
// - the IP address of the router service
32+
// - the configuration of the simulation
33+
// - the shutdown handles of the executor (= receiver of shutdown signals from the controller)
34+
// The AsyncExecutor will spawn a thread for the routing service adapter and an async runtime.
3035
let executor = AsyncExecutor::from_config(&config, barrier.clone());
3136
let factory = RoutingServiceAdapterFactory::new(
3237
vec![&args.router_ip],
3338
config.clone(),
3439
executor.shutdown_handles(),
3540
);
3641

42+
// Spawning the routing service adapter in a separate thread. The adapter will be run in its own tokio runtime.
43+
// This function returns
44+
// - the join handle of the adapter thread
45+
// - a channel for sending requests to the adapter
46+
// - a channel for sending shutdown signal for the adapter
3747
let (router_handle, send, send_sd) = executor.spawn_thread("router", factory);
3848

49+
// The request sender is passed to the controller.
3950
let mut services = ExternalServices::default();
4051
services.insert(ExternalServiceType::Routing("pt".into()), send.into());
4152

42-
let scenario = GlobalScenario::build(config);
53+
// Load scenario
54+
let scenario = GlobalScenario::load(config);
4355

56+
// Create controller
4457
let controller = LocalControllerBuilder::default()
4558
.global_scenario(scenario)
4659
.external_services(services)
4760
.global_barrier(barrier)
4861
.build()
4962
.unwrap();
5063

64+
// Run controller
5165
let sim_handles = controller.run();
5266

67+
// Wait for the controller to finish and the routing adapter to finish.
5368
controller::try_join(
5469
sim_handles,
5570
vec![AdapterHandleBuilder::default()

rust_qsim/src/external_services/routing/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,14 @@ impl From<Response> for InternalRoutingResponse {
109109
pub struct RoutingServiceAdapterFactory {
110110
ip: Vec<String>,
111111
config: Arc<Config>,
112-
shutdown_handles: Arc<Mutex<Vec<tokio::task::JoinHandle<()>>>>,
112+
shutdown_handles: Arc<Mutex<Vec<JoinHandle<()>>>>,
113113
}
114114

115115
impl RoutingServiceAdapterFactory {
116116
pub fn new(
117117
ip: Vec<impl Into<String>>,
118118
config: Arc<Config>,
119-
shutdown_handles: Arc<Mutex<Vec<tokio::task::JoinHandle<()>>>>,
119+
shutdown_handles: Arc<Mutex<Vec<JoinHandle<()>>>>,
120120
) -> Self {
121121
Self {
122122
ip: ip.into_iter().map(|s| s.into()).collect(),

rust_qsim/src/generated/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,12 @@ impl From<&str> for AttributeValue {
225225
}
226226
}
227227

228+
impl From<f64> for AttributeValue {
229+
fn from(value: f64) -> Self {
230+
AttributeValue::new_double(value)
231+
}
232+
}
233+
228234
// we can't tag the enum as non-exhaustive because prost generates it. This is why the warning is manually disabled.
229235
#[allow(unreachable_patterns)]
230236
impl Serialize for AttributeValue {

rust_qsim/src/simulation/agents/agent.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,19 @@ impl SimulationAgentLogic for SimulationAgent {
6868
fn advance_plan(&mut self) {
6969
self.logic.advance_plan();
7070
}
71-
fn wakeup_time(&self, now: u32) -> u32 {
72-
self.logic.wakeup_time(now)
73-
}
7471
fn state(&self) -> SimulationAgentState {
7572
self.logic.state()
7673
}
74+
fn is_wanting_to_arrive_on_current_link(&self) -> bool {
75+
self.logic.is_wanting_to_arrive_on_current_link()
76+
}
7777
fn curr_link_id(&self) -> Option<&Id<Link>> {
7878
self.logic.curr_link_id()
7979
}
8080
fn peek_next_link_id(&self) -> Option<&Id<Link>> {
8181
self.logic.peek_next_link_id()
8282
}
83+
fn wakeup_time(&self, now: u32) -> u32 {
84+
self.logic.wakeup_time(now)
85+
}
8386
}

rust_qsim/src/simulation/agents/agent_logic.rs

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,6 @@ impl SimulationAgentLogic for PlanBasedSimulationLogic {
139139
);
140140
}
141141

142-
fn wakeup_time(&self, now: u32) -> u32 {
143-
match self
144-
.basic_agent_delegate
145-
.plan_element_at(self.curr_plan_element)
146-
.unwrap()
147-
{
148-
InternalPlanElement::Activity(a) => a.cmp_end_time(now),
149-
InternalPlanElement::Leg(_) => panic!("Cannot wake up on a leg!"),
150-
}
151-
}
152-
153142
fn state(&self) -> SimulationAgentState {
154143
match self.curr_plan_element % 2 {
155144
0 => SimulationAgentState::ACTIVITY,
@@ -158,6 +147,10 @@ impl SimulationAgentLogic for PlanBasedSimulationLogic {
158147
}
159148
}
160149

150+
fn is_wanting_to_arrive_on_current_link(&self) -> bool {
151+
self.peek_next_link_id().is_none()
152+
}
153+
161154
fn curr_link_id(&self) -> Option<&Id<Link>> {
162155
if self.state() != SimulationAgentState::LEG {
163156
return None;
@@ -192,6 +185,17 @@ impl SimulationAgentLogic for PlanBasedSimulationLogic {
192185
.unwrap()
193186
.route_element_at(next_i)
194187
}
188+
189+
fn wakeup_time(&self, now: u32) -> u32 {
190+
match self
191+
.basic_agent_delegate
192+
.plan_element_at(self.curr_plan_element)
193+
.unwrap()
194+
{
195+
InternalPlanElement::Activity(a) => a.cmp_end_time(now),
196+
InternalPlanElement::Leg(_) => panic!("Cannot wake up on a leg!"),
197+
}
198+
}
195199
}
196200

197201
impl EndTime for PlanBasedSimulationLogic {
@@ -228,6 +232,22 @@ impl SimulationAgentLogic for AdaptivePlanBasedSimulationLogic {
228232
self.delegate.advance_plan();
229233
}
230234

235+
fn state(&self) -> SimulationAgentState {
236+
self.delegate.state()
237+
}
238+
239+
fn is_wanting_to_arrive_on_current_link(&self) -> bool {
240+
self.delegate.is_wanting_to_arrive_on_current_link()
241+
}
242+
243+
fn curr_link_id(&self) -> Option<&Id<Link>> {
244+
self.delegate.curr_link_id()
245+
}
246+
247+
fn peek_next_link_id(&self) -> Option<&Id<Link>> {
248+
self.delegate.peek_next_link_id()
249+
}
250+
231251
fn wakeup_time(&self, now: u32) -> u32 {
232252
let mut end = self.delegate.curr_act().cmp_end_time(now);
233253
if let Some(l) = self.delegate.next_leg() {
@@ -240,18 +260,6 @@ impl SimulationAgentLogic for AdaptivePlanBasedSimulationLogic {
240260
}
241261
end
242262
}
243-
244-
fn state(&self) -> SimulationAgentState {
245-
self.delegate.state()
246-
}
247-
248-
fn curr_link_id(&self) -> Option<&Id<Link>> {
249-
self.delegate.curr_link_id()
250-
}
251-
252-
fn peek_next_link_id(&self) -> Option<&Id<Link>> {
253-
self.delegate.peek_next_link_id()
254-
}
255263
}
256264

257265
impl EndTime for AdaptivePlanBasedSimulationLogic {

rust_qsim/src/simulation/agents/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ pub trait SimulationAgentLogic:
1616
fn curr_leg(&self) -> &InternalLeg;
1717
fn next_leg(&self) -> Option<&InternalLeg>;
1818
fn advance_plan(&mut self);
19-
fn wakeup_time(&self, now: u32) -> u32;
2019
fn state(&self) -> SimulationAgentState;
20+
21+
// Having these functions here is not ideal. See https://github.com/matsim-vsp/parallel_qsim_rust/issues/203 for more details.
22+
fn is_wanting_to_arrive_on_current_link(&self) -> bool;
2123
fn curr_link_id(&self) -> Option<&Id<Link>>;
2224
fn peek_next_link_id(&self) -> Option<&Id<Link>>;
25+
fn wakeup_time(&self, now: u32) -> u32;
2326
}
2427

2528
pub trait EnvironmentalEventObserver {
@@ -28,12 +31,12 @@ pub trait EnvironmentalEventObserver {
2831

2932
#[non_exhaustive]
3033
pub enum AgentEvent<'a> {
31-
// activity related events
34+
// activity-related events
3235
ActivityStarted(),
3336
WokeUp(WokeUpEvent<'a>),
3437
ActivityFinished(),
3538

36-
// teleportation related events
39+
// teleportation-related events
3740
TeleportationStarted(),
3841
TeleportationFinished(),
3942

rust_qsim/src/simulation/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,10 +436,15 @@ pub struct Simulation {
436436
pub start_time: u32,
437437
pub end_time: u32,
438438
pub sample_size: f32,
439+
#[serde(default = "default_to_10")]
439440
pub stuck_threshold: u32,
440441
pub main_modes: Vec<String>,
441442
}
442443

444+
fn default_to_10() -> u32 {
445+
10
446+
}
447+
443448
#[derive(Serialize, Deserialize, Clone, Copy, Debug)]
444449
pub struct ComputationalSetup {
445450
pub global_sync: bool,

rust_qsim/src/simulation/controller/local_controller.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub fn run_channel_from_args() {
122122
let config = Arc::new(Config::from(args));
123123

124124
// Load and adapt scenario
125-
let scenario = GlobalScenario::build(config);
125+
let scenario = GlobalScenario::load(config);
126126

127127
// Create and run simulation
128128
let controller = LocalControllerBuilder::default()

0 commit comments

Comments
 (0)