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

internals

This commit is contained in:
Stefan Schroeder 2013-10-09 14:12:47 +02:00
parent 05b883c034
commit fb365036af
15 changed files with 177 additions and 62 deletions

View file

@ -39,8 +39,10 @@ import basics.VehicleRoutingProblemSolution;
import basics.algo.IterationStartsListener;
import basics.algo.SearchStrategy;
import basics.algo.SearchStrategyManager;
import basics.algo.SolutionCostCalculator;
import basics.io.VrpXMLReader;
import basics.route.TourActivity;
import basics.route.VehicleRoute;
public class BuildCVRPAlgoFromScratchTest {
@ -73,11 +75,23 @@ public class BuildCVRPAlgoFromScratchTest {
RuinRadial radial = new RuinRadial(vrp, 0.15, new JobDistanceAvgCosts(vrp.getTransportCosts()));
RuinRandom random = new RuinRandom(vrp, 0.25);
SearchStrategy randomStrategy = new SearchStrategy(new SelectBest(), new AcceptNewIfBetterThanWorst(1));
SolutionCostCalculator solutionCostCalculator = new SolutionCostCalculator() {
@Override
public void calculateCosts(VehicleRoutingProblemSolution solution) {
double costs = 0.0;
for(VehicleRoute route : solution.getRoutes()){
costs += stateManager.getRouteState(route, StateTypes.COSTS).toDouble();
}
solution.setCost(costs);
}
};
SearchStrategy randomStrategy = new SearchStrategy(new SelectBest(), new AcceptNewIfBetterThanWorst(1), solutionCostCalculator);
RuinAndRecreateModule randomModule = new RuinAndRecreateModule("randomRuin_bestInsertion", bestInsertion, random);
randomStrategy.addModule(randomModule);
SearchStrategy radialStrategy = new SearchStrategy(new SelectBest(), new AcceptNewIfBetterThanWorst(1));
SearchStrategy radialStrategy = new SearchStrategy(new SelectBest(), new AcceptNewIfBetterThanWorst(1), solutionCostCalculator);
RuinAndRecreateModule radialModule = new RuinAndRecreateModule("radialRuin_bestInsertion", bestInsertion, radial);
radialStrategy.addModule(radialModule);
@ -101,7 +115,7 @@ public class BuildCVRPAlgoFromScratchTest {
vra.getSearchStrategyManager().addSearchStrategyModuleListener(new UpdateCostsAtRouteLevel(stateManager, vrp.getTransportCosts(), vrp.getActivityCosts()));
vra.getSearchStrategyManager().addSearchStrategyModuleListener(new UpdateLoadAtRouteLevel(stateManager));
VehicleRoutingProblemSolution iniSolution = new CreateInitialSolution(bestInsertion).createInitialSolution(vrp);
VehicleRoutingProblemSolution iniSolution = new CreateInitialSolution(bestInsertion, solutionCostCalculator).createInitialSolution(vrp);
// System.out.println("ini: costs="+iniSolution.getCost()+";#routes="+iniSolution.getRoutes().size());
vra.addInitialSolution(iniSolution);

View file

@ -48,6 +48,7 @@ import basics.algo.IterationStartsListener;
import basics.algo.JobInsertedListener;
import basics.algo.SearchStrategy;
import basics.algo.SearchStrategyManager;
import basics.algo.SolutionCostCalculator;
import basics.io.VrpXMLReader;
import basics.io.VrpXMLWriter;
import basics.route.DeliveryActivity;
@ -91,11 +92,23 @@ public class BuildPDVRPAlgoFromScratchTest {
RuinRadial radial = new RuinRadial(vrp, 0.15, new JobDistanceAvgCosts(vrp.getTransportCosts()));
RuinRandom random = new RuinRandom(vrp, 0.25);
SearchStrategy randomStrategy = new SearchStrategy(new SelectBest(), new AcceptNewIfBetterThanWorst(1));
SolutionCostCalculator solutionCostCalculator = new SolutionCostCalculator() {
@Override
public void calculateCosts(VehicleRoutingProblemSolution solution) {
double costs = 0.0;
for(VehicleRoute route : solution.getRoutes()){
costs += stateManager.getRouteState(route, StateTypes.COSTS).toDouble();
}
solution.setCost(costs);
}
};
SearchStrategy randomStrategy = new SearchStrategy(new SelectBest(), new AcceptNewIfBetterThanWorst(1), solutionCostCalculator);
RuinAndRecreateModule randomModule = new RuinAndRecreateModule("randomRuin_bestInsertion", bestInsertion, random);
randomStrategy.addModule(randomModule);
SearchStrategy radialStrategy = new SearchStrategy(new SelectBest(), new AcceptNewIfBetterThanWorst(1));
SearchStrategy radialStrategy = new SearchStrategy(new SelectBest(), new AcceptNewIfBetterThanWorst(1), solutionCostCalculator);
RuinAndRecreateModule radialModule = new RuinAndRecreateModule("radialRuin_bestInsertion", bestInsertion, radial);
radialStrategy.addModule(radialModule);
@ -172,7 +185,7 @@ public class BuildPDVRPAlgoFromScratchTest {
bestInsertion.addListener(loadVehicleInDepot);
bestInsertion.addListener(updateLoadAfterJobHasBeenInserted);
VehicleRoutingProblemSolution iniSolution = new CreateInitialSolution(bestInsertion).createInitialSolution(vrp);
VehicleRoutingProblemSolution iniSolution = new CreateInitialSolution(bestInsertion, solutionCostCalculator).createInitialSolution(vrp);
// System.out.println("ini: costs="+iniSolution.getCost()+";#routes="+iniSolution.getRoutes().size());
vra.addInitialSolution(iniSolution);

View file

@ -187,7 +187,7 @@ public class GendreauPostOptTest {
// routes.add(new VehicleRoute(getEmptyTour(),getDriver(),getNoVehicle()));
// routes.add(new VehicleRoute(getEmptyTour(),getDriver(),getNoVehicle()));
VehicleRoutingProblemSolution sol = new VehicleRoutingProblemSolution(routes, route.getCost());
VehicleRoutingProblemSolution sol = new VehicleRoutingProblemSolution(routes, states.getRouteState(route, StateTypes.COSTS).toDouble());
assertEquals(110.0, sol.getCost(), 0.5);
@ -202,12 +202,21 @@ public class GendreauPostOptTest {
postOpt.setFleetManager(fleetManager);
VehicleRoutingProblemSolution newSolution = postOpt.runAndGetSolution(sol);
newSolution.setCost(getCosts(newSolution,states));
assertEquals(2,RouteUtils.getNuOfActiveRoutes(newSolution.getRoutes()));
assertEquals(2,newSolution.getRoutes().size());
assertEquals(80.0,newSolution.getCost(),0.5);
}
private double getCosts(VehicleRoutingProblemSolution newSolution, StateManagerImpl states) {
double c = 0.0;
for(VehicleRoute r : newSolution.getRoutes()){
c += states.getRouteState(r, StateTypes.COSTS).toDouble();
}
return c;
}
@Test
public void whenPostOpt_optsRoutesWithMoreThanTwoJobs_oneRouteBecomesTwoRoutes(){
Collection<Job> jobs = new ArrayList<Job>();
@ -233,6 +242,7 @@ public class GendreauPostOptTest {
routes.add(route);
VehicleRoutingProblemSolution sol = new VehicleRoutingProblemSolution(routes, route.getCost());
sol.setCost(getCosts(sol,states));
assertEquals(110.0, sol.getCost(), 0.5);
@ -246,6 +256,7 @@ public class GendreauPostOptTest {
postOpt.setFleetManager(fleetManager);
// postOpt.setWithFix(withFixCost);
VehicleRoutingProblemSolution newSolution = postOpt.runAndGetSolution(sol);
newSolution.setCost(getCosts(newSolution,states));
assertEquals(2,RouteUtils.getNuOfActiveRoutes(newSolution.getRoutes()));
assertEquals(2,newSolution.getRoutes().size());

View file

@ -46,8 +46,9 @@ public class SearchStrategyTest {
public void whenANullModule_IsAdded_throwException(){
SolutionSelector select = mock(SolutionSelector.class);
SolutionAcceptor accept = mock(SolutionAcceptor.class);
SolutionCostCalculator calc = mock(SolutionCostCalculator.class);
SearchStrategy strat = new SearchStrategy(select, accept);
SearchStrategy strat = new SearchStrategy(select, accept, calc);
strat.addModule(null);
}
@ -56,6 +57,7 @@ public class SearchStrategyTest {
public void whenStratRunsWithOneModule_runItOnes(){
SolutionSelector select = mock(SolutionSelector.class);
SolutionAcceptor accept = mock(SolutionAcceptor.class);
SolutionCostCalculator calc = mock(SolutionCostCalculator.class);
final VehicleRoutingProblem vrp = mock(VehicleRoutingProblem.class);
final VehicleRoutingProblemSolution newSol = mock(VehicleRoutingProblemSolution.class);
@ -64,7 +66,7 @@ public class SearchStrategyTest {
final Collection<Integer> runs = new ArrayList<Integer>();
SearchStrategy strat = new SearchStrategy(select, accept);
SearchStrategy strat = new SearchStrategy(select, accept, calc);
SearchStrategyModule mod = new SearchStrategyModule() {
@Override
@ -96,6 +98,7 @@ public class SearchStrategyTest {
public void whenStratRunsWithTwoModule_runItTwice(){
SolutionSelector select = mock(SolutionSelector.class);
SolutionAcceptor accept = mock(SolutionAcceptor.class);
SolutionCostCalculator calc = mock(SolutionCostCalculator.class);
final VehicleRoutingProblem vrp = mock(VehicleRoutingProblem.class);
final VehicleRoutingProblemSolution newSol = mock(VehicleRoutingProblemSolution.class);
@ -104,7 +107,7 @@ public class SearchStrategyTest {
final Collection<Integer> runs = new ArrayList<Integer>();
SearchStrategy strat = new SearchStrategy(select, accept);
SearchStrategy strat = new SearchStrategy(select, accept, calc);
SearchStrategyModule mod = new SearchStrategyModule() {
@ -159,6 +162,7 @@ public class SearchStrategyTest {
public void whenStratRunsWithNModule_runItNTimes(){
SolutionSelector select = mock(SolutionSelector.class);
SolutionAcceptor accept = mock(SolutionAcceptor.class);
SolutionCostCalculator calc = mock(SolutionCostCalculator.class);
final VehicleRoutingProblem vrp = mock(VehicleRoutingProblem.class);
final VehicleRoutingProblemSolution newSol = mock(VehicleRoutingProblemSolution.class);
@ -169,7 +173,7 @@ public class SearchStrategyTest {
final Collection<Integer> runs = new ArrayList<Integer>();
SearchStrategy strat = new SearchStrategy(select, accept);
SearchStrategy strat = new SearchStrategy(select, accept, calc);
for(int i=0;i<N;i++){
SearchStrategyModule mod = new SearchStrategyModule() {
@ -203,6 +207,7 @@ public class SearchStrategyTest {
public void whenSelectorDeliversNullSolution_throwException(){
SolutionSelector select = mock(SolutionSelector.class);
SolutionAcceptor accept = mock(SolutionAcceptor.class);
SolutionCostCalculator calc = mock(SolutionCostCalculator.class);
final VehicleRoutingProblem vrp = mock(VehicleRoutingProblem.class);
@ -212,7 +217,7 @@ public class SearchStrategyTest {
final Collection<Integer> runs = new ArrayList<Integer>();
SearchStrategy strat = new SearchStrategy(select, accept);
SearchStrategy strat = new SearchStrategy(select, accept, calc);
for(int i=0;i<N;i++){
SearchStrategyModule mod = new SearchStrategyModule() {