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:
parent
6c7e950b86
commit
2516cfd051
4 changed files with 97 additions and 23 deletions
|
|
@ -658,7 +658,7 @@ public class VehicleRoutingAlgorithms {
|
||||||
log.info("set prematureBreak based on time");
|
log.info("set prematureBreak based on time");
|
||||||
String timeString = config.getString("time");
|
String timeString = config.getString("time");
|
||||||
if(timeString == null) throw new IllegalStateException("time is missing");
|
if(timeString == null) throw new IllegalStateException("time is missing");
|
||||||
double time = Double.valueOf(timeString);
|
long time = Long.parseLong(timeString);
|
||||||
TimeTermination timeBreaker = new TimeTermination(time);
|
TimeTermination timeBreaker = new TimeTermination(time);
|
||||||
algorithmListeners.add(new PrioritizedVRAListener(Priority.LOW, timeBreaker));
|
algorithmListeners.add(new PrioritizedVRAListener(Priority.LOW, timeBreaker));
|
||||||
return timeBreaker;
|
return timeBreaker;
|
||||||
|
|
@ -695,7 +695,7 @@ public class VehicleRoutingAlgorithms {
|
||||||
log.info("set prematureBreak based on time");
|
log.info("set prematureBreak based on time");
|
||||||
String timeString = config.getString("prematureBreak.time");
|
String timeString = config.getString("prematureBreak.time");
|
||||||
if(timeString == null) throw new IllegalStateException("prematureBreak.time is missing");
|
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);
|
TimeTermination timeBreaker = new TimeTermination(time);
|
||||||
algorithmListeners.add(new PrioritizedVRAListener(Priority.LOW, timeBreaker));
|
algorithmListeners.add(new PrioritizedVRAListener(Priority.LOW, timeBreaker));
|
||||||
return timeBreaker;
|
return timeBreaker;
|
||||||
|
|
|
||||||
|
|
@ -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
|
* This library is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU Lesser General Public
|
* 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.
|
* 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,
|
* This library is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
* Lesser General Public License for more details.
|
* Lesser General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU Lesser General Public
|
* 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/>.
|
* 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{
|
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.
|
* Constructs TimeTermination that terminates algorithm prematurely based on specified time.
|
||||||
*
|
*
|
||||||
* @param timeThreshold_in_seconds the computation time after which the algorithm terminates
|
* @param timeThreshold_in_milliseconds the computation time [in ms] after which the algorithm terminates
|
||||||
*/
|
*/
|
||||||
public TimeTermination(double timeThreshold_in_seconds) {
|
public TimeTermination(long timeThreshold_in_milliseconds) {
|
||||||
super();
|
super();
|
||||||
this.timeThreshold = timeThreshold_in_seconds;
|
this.timeThreshold = timeThreshold_in_milliseconds;
|
||||||
logger.info("initialise " + this);
|
logger.info("initialise " + this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public void setTimeGetter(TimeGetter timeGetter) {
|
||||||
|
this.timeGetter = timeGetter;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "[name=TimeTermination][timeThreshold="+timeThreshold+"]";
|
return "[name=TimeTermination][timeThreshold="+timeThreshold+" ms]";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isPrematureBreak(DiscoveredSolution discoveredSolution) {
|
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;
|
this.startTime = startTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
private double now(){
|
private long now(){
|
||||||
return System.currentTimeMillis();
|
return timeGetter.getCurrentTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void informAlgorithmStarts(VehicleRoutingProblem problem,VehicleRoutingAlgorithm algorithm, Collection<VehicleRoutingProblemSolution> solutions) {
|
public void informAlgorithmStarts(VehicleRoutingProblem problem,VehicleRoutingAlgorithm algorithm, Collection<VehicleRoutingProblemSolution> solutions) {
|
||||||
start(System.currentTimeMillis());
|
start(timeGetter.getCurrentTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -151,7 +151,7 @@
|
||||||
|
|
||||||
<xs:group name="pBreak_time_group">
|
<xs:group name="pBreak_time_group">
|
||||||
<xs:sequence>
|
<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:sequence>
|
||||||
</xs:group>
|
</xs:group>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue