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:
parent
657f85e896
commit
f8f063773a
3 changed files with 101 additions and 13 deletions
|
|
@ -32,7 +32,11 @@ import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
|
||||||
public class ComputationalLaboratory {
|
public class ComputationalLaboratory {
|
||||||
|
|
||||||
|
public static interface LabListener {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Listener-interface to listen to calculation.
|
* Listener-interface to listen to calculation.
|
||||||
*
|
*
|
||||||
|
|
@ -42,13 +46,20 @@ public class ComputationalLaboratory {
|
||||||
* @author schroeder
|
* @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);
|
public void calculationStarts(final BenchmarkInstance p, final String algorithmName, final VehicleRoutingAlgorithm algorithm, final int run);
|
||||||
|
|
||||||
public void calculationEnds(final BenchmarkInstance p, final String algorithmName, final VehicleRoutingAlgorithm algorithm, final int run, final Collection<VehicleRoutingProblemSolution> solutions);
|
public void calculationEnds(final BenchmarkInstance p, final String algorithmName, final VehicleRoutingAlgorithm algorithm, final int run, final Collection<VehicleRoutingProblemSolution> solutions);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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.
|
* Collects whatever indicators you require by algorithmName, instanceName, run and indicator.
|
||||||
|
|
@ -251,6 +262,8 @@ public class ComputationalLaboratory {
|
||||||
private int runs = 1;
|
private int runs = 1;
|
||||||
|
|
||||||
private Collection<CalculationListener> listeners = new ArrayList<ComputationalLaboratory.CalculationListener>();
|
private Collection<CalculationListener> listeners = new ArrayList<ComputationalLaboratory.CalculationListener>();
|
||||||
|
|
||||||
|
private Collection<LabStartsAndEndsListener> startsAndEndslisteners = new ArrayList<LabStartsAndEndsListener>();
|
||||||
|
|
||||||
private List<Algorithm> algorithms = new ArrayList<ComputationalLaboratory.Algorithm>();
|
private List<Algorithm> algorithms = new ArrayList<ComputationalLaboratory.Algorithm>();
|
||||||
|
|
||||||
|
|
@ -258,7 +271,7 @@ public class ComputationalLaboratory {
|
||||||
|
|
||||||
private Set<String> instanceNames = new HashSet<String>();
|
private Set<String> instanceNames = new HashSet<String>();
|
||||||
|
|
||||||
private int threads = Runtime.getRuntime().availableProcessors()+1;
|
private int threads = 1;
|
||||||
|
|
||||||
public ComputationalLaboratory() {
|
public ComputationalLaboratory() {
|
||||||
|
|
||||||
|
|
@ -335,11 +348,16 @@ public class ComputationalLaboratory {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds listener to listen computational experiments.
|
* Adds listener to listen computational experiments.
|
||||||
*
|
*
|
||||||
* @param listener
|
* @param listener
|
||||||
*/
|
*/
|
||||||
public void addListener(CalculationListener listener){
|
public void addListener(LabListener listener){
|
||||||
listeners.add(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()){
|
if(benchmarkInstances.isEmpty()){
|
||||||
throw new IllegalStateException("no instance specified. at least one instance needs to be specified.");
|
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 + "]");
|
System.out.println("start benchmarking [nuAlgorithms="+algorithms.size()+"][nuInstances=" + benchmarkInstances.size() + "][runsPerInstance=" + runs + "]");
|
||||||
double startTime = System.currentTimeMillis();
|
double startTime = System.currentTimeMillis();
|
||||||
ExecutorService executor = Executors.newFixedThreadPool(threads);
|
ExecutorService executor = Executors.newFixedThreadPool(threads);
|
||||||
|
|
@ -379,14 +398,16 @@ public class ComputationalLaboratory {
|
||||||
for(final BenchmarkInstance p : benchmarkInstances){
|
for(final BenchmarkInstance p : benchmarkInstances){
|
||||||
for(int run=0;run<runs;run++){
|
for(int run=0;run<runs;run++){
|
||||||
final int r = run;
|
final int r = run;
|
||||||
executor.submit(new Runnable(){
|
runAlgorithm(p, algorithm, r+1);
|
||||||
|
|
||||||
@Override
|
// executor.submit(new Runnable(){
|
||||||
public void run() {
|
//
|
||||||
runAlgorithm(p, algorithm, r+1);
|
// @Override
|
||||||
}
|
// public void run() {
|
||||||
|
// ;
|
||||||
});
|
// }
|
||||||
|
//
|
||||||
|
// });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -398,6 +419,19 @@ public class ComputationalLaboratory {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
System.out.println("benchmarking done [time="+(System.currentTimeMillis()-startTime)/1000 + "sec]");
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,37 @@ import jsprit.core.util.Coordinate;
|
||||||
*/
|
*/
|
||||||
public final class Location implements HasIndex, HasId{
|
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 {
|
public static class Builder {
|
||||||
|
|
||||||
private String id;
|
private String id;
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,13 @@ public class LocationTest {
|
||||||
Assert.assertTrue(true);
|
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)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void whenIndexSmallerZero_throwException(){
|
public void whenIndexSmallerZero_throwException(){
|
||||||
Location l = Location.Builder.newInstance().setIndex(-1).build();
|
Location l = Location.Builder.newInstance().setIndex(-1).build();
|
||||||
|
|
@ -50,6 +57,13 @@ public class LocationTest {
|
||||||
Assert.assertTrue(true);
|
Assert.assertTrue(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenIdSetWithFactory_returnCorrectLocation(){
|
||||||
|
Location l = Location.newInstance("id");
|
||||||
|
Assert.assertEquals("id",l.getId());
|
||||||
|
Assert.assertTrue(true);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCoordinateSet_build(){
|
public void whenCoordinateSet_build(){
|
||||||
Location l = Location.Builder.newInstance().setCoordinate(Coordinate.newInstance(10,20)).build();
|
Location l = Location.Builder.newInstance().setCoordinate(Coordinate.newInstance(10,20)).build();
|
||||||
|
|
@ -58,5 +72,14 @@ public class LocationTest {
|
||||||
Assert.assertTrue(true);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue