@Before public void doBefore() { Vehicle vehicle = mock(Vehicle.class); VehicleType type = mock(VehicleType.class); when(type.getCapacityDimensions()) .thenReturn(Capacity.Builder.newInstance().addDimension(0, 20).build()); when(vehicle.getType()).thenReturn(type); VehicleRoutingProblem.Builder serviceProblemBuilder = VehicleRoutingProblem.Builder.newInstance(); Service s1 = Service.Builder.newInstance("s").addSizeDimension(0, 10).setLocationId("loc").build(); Service s2 = Service.Builder.newInstance("s2").addSizeDimension(0, 5).setLocationId("loc").build(); serviceProblemBuilder.addJob(s1).addJob(s2); final VehicleRoutingProblem serviceProblem = serviceProblemBuilder.build(); final VehicleRoutingProblem.Builder pdProblemBuilder = VehicleRoutingProblem.Builder.newInstance(); Pickup pickup = (Pickup) Pickup.Builder.newInstance("pick").addSizeDimension(0, 10).setLocationId("loc").build(); Delivery delivery = (Delivery) Delivery.Builder.newInstance("del").addSizeDimension(0, 5).setLocationId("loc").build(); pdProblemBuilder.addJob(pickup).addJob(delivery); final VehicleRoutingProblem pdProblem = pdProblemBuilder.build(); final VehicleRoutingProblem.Builder shipmentProblemBuilder = VehicleRoutingProblem.Builder.newInstance(); Shipment shipment1 = Shipment.Builder.newInstance("s1") .addSizeDimension(0, 10) .setPickupLocation("pick") .setDeliveryLocation("del") .build(); Shipment shipment2 = Shipment.Builder.newInstance("s2") .addSizeDimension(0, 5) .setPickupLocation("pick") .setDeliveryLocation("del") .build(); shipmentProblemBuilder.addJob(shipment1).addJob(shipment2).build(); final VehicleRoutingProblem shipmentProblem = shipmentProblemBuilder.build(); VehicleRoute.Builder serviceRouteBuilder = VehicleRoute.Builder.newInstance(vehicle); serviceRouteBuilder.setJobActivityFactory( new JobActivityFactory() { @Override public List<AbstractActivity> createActivities(Job job) { return serviceProblem.copyAndGetActivities(job); } }); serviceRoute = serviceRouteBuilder.addService(s1).addService(s2).build(); VehicleRoute.Builder pdRouteBuilder = VehicleRoute.Builder.newInstance(vehicle); pdRouteBuilder.setJobActivityFactory( new JobActivityFactory() { @Override public List<AbstractActivity> createActivities(Job job) { return pdProblem.copyAndGetActivities(job); } }); pickup_delivery_route = pdRouteBuilder.addService(pickup).addService(delivery).build(); VehicleRoute.Builder shipmentRouteBuilder = VehicleRoute.Builder.newInstance(vehicle); shipmentRouteBuilder.setJobActivityFactory( new JobActivityFactory() { @Override public List<AbstractActivity> createActivities(Job job) { return shipmentProblem.copyAndGetActivities(job); } }); shipment_route = shipmentRouteBuilder .addPickup(shipment1) .addPickup(shipment2) .addDelivery(shipment2) .addDelivery(shipment1) .build(); stateManager = new StateManager(mock(VehicleRoutingProblem.class)); stateManager.updateLoadStates(); }
private void writeVehiclesAndTheirTypes(XMLConfiguration xmlConfig) { // vehicles String vehiclePathString = Schema.VEHICLES + "." + Schema.VEHICLE; int counter = 0; for (Vehicle vehicle : vrp.getVehicles()) { xmlConfig.setProperty(vehiclePathString + "(" + counter + ").id", vehicle.getId()); xmlConfig.setProperty( vehiclePathString + "(" + counter + ").typeId", vehicle.getType().getTypeId()); xmlConfig.setProperty( vehiclePathString + "(" + counter + ").startLocation.id", vehicle.getStartLocation().getId()); if (vehicle.getStartLocation().getCoordinate() != null) { xmlConfig.setProperty( vehiclePathString + "(" + counter + ").startLocation.coord[@x]", vehicle.getStartLocation().getCoordinate().getX()); xmlConfig.setProperty( vehiclePathString + "(" + counter + ").startLocation.coord[@y]", vehicle.getStartLocation().getCoordinate().getY()); } if (vehicle.getStartLocation().getIndex() != Location.NO_INDEX) { xmlConfig.setProperty( vehiclePathString + "(" + counter + ").startLocation.index", vehicle.getStartLocation().getIndex()); } xmlConfig.setProperty( vehiclePathString + "(" + counter + ").endLocation.id", vehicle.getEndLocation().getId()); if (vehicle.getEndLocation().getCoordinate() != null) { xmlConfig.setProperty( vehiclePathString + "(" + counter + ").endLocation.coord[@x]", vehicle.getEndLocation().getCoordinate().getX()); xmlConfig.setProperty( vehiclePathString + "(" + counter + ").endLocation.coord[@y]", vehicle.getEndLocation().getCoordinate().getY()); } if (vehicle.getEndLocation().getIndex() != Location.NO_INDEX) { xmlConfig.setProperty( vehiclePathString + "(" + counter + ").endLocation.index", vehicle.getEndLocation().getId()); } xmlConfig.setProperty( vehiclePathString + "(" + counter + ").timeSchedule.start", vehicle.getEarliestDeparture()); xmlConfig.setProperty( vehiclePathString + "(" + counter + ").timeSchedule.end", vehicle.getLatestArrival()); xmlConfig.setProperty( vehiclePathString + "(" + counter + ").returnToDepot", vehicle.isReturnToDepot()); // write skills String skillString = getSkillString(vehicle); xmlConfig.setProperty(vehiclePathString + "(" + counter + ").skills", skillString); counter++; } // types String typePathString = Schema.builder().append(Schema.TYPES).dot(Schema.TYPE).build(); int typeCounter = 0; for (VehicleType type : vrp.getTypes()) { xmlConfig.setProperty(typePathString + "(" + typeCounter + ").id", type.getTypeId()); for (int i = 0; i < type.getCapacityDimensions().getNuOfDimensions(); i++) { xmlConfig.setProperty( typePathString + "(" + typeCounter + ").capacity-dimensions.dimension(" + i + ")[@index]", i); xmlConfig.setProperty( typePathString + "(" + typeCounter + ").capacity-dimensions.dimension(" + i + ")", type.getCapacityDimensions().get(i)); } xmlConfig.setProperty( typePathString + "(" + typeCounter + ").costs.fixed", type.getVehicleCostParams().fix); xmlConfig.setProperty( typePathString + "(" + typeCounter + ").costs.distance", type.getVehicleCostParams().perDistanceUnit); xmlConfig.setProperty( typePathString + "(" + typeCounter + ").costs.time", type.getVehicleCostParams().perTimeUnit); typeCounter++; } }
private void readVehiclesAndTheirTypes(XMLConfiguration vrpProblem) { // read vehicle-types Map<String, VehicleType> types = new HashMap<String, VehicleType>(); List<HierarchicalConfiguration> typeConfigs = vrpProblem.configurationsAt("vehicleTypes.type"); for (HierarchicalConfiguration typeConfig : typeConfigs) { String typeId = typeConfig.getString("id"); if (typeId == null) throw new IllegalStateException("typeId is missing."); String capacityString = typeConfig.getString("capacity"); boolean capacityDimensionsExist = typeConfig.containsKey("capacity-dimensions.dimension(0)"); if (capacityString == null && !capacityDimensionsExist) { throw new IllegalStateException("capacity of type is not set. use 'capacity-dimensions'"); } if (capacityString != null && capacityDimensionsExist) { throw new IllegalStateException( "either use capacity or capacity-dimension, not both. prefer the use of 'capacity-dimensions' over 'capacity'."); } VehicleTypeImpl.Builder typeBuilder; if (capacityString != null) { typeBuilder = VehicleTypeImpl.Builder.newInstance(typeId) .addCapacityDimension(0, Integer.parseInt(capacityString)); } else { typeBuilder = VehicleTypeImpl.Builder.newInstance(typeId); List<HierarchicalConfiguration> dimensionConfigs = typeConfig.configurationsAt("capacity-dimensions.dimension"); for (HierarchicalConfiguration dimension : dimensionConfigs) { Integer index = dimension.getInt("[@index]"); Integer value = dimension.getInt(""); typeBuilder.addCapacityDimension(index, value); } } Double fix = typeConfig.getDouble("costs.fixed"); Double timeC = typeConfig.getDouble("costs.time"); Double distC = typeConfig.getDouble("costs.distance"); if (fix != null) typeBuilder.setFixedCost(fix); if (timeC != null) typeBuilder.setCostPerTime(timeC); if (distC != null) typeBuilder.setCostPerDistance(distC); VehicleType type = typeBuilder.build(); String id = type.getTypeId(); String penalty = typeConfig.getString("[@type]"); if (penalty != null) { if (penalty.equals("penalty")) { String penaltyFactor = typeConfig.getString("[@penaltyFactor]"); if (penaltyFactor != null) { type = new PenaltyVehicleType(type, Double.parseDouble(penaltyFactor)); } else type = new PenaltyVehicleType(type); id = id + "_penalty"; } } types.put(id, type); } // read vehicles List<HierarchicalConfiguration> vehicleConfigs = vrpProblem.configurationsAt("vehicles.vehicle"); boolean doNotWarnAgain = false; for (HierarchicalConfiguration vehicleConfig : vehicleConfigs) { String vehicleId = vehicleConfig.getString("id"); if (vehicleId == null) throw new IllegalStateException("vehicleId is missing."); Builder builder = VehicleImpl.Builder.newInstance(vehicleId); String typeId = vehicleConfig.getString("typeId"); if (typeId == null) throw new IllegalStateException("typeId is missing."); String vType = vehicleConfig.getString("[@type]"); if (vType != null) { if (vType.equals("penalty")) { typeId += "_penalty"; } } VehicleType type = types.get(typeId); if (type == null) throw new IllegalStateException("vehicleType with typeId " + typeId + " is missing."); builder.setType(type); String locationId = vehicleConfig.getString("location.id"); if (locationId == null) { locationId = vehicleConfig.getString("startLocation.id"); } if (locationId == null) throw new IllegalStateException("location.id is missing."); builder.setStartLocationId(locationId); String coordX = vehicleConfig.getString("location.coord[@x]"); String coordY = vehicleConfig.getString("location.coord[@y]"); if (coordX == null || coordY == null) { coordX = vehicleConfig.getString("startLocation.coord[@x]"); coordY = vehicleConfig.getString("startLocation.coord[@y]"); } if (coordX == null || coordY == null) { if (!doNotWarnAgain) { logger.warn("location.coord is missing. will not warn you again."); doNotWarnAgain = true; } } else { Coordinate coordinate = Coordinate.newInstance(Double.parseDouble(coordX), Double.parseDouble(coordY)); builder.setStartLocationCoordinate(coordinate); } String endLocationId = vehicleConfig.getString("endLocation.id"); if (endLocationId != null) builder.setEndLocationId(endLocationId); String endCoordX = vehicleConfig.getString("endLocation.coord[@x]"); String endCoordY = vehicleConfig.getString("endLocation.coord[@y]"); if (endCoordX == null || endCoordY == null) { if (!doNotWarnAgain) { logger.warn("endLocation.coord is missing. will not warn you again."); doNotWarnAgain = true; } } else { Coordinate coordinate = Coordinate.newInstance(Double.parseDouble(endCoordX), Double.parseDouble(endCoordY)); builder.setEndLocationCoordinate(coordinate); } String start = vehicleConfig.getString("timeSchedule.start"); String end = vehicleConfig.getString("timeSchedule.end"); if (start != null) builder.setEarliestStart(Double.parseDouble(start)); if (end != null) builder.setLatestArrival(Double.parseDouble(end)); String returnToDepot = vehicleConfig.getString("returnToDepot"); if (returnToDepot != null) { builder.setReturnToDepot(vehicleConfig.getBoolean("returnToDepot")); } VehicleImpl vehicle = builder.build(); vrpBuilder.addVehicle(vehicle); vehicleMap.put(vehicleId, vehicle); } }