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

issue #143 - changed to long

This commit is contained in:
oblonski 2014-12-16 12:23:10 +01:00
parent 6c7e950b86
commit 2516cfd051
4 changed files with 97 additions and 23 deletions

View file

@ -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;

View file

@ -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 <http://www.gnu.org/licenses/>.
******************************************************************************/
@ -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<VehicleRoutingProblemSolution> solutions) {
start(System.currentTimeMillis());
start(timeGetter.getCurrentTime());
}
}

View file

@ -151,7 +151,7 @@
<xs:group name="pBreak_time_group">
<xs:sequence>
<xs:element name="time" type="xs:double" minOccurs="1" maxOccurs="1"/>
<xs:element name="time" type="xs:long" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:group>

View file

@ -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 <http://www.gnu.org/licenses/>.
******************************************************************************/
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));
}
}