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); } }
private void readServices(XMLConfiguration vrpProblem) { List<HierarchicalConfiguration> serviceConfigs = vrpProblem.configurationsAt("services.service"); for (HierarchicalConfiguration serviceConfig : serviceConfigs) { String id = serviceConfig.getString("[@id]"); if (id == null) throw new IllegalStateException("service[@id] is missing."); String type = serviceConfig.getString("[@type]"); if (type == null) type = "service"; String capacityString = serviceConfig.getString("capacity-demand"); boolean capacityDimensionsExist = serviceConfig.containsKey("capacity-dimensions.dimension(0)"); if (capacityString == null && !capacityDimensionsExist) { throw new IllegalStateException( "capacity of service 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'."); } Service.Builder builder; if (capacityString != null) { builder = serviceBuilderFactory.createBuilder(type, id, Integer.parseInt(capacityString)); } else { builder = serviceBuilderFactory.createBuilder(type, id, null); List<HierarchicalConfiguration> dimensionConfigs = serviceConfig.configurationsAt("capacity-dimensions.dimension"); for (HierarchicalConfiguration dimension : dimensionConfigs) { Integer index = dimension.getInt("[@index]"); Integer value = dimension.getInt(""); builder.addSizeDimension(index, value); } } String serviceLocationId = serviceConfig.getString("locationId"); if (serviceLocationId != null) builder.setLocationId(serviceLocationId); Coordinate serviceCoord = getCoord(serviceConfig, ""); if (serviceCoord != null) { builder.setCoord(serviceCoord); if (serviceLocationId != null) { // vrpBuilder.addLocation(serviceLocationId,serviceCoord); } else { // vrpBuilder.addLocation(serviceCoord.toString(),serviceCoord); builder.setLocationId(serviceCoord.toString()); } } if (serviceConfig.containsKey("duration")) { builder.setServiceTime(serviceConfig.getDouble("duration")); } List<HierarchicalConfiguration> deliveryTWConfigs = serviceConfig.configurationsAt("timeWindows.timeWindow"); if (!deliveryTWConfigs.isEmpty()) { for (HierarchicalConfiguration twConfig : deliveryTWConfigs) { builder.setTimeWindow( TimeWindow.newInstance(twConfig.getDouble("start"), twConfig.getDouble("end"))); } } Service service = builder.build(); serviceMap.put(service.getId(), service); // vrpBuilder.addJob(service); } }