From 564cf47cdec69482b1bd442fbe353edfc2e14d0a Mon Sep 17 00:00:00 2001 From: nkuehnel Date: Tue, 30 Apr 2024 12:00:49 +0200 Subject: [PATCH] move pairwise plan elements comparison from test utils to population comparison --- .../routes/PopulationComparison.java | 73 +++++++++++++++++-- .../ChooseRandomLegModeForSubtourTest.java | 17 +++-- .../population/algorithms/TestsUtil.java | 61 ---------------- 3 files changed, 75 insertions(+), 76 deletions(-) diff --git a/matsim/src/main/java/org/matsim/core/population/routes/PopulationComparison.java b/matsim/src/main/java/org/matsim/core/population/routes/PopulationComparison.java index d6cc75e3923..8dac954aa3f 100644 --- a/matsim/src/main/java/org/matsim/core/population/routes/PopulationComparison.java +++ b/matsim/src/main/java/org/matsim/core/population/routes/PopulationComparison.java @@ -2,12 +2,10 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.matsim.api.core.v01.population.Person; -import org.matsim.api.core.v01.population.Plan; -import org.matsim.api.core.v01.population.PlanElement; -import org.matsim.api.core.v01.population.Population; +import org.matsim.api.core.v01.population.*; import java.util.Iterator; +import java.util.List; public class PopulationComparison{ public enum Result { equal, notEqual } @@ -39,7 +37,8 @@ public Result compare( Population population1, Population population2 ){ } Plan plan1 = person1.getSelectedPlan(); Plan plan2 = person2.getSelectedPlan(); - if ( Math.abs( plan1.getScore() - plan2.getScore() ) > 100.*Double.MIN_VALUE ) { + if ( Math.abs( plan1.getScore() - plan2.getScore() ) > 100.*Double.MIN_VALUE || + !equals(plan1.getPlanElements(), plan2.getPlanElements())) { double maxScore = Double.NEGATIVE_INFINITY; for( Plan plan : person2.getPlans() ){ @@ -61,10 +60,70 @@ public Result compare( Population population1, Population population2 ){ log.warn( "" ); } - } + return result ; + } - return result ; + public static boolean equals(List planElements, + List planElements2) { + int nElements = planElements.size(); + if (nElements != planElements2.size()) { + return false; + } else { + for (int i = 0; i < nElements; i++) { + if (!equals(planElements.get(i), planElements2.get(i))) { + return false; + } + } + } + return true; + } + + /* Warning: This is NOT claimed to be correct. (It isn't.) + * + */ + private static boolean equals(PlanElement o1, PlanElement o2) { + if (o1 instanceof Leg) { + if (o2 instanceof Leg) { + Leg leg1 = (Leg) o1; + Leg leg2 = (Leg) o2; + if (!leg1.getDepartureTime().equals(leg2.getDepartureTime())) { + return false; + } + if (!leg1.getMode().equals(leg2.getMode())) { + return false; + } + if (!leg1.getTravelTime().equals(leg2.getTravelTime())) { + return false; + } + } else { + return false; + } + } else if (o1 instanceof Activity) { + if (o2 instanceof Activity) { + Activity activity1 = (Activity) o1; + Activity activity2 = (Activity) o2; + if (activity1.getEndTime().isUndefined() ^ activity2.getEndTime().isUndefined()) { + return false; + } + if (activity1.getEndTime().isDefined() && activity1.getEndTime().seconds() + != activity2.getEndTime().seconds()) { + return false; + } + if (activity1.getStartTime().isUndefined() ^ activity2.getStartTime().isUndefined()) { + return false; + } + if (activity1.getStartTime().isDefined() && activity1.getStartTime().seconds() + != activity2.getStartTime().seconds()) { + return false; + } + } else { + return false; + } + } else { + throw new RuntimeException ("Unexpected PlanElement"); + } + return true; } } diff --git a/matsim/src/test/java/org/matsim/population/algorithms/ChooseRandomLegModeForSubtourTest.java b/matsim/src/test/java/org/matsim/population/algorithms/ChooseRandomLegModeForSubtourTest.java index 93986fe0404..0bb065a1ee5 100644 --- a/matsim/src/test/java/org/matsim/population/algorithms/ChooseRandomLegModeForSubtourTest.java +++ b/matsim/src/test/java/org/matsim/population/algorithms/ChooseRandomLegModeForSubtourTest.java @@ -46,6 +46,7 @@ import org.matsim.core.population.PopulationUtils; import org.matsim.core.population.algorithms.ChooseRandomLegModeForSubtour; import org.matsim.core.population.algorithms.PermissibleModesCalculator; +import org.matsim.core.population.routes.PopulationComparison; import org.matsim.core.replanning.modules.SubtourModeChoice; import org.matsim.core.router.MainModeIdentifierImpl; import org.matsim.core.router.TripStructureUtils; @@ -291,7 +292,7 @@ private void testSubTourMutationToCar(Network network, double probaForRandomSing Plan plan = createPlan(network, activityChainString, originalMode); Plan originalPlan = PopulationUtils.createPlan(person); PopulationUtils.copyFromTo(plan, originalPlan); - assertTrue(TestsUtil.equals(plan.getPlanElements(), originalPlan.getPlanElements())); + assertTrue(PopulationComparison.equals(plan.getPlanElements(), originalPlan.getPlanElements())); testee.run(plan); assertSubTourMutated(plan, originalPlan, expectedMode, false); } @@ -307,7 +308,7 @@ private void testSubTourMutationToCar(ActivityFacilities facilities, double prob Plan plan = createPlan(facilities, activityChainString, originalMode); Plan originalPlan = PopulationUtils.createPlan(person); PopulationUtils.copyFromTo(plan, originalPlan); - assertTrue(TestsUtil.equals(plan.getPlanElements(), originalPlan.getPlanElements())); + assertTrue(PopulationComparison.equals(plan.getPlanElements(), originalPlan.getPlanElements())); testee.run(plan); assertSubTourMutated(plan, originalPlan, expectedMode, true); } @@ -322,9 +323,9 @@ private void testUnknownModeDoesntMutate(Network network, double probaForRandomS Plan plan = createPlan(network, activityChainString, originalMode); Plan originalPlan = PopulationUtils.createPlan(person); PopulationUtils.copyFromTo(plan, originalPlan); - assertTrue(TestsUtil.equals(plan.getPlanElements(), originalPlan.getPlanElements())); + assertTrue(PopulationComparison.equals(plan.getPlanElements(), originalPlan.getPlanElements())); testee.run(plan); - assertTrue(TestsUtil.equals(plan.getPlanElements(), originalPlan.getPlanElements())); + assertTrue(PopulationComparison.equals(plan.getPlanElements(), originalPlan.getPlanElements())); } } @@ -337,9 +338,9 @@ private void testUnknownModeDoesntMutate(ActivityFacilities facilities, double p Plan plan = createPlan(facilities, activityChainString, originalMode); Plan originalPlan = PopulationUtils.createPlan(person); PopulationUtils.copyFromTo(plan, originalPlan); - assertTrue(TestsUtil.equals(plan.getPlanElements(), originalPlan.getPlanElements())); + assertTrue(PopulationComparison.equals(plan.getPlanElements(), originalPlan.getPlanElements())); testee.run(plan); - assertTrue(TestsUtil.equals(plan.getPlanElements(), originalPlan.getPlanElements())); + assertTrue(PopulationComparison.equals(plan.getPlanElements(), originalPlan.getPlanElements())); } } @@ -353,7 +354,7 @@ private void testSubTourMutationToPt(ActivityFacilities facilities, double proba Plan plan = createPlan(facilities, activityChainString, originalMode); Plan originalPlan = PopulationUtils.createPlan(person); PopulationUtils.copyFromTo(plan, originalPlan); - assertTrue(TestsUtil.equals(plan.getPlanElements(), originalPlan.getPlanElements())); + assertTrue(PopulationComparison.equals(plan.getPlanElements(), originalPlan.getPlanElements())); testee.run(plan); assertSubTourMutated(plan, originalPlan, expectedMode, true); } @@ -369,7 +370,7 @@ private void testSubTourMutationToPt(Network network, double probaForRandomSingl Plan plan = createPlan(network, activityChainString, originalMode); Plan originalPlan = PopulationUtils.createPlan(person); PopulationUtils.copyFromTo(plan, originalPlan); - assertTrue(TestsUtil.equals(plan.getPlanElements(), originalPlan.getPlanElements())); + assertTrue(PopulationComparison.equals(plan.getPlanElements(), originalPlan.getPlanElements())); testee.run(plan); assertSubTourMutated(plan, originalPlan, expectedMode, false); } diff --git a/matsim/src/test/java/org/matsim/population/algorithms/TestsUtil.java b/matsim/src/test/java/org/matsim/population/algorithms/TestsUtil.java index 2c18cc6e194..34364d60d6f 100644 --- a/matsim/src/test/java/org/matsim/population/algorithms/TestsUtil.java +++ b/matsim/src/test/java/org/matsim/population/algorithms/TestsUtil.java @@ -68,66 +68,5 @@ static Plan createPlanFromLinks(Network layer, Person person, String mode, Strin return plan; } - /* Warning: This is NOT claimed to be correct. (It isn't.) - * - */ - static boolean equals(PlanElement o1, PlanElement o2) { - if (o1 instanceof Leg) { - if (o2 instanceof Leg) { - Leg leg1 = (Leg) o1; - Leg leg2 = (Leg) o2; - if (!leg1.getDepartureTime().equals(leg2.getDepartureTime())) { - return false; - } - if (!leg1.getMode().equals(leg2.getMode())) { - return false; - } - if (!leg1.getTravelTime().equals(leg2.getTravelTime())) { - return false; - } - } else { - return false; - } - } else if (o1 instanceof Activity) { - if (o2 instanceof Activity) { - Activity activity1 = (Activity) o1; - Activity activity2 = (Activity) o2; - if (activity1.getEndTime().isUndefined() ^ activity2.getEndTime().isUndefined()) { - return false; - } - if (activity1.getEndTime().isDefined() && activity1.getEndTime().seconds() - != activity2.getEndTime().seconds()) { - return false; - } - if (activity1.getStartTime().isUndefined() ^ activity2.getStartTime().isUndefined()) { - return false; - } - if (activity1.getStartTime().isDefined() && activity1.getStartTime().seconds() - != activity2.getStartTime().seconds()) { - return false; - } - } else { - return false; - } - } else { - throw new RuntimeException ("Unexpected PlanElement"); - } - return true; - } - - public static boolean equals(List planElements, - List planElements2) { - int nElements = planElements.size(); - if (nElements != planElements2.size()) { - return false; - } else { - for (int i = 0; i < nElements; i++) { - if (!equals(planElements.get(i), planElements2.get(i))) { - return false; - } - } - } - return true; - } }