mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
refactor ruinShare to make it variable
This commit is contained in:
parent
7450f476b6
commit
62e90c14ad
2 changed files with 1 additions and 212 deletions
|
|
@ -20,7 +20,6 @@ import jsprit.core.algorithm.ruin.distance.JobDistance;
|
||||||
import jsprit.core.problem.VehicleRoutingProblem;
|
import jsprit.core.problem.VehicleRoutingProblem;
|
||||||
import jsprit.core.problem.job.Job;
|
import jsprit.core.problem.job.Job;
|
||||||
import jsprit.core.problem.solution.route.VehicleRoute;
|
import jsprit.core.problem.solution.route.VehicleRoute;
|
||||||
import jsprit.core.util.StopWatch;
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
|
|
@ -38,220 +37,10 @@ import java.util.*;
|
||||||
*/
|
*/
|
||||||
public final class RuinRadial extends AbstractRuinStrategy {
|
public final class RuinRadial extends AbstractRuinStrategy {
|
||||||
|
|
||||||
static interface JobNeighborhoods {
|
|
||||||
|
|
||||||
public Iterator<Job> getNearestNeighborsIterator(int nNeighbors, Job neighborTo);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static class NeighborhoodIterator implements Iterator<Job>{
|
|
||||||
|
|
||||||
private static Logger log = LogManager.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 = LogManager.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 = LogManager.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.");
|
|
||||||
if(capacity==0) return;
|
|
||||||
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;
|
|
||||||
|
|
||||||
public ReferencedJob(Job job, double distance) {
|
|
||||||
super();
|
|
||||||
this.job = job;
|
|
||||||
this.distance = distance;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Job getJob() {
|
|
||||||
return job;
|
|
||||||
}
|
|
||||||
|
|
||||||
public double getDistance() {
|
|
||||||
return distance;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Logger logger = LogManager.getLogger(RuinRadial.class);
|
private Logger logger = LogManager.getLogger(RuinRadial.class);
|
||||||
|
|
||||||
private VehicleRoutingProblem vrp;
|
private VehicleRoutingProblem vrp;
|
||||||
|
|
||||||
private double fractionOfAllNodes2beRuined;
|
|
||||||
|
|
||||||
private JobNeighborhoods jobNeighborhoods;
|
private JobNeighborhoods jobNeighborhoods;
|
||||||
|
|
||||||
private final int noJobsToMemorize;
|
private final int noJobsToMemorize;
|
||||||
|
|
@ -266,7 +55,6 @@ public final class RuinRadial extends AbstractRuinStrategy {
|
||||||
public RuinRadial(VehicleRoutingProblem vrp, double fraction2beRemoved, JobDistance jobDistance) {
|
public RuinRadial(VehicleRoutingProblem vrp, double fraction2beRemoved, JobDistance jobDistance) {
|
||||||
super();
|
super();
|
||||||
this.vrp = vrp;
|
this.vrp = vrp;
|
||||||
this.fractionOfAllNodes2beRuined = fraction2beRemoved;
|
|
||||||
noJobsToMemorize = (int) Math.ceil(vrp.getJobs().values().size()*fraction2beRemoved);
|
noJobsToMemorize = (int) Math.ceil(vrp.getJobs().values().size()*fraction2beRemoved);
|
||||||
ruinShareFactory = new RuinShareFactory() {
|
ruinShareFactory = new RuinShareFactory() {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -85,6 +85,7 @@ public final class RuinRandom extends AbstractRuinStrategy {
|
||||||
throw new IllegalStateException("not supported");
|
throw new IllegalStateException("not supported");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
public void setRuinFraction(double fractionOfAllNodes2beRuined) {
|
public void setRuinFraction(double fractionOfAllNodes2beRuined) {
|
||||||
this.fractionOfAllNodes2beRuined = fractionOfAllNodes2beRuined;
|
this.fractionOfAllNodes2beRuined = fractionOfAllNodes2beRuined;
|
||||||
logger.info("fraction set " + this);
|
logger.info("fraction set " + this);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue