mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
Merge branch 'prepareV010' into PickupMergeRelaxAPI
Conflicts: jsprit-core/src/main/java/algorithms/BestInsertionBuilder.java jsprit-core/src/main/java/algorithms/BestInsertionConcurrent.java
This commit is contained in:
commit
d1dac2d622
21 changed files with 11614 additions and 10997 deletions
|
|
@ -9,7 +9,7 @@ import basics.algo.InsertionListener;
|
|||
import basics.algo.VehicleRoutingAlgorithmListeners.PrioritizedVRAListener;
|
||||
import basics.route.VehicleFleetManager;
|
||||
|
||||
public class BestInsertionBuilder implements InsertionStrategyBuilder{
|
||||
public class BestInsertionBuilder {
|
||||
|
||||
private VehicleRoutingProblem vrp;
|
||||
|
||||
|
|
@ -35,11 +35,11 @@ public class BestInsertionBuilder implements InsertionStrategyBuilder{
|
|||
|
||||
private int nuOfThreads;
|
||||
|
||||
public BestInsertionBuilder(VehicleRoutingProblem vrp, VehicleFleetManager vehicleFleetManager, StateManager stateManager) {
|
||||
public BestInsertionBuilder(VehicleRoutingProblem vrp, VehicleFleetManager vehicleFleetManager, StateManager stateManager, ConstraintManager constraintManager) {
|
||||
super();
|
||||
this.vrp = vrp;
|
||||
this.stateManager = stateManager;
|
||||
this.constraintManager = new ConstraintManager(vrp,stateManager);
|
||||
this.constraintManager = constraintManager;
|
||||
this.fleetManager = vehicleFleetManager;
|
||||
}
|
||||
|
||||
|
|
@ -73,7 +73,7 @@ public class BestInsertionBuilder implements InsertionStrategyBuilder{
|
|||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
public InsertionStrategy build() {
|
||||
List<InsertionListener> iListeners = new ArrayList<InsertionListener>();
|
||||
List<PrioritizedVRAListener> algorithmListeners = new ArrayList<PrioritizedVRAListener>();
|
||||
|
|
@ -99,14 +99,12 @@ public class BestInsertionBuilder implements InsertionStrategyBuilder{
|
|||
|
||||
}
|
||||
else{
|
||||
bestInsertion = new BestInsertionConc(jobInsertions,executor,nuOfThreads);
|
||||
|
||||
bestInsertion = new BestInsertionConcurrent(jobInsertions,executor,nuOfThreads);
|
||||
|
||||
}
|
||||
for(InsertionListener l : iListeners) bestInsertion.addListener(l);
|
||||
return bestInsertion;
|
||||
}
|
||||
|
||||
public void setConstraintManager(ConstraintManager constraintManager) {
|
||||
this.constraintManager = constraintManager;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ import basics.route.VehicleRoute;
|
|||
*
|
||||
*/
|
||||
|
||||
final class BestInsertionConc implements InsertionStrategy{
|
||||
final class BestInsertionConcurrent implements InsertionStrategy{
|
||||
|
||||
static class Batch {
|
||||
List<VehicleRoute> routes = new ArrayList<VehicleRoute>();
|
||||
|
|
@ -74,7 +74,7 @@ final class BestInsertionConc implements InsertionStrategy{
|
|||
|
||||
}
|
||||
|
||||
private static Logger logger = Logger.getLogger(BestInsertionConc.class);
|
||||
private static Logger logger = Logger.getLogger(BestInsertionConcurrent.class);
|
||||
|
||||
private Random random = RandomNumberGeneration.getRandom();
|
||||
|
||||
|
|
@ -102,7 +102,7 @@ final class BestInsertionConc implements InsertionStrategy{
|
|||
this.random = random;
|
||||
}
|
||||
|
||||
public BestInsertionConc(JobInsertionCostsCalculator jobInsertionCalculator, ExecutorService executorService, int nuOfBatches) {
|
||||
public BestInsertionConcurrent(JobInsertionCostsCalculator jobInsertionCalculator, ExecutorService executorService, int nuOfBatches) {
|
||||
super();
|
||||
this.insertionsListeners = new InsertionListeners();
|
||||
this.executor = executorService;
|
||||
|
|
@ -27,8 +27,10 @@ import basics.route.Driver;
|
|||
import basics.route.Vehicle;
|
||||
import basics.route.VehicleRoute;
|
||||
|
||||
|
||||
class CalculatesServiceInsertionWithTimeScheduling implements JobInsertionCostsCalculator{
|
||||
|
||||
|
||||
private static Logger log = Logger.getLogger(CalculatesServiceInsertionWithTimeScheduling.class);
|
||||
|
||||
private JobInsertionCostsCalculator jic;
|
||||
|
|
@ -67,9 +69,13 @@ class CalculatesServiceInsertionWithTimeScheduling implements JobInsertionCostsC
|
|||
|
||||
for(int i=0;i<nOfDepartureTimes;i++){
|
||||
double neighborStartTime_earlier = currentStart - (i+1)*timeSlice;
|
||||
if(neighborStartTime_earlier > earliestDeparture) vehicleDepartureTimes.add(neighborStartTime_earlier);
|
||||
// if(neighborStartTime_earlier > earliestDeparture) {
|
||||
vehicleDepartureTimes.add(neighborStartTime_earlier);
|
||||
// }
|
||||
double neighborStartTime_later = currentStart + (i+1)*timeSlice;
|
||||
if(neighborStartTime_later < latestEnd) vehicleDepartureTimes.add(neighborStartTime_later);
|
||||
// if(neighborStartTime_later < latestEnd) {
|
||||
vehicleDepartureTimes.add(neighborStartTime_later);
|
||||
// }
|
||||
}
|
||||
|
||||
InsertionData bestIData = null;
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ import basics.route.VehicleFleetManager;
|
|||
import basics.route.TourActivity.JobActivity;
|
||||
import basics.route.VehicleRoute;
|
||||
|
||||
final class Gendreau implements SearchStrategyModule{
|
||||
public final class Gendreau implements SearchStrategyModule{
|
||||
|
||||
private final static Logger log = Logger.getLogger(Gendreau.class);
|
||||
|
||||
|
|
@ -64,7 +64,7 @@ final class Gendreau implements SearchStrategyModule{
|
|||
this.shareOfJobsToRuin = shareOfJobsToRuin;
|
||||
}
|
||||
|
||||
public Gendreau(VehicleRoutingProblem vrp, RuinStrategy ruin, InsertionStrategy insertionStrategy) {
|
||||
public Gendreau(VehicleRoutingProblem vrp, RuinStrategy ruin, InsertionStrategy insertionStrategy, VehicleFleetManager vehicleFleetManager) {
|
||||
super();
|
||||
InsertionListeners insertionListeners = new InsertionListeners();
|
||||
insertionListeners.addAllListeners(insertionStrategy.getListeners());
|
||||
|
|
@ -72,6 +72,7 @@ final class Gendreau implements SearchStrategyModule{
|
|||
this.ruin = ruin;
|
||||
this.vrp = vrp;
|
||||
this.insertionStrategy = insertionStrategy;
|
||||
this.fleetManager = vehicleFleetManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -88,10 +89,10 @@ final class Gendreau implements SearchStrategyModule{
|
|||
this.nOfIterations = nOfIterations;
|
||||
}
|
||||
|
||||
public void setFleetManager(VehicleFleetManager vehicleFleetManager) {
|
||||
this.fleetManager = vehicleFleetManager;
|
||||
|
||||
}
|
||||
// public void setFleetManager(VehicleFleetManager vehicleFleetManager) {
|
||||
// this.fleetManager = vehicleFleetManager;
|
||||
//
|
||||
// }
|
||||
|
||||
@Override
|
||||
public VehicleRoutingProblemSolution runAndGetSolution(VehicleRoutingProblemSolution vrpSolution) {
|
||||
|
|
@ -119,14 +120,14 @@ final class Gendreau implements SearchStrategyModule{
|
|||
VehicleRoute emptyRoute1 = VehicleRoute.emptyRoute();
|
||||
copiedRoutes.add(emptyRoute1);
|
||||
insertionStrategy.insertJobs(Arrays.asList(emptyRoute1), Arrays.asList(targetJob));
|
||||
// routeAlgorithm.insertJob(targetJob, routeAlgorithm.calculateBestInsertion(emptyRoute1, targetJob, Double.MAX_VALUE), emptyRoute1);
|
||||
|
||||
unassignedJobs.remove(targetJob);
|
||||
|
||||
VehicleRoute emptyRoute2 = VehicleRoute.emptyRoute();
|
||||
copiedRoutes.add(emptyRoute2);
|
||||
Job job2 = jobsInRoute.get(1);
|
||||
insertionStrategy.insertJobs(Arrays.asList(emptyRoute2), Arrays.asList(job2));
|
||||
// routeAlgorithm.insertJob(job2, routeAlgorithm.calculateBestInsertion(emptyRoute2, job2, Double.MAX_VALUE), emptyRoute2);
|
||||
|
||||
unassignedJobs.remove(job2);
|
||||
|
||||
insertionStrategy.insertJobs(copiedRoutes, unassignedJobs);
|
||||
|
|
|
|||
|
|
@ -1,7 +0,0 @@
|
|||
package algorithms;
|
||||
|
||||
public interface InsertionStrategyBuilder {
|
||||
|
||||
public InsertionStrategy build();
|
||||
|
||||
}
|
||||
|
|
@ -18,7 +18,7 @@ package algorithms;
|
|||
|
||||
import basics.VehicleRoutingProblem;
|
||||
|
||||
interface InsertionStrategyFactory {
|
||||
public interface InsertionStrategyFactory {
|
||||
|
||||
public InsertionStrategy createStrategy(VehicleRoutingProblem vrp);
|
||||
|
||||
|
|
|
|||
|
|
@ -49,6 +49,194 @@ import basics.route.VehicleRoute;
|
|||
*/
|
||||
final class RuinRadial implements RuinStrategy {
|
||||
|
||||
static interface JobNeighborhoods {
|
||||
|
||||
public Iterator<Job> getNearestNeighborsIterator(int nNeighbors, Job neighborTo);
|
||||
|
||||
}
|
||||
|
||||
static class NeighborhoodIterator implements Iterator<Job>{
|
||||
|
||||
private static Logger log = Logger.getLogger(NeighborhoodIterator.class);
|
||||
|
||||
private Iterator<ReferencedJob> jobIter;
|
||||
|
||||
private int nJobs;
|
||||
|
||||
private int jobCount = 0;
|
||||
|
||||
public NeighborhoodIterator(Iterator<ReferencedJob> jobIter, int nJobs) {
|
||||
super();
|
||||
this.jobIter = jobIter;
|
||||
this.nJobs = nJobs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
if(jobCount < nJobs){
|
||||
boolean hasNext = jobIter.hasNext();
|
||||
if(!hasNext) log.warn("more jobs are requested then iterator can iterate over. probably the number of neighbors memorized in JobNeighborhoods is too small");
|
||||
return hasNext;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Job next() {
|
||||
ReferencedJob next = jobIter.next();
|
||||
jobCount++;
|
||||
return next.getJob();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
jobIter.remove();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class JobNeighborhoodsImpl implements JobNeighborhoods {
|
||||
|
||||
private static Logger logger = Logger.getLogger(JobNeighborhoodsImpl.class);
|
||||
|
||||
private VehicleRoutingProblem vrp;
|
||||
|
||||
private Map<String, TreeSet<ReferencedJob>> distanceNodeTree = new HashMap<String, TreeSet<ReferencedJob>>();
|
||||
|
||||
private JobDistance jobDistance;
|
||||
|
||||
public JobNeighborhoodsImpl(VehicleRoutingProblem vrp, JobDistance jobDistance) {
|
||||
super();
|
||||
this.vrp = vrp;
|
||||
this.jobDistance = jobDistance;
|
||||
logger.info("intialise " + this);
|
||||
}
|
||||
|
||||
public Iterator<Job> getNearestNeighborsIterator(int nNeighbors, Job neighborTo){
|
||||
TreeSet<ReferencedJob> tree = distanceNodeTree.get(neighborTo.getId());
|
||||
Iterator<ReferencedJob> descendingIterator = tree.iterator();
|
||||
return new NeighborhoodIterator(descendingIterator, nNeighbors);
|
||||
}
|
||||
|
||||
public void initialise(){
|
||||
logger.info("calculates and memorizes distances from EACH job to EACH job --> n^2 calculations");
|
||||
calculateDistancesFromJob2Job();
|
||||
}
|
||||
|
||||
private void calculateDistancesFromJob2Job() {
|
||||
logger.info("preprocess distances between locations ...");
|
||||
StopWatch stopWatch = new StopWatch();
|
||||
stopWatch.start();
|
||||
int nuOfDistancesStored = 0;
|
||||
for (Job i : vrp.getJobs().values()) {
|
||||
TreeSet<ReferencedJob> treeSet = new TreeSet<ReferencedJob>(
|
||||
new Comparator<ReferencedJob>() {
|
||||
@Override
|
||||
public int compare(ReferencedJob o1, ReferencedJob o2) {
|
||||
if (o1.getDistance() <= o2.getDistance()) {
|
||||
return -1;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
});
|
||||
distanceNodeTree.put(i.getId(), treeSet);
|
||||
for (Job j : vrp.getJobs().values()) {
|
||||
if(i==j) continue;
|
||||
double distance = jobDistance.getDistance(i, j);
|
||||
ReferencedJob refNode = new ReferencedJob(j, distance);
|
||||
treeSet.add(refNode);
|
||||
nuOfDistancesStored++;
|
||||
}
|
||||
|
||||
}
|
||||
stopWatch.stop();
|
||||
logger.info("preprocessing comp-time: " + stopWatch + "; nuOfDistances stored: " + nuOfDistancesStored + "; estimated memory: " +
|
||||
(distanceNodeTree.keySet().size()*64+nuOfDistancesStored*92) + " bytes");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class JobNeighborhoodsImplWithCapRestriction implements JobNeighborhoods {
|
||||
|
||||
private static Logger logger = Logger.getLogger(JobNeighborhoodsImpl.class);
|
||||
|
||||
private VehicleRoutingProblem vrp;
|
||||
|
||||
private Map<String, TreeSet<ReferencedJob>> distanceNodeTree = new HashMap<String, TreeSet<ReferencedJob>>();
|
||||
|
||||
private JobDistance jobDistance;
|
||||
|
||||
private int capacity;
|
||||
|
||||
public JobNeighborhoodsImplWithCapRestriction(VehicleRoutingProblem vrp, JobDistance jobDistance, int capacity) {
|
||||
super();
|
||||
this.vrp = vrp;
|
||||
this.jobDistance = jobDistance;
|
||||
this.capacity = capacity;
|
||||
logger.info("intialise " + this);
|
||||
}
|
||||
|
||||
public Iterator<Job> getNearestNeighborsIterator(int nNeighbors, Job neighborTo){
|
||||
TreeSet<ReferencedJob> tree = distanceNodeTree.get(neighborTo.getId());
|
||||
Iterator<ReferencedJob> descendingIterator = tree.iterator();
|
||||
return new NeighborhoodIterator(descendingIterator, nNeighbors);
|
||||
}
|
||||
|
||||
public void initialise(){
|
||||
logger.info("calculates distances from EACH job to EACH job --> n^2="+Math.pow(vrp.getJobs().values().size(), 2) + " calculations, but 'only' "+(vrp.getJobs().values().size()*capacity)+ " are cached.");
|
||||
calculateDistancesFromJob2Job();
|
||||
}
|
||||
|
||||
private void calculateDistancesFromJob2Job() {
|
||||
logger.info("preprocess distances between locations ...");
|
||||
StopWatch stopWatch = new StopWatch();
|
||||
stopWatch.start();
|
||||
int nuOfDistancesStored = 0;
|
||||
for (Job i : vrp.getJobs().values()) {
|
||||
TreeSet<ReferencedJob> treeSet = new TreeSet<ReferencedJob>(
|
||||
new Comparator<ReferencedJob>() {
|
||||
@Override
|
||||
public int compare(ReferencedJob o1, ReferencedJob o2) {
|
||||
if (o1.getDistance() <= o2.getDistance()) {
|
||||
return -1;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
});
|
||||
distanceNodeTree.put(i.getId(), treeSet);
|
||||
for (Job j : vrp.getJobs().values()) {
|
||||
if(i==j) continue;
|
||||
double distance = jobDistance.getDistance(i, j);
|
||||
ReferencedJob refNode = new ReferencedJob(j, distance);
|
||||
if(treeSet.size() < capacity){
|
||||
treeSet.add(refNode);
|
||||
nuOfDistancesStored++;
|
||||
}
|
||||
else{
|
||||
if(treeSet.last().getDistance() > distance){
|
||||
treeSet.pollLast();
|
||||
treeSet.add(refNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
assert treeSet.size() <= capacity : "treeSet.size() is bigger than specified capacity";
|
||||
|
||||
}
|
||||
stopWatch.stop();
|
||||
logger.info("preprocessing comp-time: " + stopWatch + "; nuOfDistances stored: " + nuOfDistancesStored + "; estimated memory: " +
|
||||
(distanceNodeTree.keySet().size()*64+nuOfDistancesStored*92) + " bytes");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "[name=neighborhoodWithCapRestriction][capacity="+capacity+"]";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
static class ReferencedJob {
|
||||
private Job job;
|
||||
private double distance;
|
||||
|
|
@ -74,14 +262,12 @@ final class RuinRadial implements RuinStrategy {
|
|||
|
||||
private double fractionOfAllNodes2beRuined;
|
||||
|
||||
private Map<String, TreeSet<ReferencedJob>> distanceNodeTree = new HashMap<String, TreeSet<ReferencedJob>>();
|
||||
|
||||
private Random random = RandomNumberGeneration.getRandom();
|
||||
|
||||
private JobDistance jobDistance;
|
||||
|
||||
private RuinListeners ruinListeners;
|
||||
|
||||
private JobNeighborhoods jobNeighborhoods;
|
||||
|
||||
public void setRandom(Random random) {
|
||||
this.random = random;
|
||||
}
|
||||
|
|
@ -96,41 +282,46 @@ final class RuinRadial implements RuinStrategy {
|
|||
public RuinRadial(VehicleRoutingProblem vrp, double fraction2beRemoved, JobDistance jobDistance) {
|
||||
super();
|
||||
this.vrp = vrp;
|
||||
this.jobDistance = jobDistance;
|
||||
this.fractionOfAllNodes2beRuined = fraction2beRemoved;
|
||||
ruinListeners = new RuinListeners();
|
||||
calculateDistancesFromJob2Job();
|
||||
int nJobsToMemorize = (int) Math.ceil(vrp.getJobs().values().size()*fraction2beRemoved);
|
||||
JobNeighborhoodsImplWithCapRestriction jobNeighborhoodsImpl = new JobNeighborhoodsImplWithCapRestriction(vrp, jobDistance, nJobsToMemorize);
|
||||
jobNeighborhoodsImpl.initialise();
|
||||
jobNeighborhoods = jobNeighborhoodsImpl;
|
||||
logger.info("intialise " + this);
|
||||
}
|
||||
|
||||
private void calculateDistancesFromJob2Job() {
|
||||
logger.info("preprocess distances between locations ...");
|
||||
StopWatch stopWatch = new StopWatch();
|
||||
stopWatch.start();
|
||||
int nuOfDistancesStored = 0;
|
||||
for (Job i : vrp.getJobs().values()) {
|
||||
TreeSet<ReferencedJob> treeSet = new TreeSet<ReferencedJob>(
|
||||
new Comparator<ReferencedJob>() {
|
||||
@Override
|
||||
public int compare(ReferencedJob o1, ReferencedJob o2) {
|
||||
if (o1.getDistance() <= o2.getDistance()) {
|
||||
return 1;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
});
|
||||
distanceNodeTree.put(i.getId(), treeSet);
|
||||
for (Job j : vrp.getJobs().values()) {
|
||||
double distance = jobDistance.getDistance(i, j);
|
||||
ReferencedJob refNode = new ReferencedJob(j, distance);
|
||||
treeSet.add(refNode);
|
||||
nuOfDistancesStored++;
|
||||
}
|
||||
}
|
||||
stopWatch.stop();
|
||||
logger.info("preprocessing comp-time: " + stopWatch + "; nuOfDistances stored: " + nuOfDistancesStored + "; estimated memory: " +
|
||||
(distanceNodeTree.keySet().size()*64+nuOfDistancesStored*92) + " bytes");
|
||||
//<<<<<<< HEAD
|
||||
// }
|
||||
//
|
||||
// private void calculateDistancesFromJob2Job() {
|
||||
// logger.info("preprocess distances between locations ...");
|
||||
// StopWatch stopWatch = new StopWatch();
|
||||
// stopWatch.start();
|
||||
// int nuOfDistancesStored = 0;
|
||||
// for (Job i : vrp.getJobs().values()) {
|
||||
// TreeSet<ReferencedJob> treeSet = new TreeSet<ReferencedJob>(
|
||||
// new Comparator<ReferencedJob>() {
|
||||
// @Override
|
||||
// public int compare(ReferencedJob o1, ReferencedJob o2) {
|
||||
// if (o1.getDistance() <= o2.getDistance()) {
|
||||
// return 1;
|
||||
// } else {
|
||||
// return -1;
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// distanceNodeTree.put(i.getId(), treeSet);
|
||||
// for (Job j : vrp.getJobs().values()) {
|
||||
// double distance = jobDistance.getDistance(i, j);
|
||||
// ReferencedJob refNode = new ReferencedJob(j, distance);
|
||||
// treeSet.add(refNode);
|
||||
// nuOfDistancesStored++;
|
||||
// }
|
||||
// }
|
||||
// stopWatch.stop();
|
||||
// logger.info("preprocessing comp-time: " + stopWatch + "; nuOfDistances stored: " + nuOfDistancesStored + "; estimated memory: " +
|
||||
// (distanceNodeTree.keySet().size()*64+nuOfDistancesStored*92) + " bytes");
|
||||
//=======
|
||||
//>>>>>>> refs/heads/master
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -145,11 +336,11 @@ final class RuinRadial implements RuinStrategy {
|
|||
@Override
|
||||
public Collection<Job> ruin(Collection<VehicleRoute> vehicleRoutes) {
|
||||
if(vehicleRoutes.isEmpty()){
|
||||
return Collections.EMPTY_LIST;
|
||||
return Collections.emptyList();
|
||||
}
|
||||
int nOfJobs2BeRemoved = getNuOfJobs2BeRemoved();
|
||||
if (nOfJobs2BeRemoved == 0) {
|
||||
return Collections.EMPTY_LIST;
|
||||
return Collections.emptyList();
|
||||
}
|
||||
Job randomJob = pickRandomJob();
|
||||
Collection<Job> unassignedJobs = ruin(vehicleRoutes,randomJob,nOfJobs2BeRemoved);
|
||||
|
|
@ -162,27 +353,30 @@ final class RuinRadial implements RuinStrategy {
|
|||
public Collection<Job> ruin(Collection<VehicleRoute> vehicleRoutes, Job targetJob, int nOfJobs2BeRemoved){
|
||||
ruinListeners.ruinStarts(vehicleRoutes);
|
||||
List<Job> unassignedJobs = new ArrayList<Job>();
|
||||
TreeSet<ReferencedJob> tree = distanceNodeTree.get(targetJob.getId());
|
||||
Iterator<ReferencedJob> descendingIterator = tree.descendingIterator();
|
||||
int counter = 0;
|
||||
while (descendingIterator.hasNext() && counter < nOfJobs2BeRemoved) {
|
||||
ReferencedJob refJob = descendingIterator.next();
|
||||
Job job = refJob.getJob();
|
||||
int nNeighbors = nOfJobs2BeRemoved - 1;
|
||||
removeJob(targetJob,vehicleRoutes);
|
||||
unassignedJobs.add(targetJob);
|
||||
Iterator<Job> neighborhoodIterator = jobNeighborhoods.getNearestNeighborsIterator(nNeighbors, targetJob);
|
||||
while(neighborhoodIterator.hasNext()){
|
||||
Job job = neighborhoodIterator.next();
|
||||
removeJob(job,vehicleRoutes);
|
||||
unassignedJobs.add(job);
|
||||
counter++;
|
||||
boolean removed = false;
|
||||
for (VehicleRoute route : vehicleRoutes) {
|
||||
removed = route.getTourActivities().removeJob(job);;
|
||||
if (removed) {
|
||||
ruinListeners.removed(job,route);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
ruinListeners.ruinEnds(vehicleRoutes, unassignedJobs);
|
||||
return unassignedJobs;
|
||||
}
|
||||
|
||||
private void removeJob(Job job, Collection<VehicleRoute> vehicleRoutes) {
|
||||
boolean removed = false;
|
||||
for (VehicleRoute route : vehicleRoutes) {
|
||||
removed = route.getTourActivities().removeJob(job);;
|
||||
if (removed) {
|
||||
ruinListeners.removed(job,route);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Job pickRandomJob() {
|
||||
int totNuOfJobs = vrp.getJobs().values().size();
|
||||
int randomIndex = random.nextInt(totNuOfJobs);
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ public class VehicleRoutingAlgorithmFactoryImpl implements VehicleRoutingAlgorit
|
|||
public VehicleRoutingAlgorithm createAlgorithm(VehicleRoutingProblem vrp) {
|
||||
this.stateManager.addActivityVisitor(new UpdateVariableCosts(vrp.getActivityCosts(), vrp.getTransportCosts(), this.stateManager));
|
||||
this.stateManager.addActivityVisitor(new UpdateMaxLoad(this.stateManager));
|
||||
this.stateManager.addActivityVisitor(new UpdateActivityTimes(vrp.getTransportCosts()));
|
||||
VehicleRoutingAlgorithm algorithm = new VehicleRoutingAlgorithm(vrp, searchStrategyManager);
|
||||
algorithm.getAlgorithmListeners().addListener(stateManager);
|
||||
algorithm.getSearchStrategyManager().addSearchStrategyModuleListener(stateManager);
|
||||
|
|
|
|||
|
|
@ -814,10 +814,9 @@ public class VehicleRoutingAlgorithms {
|
|||
insertion = createInsertionStrategy(insertionConfigs.get(0), vrp, vehicleFleetManager, routeStates, prioListeners, executorService, nuOfThreads, constraintManager);
|
||||
algorithmListeners.addAll(prioListeners);
|
||||
}
|
||||
Gendreau gendreau = new Gendreau(vrp, ruin, insertion);
|
||||
Gendreau gendreau = new Gendreau(vrp, ruin, insertion, vehicleFleetManager);
|
||||
gendreau.setShareOfJobsToRuin(share);
|
||||
gendreau.setNuOfIterations(iterations);
|
||||
gendreau.setFleetManager(vehicleFleetManager);
|
||||
definedClasses.put(strategyModuleKey, gendreau);
|
||||
return gendreau;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,6 +25,15 @@ import basics.VehicleRoutingProblem;
|
|||
import basics.VehicleRoutingProblemSolution;
|
||||
import basics.algo.SearchStrategy.DiscoveredSolution;
|
||||
|
||||
/**
|
||||
* Breaks algorithm prematurely based on specified time.
|
||||
*
|
||||
* <p>Note, TimeBreaker must be registered as AlgorithmListener <br>
|
||||
* <code>agorithm.getAlgorithmListeners().addListener(this);</code>
|
||||
*
|
||||
* @author stefan
|
||||
*
|
||||
*/
|
||||
public class TimeBreaker implements PrematureAlgorithmBreaker, AlgorithmStartsListener{
|
||||
|
||||
private static Logger logger = Logger.getLogger(TimeBreaker.class);
|
||||
|
|
@ -33,9 +42,18 @@ public class TimeBreaker implements PrematureAlgorithmBreaker, AlgorithmStartsLi
|
|||
|
||||
private double startTime;
|
||||
|
||||
public TimeBreaker(double time) {
|
||||
/**
|
||||
* Constructs TimeBreaker that breaks algorithm prematurely based on specified time.
|
||||
*
|
||||
* <p>Note, TimeBreaker must be registered as AlgorithmListener <br>
|
||||
* <code>agorithm.getAlgorithmListeners().addListener(this);</code>
|
||||
*
|
||||
* @author stefan
|
||||
*
|
||||
*/
|
||||
public TimeBreaker(double time_in_seconds) {
|
||||
super();
|
||||
this.timeThreshold = time;
|
||||
this.timeThreshold = time_in_seconds;
|
||||
logger.info("initialise " + this);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -28,6 +28,16 @@ import basics.VehicleRoutingProblem;
|
|||
import basics.VehicleRoutingProblemSolution;
|
||||
import basics.algo.SearchStrategy.DiscoveredSolution;
|
||||
|
||||
/**
|
||||
* Breaks algorithm prematurely based on variationCoefficient.
|
||||
*
|
||||
* <p>Note that this must be registered in algorithm<br>
|
||||
* <code>algorithm.getAlgorithmListeners().addListener(this);</code>
|
||||
*
|
||||
*
|
||||
* @author stefan
|
||||
*
|
||||
*/
|
||||
public class VariationCoefficientBreaker implements PrematureAlgorithmBreaker, IterationStartsListener, AlgorithmStartsListener, IterationEndsListener{
|
||||
|
||||
private static Logger logger = Logger.getLogger(VariationCoefficientBreaker.class);
|
||||
|
|
@ -42,6 +52,16 @@ public class VariationCoefficientBreaker implements PrematureAlgorithmBreaker, I
|
|||
|
||||
private VehicleRoutingProblemSolution lastAccepted = null;
|
||||
|
||||
/**
|
||||
* Breaks algorithm prematurely based on variationCoefficient.
|
||||
*
|
||||
* <p>Note that this must be registered in algorithm<br>
|
||||
* <code>algorithm.getAlgorithmListeners().addListener(this);</code>
|
||||
*
|
||||
*
|
||||
* @author stefan
|
||||
*
|
||||
*/
|
||||
public VariationCoefficientBreaker(int nuOfIterations, double variationCoefficientThreshold) {
|
||||
super();
|
||||
this.nuOfIterations = nuOfIterations;
|
||||
|
|
|
|||
|
|
@ -62,6 +62,5 @@ public interface VehicleRoutingActivityCosts {
|
|||
*/
|
||||
public double getActivityCost(TourActivity tourAct, double arrivalTime, Driver driver, Vehicle vehicle);
|
||||
|
||||
// public Parameter getParameter(TourActivity tourAct, Vehicle vehicle, Driver driver);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,6 +22,12 @@ import basics.VehicleRoutingProblemSolution;
|
|||
|
||||
public class Solutions {
|
||||
|
||||
/**
|
||||
*
|
||||
* @deprecated use bestOf instead.
|
||||
* @param solutions
|
||||
* @return
|
||||
*/
|
||||
@Deprecated
|
||||
public static VehicleRoutingProblemSolution getBest(Collection<VehicleRoutingProblemSolution> solutions){
|
||||
VehicleRoutingProblemSolution best = null;
|
||||
|
|
|
|||
|
|
@ -87,8 +87,7 @@ public class BuildPDVRPWithShipmentsAlgoFromScratchTest {
|
|||
int nuOfThreads = 10;
|
||||
executorService = Executors.newFixedThreadPool(nuOfThreads);
|
||||
|
||||
BestInsertionBuilder bestIBuilder = new BestInsertionBuilder(vrp, fleetManager, stateManager);
|
||||
bestIBuilder.setConstraintManager(constraintManager);
|
||||
BestInsertionBuilder bestIBuilder = new BestInsertionBuilder(vrp, fleetManager, stateManager,constraintManager);
|
||||
bestIBuilder.setConcurrentMode(executorService, nuOfThreads);
|
||||
InsertionStrategy bestInsertion = bestIBuilder.build();
|
||||
|
||||
|
|
@ -187,7 +186,7 @@ public class BuildPDVRPWithShipmentsAlgoFromScratchTest {
|
|||
// System.out.println("ini: costs="+iniSolution.getCost()+";#routes="+iniSolution.getRoutes().size());
|
||||
vra.addInitialSolution(iniSolution);
|
||||
|
||||
vra.setNuOfIterations(100);
|
||||
vra.setNuOfIterations(10);
|
||||
// vra.setPrematureBreak(500);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -201,8 +201,7 @@ public class GendreauPostOptTest {
|
|||
InsertionStrategy insertionStrategy = new BestInsertion(insertionCalc);
|
||||
insertionStrategy.addListener(stateUpdater);
|
||||
insertionStrategy.addListener(new VehicleSwitched(fleetManager));
|
||||
Gendreau postOpt = new Gendreau(vrp, radialRuin, insertionStrategy);
|
||||
postOpt.setFleetManager(fleetManager);
|
||||
Gendreau postOpt = new Gendreau(vrp, radialRuin, insertionStrategy, fleetManager);
|
||||
|
||||
VehicleRoutingProblemSolution newSolution = postOpt.runAndGetSolution(sol);
|
||||
newSolution.setCost(getCosts(newSolution,states));
|
||||
|
|
@ -261,10 +260,10 @@ public class GendreauPostOptTest {
|
|||
InsertionStrategy insertionStrategy = new BestInsertion(insertionCalc);
|
||||
insertionStrategy.addListener(stateUpdater);
|
||||
insertionStrategy.addListener(new VehicleSwitched(fleetManager));
|
||||
Gendreau postOpt = new Gendreau(vrp, radialRuin, insertionStrategy);
|
||||
Gendreau postOpt = new Gendreau(vrp, radialRuin, insertionStrategy, fleetManager);
|
||||
postOpt.setShareOfJobsToRuin(1.0);
|
||||
postOpt.setNuOfIterations(1);
|
||||
postOpt.setFleetManager(fleetManager);
|
||||
|
||||
// postOpt.setWithFix(withFixCost);
|
||||
VehicleRoutingProblemSolution newSolution = postOpt.runAndGetSolution(sol);
|
||||
newSolution.setCost(getCosts(newSolution,states));
|
||||
|
|
|
|||
|
|
@ -0,0 +1,110 @@
|
|||
package algorithms;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import util.Coordinate;
|
||||
import algorithms.RuinRadial.JobNeighborhoodsImpl;
|
||||
import basics.Job;
|
||||
import basics.Service;
|
||||
import basics.VehicleRoutingProblem;
|
||||
|
||||
public class JobNeighborhoodsImplTest {
|
||||
|
||||
VehicleRoutingProblem vrp;
|
||||
|
||||
JobDistance jobDistance;
|
||||
|
||||
Service target;
|
||||
Service s2;
|
||||
Service s3;
|
||||
Service s4;
|
||||
Service s5;
|
||||
Service s6;
|
||||
Service s7;
|
||||
|
||||
@Before
|
||||
public void doBefore(){
|
||||
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
|
||||
target = Service.Builder.newInstance("s1", 1).setCoord(Coordinate.newInstance(0, 5)).build();
|
||||
s2 = Service.Builder.newInstance("s2", 1).setCoord(Coordinate.newInstance(0, 4)).build();
|
||||
s3 = Service.Builder.newInstance("s3", 1).setCoord(Coordinate.newInstance(0, 3)).build();
|
||||
s4 = Service.Builder.newInstance("s4", 1).setCoord(Coordinate.newInstance(0, 2)).build();
|
||||
|
||||
s5 = Service.Builder.newInstance("s5", 1).setCoord(Coordinate.newInstance(0, 6)).build();
|
||||
s6 = Service.Builder.newInstance("s6", 1).setCoord(Coordinate.newInstance(0, 7)).build();
|
||||
s7 = Service.Builder.newInstance("s7", 1).setCoord(Coordinate.newInstance(0, 8)).build();
|
||||
|
||||
vrp = builder.addJob(target).addJob(s2).addJob(s3).addJob(s4).addJob(s5).addJob(s6).addJob(s7).build();
|
||||
|
||||
jobDistance = new EuclideanServiceDistance();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestingNeighborhoodOfTargetJob_nNeighborsShouldBeTwo(){
|
||||
JobNeighborhoodsImpl jn = new JobNeighborhoodsImpl(vrp, jobDistance);
|
||||
jn.initialise();
|
||||
Iterator<Job> iter = jn.getNearestNeighborsIterator(2, target);
|
||||
List<Service> services = new ArrayList<Service>();
|
||||
while(iter.hasNext()){
|
||||
services.add((Service) iter.next());
|
||||
}
|
||||
assertEquals(2,services.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestingNeighborhoodOfTargetJob_s2ShouldBeNeighbor(){
|
||||
JobNeighborhoodsImpl jn = new JobNeighborhoodsImpl(vrp, jobDistance);
|
||||
jn.initialise();
|
||||
Iterator<Job> iter = jn.getNearestNeighborsIterator(2, target);
|
||||
List<Service> services = new ArrayList<Service>();
|
||||
while(iter.hasNext()){
|
||||
services.add((Service) iter.next());
|
||||
}
|
||||
assertTrue(services.contains(s2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestingNeighborhoodOfTargetJob_s4ShouldBeNeighbor(){
|
||||
JobNeighborhoodsImpl jn = new JobNeighborhoodsImpl(vrp, jobDistance);
|
||||
jn.initialise();
|
||||
Iterator<Job> iter = jn.getNearestNeighborsIterator(2, target);
|
||||
List<Service> services = new ArrayList<Service>();
|
||||
while(iter.hasNext()){
|
||||
services.add((Service) iter.next());
|
||||
}
|
||||
assertTrue(services.contains(s5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestingNeighborhoodOfTargetJob_sizeShouldBe4(){
|
||||
JobNeighborhoodsImpl jn = new JobNeighborhoodsImpl(vrp, jobDistance);
|
||||
jn.initialise();
|
||||
Iterator<Job> iter = jn.getNearestNeighborsIterator(4, target);
|
||||
List<Service> services = new ArrayList<Service>();
|
||||
while(iter.hasNext()){
|
||||
services.add((Service) iter.next());
|
||||
}
|
||||
assertEquals(4,services.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestingMoreNeighborsThanExisting_itShouldReturnMaxNeighbors(){
|
||||
JobNeighborhoodsImpl jn = new JobNeighborhoodsImpl(vrp, jobDistance);
|
||||
jn.initialise();
|
||||
Iterator<Job> iter = jn.getNearestNeighborsIterator(100, target);
|
||||
List<Service> services = new ArrayList<Service>();
|
||||
while(iter.hasNext()){
|
||||
services.add((Service) iter.next());
|
||||
}
|
||||
assertEquals(6,services.size());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,111 @@
|
|||
package algorithms;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import util.Coordinate;
|
||||
import algorithms.RuinRadial.JobNeighborhoodsImpl;
|
||||
import algorithms.RuinRadial.JobNeighborhoodsImplWithCapRestriction;
|
||||
import basics.Job;
|
||||
import basics.Service;
|
||||
import basics.VehicleRoutingProblem;
|
||||
|
||||
public class JobNeighborhoodsWithCapRestrictionImplTest {
|
||||
|
||||
VehicleRoutingProblem vrp;
|
||||
|
||||
JobDistance jobDistance;
|
||||
|
||||
Service target;
|
||||
Service s2;
|
||||
Service s3;
|
||||
Service s4;
|
||||
Service s5;
|
||||
Service s6;
|
||||
Service s7;
|
||||
|
||||
@Before
|
||||
public void doBefore(){
|
||||
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
|
||||
target = Service.Builder.newInstance("s1", 1).setCoord(Coordinate.newInstance(0, 5)).build();
|
||||
s2 = Service.Builder.newInstance("s2", 1).setCoord(Coordinate.newInstance(0, 4)).build();
|
||||
s3 = Service.Builder.newInstance("s3", 1).setCoord(Coordinate.newInstance(0, 3)).build();
|
||||
s4 = Service.Builder.newInstance("s4", 1).setCoord(Coordinate.newInstance(0, 2)).build();
|
||||
|
||||
s5 = Service.Builder.newInstance("s5", 1).setCoord(Coordinate.newInstance(0, 6)).build();
|
||||
s6 = Service.Builder.newInstance("s6", 1).setCoord(Coordinate.newInstance(0, 7)).build();
|
||||
s7 = Service.Builder.newInstance("s7", 1).setCoord(Coordinate.newInstance(0, 8)).build();
|
||||
|
||||
vrp = builder.addJob(target).addJob(s2).addJob(s3).addJob(s4).addJob(s5).addJob(s6).addJob(s7).build();
|
||||
|
||||
jobDistance = new EuclideanServiceDistance();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestingNeighborhoodOfTargetJob_nNeighborsShouldBeTwo(){
|
||||
JobNeighborhoodsImplWithCapRestriction jn = new JobNeighborhoodsImplWithCapRestriction(vrp, jobDistance, 2);
|
||||
jn.initialise();
|
||||
Iterator<Job> iter = jn.getNearestNeighborsIterator(2, target);
|
||||
List<Service> services = new ArrayList<Service>();
|
||||
while(iter.hasNext()){
|
||||
services.add((Service) iter.next());
|
||||
}
|
||||
assertEquals(2,services.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestingNeighborhoodOfTargetJob_s2ShouldBeNeighbor(){
|
||||
JobNeighborhoodsImplWithCapRestriction jn = new JobNeighborhoodsImplWithCapRestriction(vrp, jobDistance, 2);
|
||||
jn.initialise();
|
||||
Iterator<Job> iter = jn.getNearestNeighborsIterator(2, target);
|
||||
List<Service> services = new ArrayList<Service>();
|
||||
while(iter.hasNext()){
|
||||
services.add((Service) iter.next());
|
||||
}
|
||||
assertTrue(services.contains(s2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestingNeighborhoodOfTargetJob_s4ShouldBeNeighbor(){
|
||||
JobNeighborhoodsImplWithCapRestriction jn = new JobNeighborhoodsImplWithCapRestriction(vrp, jobDistance, 2);
|
||||
jn.initialise();
|
||||
Iterator<Job> iter = jn.getNearestNeighborsIterator(2, target);
|
||||
List<Service> services = new ArrayList<Service>();
|
||||
while(iter.hasNext()){
|
||||
services.add((Service) iter.next());
|
||||
}
|
||||
assertTrue(services.contains(s5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestingNeighborhoodOfTargetJob_sizeShouldBe4(){
|
||||
JobNeighborhoodsImplWithCapRestriction jn = new JobNeighborhoodsImplWithCapRestriction(vrp, jobDistance, 4);
|
||||
jn.initialise();
|
||||
Iterator<Job> iter = jn.getNearestNeighborsIterator(4, target);
|
||||
List<Service> services = new ArrayList<Service>();
|
||||
while(iter.hasNext()){
|
||||
services.add((Service) iter.next());
|
||||
}
|
||||
assertEquals(4,services.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestingMoreNeighborsThanExisting_itShouldReturnMaxNeighbors(){
|
||||
JobNeighborhoodsImplWithCapRestriction jn = new JobNeighborhoodsImplWithCapRestriction(vrp, jobDistance, 2);
|
||||
jn.initialise();
|
||||
Iterator<Job> iter = jn.getNearestNeighborsIterator(100, target);
|
||||
List<Service> services = new ArrayList<Service>();
|
||||
while(iter.hasNext()){
|
||||
services.add((Service) iter.next());
|
||||
}
|
||||
assertEquals(2,services.size());
|
||||
}
|
||||
|
||||
}
|
||||
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue