mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
outsourced automatic threshold initialization from
algorithm.acceptor.SchrimpfAcceptance to algorithm.acceptor.SchrimpfInitialThresholdGenerator
This commit is contained in:
parent
deda983cc0
commit
057f128697
2 changed files with 70 additions and 3 deletions
|
|
@ -1,9 +1,75 @@
|
||||||
package jsprit.core.algorithm.acceptor;
|
package jsprit.core.algorithm.acceptor;
|
||||||
|
|
||||||
class SchrimpfInitialThresholdGenerator {
|
import java.net.URL;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import org.apache.commons.math.stat.descriptive.moment.StandardDeviation;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
|
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
|
||||||
|
import jsprit.core.algorithm.io.AlgorithmConfig;
|
||||||
|
import jsprit.core.algorithm.io.AlgorithmConfigXmlReader;
|
||||||
|
import jsprit.core.algorithm.io.VehicleRoutingAlgorithms;
|
||||||
|
import jsprit.core.algorithm.listener.AlgorithmStartsListener;
|
||||||
|
import jsprit.core.algorithm.listener.IterationEndsListener;
|
||||||
|
import jsprit.core.problem.VehicleRoutingProblem;
|
||||||
|
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
|
||||||
|
import jsprit.core.util.Resource;
|
||||||
|
import jsprit.core.util.Solutions;
|
||||||
|
|
||||||
|
public class SchrimpfInitialThresholdGenerator implements AlgorithmStartsListener {
|
||||||
|
|
||||||
public double getInitialThreshold(){
|
private static Logger logger = Logger.getLogger(SchrimpfInitialThresholdGenerator.class);
|
||||||
return 0.0;
|
|
||||||
|
private SchrimpfAcceptance schrimpfAcceptance;
|
||||||
|
|
||||||
|
private int nOfRandomWalks;
|
||||||
|
|
||||||
|
public SchrimpfInitialThresholdGenerator(SchrimpfAcceptance schrimpfAcceptance, int nOfRandomWalks) {
|
||||||
|
super();
|
||||||
|
this.schrimpfAcceptance = schrimpfAcceptance;
|
||||||
|
this.nOfRandomWalks = nOfRandomWalks;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void informAlgorithmStarts(VehicleRoutingProblem problem,VehicleRoutingAlgorithm algorithm,Collection<VehicleRoutingProblemSolution> solutions) {
|
||||||
|
logger.info("---------------------------------------------------------------------");
|
||||||
|
logger.info("prepare schrimpfAcceptanceFunction, i.e. determine initial threshold");
|
||||||
|
logger.info("start random-walk (see randomWalk.xml)");
|
||||||
|
double now = System.currentTimeMillis();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* randomWalk to determine standardDev
|
||||||
|
*/
|
||||||
|
final double[] results = new double[nOfRandomWalks];
|
||||||
|
|
||||||
|
URL resource = Resource.getAsURL("randomWalk.xml");
|
||||||
|
AlgorithmConfig algorithmConfig = new AlgorithmConfig();
|
||||||
|
new AlgorithmConfigXmlReader(algorithmConfig).read(resource);
|
||||||
|
VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.createAlgorithm(problem, algorithmConfig);
|
||||||
|
vra.setNuOfIterations(nOfRandomWalks);
|
||||||
|
vra.getAlgorithmListeners().addListener(new IterationEndsListener() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void informIterationEnds(int iteration, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
|
||||||
|
double result = Solutions.bestOf(solutions).getCost();
|
||||||
|
// logger.info("result="+result);
|
||||||
|
results[iteration-1] = result;
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
vra.searchSolutions();
|
||||||
|
|
||||||
|
StandardDeviation dev = new StandardDeviation();
|
||||||
|
double standardDeviation = dev.evaluate(results);
|
||||||
|
double initialThreshold = standardDeviation / 2;
|
||||||
|
|
||||||
|
schrimpfAcceptance.setInitialThreshold(initialThreshold);
|
||||||
|
|
||||||
|
logger.info("warmup done");
|
||||||
|
logger.info("total time: " + ((System.currentTimeMillis()-now)/1000.0) + "s");
|
||||||
|
logger.info("initial threshold: " + initialThreshold);
|
||||||
|
logger.info("---------------------------------------------------------------------");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ public class SchrimpfAcceptanceTest {
|
||||||
return when(mock(VehicleRoutingProblemSolution.class).getCost()).thenReturn(cost).getMock();
|
return when(mock(VehicleRoutingProblemSolution.class).getCost()).thenReturn(cost).getMock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
schrimpfAcceptance = new SchrimpfAcceptance(1, 0.3, 100);
|
schrimpfAcceptance = new SchrimpfAcceptance(1, 0.3, 100);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue