From 891b0083c6e767a8629d919121e1b0cb76a94239 Mon Sep 17 00:00:00 2001 From: oblonski <4sschroeder@gmail.com> Date: Wed, 16 Jul 2014 15:01:15 +0200 Subject: [PATCH] added vehicleTypeKey to identify similar vehicles --- .../jsprit/core/problem/AbstractVehicle.java | 23 +++- .../core/problem/VehicleRoutingProblem.java | 127 +++++++++--------- .../jsprit/core/problem/vehicle/Vehicle.java | 2 + .../core/problem/vehicle/VehicleImpl.java | 1 + .../core/problem/vehicle/VehicleTypeKey.java | 6 +- .../problem/VehicleRoutingProblemTest.java | 108 ++++++++++----- 6 files changed, 161 insertions(+), 106 deletions(-) diff --git a/jsprit-core/src/main/java/jsprit/core/problem/AbstractVehicle.java b/jsprit-core/src/main/java/jsprit/core/problem/AbstractVehicle.java index b4630928..87c7896f 100644 --- a/jsprit-core/src/main/java/jsprit/core/problem/AbstractVehicle.java +++ b/jsprit-core/src/main/java/jsprit/core/problem/AbstractVehicle.java @@ -1,15 +1,36 @@ package jsprit.core.problem; import jsprit.core.problem.vehicle.Vehicle; +import jsprit.core.problem.vehicle.VehicleTypeKey; /** - * Created by schroeder on 14.07.14. + * AbstractVehicle to handle indeces of vehicles. */ public abstract class AbstractVehicle implements Vehicle { + public abstract static class AbstractTypeKey implements HasIndex { + + private int index; + + public int getIndex(){ return index; } + + public void setIndex(int index) { this.index = index; } + + } + private int index; + private VehicleTypeKey vehicleIdentifier; + public int getIndex(){ return index; } protected void setIndex(int index){ this.index = index; } + + public VehicleTypeKey getVehicleTypeIdentifier(){ + return vehicleIdentifier; + } + + protected void setVehicleIdentifier(VehicleTypeKey vehicleTypeIdentifier){ + this.vehicleIdentifier = vehicleTypeIdentifier; + } } diff --git a/jsprit-core/src/main/java/jsprit/core/problem/VehicleRoutingProblem.java b/jsprit-core/src/main/java/jsprit/core/problem/VehicleRoutingProblem.java index 359be349..7a6754f3 100644 --- a/jsprit-core/src/main/java/jsprit/core/problem/VehicleRoutingProblem.java +++ b/jsprit-core/src/main/java/jsprit/core/problem/VehicleRoutingProblem.java @@ -91,10 +91,6 @@ public class VehicleRoutingProblem { private Set jobsInInitialRoutes = new HashSet(); - private Collection services = new ArrayList(); - - private Map coordinates = new HashMap(); - private Map tentative_coordinates = new HashMap(); private FleetSize fleetSize = FleetSize.INFINITE; @@ -104,7 +100,8 @@ public class VehicleRoutingProblem { private Collection initialRoutes = new ArrayList(); private Set uniqueVehicles = new HashSet(); - + + @Deprecated private Collection constraints = new ArrayList(); private JobActivityFactory jobActivityFactory = new JobActivityFactory() { @@ -136,10 +133,11 @@ public class VehicleRoutingProblem { private int activityIndexCounter = 0; - private int nuActivities = 0; + private int vehicleTypeIdIndexCounter = 0; + + private Map typeKeyIndices = new HashMap(); private Map> activityMap = new HashMap>(); -// private ArrayList> activityList; private final DefaultShipmentActivityFactory shipmentActivityFactory = new DefaultShipmentActivityFactory(); @@ -148,21 +146,23 @@ public class VehicleRoutingProblem { /** * Create a location (i.e. coordinate) and returns the key of the location which is Coordinate.toString(). * - * @param x - * @param y + * @param x x-coordinate of location + * @param y y-coordinate of location * @return locationId * @see Coordinate */ - @Deprecated + @SuppressWarnings("UnusedDeclaration") + @Deprecated public String createLocation(double x, double y){ - Coordinate coord = new Coordinate(x, y); - String id = coord.toString(); + Coordinate coordinate = new Coordinate(x, y); + String id = coordinate.toString(); if(!tentative_coordinates.containsKey(id)){ - tentative_coordinates.put(id, coord); + tentative_coordinates.put(id, coordinate); } return id; } + @SuppressWarnings("UnusedDeclaration") public Builder setJobActivityFactory(JobActivityFactory factory){ this.jobActivityFactory = factory; return this; @@ -176,6 +176,8 @@ public class VehicleRoutingProblem { activityIndexCounter++; } + private void incVehicleTypeIdIndexCounter() { vehicleTypeIdIndexCounter++; } + /** * Returns the unmodifiable map of collected locations (mapped by their location-id). * @@ -207,7 +209,7 @@ public class VehicleRoutingProblem { /** * Sets routing costs. * - * @param costs + * @param costs the routingCosts * @return builder * @see VehicleRoutingTransportCosts */ @@ -222,7 +224,7 @@ public class VehicleRoutingProblem { * *

FleetSize is either FleetSize.INFINITE or FleetSize.FINITE. By default it is FleetSize.INFINITE. * - * @param fleetSize + * @param fleetSize the fleet size used in this problem. it can either be FleetSize.INFINITE or FleetSize.FINITE * @return this builder */ public Builder setFleetSize(FleetSize fleetSize){ @@ -235,7 +237,7 @@ public class VehicleRoutingProblem { * *

Note that job.getId() must be unique, i.e. no job (either it is a shipment or a service) is allowed to have an already allocated id. * - * @param job + * @param job job to be added * @return this builder * @throws IllegalStateException if job is neither a shipment nor a service, or jobId has already been added. * @deprecated use addJob(AbstractJob job) instead @@ -251,7 +253,7 @@ public class VehicleRoutingProblem { * *

Note that job.getId() must be unique, i.e. no job (either it is a shipment or a service) is allowed to have an already allocated id. * - * @param job + * @param job job to be added * @return this builder * @throws IllegalStateException if job is neither a shipment nor a service, or jobId has already been added. */ @@ -277,7 +279,6 @@ public class VehicleRoutingProblem { } private void addJobToFinalJobMapAndCreateActivities(Job job){ - List acts; if(job instanceof Service) { Service service = (Service) job; addService(service); @@ -294,19 +295,18 @@ public class VehicleRoutingProblem { activityMap.put(job, jobActs); } - public Builder addInitialVehicleRoute(VehicleRoute route){ + + @SuppressWarnings("deprecation") + public Builder addInitialVehicleRoute(VehicleRoute route){ addVehicle(route.getVehicle()); for(Job job : route.getTourActivities().getJobs()){ jobsInInitialRoutes.add(job.getId()); if(job instanceof Service) { - coordinates.put(((Service)job).getLocationId(), ((Service)job).getCoord()); tentative_coordinates.put(((Service)job).getLocationId(), ((Service)job).getCoord()); } if(job instanceof Shipment){ Shipment shipment = (Shipment)job; - coordinates.put(shipment.getPickupLocation(), shipment.getPickupCoord()); tentative_coordinates.put(shipment.getPickupLocation(), shipment.getPickupCoord()); - coordinates.put(shipment.getDeliveryLocation(), shipment.getDeliveryCoord()); tentative_coordinates.put(shipment.getDeliveryLocation(), shipment.getDeliveryCoord()); } } @@ -323,8 +323,8 @@ public class VehicleRoutingProblem { private void addShipment(Shipment job) { if(jobs.containsKey(job.getId())){ logger.warn("job " + job + " already in job list. overrides existing job."); } - coordinates.put(job.getPickupLocation(), job.getPickupCoord()); - coordinates.put(job.getDeliveryLocation(), job.getDeliveryCoord()); + tentative_coordinates.put(job.getPickupLocation(), job.getPickupCoord()); + tentative_coordinates.put(job.getDeliveryLocation(), job.getDeliveryCoord()); jobs.put(job.getId(),job); } @@ -332,21 +332,21 @@ public class VehicleRoutingProblem { * Adds a vehicle. * * - * @param vehicle + * @param vehicle vehicle to be added * @return this builder * @deprecated use addVehicle(AbstractVehicle vehicle) instead */ @Deprecated public Builder addVehicle(Vehicle vehicle) { if(!(vehicle instanceof AbstractVehicle)) throw new IllegalStateException("vehicle must be an AbstractVehicle"); - return addVehicle((AbstractVehicle)vehicle); + return addVehicle((AbstractVehicle)vehicle); } /** * Adds a vehicle. * * - * @param vehicle + * @param vehicle vehicle to be added * @return this builder */ public Builder addVehicle(AbstractVehicle vehicle) { @@ -354,15 +354,21 @@ public class VehicleRoutingProblem { vehicle.setIndex(vehicleIndexCounter); incVehicleIndexCounter(); } + if(typeKeyIndices.containsKey(vehicle.getVehicleTypeIdentifier())){ + vehicle.getVehicleTypeIdentifier().setIndex(typeKeyIndices.get(vehicle.getVehicleTypeIdentifier())); + } + else { + vehicle.getVehicleTypeIdentifier().setIndex(vehicleTypeIdIndexCounter); + typeKeyIndices.put(vehicle.getVehicleTypeIdentifier(),vehicleTypeIdIndexCounter); + incVehicleTypeIdIndexCounter(); + } uniqueVehicles.add(vehicle); if(!vehicleTypes.contains(vehicle.getType())){ vehicleTypes.add(vehicle.getType()); } String startLocationId = vehicle.getStartLocationId(); - coordinates.put(startLocationId, vehicle.getStartLocationCoordinate()); tentative_coordinates.put(startLocationId, vehicle.getStartLocationCoordinate()); if(!vehicle.getEndLocationId().equals(startLocationId)){ - coordinates.put(vehicle.getEndLocationId(), vehicle.getEndLocationCoordinate()); tentative_coordinates.put(vehicle.getEndLocationId(), vehicle.getEndLocationCoordinate()); } return this; @@ -377,7 +383,7 @@ public class VehicleRoutingProblem { * *

By default it is set to zero. * - * @param activityCosts + * @param activityCosts activity costs of the problem * @return this builder * @see VehicleRoutingActivityCosts */ @@ -438,8 +444,7 @@ public class VehicleRoutingProblem { .build(); PenaltyVehicleType penType = new PenaltyVehicleType(t,penaltyFactor); String vehicleId = v.getId(); -// String vehicleId = "penaltyVehicle_" + new VehicleTypeKey(v.getType().getTypeId(),v.getStartLocationId(),v.getEndLocationId(),v.getEarliestDeparture(),v.getLatestArrival()).toString(); - Vehicle penVehicle = VehicleImpl.Builder.newInstance(vehicleId).setEarliestStart(v.getEarliestDeparture()) + VehicleImpl penVehicle = VehicleImpl.Builder.newInstance(vehicleId).setEarliestStart(v.getEarliestDeparture()) .setLatestArrival(v.getLatestArrival()).setStartLocationCoordinate(v.getStartLocationCoordinate()).setStartLocationId(v.getStartLocationId()) .setEndLocationId(v.getEndLocationId()).setEndLocationCoordinate(v.getEndLocationCoordinate()) .setReturnToDepot(v.isReturnToDepot()).setType(penType).build(); @@ -447,8 +452,10 @@ public class VehicleRoutingProblem { } } - public Builder addLocation(String locationId, Coordinate coord) { - coordinates.put(locationId, coord); + + @SuppressWarnings("UnusedDeclaration") + public Builder addLocation(String locationId, Coordinate coordinate) { + tentative_coordinates.put(locationId, coordinate); return this; } @@ -458,7 +465,8 @@ public class VehicleRoutingProblem { * @param jobs which is a collection of jobs that subclasses Job * @return this builder */ - public Builder addAllJobs(Collection jobs) { + @SuppressWarnings("deprecation") + public Builder addAllJobs(Collection jobs) { for(Job j : jobs){ addJob(j); } @@ -468,10 +476,11 @@ public class VehicleRoutingProblem { /** * Adds a collection of vehicles. * - * @param vehicles + * @param vehicles vehicles to be added * @return this builder */ - public Builder addAllVehicles(Collection vehicles) { + @SuppressWarnings("deprecation") + public Builder addAllVehicles(Collection vehicles) { for(Vehicle v : vehicles){ addVehicle(v); } @@ -490,7 +499,7 @@ public class VehicleRoutingProblem { /** * Gets an unmodifiable collection of already added vehicle-types. * - * @returns collection of vehicle-types + * @return collection of vehicle-types */ public Collection getAddedVehicleTypes(){ return Collections.unmodifiableCollection(vehicleTypes); @@ -499,11 +508,14 @@ public class VehicleRoutingProblem { /** * Adds constraint to problem. * - * @param constraint + * @param constraint constraint to be added * @return this builder + * @deprecated use ConstraintManager instead */ + @Deprecated public Builder addConstraint(jsprit.core.problem.constraint.Constraint constraint){ - constraints.add(constraint); + //noinspection deprecation + constraints.add(constraint); return this; } @@ -515,7 +527,7 @@ public class VehicleRoutingProblem { *

The id of penaltyVehicles is constructed as follows vehicleId = "penaltyVehicle" + "_" + {locationId} + "_" + {typeId}. *

By default: no penalty-vehicles are added * - * @param penaltyFactor + * @param penaltyFactor penaltyFactor of penaltyVehicle * @return this builder */ public Builder addPenaltyVehicles(double penaltyFactor){ @@ -531,7 +543,7 @@ public class VehicleRoutingProblem { *

The id of penaltyVehicles is constructed as follows vehicleId = "penaltyVehicle" + "_" + {locationId} + "_" + {typeId}. *

By default: no penalty-vehicles are added * - * @param penaltyFactor + * @param penaltyFactor the penaltyFactor of penaltyVehicle * @param penaltyFixedCosts which is an absolute penaltyValue (in contrary to penaltyFactor) * @return this builder */ @@ -551,19 +563,10 @@ public class VehicleRoutingProblem { return Collections.unmodifiableCollection(tentativeJobs.values()); } - /** - * Adds a service to jobList. - * - *

If jobList already contains service, a warning message is printed, and the existing job will be overwritten. - * - * @param service - * @return - */ private Builder addService(Service service){ - coordinates.put(service.getLocationId(), service.getCoord()); + tentative_coordinates.put(service.getLocationId(), service.getCoord()); if(jobs.containsKey(service.getId())){ logger.warn("service " + service + " already in job list. overrides existing job."); } jobs.put(service.getId(),service); - services.add(service); return this; } @@ -577,18 +580,7 @@ public class VehicleRoutingProblem { * */ public static enum FleetSize { - FINITE, INFINITE; - } - - /** - * Enum that characterizes fleet-compostion. - * - * @author sschroeder - * @deprecated FleetComposition is not used - */ - @Deprecated - public static enum FleetComposition { - HETEROGENEOUS, HOMOGENEOUS; + FINITE, INFINITE } /** @@ -648,7 +640,8 @@ public class VehicleRoutingProblem { this.initialVehicleRoutes = builder.initialRoutes; this.transportCosts = builder.transportCosts; this.activityCosts = builder.activityCosts; - this.constraints = builder.constraints; + //noinspection deprecation + this.constraints = builder.constraints; this.locations = builder.getLocations(); this.activityMap = builder.activityMap; this.nuActivities = builder.activityIndexCounter; @@ -726,8 +719,10 @@ public class VehicleRoutingProblem { /** * Returns an unmodifiable collection of constraints. * - * @return + * @return collection of constraints + * @deprecated use ConstraintManager instead */ + @Deprecated public Collection getConstraints(){ return Collections.unmodifiableCollection(constraints); } diff --git a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/Vehicle.java b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/Vehicle.java index 0337e504..4b5f9e34 100644 --- a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/Vehicle.java +++ b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/Vehicle.java @@ -84,4 +84,6 @@ public interface Vehicle extends HasId, HasIndex { * Returns the end-locationCoord of this vehicle. */ public abstract Coordinate getEndLocationCoordinate(); + + public abstract VehicleTypeKey getVehicleTypeIdentifier(); } diff --git a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java index 882bf8d7..7e26460e 100644 --- a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java +++ b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleImpl.java @@ -275,6 +275,7 @@ public class VehicleImpl extends AbstractVehicle{ startLocationCoord = builder.startLocationCoord; endLocationId = builder.endLocationId; endLocationCoord = builder.endLocationCoord; + setVehicleIdentifier(new VehicleTypeKey(type.getTypeId(),startLocationId,endLocationId,earliestDeparture,latestArrival)); } /** diff --git a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleTypeKey.java b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleTypeKey.java index 019abfb8..57eaa9b5 100644 --- a/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleTypeKey.java +++ b/jsprit-core/src/main/java/jsprit/core/problem/vehicle/VehicleTypeKey.java @@ -18,15 +18,17 @@ ******************************************************************************/ package jsprit.core.problem.vehicle; +import jsprit.core.problem.AbstractVehicle; + /** - * Key to identify different vehicles + * Key to identify similar vehicles * *

Two vehicles are equal if they share the same type, the same start and end-location and the same earliestStart and latestStart. * * @author stefan * */ -public class VehicleTypeKey { +public class VehicleTypeKey extends AbstractVehicle.AbstractTypeKey{ public final String type; public final String startLocationId; diff --git a/jsprit-core/src/test/java/jsprit/core/problem/VehicleRoutingProblemTest.java b/jsprit-core/src/test/java/jsprit/core/problem/VehicleRoutingProblemTest.java index cd7b605a..570ae68a 100644 --- a/jsprit-core/src/test/java/jsprit/core/problem/VehicleRoutingProblemTest.java +++ b/jsprit-core/src/test/java/jsprit/core/problem/VehicleRoutingProblemTest.java @@ -22,7 +22,10 @@ import jsprit.core.problem.cost.AbstractForwardVehicleRoutingTransportCosts; import jsprit.core.problem.cost.VehicleRoutingActivityCosts; import jsprit.core.problem.driver.Driver; import jsprit.core.problem.driver.DriverImpl; -import jsprit.core.problem.job.*; +import jsprit.core.problem.job.Delivery; +import jsprit.core.problem.job.Pickup; +import jsprit.core.problem.job.Service; +import jsprit.core.problem.job.Shipment; import jsprit.core.problem.solution.route.VehicleRoute; import jsprit.core.problem.solution.route.activity.TourActivity; import jsprit.core.problem.vehicle.*; @@ -60,10 +63,10 @@ public class VehicleRoutingProblemTest { public void whenBuildingWithFourVehicles_vrpShouldContainTheCorrectNuOfVehicles(){ VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance(); - Vehicle v1 = VehicleImpl.Builder.newInstance("v1").setStartLocationId("start").build(); - Vehicle v2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("start").build(); - Vehicle v3 = VehicleImpl.Builder.newInstance("v3").setStartLocationId("start").build(); - Vehicle v4 = VehicleImpl.Builder.newInstance("v4").setStartLocationId("start").build(); + VehicleImpl v1 = VehicleImpl.Builder.newInstance("v1").setStartLocationId("start").build(); + VehicleImpl v2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("start").build(); + VehicleImpl v3 = VehicleImpl.Builder.newInstance("v3").setStartLocationId("start").build(); + VehicleImpl v4 = VehicleImpl.Builder.newInstance("v4").setStartLocationId("start").build(); builder.addVehicle(v1).addVehicle(v2).addVehicle(v3).addVehicle(v4); @@ -76,10 +79,10 @@ public class VehicleRoutingProblemTest { public void whenAddingFourVehiclesAllAtOnce_vrpShouldContainTheCorrectNuOfVehicles(){ VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance(); - Vehicle v1 = VehicleImpl.Builder.newInstance("v1").setStartLocationId("start").build(); - Vehicle v2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("start").build(); - Vehicle v3 = VehicleImpl.Builder.newInstance("v3").setStartLocationId("start").build(); - Vehicle v4 = VehicleImpl.Builder.newInstance("v4").setStartLocationId("start").build(); + VehicleImpl v1 = VehicleImpl.Builder.newInstance("v1").setStartLocationId("start").build(); + VehicleImpl v2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("start").build(); + VehicleImpl v3 = VehicleImpl.Builder.newInstance("v3").setStartLocationId("start").build(); + VehicleImpl v4 = VehicleImpl.Builder.newInstance("v4").setStartLocationId("start").build(); builder.addAllVehicles(Arrays.asList(v1,v2,v3,v4)); @@ -231,7 +234,8 @@ public class VehicleRoutingProblemTest { assertEquals(s2,vrp.getJobs().get("s2")); } - @Test + @SuppressWarnings("deprecation") + @Test public void whenConstraintsAdded_vrpShouldContainThem(){ Constraint c1 = mock(Constraint.class); Constraint c2 = mock(Constraint.class); @@ -284,7 +288,7 @@ public class VehicleRoutingProblemTest { public void whenAddingAVehicle_getAddedVehicleTypesShouldReturnItsType(){ VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance(); VehicleType type = VehicleTypeImpl.Builder.newInstance("type").build(); - Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type).build(); + VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type).build(); builder.addVehicle(vehicle); assertEquals(1,builder.getAddedVehicleTypes().size()); @@ -296,8 +300,8 @@ public class VehicleRoutingProblemTest { public void whenAddingTwoVehicleWithSameType_getAddedVehicleTypesShouldReturnOnlyOneType(){ VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance(); VehicleType type = VehicleTypeImpl.Builder.newInstance("type").build(); - Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type).build(); - Vehicle vehicle2 = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type).build(); + VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type).build(); + VehicleImpl vehicle2 = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type).build(); builder.addVehicle(vehicle); builder.addVehicle(vehicle2); @@ -312,8 +316,8 @@ public class VehicleRoutingProblemTest { VehicleType type = VehicleTypeImpl.Builder.newInstance("type").build(); VehicleType type2 = VehicleTypeImpl.Builder.newInstance("type2").build(); - Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type).build(); - Vehicle vehicle2 = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type2).build(); + VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type).build(); + VehicleImpl vehicle2 = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type2).build(); builder.addVehicle(vehicle); builder.addVehicle(vehicle2); @@ -326,7 +330,7 @@ public class VehicleRoutingProblemTest { public void whenSettingAddPenaltyVehicleOptions_itShouldAddPenaltyVehicle(){ VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance(); VehicleType type = VehicleTypeImpl.Builder.newInstance("type").build(); - Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type).build(); + VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type).build(); builder.addVehicle(vehicle); builder.setFleetSize(FleetSize.FINITE); @@ -348,7 +352,7 @@ public class VehicleRoutingProblemTest { public void whenSettingAddPenaltyVehicleOptionsAndFleetSizeIsInfinite_noPenaltyVehicleIsAdded(){ VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance(); VehicleType type = VehicleTypeImpl.Builder.newInstance("type").build(); - Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type).build(); + VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type).build(); builder.addVehicle(vehicle); builder.addPenaltyVehicles(3.0); @@ -371,8 +375,8 @@ public class VehicleRoutingProblemTest { public void whenSettingAddPenaltyVehicleOptionsAndTwoVehiclesWithSameLocationAndType_onlyOnePenaltyVehicleIsAdded(){ VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance(); VehicleType type = VehicleTypeImpl.Builder.newInstance("type").build(); - Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type).build(); - Vehicle vehicle2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("loc").setType(type).build(); + VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type).build(); + VehicleImpl vehicle2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("loc").setType(type).build(); builder.addVehicle(vehicle); builder.addVehicle(vehicle2); @@ -395,8 +399,8 @@ public class VehicleRoutingProblemTest { public void whenSettingAddPenaltyVehicleOptionsWithAbsoluteFixedCostsAndTwoVehiclesWithSameLocationAndType_onePenaltyVehicleIsAddedWithTheCorrectPenaltyFixedCosts(){ VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance(); VehicleType type = VehicleTypeImpl.Builder.newInstance("type").build(); - Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type).build(); - Vehicle vehicle2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("loc").setType(type).build(); + VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type).build(); + VehicleImpl vehicle2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("loc").setType(type).build(); builder.addVehicle(vehicle); builder.addVehicle(vehicle2); @@ -421,8 +425,8 @@ public class VehicleRoutingProblemTest { public void whenSettingAddPenaltyVehicleOptionsAndTwoVehiclesWithDiffLocationAndType_twoPenaltyVehicleIsAdded(){ VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance(); VehicleType type = VehicleTypeImpl.Builder.newInstance("type").build(); - Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type).build(); - Vehicle vehicle2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("loc2").setType(type).build(); + VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type).build(); + VehicleImpl vehicle2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("loc2").setType(type).build(); builder.addVehicle(vehicle); builder.addVehicle(vehicle2); @@ -449,8 +453,8 @@ public class VehicleRoutingProblemTest { VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance(); VehicleType type = VehicleTypeImpl.Builder.newInstance("type").build(); VehicleType type2 = VehicleTypeImpl.Builder.newInstance("type2").build(); - Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type).build(); - Vehicle vehicle2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("loc").setType(type2).build(); + VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type).build(); + VehicleImpl vehicle2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("loc").setType(type2).build(); builder.addVehicle(vehicle); builder.addVehicle(vehicle2); @@ -472,7 +476,7 @@ public class VehicleRoutingProblemTest { @Test public void whenAddingVehicleWithDiffStartAndEnd_startLocationMustBeRegisteredInLocationMap(){ - Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setEndLocationId("end").build(); + VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setEndLocationId("end").build(); VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); vrpBuilder.addVehicle(vehicle); @@ -481,7 +485,7 @@ public class VehicleRoutingProblemTest { @Test public void whenAddingVehicleWithDiffStartAndEnd_endLocationMustBeRegisteredInLocationMap(){ - Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setEndLocationId("end").build(); + VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setEndLocationId("end").build(); VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); vrpBuilder.addVehicle(vehicle); @@ -490,7 +494,7 @@ public class VehicleRoutingProblemTest { @Test public void whenAddingInitialRoute_itShouldBeAddedCorrectly(){ - Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setEndLocationId("end").build(); + VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setEndLocationId("end").build(); VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build(); VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); vrpBuilder.addInitialVehicleRoute(route); @@ -500,10 +504,10 @@ public class VehicleRoutingProblemTest { @Test public void whenAddingInitialRoutes_theyShouldBeAddedCorrectly(){ - Vehicle vehicle1 = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setEndLocationId("end").build(); + VehicleImpl vehicle1 = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setEndLocationId("end").build(); VehicleRoute route1 = VehicleRoute.Builder.newInstance(vehicle1, DriverImpl.noDriver()).build(); - Vehicle vehicle2 = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setEndLocationId("end").build(); + VehicleImpl vehicle2 = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setEndLocationId("end").build(); VehicleRoute route2 = VehicleRoute.Builder.newInstance(vehicle2, DriverImpl.noDriver()).build(); VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); @@ -515,7 +519,7 @@ public class VehicleRoutingProblemTest { @Test public void whenAddingInitialRoute_locationOfVehicleMustBeMemorized(){ - Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setStartLocationCoordinate(Coordinate.newInstance(0, 1)).setEndLocationId("end").build(); + VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setStartLocationCoordinate(Coordinate.newInstance(0, 1)).setEndLocationId("end").build(); VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, DriverImpl.noDriver()).build(); VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); vrpBuilder.addInitialVehicleRoute(route); @@ -529,7 +533,7 @@ public class VehicleRoutingProblemTest { Service service = Service.Builder.newInstance("myService").setLocationId("loc").build(); VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); vrpBuilder.addJob(service); - Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setStartLocationCoordinate(Coordinate.newInstance(0, 1)).setEndLocationId("end").build(); + VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("start").setStartLocationCoordinate(Coordinate.newInstance(0, 1)).setEndLocationId("end").build(); VehicleRoute initialRoute = VehicleRoute.Builder.newInstance(vehicle).addService(service).build(); vrpBuilder.addInitialVehicleRoute(initialRoute); VehicleRoutingProblem vrp = vrpBuilder.build(); @@ -538,8 +542,8 @@ public class VehicleRoutingProblemTest { @Test public void whenAddingTwoJobs_theyShouldHaveProperIndeces(){ - Job service = Service.Builder.newInstance("myService").setLocationId("loc").build(); - Job shipment = Shipment.Builder.newInstance("shipment").setPickupLocation("pick").setDeliveryLocation("del").build(); + Service service = Service.Builder.newInstance("myService").setLocationId("loc").build(); + Shipment shipment = Shipment.Builder.newInstance("shipment").setPickupLocation("pick").setDeliveryLocation("del").build(); VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); vrpBuilder.addJob(service); vrpBuilder.addJob(shipment); @@ -551,9 +555,9 @@ public class VehicleRoutingProblemTest { } @Test - public void whenAddingTwoVehicles_theyShouldHaveProperIndeces(){ - Vehicle veh1 = VehicleImpl.Builder.newInstance("v1").setStartLocationId("start").setStartLocationCoordinate(Coordinate.newInstance(0, 1)).setEndLocationId("end").build(); - Vehicle veh2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("start").setStartLocationCoordinate(Coordinate.newInstance(0, 1)).setEndLocationId("end").build(); + public void whenAddingTwoVehicles_theyShouldHaveProperIndices(){ + VehicleImpl veh1 = VehicleImpl.Builder.newInstance("v1").setStartLocationId("start").setStartLocationCoordinate(Coordinate.newInstance(0, 1)).setEndLocationId("end").build(); + VehicleImpl veh2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("start").setStartLocationCoordinate(Coordinate.newInstance(0, 1)).setEndLocationId("end").build(); VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); vrpBuilder.addVehicle(veh1); @@ -564,4 +568,34 @@ public class VehicleRoutingProblemTest { assertEquals(1,veh2.getIndex()); } + + @Test + public void whenAddingTwoVehiclesWithSameTypeIdentifier_typeIdentifiersShouldHaveSameIndices(){ + VehicleImpl veh1 = VehicleImpl.Builder.newInstance("v1").setStartLocationId("start").setStartLocationCoordinate(Coordinate.newInstance(0, 1)).setEndLocationId("end").build(); + VehicleImpl veh2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("start").setStartLocationCoordinate(Coordinate.newInstance(0, 1)).setEndLocationId("end").build(); + + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + vrpBuilder.addVehicle(veh1); + vrpBuilder.addVehicle(veh2); + vrpBuilder.build(); + + assertEquals(0, veh1.getVehicleTypeIdentifier().getIndex()); + assertEquals(0, veh2.getVehicleTypeIdentifier().getIndex()); + + } + + @Test + public void whenAddingTwoVehiclesDifferentTypeIdentifier_typeIdentifiersShouldHaveDifferentIndices(){ + VehicleImpl veh1 = VehicleImpl.Builder.newInstance("v1").setStartLocationId("start").setStartLocationCoordinate(Coordinate.newInstance(0, 1)).setEndLocationId("end").build(); + VehicleImpl veh2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("startLoc").setStartLocationCoordinate(Coordinate.newInstance(0, 1)).setEndLocationId("end").build(); + + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + vrpBuilder.addVehicle(veh1); + vrpBuilder.addVehicle(veh2); + vrpBuilder.build(); + + assertEquals(0,veh1.getVehicleTypeIdentifier().getIndex()); + assertEquals(1,veh2.getVehicleTypeIdentifier().getIndex()); + + } }