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

add tests to ensure reproducibility

This commit is contained in:
oblonski 2015-04-24 17:36:49 +02:00
parent 4757753f26
commit 5f0fee329b
3 changed files with 387 additions and 32 deletions

View file

@ -16,8 +16,15 @@
******************************************************************************/
package jsprit.core.algorithm;
import jsprit.core.algorithm.acceptor.SolutionAcceptor;
import jsprit.core.algorithm.selector.SolutionSelector;
import jsprit.core.problem.solution.SolutionCostCalculator;
import jsprit.core.util.RandomNumberGeneration;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import static org.hamcrest.CoreMatchers.is;
@ -192,4 +199,33 @@ public class SearchStrategyManagerTest {
managerUnderTest.getRandomStrategy();
}
@Test
public void strategyDrawShouldBeReproducible(){
SearchStrategyManager managerUnderTest = new SearchStrategyManager();
SearchStrategy mockedStrategy1 = new SearchStrategy("strat1"
,mock(SolutionSelector.class),mock(SolutionAcceptor.class),mock(SolutionCostCalculator.class));
SearchStrategy mockedStrategy2 = new SearchStrategy("strat2"
,mock(SolutionSelector.class),mock(SolutionAcceptor.class),mock(SolutionCostCalculator.class));
managerUnderTest.addStrategy(mockedStrategy1, 0.2);
managerUnderTest.addStrategy(mockedStrategy2, 0.8);
List<String> firstRecord = new ArrayList<String>();
for(int i=0; i<1000;i++){
firstRecord.add(managerUnderTest.getRandomStrategy().getId());
}
RandomNumberGeneration.reset();
List<String> secondRecord = new ArrayList<String>();
for(int i=0; i<1000;i++){
secondRecord.add(managerUnderTest.getRandomStrategy().getId());
}
for(int i=0;i<1000;i++){
if(!firstRecord.get(i).equals(secondRecord.get(i))){
Assert.assertFalse(true);
}
}
Assert.assertTrue(true);
}
}

View file

@ -3,23 +3,38 @@ package jsprit.core.algorithm.box;
import jsprit.core.algorithm.SearchStrategy;
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
import jsprit.core.algorithm.listener.StrategySelectedListener;
import jsprit.core.algorithm.recreate.InsertionData;
import jsprit.core.algorithm.recreate.listener.BeforeJobInsertionListener;
import jsprit.core.algorithm.recreate.listener.JobInsertedListener;
import jsprit.core.algorithm.ruin.listener.RuinListener;
import jsprit.core.algorithm.termination.VariationCoefficientTermination;
import jsprit.core.problem.Location;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.io.VrpXMLReader;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.job.Service;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.vehicle.VehicleImpl;
import jsprit.core.util.RandomNumberGeneration;
import jsprit.core.util.Solutions;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
/**
* Created by schroeder on 06/03/15.
*/
public class JspritTest {
@Before
public void doBefore(){
RandomNumberGeneration.reset();
}
@Test
public void whenRunningJspritWithSingleCustomer_itShouldWork(){
Service s = Service.Builder.newInstance("s1").setLocation(Location.newInstance(1,1)).build();
@ -210,4 +225,337 @@ public class JspritTest {
}
@Test
public void strategyDrawShouldBeReproducible(){
Service s = Service.Builder.newInstance("s1").setLocation(Location.newInstance(1,1)).build();
Service s2 = Service.Builder.newInstance("s2").setLocation(Location.newInstance(1,2)).build();
Service s3 = Service.Builder.newInstance("s3").setLocation(Location.newInstance(1,2)).build();
Service s4 = Service.Builder.newInstance("s4").setLocation(Location.newInstance(1,2)).build();
VehicleImpl v = VehicleImpl.Builder.newInstance("v").setStartLocation(Location.newInstance(0,0)).build();
VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addJob(s4).addJob(s3).addVehicle(v).addJob(s2).addJob(s).build();
VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp);
vra.setMaxIterations(1000);
final List<String> firstRecord = new ArrayList<String>();
vra.addListener(new StrategySelectedListener() {
@Override
public void informSelectedStrategy(SearchStrategy.DiscoveredSolution discoveredSolution, VehicleRoutingProblem vehicleRoutingProblem, Collection<VehicleRoutingProblemSolution> vehicleRoutingProblemSolutions) {
firstRecord.add(discoveredSolution.getStrategyId());
}
});
vra.searchSolutions();
RandomNumberGeneration.reset();
VehicleRoutingAlgorithm second = Jsprit.createAlgorithm(vrp);
second.setMaxIterations(1000);
final List<String> secondRecord = new ArrayList<String>();
second.addListener(new StrategySelectedListener() {
@Override
public void informSelectedStrategy(SearchStrategy.DiscoveredSolution discoveredSolution, VehicleRoutingProblem vehicleRoutingProblem, Collection<VehicleRoutingProblemSolution> vehicleRoutingProblemSolutions) {
secondRecord.add(discoveredSolution.getStrategyId());
}
});
second.searchSolutions();
for(int i=0;i<1000;i++){
if(!firstRecord.get(i).equals(secondRecord.get(i))){
org.junit.Assert.assertFalse(true);
}
}
org.junit.Assert.assertTrue(true);
}
@Test
public void ruinedJobsShouldBeReproducible(){
Service s = Service.Builder.newInstance("s1").setLocation(Location.newInstance(1,1)).build();
Service s2 = Service.Builder.newInstance("s2").setLocation(Location.newInstance(1,2)).build();
Service s3 = Service.Builder.newInstance("s3").setLocation(Location.newInstance(1,2)).build();
Service s4 = Service.Builder.newInstance("s4").setLocation(Location.newInstance(1,2)).build();
VehicleImpl v = VehicleImpl.Builder.newInstance("v").setStartLocation(Location.newInstance(0,0)).build();
VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addJob(s4).addJob(s3).addVehicle(v).addJob(s2).addJob(s).build();
VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp);
vra.setMaxIterations(1000);
final List<String> firstRecord = new ArrayList<String>();
vra.addListener(new RuinListener() {
@Override
public void ruinStarts(Collection<VehicleRoute> routes) {
}
@Override
public void ruinEnds(Collection<VehicleRoute> routes, Collection<Job> unassignedJobs) {
}
@Override
public void removed(Job job, VehicleRoute fromRoute) {
firstRecord.add(job.getId());
}
});
vra.searchSolutions();
RandomNumberGeneration.reset();
VehicleRoutingAlgorithm second = Jsprit.createAlgorithm(vrp);
second.setMaxIterations(1000);
final List<String> secondRecord = new ArrayList<String>();
second.addListener(new RuinListener() {
@Override
public void ruinStarts(Collection<VehicleRoute> routes) {
}
@Override
public void ruinEnds(Collection<VehicleRoute> routes, Collection<Job> unassignedJobs) {
}
@Override
public void removed(Job job, VehicleRoute fromRoute) {
secondRecord.add(job.getId());
}
});
second.searchSolutions();
Assert.assertEquals(secondRecord.size(),firstRecord.size());
for(int i=0;i<firstRecord.size();i++){
if(!firstRecord.get(i).equals(secondRecord.get(i))){
Assert.assertFalse(true);
}
}
Assert.assertTrue(true);
}
@Test
public void whenBiggerProblem_ruinedJobsShouldBeReproducible(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/vrpnc1-jsprit-with-deliveries.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp);
vra.setMaxIterations(1000);
final List<String> firstRecord = new ArrayList<String>();
vra.addListener(new RuinListener() {
@Override
public void ruinStarts(Collection<VehicleRoute> routes) {
}
@Override
public void ruinEnds(Collection<VehicleRoute> routes, Collection<Job> unassignedJobs) {
}
@Override
public void removed(Job job, VehicleRoute fromRoute) {
firstRecord.add(job.getId());
}
});
vra.searchSolutions();
RandomNumberGeneration.reset();
VehicleRoutingAlgorithm second = Jsprit.createAlgorithm(vrp);
second.setMaxIterations(1000);
final List<String> secondRecord = new ArrayList<String>();
second.addListener(new RuinListener() {
@Override
public void ruinStarts(Collection<VehicleRoute> routes) {
}
@Override
public void ruinEnds(Collection<VehicleRoute> routes, Collection<Job> unassignedJobs) {
}
@Override
public void removed(Job job, VehicleRoute fromRoute) {
secondRecord.add(job.getId());
}
});
second.searchSolutions();
Assert.assertEquals(secondRecord.size(),firstRecord.size());
for(int i=0;i<firstRecord.size();i++){
if(!firstRecord.get(i).equals(secondRecord.get(i))){
Assert.assertFalse(true);
}
}
Assert.assertTrue(true);
}
@Test
public void insertionShouldBeReproducible(){
Service s = Service.Builder.newInstance("s1").setLocation(Location.newInstance(1,1)).build();
Service s2 = Service.Builder.newInstance("s2").setLocation(Location.newInstance(1,2)).build();
Service s3 = Service.Builder.newInstance("s3").setLocation(Location.newInstance(1,2)).build();
Service s4 = Service.Builder.newInstance("s4").setLocation(Location.newInstance(1,2)).build();
VehicleImpl v = VehicleImpl.Builder.newInstance("v").setStartLocation(Location.newInstance(0,0)).build();
VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addJob(s4).addJob(s3).addVehicle(v).addJob(s2).addJob(s).build();
RandomNumberGeneration.reset();
VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp);
vra.setMaxIterations(1000);
final List<String> firstRecord = new ArrayList<String>();
vra.addListener(new JobInsertedListener() {
@Override
public void informJobInserted(Job job2insert, VehicleRoute inRoute, double additionalCosts, double additionalTime) {
firstRecord.add(job2insert.getId());
}
});
vra.searchSolutions();
RandomNumberGeneration.reset();
VehicleRoutingAlgorithm second = Jsprit.createAlgorithm(vrp);
second.setMaxIterations(1000);
final List<String> secondRecord = new ArrayList<String>();
second.addListener(new JobInsertedListener() {
@Override
public void informJobInserted(Job job2insert, VehicleRoute inRoute, double additionalCosts, double additionalTime) {
secondRecord.add(job2insert.getId());
}
});
second.searchSolutions();
Assert.assertEquals(secondRecord.size(),firstRecord.size());
for(int i=0;i<firstRecord.size();i++){
if(!firstRecord.get(i).equals(secondRecord.get(i))){
Assert.assertFalse(true);
}
}
Assert.assertTrue(true);
}
@Test
public void whenBiggerProblem_insertionShouldBeReproducible(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/vrpnc1-jsprit-with-deliveries.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
RandomNumberGeneration.reset();
VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp);
vra.setMaxIterations(200);
final List<String> firstRecord = new ArrayList<String>();
vra.addListener(new JobInsertedListener() {
@Override
public void informJobInserted(Job job2insert, VehicleRoute inRoute, double additionalCosts, double additionalTime) {
firstRecord.add(job2insert.getId());
}
});
vra.searchSolutions();
RandomNumberGeneration.reset();
VehicleRoutingAlgorithm second = Jsprit.createAlgorithm(vrp);
second.setMaxIterations(200);
final List<String> secondRecord = new ArrayList<String>();
second.addListener(new JobInsertedListener() {
@Override
public void informJobInserted(Job job2insert, VehicleRoute inRoute, double additionalCosts, double additionalTime) {
secondRecord.add(job2insert.getId());
}
});
second.searchSolutions();
Assert.assertEquals(secondRecord.size(),firstRecord.size());
for(int i=0;i<firstRecord.size();i++){
if(!firstRecord.get(i).equals(secondRecord.get(i))){
Assert.assertFalse(true);
}
}
Assert.assertTrue(true);
}
@Test
public void whenBiggerProblem_insertionPositionsShouldBeReproducible(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/vrpnc1-jsprit-with-deliveries.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
RandomNumberGeneration.reset();
VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp);
vra.setMaxIterations(200);
final List<Integer> firstRecord = new ArrayList<Integer>();
vra.addListener(new BeforeJobInsertionListener() {
@Override
public void informBeforeJobInsertion(Job job, InsertionData data, VehicleRoute route) {
firstRecord.add(data.getDeliveryInsertionIndex());
}
});
Collection<VehicleRoutingProblemSolution> firstSolutions = vra.searchSolutions();
RandomNumberGeneration.reset();
VehicleRoutingAlgorithm second = Jsprit.createAlgorithm(vrp);
second.setMaxIterations(200);
final List<Integer> secondRecord = new ArrayList<Integer>();
second.addListener(new BeforeJobInsertionListener() {
@Override
public void informBeforeJobInsertion(Job job, InsertionData data, VehicleRoute route) {
secondRecord.add(data.getDeliveryInsertionIndex());
}
});
Collection<VehicleRoutingProblemSolution> secondSolutions = second.searchSolutions();
Assert.assertEquals(secondRecord.size(),firstRecord.size());
for(int i=0;i<firstRecord.size();i++){
if(!firstRecord.get(i).equals(secondRecord.get(i))){
Assert.assertFalse(true);
}
}
Assert.assertTrue(true);
Assert.assertEquals(Solutions.bestOf(firstSolutions).getCost(),Solutions.bestOf(secondSolutions).getCost());
}
@Test
public void whenTerminatingWithVariationCoefficient_terminationShouldBeReproducible(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/vrpnc1-jsprit-with-deliveries.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
RandomNumberGeneration.reset();
VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp);
vra.setMaxIterations(1000);
VariationCoefficientTermination termination = new VariationCoefficientTermination(50, 0.005);
vra.setPrematureAlgorithmTermination(termination);
vra.addListener(termination);
final List<Integer> firstRecord = new ArrayList<Integer>();
vra.addListener(new BeforeJobInsertionListener() {
@Override
public void informBeforeJobInsertion(Job job, InsertionData data, VehicleRoute route) {
firstRecord.add(data.getDeliveryInsertionIndex());
}
});
Collection<VehicleRoutingProblemSolution> firstSolutions = vra.searchSolutions();
RandomNumberGeneration.reset();
VehicleRoutingAlgorithm second = Jsprit.createAlgorithm(vrp);
VariationCoefficientTermination secondTermination = new VariationCoefficientTermination(50, 0.005);
second.setPrematureAlgorithmTermination(secondTermination);
second.addListener(secondTermination);
second.setMaxIterations(1000);
final List<Integer> secondRecord = new ArrayList<Integer>();
second.addListener(new BeforeJobInsertionListener() {
@Override
public void informBeforeJobInsertion(Job job, InsertionData data, VehicleRoute route) {
secondRecord.add(data.getDeliveryInsertionIndex());
}
});
Collection<VehicleRoutingProblemSolution> secondSolutions = second.searchSolutions();
Assert.assertEquals(secondRecord.size(),firstRecord.size());
for(int i=0;i<firstRecord.size();i++){
if(!firstRecord.get(i).equals(secondRecord.get(i))){
Assert.assertFalse(true);
}
}
Assert.assertTrue(true);
Assert.assertEquals(Solutions.bestOf(firstSolutions).getCost(),Solutions.bestOf(secondSolutions).getCost());
}
}

View file

@ -1,29 +0,0 @@
/*******************************************************************************
* Copyright (C) 2013 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.box;
import org.junit.Test;
public class TestSchrimpf {
@Test
public void whenUsingSchrimpfFactory_itFindsTheConfig(){
}
}