/** * Builds and returns the vehicle. * * <p>if {@link VehicleType} is not set, default vehicle-type is set with id="default" and * capacity=0 * * <p>if startLocationId || locationId is null (=> startLocationCoordinate || locationCoordinate * must be set) then startLocationId=startLocationCoordinate.toString() and * locationId=locationCoordinate.toString() [coord.toString() --> [x=x_val][y=y_val]) * * <p>if endLocationId is null and endLocationCoordinate is set then * endLocationId=endLocationCoordinate.toString() * * <p>if endLocationId==null AND endLocationCoordinate==null then endLocationId=startLocationId * AND endLocationCoord=startLocationCoord Thus endLocationId can never be null even * returnToDepot is false. * * @return vehicle * @throws IllegalStateException if both locationId and locationCoord is not set or * (endLocationCoord!=null AND returnToDepot=false) or (endLocationId!=null AND * returnToDepot=false) */ public VehicleImpl build() { if (startLocation != null && endLocation != null) { if (!startLocation.getId().equals(endLocation.getId()) && !returnToDepot) throw new IllegalStateException( "this must not be. you specified both endLocationId and open-routes. this is contradictory. <br>" + "if you set endLocation, returnToDepot must be true. if returnToDepot is false, endLocationCoord must not be specified."); } if (startLocation != null && endLocation == null) { endLocation = startLocation; } if (startLocation == null && endLocation == null) { throw new IllegalStateException( "vehicle requires startLocation. but neither locationId nor locationCoord nor startLocationId nor startLocationCoord has been set"); } skills = skillBuilder.build(); return new VehicleImpl(this); }
private VehicleImpl(Builder builder) { id = builder.id; type = builder.type; earliestDeparture = builder.earliestStart; latestArrival = builder.latestArrival; returnToDepot = builder.returnToDepot; skills = builder.skills; endLocation = builder.endLocation; startLocation = builder.startLocation; setVehicleIdentifier( new VehicleTypeKey( type.getTypeId(), startLocation.getId(), endLocation.getId(), earliestDeparture, latestArrival, skills)); }
private double calculateDistance(Location fromLocation, Location toLocation) { Coordinate from = null; Coordinate to = null; if (fromLocation.getCoordinate() != null & toLocation.getCoordinate() != null) { from = fromLocation.getCoordinate(); to = toLocation.getCoordinate(); } else if (locations != null) { from = locations.getCoord(fromLocation.getId()); to = locations.getCoord(toLocation.getId()); } if (from == null || to == null) throw new NullPointerException(); return calculateDistance(from, to); }
public void read(String fileName) { vrpBuilder.setFleetSize(FleetSize.FINITE); BufferedReader reader = getReader(fileName); int vrpType; int nOfDepots = 0; int nOfCustomers = 0; int nOfVehiclesAtEachDepot = 0; int counter = 0; String line; List<List<Builder>> vehiclesAtDepot = new ArrayList<List<Builder>>(); int depotCounter = 0; while ((line = readLine(reader)) != null) { line = line.replace("\r", ""); line = line.trim(); String[] tokens = line.split("\\s+"); if (counter == 0) { vrpType = Integer.parseInt(tokens[0].trim()); if (vrpType != 2) throw new IllegalStateException("expect vrpType to be equal to 2 and thus to be MDVRP"); nOfVehiclesAtEachDepot = Integer.parseInt(tokens[1].trim()); nOfCustomers = Integer.parseInt(tokens[2].trim()); nOfDepots = Integer.parseInt(tokens[3].trim()); } else if (counter <= nOfDepots) { String depot = Integer.valueOf(counter).toString(); int duration = Integer.parseInt(tokens[0].trim()); if (duration == 0) duration = 999999; int capacity = Integer.parseInt(tokens[1].trim()); VehicleTypeImpl vehicleType = VehicleTypeImpl.Builder.newInstance(counter + "_cordeauType") .addCapacityDimension(0, capacity) .setCostPerDistance(1.0) .setFixedCost(0) .build(); List<Builder> builders = new ArrayList<VehicleImpl.Builder>(); for (int vehicleCounter = 0; vehicleCounter < nOfVehiclesAtEachDepot; vehicleCounter++) { Builder vBuilder = VehicleImpl.Builder.newInstance( depot + "_" + (vehicleCounter + 1) + "_cordeauVehicle"); vBuilder.setLatestArrival(duration).setType(vehicleType); builders.add(vBuilder); } vehiclesAtDepot.add(builders); } else if (counter <= (nOfCustomers + nOfDepots)) { String id = tokens[0].trim(); Coordinate customerCoord = makeCoord(tokens[1].trim(), tokens[2].trim()); double serviceTime = Double.parseDouble(tokens[3].trim()); int demand = Integer.parseInt(tokens[4].trim()); Service service = Service.Builder.newInstance(id) .addSizeDimension(0, demand) .setServiceTime(serviceTime) .setLocation( Location.Builder.newInstance().setId(id).setCoordinate(customerCoord).build()) .build(); vrpBuilder.addJob(service); } else if (counter <= (nOfCustomers + nOfDepots + nOfDepots)) { Coordinate depotCoord = makeCoord(tokens[1].trim(), tokens[2].trim()); List<Builder> vBuilders = vehiclesAtDepot.get(depotCounter); for (Builder vBuilder : vBuilders) { vBuilder.setStartLocation(Location.newInstance(depotCoord.getX(), depotCoord.getY())); VehicleImpl vehicle = vBuilder.build(); vrpBuilder.addVehicle(vehicle); } depotCounter++; } else { throw new IllegalStateException("there are more lines than expected in file."); } counter++; } close(reader); }