1
0
Fork 0
mirror of https://github.com/graphhopper/jsprit.git synced 2020-01-24 07:45:05 +01:00

clean and document core.algorithm.termination.TimeTermination

This commit is contained in:
oblonski 2014-08-12 10:17:41 +02:00
parent 632a889c4b
commit e198f71600
2 changed files with 35 additions and 19 deletions

View file

@ -18,8 +18,18 @@ package jsprit.core.algorithm.termination;
import jsprit.core.algorithm.SearchStrategy.DiscoveredSolution; import jsprit.core.algorithm.SearchStrategy.DiscoveredSolution;
/**
* Basic interface for prematureTermination.
*
*/
public interface PrematureAlgorithmTermination { 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); public boolean isPrematureBreak(DiscoveredSolution discoveredSolution);
} }

View file

@ -28,51 +28,57 @@ import java.util.Collection;
/** /**
* Breaks algorithm prematurely based on specified time. * Terminates algorithm prematurely based on specified time.
* *
* <p>Note, TimeBreaker must be registered as AlgorithmListener <br> * <p>Note, TimeTermination must be registered as AlgorithmListener <br>
* <code>agorithm.getAlgorithmListeners().addListener(this);</code> * TimeTermination will be activated by:<br>
* *
* @author stefan * <code>algorithm.setPrematureAlgorithmTermination(this);</code><br>
* <code>algorithm.addListener(this);</code>
*
* @author stefan schroeder
* *
*/ */
public class TimeTermination implements PrematureAlgorithmTermination, AlgorithmStartsListener{ public class TimeTermination implements PrematureAlgorithmTermination, AlgorithmStartsListener{
private static Logger logger = LogManager.getLogger(TimeTermination.class); private static Logger logger = LogManager.getLogger(TimeTermination.class);
private double timeThreshold; private final double timeThreshold;
private double startTime; private double startTime;
/** /**
* Constructs TimeBreaker that breaks algorithm prematurely based on specified time. * Constructs TimeTermination that terminates algorithm prematurely based on specified time.
*
* <p>Note, TimeBreaker must be registered as AlgorithmListener <br>
* <code>agorithm.addListener(this);</code>
*
* @author stefan
* *
* @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(); super();
this.timeThreshold = time_in_seconds; this.timeThreshold = timeThreshold_in_seconds;
logger.info("initialise " + this); logger.info("initialise " + this);
} }
@Override @Override
public String toString() { public String toString() {
return "[name=TimeBreaker][timeThreshold="+timeThreshold+"]"; return "[name=TimeTermination][timeThreshold="+timeThreshold+"]";
} }
@Override @Override
public boolean isPrematureBreak(DiscoveredSolution discoveredSolution) { public boolean isPrematureBreak(DiscoveredSolution discoveredSolution) {
if((System.currentTimeMillis() - startTime)/1000.0 > timeThreshold) return true; return ( ( now() - startTime ) / 1000.0 > timeThreshold );
return false; }
void start(double startTime){
this.startTime = startTime;
}
private double now(){
return System.currentTimeMillis();
} }
@Override @Override
public void informAlgorithmStarts(VehicleRoutingProblem problem,VehicleRoutingAlgorithm algorithm, Collection<VehicleRoutingProblemSolution> solutions) { public void informAlgorithmStarts(VehicleRoutingProblem problem,VehicleRoutingAlgorithm algorithm, Collection<VehicleRoutingProblemSolution> solutions) {
startTime = System.currentTimeMillis(); start(System.currentTimeMillis());
} }
} }