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;
/**
* 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);
}

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>
* <code>agorithm.getAlgorithmListeners().addListener(this);</code>
* <p>Note, TimeTermination must be registered as AlgorithmListener <br>
* 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{
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.
*
* <p>Note, TimeBreaker must be registered as AlgorithmListener <br>
* <code>agorithm.addListener(this);</code>
*
* @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<VehicleRoutingProblemSolution> solutions) {
startTime = System.currentTimeMillis();
public void informAlgorithmStarts(VehicleRoutingProblem problem,VehicleRoutingAlgorithm algorithm, Collection<VehicleRoutingProblemSolution> solutions) {
start(System.currentTimeMillis());
}
}