Skip to content

Commit 6085443

Browse files
authored
Merge pull request #135 from matsim-org/master
matsim 2025 CW 9 [2]
2 parents 9ae9965 + 4348eb8 commit 6085443

File tree

2 files changed

+66
-16
lines changed

2 files changed

+66
-16
lines changed

contribs/drt/src/main/java/org/matsim/contrib/drt/prebooking/PrebookingManager.java

+47-15
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,9 @@ private void processAbandonedRequests(double now) {
378378

379379
// Rejections
380380

381+
private static final String ABORT_STUCK_REASON = " rejected prebooked request";
382+
private final IdSet<Person> stuckUponActivity = new IdSet<>(Person.class);
383+
381384
private void processRejections(double now) {
382385
for (Id<Request> requestId : rejectedEventIds) {
383386
RequestItem item = requests.remove(requestId);
@@ -401,25 +404,20 @@ private void processRejections(double now) {
401404
Plan plan = WithinDayAgentUtils.getModifiablePlan(agent);
402405
PlanElement planElement = plan.getPlanElements().get(index);
403406

404-
if (planElement instanceof Activity currentActivity) {
405-
Activity activity = currentActivity;
406-
activity.setEndTime(Double.POSITIVE_INFINITY);
407-
activity.setMaximumDurationUndefined();
408-
409-
((HasModifiablePlan) agent).resetCaches();
410-
internalInterface.getMobsim().rescheduleActivityEnd(agent);
411-
eventsManager.processEvent(new PersonStuckEvent(now, agent.getId(), agent.getCurrentLinkId(),
412-
this.mode));
413-
414-
internalInterface.getMobsim().getAgentCounter().incLost();
415-
internalInterface.getMobsim().getAgentCounter().decLiving();
407+
if (planElement instanceof Activity) {
408+
// the agent is on an activity, let it get stuck further below
409+
stuckUponActivity.add(passengerId);
416410
} else {
417-
// If the current element is a leg, the agent is walking towards the pickup location
418-
// We make the agent stuck at the interaction activity
411+
// the agent is on a leg, so we cannot make it stuck directly.
412+
// instead, we set the next activity to an infinite duration and
413+
// make the agent get stuck right after the activity starts
414+
419415
while (index < plan.getPlanElements().size()) {
420416
if (plan.getPlanElements().get(index) instanceof Activity activity) {
421-
activity.setEndTime(Double.POSITIVE_INFINITY);
417+
activity.setEndTime(Double.MAX_VALUE); // very long but not infinite (= regular last activity)
422418
activity.setMaximumDurationUndefined();
419+
stuckUponActivity.add(agent.getId());
420+
break;
423421
}
424422

425423
index++;
@@ -431,6 +429,40 @@ private void processRejections(double now) {
431429
}
432430

433431
rejectedEventIds.clear();
432+
433+
Iterator<Id<Person>> stuckIterator = stuckUponActivity.iterator();
434+
while (stuckIterator.hasNext()) {
435+
Id<Person> personId = stuckIterator.next();
436+
MobsimAgent agent = internalInterface.getMobsim().getAgents().get(personId);
437+
438+
PlanElement planElement = WithinDayAgentUtils.getCurrentPlanElement(agent);
439+
if (planElement instanceof Activity activity) {
440+
abortAgent(agent, activity, now);
441+
stuckIterator.remove();
442+
}
443+
}
444+
}
445+
446+
private void abortAgent(MobsimAgent agent, Activity activity, double now) {
447+
// this would be the simple way, but then it looks like the agent is stuck
448+
// because of something related to the activity. however, we want to make
449+
//it clear that drt is the cause here
450+
451+
// agent.setStateToAbort(now);
452+
// internalInterface.arrangeNextAgentState(agent);
453+
454+
activity.setEndTime(Double.POSITIVE_INFINITY);
455+
activity.setMaximumDurationUndefined();
456+
457+
((HasModifiablePlan) agent).resetCaches();
458+
internalInterface.getMobsim().rescheduleActivityEnd(agent);
459+
eventsManager.processEvent(new PersonStuckEvent(now, agent.getId(), agent.getCurrentLinkId(),
460+
null, mode + ABORT_STUCK_REASON));
461+
462+
internalInterface.getMobsim().getAgentCounter().incLost();
463+
464+
// will be called by the activity engine
465+
// internalInterface.getMobsim().getAgentCounter().decLiving();
434466
}
435467

436468
// Stuck

matsim/src/main/java/org/matsim/api/core/v01/events/PersonStuckEvent.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,23 @@ public class PersonStuckEvent extends Event implements HasPersonId, HasLinkId {
3434
public static final String ATTRIBUTE_LINK = "link";
3535
public static final String ATTRIBUTE_LEGMODE = "legMode";
3636
public static final String ATTRIBUTE_PERSON = "person";
37+
public static final String ATTRIBUTE_REASON = "reason";
3738

3839
private final Id<Person> personId;
3940
private final Id<Link> linkId;
4041
private final String legMode;
42+
private final String reason;
4143

42-
public PersonStuckEvent(final double time, final Id<Person> agentId, final Id<Link> linkId, final String legMode) {
44+
public PersonStuckEvent(final double time, final Id<Person> agentId, final Id<Link> linkId, final String legMode, String reason) {
4345
super(time);
4446
this.personId = agentId;
4547
this.linkId = linkId;
4648
this.legMode = legMode;
49+
this.reason = reason;
50+
}
51+
52+
public PersonStuckEvent(final double time, final Id<Person> agentId, final Id<Link> linkId, final String legMode) {
53+
this(time, agentId, linkId, legMode, null);
4754
}
4855

4956
public Id<Person> getPersonId() {
@@ -58,6 +65,10 @@ public String getLegMode() {
5865
return this.legMode;
5966
}
6067

68+
public String getReason() {
69+
return this.reason;
70+
}
71+
6172
@Override
6273
public String getEventType() {
6374
return EVENT_TYPE;
@@ -70,6 +81,9 @@ public Map<String, String> getAttributes() {
7081
if (this.legMode != null) {
7182
attr.put(ATTRIBUTE_LEGMODE, this.legMode);
7283
}
84+
if (this.reason != null) {
85+
attr.put(ATTRIBUTE_REASON, this.legMode);
86+
}
7387
return attr;
7488
}
7589

@@ -82,6 +96,10 @@ public void writeAsXML(StringBuilder out) {
8296
XmlUtils.writeEncodedAttributeKeyValue(out, ATTRIBUTE_LEGMODE, this.legMode);
8397
}
8498

99+
if (this.reason != null) {
100+
XmlUtils.writeEncodedAttributeKeyValue(out, ATTRIBUTE_REASON, this.reason);
101+
}
102+
85103
writeXMLEnd(out);
86104
}
87105
}

0 commit comments

Comments
 (0)