mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
removed constructor from StateManager and its according implications
This commit is contained in:
parent
4d3e5be4fb
commit
279826f061
10 changed files with 99 additions and 135 deletions
|
|
@ -161,15 +161,6 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
|
|||
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public StateManager(VehicleRoutingTransportCosts routingCosts){
|
||||
this.routingCosts = routingCosts;
|
||||
nuActivities = 101;
|
||||
activity_states = new Object[nuActivities][initialNuStates];
|
||||
route_states = new Object[nuActivities][initialNuStates];
|
||||
addDefaultStates();
|
||||
}
|
||||
|
||||
public StateManager(VehicleRoutingProblem vehicleRoutingProblem){
|
||||
this.routingCosts = vehicleRoutingProblem.getTransportCosts();
|
||||
this.vrp = vehicleRoutingProblem;
|
||||
|
|
@ -243,16 +234,14 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
|
|||
*
|
||||
*/
|
||||
public void clear(){
|
||||
fill(activity_states, null);
|
||||
fill(route_states, null);
|
||||
fill_(vehicle_dependent_activity_states, null);
|
||||
fill_(vehicle_dependent_route_states,null);
|
||||
// vehicle_dependent_activity_states = new Object[nuActivities][nuVehicleTypeKeys][initialNuStates];
|
||||
// vehicle_dependent_route_states = new Object[nuActivities][nuVehicleTypeKeys][initialNuStates];
|
||||
fill_twoDimArr(activity_states, null);
|
||||
fill_twoDimArr(route_states, null);
|
||||
fill_threeDimArr(vehicle_dependent_activity_states, null);
|
||||
fill_threeDimArr(vehicle_dependent_route_states, null);
|
||||
problemStates_.clear();
|
||||
}
|
||||
|
||||
private void fill_(Object[][][] states, Object o) {
|
||||
private void fill_threeDimArr(Object[][][] states, Object o) {
|
||||
for(Object[][] twoDimArr : states){
|
||||
for(Object[] oneDimArr : twoDimArr){
|
||||
Arrays.fill(oneDimArr,o);
|
||||
|
|
@ -260,7 +249,7 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
|
|||
}
|
||||
}
|
||||
|
||||
private void fill(Object[][] states, Object o) {
|
||||
private void fill_twoDimArr(Object[][] states, Object o) {
|
||||
for(Object[] rows : states){
|
||||
Arrays.fill(rows,o);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,8 +36,6 @@ import jsprit.core.problem.solution.route.VehicleRoute;
|
|||
import jsprit.core.problem.solution.route.state.StateFactory;
|
||||
import jsprit.core.problem.vehicle.InfiniteFleetManagerFactory;
|
||||
import jsprit.core.problem.vehicle.VehicleFleetManager;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collection;
|
||||
|
|
@ -46,80 +44,69 @@ import static org.junit.Assert.assertTrue;
|
|||
|
||||
|
||||
public class BuildPDVRPWithShipmentsAlgoFromScratch_IT {
|
||||
|
||||
VehicleRoutingProblem vrp;
|
||||
|
||||
VehicleRoutingAlgorithm vra;
|
||||
|
||||
static Logger log = Logger.getLogger(BuildPDVRPWithShipmentsAlgoFromScratch_IT.class);
|
||||
|
||||
@Before
|
||||
public void setup(){
|
||||
|
||||
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
|
||||
new VrpXMLReader(builder).read("src/test/resources/pdp.xml");
|
||||
|
||||
vrp = builder.build();
|
||||
|
||||
final StateManager stateManager = new StateManager(vrp);
|
||||
stateManager.updateLoadStates();
|
||||
stateManager.updateTimeWindowStates();
|
||||
stateManager.addStateUpdater(new UpdateVariableCosts(vrp.getActivityCosts(), vrp.getTransportCosts(), stateManager));
|
||||
|
||||
ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager);
|
||||
constraintManager.addTimeWindowConstraint();
|
||||
constraintManager.addLoadConstraint();
|
||||
|
||||
VehicleFleetManager fleetManager = new InfiniteFleetManagerFactory(vrp.getVehicles()).createFleetManager();
|
||||
|
||||
BestInsertionBuilder bestIBuilder = new BestInsertionBuilder(vrp, fleetManager, stateManager,constraintManager);
|
||||
InsertionStrategy bestInsertion = bestIBuilder.build();
|
||||
|
||||
|
||||
RuinStrategy radial = new RadialRuinStrategyFactory( 0.3, new AvgServiceAndShipmentDistance(vrp.getTransportCosts())).createStrategy(vrp);
|
||||
RuinStrategy random = new RandomRuinStrategyFactory(0.5).createStrategy(vrp);
|
||||
|
||||
|
||||
SolutionCostCalculator solutionCostCalculator = new SolutionCostCalculator() {
|
||||
|
||||
@Override
|
||||
public double getCosts(VehicleRoutingProblemSolution solution) {
|
||||
double costs = 0.0;
|
||||
for(VehicleRoute route : solution.getRoutes()){
|
||||
costs += stateManager.getRouteState(route, StateFactory.COSTS, Double.class);
|
||||
}
|
||||
return costs;
|
||||
}
|
||||
};
|
||||
|
||||
SearchStrategy randomStrategy = new SearchStrategy(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);
|
||||
RuinAndRecreateModule radialModule = new RuinAndRecreateModule("radialRuin_bestInsertion", bestInsertion, radial);
|
||||
radialStrategy.addModule(radialModule);
|
||||
|
||||
SearchStrategyManager strategyManager = new SearchStrategyManager();
|
||||
strategyManager.addStrategy(radialStrategy, 0.5);
|
||||
strategyManager.addStrategy(randomStrategy, 0.5);
|
||||
|
||||
vra = new VehicleRoutingAlgorithm(vrp, strategyManager);
|
||||
vra.addListener(stateManager);
|
||||
vra.addListener(new RemoveEmptyVehicles(fleetManager));
|
||||
|
||||
VehicleRoutingProblemSolution iniSolution = new InsertionInitialSolutionFactory(bestInsertion, solutionCostCalculator).createSolution(vrp);
|
||||
vra.addInitialSolution(iniSolution);
|
||||
|
||||
vra.setNuOfIterations(3);
|
||||
// vra.setPrematureBreak(500);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test(){
|
||||
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
|
||||
new VrpXMLReader(builder).read("src/test/resources/pdp.xml");
|
||||
|
||||
VehicleRoutingProblem vrp = builder.build();
|
||||
|
||||
final StateManager stateManager = new StateManager(vrp);
|
||||
stateManager.updateLoadStates();
|
||||
stateManager.updateTimeWindowStates();
|
||||
stateManager.addStateUpdater(new UpdateVariableCosts(vrp.getActivityCosts(), vrp.getTransportCosts(), stateManager));
|
||||
|
||||
ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager);
|
||||
constraintManager.addTimeWindowConstraint();
|
||||
constraintManager.addLoadConstraint();
|
||||
|
||||
VehicleFleetManager fleetManager = new InfiniteFleetManagerFactory(vrp.getVehicles()).createFleetManager();
|
||||
|
||||
BestInsertionBuilder bestIBuilder = new BestInsertionBuilder(vrp, fleetManager, stateManager,constraintManager);
|
||||
InsertionStrategy bestInsertion = bestIBuilder.build();
|
||||
|
||||
|
||||
RuinStrategy radial = new RadialRuinStrategyFactory( 0.3, new AvgServiceAndShipmentDistance(vrp.getTransportCosts())).createStrategy(vrp);
|
||||
RuinStrategy random = new RandomRuinStrategyFactory(0.5).createStrategy(vrp);
|
||||
|
||||
|
||||
SolutionCostCalculator solutionCostCalculator = new SolutionCostCalculator() {
|
||||
|
||||
@Override
|
||||
public double getCosts(VehicleRoutingProblemSolution solution) {
|
||||
double costs = 0.0;
|
||||
for(VehicleRoute route : solution.getRoutes()){
|
||||
costs += stateManager.getRouteState(route, StateFactory.COSTS, Double.class);
|
||||
}
|
||||
return costs;
|
||||
}
|
||||
};
|
||||
|
||||
SearchStrategy randomStrategy = new SearchStrategy(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);
|
||||
RuinAndRecreateModule radialModule = new RuinAndRecreateModule("radialRuin_bestInsertion", bestInsertion, radial);
|
||||
radialStrategy.addModule(radialModule);
|
||||
|
||||
SearchStrategyManager strategyManager = new SearchStrategyManager();
|
||||
strategyManager.addStrategy(radialStrategy, 0.5);
|
||||
strategyManager.addStrategy(randomStrategy, 0.5);
|
||||
|
||||
VehicleRoutingAlgorithm vra = new VehicleRoutingAlgorithm(vrp, strategyManager);
|
||||
vra.addListener(stateManager);
|
||||
vra.addListener(new RemoveEmptyVehicles(fleetManager));
|
||||
|
||||
VehicleRoutingProblemSolution iniSolution = new InsertionInitialSolutionFactory(bestInsertion, solutionCostCalculator).createSolution(vrp);
|
||||
vra.addInitialSolution(iniSolution);
|
||||
|
||||
vra.setNuOfIterations(3);
|
||||
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
|
||||
assertTrue(!solutions.isEmpty());
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -127,7 +127,7 @@ public class ServiceInsertionAndLoadConstraintsTest {
|
|||
|
||||
VehicleRoutingProblem vrp = mock(VehicleRoutingProblem.class);
|
||||
|
||||
StateManager stateManager = new StateManager(vrp.getTransportCosts());
|
||||
StateManager stateManager = new StateManager(vrp);
|
||||
stateManager.updateLoadStates();
|
||||
|
||||
ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager);
|
||||
|
|
|
|||
|
|
@ -251,7 +251,7 @@ public class ShipmentInsertionCalculatorTest {
|
|||
inserter.insertJob(shipment, new InsertionData(0,0,0,vehicle,null), route);
|
||||
inserter.insertJob(shipment2, new InsertionData(0,1,2,vehicle,null), route);
|
||||
|
||||
StateManager stateManager = new StateManager(vrp.getTransportCosts());
|
||||
StateManager stateManager = new StateManager(vrp);
|
||||
stateManager.updateLoadStates();
|
||||
stateManager.informInsertionStarts(Arrays.asList(route), null);
|
||||
|
||||
|
|
|
|||
|
|
@ -16,16 +16,6 @@
|
|||
******************************************************************************/
|
||||
package jsprit.core.algorithm.recreate;
|
||||
|
||||
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.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import jsprit.core.algorithm.state.StateManager;
|
||||
import jsprit.core.problem.VehicleRoutingProblem;
|
||||
import jsprit.core.problem.constraint.ConstraintManager;
|
||||
|
|
@ -49,12 +39,17 @@ import jsprit.core.util.Coordinate;
|
|||
import jsprit.core.util.EuclideanDistanceCalculator;
|
||||
import jsprit.core.util.Locations;
|
||||
import jsprit.core.util.ManhattanDistanceCalculator;
|
||||
|
||||
import org.apache.log4j.Level;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -128,7 +123,7 @@ public class TestCalculatesServiceInsertion {
|
|||
|
||||
VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addAllJobs(jobs).addVehicle(vehicle).setRoutingCost(costs).build();
|
||||
|
||||
states = new StateManager(vrp.getTransportCosts());
|
||||
states = new StateManager(vrp);
|
||||
states.updateLoadStates();
|
||||
states.updateTimeWindowStates();
|
||||
|
||||
|
|
|
|||
|
|
@ -18,23 +18,18 @@
|
|||
******************************************************************************/
|
||||
package jsprit.core.algorithm.recreate;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import jsprit.core.algorithm.state.StateManager;
|
||||
import jsprit.core.problem.VehicleRoutingProblem;
|
||||
import jsprit.core.problem.constraint.ConstraintManager;
|
||||
import jsprit.core.problem.job.Delivery;
|
||||
import jsprit.core.problem.job.Shipment;
|
||||
import jsprit.core.problem.vehicle.InfiniteFleetManagerFactory;
|
||||
import jsprit.core.problem.vehicle.Vehicle;
|
||||
import jsprit.core.problem.vehicle.VehicleFleetManager;
|
||||
import jsprit.core.problem.vehicle.VehicleImpl;
|
||||
import jsprit.core.problem.vehicle.*;
|
||||
import jsprit.core.problem.vehicle.VehicleImpl.Builder;
|
||||
import jsprit.core.problem.vehicle.VehicleType;
|
||||
import jsprit.core.problem.vehicle.VehicleTypeImpl;
|
||||
import jsprit.core.util.Coordinate;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
|
||||
public class TestMixedServiceAndShipmentsProblemOnRouteLevel {
|
||||
|
||||
|
|
@ -56,7 +51,7 @@ public class TestMixedServiceAndShipmentsProblemOnRouteLevel {
|
|||
Builder vehicleBuilder = VehicleImpl.Builder.newInstance("vehicle");
|
||||
vehicleBuilder.setStartLocationCoordinate(Coordinate.newInstance(10, 10));
|
||||
vehicleBuilder.setType(vehicleType);
|
||||
Vehicle vehicle = vehicleBuilder.build();
|
||||
VehicleImpl vehicle = vehicleBuilder.build();
|
||||
|
||||
/*
|
||||
* build shipments at the required locations, each with a capacity-demand of 1.
|
||||
|
|
@ -92,7 +87,7 @@ public class TestMixedServiceAndShipmentsProblemOnRouteLevel {
|
|||
|
||||
VehicleRoutingProblem vrp = vrpBuilder.build();
|
||||
|
||||
final StateManager stateManager = new StateManager(vrp.getTransportCosts());
|
||||
final StateManager stateManager = new StateManager(vrp);
|
||||
|
||||
|
||||
ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager);
|
||||
|
|
@ -121,7 +116,7 @@ public class TestMixedServiceAndShipmentsProblemOnRouteLevel {
|
|||
Builder vehicleBuilder = VehicleImpl.Builder.newInstance("vehicle");
|
||||
vehicleBuilder.setStartLocationCoordinate(Coordinate.newInstance(10, 10));
|
||||
vehicleBuilder.setType(vehicleType);
|
||||
Vehicle vehicle = vehicleBuilder.build();
|
||||
VehicleImpl vehicle = vehicleBuilder.build();
|
||||
|
||||
/*
|
||||
* build shipments at the required locations, each with a capacity-demand of 1.
|
||||
|
|
@ -157,7 +152,7 @@ public class TestMixedServiceAndShipmentsProblemOnRouteLevel {
|
|||
|
||||
VehicleRoutingProblem vrp = vrpBuilder.build();
|
||||
|
||||
final StateManager stateManager = new StateManager(vrp.getTransportCosts());
|
||||
final StateManager stateManager = new StateManager(vrp);
|
||||
|
||||
ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager);
|
||||
constraintManager.addLoadConstraint();
|
||||
|
|
|
|||
|
|
@ -91,7 +91,7 @@ public class TestRouteLevelActivityInsertionCostEstimator {
|
|||
|
||||
}).addService(s1).addService(s2).addService(s3).build();
|
||||
|
||||
stateManager = new StateManager(routingCosts);
|
||||
stateManager = new StateManager(vrp);
|
||||
stateManager.addStateUpdater(new UpdateVariableCosts(activityCosts,routingCosts,stateManager));
|
||||
stateManager.informInsertionStarts(Arrays.asList(route), Collections.<Job>emptyList());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,13 +18,6 @@
|
|||
******************************************************************************/
|
||||
package jsprit.core.algorithm.recreate;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import jsprit.core.algorithm.state.StateManager;
|
||||
import jsprit.core.algorithm.state.UpdateVariableCosts;
|
||||
import jsprit.core.problem.VehicleRoutingProblem;
|
||||
|
|
@ -42,10 +35,16 @@ import jsprit.core.problem.vehicle.VehicleImpl;
|
|||
import jsprit.core.problem.vehicle.VehicleType;
|
||||
import jsprit.core.problem.vehicle.VehicleTypeImpl;
|
||||
import jsprit.core.util.CostFactory;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Created by schroeder on 02.07.14.
|
||||
*/
|
||||
|
|
@ -82,7 +81,7 @@ public class TestRouteLevelServiceInsertionCostEstimator {
|
|||
|
||||
route = VehicleRoute.Builder.newInstance(vehicle).addService(s1).addService(s2).addService(s3).build();
|
||||
|
||||
stateManager = new StateManager(routingCosts);
|
||||
stateManager = new StateManager(mock(VehicleRoutingProblem.class));
|
||||
stateManager.addStateUpdater(new UpdateVariableCosts(activityCosts,routingCosts,stateManager));
|
||||
stateManager.informInsertionStarts(Arrays.asList(route), Collections.<Job>emptyList());
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import jsprit.core.problem.AbstractActivity;
|
|||
import jsprit.core.problem.Capacity;
|
||||
import jsprit.core.problem.JobActivityFactory;
|
||||
import jsprit.core.problem.VehicleRoutingProblem;
|
||||
import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
|
||||
import jsprit.core.problem.job.*;
|
||||
import jsprit.core.problem.misc.JobInsertionContext;
|
||||
import jsprit.core.problem.solution.route.VehicleRoute;
|
||||
|
|
@ -90,7 +89,7 @@ public class LoadConstraintTest {
|
|||
});
|
||||
shipment_route = shipmentRouteBuilder.addPickup(shipment1).addPickup(shipment2).addDelivery(shipment2).addDelivery(shipment1).build();
|
||||
|
||||
stateManager = new StateManager(mock(VehicleRoutingTransportCosts.class));
|
||||
stateManager = new StateManager(mock(VehicleRoutingProblem.class));
|
||||
stateManager.updateLoadStates();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,15 +18,9 @@
|
|||
******************************************************************************/
|
||||
package jsprit.core.problem.constraint;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import jsprit.core.algorithm.state.StateManager;
|
||||
import jsprit.core.problem.Capacity;
|
||||
import jsprit.core.problem.VehicleRoutingProblem;
|
||||
import jsprit.core.problem.cost.AbstractForwardVehicleRoutingTransportCosts;
|
||||
import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
|
||||
import jsprit.core.problem.driver.Driver;
|
||||
|
|
@ -39,10 +33,16 @@ import jsprit.core.problem.solution.route.state.RouteAndActivityStateGetter;
|
|||
import jsprit.core.problem.solution.route.state.StateFactory;
|
||||
import jsprit.core.problem.vehicle.Vehicle;
|
||||
import jsprit.core.problem.vehicle.VehicleType;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class ServiceLoadRouteLevelConstraintTest {
|
||||
|
||||
private Vehicle vehicle;
|
||||
|
|
@ -87,7 +87,7 @@ public class ServiceLoadRouteLevelConstraintTest {
|
|||
|
||||
constraint = new ServiceLoadRouteLevelConstraint(stateGetter);
|
||||
|
||||
stateManager = new StateManager(routingCosts);
|
||||
stateManager = new StateManager(mock(VehicleRoutingProblem.class));
|
||||
stateManager.updateLoadStates();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue