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

add according tests and change resources

This commit is contained in:
oblonski 2014-11-22 12:38:11 +01:00
parent 64b5d77267
commit b7a35a26b0
11 changed files with 416 additions and 123 deletions

View file

@ -226,7 +226,7 @@
<xs:complexType name="insertionType"> <xs:complexType name="insertionType">
<xs:sequence> <xs:all>
<xs:element name="level" minOccurs="0" maxOccurs="1"> <xs:element name="level" minOccurs="0" maxOccurs="1">
<xs:complexType> <xs:complexType>
<xs:simpleContent> <xs:simpleContent>
@ -258,8 +258,8 @@
</xs:complexType> </xs:complexType>
</xs:element> </xs:element>
</xs:sequence> </xs:all>
<xs:attribute name="name" use="required"> <xs:attribute name="name" use="required">
<xs:simpleType> <xs:simpleType>
<xs:restriction base="xs:string"> <xs:restriction base="xs:string">

View file

@ -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); RuinAndRecreateModule randomModule = new RuinAndRecreateModule("randomRuin_bestInsertion", bestInsertion, random);
randomStrategy.addModule(randomModule); 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); RuinAndRecreateModule radialModule = new RuinAndRecreateModule("radialRuin_bestInsertion", bestInsertion, radial);
radialStrategy.addModule(radialModule); radialStrategy.addModule(radialModule);

View file

@ -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/>.
******************************************************************************/ ******************************************************************************/
@ -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); RuinAndRecreateModule randomModule = new RuinAndRecreateModule("randomRuin_bestInsertion", bestInsertion, random);
randomStrategy.addModule(randomModule); 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); RuinAndRecreateModule radialModule = new RuinAndRecreateModule("radialRuin_bestInsertion", bestInsertion, radial);
radialStrategy.addModule(radialModule); radialStrategy.addModule(radialModule);

View file

@ -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/>.
******************************************************************************/ ******************************************************************************/
@ -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); RuinAndRecreateModule randomModule = new RuinAndRecreateModule("randomRuin_bestInsertion", bestInsertion, random);
randomStrategy.addModule(randomModule); 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); RuinAndRecreateModule radialModule = new RuinAndRecreateModule("radialRuin_bestInsertion", bestInsertion, radial);
radialStrategy.addModule(radialModule); radialStrategy.addModule(radialModule);

View file

@ -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 * 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/>.
******************************************************************************/ ******************************************************************************/
package jsprit.core.algorithm; package jsprit.core.algorithm;
import static org.hamcrest.CoreMatchers.is; import org.junit.Test;
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 java.util.Random; import java.util.Random;
import jsprit.core.algorithm.SearchStrategy; import static org.hamcrest.CoreMatchers.is;
import jsprit.core.algorithm.SearchStrategyManager; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import org.junit.Test; import static org.mockito.Mockito.*;
@ -39,6 +34,8 @@ public class SearchStrategyManagerTest {
SearchStrategyManager manager = new SearchStrategyManager(); SearchStrategyManager manager = new SearchStrategyManager();
SearchStrategy strat1 = mock(SearchStrategy.class); SearchStrategy strat1 = mock(SearchStrategy.class);
SearchStrategy strat2 = 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(strat1, 0.5);
manager.addStrategy(strat2, 0.5); manager.addStrategy(strat2, 0.5);
assertTrue(true); assertTrue(true);
@ -51,41 +48,87 @@ public class SearchStrategyManagerTest {
assertTrue(false); 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) @Test(expected=IllegalStateException.class)
public void StrategyManagerInAction_probabilityIsLowerThanZero_throwsException(){ public void StrategyManagerInAction_probabilityIsLowerThanZero_throwsException(){
SearchStrategyManager manager = new SearchStrategyManager(); SearchStrategyManager manager = new SearchStrategyManager();
SearchStrategy strat = mock(SearchStrategy.class); SearchStrategy strat = mock(SearchStrategy.class);
when(strat.getId()).thenReturn("strat1");
manager.addStrategy(strat, -1.0); manager.addStrategy(strat, -1.0);
assertTrue(false); assertTrue(false);
} }
@Test(expected = IllegalStateException.class) @Test
public void StrategyManagerInAction_addingSeveralStratsLeadsToAProbHigherThanOne_throwsException(){ public void itShouldReturnStrategy2(){
SearchStrategyManager manager = new SearchStrategyManager(); SearchStrategyManager manager = new SearchStrategyManager();
SearchStrategy mockedStrat1 = mock(SearchStrategy.class); SearchStrategy mockedStrat1 = mock(SearchStrategy.class);
SearchStrategy mockedStrat2 = 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(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 @Test
public void whenRandomDices_0point1_returnsStrategy1(){ public void whenRandomDices_0point1_returnsStrategy1(){
SearchStrategyManager managerUnderTest = new SearchStrategyManager(); SearchStrategyManager managerUnderTest = new SearchStrategyManager();
SearchStrategy mockedStrategy1 = mock(SearchStrategy.class); SearchStrategy mockedStrategy1 = mock(SearchStrategy.class);
SearchStrategy mockedStrategy2 = mock(SearchStrategy.class); SearchStrategy mockedStrategy2 = mock(SearchStrategy.class);
managerUnderTest.addStrategy(mockedStrategy1, 0.2);
managerUnderTest.addStrategy(mockedStrategy2, 0.8); when(mockedStrategy1.getId()).thenReturn("strat1");
when(mockedStrategy2.getId()).thenReturn("strat2");
Random mockedRandom = mock(Random.class);
managerUnderTest.addStrategy(mockedStrategy1, 0.2);
managerUnderTest.addStrategy(mockedStrategy2, 0.8);
Random mockedRandom = mock(Random.class);
managerUnderTest.setRandom(mockedRandom); managerUnderTest.setRandom(mockedRandom);
stub(mockedRandom.nextDouble()).toReturn(0.1); stub(mockedRandom.nextDouble()).toReturn(0.1);
@ -98,10 +141,14 @@ public class SearchStrategyManagerTest {
SearchStrategyManager managerUnderTest = new SearchStrategyManager(); SearchStrategyManager managerUnderTest = new SearchStrategyManager();
SearchStrategy mockedStrategy1 = mock(SearchStrategy.class); SearchStrategy mockedStrategy1 = mock(SearchStrategy.class);
SearchStrategy mockedStrategy2 = mock(SearchStrategy.class); SearchStrategy mockedStrategy2 = mock(SearchStrategy.class);
managerUnderTest.addStrategy(mockedStrategy1, 0.2);
managerUnderTest.addStrategy(mockedStrategy2, 0.8); when(mockedStrategy1.getId()).thenReturn("strat1");
when(mockedStrategy2.getId()).thenReturn("strat2");
Random mockedRandom = mock(Random.class);
managerUnderTest.addStrategy(mockedStrategy1, 0.2);
managerUnderTest.addStrategy(mockedStrategy2, 0.8);
Random mockedRandom = mock(Random.class);
managerUnderTest.setRandom(mockedRandom); managerUnderTest.setRandom(mockedRandom);
when(mockedRandom.nextDouble()).thenReturn(0.5); when(mockedRandom.nextDouble()).thenReturn(0.5);
@ -114,10 +161,14 @@ public class SearchStrategyManagerTest {
SearchStrategyManager managerUnderTest = new SearchStrategyManager(); SearchStrategyManager managerUnderTest = new SearchStrategyManager();
SearchStrategy mockedStrategy1 = mock(SearchStrategy.class); SearchStrategy mockedStrategy1 = mock(SearchStrategy.class);
SearchStrategy mockedStrategy2 = mock(SearchStrategy.class); SearchStrategy mockedStrategy2 = mock(SearchStrategy.class);
managerUnderTest.addStrategy(mockedStrategy1, 0.2);
managerUnderTest.addStrategy(mockedStrategy2, 0.8); when(mockedStrategy1.getId()).thenReturn("strat1");
when(mockedStrategy2.getId()).thenReturn("strat2");
Random mockedRandom = mock(Random.class);
managerUnderTest.addStrategy(mockedStrategy1, 0.2);
managerUnderTest.addStrategy(mockedStrategy2, 0.8);
Random mockedRandom = mock(Random.class);
managerUnderTest.setRandom(mockedRandom); managerUnderTest.setRandom(mockedRandom);
when(mockedRandom.nextDouble()).thenReturn(0.0); when(mockedRandom.nextDouble()).thenReturn(0.0);
@ -132,8 +183,11 @@ public class SearchStrategyManagerTest {
SearchStrategy mockedStrategy2 = mock(SearchStrategy.class); SearchStrategy mockedStrategy2 = mock(SearchStrategy.class);
managerUnderTest.addStrategy(mockedStrategy1, 0.2); managerUnderTest.addStrategy(mockedStrategy1, 0.2);
managerUnderTest.addStrategy(mockedStrategy2, 0.8); 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.setRandom(mockedRandom);
managerUnderTest.getRandomStrategy(); managerUnderTest.getRandomStrategy();

View file

@ -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 * 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/>.
******************************************************************************/ ******************************************************************************/
package jsprit.core.algorithm; 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.acceptor.SolutionAcceptor;
import jsprit.core.algorithm.listener.SearchStrategyModuleListener; import jsprit.core.algorithm.listener.SearchStrategyModuleListener;
import jsprit.core.algorithm.selector.SolutionSelector; import jsprit.core.algorithm.selector.SolutionSelector;
import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.solution.SolutionCostCalculator; import jsprit.core.problem.solution.SolutionCostCalculator;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution; import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import org.junit.Test; 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); SolutionAcceptor accept = mock(SolutionAcceptor.class);
SolutionCostCalculator calc = mock(SolutionCostCalculator.class); SolutionCostCalculator calc = mock(SolutionCostCalculator.class);
SearchStrategy strat = new SearchStrategy(select, accept, calc); SearchStrategy strat = new SearchStrategy("strat", select, accept, calc);
strat.addModule(null); strat.addModule(null);
} }
@ -64,7 +61,7 @@ public class SearchStrategyTest {
final Collection<Integer> runs = new ArrayList<Integer>(); final Collection<Integer> runs = new ArrayList<Integer>();
SearchStrategy strat = new SearchStrategy(select, accept, calc); SearchStrategy strat = new SearchStrategy("strat", select, accept, calc);
SearchStrategyModule mod = new SearchStrategyModule() { SearchStrategyModule mod = new SearchStrategyModule() {
@Override @Override
@ -105,7 +102,7 @@ public class SearchStrategyTest {
final Collection<Integer> runs = new ArrayList<Integer>(); final Collection<Integer> runs = new ArrayList<Integer>();
SearchStrategy strat = new SearchStrategy(select, accept, calc); SearchStrategy strat = new SearchStrategy("strat", select, accept, calc);
SearchStrategyModule mod = new SearchStrategyModule() { SearchStrategyModule mod = new SearchStrategyModule() {
@ -171,7 +168,7 @@ public class SearchStrategyTest {
final Collection<Integer> runs = new ArrayList<Integer>(); final Collection<Integer> runs = new ArrayList<Integer>();
SearchStrategy strat = new SearchStrategy(select, accept, calc); SearchStrategy strat = new SearchStrategy("strat", select, accept, calc);
for(int i=0;i<N;i++){ for(int i=0;i<N;i++){
SearchStrategyModule mod = new SearchStrategyModule() { SearchStrategyModule mod = new SearchStrategyModule() {
@ -215,7 +212,7 @@ public class SearchStrategyTest {
final Collection<Integer> runs = new ArrayList<Integer>(); final Collection<Integer> runs = new ArrayList<Integer>();
SearchStrategy strat = new SearchStrategy(select, accept, calc); SearchStrategy strat = new SearchStrategy("strat", select, accept, calc);
for(int i=0;i<N;i++){ for(int i=0;i<N;i++){
SearchStrategyModule mod = new SearchStrategyModule() { SearchStrategyModule mod = new SearchStrategyModule() {

View file

@ -0,0 +1,174 @@
/*******************************************************************************
* 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.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<VehicleRoute> routes = new ArrayList<VehicleRoute>();
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<VehicleRoute> routes = new ArrayList<VehicleRoute>();
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<VehicleRoute> routes = new ArrayList<VehicleRoute>();
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;
}
}

View file

@ -1,27 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ 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/>.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<problem xmlns="http://www.w3schools.com" <problem xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com vrp_xml_schema.xsd"> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com vrp_xml_schema.xsd">
<problemType> <problemType>
<fleetSize>FINITE</fleetSize> <fleetSize>FINITE</fleetSize>
</problemType> </problemType>
<vehicles> <vehicles>
<vehicle>
<id>v3</id>
<typeId>vehType2</typeId>
<startLocation>
<id>startLoc</id>
<coord x="10.0" y="100.0"/>
</startLocation>
<endLocation>
<id>endLoc</id>
<coord x="1000.0" y="2000.0"/>
</endLocation>
<timeSchedule>
<start>0.0</start>
<end>1000.0</end>
</timeSchedule>
<returnToDepot>true</returnToDepot>
</vehicle>
<vehicle> <vehicle>
<id>v2</id> <id>v2</id>
<typeId>vehType2</typeId> <typeId>vehType2</typeId>
@ -40,7 +40,7 @@
<returnToDepot>false</returnToDepot> <returnToDepot>false</returnToDepot>
</vehicle> </vehicle>
<vehicle> <vehicle>
<id>v4</id> <id>v3</id>
<typeId>vehType2</typeId> <typeId>vehType2</typeId>
<startLocation> <startLocation>
<id>startLoc</id> <id>startLoc</id>
@ -73,6 +73,23 @@
</timeSchedule> </timeSchedule>
<returnToDepot>true</returnToDepot> <returnToDepot>true</returnToDepot>
</vehicle> </vehicle>
<vehicle>
<id>v4</id>
<typeId>vehType2</typeId>
<startLocation>
<id>startLoc</id>
<coord x="10.0" y="100.0"/>
</startLocation>
<endLocation>
<id>endLoc</id>
<coord x="1000.0" y="2000.0"/>
</endLocation>
<timeSchedule>
<start>0.0</start>
<end>1000.0</end>
</timeSchedule>
<returnToDepot>true</returnToDepot>
</vehicle>
<vehicle> <vehicle>
<id>v1</id> <id>v1</id>
<typeId>vehType</typeId> <typeId>vehType</typeId>
@ -137,20 +154,6 @@
</type> </type>
</vehicleTypes> </vehicleTypes>
<services> <services>
<service id="1" type="service">
<locationId>j(1,5)</locationId>
<coord x="10.0" y="10.0"/>
<capacity-dimensions>
<dimension index="0">1</dimension>
</capacity-dimensions>
<duration>10.0</duration>
<timeWindows>
<timeWindow>
<start>0.0</start>
<end>4000.0</end>
</timeWindow>
</timeWindows>
</service>
<service id="2" type="service"> <service id="2" type="service">
<locationId>i(3,9)</locationId> <locationId>i(3,9)</locationId>
<coord x="10.0" y="10.0"/> <coord x="10.0" y="10.0"/>
@ -165,6 +168,20 @@
</timeWindow> </timeWindow>
</timeWindows> </timeWindows>
</service> </service>
<service id="1" type="service">
<locationId>j(1,5)</locationId>
<coord x="10.0" y="10.0"/>
<capacity-dimensions>
<dimension index="0">1</dimension>
</capacity-dimensions>
<duration>10.0</duration>
<timeWindows>
<timeWindow>
<start>0.0</start>
<end>4000.0</end>
</timeWindow>
</timeWindows>
</service>
</services> </services>
<shipments> <shipments>
<shipment id="3"> <shipment id="3">

View file

@ -1,4 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ 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/>.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<problem xmlns="http://www.w3schools.com" <problem xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com vrp_xml_schema.xsd"> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com vrp_xml_schema.xsd">
<problemType> <problemType>
@ -35,12 +52,12 @@
</type> </type>
</vehicleTypes> </vehicleTypes>
<services> <services>
<service id="1" type="service"> <service id="2" type="service">
<locationId>loc</locationId> <locationId>loc2</locationId>
<capacity-dimensions> <capacity-dimensions>
<dimension index="0">1</dimension> <dimension index="0">1</dimension>
</capacity-dimensions> </capacity-dimensions>
<duration>2.0</duration> <duration>4.0</duration>
<timeWindows> <timeWindows>
<timeWindow> <timeWindow>
<start>0.0</start> <start>0.0</start>
@ -48,12 +65,12 @@
</timeWindow> </timeWindow>
</timeWindows> </timeWindows>
</service> </service>
<service id="2" type="service"> <service id="1" type="service">
<locationId>loc2</locationId> <locationId>loc</locationId>
<capacity-dimensions> <capacity-dimensions>
<dimension index="0">1</dimension> <dimension index="0">1</dimension>
</capacity-dimensions> </capacity-dimensions>
<duration>4.0</duration> <duration>2.0</duration>
<timeWindows> <timeWindows>
<timeWindow> <timeWindow>
<start>0.0</start> <start>0.0</start>

View file

@ -1,5 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ 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/>.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<algorithm xmlns="http://www.w3schools.com" <algorithm xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com algorithm_schema.xsd"> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com algorithm_schema.xsd">
@ -13,7 +30,7 @@
<strategy> <strategy>
<memory>1</memory> <memory>1</memory>
<searchStrategies> <searchStrategies>
<searchStrategy name="radialRuinAndRecreate"> <searchStrategy name="randomStrategy">
<selector name="selectBest"/> <selector name="selectBest"/>
<acceptor name="schrimpfAcceptance"> <acceptor name="schrimpfAcceptance">
<alpha>0.1</alpha> <alpha>0.1</alpha>
@ -31,7 +48,7 @@
<probability>0.5</probability> <probability>0.5</probability>
</searchStrategy> </searchStrategy>
<searchStrategy name="radialRuinAndRecreate"> <searchStrategy name="radialStrategy">
<selector name="selectBest"/> <selector name="selectBest"/>
<acceptor name="schrimpfAcceptance"/> <acceptor name="schrimpfAcceptance"/>

View file

@ -1,5 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ 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/>.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<algorithm xmlns="http://www.w3schools.com" <algorithm xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com algorithm_schema.xsd"> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com algorithm_schema.xsd">
@ -14,7 +31,7 @@
<strategy> <strategy>
<memory>1</memory> <memory>1</memory>
<searchStrategies> <searchStrategies>
<searchStrategy name="radialRuinAndRecreate"> <searchStrategy name="randomStrategy">
<selector name="selectBest"/> <selector name="selectBest"/>
<acceptor name="schrimpfAcceptance"> <acceptor name="schrimpfAcceptance">
<alpha>0.1</alpha> <alpha>0.1</alpha>
@ -32,7 +49,7 @@
<probability>0.5</probability> <probability>0.5</probability>
</searchStrategy> </searchStrategy>
<searchStrategy name="radialRuinAndRecreate"> <searchStrategy name="radialStrategy">
<selector name="selectBest"/> <selector name="selectBest"/>
<acceptor name="schrimpfAcceptance"/> <acceptor name="schrimpfAcceptance"/>