1
0
Fork 0
mirror of https://github.com/graphhopper/jsprit.git synced 2020-01-24 07:45:05 +01:00

speed up regret insertion with breaks

This commit is contained in:
oblonski 2015-10-13 20:44:00 +02:00
parent a54315f28a
commit 10defbe1ec
3 changed files with 61 additions and 34 deletions

View file

@ -45,37 +45,6 @@ public class VehicleRoutingAlgorithmListeners {
return l;
}
// @Override
// public int hashCode() {
// final int prime = 31;
// int result = 1;
// result = prime * result + ((l == null) ? 0 : l.hashCode());
// result = prime * result
// + ((priority == null) ? 0 : priority.hashCode());
// return result;
// }
// @Override
// public boolean equals(Object obj) {
// if (this == obj)
// return true;
// if (obj == null)
// return false;
// if (getClass() != obj.getClass())
// return false;
// PrioritizedVRAListener other = (PrioritizedVRAListener) obj;
// if (l == null) {
// if (other.l != null)
// return false;
// } else if (!l.equals(other.l))
// return false;
// if (priority == null) {
// if (other.priority != null)
// return false;
// } else if (!priority.equals(other.priority))
// return false;
// return true;
// }
}
public enum Priority {

View file

@ -19,6 +19,7 @@ package jsprit.core.algorithm.recreate;
import jsprit.core.problem.Location;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.job.Break;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.job.Service;
import jsprit.core.problem.job.Shipment;
@ -28,6 +29,7 @@ import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
/**
@ -223,8 +225,28 @@ public class RegretInsertion extends AbstractInsertionStrategy {
@Override
public Collection<Job> insertUnassignedJobs(Collection<VehicleRoute> routes, Collection<Job> unassignedJobs) {
List<Job> badJobs = new ArrayList<Job>(unassignedJobs.size());
List<Job> jobs = new ArrayList<Job>(unassignedJobs);
Iterator<Job> jobIterator = unassignedJobs.iterator();
while (jobIterator.hasNext()){
Job job = jobIterator.next();
if(job instanceof Break){
VehicleRoute route = findRoute(routes,job);
if(route == null){
badJobs.add(job);
}
else {
InsertionData iData = insertionCostsCalculator.getInsertionData(route, job, NO_NEW_VEHICLE_YET, NO_NEW_DEPARTURE_TIME_YET, NO_NEW_DRIVER_YET, Double.MAX_VALUE);
if (iData instanceof InsertionData.NoInsertionFound) {
badJobs.add(job);
} else {
insertJob(job, iData, route);
}
}
jobIterator.remove();
}
}
List<Job> jobs = new ArrayList<Job>(unassignedJobs);
while (!jobs.isEmpty()) {
List<Job> unassignedJobList = new ArrayList<Job>(jobs);
List<Job> badJobList = new ArrayList<Job>();
@ -244,7 +266,14 @@ public class RegretInsertion extends AbstractInsertionStrategy {
return badJobs;
}
private ScoredJob nextJob(Collection<VehicleRoute> routes, List<Job> unassignedJobList, List<Job> badJobs) {
private VehicleRoute findRoute(Collection<VehicleRoute> routes, Job job) {
for(VehicleRoute r : routes){
if(r.getVehicle().getBreak() == job) return r;
}
return null;
}
private ScoredJob nextJob(Collection<VehicleRoute> routes, Collection<Job> unassignedJobList, List<Job> badJobs) {
ScoredJob bestScoredJob = null;
for (Job unassignedJob : unassignedJobList) {
ScoredJob scoredJob = getScoredJob(routes, unassignedJob, insertionCostsCalculator, scoringFunction);

View file

@ -21,6 +21,7 @@ import jsprit.core.algorithm.recreate.RegretInsertion.DefaultScorer;
import jsprit.core.algorithm.recreate.RegretInsertion.ScoredJob;
import jsprit.core.algorithm.recreate.RegretInsertion.ScoringFunction;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.job.Break;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.solution.route.VehicleRoute;
import org.apache.logging.log4j.LogManager;
@ -28,6 +29,7 @@ import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.*;
@ -88,8 +90,28 @@ public class RegretInsertionConcurrent extends AbstractInsertionStrategy {
@Override
public Collection<Job> insertUnassignedJobs(Collection<VehicleRoute> routes, Collection<Job> unassignedJobs) {
List<Job> badJobs = new ArrayList<Job>(unassignedJobs.size());
List<Job> jobs = new ArrayList<Job>(unassignedJobs);
Iterator<Job> jobIterator = unassignedJobs.iterator();
while (jobIterator.hasNext()){
Job job = jobIterator.next();
if(job instanceof Break){
VehicleRoute route = findRoute(routes,job);
if(route == null){
badJobs.add(job);
}
else {
InsertionData iData = insertionCostsCalculator.getInsertionData(route, job, NO_NEW_VEHICLE_YET, NO_NEW_DEPARTURE_TIME_YET, NO_NEW_DRIVER_YET, Double.MAX_VALUE);
if (iData instanceof InsertionData.NoInsertionFound) {
badJobs.add(job);
} else {
insertJob(job, iData, route);
}
}
jobIterator.remove();
}
}
List<Job> jobs = new ArrayList<Job>(unassignedJobs);
while (!jobs.isEmpty()) {
List<Job> unassignedJobList = new ArrayList<Job>(jobs);
List<Job> badJobList = new ArrayList<Job>();
@ -150,5 +172,12 @@ public class RegretInsertionConcurrent extends AbstractInsertionStrategy {
return bestScoredJob;
}
private VehicleRoute findRoute(Collection<VehicleRoute> routes, Job job) {
for(VehicleRoute r : routes){
if(r.getVehicle().getBreak() == job) return r;
}
return null;
}
}