From ead05fb860dd64fe999deb79d80862d7ed061163 Mon Sep 17 00:00:00 2001 From: sime94 Date: Tue, 8 Oct 2024 16:31:11 +0200 Subject: [PATCH] add test + feinschliff --- ...case-test_experienced_plans_1person.xml.gz | Bin 0 -> 1980 bytes .../run/prepare/PrepareDrtScenarioAgents.java | 43 ++++++---- .../matsim/run/scenarios/LausitzScenario.java | 4 +- .../prepare/PrepareDrtScenarioAgentsTest.java | 75 ++++++++++++++++++ 4 files changed, 106 insertions(+), 16 deletions(-) create mode 100644 input/v1.1/lausitz-pt-case-test_experienced_plans_1person.xml.gz create mode 100644 src/test/java/org/matsim/run/prepare/PrepareDrtScenarioAgentsTest.java diff --git a/input/v1.1/lausitz-pt-case-test_experienced_plans_1person.xml.gz b/input/v1.1/lausitz-pt-case-test_experienced_plans_1person.xml.gz new file mode 100644 index 0000000000000000000000000000000000000000..12097b49de657496c74a3ee0d7ae05d7abf4e79e GIT binary patch literal 1980 zcmV;t2SfNDiwFoVAO&Xx0Bm7(b7^#XEpT)#V_|b;Ep%mbbYEq7aAk67Wo~0-WM6P> zVQzC@F>qyab8l`gcx`L|?ONMz(?Ae?hOaQ@m2CEYu^M>bA|Zi5fRK2yOk#>5O(NSZ zREU3P)=9ka+9b}UP$Z&ARkog8&z#vaXU9H!^!;KQT%~!D&1a*KARGqiY&@T2v-44S zS$-Pm@X^D&XZN1Gdi?Ig>!-nDzPOwwC0rf6{qXkP)0aW`xhxmaaQN-pH*}GdMRtMa z`T6iuHcgA+q?`<$Nqi*{jD!#G-aT`p_Se0^AQ+wG6AT70;hnPu_yjkZO-7;Q6blNg z-htt1G7F0FJWofVp@;~^8Pmj2rL_rzBAuq=G6kdSw6GRnl8npjDl4yp@_GSQpXV28 z%%#Q~LY0!ex_rf$gxYr&>4wTUB}GjqSv?CSTvM%~g#4bJ#XZ zYuh1@d<{|x*Pr8g_$0N}A|f3TyRk!vcy0J!I`x-MmE2YIAhqiQsi5LFfD{on5taJ` zxib6@WPc!Uc%KS5l0=kLetBa%;jL-dGxUN{oqPU4csYS_tcdLLrvWZA!XUzM{W-qI z){C#PHBv!|C0a?PgC8T$DG7;l{4*^kx%9${0lgL&JzsgEiH?VXVq1IYAkf5d~^&>LF>tV7qy?M$t3$ z{U$tOC*O;wLh&~&yC`D>5GXdWc~LQ!6ev zjxQ8x_(;kH6)ivo^m~@Rcw;*OU6c+^XLcls4y)2d1Nlz0IhPF2~Y%(vv7A38(k-}Ej5M`sWs8f5@Y=Z zna@}ib#H8EF4yGb8Txot-~QQrQ;F9mCl^64>#mALj62=fOgoJ5(Nt8e^q25klzq@+ z62AS>+lPy&(B!sq(Gr+;T8Pg0G|qccVn~fn4tn!+@c!*<0yB3}4`m=jed=*mOUBvC zCp!KO>lB~o;2ren7!ol)nsus3tfed?u&@9v1RW{97DHSrt`Sf&w(^#?(w3VDJ5A)q z4(JiB3_W0MH0jA(fcxi|M>~E9U4kh|jbu=syBuk@{xF>OK^hY#4R8jPtS=9xIi%L4 zh>3d`>egLLA|*f}$Tg18rj$tg0P2b?*3APqc95uD8McpPH_*d95rB2jJc_Uh-|0s~ z(MrUb)RaNHKpEWsXzB0A#$?ob6^F!4HtL4WUYU9HDhgjwYUED2_OW8QbH- z%jShQwli^S>hTQ!v19ZyhF*@r6ASW3&e5>Y-@l(w>&GuISyV||)JH5>VcZ3Th%$mg3%y%rm} z2=;%q23+xz4>8H?E50%%I%f1v{y)->szBT>u&JDbh4U=!Y O^8E{=h(u(0E&u= indexes = TripStructureUtils.getLegs(selected).stream() - .filter(l -> l.getRoute().getStartLinkId().toString().contains("pt_vsp_") - && l.getRoute().getEndLinkId().toString().contains("pt_vsp_")) - .map(l -> selected.getPlanElements().indexOf(l)).toList(); + List indexes = getNewPtLineIndexes(selected); + +// only remove routes from legs if no legs with new vsp pt line + if (indexes.isEmpty()) { + TripStructureUtils.getLegs(selected).forEach(CleanPopulation::removeRouteFromLeg); + continue; + } for (Integer index : indexes) { for (int i = 0; i < selected.getPlanElements().size(); i++) { @@ -105,8 +106,10 @@ private void convertVspRegionalTrainLegsToDrt(Population population, Network net person.getId(), TransportMode.walk, TransportMode.pt, i, leg.getMode(), leg.getRoutingMode()); throw new IllegalStateException(); } - leg.setRoute(null); + CleanPopulation.removeRouteFromLeg(leg); leg.setRoutingMode(TransportMode.drt); + leg.setTravelTimeUndefined(); + leg.setDepartureTimeUndefined(); continue; } @@ -130,17 +133,20 @@ private void convertVspRegionalTrainLegsToDrt(Population population, Network net // pt leg with new pt line leg.setRoute(null); leg.setMode(TransportMode.drt); + leg.setTravelTimeUndefined(); + leg.setDepartureTimeUndefined(); + leg.getAttributes().removeAttribute("enterVehicleTime"); continue; } if (i == index + 1 && selected.getPlanElements().get(i) instanceof Activity act) { -// interaction act before leg +// interaction act after leg if (!act.getType().equals(PT_INTERACTION)) { logNotPtInteractionAct(person, act, i); throw new IllegalStateException(); } - if (selected.getPlanElements().get(i + 2) instanceof Activity prev) { - convertToDrtInteraction(act, prev, network, filtered); + if (selected.getPlanElements().get(i + 2) instanceof Activity next) { + convertToDrtInteraction(act, next, network, filtered); } else { logWrongPlanElementType(person, i); throw new IllegalStateException(); @@ -151,6 +157,13 @@ private void convertVspRegionalTrainLegsToDrt(Population population, Network net } } + public static @NotNull List getNewPtLineIndexes(Plan selected) { + return TripStructureUtils.getLegs(selected).stream() + .filter(l -> l.getRoute().getStartLinkId().toString().contains("pt_vsp_") + && l.getRoute().getEndLinkId().toString().contains("pt_vsp_")) + .map(l -> selected.getPlanElements().indexOf(l)).toList(); + } + private static void logNotPtInteractionAct(Person person, Activity act, int i) { log.fatal("For selected plan of person {} type {} expected for activity at index {}. Activity has type {} instead. Abort.", person.getId(), PT_INTERACTION, i, act.getType()); diff --git a/src/main/java/org/matsim/run/scenarios/LausitzScenario.java b/src/main/java/org/matsim/run/scenarios/LausitzScenario.java index d082bb2..30803be 100644 --- a/src/main/java/org/matsim/run/scenarios/LausitzScenario.java +++ b/src/main/java/org/matsim/run/scenarios/LausitzScenario.java @@ -34,6 +34,7 @@ import org.matsim.run.analysis.CommunityFilter; import org.matsim.run.analysis.CommuterAnalysis; import org.matsim.run.analysis.DistanceMatrix; +import org.matsim.run.prepare.PrepareDrtScenarioAgents; import org.matsim.run.prepare.PrepareNetwork; import org.matsim.run.prepare.PreparePopulation; import org.matsim.simwrapper.SimWrapperConfigGroup; @@ -54,7 +55,8 @@ CreateNetworkFromSumo.class, CreateTransitScheduleFromGtfs.class, TrajectoryToPlans.class, GenerateShortDistanceTrips.class, MergePopulations.class, ExtractRelevantFreightTrips.class, DownSamplePopulation.class, ExtractHomeCoordinates.class, CleanNetwork.class, CreateLandUseShp.class, ResolveGridCoordinates.class, FixSubtourModes.class, AdjustActivityToLinkDistances.class, XYToLinks.class, - SplitActivityTypesDuration.class, CreateCountsFromBAStData.class, PreparePopulation.class, CleanPopulation.class, PrepareNetwork.class + SplitActivityTypesDuration.class, CreateCountsFromBAStData.class, PreparePopulation.class, CleanPopulation.class, PrepareNetwork.class, + PrepareDrtScenarioAgents.class }) @MATSimApplication.Analysis({ LinkStats.class, CheckPopulation.class, CommuterAnalysis.class, CommunityFilter.class, DistanceMatrix.class diff --git a/src/test/java/org/matsim/run/prepare/PrepareDrtScenarioAgentsTest.java b/src/test/java/org/matsim/run/prepare/PrepareDrtScenarioAgentsTest.java new file mode 100644 index 0000000..61b690a --- /dev/null +++ b/src/test/java/org/matsim/run/prepare/PrepareDrtScenarioAgentsTest.java @@ -0,0 +1,75 @@ +package org.matsim.run.prepare; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.matsim.api.core.v01.Id; +import org.matsim.api.core.v01.TransportMode; +import org.matsim.api.core.v01.population.*; +import org.matsim.application.MATSimApplication; +import org.matsim.core.population.PopulationUtils; +import org.matsim.run.scenarios.LausitzScenario; +import org.matsim.testcases.MatsimTestUtils; + +import java.util.List; + +class PrepareDrtScenarioAgentsTest { + @RegisterExtension + private final MatsimTestUtils utils = new MatsimTestUtils(); + private static final String URL = String.format("https://svn.vsp.tu-berlin.de/repos/public-svn/matsim/scenarios/countries/de/lausitz/input/v%s/", + LausitzScenario.VERSION); + private static final Id PERSON_ID = Id.createPersonId("642052"); + + @Test + void testPrepareDrtScenarioAgents() { + String inputPopulationPath = "./input/v1.1/lausitz-pt-case-test_experienced_plans_1person.xml.gz"; + Population in = PopulationUtils.readPopulation(inputPopulationPath); + String networkPath = URL + String.format("lausitz-v%s-network-with-pt.xml.gz", LausitzScenario.VERSION); + String outPath = utils.getOutputDirectory() + "/drt-test-population.xml.gz"; + + assert MATSimApplication.execute(LausitzScenario.class, "prepare", "prepare-drt-agents", + inputPopulationPath, + "--network", networkPath, + "--output", outPath, + "--shp", "./input/shp/lausitz.shp") + == 0 : "Must return non error code"; + + Population out = PopulationUtils.readPopulation(outPath); + List outSelectedPlanElements = out.getPersons().get(PERSON_ID).getSelectedPlan().getPlanElements(); + +// there is only 1 person in the population + for (int index : PrepareDrtScenarioAgents.getNewPtLineIndexes(in.getPersons().get(PERSON_ID).getSelectedPlan())) { +// access leg + Assertions.assertInstanceOf(Leg.class, outSelectedPlanElements.get(index - 2)); + Leg access = (Leg) outSelectedPlanElements.get(index - 2); + Assertions.assertNull(access.getRoute()); + Assertions.assertEquals(TransportMode.drt, access.getRoutingMode()); + +// interaction act before leg + Assertions.assertInstanceOf(Activity.class, outSelectedPlanElements.get(index - 1)); + Activity before = (Activity) outSelectedPlanElements.get(index - 1); + Assertions.assertNull(before.getCoord()); + Assertions.assertNull(before.getFacilityId()); + Assertions.assertEquals("drt interaction", before.getType()); + +// pt leg which was converted to drt leg + Assertions.assertInstanceOf(Leg.class, outSelectedPlanElements.get(index)); + Leg leg = (Leg) outSelectedPlanElements.get(index); + Assertions.assertNull(leg.getRoute()); + Assertions.assertEquals(TransportMode.drt, leg.getMode()); + + // interaction act after leg + Assertions.assertInstanceOf(Activity.class, outSelectedPlanElements.get(index + 1)); + Activity after = (Activity) outSelectedPlanElements.get(index + 1); + Assertions.assertNull(after.getCoord()); + Assertions.assertNull(after.getFacilityId()); + Assertions.assertEquals("drt interaction", after.getType()); + + // egress leg + Assertions.assertInstanceOf(Leg.class, outSelectedPlanElements.get(index + 2)); + Leg egress = (Leg) outSelectedPlanElements.get(index + 2); + Assertions.assertNull(egress.getRoute()); + Assertions.assertEquals(TransportMode.drt, egress.getRoutingMode()); + } + } +}