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

add factory methods to Location to simplify location building

This commit is contained in:
oblonski 2015-02-12 20:33:08 +01:00
parent 657f85e896
commit f8f063773a
3 changed files with 101 additions and 13 deletions

View file

@ -33,6 +33,10 @@ import java.util.concurrent.TimeUnit;
public class ComputationalLaboratory {
public static interface LabListener {
}
/**
* Listener-interface to listen to calculation.
*
@ -42,7 +46,7 @@ public class ComputationalLaboratory {
* @author schroeder
*
*/
public static interface CalculationListener {
public static interface CalculationListener extends LabListener{
public void calculationStarts(final BenchmarkInstance p, final String algorithmName, final VehicleRoutingAlgorithm algorithm, final int run);
@ -50,6 +54,13 @@ public class ComputationalLaboratory {
}
public static interface LabStartsAndEndsListener extends LabListener {
public void labStarts(List<BenchmarkInstance> instances, int noAlgorithms, int runs);
public void labEnds();
}
/**
* Collects whatever indicators you require by algorithmName, instanceName, run and indicator.
*
@ -252,13 +263,15 @@ public class ComputationalLaboratory {
private Collection<CalculationListener> listeners = new ArrayList<ComputationalLaboratory.CalculationListener>();
private Collection<LabStartsAndEndsListener> startsAndEndslisteners = new ArrayList<LabStartsAndEndsListener>();
private List<Algorithm> algorithms = new ArrayList<ComputationalLaboratory.Algorithm>();
private Set<String> algorithmNames = new HashSet<String>();
private Set<String> instanceNames = new HashSet<String>();
private int threads = Runtime.getRuntime().availableProcessors()+1;
private int threads = 1;
public ComputationalLaboratory() {
@ -338,8 +351,13 @@ public class ComputationalLaboratory {
*
* @param listener
*/
public void addListener(CalculationListener listener){
listeners.add(listener);
public void addListener(LabListener listener){
if(listener instanceof CalculationListener) {
listeners.add((CalculationListener) listener);
}
if(listener instanceof LabStartsAndEndsListener){
startsAndEndslisteners.add((LabStartsAndEndsListener) listener);
}
}
/**
@ -372,6 +390,7 @@ public class ComputationalLaboratory {
if(benchmarkInstances.isEmpty()){
throw new IllegalStateException("no instance specified. at least one instance needs to be specified.");
}
informStart();
System.out.println("start benchmarking [nuAlgorithms="+algorithms.size()+"][nuInstances=" + benchmarkInstances.size() + "][runsPerInstance=" + runs + "]");
double startTime = System.currentTimeMillis();
ExecutorService executor = Executors.newFixedThreadPool(threads);
@ -379,14 +398,16 @@ public class ComputationalLaboratory {
for(final BenchmarkInstance p : benchmarkInstances){
for(int run=0;run<runs;run++){
final int r = run;
executor.submit(new Runnable(){
@Override
public void run() {
runAlgorithm(p, algorithm, r+1);
}
});
// executor.submit(new Runnable(){
//
// @Override
// public void run() {
// ;
// }
//
// });
}
}
}
@ -398,6 +419,19 @@ public class ComputationalLaboratory {
e.printStackTrace();
}
System.out.println("benchmarking done [time="+(System.currentTimeMillis()-startTime)/1000 + "sec]");
informEnd();
}
private void informEnd() {
for(LabStartsAndEndsListener l : startsAndEndslisteners){
l.labEnds();
}
}
private void informStart() {
for(LabStartsAndEndsListener l : startsAndEndslisteners){
l.labStarts(benchmarkInstances, algorithms.size(),runs);
}
}
/**

View file

@ -24,6 +24,37 @@ import jsprit.core.util.Coordinate;
*/
public final class Location implements HasIndex, HasId{
/**
* Factory method (and shortcut) for creating a location object just with x and y coordinates.
*
* @param x coordinate
* @param y coordinate
* @return location
*/
public static Location newInstance(double x, double y){
return Location.Builder.newInstance().setCoordinate(Coordinate.newInstance(x,y)).build();
}
/**
* Factory method (and shortcut) for creating location object just with id
*
* @param id location id
* @return location
*/
public static Location newInstance(String id){
return Location.Builder.newInstance().setId(id).build();
}
/**
* Factory method (and shortcut) for creating location object just with location index
*
* @param index
* @return
*/
public static Location newInstance(int index){
return Location.Builder.newInstance().setIndex(index).build();
}
public static class Builder {
private String id;

View file

@ -33,6 +33,13 @@ public class LocationTest {
Assert.assertTrue(true);
}
@Test
public void whenIndexSetWitFactory_returnCorrectLocation(){
Location l = Location.newInstance(1);
Assert.assertEquals(1,l.getIndex());
Assert.assertTrue(true);
}
@Test(expected = IllegalArgumentException.class)
public void whenIndexSmallerZero_throwException(){
Location l = Location.Builder.newInstance().setIndex(-1).build();
@ -50,6 +57,13 @@ public class LocationTest {
Assert.assertTrue(true);
}
@Test
public void whenIdSetWithFactory_returnCorrectLocation(){
Location l = Location.newInstance("id");
Assert.assertEquals("id",l.getId());
Assert.assertTrue(true);
}
@Test
public void whenCoordinateSet_build(){
Location l = Location.Builder.newInstance().setCoordinate(Coordinate.newInstance(10,20)).build();
@ -58,5 +72,14 @@ public class LocationTest {
Assert.assertTrue(true);
}
@Test
public void whenCoordinateSetWithFactory_returnCorrectLocation(){
// Location l = Location.Builder.newInstance().setCoordinate(Coordinate.newInstance(10,20)).build();
Location l = Location.newInstance(10,20);
Assert.assertEquals(10.,l.getCoordinate().getX());
Assert.assertEquals(20.,l.getCoordinate().getY());
Assert.assertTrue(true);
}
}