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

refactor ruin share

This commit is contained in:
oblonski 2015-01-06 18:29:19 +01:00
parent 44d0efdd54
commit fd62578890
4 changed files with 60 additions and 29 deletions

View file

@ -41,6 +41,16 @@ public abstract class AbstractRuinStrategy implements RuinStrategy{
ruinListeners = new RuinListeners(); ruinListeners = new RuinListeners();
} }
protected RuinShareFactory ruinShareFactory;
public void setRuinShareFactory(RuinShareFactory ruinShareFactory){
this.ruinShareFactory = ruinShareFactory;
}
public RuinShareFactory getRuinShareFactory(){
return ruinShareFactory;
}
@Override @Override
public Collection<Job> ruin(Collection<VehicleRoute> vehicleRoutes){ public Collection<Job> ruin(Collection<VehicleRoute> vehicleRoutes){
ruinListeners.ruinStarts(vehicleRoutes); ruinListeners.ruinStarts(vehicleRoutes);

View file

@ -36,7 +36,7 @@ import java.util.*;
* @author stefan * @author stefan
* *
*/ */
final class RuinRadial extends AbstractRuinStrategy { public final class RuinRadial extends AbstractRuinStrategy {
static interface JobNeighborhoods { static interface JobNeighborhoods {
@ -254,6 +254,7 @@ final class RuinRadial extends AbstractRuinStrategy {
private JobNeighborhoods jobNeighborhoods; private JobNeighborhoods jobNeighborhoods;
private final int noJobsToMemorize;
/** /**
* Constructs RuinRadial. * Constructs RuinRadial.
@ -266,16 +267,43 @@ final class RuinRadial extends AbstractRuinStrategy {
super(); super();
this.vrp = vrp; this.vrp = vrp;
this.fractionOfAllNodes2beRuined = fraction2beRemoved; this.fractionOfAllNodes2beRuined = fraction2beRemoved;
int nJobsToMemorize = (int) Math.ceil(vrp.getJobs().values().size()*fraction2beRemoved); noJobsToMemorize = (int) Math.ceil(vrp.getJobs().values().size()*fraction2beRemoved);
JobNeighborhoodsImplWithCapRestriction jobNeighborhoodsImpl = new JobNeighborhoodsImplWithCapRestriction(vrp, jobDistance, nJobsToMemorize); ruinShareFactory = new RuinShareFactory() {
@Override
public int createNumberToBeRemoved() {
return noJobsToMemorize;
}
};
JobNeighborhoodsImplWithCapRestriction jobNeighborhoodsImpl = new JobNeighborhoodsImplWithCapRestriction(vrp, jobDistance, noJobsToMemorize);
jobNeighborhoodsImpl.initialise(); jobNeighborhoodsImpl.initialise();
jobNeighborhoods = jobNeighborhoodsImpl; jobNeighborhoods = jobNeighborhoodsImpl;
logger.info("intialise " + this); logger.info("initialise " + this);
}
public RuinRadial(VehicleRoutingProblem vrp, int noJobs2beRemoved, JobDistance jobDistance) {
super();
this.vrp = vrp;
// this.fractionOfAllNodes2beRuined = fraction2beRemoved;
noJobsToMemorize = noJobs2beRemoved;
ruinShareFactory = new RuinShareFactory() {
@Override
public int createNumberToBeRemoved() {
return noJobsToMemorize;
}
};
JobNeighborhoodsImplWithCapRestriction jobNeighborhoodsImpl = new JobNeighborhoodsImplWithCapRestriction(vrp, jobDistance, noJobsToMemorize);
jobNeighborhoodsImpl.initialise();
jobNeighborhoods = jobNeighborhoodsImpl;
logger.info("initialise " + this);
} }
@Override @Override
public String toString() { public String toString() {
return "[name=radialRuin][fraction="+fractionOfAllNodes2beRuined+"]"; return "[name=radialRuin][noJobsToBeRemoved="+noJobsToMemorize+"]";
} }
/** /**
@ -287,7 +315,7 @@ final class RuinRadial extends AbstractRuinStrategy {
if(vehicleRoutes.isEmpty()){ if(vehicleRoutes.isEmpty()){
return Collections.emptyList(); return Collections.emptyList();
} }
int nOfJobs2BeRemoved = getNuOfJobs2BeRemoved(); int nOfJobs2BeRemoved = Math.min(ruinShareFactory.createNumberToBeRemoved(), noJobsToMemorize);
if (nOfJobs2BeRemoved == 0) { if (nOfJobs2BeRemoved == 0) {
return Collections.emptyList(); return Collections.emptyList();
} }
@ -320,10 +348,4 @@ final class RuinRadial extends AbstractRuinStrategy {
return new ArrayList<Job>(vrp.getJobs().values()).get(randomIndex); return new ArrayList<Job>(vrp.getJobs().values()).get(randomIndex);
} }
private int getNuOfJobs2BeRemoved() {
return (int) Math.ceil(vrp.getJobs().values().size()
* fractionOfAllNodes2beRuined);
}
} }

View file

@ -36,7 +36,7 @@ import java.util.List;
* *
*/ */
final class RuinRandom extends AbstractRuinStrategy { public final class RuinRandom extends AbstractRuinStrategy {
private Logger logger = LogManager.getLogger(RuinRandom.class); private Logger logger = LogManager.getLogger(RuinRandom.class);
@ -54,6 +54,12 @@ final class RuinRandom extends AbstractRuinStrategy {
super(); super();
this.vrp = vrp; this.vrp = vrp;
this.fractionOfAllNodes2beRuined = fraction; this.fractionOfAllNodes2beRuined = fraction;
setRuinShareFactory(new RuinShareFactory() {
@Override
public int createNumberToBeRemoved() {
return selectNuOfJobs2BeRemoved();
}
});
logger.info("initialise " + this); logger.info("initialise " + this);
logger.info("done"); logger.info("done");
} }
@ -66,7 +72,7 @@ final class RuinRandom extends AbstractRuinStrategy {
@Override @Override
public Collection<Job> ruinRoutes(Collection<VehicleRoute> vehicleRoutes) { public Collection<Job> ruinRoutes(Collection<VehicleRoute> vehicleRoutes) {
List<Job> unassignedJobs = new ArrayList<Job>(); List<Job> unassignedJobs = new ArrayList<Job>();
int nOfJobs2BeRemoved = selectNuOfJobs2BeRemoved(); int nOfJobs2BeRemoved = getRuinShareFactory().createNumberToBeRemoved();
ruin(vehicleRoutes, nOfJobs2BeRemoved, unassignedJobs); ruin(vehicleRoutes, nOfJobs2BeRemoved, unassignedJobs);
return unassignedJobs; return unassignedJobs;
} }
@ -77,20 +83,6 @@ final class RuinRandom extends AbstractRuinStrategy {
@Override @Override
public Collection<Job> ruinRoutes(Collection<VehicleRoute> vehicleRoutes, Job targetJob, int nOfJobs2BeRemoved) { public Collection<Job> ruinRoutes(Collection<VehicleRoute> vehicleRoutes, Job targetJob, int nOfJobs2BeRemoved) {
throw new IllegalStateException("not supported"); throw new IllegalStateException("not supported");
// List<Job> unassignedJobs = new ArrayList<Job>();
// if(targetJob != null){
// boolean removed = false;
// for (VehicleRoute route : vehicleRoutes) {
// removed = removeJob(targetJob,route);
// if (removed) {
// nOfJobs2BeRemoved--;
// unassignedJobs.add(targetJob);
// break;
// }
// }
// }
// ruin(vehicleRoutes, nOfJobs2BeRemoved, unassignedJobs);
// return unassignedJobs;
} }
public void setRuinFraction(double fractionOfAllNodes2beRuined) { public void setRuinFraction(double fractionOfAllNodes2beRuined) {
@ -111,7 +103,7 @@ final class RuinRandom extends AbstractRuinStrategy {
@Override @Override
public String toString() { public String toString() {
return "[name=randomRuin][fraction="+fractionOfAllNodes2beRuined+"]"; return "[name=randomRuin][noJobsToBeRemoved="+selectNuOfJobs2BeRemoved()+"]";
} }
private Job pickRandomJob(LinkedList<Job> availableJobs) { private Job pickRandomJob(LinkedList<Job> availableJobs) {

View file

@ -0,0 +1,7 @@
package jsprit.core.algorithm.ruin;
public interface RuinShareFactory {
public int createNumberToBeRemoved();
}