mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
bugfix #128
This commit is contained in:
parent
55ffe3970f
commit
725e95414f
3 changed files with 18 additions and 85 deletions
|
|
@ -17,21 +17,13 @@
|
||||||
package jsprit.core.algorithm.acceptor;
|
package jsprit.core.algorithm.acceptor;
|
||||||
|
|
||||||
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
|
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.AlgorithmStartsListener;
|
||||||
import jsprit.core.algorithm.listener.IterationEndsListener;
|
|
||||||
import jsprit.core.algorithm.listener.IterationStartsListener;
|
import jsprit.core.algorithm.listener.IterationStartsListener;
|
||||||
import jsprit.core.problem.VehicleRoutingProblem;
|
import jsprit.core.problem.VehicleRoutingProblem;
|
||||||
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
|
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.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
import java.net.URL;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -81,35 +73,18 @@ public class SchrimpfAcceptance implements SolutionAcceptor, IterationStartsList
|
||||||
|
|
||||||
private final double alpha;
|
private final double alpha;
|
||||||
|
|
||||||
private int nOfTotalIterations = 1000;
|
private int maxIterations = 1000;
|
||||||
|
|
||||||
private int currentIteration = 0;
|
private int currentIteration = 0;
|
||||||
|
|
||||||
private double initialThreshold = 0.0;
|
private double initialThreshold = 0.0;
|
||||||
|
|
||||||
private int nOfRandomWalks;
|
|
||||||
|
|
||||||
private final int solutionMemory;
|
private final int solutionMemory;
|
||||||
|
|
||||||
private boolean determineInitialThreshold = true;
|
|
||||||
|
|
||||||
public SchrimpfAcceptance(int solutionMemory, double alpha){
|
public SchrimpfAcceptance(int solutionMemory, double alpha){
|
||||||
this.alpha = alpha;
|
this.alpha = alpha;
|
||||||
this.solutionMemory = solutionMemory;
|
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);
|
logger.info("initialise " + this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -141,11 +116,11 @@ public class SchrimpfAcceptance implements SolutionAcceptor, IterationStartsList
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "[name=SchrimpfAcceptance][alpha="+alpha+"][warmup=" + nOfRandomWalks + "]";
|
return "[name=SchrimpfAcceptance][alpha="+alpha+"]";
|
||||||
}
|
}
|
||||||
|
|
||||||
private double getThreshold(int iteration) {
|
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);
|
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) {
|
public void setInitialThreshold(double initialThreshold) {
|
||||||
this.initialThreshold = initialThreshold;
|
this.initialThreshold = initialThreshold;
|
||||||
determineInitialThreshold=false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void informAlgorithmStarts(VehicleRoutingProblem problem, VehicleRoutingAlgorithm algorithm, Collection<VehicleRoutingProblemSolution> solutions) {
|
public void informAlgorithmStarts(VehicleRoutingProblem problem, VehicleRoutingAlgorithm algorithm, Collection<VehicleRoutingProblemSolution> solutions) {
|
||||||
if(!determineInitialThreshold){
|
|
||||||
logger.info("skip threshold initialization from here");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
reset();
|
reset();
|
||||||
logger.info("---------------------------------------------------------------------");
|
this.maxIterations = algorithm.getMaxIterations();
|
||||||
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("---------------------------------------------------------------------");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void reset() {
|
private void reset() {
|
||||||
|
|
|
||||||
|
|
@ -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
|
* This library is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU Lesser General Public
|
* 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,
|
* This library is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* 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
|
* 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/>.
|
* 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;
|
package jsprit.core.algorithm.acceptor;
|
||||||
|
|
||||||
|
|
@ -44,7 +42,7 @@ public class SchrimpfAcceptanceTest {
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
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
|
// we skip the warmup, but still want to test that the initialThreshold is set
|
||||||
schrimpfAcceptance.setInitialThreshold(0.0);
|
schrimpfAcceptance.setInitialThreshold(0.0);
|
||||||
// create empty memory with an initial capacity of 1
|
// create empty memory with an initial capacity of 1
|
||||||
|
|
|
||||||
|
|
@ -62,7 +62,8 @@ public class SolomonR101Example {
|
||||||
* The algorithm can be defined and configured in an xml-file.
|
* The algorithm can be defined and configured in an xml-file.
|
||||||
*/
|
*/
|
||||||
// VehicleRoutingAlgorithm vra = new SchrimpfFactory().createAlgorithm(vrp);
|
// 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.setPrematureBreak(100);
|
||||||
// vra.getAlgorithmListeners().addListener(new AlgorithmSearchProgressChartListener("output/sol_progress.png"));
|
// vra.getAlgorithmListeners().addListener(new AlgorithmSearchProgressChartListener("output/sol_progress.png"));
|
||||||
/*
|
/*
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue