Skip to content

Commit

Permalink
extend population comparison
Browse files Browse the repository at this point in the history
add attributes comparison
  • Loading branch information
nkuehnel committed May 2, 2024
1 parent 2bd106f commit af6cde9
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.population.*;
import org.matsim.utils.objectattributes.attributable.AttributesComparison;

import java.util.Iterator;
import java.util.List;
import java.util.Optional;

public class PopulationComparison{
public enum Result { equal, notEqual }
Expand All @@ -24,34 +26,70 @@ public static Result compare( Population population1, Population population2 ){
while( it1.hasNext() || it2.hasNext() ) {
if ( ! it1.hasNext() ) {
result = Result.notEqual ;
log.warn( "" );
log.warn( " different length in populations. " );
return result ;
}
if ( ! it2.hasNext() ) {
result = Result.notEqual ;
log.warn( "" );
log.warn( " different length in populations. " );
return result ;
}

Person person1 = it1.next();
Person person2 = it2.next();

if ( !person1.getId().equals( person2.getId() ) ) {
log.warn( "persons out of sequence" );
log.warn( "" );
log.warn( "persons out of sequence p1: " + person1.getId() + " | p2: " + person2.getId());
result = Result.notEqual ;
continue;
}

if(!AttributesComparison.equals(person1.getAttributes(), person2.getAttributes())) {
log.warn( "" );
log.warn( "person attributes different p1: " + person1.getId() + " | p2: " + person2.getId());
}

Plan plan1 = person1.getSelectedPlan();
Plan plan2 = person2.getSelectedPlan();
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() ){
if ( plan.getScore() > maxScore ) {
maxScore = plan.getScore() ;
if(!AttributesComparison.equals(plan1.getAttributes(), plan2.getAttributes())) {
log.warn( "" );
log.warn( "selected plan attributes different p1: " + person1.getId() + " | p2: " + person2.getId());
}

Optional<Double> score1 = Optional.ofNullable(plan1.getScore());
Optional<Double> score2 = Optional.ofNullable(plan2.getScore());

if(score1.isPresent() && score2.isPresent()) {
if ( Math.abs( plan1.getScore() - plan2.getScore() ) > 100.*Double.MIN_VALUE) {

double maxScore = Double.NEGATIVE_INFINITY;
for( Plan plan : person2.getPlans() ){
if ( plan.getScore() > maxScore ) {
maxScore = plan.getScore() ;
}
}
}

log.warn( "" );
log.warn("personId=" + person1.getId() + "; score1=" + plan1.getScore() + "; score2=" + plan2.getScore() + "; maxScore2=" + maxScore ) ;
log.warn( "" );

result = Result.notEqual;

}
} else if(score1.isEmpty() && score2.isEmpty()) {} else {
log.warn( "" );
log.warn("personId=" + person1.getId() + "; score1=" + plan1.getScore() + "; score2=" + plan2.getScore() + "; maxScore2=" + maxScore ) ;
log.warn( " selected plan scores not consistently present: p1: " + person1.getId() + " | p2: " + person2.getId());
result = Result.notEqual;
}

if(!equals(plan1.getPlanElements(), plan2.getPlanElements())) {
log.warn( "" );
log.warn( " selected plan elements not equal: p1: " + person1.getId() + " | p2: " + person2.getId() );

for( PlanElement planElement : plan1.getPlanElements() ){
log.warn( planElement );
}
Expand All @@ -60,7 +98,7 @@ public static Result compare( Population population1, Population population2 ){
log.warn( planElement );
}
log.warn( "" );

result = Result.notEqual;
}
}
return result ;
Expand All @@ -86,6 +124,9 @@ public static boolean equals(List<PlanElement> planElements,
*
*/
private static boolean equals(PlanElement o1, PlanElement o2) {
if(!AttributesComparison.equals(o1.getAttributes(), o2.getAttributes())) {
return false;
}
if (o1 instanceof Leg) {
if (o2 instanceof Leg) {
Leg leg1 = (Leg) o1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.matsim.utils.objectattributes.attributable;

import com.google.common.base.Equivalence;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.matsim.vehicles.PersonVehicles;

import java.util.Map;

/**
* @author nkuehnel / MOIA
*/
public final class AttributesComparison {

private AttributesComparison(){}

public static boolean equals(Attributes a1, Attributes a2) {
if(a1.size() != a2.size()) {
return false;
}

return Maps.difference(a1.getAsMap(), a2.getAsMap(), new CustomEquivalence()).areEqual();
}

private static class CustomEquivalence extends Equivalence<Object> {
@Override
protected boolean doEquivalent(Object a, Object b) {
if (a instanceof Map<?, ?> mapA && b instanceof Map<?, ?> mapB) {
return Maps.difference(mapA, mapB, new CustomEquivalence()).areEqual();
} else if (a instanceof PersonVehicles vehiclesA && b instanceof PersonVehicles vehiclesB) {
return Maps.difference(vehiclesA.getModeVehicles(), vehiclesB.getModeVehicles(), new CustomEquivalence()).areEqual();
}
return a.equals(b);
}

@Override
protected int doHash(Object o) {
return o.hashCode();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,22 @@ void testGetAsMap() {
Assertions.fail("Expected NoSuchElementException, but caught a different one.");
}
}

@Test
void testComparison() {

AttributesImpl a1 = new AttributesImpl();
AttributesImpl a2 = new AttributesImpl();
Assertions.assertTrue(AttributesComparison.equals(a1, a2));

a1.putAttribute("att1", "1");
Assertions.assertFalse(AttributesComparison.equals(a1, a2));

a2.putAttribute("att1", "1");
Assertions.assertTrue(AttributesComparison.equals(a1, a2));

a2.putAttribute("att1", "one");
Assertions.assertFalse(AttributesComparison.equals(a1, a2));

}
}

0 comments on commit af6cde9

Please sign in to comment.