private void writeInitialRoutes(XMLConf xmlConfig) { if (vrp.getInitialVehicleRoutes().isEmpty()) return; String path = "initialRoutes.route"; int routeCounter = 0; for (VehicleRoute route : vrp.getInitialVehicleRoutes()) { xmlConfig.setProperty(path + "(" + routeCounter + ").driverId", route.getDriver().getId()); xmlConfig.setProperty(path + "(" + routeCounter + ").vehicleId", route.getVehicle().getId()); xmlConfig.setProperty(path + "(" + routeCounter + ").start", route.getStart().getEndTime()); int actCounter = 0; for (TourActivity act : route.getTourActivities().getActivities()) { xmlConfig.setProperty( path + "(" + routeCounter + ").act(" + actCounter + ")[@type]", act.getName()); if (act instanceof JobActivity) { Job job = ((JobActivity) act).getJob(); if (job instanceof Service) { xmlConfig.setProperty( path + "(" + routeCounter + ").act(" + actCounter + ").serviceId", job.getId()); } else if (job instanceof Shipment) { xmlConfig.setProperty( path + "(" + routeCounter + ").act(" + actCounter + ").shipmentId", job.getId()); } else { throw new IllegalStateException( "cannot write solution correctly since job-type is not know. make sure you use either service or shipment, or another writer"); } } xmlConfig.setProperty( path + "(" + routeCounter + ").act(" + actCounter + ").arrTime", act.getArrTime()); xmlConfig.setProperty( path + "(" + routeCounter + ").act(" + actCounter + ").endTime", act.getEndTime()); actCounter++; } xmlConfig.setProperty(path + "(" + routeCounter + ").end", route.getEnd().getArrTime()); routeCounter++; } }
public void visit(TourActivity activity) { for (Vehicle vehicle : vehicles) { double latestArrTimeAtPrevAct = latest_arrTimes_at_prevAct[vehicle.getVehicleTypeIdentifier().getIndex()]; Location prevLocation = location_of_prevAct[vehicle.getVehicleTypeIdentifier().getIndex()]; double potentialLatestArrivalTimeAtCurrAct = latestArrTimeAtPrevAct - transportCosts.getBackwardTransportTime( activity.getLocation(), prevLocation, latestArrTimeAtPrevAct, route.getDriver(), vehicle) - activity.getOperationTime(); double latestArrivalTime = Math.min( activity.getTheoreticalLatestOperationStartTime(), potentialLatestArrivalTimeAtCurrAct); if (latestArrivalTime < activity.getTheoreticalEarliestOperationStartTime()) { stateManager.putTypedInternalRouteState( route, vehicle, InternalStates.SWITCH_NOT_FEASIBLE, true); } stateManager.putInternalTypedActivityState( activity, vehicle, InternalStates.LATEST_OPERATION_START_TIME, latestArrivalTime); latest_arrTimes_at_prevAct[vehicle.getVehicleTypeIdentifier().getIndex()] = latestArrivalTime; location_of_prevAct[vehicle.getVehicleTypeIdentifier().getIndex()] = activity.getLocation(); } }
@Override public double getCosts( JobInsertionContext iFacts, TourActivity prevAct, TourActivity newAct, TourActivity nextAct, double depTimeAtPrevAct) { double tp_costs_prevAct_newAct = routingCosts.getTransportCost( prevAct.getLocationId(), newAct.getLocationId(), depTimeAtPrevAct, iFacts.getNewDriver(), iFacts.getNewVehicle()); double tp_time_prevAct_newAct = routingCosts.getTransportTime( prevAct.getLocationId(), newAct.getLocationId(), depTimeAtPrevAct, iFacts.getNewDriver(), iFacts.getNewVehicle()); double newAct_arrTime = depTimeAtPrevAct + tp_time_prevAct_newAct; double newAct_endTime = CalculationUtils.getActivityEndTime(newAct_arrTime, newAct); // open routes if (nextAct instanceof End) { if (!iFacts.getNewVehicle().isReturnToDepot()) { return tp_costs_prevAct_newAct; } } double tp_costs_newAct_nextAct = routingCosts.getTransportCost( newAct.getLocationId(), nextAct.getLocationId(), newAct_endTime, iFacts.getNewDriver(), iFacts.getNewVehicle()); double totalCosts = tp_costs_prevAct_newAct + tp_costs_newAct_nextAct; double oldCosts; if (iFacts.getRoute().isEmpty()) { double tp_costs_prevAct_nextAct = routingCosts.getTransportCost( prevAct.getLocationId(), nextAct.getLocationId(), depTimeAtPrevAct, iFacts.getNewDriver(), iFacts.getNewVehicle()); oldCosts = tp_costs_prevAct_nextAct; } else { double tp_costs_prevAct_nextAct = routingCosts.getTransportCost( prevAct.getLocationId(), nextAct.getLocationId(), prevAct.getEndTime(), iFacts.getRoute().getDriver(), iFacts.getRoute().getVehicle()); oldCosts = tp_costs_prevAct_nextAct; } return totalCosts - oldCosts; }
private void writeSolutions(XMLConf xmlConfig) { if (solutions == null) return; String solutionPath = "solutions.solution"; int counter = 0; for (VehicleRoutingProblemSolution solution : solutions) { xmlConfig.setProperty(solutionPath + "(" + counter + ").cost", solution.getCost()); int routeCounter = 0; for (VehicleRoute route : solution.getRoutes()) { // xmlConfig.setProperty(solutionPath + "(" + counter + ").routes.route(" + routeCounter // + ").cost", route.getCost()); xmlConfig.setProperty( solutionPath + "(" + counter + ").routes.route(" + routeCounter + ").driverId", route.getDriver().getId()); xmlConfig.setProperty( solutionPath + "(" + counter + ").routes.route(" + routeCounter + ").vehicleId", route.getVehicle().getId()); xmlConfig.setProperty( solutionPath + "(" + counter + ").routes.route(" + routeCounter + ").start", route.getStart().getEndTime()); int actCounter = 0; for (TourActivity act : route.getTourActivities().getActivities()) { xmlConfig.setProperty( solutionPath + "(" + counter + ").routes.route(" + routeCounter + ").act(" + actCounter + ")[@type]", act.getName()); if (act instanceof JobActivity) { Job job = ((JobActivity) act).getJob(); if (job instanceof Service) { xmlConfig.setProperty( solutionPath + "(" + counter + ").routes.route(" + routeCounter + ").act(" + actCounter + ").serviceId", job.getId()); } else if (job instanceof Shipment) { xmlConfig.setProperty( solutionPath + "(" + counter + ").routes.route(" + routeCounter + ").act(" + actCounter + ").shipmentId", job.getId()); } else { throw new IllegalStateException( "cannot write solution correctly since job-type is not know. make sure you use either service or shipment, or another writer"); } } xmlConfig.setProperty( solutionPath + "(" + counter + ").routes.route(" + routeCounter + ").act(" + actCounter + ").arrTime", act.getArrTime()); xmlConfig.setProperty( solutionPath + "(" + counter + ").routes.route(" + routeCounter + ").act(" + actCounter + ").endTime", act.getEndTime()); actCounter++; } xmlConfig.setProperty( solutionPath + "(" + counter + ").routes.route(" + routeCounter + ").end", route.getEnd().getArrTime()); routeCounter++; } int unassignedJobCounter = 0; for (Job unassignedJob : solution.getUnassignedJobs()) { xmlConfig.setProperty( solutionPath + "(" + counter + ").unassignedJobs.job(" + unassignedJobCounter + ")[@id]", unassignedJob.getId()); unassignedJobCounter++; } counter++; } }
private static void printVerbose( PrintWriter out, VehicleRoutingProblem problem, VehicleRoutingProblemSolution solution) { String leftAlgin = "| %-7s | %-20s | %-21s | %-15s | %-15s | %-15s | %-15s |%n"; out.format( "+--------------------------------------------------------------------------------------------------------------------------------+%n"); out.printf( "| detailed solution |%n"); out.format( "+---------+----------------------+-----------------------+-----------------+-----------------+-----------------+-----------------+%n"); out.printf( "| route | vehicle | activity | job | arrTime | endTime | costs |%n"); int routeNu = 1; for (VehicleRoute route : solution.getRoutes()) { out.format( "+---------+----------------------+-----------------------+-----------------+-----------------+-----------------+-----------------+%n"); double costs = 0; out.format( leftAlgin, routeNu, getVehicleString(route), route.getStart().getName(), "-", "undef", Math.round(route.getStart().getEndTime()), Math.round(costs)); TourActivity prevAct = route.getStart(); for (TourActivity act : route.getActivities()) { String jobId; if (act instanceof JobActivity) { jobId = ((JobActivity) act).getJob().getId(); } else { jobId = "-"; } double c = problem .getTransportCosts() .getTransportCost( prevAct.getLocation(), act.getLocation(), prevAct.getEndTime(), route.getDriver(), route.getVehicle()); c += problem .getActivityCosts() .getActivityCost(act, act.getArrTime(), route.getDriver(), route.getVehicle()); costs += c; out.format( leftAlgin, routeNu, getVehicleString(route), act.getName(), jobId, Math.round(act.getArrTime()), Math.round(act.getEndTime()), Math.round(costs)); prevAct = act; } double c = problem .getTransportCosts() .getTransportCost( prevAct.getLocation(), route.getEnd().getLocation(), prevAct.getEndTime(), route.getDriver(), route.getVehicle()); c += problem .getActivityCosts() .getActivityCost( route.getEnd(), route.getEnd().getArrTime(), route.getDriver(), route.getVehicle()); costs += c; out.format( leftAlgin, routeNu, getVehicleString(route), route.getEnd().getName(), "-", Math.round(route.getEnd().getArrTime()), "undef", Math.round(costs)); routeNu++; } out.format( "+--------------------------------------------------------------------------------------------------------------------------------+%n"); if (!solution.getUnassignedJobs().isEmpty()) { out.format("+----------------+%n"); out.format("| unassignedJobs |%n"); out.format("+----------------+%n"); String unassignedJobAlgin = "| %-14s |%n"; for (Job j : solution.getUnassignedJobs()) { out.format(unassignedJobAlgin, j.getId()); } out.format("+----------------+%n"); } }
/** * Calculates actEndTime assuming that activity can at earliest start at * act.getTheoreticalEarliestOperationStartTime(). * * @param actArrTime * @param act * @return */ public static double getActivityEndTime(double actArrTime, TourActivity act) { return Math.max(actArrTime, act.getTheoreticalEarliestOperationStartTime()) + act.getOperationTime(); }