From 55ffe3970f39ab66fc4b653398c7397f16f84e4f Mon Sep 17 00:00:00 2001 From: oblonski <4sschroeder@gmail.com> Date: Thu, 28 Aug 2014 05:55:54 +0200 Subject: [PATCH] add javadoc to acceptors --- .../acceptor/AcceptNewRemoveFirst.java | 18 +++---- .../algorithm/acceptor/GreedyAcceptance.java | 19 +++++--- .../GreedyAcceptance_minVehFirst.java | 14 +++--- .../acceptor/SchrimpfAcceptance.java | 48 +++++++++---------- .../SchrimpfInitialThresholdGenerator.java | 20 ++++---- 5 files changed, 62 insertions(+), 57 deletions(-) diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/acceptor/AcceptNewRemoveFirst.java b/jsprit-core/src/main/java/jsprit/core/algorithm/acceptor/AcceptNewRemoveFirst.java index 8da92a94..c14762e3 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/acceptor/AcceptNewRemoveFirst.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/acceptor/AcceptNewRemoveFirst.java @@ -1,27 +1,29 @@ /******************************************************************************* - * 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 - * License as published by the Free Software Foundation; either + * License as published by the Free Software Foundation; either * version 3.0 of the License, or (at your option) any later version. - * + * * 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. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . ******************************************************************************/ package jsprit.core.algorithm.acceptor; -import java.util.Collection; - import jsprit.core.problem.solution.VehicleRoutingProblemSolution; +import java.util.Collection; - +/** + * @deprecated use GreedyAcceptance instead + */ +@Deprecated public class AcceptNewRemoveFirst implements SolutionAcceptor{ private final int solutionMemory; diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/acceptor/GreedyAcceptance.java b/jsprit-core/src/main/java/jsprit/core/algorithm/acceptor/GreedyAcceptance.java index b892d145..b54be03a 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/acceptor/GreedyAcceptance.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/acceptor/GreedyAcceptance.java @@ -1,16 +1,16 @@ /******************************************************************************* - * 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 - * License as published by the Free Software Foundation; either + * License as published by the Free Software Foundation; either * version 3.0 of the License, or (at your option) any later version. - * + * * 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. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . ******************************************************************************/ @@ -21,7 +21,12 @@ import jsprit.core.problem.solution.VehicleRoutingProblemSolution; import java.util.Collection; - +/** + * Acceptor that accepts solutions to be memorized only better solutions. + * + *

If there is enough memory, every solution will be accepted. If there is no memory anymore and the solution + * to be evaluated is better than the worst, the worst will be replaced by the new solution.

+ */ public class GreedyAcceptance implements SolutionAcceptor{ private final int solutionMemory; @@ -59,7 +64,7 @@ public class GreedyAcceptance implements SolutionAcceptor{ @Override public String toString() { - return "[name=acceptNewRemoveWorst]"; + return "[name=GreedyAcceptance]"; } diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/acceptor/GreedyAcceptance_minVehFirst.java b/jsprit-core/src/main/java/jsprit/core/algorithm/acceptor/GreedyAcceptance_minVehFirst.java index ae5b0976..0fda452b 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/acceptor/GreedyAcceptance_minVehFirst.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/acceptor/GreedyAcceptance_minVehFirst.java @@ -1,27 +1,27 @@ /******************************************************************************* - * 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 - * License as published by the Free Software Foundation; either + * License as published by the Free Software Foundation; either * version 3.0 of the License, or (at your option) any later version. - * + * * 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. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . ******************************************************************************/ package jsprit.core.algorithm.acceptor; -import java.util.Collection; - import jsprit.core.problem.solution.VehicleRoutingProblemSolution; +import java.util.Collection; +@Deprecated public class GreedyAcceptance_minVehFirst implements SolutionAcceptor{ private final int solutionMemory; diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/acceptor/SchrimpfAcceptance.java b/jsprit-core/src/main/java/jsprit/core/algorithm/acceptor/SchrimpfAcceptance.java index 7bab59ff..3b55288c 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/acceptor/SchrimpfAcceptance.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/acceptor/SchrimpfAcceptance.java @@ -1,16 +1,16 @@ /******************************************************************************* - * 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 - * License as published by the Free Software Foundation; either + * License as published by the Free Software Foundation; either * version 3.0 of the License, or (at your option) any later version. - * + * * 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. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . ******************************************************************************/ @@ -101,11 +101,7 @@ public class SchrimpfAcceptance implements SolutionAcceptor, IterationStartsList } /** - * - * @param solutionMemory - * @param alpha - * @param nOfWarmupIterations - * @Deprecated use new SchrimpfAcceptance(solutionMemory,alpha) instead. if you want to determine ini-threshold with a + * @deprecated use new SchrimpfAcceptance(solutionMemory,alpha) instead. if you want to determine ini-threshold with a * random walk and the algorithm 'randomWalk.xml' use SchrimpfInitialThresholdGenerator.class instead. */ @Deprecated @@ -130,7 +126,11 @@ public class SchrimpfAcceptance implements SolutionAcceptor, IterationStartsList if(worst == null) worst = solutionInMemory; else if(solutionInMemory.getCost() > worst.getCost()) worst = solutionInMemory; } - if(newSolution.getCost() < worst.getCost() + threshold){ + if(worst == null){ + solutions.add(newSolution); + solutionAccepted = true; + } + else if(newSolution.getCost() < worst.getCost() + threshold){ solutions.remove(worst); solutions.add(newSolution); solutionAccepted = true; @@ -141,17 +141,17 @@ public class SchrimpfAcceptance implements SolutionAcceptor, IterationStartsList @Override public String toString() { - return "[name=schrimpfAcceptanceFunction][alpha="+alpha+"][warmup=" + nOfRandomWalks + "]"; + return "[name=SchrimpfAcceptance][alpha="+alpha+"][warmup=" + nOfRandomWalks + "]"; } private double getThreshold(int iteration) { double scheduleVariable = (double) iteration / (double) nOfTotalIterations; - double currentThreshold = initialThreshold * Math.exp(-1. * Math.log(2) * scheduleVariable / alpha); - return currentThreshold; + return initialThreshold * Math.exp(-1. * Math.log(2) * scheduleVariable / alpha); } - public double getInitialThreshold(){ + @SuppressWarnings("UnusedDeclaration") + public double getInitialThreshold(){ return initialThreshold; } @@ -177,39 +177,39 @@ public class SchrimpfAcceptance implements SolutionAcceptor, IterationStartsList logger.info("prepare schrimpfAcceptanceFunction, i.e. determine initial threshold"); logger.info("start random-walk (see randomWalk.xml)"); double now = System.currentTimeMillis(); - this.nOfTotalIterations = algorithm.getNuOfIterations(); - + 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.setNuOfIterations(nOfRandomWalks); + vra.setMaxIterations(nOfRandomWalks); vra.getAlgorithmListeners().addListener(new IterationEndsListener() { - + @Override public void informIterationEnds(int iteration, VehicleRoutingProblem problem, Collection 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() { diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/acceptor/SchrimpfInitialThresholdGenerator.java b/jsprit-core/src/main/java/jsprit/core/algorithm/acceptor/SchrimpfInitialThresholdGenerator.java index 86d704df..cbfca8e9 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/acceptor/SchrimpfInitialThresholdGenerator.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/acceptor/SchrimpfInitialThresholdGenerator.java @@ -1,20 +1,18 @@ /******************************************************************************* - * 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 - * License as published by the Free Software Foundation; either + * License as published by the Free Software Foundation; either * version 3.0 of the License, or (at your option) any later version. - * + * * 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. - * - * You should have received a copy of the GNU Lesser General Public + * 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 . - * - * Contributors: - * Stefan Schroeder - initial API and implementation ******************************************************************************/ package jsprit.core.algorithm.acceptor; @@ -65,7 +63,7 @@ public class SchrimpfInitialThresholdGenerator implements AlgorithmStartsListene AlgorithmConfig algorithmConfig = new AlgorithmConfig(); new AlgorithmConfigXmlReader(algorithmConfig).read(resource); VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.createAlgorithm(problem, algorithmConfig); - vra.setNuOfIterations(nOfRandomWalks); + vra.setMaxIterations(nOfRandomWalks); vra.getAlgorithmListeners().addListener(new IterationEndsListener() { @Override