Skip to content

Commit

Permalink
TSP and VRP: rename getDistance() to getDistanceTo() to make it clear…
Browse files Browse the repository at this point in the history
… which way an asymmetric distance it's going.
  • Loading branch information
ge0ffrey committed Apr 3, 2015
1 parent 0f6e824 commit 3d7510b
Show file tree
Hide file tree
Showing 19 changed files with 45 additions and 46 deletions.
Expand Up @@ -42,7 +42,7 @@ public void setLocation(Location location) {
* @return a positive number, the distance multiplied by 1000 to avoid floating point arithmetic rounding errors * @return a positive number, the distance multiplied by 1000 to avoid floating point arithmetic rounding errors
*/ */
public long getDistanceTo(Standstill standstill) { public long getDistanceTo(Standstill standstill) {
return location.getDistance(standstill.getLocation()); return location.getDistanceTo(standstill.getLocation());
} }


@Override @Override
Expand Down
Expand Up @@ -23,7 +23,6 @@
import org.optaplanner.examples.common.domain.AbstractPersistable; import org.optaplanner.examples.common.domain.AbstractPersistable;
import org.optaplanner.examples.tsp.domain.location.Location; import org.optaplanner.examples.tsp.domain.location.Location;
import org.optaplanner.examples.tsp.domain.solver.DomicileAngleVisitDifficultyWeightFactory; import org.optaplanner.examples.tsp.domain.solver.DomicileAngleVisitDifficultyWeightFactory;
import org.optaplanner.examples.tsp.domain.solver.LatitudeVisitDifficultyComparator;


@PlanningEntity(difficultyWeightFactoryClass = DomicileAngleVisitDifficultyWeightFactory.class) @PlanningEntity(difficultyWeightFactoryClass = DomicileAngleVisitDifficultyWeightFactory.class)
@XStreamAlias("Visit") @XStreamAlias("Visit")
Expand Down Expand Up @@ -71,15 +70,15 @@ public long getDistanceFromPreviousStandstill() {
* @return a positive number, the distance multiplied by 1000 to avoid floating point arithmetic rounding errors * @return a positive number, the distance multiplied by 1000 to avoid floating point arithmetic rounding errors
*/ */
public long getDistanceFrom(Standstill standstill) { public long getDistanceFrom(Standstill standstill) {
return standstill.getLocation().getDistance(location); return standstill.getLocation().getDistanceTo(location);
} }


/** /**
* @param standstill never null * @param standstill never null
* @return a positive number, the distance multiplied by 1000 to avoid floating point arithmetic rounding errors * @return a positive number, the distance multiplied by 1000 to avoid floating point arithmetic rounding errors
*/ */
public long getDistanceTo(Standstill standstill) { public long getDistanceTo(Standstill standstill) {
return location.getDistance(standstill.getLocation()); return location.getDistanceTo(standstill.getLocation());
} }


@Override @Override
Expand Down
Expand Up @@ -33,8 +33,8 @@ public AirLocation(long id, double latitude, double longitude) {
} }


@Override @Override
public long getDistance(Location location) { public long getDistanceTo(Location location) {
double distance = getAirDistanceDouble(location); double distance = getAirDistanceDoubleTo(location);
// Multiplied by 1000 to avoid floating point arithmetic rounding errors // Multiplied by 1000 to avoid floating point arithmetic rounding errors
return (long) (distance * 1000.0 + 0.5); return (long) (distance * 1000.0 + 0.5);
} }
Expand Down
Expand Up @@ -75,9 +75,9 @@ public void setLongitude(double longitude) {
* @param location never null * @param location never null
* @return a positive number, the distance multiplied by 1000 to avoid floating point arithmetic rounding errors * @return a positive number, the distance multiplied by 1000 to avoid floating point arithmetic rounding errors
*/ */
public abstract long getDistance(Location location); public abstract long getDistanceTo(Location location);


public double getAirDistanceDouble(Location location) { public double getAirDistanceDoubleTo(Location location) {
// Implementation specified by TSPLIB http://www2.iwr.uni-heidelberg.de/groups/comopt/software/TSPLIB95/ // Implementation specified by TSPLIB http://www2.iwr.uni-heidelberg.de/groups/comopt/software/TSPLIB95/
// Euclidean distance (Pythagorean theorem) - not correct when the surface is a sphere // Euclidean distance (Pythagorean theorem) - not correct when the surface is a sphere
double latitudeDifference = location.latitude - latitude; double latitudeDifference = location.latitude - latitude;
Expand Down
Expand Up @@ -47,7 +47,7 @@ public void setTravelDistanceMap(Map<RoadLocation, Double> travelDistanceMap) {
} }


@Override @Override
public long getDistance(Location location) { public long getDistanceTo(Location location) {
if (this == location) { if (this == location) {
return 0L; return 0L;
} }
Expand Down
Expand Up @@ -32,8 +32,8 @@ public Comparable createSorterWeight(TravelingSalesmanTour vehicleRoutingSolutio
Domicile domicile = vehicleRoutingSolution.getDomicile(); Domicile domicile = vehicleRoutingSolution.getDomicile();
return new DomicileAngleVisitDifficultyWeight(visit, return new DomicileAngleVisitDifficultyWeight(visit,
visit.getLocation().getAngle(domicile.getLocation()), visit.getLocation().getAngle(domicile.getLocation()),
visit.getLocation().getDistance(domicile.getLocation()) visit.getLocation().getDistanceTo(domicile.getLocation())
+ domicile.getLocation().getDistance(visit.getLocation())); + domicile.getLocation().getDistanceTo(visit.getLocation()));
} }


public static class DomicileAngleVisitDifficultyWeight public static class DomicileAngleVisitDifficultyWeight
Expand Down
Expand Up @@ -106,15 +106,15 @@ public int getDistanceFromPreviousStandstill() {
* @return a positive number, the distance multiplied by 1000 to avoid floating point arithmetic rounding errors * @return a positive number, the distance multiplied by 1000 to avoid floating point arithmetic rounding errors
*/ */
public int getDistanceFrom(Standstill standstill) { public int getDistanceFrom(Standstill standstill) {
return standstill.getLocation().getDistance(location); return standstill.getLocation().getDistanceTo(location);
} }


/** /**
* @param standstill never null * @param standstill never null
* @return a positive number, the distance multiplied by 1000 to avoid floating point arithmetic rounding errors * @return a positive number, the distance multiplied by 1000 to avoid floating point arithmetic rounding errors
*/ */
public int getDistanceTo(Standstill standstill) { public int getDistanceTo(Standstill standstill) {
return location.getDistance(standstill.getLocation()); return location.getDistanceTo(standstill.getLocation());
} }


@Override @Override
Expand Down
Expand Up @@ -47,7 +47,7 @@ public void setLocation(Location location) {
* @return a positive number, the distance multiplied by 1000 to avoid floating point arithmetic rounding errors * @return a positive number, the distance multiplied by 1000 to avoid floating point arithmetic rounding errors
*/ */
public int getDistanceTo(Standstill standstill) { public int getDistanceTo(Standstill standstill) {
return location.getDistance(standstill.getLocation()); return location.getDistanceTo(standstill.getLocation());
} }


} }
Expand Up @@ -33,8 +33,8 @@ public AirLocation(long id, double latitude, double longitude) {
} }


@Override @Override
public int getDistance(Location location) { public int getDistanceTo(Location location) {
double distance = getAirDistanceDouble(location); double distance = getAirDistanceDoubleTo(location);
// Multiplied by 1000 to avoid floating point arithmetic rounding errors // Multiplied by 1000 to avoid floating point arithmetic rounding errors
return (int) (distance * 1000.0 + 0.5); return (int) (distance * 1000.0 + 0.5);
} }
Expand Down
Expand Up @@ -75,9 +75,9 @@ public void setLongitude(double longitude) {
* @param location never null * @param location never null
* @return a positive number, the distance multiplied by 1000 to avoid floating point arithmetic rounding errors * @return a positive number, the distance multiplied by 1000 to avoid floating point arithmetic rounding errors
*/ */
public abstract int getDistance(Location location); public abstract int getDistanceTo(Location location);


public double getAirDistanceDouble(Location location) { public double getAirDistanceDoubleTo(Location location) {
// Implementation specified by TSPLIB http://www2.iwr.uni-heidelberg.de/groups/comopt/software/TSPLIB95/ // Implementation specified by TSPLIB http://www2.iwr.uni-heidelberg.de/groups/comopt/software/TSPLIB95/
// Euclidean distance (Pythagorean theorem) - not correct when the surface is a sphere // Euclidean distance (Pythagorean theorem) - not correct when the surface is a sphere
double latitudeDifference = location.latitude - latitude; double latitudeDifference = location.latitude - latitude;
Expand Down
Expand Up @@ -47,7 +47,7 @@ public void setTravelDistanceMap(Map<RoadLocation, Double> travelDistanceMap) {
} }


@Override @Override
public int getDistance(Location location) { public int getDistanceTo(Location location) {
if (this == location) { if (this == location) {
return 0; return 0;
} }
Expand Down
Expand Up @@ -57,7 +57,7 @@ public void setHubTravelDistanceMap(Map<HubSegmentLocation, Double> hubTravelDis
} }


@Override @Override
public int getDistance(Location location) { public int getDistanceTo(Location location) {
double distance; double distance;
if (location instanceof RoadSegmentLocation) { if (location instanceof RoadSegmentLocation) {
distance = getDistanceDouble((RoadSegmentLocation) location); distance = getDistanceDouble((RoadSegmentLocation) location);
Expand Down
Expand Up @@ -59,7 +59,7 @@ public void setHubTravelDistanceMap(Map<HubSegmentLocation, Double> hubTravelDis
} }


@Override @Override
public int getDistance(Location location) { public int getDistanceTo(Location location) {
Double distance = getDistanceDouble((RoadSegmentLocation) location); Double distance = getDistanceDouble((RoadSegmentLocation) location);
// Multiplied by 1000 to avoid floating point arithmetic rounding errors // Multiplied by 1000 to avoid floating point arithmetic rounding errors
return (int) (distance * 1000.0 + 0.5); return (int) (distance * 1000.0 + 0.5);
Expand Down
Expand Up @@ -32,8 +32,8 @@ public Comparable createSorterWeight(VehicleRoutingSolution vehicleRoutingSoluti
Depot depot = vehicleRoutingSolution.getDepotList().get(0); Depot depot = vehicleRoutingSolution.getDepotList().get(0);
return new DepotAngleCustomerDifficultyWeight(customer, return new DepotAngleCustomerDifficultyWeight(customer,
customer.getLocation().getAngle(depot.getLocation()), customer.getLocation().getAngle(depot.getLocation()),
customer.getLocation().getDistance(depot.getLocation()) customer.getLocation().getDistanceTo(depot.getLocation())
+ depot.getLocation().getDistance(customer.getLocation())); + depot.getLocation().getDistanceTo(customer.getLocation()));
} }


public static class DepotAngleCustomerDifficultyWeight public static class DepotAngleCustomerDifficultyWeight
Expand Down
Expand Up @@ -31,8 +31,8 @@ public class DepotDistanceCustomerDifficultyWeightFactory
public Comparable createSorterWeight(VehicleRoutingSolution vehicleRoutingSolution, Customer customer) { public Comparable createSorterWeight(VehicleRoutingSolution vehicleRoutingSolution, Customer customer) {
Depot depot = vehicleRoutingSolution.getDepotList().get(0); Depot depot = vehicleRoutingSolution.getDepotList().get(0);
return new DepotDistanceCustomerDifficultyWeight(customer, return new DepotDistanceCustomerDifficultyWeight(customer,
customer.getLocation().getDistance(depot.getLocation()) customer.getLocation().getDistanceTo(depot.getLocation())
+ depot.getLocation().getDistance(customer.getLocation())); + depot.getLocation().getDistanceTo(customer.getLocation()));
} }


public static class DepotDistanceCustomerDifficultyWeight public static class DepotDistanceCustomerDifficultyWeight
Expand Down
Expand Up @@ -468,7 +468,7 @@ private void readTimeWindowedDepotAndCustomers() throws IOException {
customer.setReadyTime(readyTime); customer.setReadyTime(readyTime);
// Score constraint arrivalAfterDueTimeAtDepot is a build-in hard constraint in VehicleRoutingImporter // Score constraint arrivalAfterDueTimeAtDepot is a build-in hard constraint in VehicleRoutingImporter
int maximumDueTime = depot.getDueTime() int maximumDueTime = depot.getDueTime()
- serviceDuration - location.getDistance(depot.getLocation()); - serviceDuration - location.getDistanceTo(depot.getLocation());
if (dueTime > maximumDueTime) { if (dueTime > maximumDueTime) {
logger.warn("The customer ({})'s dueTime ({}) was automatically reduced" + logger.warn("The customer ({})'s dueTime ({}) was automatically reduced" +
" to maximumDueTime ({}) because of the depot's dueTime ({}).", " to maximumDueTime ({}) because of the depot's dueTime ({}).",
Expand Down
Expand Up @@ -50,7 +50,7 @@ public HardSoftScore calculateScore(VehicleRoutingSolution solution) {
softScore -= customer.getDistanceFromPreviousStandstill(); softScore -= customer.getDistanceFromPreviousStandstill();
if (customer.getNextCustomer() == null) { if (customer.getNextCustomer() == null) {
// Score constraint distanceFromLastCustomerToDepot // Score constraint distanceFromLastCustomerToDepot
softScore -= customer.getLocation().getDistance(vehicle.getLocation()); softScore -= customer.getLocation().getDistanceTo(vehicle.getLocation());
} }
if (timeWindowed) { if (timeWindowed) {
TimeWindowedCustomer timeWindowedCustomer = (TimeWindowedCustomer) customer; TimeWindowedCustomer timeWindowedCustomer = (TimeWindowedCustomer) customer;
Expand Down
Expand Up @@ -149,7 +149,7 @@ private void insertVehicle(Customer customer) {
vehicleDemandMap.put(vehicle, newDemand); vehicleDemandMap.put(vehicle, newDemand);
if (customer.getNextCustomer() == null) { if (customer.getNextCustomer() == null) {
// Score constraint distanceFromLastCustomerToDepot // Score constraint distanceFromLastCustomerToDepot
softScore -= customer.getLocation().getDistance(vehicle.getLocation()); softScore -= customer.getLocation().getDistanceTo(vehicle.getLocation());
} }
} }
} }
Expand All @@ -165,7 +165,7 @@ private void retractVehicle(Customer customer) {
vehicleDemandMap.put(vehicle, newDemand); vehicleDemandMap.put(vehicle, newDemand);
if (customer.getNextCustomer() == null) { if (customer.getNextCustomer() == null) {
// Score constraint distanceFromLastCustomerToDepot // Score constraint distanceFromLastCustomerToDepot
softScore += customer.getLocation().getDistance(vehicle.getLocation()); softScore += customer.getLocation().getDistanceTo(vehicle.getLocation());
} }
} }
} }
Expand All @@ -175,7 +175,7 @@ private void insertNextCustomer(Customer customer) {
if (vehicle != null) { if (vehicle != null) {
if (customer.getNextCustomer() == null) { if (customer.getNextCustomer() == null) {
// Score constraint distanceFromLastCustomerToDepot // Score constraint distanceFromLastCustomerToDepot
softScore -= customer.getLocation().getDistance(vehicle.getLocation()); softScore -= customer.getLocation().getDistanceTo(vehicle.getLocation());
} }
} }
} }
Expand All @@ -185,7 +185,7 @@ private void retractNextCustomer(Customer customer) {
if (vehicle != null) { if (vehicle != null) {
if (customer.getNextCustomer() == null) { if (customer.getNextCustomer() == null) {
// Score constraint distanceFromLastCustomerToDepot // Score constraint distanceFromLastCustomerToDepot
softScore += customer.getLocation().getDistance(vehicle.getLocation()); softScore += customer.getLocation().getDistanceTo(vehicle.getLocation());
} }
} }
} }
Expand Down
Expand Up @@ -51,25 +51,25 @@ public void getDistance() {
z.setNearbyTravelDistanceMap(createNearbyTravelDistanceMap(z, d)); z.setNearbyTravelDistanceMap(createNearbyTravelDistanceMap(z, d));
z.setHubTravelDistanceMap(createHubTravelDistanceMap(z, x, y)); z.setHubTravelDistanceMap(createHubTravelDistanceMap(z, x, y));


assertEquals(sumOfArcs(a, b), a.getDistance(b)); assertEquals(sumOfArcs(a, b), a.getDistanceTo(b));
assertEquals(sumOfArcs(a, c), a.getDistance(c)); assertEquals(sumOfArcs(a, c), a.getDistanceTo(c));
assertEquals(sumOfArcs(a, x, z, d), a.getDistance(d)); assertEquals(sumOfArcs(a, x, z, d), a.getDistanceTo(d));
assertEquals(sumOfArcs(b, a), b.getDistance(a)); assertEquals(sumOfArcs(b, a), b.getDistanceTo(a));
assertEquals(sumOfArcs(b, y, c), b.getDistance(c)); assertEquals(sumOfArcs(b, y, c), b.getDistanceTo(c));
assertEquals(sumOfArcs(b, y, z, d), b.getDistance(d)); assertEquals(sumOfArcs(b, y, z, d), b.getDistanceTo(d));
assertEquals(sumOfArcs(c, a), c.getDistance(a)); assertEquals(sumOfArcs(c, a), c.getDistanceTo(a));
assertEquals(sumOfArcs(c, y, b), c.getDistance(b)); assertEquals(sumOfArcs(c, y, b), c.getDistanceTo(b));
assertEquals(sumOfArcs(c, x, z, d), c.getDistance(d)); assertEquals(sumOfArcs(c, x, z, d), c.getDistanceTo(d));
assertEquals(sumOfArcs(d, z, x, a), d.getDistance(a)); assertEquals(sumOfArcs(d, z, x, a), d.getDistanceTo(a));
assertEquals(sumOfArcs(d, z, y, b), d.getDistance(b)); assertEquals(sumOfArcs(d, z, y, b), d.getDistanceTo(b));
assertEquals(sumOfArcs(d, z, x, c), d.getDistance(c)); assertEquals(sumOfArcs(d, z, x, c), d.getDistanceTo(c));
} }


protected int sumOfArcs(Location fromLocation, Location... stopLocations) { protected int sumOfArcs(Location fromLocation, Location... stopLocations) {
Location previousLocation = fromLocation; Location previousLocation = fromLocation;
int distance = 0; int distance = 0;
for (Location stopLocation : stopLocations) { for (Location stopLocation : stopLocations) {
distance += (int) (previousLocation.getAirDistanceDouble(stopLocation) * 1000.0 + 0.5); distance += (int) (previousLocation.getAirDistanceDoubleTo(stopLocation) * 1000.0 + 0.5);
previousLocation = stopLocation; previousLocation = stopLocation;
} }
return distance; return distance;
Expand All @@ -78,7 +78,7 @@ protected int sumOfArcs(Location fromLocation, Location... stopLocations) {
private Map<HubSegmentLocation, Double> createHubTravelDistanceMap(Location fromLocation, HubSegmentLocation... toLocations) { private Map<HubSegmentLocation, Double> createHubTravelDistanceMap(Location fromLocation, HubSegmentLocation... toLocations) {
Map<HubSegmentLocation, Double> map = new LinkedHashMap<HubSegmentLocation, Double>(toLocations.length); Map<HubSegmentLocation, Double> map = new LinkedHashMap<HubSegmentLocation, Double>(toLocations.length);
for (HubSegmentLocation toLocation : toLocations) { for (HubSegmentLocation toLocation : toLocations) {
map.put(toLocation, fromLocation.getAirDistanceDouble(toLocation)); map.put(toLocation, fromLocation.getAirDistanceDoubleTo(toLocation));
} }
return map; return map;
} }
Expand All @@ -87,7 +87,7 @@ protected Map<RoadSegmentLocation, Double> createNearbyTravelDistanceMap(Locatio
RoadSegmentLocation... toLocations) { RoadSegmentLocation... toLocations) {
Map<RoadSegmentLocation, Double> map = new LinkedHashMap<RoadSegmentLocation, Double>(toLocations.length); Map<RoadSegmentLocation, Double> map = new LinkedHashMap<RoadSegmentLocation, Double>(toLocations.length);
for (RoadSegmentLocation toLocation : toLocations) { for (RoadSegmentLocation toLocation : toLocations) {
map.put(toLocation, fromLocation.getAirDistanceDouble(toLocation)); map.put(toLocation, fromLocation.getAirDistanceDoubleTo(toLocation));
} }
return map; return map;
} }
Expand Down

0 comments on commit 3d7510b

Please sign in to comment.