diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/termination/PrematureAlgorithmTermination.java b/jsprit-core/src/main/java/jsprit/core/algorithm/termination/PrematureAlgorithmTermination.java index 78d31e5d..92c42d1d 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/termination/PrematureAlgorithmTermination.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/termination/PrematureAlgorithmTermination.java @@ -18,8 +18,18 @@ package jsprit.core.algorithm.termination; import jsprit.core.algorithm.SearchStrategy.DiscoveredSolution; +/** + * Basic interface for prematureTermination. + * + */ public interface PrematureAlgorithmTermination { - + + /** + * Returns true if algorithm should terminate, false otherwise. + * + * @param discoveredSolution the discovered solution + * @return true if algorithm should terminate, false otherwise + */ public boolean isPrematureBreak(DiscoveredSolution discoveredSolution); } 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 a24ef879..19f86c39 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 @@ -28,51 +28,57 @@ import java.util.Collection; /** - * Breaks algorithm prematurely based on specified time. + * Terminates algorithm prematurely based on specified time. * - *

Note, TimeBreaker must be registered as AlgorithmListener
- * agorithm.getAlgorithmListeners().addListener(this); + *

Note, TimeTermination must be registered as AlgorithmListener
+ * TimeTermination will be activated by:
+ * + * algorithm.setPrematureAlgorithmTermination(this);
+ * algorithm.addListener(this); * - * @author stefan + * @author stefan schroeder * */ public class TimeTermination implements PrematureAlgorithmTermination, AlgorithmStartsListener{ private static Logger logger = LogManager.getLogger(TimeTermination.class); - private double timeThreshold; + private final double timeThreshold; private double startTime; /** - * Constructs TimeBreaker that breaks algorithm prematurely based on specified time. - * - *

Note, TimeBreaker must be registered as AlgorithmListener
- * agorithm.addListener(this); - * - * @author stefan + * 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 time_in_seconds) { + public TimeTermination(double timeThreshold_in_seconds) { super(); - this.timeThreshold = time_in_seconds; + this.timeThreshold = timeThreshold_in_seconds; logger.info("initialise " + this); } @Override public String toString() { - return "[name=TimeBreaker][timeThreshold="+timeThreshold+"]"; + return "[name=TimeTermination][timeThreshold="+timeThreshold+"]"; } @Override public boolean isPrematureBreak(DiscoveredSolution discoveredSolution) { - if((System.currentTimeMillis() - startTime)/1000.0 > timeThreshold) return true; - return false; + return ( ( now() - startTime ) / 1000.0 > timeThreshold ); } + + void start(double startTime){ + this.startTime = startTime; + } + + private double now(){ + return System.currentTimeMillis(); + } @Override - public void informAlgorithmStarts(VehicleRoutingProblem problem,VehicleRoutingAlgorithm algorithm,Collection solutions) { - startTime = System.currentTimeMillis(); + public void informAlgorithmStarts(VehicleRoutingProblem problem,VehicleRoutingAlgorithm algorithm, Collection solutions) { + start(System.currentTimeMillis()); } }