Skip to content

Commit

Permalink
move pairwise plan elements comparison from test utils to population …
Browse files Browse the repository at this point in the history
…comparison
  • Loading branch information
nkuehnel committed Apr 30, 2024
1 parent 9955199 commit 564cf47
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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() ){
Expand All @@ -61,10 +60,70 @@ public Result compare( Population population1, Population population2 ){
log.warn( "" );

}

}
return result ;
}


return result ;
public static boolean equals(List<PlanElement> planElements,
List<PlanElement> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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()));
}
}

Expand All @@ -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()));
}
}

Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PlanElement> planElements,
List<PlanElement> 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;
}

}

0 comments on commit 564cf47

Please sign in to comment.