1
0
Fork 0
mirror of https://github.com/graphhopper/jsprit.git synced 2020-01-24 07:45:05 +01:00
This commit is contained in:
oblonski 2014-08-28 06:21:26 +02:00
parent 55ffe3970f
commit 725e95414f
3 changed files with 18 additions and 85 deletions

View file

@ -17,21 +17,13 @@
package jsprit.core.algorithm.acceptor;
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.algorithm.listener.IterationStartsListener;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.util.Resource;
import jsprit.core.util.Solutions;
import org.apache.commons.math.stat.descriptive.moment.StandardDeviation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.net.URL;
import java.util.Collection;
@ -81,35 +73,18 @@ public class SchrimpfAcceptance implements SolutionAcceptor, IterationStartsList
private final double alpha;
private int nOfTotalIterations = 1000;
private int maxIterations = 1000;
private int currentIteration = 0;
private double initialThreshold = 0.0;
private int nOfRandomWalks;
private final int solutionMemory;
private boolean determineInitialThreshold = true;
public SchrimpfAcceptance(int solutionMemory, double alpha){
this.alpha = alpha;
this.solutionMemory = solutionMemory;
determineInitialThreshold = false;
logger.info("initialise " + this);
}
/**
* @deprecated use <code>new SchrimpfAcceptance(solutionMemory,alpha)</code> instead. if you want to determine ini-threshold with a
* random walk and the algorithm 'randomWalk.xml' use SchrimpfInitialThresholdGenerator.class instead.
*/
@Deprecated
public SchrimpfAcceptance(int solutionMemory, double alpha, int nOfWarmupIterations) {
super();
this.alpha = alpha;
this.nOfRandomWalks = nOfWarmupIterations;
this.solutionMemory = solutionMemory;
logger.info("initialise " + this);
}
@ -141,11 +116,11 @@ public class SchrimpfAcceptance implements SolutionAcceptor, IterationStartsList
@Override
public String toString() {
return "[name=SchrimpfAcceptance][alpha="+alpha+"][warmup=" + nOfRandomWalks + "]";
return "[name=SchrimpfAcceptance][alpha="+alpha+"]";
}
private double getThreshold(int iteration) {
double scheduleVariable = (double) iteration / (double) nOfTotalIterations;
double scheduleVariable = (double) iteration / (double) maxIterations;
return initialThreshold * Math.exp(-1. * Math.log(2) * scheduleVariable / alpha);
}
@ -163,53 +138,12 @@ public class SchrimpfAcceptance implements SolutionAcceptor, IterationStartsList
*/
public void setInitialThreshold(double initialThreshold) {
this.initialThreshold = initialThreshold;
determineInitialThreshold=false;
}
@Override
public void informAlgorithmStarts(VehicleRoutingProblem problem, VehicleRoutingAlgorithm algorithm, Collection<VehicleRoutingProblemSolution> solutions) {
if(!determineInitialThreshold){
logger.info("skip threshold initialization from here");
return;
}
reset();
logger.info("---------------------------------------------------------------------");
logger.info("prepare schrimpfAcceptanceFunction, i.e. determine initial threshold");
logger.info("start random-walk (see randomWalk.xml)");
double now = System.currentTimeMillis();
this.nOfTotalIterations = algorithm.getMaxIterations();
/*
* 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.setMaxIterations(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);
initialThreshold = standardDeviation / 2;
logger.info("warmup done");
logger.info("total time: " + ((System.currentTimeMillis()-now)/1000.0) + "s");
logger.info("initial threshold: " + initialThreshold);
logger.info("---------------------------------------------------------------------");
reset();
this.maxIterations = algorithm.getMaxIterations();
}
private void reset() {

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2014 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
@ -8,13 +8,11 @@
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Contributors:
* Stefan Schroeder - initial API and implementation
******************************************************************************/
package jsprit.core.algorithm.acceptor;
@ -44,7 +42,7 @@ public class SchrimpfAcceptanceTest {
@SuppressWarnings("deprecation")
@Before
public void setup() {
schrimpfAcceptance = new SchrimpfAcceptance(1, 0.3, 100);
schrimpfAcceptance = new SchrimpfAcceptance(1, 0.3);
// we skip the warmup, but still want to test that the initialThreshold is set
schrimpfAcceptance.setInitialThreshold(0.0);
// create empty memory with an initial capacity of 1

View file

@ -62,7 +62,8 @@ public class SolomonR101Example {
* The algorithm can be defined and configured in an xml-file.
*/
// VehicleRoutingAlgorithm vra = new SchrimpfFactory().createAlgorithm(vrp);
VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, "input/algorithmConfig_solomon.xml");
VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, "input/algorithmConfig.xml");
vra.setMaxIterations(100);
// vra.setPrematureBreak(100);
// vra.getAlgorithmListeners().addListener(new AlgorithmSearchProgressChartListener("output/sol_progress.png"));
/*