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

test and modify ruin strategies to deal with initial routes

This commit is contained in:
oblonski 2015-03-10 10:45:59 +01:00
parent 62e12d5153
commit 2b87155879
10 changed files with 192 additions and 29 deletions

View file

@ -20,6 +20,7 @@ package jsprit.core.algorithm.ruin;
import jsprit.core.algorithm.ruin.listener.RuinListener;
import jsprit.core.algorithm.ruin.listener.RuinListeners;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.util.RandomNumberGeneration;
@ -33,14 +34,12 @@ public abstract class AbstractRuinStrategy implements RuinStrategy{
protected Random random = RandomNumberGeneration.getRandom();
protected VehicleRoutingProblem vrp;
public void setRandom(Random random) {
this.random = random;
}
protected AbstractRuinStrategy() {
ruinListeners = new RuinListeners();
}
protected RuinShareFactory ruinShareFactory;
public void setRuinShareFactory(RuinShareFactory ruinShareFactory){
@ -51,6 +50,11 @@ public abstract class AbstractRuinStrategy implements RuinStrategy{
return ruinShareFactory;
}
protected AbstractRuinStrategy(VehicleRoutingProblem vrp) {
this.vrp = vrp;
ruinListeners = new RuinListeners();
}
@Override
public Collection<Job> ruin(Collection<VehicleRoute> vehicleRoutes){
ruinListeners.ruinStarts(vehicleRoutes);
@ -90,6 +94,7 @@ public abstract class AbstractRuinStrategy implements RuinStrategy{
}
protected boolean removeJob(Job job, Collection<VehicleRoute> vehicleRoutes) {
if(jobIsInitial(job)) return false;
for (VehicleRoute route : vehicleRoutes) {
if (removeJob(job, route)) {
return true;
@ -98,7 +103,12 @@ public abstract class AbstractRuinStrategy implements RuinStrategy{
return false;
}
private boolean jobIsInitial(Job job){
return !vrp.getJobs().containsKey(job.getId()); //for initial jobs (being not contained in problem
}
protected boolean removeJob(Job job, VehicleRoute route) {
if(jobIsInitial(job)) return false;
boolean removed = route.getTourActivities().removeJob(job);
if (removed) {
ruinListeners.removed(job,route);

View file

@ -79,7 +79,7 @@ public final class RuinClusters extends AbstractRuinStrategy implements Iteratio
private double epsFactor = 0.8;
public RuinClusters(VehicleRoutingProblem vrp, final int initialNumberJobsToRemove, JobNeighborhoods jobNeighborhoods) {
super();
super(vrp);
this.vrp = vrp;
setRuinShareFactory(new RuinShareFactory() {
@Override
@ -156,9 +156,10 @@ public final class RuinClusters extends AbstractRuinStrategy implements Iteratio
List<Job> cluster = dbscan.getRandomCluster(targetRoute);
for(Job j : cluster){
if(toRemove == 0) break;
removeJob(j, vehicleRoutes);
lastRemoved.add(j);
unassignedJobs.add(j);
if(removeJob(j, vehicleRoutes)) {
lastRemoved.add(j);
unassignedJobs.add(j);
}
toRemove--;
}
ruined.add(targetRoute);

View file

@ -54,7 +54,7 @@ public final class RuinRadial extends AbstractRuinStrategy {
* @param jobDistance i.e. a measure to define the distance between two jobs and whether they are located close or distant to eachother
*/
public RuinRadial(VehicleRoutingProblem vrp, double fraction2beRemoved, JobDistance jobDistance) {
super();
super(vrp);
this.vrp = vrp;
noJobsToMemorize = (int) Math.ceil(vrp.getJobs().values().size()*fraction2beRemoved);
ruinShareFactory = new RuinShareFactory() {
@ -72,7 +72,7 @@ public final class RuinRadial extends AbstractRuinStrategy {
}
public RuinRadial(VehicleRoutingProblem vrp, int noJobs2beRemoved, JobDistance jobDistance) {
super();
super(vrp);
this.vrp = vrp;
// this.fractionOfAllNodes2beRuined = fraction2beRemoved;
noJobsToMemorize = noJobs2beRemoved;
@ -91,7 +91,7 @@ public final class RuinRadial extends AbstractRuinStrategy {
}
public RuinRadial(VehicleRoutingProblem vrp, int noJobs2beRemoved, JobNeighborhoods neighborhoods) {
super();
super(vrp);
this.vrp = vrp;
noJobsToMemorize = noJobs2beRemoved;
ruinShareFactory = new RuinShareFactory() {
@ -141,8 +141,9 @@ public final class RuinRadial extends AbstractRuinStrategy {
Iterator<Job> neighborhoodIterator = jobNeighborhoods.getNearestNeighborsIterator(nNeighbors, targetJob);
while(neighborhoodIterator.hasNext()){
Job job = neighborhoodIterator.next();
removeJob(job,vehicleRoutes);
unassignedJobs.add(job);
if(removeJob(job,vehicleRoutes)){
unassignedJobs.add(job);
}
}
return unassignedJobs;
}

View file

@ -48,7 +48,7 @@ public final class RuinRadialMultipleCenters extends AbstractRuinStrategy {
private int noCenters = 1;
public RuinRadialMultipleCenters(VehicleRoutingProblem vrp, int neighborhoodSize, JobDistance jobDistance) {
super();
super(vrp);
this.vrp = vrp;
noJobsToMemorize = neighborhoodSize;
ruinShareFactory = new RuinShareFactory() {
@ -116,8 +116,9 @@ public final class RuinRadialMultipleCenters extends AbstractRuinStrategy {
while(neighborhoodIterator.hasNext()){
Job job = neighborhoodIterator.next();
if(available!=null) available.remove(job);
removeJob(job,vehicleRoutes);
unassignedJobs.add(job);
if(removeJob(job,vehicleRoutes)) {
unassignedJobs.add(job);
}
}
return unassignedJobs;
}

View file

@ -51,7 +51,7 @@ public final class RuinRandom extends AbstractRuinStrategy {
* @param fraction which is the fraction of total c
*/
public RuinRandom(VehicleRoutingProblem vrp, double fraction) {
super();
super(vrp);
this.vrp = vrp;
this.fractionOfAllNodes2beRuined = fraction;
setRuinShareFactory(new RuinShareFactory() {
@ -96,9 +96,10 @@ public final class RuinRandom extends AbstractRuinStrategy {
for (int i = 0; i < nOfJobs2BeRemoved; i++) {
if(availableJobs.isEmpty()) break;
Job job = pickRandomJob(availableJobs);
unassignedJobs.add(job);
availableJobs.remove(job);
removeJob(job,vehicleRoutes);
if(removeJob(job,vehicleRoutes)) {
unassignedJobs.add(job);
availableJobs.remove(job);
}
}
}

View file

@ -56,7 +56,7 @@ public final class RuinWorst extends AbstractRuinStrategy {
}
public RuinWorst(VehicleRoutingProblem vrp, final int initialNumberJobsToRemove) {
super();
super(vrp);
this.vrp = vrp;
setRuinShareFactory(new RuinShareFactory() {
@Override
@ -95,9 +95,10 @@ public final class RuinWorst extends AbstractRuinStrategy {
while(toRemove > 0){
Job worst = getWorst(vehicleRoutes);
if(worst == null) break;
removeJob(worst,vehicleRoutes);
availableJobs.remove(worst);
unassignedJobs.add(worst);
if(removeJob(worst,vehicleRoutes)) {
availableJobs.remove(worst);
unassignedJobs.add(worst);
}
toRemove--;
}
}