diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/io/VehicleRoutingAlgorithms.java b/jsprit-core/src/main/java/jsprit/core/algorithm/io/VehicleRoutingAlgorithms.java index 4a84a42a..bfb43c1b 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/io/VehicleRoutingAlgorithms.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/io/VehicleRoutingAlgorithms.java @@ -658,7 +658,7 @@ public class VehicleRoutingAlgorithms { log.info("set prematureBreak based on time"); String timeString = config.getString("time"); if(timeString == null) throw new IllegalStateException("time is missing"); - double time = Double.valueOf(timeString); + long time = Long.parseLong(timeString); TimeTermination timeBreaker = new TimeTermination(time); algorithmListeners.add(new PrioritizedVRAListener(Priority.LOW, timeBreaker)); return timeBreaker; @@ -695,7 +695,7 @@ public class VehicleRoutingAlgorithms { log.info("set prematureBreak based on time"); String timeString = config.getString("prematureBreak.time"); if(timeString == null) throw new IllegalStateException("prematureBreak.time is missing"); - double time = Double.valueOf(timeString); + long time = Long.parseLong(timeString); TimeTermination timeBreaker = new TimeTermination(time); algorithmListeners.add(new PrioritizedVRAListener(Priority.LOW, timeBreaker)); return timeBreaker; diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/termination/TimeTermination.java b/jsprit-core/src/main/java/jsprit/core/algorithm/termination/TimeTermination.java index 19f86c39..fbefde61 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/termination/TimeTermination.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/termination/TimeTermination.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 . ******************************************************************************/ @@ -41,44 +41,63 @@ import java.util.Collection; */ public class TimeTermination implements PrematureAlgorithmTermination, AlgorithmStartsListener{ - private static Logger logger = LogManager.getLogger(TimeTermination.class); + public static interface TimeGetter { + + public long getCurrentTime(); + + } + + private static Logger logger = LogManager.getLogger(TimeTermination.class); - private final double timeThreshold; + private final long timeThreshold; + + private TimeGetter timeGetter = new TimeGetter() { + + @Override + public long getCurrentTime() { + return System.currentTimeMillis(); + } + + }; - private double startTime; + private long startTime; /** * Constructs TimeTermination that terminates algorithm prematurely based on specified time. * - * @param timeThreshold_in_seconds the computation time after which the algorithm terminates - */ - public TimeTermination(double timeThreshold_in_seconds) { + * @param timeThreshold_in_milliseconds the computation time [in ms] after which the algorithm terminates + */ + public TimeTermination(long timeThreshold_in_milliseconds) { super(); - this.timeThreshold = timeThreshold_in_seconds; + this.timeThreshold = timeThreshold_in_milliseconds; logger.info("initialise " + this); } - - @Override + + public void setTimeGetter(TimeGetter timeGetter) { + this.timeGetter = timeGetter; + } + + @Override public String toString() { - return "[name=TimeTermination][timeThreshold="+timeThreshold+"]"; + return "[name=TimeTermination][timeThreshold="+timeThreshold+" ms]"; } @Override public boolean isPrematureBreak(DiscoveredSolution discoveredSolution) { - return ( ( now() - startTime ) / 1000.0 > timeThreshold ); + return ( now() - startTime ) > timeThreshold ; } - void start(double startTime){ + void start(long startTime){ this.startTime = startTime; } - private double now(){ - return System.currentTimeMillis(); + private long now(){ + return timeGetter.getCurrentTime(); } @Override public void informAlgorithmStarts(VehicleRoutingProblem problem,VehicleRoutingAlgorithm algorithm, Collection solutions) { - start(System.currentTimeMillis()); + start(timeGetter.getCurrentTime()); } } diff --git a/jsprit-core/src/main/resources/algorithm_schema.xsd b/jsprit-core/src/main/resources/algorithm_schema.xsd index d8394b54..17d06146 100644 --- a/jsprit-core/src/main/resources/algorithm_schema.xsd +++ b/jsprit-core/src/main/resources/algorithm_schema.xsd @@ -151,7 +151,7 @@ - + diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/termination/TimeTerminationTest.java b/jsprit-core/src/test/java/jsprit/core/algorithm/termination/TimeTerminationTest.java new file mode 100644 index 00000000..07c2bfd6 --- /dev/null +++ b/jsprit-core/src/test/java/jsprit/core/algorithm/termination/TimeTerminationTest.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * 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 + * 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.termination; + +import junit.framework.Assert; +import org.junit.Test; + +/** + * Created by schroeder on 16.12.14. + */ +public class TimeTerminationTest { + + @Test + public void whenTimeThreshold2000msAndCurrentTime0_itShouldNotBreak(){ + long threshold = 2000; + TimeTermination tt = new TimeTermination(threshold); + tt.setTimeGetter(new TimeTermination.TimeGetter() { + @Override + public long getCurrentTime() { + return 0; + } + }); + tt.start(0); + Assert.assertFalse(tt.isPrematureBreak(null)); + } + + @Test + public void whenTimeThreshold2000msAndCurrentTime2000ms_itShouldBreak(){ + long threshold = 2000; + TimeTermination tt = new TimeTermination(threshold); + tt.setTimeGetter(new TimeTermination.TimeGetter() { + @Override + public long getCurrentTime() { + return 2001; + } + }); + tt.start(0); + Assert.assertTrue(tt.isPrematureBreak(null)); + } +}