/** * Removes job AND belonging activity from tour. Note that if job is a Service, it is assumed that there is only one belonging activity, * thus, it stops trying to remove activities once the first is found. * If you want to always look for belonging activities to be removed in the entire route, use removeJob(Job job, boolean enforceEntireRoute) * * @param job to be removed * @return true if job has been removed, otherwise false. */ public boolean removeJob(Job job) { boolean jobRemoved = false; if (!jobs.contains(job)) { return false; } else { jobRemoved = jobs.remove(job); } boolean activityRemoved = false; Iterator<TourActivity> iterator = tourActivities.iterator(); while (iterator.hasNext()) { TourActivity c = iterator.next(); if (c instanceof JobActivity) { Job underlyingJob = ((JobActivity) c).getJob(); if (job.equals(underlyingJob)) { iterator.remove(); activityRemoved = true; } } } assert jobRemoved == activityRemoved : "job removed, but belonging activity not."; return activityRemoved; }
private List<LocationWrapper> getLocationWrappers(VehicleRoute route) { List<LocationWrapper> locations = new ArrayList<LocationWrapper>(route.getTourActivities().getJobs().size()); Map<Job, List<Location>> jobs2locations = new HashMap<Job, List<Location>>(); for (TourActivity act : route.getActivities()) { if (act instanceof TourActivity.JobActivity) { Job job = ((TourActivity.JobActivity) act).getJob(); if (!jobs2locations.containsKey(job)) { jobs2locations.put(job, new ArrayList<Location>()); } jobs2locations.get(job).add(act.getLocation()); } } for (Job j : jobs2locations.keySet()) { locations.add(new LocationWrapper(j, jobs2locations.get(j))); } return locations; }
private void log(VehicleRoutingProblemSolution solution) { logger.trace("solution costs: {}", solution.getCost()); for (VehicleRoute r : solution.getRoutes()) { StringBuilder b = new StringBuilder(); b.append(r.getVehicle().getId()).append(" : ").append("[ "); for (TourActivity act : r.getActivities()) { if (act instanceof TourActivity.JobActivity) { b.append(((TourActivity.JobActivity) act).getJob().getId()).append(" "); } } b.append("]"); logger.trace(b.toString()); } StringBuilder b = new StringBuilder(); b.append("unassigned : [ "); for (Job j : solution.getUnassignedJobs()) { b.append(j.getId()).append(" "); } b.append("]"); logger.trace(b.toString()); }
xmlConfig.setProperty(path + "(" + routeCounter + ").act(" + actCounter + ")[@type]", act.getName()); if (act instanceof TourActivity.JobActivity) { Job job = ((TourActivity.JobActivity) act).getJob(); if (job instanceof Service) { xmlConfig.setProperty(path + "(" + routeCounter + ").act(" + actCounter + ").serviceId", job.getId());
double directTimeOfNearestMessenger = bestMessengers.get(((JobActivity) newAct).getJob().getId()); if (arrTime_at_newAct > 3 * directTimeOfNearestMessenger) {
@Override public void visit(TourActivity activity) { double maxTime = getMaxTimeInVehicle(activity); for(Vehicle v : vehicles) { int vehicleIndex = v.getVehicleTypeIdentifier().getIndex(); Location prevActLocation = prevActLocations[vehicleIndex]; double prevActEndTime = prevActEndTimes[v.getVehicleTypeIdentifier().getIndex()]; double activityArrival = prevActEndTimes[v.getVehicleTypeIdentifier().getIndex()] + transportTime.getTransportTime(prevActLocation,activity.getLocation(),prevActEndTime,route.getDriver(),v); double activityStart = Math.max(activityArrival,activity.getTheoreticalEarliestOperationStartTime()); memorizeActStart(activity,v,activityStart); double activityEnd = activityStart + activityCosts.getActivityDuration(activity, activityArrival, route.getDriver(), v); Map<Job, Double> openPickups = openPickupEndTimesPerVehicle.get(vehicleIndex); if (activity instanceof ServiceActivity || activity instanceof PickupActivity) { openPickups.put(((TourActivity.JobActivity) activity).getJob(), activityEnd); } else if (activity instanceof DeliveryActivity) { Job job = ((TourActivity.JobActivity) activity).getJob(); double pickupEnd; if (openPickups.containsKey(job)) { pickupEnd = openPickups.get(job); openPickups.remove(job); } else pickupEnd = v.getEarliestDeparture(); double slackTime = maxTime - (activityStart - pickupEnd); slackTimesPerVehicle.get(vehicleIndex).put(activity, slackTime); } prevActLocations[vehicleIndex] = activity.getLocation(); prevActEndTimes[vehicleIndex] = activityEnd; } }
/** * Adds an initial vehicle route. * * @param route initial route * @return the builder */ public Builder addInitialVehicleRoute(VehicleRoute route) { if(!addedVehicleIds.contains(route.getVehicle().getId())){ addVehicle((AbstractVehicle) route.getVehicle()); addedVehicleIds.add(route.getVehicle().getId()); } for (TourActivity act : route.getActivities()) { AbstractActivity abstractAct = (AbstractActivity) act; abstractAct.setIndex(activityIndexCounter); incActivityIndexCounter(); if (act instanceof TourActivity.JobActivity) { Job job = ((TourActivity.JobActivity) act).getJob(); jobsInInitialRoutes.put(job.getId(), job); addLocationToTentativeLocations(job); registerJobAndActivity(abstractAct, job); } } initialRoutes.add(route); return this; }
private void handleVehicleSwitch(InsertionData data, VehicleRoute route) { boolean vehicleSwitch = false; if (!(route.getVehicle() instanceof VehicleImpl.NoVehicle)) { if (!route.getVehicle().getId().equals(data.getSelectedVehicle().getId())) { vehicleSwitch = true; } } if (vehicleSwitch && !route.getActivities().isEmpty()) { String oldStart = makeStartId(route.getVehicle()); String firstAct = ((TourActivity.JobActivity) route.getActivities().get(0)).getJob().getId(); String oldEnd = makeEndId(route.getVehicle()); String lastAct = ((TourActivity.JobActivity) route.getActivities().get(route.getActivities().size() - 1)).getJob().getId(); removeEdge(oldStart + "_" + firstAct); if (route.getVehicle().isReturnToDepot()) { removeEdge(lastAct + "_" + oldEnd); } String newStart = makeStartId(data.getSelectedVehicle()); String newEnd = makeEndId(data.getSelectedVehicle()); addEdge(newStart + "_" + firstAct, newStart, firstAct); if (data.getSelectedVehicle().isReturnToDepot()) { addEdge(lastAct + "_" + newEnd, lastAct, newEnd); } } }
@Override public void visit(TourActivity activity) { boolean violatedAtActivity = false; if (activity instanceof TourActivity.JobActivity) { Set<String> requiredForActivity = ((TourActivity.JobActivity) activity).getJob().getRequiredSkills().values(); for (String skill : requiredForActivity) { if (!route.getVehicle().getSkills().containsSkill(skill)) { violatedAtActivity = true; skillConstraintViolatedOnRoute = true; } } } stateManager.putActivityState(activity, skill_id, violatedAtActivity); }
private boolean isUseKey(TourActivity act) { if (!(act instanceof TourActivity.JobActivity)) return false; return ((TourActivity.JobActivity) act).getJob().getName().equals("use key"); }
private boolean isPickupKey(TourActivity act) { if (!(act instanceof TourActivity.JobActivity)) return false; return ((TourActivity.JobActivity) act).getJob().getName().equals("get key"); }
private boolean isDeliverKey(TourActivity act) { if (!(act instanceof TourActivity.JobActivity)) return false; return ((TourActivity.JobActivity) act).getJob().getName().equals("deliver key"); }
private String getNodeId(TourActivity act) { String nodeId = null; if (act instanceof TourActivity.JobActivity) { Job job = ((TourActivity.JobActivity) act).getJob(); if (job instanceof Service) { nodeId = job.getId(); } else if (job instanceof Shipment) { if (act.getName().equals("pickupShipment")) nodeId = getFromNodeId((Shipment) job); else nodeId = getToNodeId((Shipment) job); } } return nodeId; }
private static void validateSolution(VehicleRoutingProblemSolution bestOf, VehicleRoutingProblem bicycleMessengerProblem, Map<String, Double> nearestMessengers) { for (VehicleRoute route : bestOf.getRoutes()) { for (TourActivity act : route.getActivities()) { if (act.getArrTime() > 3 * nearestMessengers.get(((JobActivity) act).getJob().getId())) { SolutionPrinter.print(bicycleMessengerProblem, bestOf, SolutionPrinter.Print.VERBOSE); throw new IllegalStateException("three times less than ... constraint broken. this must not be. act.getArrTime(): " + act.getArrTime() + " allowed: " + 3 * nearestMessengers.get(((JobActivity) act).getJob().getId())); } } } }
@Override public void visit(TourActivity currAct) { double timeOfNearestMessenger = bestMessengers.get(((JobActivity) currAct).getJob().getId()); double potential_latest_arrTime_at_currAct = latest_arrTime_at_prevAct - routingCosts.getBackwardTransportTime(currAct.getLocation(), prevAct.getLocation(), latest_arrTime_at_prevAct, route.getDriver(), route.getVehicle()) - currAct.getOperationTime(); double latest_arrTime_at_currAct = Math.min(3 * timeOfNearestMessenger, potential_latest_arrTime_at_currAct); stateManager.putActivityState(currAct, latest_act_arrival_time_stateId, latest_arrTime_at_currAct); assert currAct.getArrTime() <= latest_arrTime_at_currAct : "this must not be since it breaks condition; actArrTime: " + currAct.getArrTime() + " latestArrTime: " + latest_arrTime_at_currAct + " vehicle: " + route.getVehicle().getId(); latest_arrTime_at_prevAct = latest_arrTime_at_currAct; prevAct = currAct; }
@Override public void visit(TourActivity activity) { if (activity instanceof TourActivity.JobActivity) { Skills skills = ((TourActivity.JobActivity) activity).getJob().getRequiredSkills(); skillBuilder.addAllSkills(skills.values()); } }
@Override public void visit(TourActivity activity) { if (((TourActivity.JobActivity) activity).getJob().getName().equals("use key")) { stateManager.putProblemState(keyUsedStateId, VehicleRoute.class, route); } else if (((TourActivity.JobActivity) activity).getJob().getName().equals("get key")) { stateManager.putProblemState(keyPickedStateId, VehicleRoute.class, route); } else if (((TourActivity.JobActivity) activity).getJob().getName().equals("deliver key")) { stateManager.putProblemState(keyDeliveredStateId, VehicleRoute.class, route); } }
private void addJob(TourActivity act) { if (act instanceof JobActivity) { Job job = ((JobActivity) act).getJob(); // if(job instanceof Service) assert !jobs.contains(job); jobs.add(job); } }
private double getMaxTimeInVehicle(TourActivity activity) { double maxTime = Double.MAX_VALUE; if(activity instanceof TourActivity.JobActivity){ maxTime = ((TourActivity.JobActivity) activity).getJob().getMaxTimeInVehicle(); } return maxTime; }