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

extended stateManager to memorize vehicle dependent states

This commit is contained in:
oblonski 2014-07-16 15:00:30 +02:00
parent 952570952a
commit dbdb3372c7
11 changed files with 167 additions and 76 deletions

View file

@ -18,13 +18,10 @@
******************************************************************************/
package jsprit.core.algorithm.state;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import jsprit.core.problem.Capacity;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.constraint.HardActivityStateLevelConstraint.ConstraintsStatus;
import jsprit.core.problem.constraint.PickupAndDeliverShipmentLoadActivityLevelConstraint;
import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
import jsprit.core.problem.job.Service;
import jsprit.core.problem.job.Shipment;
import jsprit.core.problem.misc.JobInsertionContext;
@ -34,10 +31,13 @@ import jsprit.core.problem.solution.route.activity.PickupShipment;
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 static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class HardPickupAndDeliveryShipmentActivityConstraintTest {
@ -59,7 +59,7 @@ public class HardPickupAndDeliveryShipmentActivityConstraintTest {
when(type.getCapacityDimensions()).thenReturn(Capacity.Builder.newInstance().addDimension(0, 2).build());
when(vehicle.getType()).thenReturn(type);
// when(vehicle.getType().getCapacityDimensions()).thenReturn(Capacity.Builder.newInstance().addDimension(0, 2).build());
stateManager = new StateManager(mock(VehicleRoutingTransportCosts.class));
stateManager = new StateManager(mock(VehicleRoutingProblem.class));
shipment = mock(Shipment.class);
when(shipment.getSize()).thenReturn(Capacity.Builder.newInstance().addDimension(0, 1).build());
// when(shipment.getCapacityDemand()).thenReturn(1);
@ -83,7 +83,7 @@ public class HardPickupAndDeliveryShipmentActivityConstraintTest {
PickupService anotherService = new PickupService(mock(Service.class));
PickupShipment pickupShipment = new PickupShipment(shipment);
stateManager.putInternalTypedActivityState(pickupService, StateFactory.LOAD, Capacity.class, Capacity.Builder.newInstance().addDimension(0, 2).build());
stateManager.putInternalTypedActivityState(pickupService, StateFactory.LOAD, Capacity.Builder.newInstance().addDimension(0, 2).build());
// when(stateManager.getActivityState(pickupService, StateFactory.LOAD)).thenReturn(StateFactory.createState(2.0));
assertEquals(ConstraintsStatus.NOT_FULFILLED,constraint.fulfilled(iFacts, pickupService, pickupShipment, anotherService, 0.0));
}
@ -94,7 +94,7 @@ public class HardPickupAndDeliveryShipmentActivityConstraintTest {
PickupService anotherService = new PickupService(mock(Service.class));
DeliverShipment pickupShipment = new DeliverShipment(shipment);
stateManager.putInternalTypedActivityState(pickupService, StateFactory.LOAD, Capacity.class, Capacity.Builder.newInstance().addDimension(0, 2).build());
stateManager.putInternalTypedActivityState(pickupService, StateFactory.LOAD, Capacity.Builder.newInstance().addDimension(0, 2).build());
assertEquals(ConstraintsStatus.NOT_FULFILLED_BREAK,constraint.fulfilled(iFacts, pickupService, pickupShipment, anotherService, 0.0));
}
@ -104,7 +104,7 @@ public class HardPickupAndDeliveryShipmentActivityConstraintTest {
PickupService anotherService = new PickupService(mock(Service.class));
DeliverShipment pickupShipment = new DeliverShipment(shipment);
stateManager.putInternalTypedActivityState(pickupService, StateFactory.LOAD, Capacity.class, Capacity.Builder.newInstance().addDimension(0, 1).build());
stateManager.putInternalTypedActivityState(pickupService, StateFactory.LOAD, Capacity.Builder.newInstance().addDimension(0, 1).build());
// stateManager.putInternalActivityState(pickupService, StateFactory.LOAD, StateFactory.createState(1));
assertEquals(ConstraintsStatus.FULFILLED,constraint.fulfilled(iFacts, pickupService, pickupShipment, anotherService, 0.0));
}

View file

@ -4,7 +4,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.solution.route.VehicleRoute;
import jsprit.core.problem.solution.route.state.StateFactory;
@ -88,7 +87,7 @@ public class LoadStateTest {
});
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();
}

View file

@ -19,12 +19,15 @@
package jsprit.core.algorithm.state;
import jsprit.core.problem.Capacity;
import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
import jsprit.core.problem.VehicleRoutingProblem;
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.solution.route.state.StateFactory;
import jsprit.core.problem.solution.route.state.StateFactory.State;
import jsprit.core.problem.solution.route.state.StateFactory.StateId;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleImpl;
import org.junit.Test;
import static org.junit.Assert.*;
@ -35,42 +38,46 @@ public class StateManagerTest {
@Test
public void whenRouteStateIsSetWithGenericMethod_itMustBeSetCorrectly(){
VehicleRoute route = mock(VehicleRoute.class);
StateManager stateManager = new StateManager(mock(VehicleRoutingTransportCosts.class));
StateId id = StateFactory.createId("myState");
VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().build();
VehicleRoute route = getRoute(mock(Vehicle.class));
StateManager stateManager = new StateManager(vrp);
StateId id = stateManager.createStateId("myState");
State state = StateFactory.createState(1.);
stateManager.putTypedRouteState(route, id, State.class, state);
stateManager.putRouteState(route, id, state);
assertEquals(1.,stateManager.getRouteState(route, id, State.class).toDouble(),0.01);
}
@Test
private VehicleRoute getRoute(Vehicle vehicle) {
return VehicleRoute.Builder.newInstance(vehicle).addService(Service.Builder.newInstance("s").setLocationId("loc").build()).build();
}
@Test
public void whenRouteStateIsSetWithGenericMethodAndBoolean_itMustBeSetCorrectly(){
VehicleRoute route = mock(VehicleRoute.class);
StateManager stateManager = new StateManager(mock(VehicleRoutingTransportCosts.class));
StateId id = StateFactory.createId("myState");
boolean routeIsRed = true;
stateManager.putTypedRouteState(route, id, Boolean.class, routeIsRed);
VehicleRoute route = getRoute(mock(Vehicle.class));
StateManager stateManager = new StateManager(mock(VehicleRoutingProblem.class));
StateId id = stateManager.createStateId("myState");
stateManager.putRouteState(route, id, true);
assertTrue(stateManager.getRouteState(route, id, Boolean.class));
}
@Test
public void whenRouteStateIsSetWithGenericMethodAndInteger_itMustBeSetCorrectly(){
VehicleRoute route = mock(VehicleRoute.class);
StateManager stateManager = new StateManager(mock(VehicleRoutingTransportCosts.class));
StateId id = StateFactory.createId("myState");
VehicleRoute route = getRoute(mock(Vehicle.class));
StateManager stateManager = new StateManager(mock(VehicleRoutingProblem.class));
StateId id = stateManager.createStateId("myState");
int load = 3;
stateManager.putTypedRouteState(route, id, Integer.class, load);
stateManager.putRouteState(route, id, load);
int getLoad = stateManager.getRouteState(route, id, Integer.class);
assertEquals(3, getLoad);
}
@Test
public void whenRouteStateIsSetWithGenericMethodAndCapacity_itMustBeSetCorrectly(){
VehicleRoute route = mock(VehicleRoute.class);
StateManager stateManager = new StateManager(mock(VehicleRoutingTransportCosts.class));
StateId id = StateFactory.createId("myState");
VehicleRoute route = getRoute(mock(Vehicle.class));
StateManager stateManager = new StateManager(mock(VehicleRoutingProblem.class));
StateId id = stateManager.createStateId("myState");
Capacity capacity = Capacity.Builder.newInstance().addDimension(0, 500).build();
stateManager.putTypedRouteState(route, id, Capacity.class, capacity);
stateManager.putRouteState(route, id, capacity);
Capacity getCap = stateManager.getRouteState(route, id, Capacity.class);
assertEquals(500, getCap.get(0));
}
@ -79,30 +86,29 @@ public class StateManagerTest {
public void whenActivityStateIsSetWithGenericMethod_itMustBeSetCorrectly(){
TourActivity activity = mock(TourActivity.class);
when(activity.getIndex()).thenReturn(0);
StateManager stateManager = new StateManager(mock(VehicleRoutingTransportCosts.class));
StateManager stateManager = new StateManager(mock(VehicleRoutingProblem.class));
StateId id = stateManager.createStateId("myState");
State state = StateFactory.createState(1.);
stateManager.putTypedActivityState(activity, id, State.class, state);
stateManager.putActivityState(activity, id, state);
assertEquals(1.,stateManager.getActivityState(activity, id, State.class).toDouble(),0.01);
}
@Test
public void whenActivityStateIsSetWithGenericMethodAndBoolean_itMustBeSetCorrectly(){
TourActivity activity = mock(TourActivity.class);
StateManager stateManager = new StateManager(mock(VehicleRoutingTransportCosts.class));
StateManager stateManager = new StateManager(mock(VehicleRoutingProblem.class));
StateId id = stateManager.createStateId("myState");
boolean routeIsRed = true;
stateManager.putTypedActivityState(activity, id, Boolean.class, routeIsRed);
stateManager.putActivityState(activity, id, true);
assertTrue(stateManager.getActivityState(activity, id, Boolean.class));
}
@Test
public void whenActivityStateIsSetWithGenericMethodAndInteger_itMustBeSetCorrectly(){
TourActivity activity = mock(TourActivity.class);
StateManager stateManager = new StateManager(mock(VehicleRoutingTransportCosts.class));
StateManager stateManager = new StateManager(mock(VehicleRoutingProblem.class));
StateId id = stateManager.createStateId("myState");
int load = 3;
stateManager.putTypedActivityState(activity, id, Integer.class, load);
stateManager.putActivityState(activity, id, load);
int getLoad = stateManager.getActivityState(activity, id, Integer.class);
assertEquals(3, getLoad);
}
@ -110,17 +116,17 @@ public class StateManagerTest {
@Test
public void whenActivityStateIsSetWithGenericMethodAndCapacity_itMustBeSetCorrectly(){
TourActivity activity = mock(TourActivity.class);
StateManager stateManager = new StateManager(mock(VehicleRoutingTransportCosts.class));
StateManager stateManager = new StateManager(mock(VehicleRoutingProblem.class));
StateId id = stateManager.createStateId("myState");
Capacity capacity = Capacity.Builder.newInstance().addDimension(0, 500).build();
stateManager.putTypedActivityState(activity, id, Capacity.class, capacity);
stateManager.putActivityState(activity, id, capacity);
Capacity getCap = stateManager.getActivityState(activity, id, Capacity.class);
assertEquals(500, getCap.get(0));
}
@Test
public void whenProblemStateIsSet_itMustBeSetCorrectly(){
StateManager stateManager = new StateManager(mock(VehicleRoutingTransportCosts.class));
StateManager stateManager = new StateManager(mock(VehicleRoutingProblem.class));
StateId id = stateManager.createStateId("problemState");
stateManager.putProblemState(id, Boolean.class, true);
boolean problemState = stateManager.getProblemState(id, Boolean.class);
@ -129,8 +135,8 @@ public class StateManagerTest {
@Test(expected=NullPointerException.class)
public void whenProblemStateIsSetAndStateManagerClearedAfterwards_itThrowsException(){
StateManager stateManager = new StateManager(mock(VehicleRoutingTransportCosts.class));
StateId id = StateFactory.createId("problemState");
StateManager stateManager = new StateManager(mock(VehicleRoutingProblem.class));
StateId id = stateManager.createStateId("problemState");
stateManager.putProblemState(id, Boolean.class, true);
stateManager.clear();
@SuppressWarnings("unused")
@ -139,7 +145,7 @@ public class StateManagerTest {
@Test
public void whenProblemStateIsSetAndStateManagerClearedAfterwards_itReturnsDefault(){
StateManager stateManager = new StateManager(mock(VehicleRoutingTransportCosts.class));
StateManager stateManager = new StateManager(mock(VehicleRoutingProblem.class));
StateId id = StateFactory.createId("problemState");
stateManager.addDefaultProblemState(id, Boolean.class, false);
stateManager.putProblemState(id, Boolean.class, true);
@ -150,14 +156,14 @@ public class StateManagerTest {
@Test
public void whenCreatingNewState_itShouldHaveAnIndex(){
StateManager stateManager = new StateManager(mock(VehicleRoutingTransportCosts.class));
StateManager stateManager = new StateManager(mock(VehicleRoutingProblem.class));
StateId stateId = stateManager.createStateId("foo-state");
assertEquals(10,stateId.getIndex());
}
@Test
public void whenCreatingNewStates_theyShouldHaveAnIndex(){
StateManager stateManager = new StateManager(mock(VehicleRoutingTransportCosts.class));
StateManager stateManager = new StateManager(mock(VehicleRoutingProblem.class));
StateId fooState = stateManager.createStateId("foo-state");
StateId foofooState = stateManager.createStateId("foo-foo-state");
assertEquals(10,fooState.getIndex());
@ -166,10 +172,34 @@ public class StateManagerTest {
@Test
public void whenCreatingTwoStatesWithTheSameName_theyShouldHaveTheSameIndex(){
StateManager stateManager = new StateManager(mock(VehicleRoutingTransportCosts.class));
StateManager stateManager = new StateManager(mock(VehicleRoutingProblem.class));
StateId fooState = stateManager.createStateId("foo-state");
StateId foofooState = stateManager.createStateId("foo-state");
assertEquals(10,fooState.getIndex());
assertEquals(10,foofooState.getIndex());
assertEquals(10, fooState.getIndex());
assertEquals(10, foofooState.getIndex());
}
@Test
public void whenCreatingAVehicleDependentRouteState_itShouldBeMemorized(){
Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").build();
VehicleRoute route = getRoute(vehicle);
StateManager stateManager = new StateManager(mock(VehicleRoutingProblem.class));
StateId id = stateManager.createStateId("myState");
Capacity capacity = Capacity.Builder.newInstance().addDimension(0, 500).build();
stateManager.putRouteState(route, vehicle, null, id, capacity);
Capacity getCap = stateManager.getRouteState(route, vehicle, null, id, Capacity.class);
assertEquals(500, getCap.get(0));
}
@Test
public void whenCreatingAVehicleDependentActivityState_itShouldBeMemorized(){
Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").build();
StateManager stateManager = new StateManager(mock(VehicleRoutingProblem.class));
StateId id = stateManager.createStateId("myState");
Capacity capacity = Capacity.Builder.newInstance().addDimension(0, 500).build();
TourActivity act = mock(TourActivity.class);
stateManager.putActivityState(act, vehicle, null, id, capacity);
Capacity getCap = stateManager.getActivityState(act, vehicle, null, id, Capacity.class);
assertEquals(500, getCap.get(0));
}
}

View file

@ -57,7 +57,7 @@ public class UpdatePracticalTimeWindowTest {
routingCosts = CostFactory.createManhattanCosts();
stateManager = new StateManager(mock(VehicleRoutingTransportCosts.class));
stateManager = new StateManager(mock(VehicleRoutingProblem.class));
reverseActivityVisitor = new ReverseRouteActivityVisitor();
reverseActivityVisitor.addActivityVisitor(new UpdatePracticalTimeWindows(stateManager, routingCosts));