From b7a35a26b019343e2486bacdd658a6da87fa8e9a Mon Sep 17 00:00:00 2001 From: oblonski <4sschroeder@gmail.com> Date: Sat, 22 Nov 2014 12:38:11 +0100 Subject: [PATCH] add according tests and change resources --- .../src/main/resources/algorithm_schema.xsd | 6 +- .../BuildCVRPAlgoFromScratch_IT.java | 4 +- .../BuildPDVRPAlgoFromScratch_IT.java | 14 +- ...dPDVRPWithShipmentsAlgoFromScratch_IT.java | 14 +- .../algorithm/SearchStrategyManagerTest.java | 136 +++++++++----- .../core/algorithm/SearchStrategyTest.java | 39 ++-- .../recreate/RegretInsertionTest.java | 174 ++++++++++++++++++ ...iteVrpWithInitialSolutionForWriterTest.xml | 81 ++++---- .../test/resources/infiniteWriterV2Test.xml | 29 ++- .../test/resources/lilim_algorithmConfig.xml | 21 ++- .../schrimpf_vehicleSwitchNotAllowed.xml | 21 ++- 11 files changed, 416 insertions(+), 123 deletions(-) create mode 100644 jsprit-core/src/test/java/jsprit/core/algorithm/recreate/RegretInsertionTest.java diff --git a/jsprit-core/src/main/resources/algorithm_schema.xsd b/jsprit-core/src/main/resources/algorithm_schema.xsd index ee2fc1f2..d8394b54 100644 --- a/jsprit-core/src/main/resources/algorithm_schema.xsd +++ b/jsprit-core/src/main/resources/algorithm_schema.xsd @@ -226,7 +226,7 @@ - + @@ -258,8 +258,8 @@ - - + + diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/BuildCVRPAlgoFromScratch_IT.java b/jsprit-core/src/test/java/jsprit/core/algorithm/BuildCVRPAlgoFromScratch_IT.java index edb34874..9a175cb3 100644 --- a/jsprit-core/src/test/java/jsprit/core/algorithm/BuildCVRPAlgoFromScratch_IT.java +++ b/jsprit-core/src/test/java/jsprit/core/algorithm/BuildCVRPAlgoFromScratch_IT.java @@ -87,11 +87,11 @@ public class BuildCVRPAlgoFromScratch_IT { } }; - SearchStrategy randomStrategy = new SearchStrategy(new SelectBest(), new GreedyAcceptance(1), solutionCostCalculator); + SearchStrategy randomStrategy = new SearchStrategy("random", new SelectBest(), new GreedyAcceptance(1), solutionCostCalculator); RuinAndRecreateModule randomModule = new RuinAndRecreateModule("randomRuin_bestInsertion", bestInsertion, random); randomStrategy.addModule(randomModule); - SearchStrategy radialStrategy = new SearchStrategy(new SelectBest(), new GreedyAcceptance(1), solutionCostCalculator); + SearchStrategy radialStrategy = new SearchStrategy("radial", new SelectBest(), new GreedyAcceptance(1), solutionCostCalculator); RuinAndRecreateModule radialModule = new RuinAndRecreateModule("radialRuin_bestInsertion", bestInsertion, radial); radialStrategy.addModule(radialModule); diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/BuildPDVRPAlgoFromScratch_IT.java b/jsprit-core/src/test/java/jsprit/core/algorithm/BuildPDVRPAlgoFromScratch_IT.java index 62a06e67..0e7cdc9f 100644 --- a/jsprit-core/src/test/java/jsprit/core/algorithm/BuildPDVRPAlgoFromScratch_IT.java +++ b/jsprit-core/src/test/java/jsprit/core/algorithm/BuildPDVRPAlgoFromScratch_IT.java @@ -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 . ******************************************************************************/ @@ -89,11 +89,11 @@ public class BuildPDVRPAlgoFromScratch_IT { } }; - SearchStrategy randomStrategy = new SearchStrategy(new SelectBest(), new GreedyAcceptance(1), solutionCostCalculator); + SearchStrategy randomStrategy = new SearchStrategy("random", new SelectBest(), new GreedyAcceptance(1), solutionCostCalculator); RuinAndRecreateModule randomModule = new RuinAndRecreateModule("randomRuin_bestInsertion", bestInsertion, random); randomStrategy.addModule(randomModule); - SearchStrategy radialStrategy = new SearchStrategy(new SelectBest(), new GreedyAcceptance(1), solutionCostCalculator); + SearchStrategy radialStrategy = new SearchStrategy("radial", new SelectBest(), new GreedyAcceptance(1), solutionCostCalculator); RuinAndRecreateModule radialModule = new RuinAndRecreateModule("radialRuin_bestInsertion", bestInsertion, radial); radialStrategy.addModule(radialModule); diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/BuildPDVRPWithShipmentsAlgoFromScratch_IT.java b/jsprit-core/src/test/java/jsprit/core/algorithm/BuildPDVRPWithShipmentsAlgoFromScratch_IT.java index 7659758c..1692d0cc 100644 --- a/jsprit-core/src/test/java/jsprit/core/algorithm/BuildPDVRPWithShipmentsAlgoFromScratch_IT.java +++ b/jsprit-core/src/test/java/jsprit/core/algorithm/BuildPDVRPWithShipmentsAlgoFromScratch_IT.java @@ -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 . ******************************************************************************/ @@ -83,11 +83,11 @@ public class BuildPDVRPWithShipmentsAlgoFromScratch_IT { } }; - SearchStrategy randomStrategy = new SearchStrategy(new SelectBest(), new GreedyAcceptance(1), solutionCostCalculator); + SearchStrategy randomStrategy = new SearchStrategy("random",new SelectBest(), new GreedyAcceptance(1), solutionCostCalculator); RuinAndRecreateModule randomModule = new RuinAndRecreateModule("randomRuin_bestInsertion", bestInsertion, random); randomStrategy.addModule(randomModule); - SearchStrategy radialStrategy = new SearchStrategy(new SelectBest(), new GreedyAcceptance(1), solutionCostCalculator); + SearchStrategy radialStrategy = new SearchStrategy("radial",new SelectBest(), new GreedyAcceptance(1), solutionCostCalculator); RuinAndRecreateModule radialModule = new RuinAndRecreateModule("radialRuin_bestInsertion", bestInsertion, radial); radialStrategy.addModule(radialModule); diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/SearchStrategyManagerTest.java b/jsprit-core/src/test/java/jsprit/core/algorithm/SearchStrategyManagerTest.java index daa426f4..e9c21840 100644 --- a/jsprit-core/src/test/java/jsprit/core/algorithm/SearchStrategyManagerTest.java +++ b/jsprit-core/src/test/java/jsprit/core/algorithm/SearchStrategyManagerTest.java @@ -1,34 +1,29 @@ /******************************************************************************* - * 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 . ******************************************************************************/ package jsprit.core.algorithm; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.stub; -import static org.mockito.Mockito.when; +import org.junit.Test; import java.util.Random; -import jsprit.core.algorithm.SearchStrategy; -import jsprit.core.algorithm.SearchStrategyManager; - -import org.junit.Test; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.*; @@ -39,6 +34,8 @@ public class SearchStrategyManagerTest { SearchStrategyManager manager = new SearchStrategyManager(); SearchStrategy strat1 = mock(SearchStrategy.class); SearchStrategy strat2 = mock(SearchStrategy.class); + when(strat1.getId()).thenReturn("strat1"); + when(strat2.getId()).thenReturn("strat2"); manager.addStrategy(strat1, 0.5); manager.addStrategy(strat2, 0.5); assertTrue(true); @@ -51,41 +48,87 @@ public class SearchStrategyManagerTest { assertTrue(false); } - @Test(expected=IllegalStateException.class) - public void StrategyManagerInAction_probabilityIsHigherThanOne_throwsException(){ - SearchStrategyManager manager = new SearchStrategyManager(); - SearchStrategy strat = mock(SearchStrategy.class); - manager.addStrategy(strat, 1.5); - assertTrue(false); - } - @Test(expected=IllegalStateException.class) public void StrategyManagerInAction_probabilityIsLowerThanZero_throwsException(){ SearchStrategyManager manager = new SearchStrategyManager(); SearchStrategy strat = mock(SearchStrategy.class); + when(strat.getId()).thenReturn("strat1"); manager.addStrategy(strat, -1.0); assertTrue(false); } - @Test(expected = IllegalStateException.class) - public void StrategyManagerInAction_addingSeveralStratsLeadsToAProbHigherThanOne_throwsException(){ + @Test + public void itShouldReturnStrategy2(){ SearchStrategyManager manager = new SearchStrategyManager(); SearchStrategy mockedStrat1 = mock(SearchStrategy.class); SearchStrategy mockedStrat2 = mock(SearchStrategy.class); - + when(mockedStrat1.getId()).thenReturn("strat1"); + when(mockedStrat2.getId()).thenReturn("strat2"); manager.addStrategy(mockedStrat1, 0.5); - manager.addStrategy(mockedStrat2, 0.6); + manager.addStrategy(mockedStrat2, 1.5); + + Random mockedRandom = mock(Random.class); + manager.setRandom(mockedRandom); + stub(mockedRandom.nextDouble()).toReturn(0.25); + + assertThat(manager.getRandomStrategy(), is(mockedStrat2)); } + + @Test + public void whenStratWeightChanged_itShouldReturnStrategy1(){ + SearchStrategyManager manager = new SearchStrategyManager(); + SearchStrategy mockedStrat1 = mock(SearchStrategy.class); + SearchStrategy mockedStrat2 = mock(SearchStrategy.class); + + when(mockedStrat1.getId()).thenReturn("strat1"); + when(mockedStrat2.getId()).thenReturn("strat2"); + + manager.addStrategy(mockedStrat1, 0.5); + manager.addStrategy(mockedStrat2, 1.5); + + Random mockedRandom = mock(Random.class); + manager.setRandom(mockedRandom); + stub(mockedRandom.nextDouble()).toReturn(0.25); + + assertThat(manager.getRandomStrategy(), is(mockedStrat2)); + + manager.informStrategyWeightChanged("strat2",1.4); + + assertThat(manager.getRandomStrategy(), is(mockedStrat1)); + } + + @Test + public void itShouldReturnStrategy1(){ + SearchStrategyManager manager = new SearchStrategyManager(); + SearchStrategy mockedStrat1 = mock(SearchStrategy.class); + SearchStrategy mockedStrat2 = mock(SearchStrategy.class); + + when(mockedStrat1.getId()).thenReturn("strat1"); + when(mockedStrat2.getId()).thenReturn("strat2"); + + manager.addStrategy(mockedStrat1, 0.5); + manager.addStrategy(mockedStrat2, 1.5); + + Random mockedRandom = mock(Random.class); + manager.setRandom(mockedRandom); + stub(mockedRandom.nextDouble()).toReturn(0.24); + + assertThat(manager.getRandomStrategy(), is(mockedStrat1)); + } @Test public void whenRandomDices_0point1_returnsStrategy1(){ SearchStrategyManager managerUnderTest = new SearchStrategyManager(); SearchStrategy mockedStrategy1 = mock(SearchStrategy.class); SearchStrategy mockedStrategy2 = mock(SearchStrategy.class); - managerUnderTest.addStrategy(mockedStrategy1, 0.2); - managerUnderTest.addStrategy(mockedStrategy2, 0.8); - - Random mockedRandom = mock(Random.class); + + when(mockedStrategy1.getId()).thenReturn("strat1"); + when(mockedStrategy2.getId()).thenReturn("strat2"); + + managerUnderTest.addStrategy(mockedStrategy1, 0.2); + managerUnderTest.addStrategy(mockedStrategy2, 0.8); + + Random mockedRandom = mock(Random.class); managerUnderTest.setRandom(mockedRandom); stub(mockedRandom.nextDouble()).toReturn(0.1); @@ -98,10 +141,14 @@ public class SearchStrategyManagerTest { SearchStrategyManager managerUnderTest = new SearchStrategyManager(); SearchStrategy mockedStrategy1 = mock(SearchStrategy.class); SearchStrategy mockedStrategy2 = mock(SearchStrategy.class); - managerUnderTest.addStrategy(mockedStrategy1, 0.2); - managerUnderTest.addStrategy(mockedStrategy2, 0.8); - - Random mockedRandom = mock(Random.class); + + when(mockedStrategy1.getId()).thenReturn("strat1"); + when(mockedStrategy2.getId()).thenReturn("strat2"); + + managerUnderTest.addStrategy(mockedStrategy1, 0.2); + managerUnderTest.addStrategy(mockedStrategy2, 0.8); + + Random mockedRandom = mock(Random.class); managerUnderTest.setRandom(mockedRandom); when(mockedRandom.nextDouble()).thenReturn(0.5); @@ -114,10 +161,14 @@ public class SearchStrategyManagerTest { SearchStrategyManager managerUnderTest = new SearchStrategyManager(); SearchStrategy mockedStrategy1 = mock(SearchStrategy.class); SearchStrategy mockedStrategy2 = mock(SearchStrategy.class); - managerUnderTest.addStrategy(mockedStrategy1, 0.2); - managerUnderTest.addStrategy(mockedStrategy2, 0.8); - - Random mockedRandom = mock(Random.class); + + when(mockedStrategy1.getId()).thenReturn("strat1"); + when(mockedStrategy2.getId()).thenReturn("strat2"); + + managerUnderTest.addStrategy(mockedStrategy1, 0.2); + managerUnderTest.addStrategy(mockedStrategy2, 0.8); + + Random mockedRandom = mock(Random.class); managerUnderTest.setRandom(mockedRandom); when(mockedRandom.nextDouble()).thenReturn(0.0); @@ -132,8 +183,11 @@ public class SearchStrategyManagerTest { SearchStrategy mockedStrategy2 = mock(SearchStrategy.class); managerUnderTest.addStrategy(mockedStrategy1, 0.2); managerUnderTest.addStrategy(mockedStrategy2, 0.8); - - Random mockedRandom = null; + + when(mockedStrategy1.getId()).thenReturn("strat1"); + when(mockedStrategy2.getId()).thenReturn("strat2"); + + Random mockedRandom = null; managerUnderTest.setRandom(mockedRandom); managerUnderTest.getRandomStrategy(); diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/SearchStrategyTest.java b/jsprit-core/src/test/java/jsprit/core/algorithm/SearchStrategyTest.java index 24e59e69..bfa7a5a4 100644 --- a/jsprit-core/src/test/java/jsprit/core/algorithm/SearchStrategyTest.java +++ b/jsprit-core/src/test/java/jsprit/core/algorithm/SearchStrategyTest.java @@ -1,40 +1,37 @@ /******************************************************************************* - * 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 . ******************************************************************************/ package jsprit.core.algorithm; -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Random; - -import jsprit.core.algorithm.SearchStrategy; -import jsprit.core.algorithm.SearchStrategyModule; import jsprit.core.algorithm.acceptor.SolutionAcceptor; import jsprit.core.algorithm.listener.SearchStrategyModuleListener; import jsprit.core.algorithm.selector.SolutionSelector; import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.solution.SolutionCostCalculator; import jsprit.core.problem.solution.VehicleRoutingProblemSolution; - import org.junit.Test; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Random; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + @@ -46,7 +43,7 @@ public class SearchStrategyTest { SolutionAcceptor accept = mock(SolutionAcceptor.class); SolutionCostCalculator calc = mock(SolutionCostCalculator.class); - SearchStrategy strat = new SearchStrategy(select, accept, calc); + SearchStrategy strat = new SearchStrategy("strat", select, accept, calc); strat.addModule(null); } @@ -64,7 +61,7 @@ public class SearchStrategyTest { final Collection runs = new ArrayList(); - SearchStrategy strat = new SearchStrategy(select, accept, calc); + SearchStrategy strat = new SearchStrategy("strat", select, accept, calc); SearchStrategyModule mod = new SearchStrategyModule() { @Override @@ -105,7 +102,7 @@ public class SearchStrategyTest { final Collection runs = new ArrayList(); - SearchStrategy strat = new SearchStrategy(select, accept, calc); + SearchStrategy strat = new SearchStrategy("strat", select, accept, calc); SearchStrategyModule mod = new SearchStrategyModule() { @@ -171,7 +168,7 @@ public class SearchStrategyTest { final Collection runs = new ArrayList(); - SearchStrategy strat = new SearchStrategy(select, accept, calc); + SearchStrategy strat = new SearchStrategy("strat", select, accept, calc); for(int i=0;i runs = new ArrayList(); - SearchStrategy strat = new SearchStrategy(select, accept, calc); + SearchStrategy strat = new SearchStrategy("strat", select, accept, calc); for(int i=0;i. + ******************************************************************************/ + +package jsprit.core.algorithm.recreate; + +import jsprit.core.algorithm.recreate.listener.BeforeJobInsertionListener; +import jsprit.core.problem.VehicleRoutingProblem; +import jsprit.core.problem.driver.Driver; +import jsprit.core.problem.job.Job; +import jsprit.core.problem.job.Service; +import jsprit.core.problem.solution.route.VehicleRoute; +import jsprit.core.problem.solution.route.activity.TourActivity; +import jsprit.core.problem.vehicle.Vehicle; +import jsprit.core.problem.vehicle.VehicleImpl; +import jsprit.core.util.Coordinate; +import junit.framework.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Collection; + +public class RegretInsertionTest { + + @Test + public void noRoutesShouldBeCorrect(){ + Service s1 = Service.Builder.newInstance("s1").setCoord(Coordinate.newInstance(0,10)).build(); + Service s2 = Service.Builder.newInstance("s2").setCoord(Coordinate.newInstance(0,5)).build(); + + VehicleImpl v = VehicleImpl.Builder.newInstance("v").setStartLocationCoordinate(Coordinate.newInstance(0,0)).build(); + VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addJob(s1).addJob(s2).addVehicle(v).build(); + + JobInsertionCostsCalculator calculator = getCalculator(vrp); + RegretInsertion regretInsertion = new RegretInsertion(calculator,vrp); + Collection routes = new ArrayList(); + + regretInsertion.insertJobs(routes,vrp.getJobs().values()); + Assert.assertEquals(1,routes.size()); + } + + @Test + public void noJobsInRouteShouldBeCorrect(){ + Service s1 = Service.Builder.newInstance("s1").setCoord(Coordinate.newInstance(0,10)).build(); + Service s2 = Service.Builder.newInstance("s2").setCoord(Coordinate.newInstance(0,5)).build(); + + VehicleImpl v = VehicleImpl.Builder.newInstance("v").setStartLocationCoordinate(Coordinate.newInstance(0,0)).build(); + VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addJob(s1).addJob(s2).addVehicle(v).build(); + + JobInsertionCostsCalculator calculator = getCalculator(vrp); + RegretInsertion regretInsertion = new RegretInsertion(calculator,vrp); + Collection routes = new ArrayList(); + + regretInsertion.insertJobs(routes,vrp.getJobs().values()); + Assert.assertEquals(2, routes.iterator().next().getActivities().size()); + } + + @Test + public void s1ShouldBeAddedFirst(){ + Service s1 = Service.Builder.newInstance("s1").setCoord(Coordinate.newInstance(0,10)).build(); + Service s2 = Service.Builder.newInstance("s2").setCoord(Coordinate.newInstance(0,5)).build(); + + VehicleImpl v = VehicleImpl.Builder.newInstance("v").setStartLocationCoordinate(Coordinate.newInstance(0,0)).build(); + final VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addJob(s1).addJob(s2).addVehicle(v).build(); + + JobInsertionCostsCalculator calculator = getCalculator(vrp); + RegretInsertion regretInsertion = new RegretInsertion(calculator,vrp); + Collection routes = new ArrayList(); + + CkeckJobSequence position = new CkeckJobSequence(1, s1); + regretInsertion.addListener(position); + regretInsertion.insertJobs(routes,vrp.getJobs().values()); + Assert.assertTrue(position.isCorrect()); + } + + + + static class CkeckJobSequence implements BeforeJobInsertionListener { + + int atPosition; + + Job job; + + int positionCounter = 1; + + boolean correct = false; + + CkeckJobSequence(int atPosition, Job job) { + this.atPosition = atPosition; + this.job = job; + } + + @Override + public void informBeforeJobInsertion(Job job, InsertionData data, VehicleRoute route) { + if(job == this.job && atPosition == positionCounter){ + correct = true; + } + positionCounter++; + } + + public boolean isCorrect() { + return correct; + } + } + + private JobInsertionCostsCalculator getCalculator(final VehicleRoutingProblem vrp) { + return new JobInsertionCostsCalculator() { + + @Override + public InsertionData getInsertionData(VehicleRoute currentRoute, Job newJob, Vehicle newVehicle, double newVehicleDepartureTime, Driver newDriver, double bestKnownCosts) { + Service service = (Service)newJob; + Vehicle vehicle = vrp.getVehicles().iterator().next(); + InsertionData iData = null; + if(currentRoute.isEmpty()){ + double mc = getCost(service.getLocationId(), vehicle.getStartLocationId()); + iData = new InsertionData(2*mc,-1,0,vehicle,newDriver); + } + else { + double best = Double.MAX_VALUE; + int bestIndex = 0; + int index = 0; + TourActivity prevAct = currentRoute.getStart(); + for (TourActivity act : currentRoute.getActivities()) { + double mc = getMarginalCost(service, prevAct, act); + if (mc < best) { + best = mc; + bestIndex = index; + } + index++; + prevAct = act; + } + double mc = getMarginalCost(service, prevAct, currentRoute.getEnd()); + if (mc < best) { + best = mc; + bestIndex = index; + } + iData = new InsertionData(best,-1,bestIndex,vehicle,newDriver); + } + return iData; + } + + private double getMarginalCost(Service service, TourActivity prevAct, TourActivity act) { + double prev_new = getCost(prevAct.getLocationId(),service.getLocationId()); + double new_act = getCost(service.getLocationId(),act.getLocationId()); + double prev_act = getCost(prevAct.getLocationId(),act.getLocationId()); + return prev_new + new_act - prev_act; + } + + private double getCost(String loc1, String loc2) { + return vrp.getTransportCosts().getTransportCost(loc1,loc2,0.,null,null); + } + }; + +// LocalActivityInsertionCostsCalculator local = new LocalActivityInsertionCostsCalculator(vrp.getTransportCosts(),vrp.getActivityCosts()); +// StateManager stateManager = new StateManager(vrp); +// ConstraintManager manager = new ConstraintManager(vrp,stateManager); +// ServiceInsertionCalculator calculator = new ServiceInsertionCalculator(vrp.getTransportCosts(), local, manager); +// calculator.setJobActivityFactory(vrp.getJobActivityFactory()); +// return calculator; + } + +} diff --git a/jsprit-core/src/test/resources/finiteVrpWithInitialSolutionForWriterTest.xml b/jsprit-core/src/test/resources/finiteVrpWithInitialSolutionForWriterTest.xml index e5348fbc..63166fc5 100644 --- a/jsprit-core/src/test/resources/finiteVrpWithInitialSolutionForWriterTest.xml +++ b/jsprit-core/src/test/resources/finiteVrpWithInitialSolutionForWriterTest.xml @@ -1,27 +1,27 @@ + + FINITE - - v3 - vehType2 - - startLoc - - - - endLoc - - - - 0.0 - 1000.0 - - true - v2 vehType2 @@ -40,7 +40,7 @@ false - v4 + v3 vehType2 startLoc @@ -73,6 +73,23 @@ true + + v4 + vehType2 + + startLoc + + + + endLoc + + + + 0.0 + 1000.0 + + true + v1 vehType @@ -137,20 +154,6 @@ - - j(1,5) - - - 1 - - 10.0 - - - 0.0 - 4000.0 - - - i(3,9) @@ -165,6 +168,20 @@ + + j(1,5) + + + 1 + + 10.0 + + + 0.0 + 4000.0 + + + diff --git a/jsprit-core/src/test/resources/infiniteWriterV2Test.xml b/jsprit-core/src/test/resources/infiniteWriterV2Test.xml index f0c10e9e..01a27b53 100644 --- a/jsprit-core/src/test/resources/infiniteWriterV2Test.xml +++ b/jsprit-core/src/test/resources/infiniteWriterV2Test.xml @@ -1,4 +1,21 @@ + + @@ -35,12 +52,12 @@ - - loc + + loc2 1 - 2.0 + 4.0 0.0 @@ -48,12 +65,12 @@ - - loc2 + + loc 1 - 4.0 + 2.0 0.0 diff --git a/jsprit-core/src/test/resources/lilim_algorithmConfig.xml b/jsprit-core/src/test/resources/lilim_algorithmConfig.xml index 03b61a9c..8466fb31 100644 --- a/jsprit-core/src/test/resources/lilim_algorithmConfig.xml +++ b/jsprit-core/src/test/resources/lilim_algorithmConfig.xml @@ -1,5 +1,22 @@ + + @@ -13,7 +30,7 @@ 1 - + 0.1 @@ -31,7 +48,7 @@ 0.5 - + diff --git a/jsprit-core/src/test/resources/schrimpf_vehicleSwitchNotAllowed.xml b/jsprit-core/src/test/resources/schrimpf_vehicleSwitchNotAllowed.xml index b870f24d..c2799761 100644 --- a/jsprit-core/src/test/resources/schrimpf_vehicleSwitchNotAllowed.xml +++ b/jsprit-core/src/test/resources/schrimpf_vehicleSwitchNotAllowed.xml @@ -1,5 +1,22 @@ + + @@ -14,7 +31,7 @@ 1 - + 0.1 @@ -32,7 +49,7 @@ 0.5 - +