mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
parent
86cd1bb0d7
commit
7be8a408f1
1 changed files with 45 additions and 31 deletions
|
|
@ -1,5 +1,5 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (C) 2013 Stefan Schroeder
|
||||
* Copyright (C) 2014 Stefan Schroeder
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
|
|
@ -23,6 +23,7 @@ import jsprit.core.problem.VehicleRoutingProblem;
|
|||
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
|
||||
import jsprit.core.problem.solution.route.VehicleRoute;
|
||||
import jsprit.core.problem.solution.route.activity.TourActivity;
|
||||
import jsprit.core.util.Solutions;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
|
|
@ -52,12 +53,10 @@ public class VehicleRoutingAlgorithm {
|
|||
long i = counter++;
|
||||
long n = nextCounter;
|
||||
if (i >= n) {
|
||||
if (nextCounter==n) {
|
||||
nextCounter=n*2;
|
||||
log.info(this.name + n);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
counter=0;
|
||||
|
|
@ -65,20 +64,20 @@ public class VehicleRoutingAlgorithm {
|
|||
}
|
||||
}
|
||||
|
||||
private static Logger logger = LogManager.getLogger(VehicleRoutingAlgorithm.class);
|
||||
private final static Logger logger = LogManager.getLogger(VehicleRoutingAlgorithm.class);
|
||||
|
||||
private VehicleRoutingProblem problem;
|
||||
private final Counter counter = new Counter("iterations ");
|
||||
|
||||
private final VehicleRoutingProblem problem;
|
||||
|
||||
private final SearchStrategyManager searchStrategyManager;
|
||||
|
||||
private final VehicleRoutingAlgorithmListeners algoListeners = new VehicleRoutingAlgorithmListeners();
|
||||
|
||||
private final Collection<VehicleRoutingProblemSolution> initialSolutions;
|
||||
|
||||
private int maxIterations = 100;
|
||||
|
||||
private Counter counter = new Counter("iterations ");
|
||||
|
||||
private SearchStrategyManager searchStrategyManager;
|
||||
|
||||
private VehicleRoutingAlgorithmListeners algoListeners = new VehicleRoutingAlgorithmListeners();
|
||||
|
||||
private Collection<VehicleRoutingProblemSolution> initialSolutions;
|
||||
|
||||
private PrematureAlgorithmTermination prematureAlgorithmTermination = new PrematureAlgorithmTermination() {
|
||||
|
||||
@Override
|
||||
|
|
@ -88,6 +87,8 @@ public class VehicleRoutingAlgorithm {
|
|||
|
||||
};
|
||||
|
||||
private VehicleRoutingProblemSolution bestEver = null;
|
||||
|
||||
public VehicleRoutingAlgorithm(VehicleRoutingProblem problem, SearchStrategyManager searchStrategyManager) {
|
||||
super();
|
||||
this.problem = problem;
|
||||
|
|
@ -168,25 +169,28 @@ public class VehicleRoutingAlgorithm {
|
|||
logger.info("algorithm starts");
|
||||
double now = System.currentTimeMillis();
|
||||
verify();
|
||||
int nuOfIterationsThisAlgoIsRunning = maxIterations;
|
||||
int noIterationsThisAlgoIsRunning = maxIterations;
|
||||
counter.reset();
|
||||
Collection<VehicleRoutingProblemSolution> solutions = new ArrayList<VehicleRoutingProblemSolution>(initialSolutions);
|
||||
algorithmStarts(problem,solutions);
|
||||
bestEver = Solutions.bestOf(solutions);
|
||||
logger.info("iterations start");
|
||||
for(int i=0;i< maxIterations;i++){
|
||||
iterationStarts(i+1,problem,solutions);
|
||||
counter.incCounter();
|
||||
SearchStrategy strategy = searchStrategyManager.getRandomStrategy();
|
||||
DiscoveredSolution discoveredSolution = strategy.run(problem, solutions);
|
||||
memorizeIfBestEver(discoveredSolution);
|
||||
selectedStrategy(strategy.getName(),problem, solutions);
|
||||
if(prematureAlgorithmTermination.isPrematureBreak(discoveredSolution)){
|
||||
logger.info("premature break at iteration "+ (i+1));
|
||||
nuOfIterationsThisAlgoIsRunning = (i+1);
|
||||
noIterationsThisAlgoIsRunning = (i+1);
|
||||
break;
|
||||
}
|
||||
iterationEnds(i+1,problem,solutions);
|
||||
}
|
||||
logger.info("iterations end at " + nuOfIterationsThisAlgoIsRunning + " iterations");
|
||||
logger.info("iterations end at " + noIterationsThisAlgoIsRunning + " iterations");
|
||||
addBestEver(solutions);
|
||||
algorithmEnds(problem, solutions);
|
||||
logger.info("total time: " + ((System.currentTimeMillis()-now)/1000.0) + "s");
|
||||
logger.info("done");
|
||||
|
|
@ -194,6 +198,16 @@ public class VehicleRoutingAlgorithm {
|
|||
return solutions;
|
||||
}
|
||||
|
||||
private void addBestEver(Collection<VehicleRoutingProblemSolution> solutions) {
|
||||
if(bestEver != null) solutions.add(bestEver);
|
||||
}
|
||||
|
||||
private void memorizeIfBestEver(DiscoveredSolution discoveredSolution) {
|
||||
if(discoveredSolution == null) return;
|
||||
if(bestEver == null) bestEver = discoveredSolution.getSolution();
|
||||
else if(discoveredSolution.getSolution().getCost() < bestEver.getCost()) bestEver = discoveredSolution.getSolution();
|
||||
}
|
||||
|
||||
|
||||
private void selectedStrategy(String name, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
|
||||
algoListeners.selectedStrategy(name,problem, solutions);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue