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:
parent
632a889c4b
commit
e198f71600
2 changed files with 35 additions and 19 deletions
|
|
@ -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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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>
|
||||||
|
*
|
||||||
|
* <code>algorithm.setPrematureAlgorithmTermination(this);</code><br>
|
||||||
|
* <code>algorithm.addListener(this);</code>
|
||||||
*
|
*
|
||||||
* @author stefan
|
* @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());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue