From a472f282c9fcdd4a6fd87118500d8d6ac41c6fbb Mon Sep 17 00:00:00 2001 From: oblonski <4sschroeder@gmail.com> Date: Tue, 29 Jul 2014 11:45:26 +0200 Subject: [PATCH] cleaned core.algorithm.state.StateManager --- .../core/algorithm/state/StateManager.java | 138 +----------------- .../algorithm/state/StateManagerTest.java | 4 +- 2 files changed, 8 insertions(+), 134 deletions(-) diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/state/StateManager.java b/jsprit-core/src/main/java/jsprit/core/algorithm/state/StateManager.java index ea7cc5b1..628b0517 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/state/StateManager.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/state/StateManager.java @@ -20,7 +20,6 @@ import jsprit.core.algorithm.listener.IterationStartsListener; import jsprit.core.algorithm.recreate.listener.*; import jsprit.core.algorithm.ruin.listener.RuinListener; import jsprit.core.algorithm.ruin.listener.RuinListeners; -import jsprit.core.problem.Capacity; import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.job.Job; import jsprit.core.problem.solution.VehicleRoutingProblemSolution; @@ -75,8 +74,6 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart private States_ problemStates_ = new States_(); - private States_ defaultProblemStates_ = new States_(); - private RouteActivityVisitor routeActivityVisitor = new RouteActivityVisitor(); private ReverseRouteActivityVisitor revRouteActivityVisitor = new ReverseRouteActivityVisitor(); @@ -88,10 +85,6 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart private InsertionListeners insertionListeners = new InsertionListeners(); private Collection updaters = new ArrayList(); - - private Map defaultRouteStates_ = new HashMap(); - - private Map defaultActivityStates_ = new HashMap(); private boolean updateLoad = false; @@ -101,8 +94,6 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart private Map createdStateIds = new HashMap(); - private int initialNuStates = 30; - private int nuActivities; private int nuVehicleTypeKeys; @@ -146,26 +137,6 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart stateIndexCounter++; } - private void addDefaultStates() { - defaultActivityStates_.put(InternalStates.LOAD, Capacity.Builder.newInstance().build()); - defaultActivityStates_.put(InternalStates.COSTS, 0.); - defaultActivityStates_.put(InternalStates.DURATION, 0.); - defaultActivityStates_.put(InternalStates.FUTURE_MAXLOAD, Capacity.Builder.newInstance().build()); - defaultActivityStates_.put(InternalStates.PAST_MAXLOAD, Capacity.Builder.newInstance().build()); - - defaultRouteStates_.put(InternalStates.LOAD, Capacity.Builder.newInstance().build()); - - defaultRouteStates_.put(InternalStates.COSTS, 0.); - defaultRouteStates_.put(InternalStates.DURATION, 0.); - defaultRouteStates_.put(InternalStates.FUTURE_MAXLOAD, Capacity.Builder.newInstance().build()); - defaultRouteStates_.put(InternalStates.PAST_MAXLOAD, Capacity.Builder.newInstance().build()); - - defaultRouteStates_.put(InternalStates.MAXLOAD, Capacity.Builder.newInstance().build()); - - defaultRouteStates_.put(InternalStates.LOAD_AT_END, Capacity.Builder.newInstance().build()); - defaultRouteStates_.put(InternalStates.LOAD_AT_BEGINNING, Capacity.Builder.newInstance().build()); - - } /** * Constructs the stateManager with the specified VehicleRoutingProblem. @@ -173,6 +144,7 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart * @param vehicleRoutingProblem the corresponding VehicleRoutingProblem */ public StateManager(VehicleRoutingProblem vehicleRoutingProblem){ + int initialNuStates = 30; this.vrp = vehicleRoutingProblem; nuActivities = Math.max(10, vrp.getNuActivities() + 1); nuVehicleTypeKeys = Math.max(3, getNuVehicleTypes(vrp) + 2); @@ -180,7 +152,6 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart route_states = new Object[nuActivities][initialNuStates]; vehicle_dependent_activity_states = new Object[nuActivities][nuVehicleTypeKeys][initialNuStates]; vehicle_dependent_route_states = new Object[nuActivities][nuVehicleTypeKeys][initialNuStates]; - addDefaultStates(); } private int getNuVehicleTypes(VehicleRoutingProblem vrp) { @@ -191,11 +162,6 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart return maxIndex; } - @Deprecated - public void addDefaultProblemState(StateId stateId, Class type, T defaultState){ - defaultProblemStates_.putState(stateId, type, defaultState); - } - /** * Associates the specified state to the stateId. If there already exists a state value for the stateId, this old * value is replaced by the new value. @@ -222,24 +188,6 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart return problemStates_.getState(stateId, type); } - @Deprecated - T getDefaultProblemState(StateId stateId, Class type){ - if(defaultProblemStates_.containsKey(stateId)) return defaultProblemStates_.getState(stateId, type); - return null; - } - - @Deprecated - public void addDefaultRouteState(StateId stateId, Class type, T defaultState){ - if(StateFactory.isReservedId(stateId)) StateFactory.throwReservedIdException(stateId.toString()); - defaultRouteStates_.put(stateId, type.cast(defaultState)); - } - - @Deprecated - public void addDefaultActivityState(StateId stateId, Class type, T defaultState){ - if(StateFactory.isReservedId(stateId)) StateFactory.throwReservedIdException(stateId.toString()); - defaultActivityStates_.put(stateId, type.cast(defaultState)); - } - /** * Clears all states, i.e. set all value to null. * @@ -335,20 +283,6 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart return new ClassCastException(e + "\n" + "state with stateId '" + stateId.toString() + "' is of " + memorizedTypeClass + ". cannot cast it to " + requestedTypeClass + "."); } - @Deprecated - private T getDefaultTypedActivityState(TourActivity act, StateId stateId, Class type) { - if(defaultActivityStates_.containsKey(stateId)){ - return type.cast(defaultActivityStates_.get(stateId)); - } - if(stateId.equals(InternalStates.EARLIEST_OPERATION_START_TIME)){ - return type.cast(act.getTheoreticalEarliestOperationStartTime()); - } - if(stateId.equals(InternalStates.LATEST_OPERATION_START_TIME)){ - return type.cast(act.getTheoreticalLatestOperationStartTime()); - } - return null; - } - /** * Returns the route state that is associated to the route and stateId, or null if no state is associated. *

If type class is not equal to the associated type class of the requested state value, it throws a ClassCastException.

@@ -383,6 +317,7 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart * @param stateId the stateId(entifier) for the state that is requested * @return true if state exists and false otherwise */ + @SuppressWarnings("UnusedDeclaration") public boolean hasRouteState(VehicleRoute route, Vehicle vehicle, StateId stateId) { return vehicle_dependent_route_states[route.getActivities().get(0).getIndex()][vehicle.getVehicleTypeIdentifier().getIndex()][stateId.getIndex()] != null; } @@ -412,49 +347,6 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart return state; } - @Deprecated - private T getDefaultTypedRouteState(StateId stateId, Class type) { - if(defaultRouteStates_.containsKey(stateId)){ - return type.cast(defaultRouteStates_.get(stateId)); - } - return null; - } - - @Deprecated - public void putTypedActivityState(TourActivity act, StateId stateId, Class type, T state){ - if(stateId.getIndex()<10) throw new IllegalStateException("either you use a reserved stateId that is applied\n" + - "internally or your stateId has been created without index, e.g. StateFactory.createId(stateName)\n" + - " does not assign indeces thus do not use it anymore, but use\n " + - "stateManager.createStateId(name)\n" + - " instead.\n"); - putInternalTypedActivityState(act, stateId, state); - } - - /** - * Method to memorize state 'state' of type 'type' of act and stateId. - * - *

For example:
- * Capacity loadAtMyActivity = Capacity.Builder.newInstance().addCapacityDimension(0,10).build();
- * stateManager.putTypedActivityState(myActivity, StateFactory.createStateId("act-load"), Capacity.class, loadAtMyActivity);
- *

you can retrieve the load at myActivity by
- * Capacity load = stateManager.getActivityState(myActivity, StateFactory.createStateId("act-load"), Capacity.class); - * - * @param act for which a new state should be memorized - * @param stateId stateId of state - * @param type class of state-value - * @param state state-value - */ - @Deprecated - public void putActivityState(TourActivity act, StateId stateId, Class type, T state){ - if(act.getIndex() == 0) throw new IllegalStateException("activity index is 0. this should not be."); - if(stateId.getIndex()<10) throw new IllegalStateException("either you use a reserved stateId that is applied\n" + - "internally or your stateId has been created without index, e.g. StateFactory.createId(stateName)\n" + - " does not assign indeces thus do not use it anymore, but use\n " + - "stateManager.createStateId(name)\n" + - " instead.\n"); - putInternalTypedActivityState(act, stateId, state); - } - /** * Associates the specified activity and stateId to the state value. If a state value is already associated to the * specified activity and stateId, it is replaced by the new state value. @@ -496,13 +388,6 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart putInternalTypedActivityState(act, vehicle, stateId, state); } - private Object[][] resizeArr(Object[][] states, int newLength) { - int oldSize = states.length; - Object[][] new_states = new Object[newLength][stateIndexCounter]; - System.arraycopy(states,0,new_states,0,Math.min(oldSize,newLength)); - return new_states; - } - void putInternalTypedActivityState(TourActivity act, StateId stateId, T state){ activity_states[act.getIndex()][stateId.getIndex()]=state; } @@ -511,12 +396,7 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart vehicle_dependent_activity_states[act.getIndex()][vehicle.getVehicleTypeIdentifier().getIndex()][stateId.getIndex()]=state; } - @Deprecated - public void putTypedRouteState(VehicleRoute route, StateId stateId, Class type, T state){ - putRouteState(route, stateId, state); - } - - /** + /** * Associates the specified route, vehicle and stateId to the state value. If a state value is already associated to the * specified activity and stateId, it is replaced by the new state value. * @@ -584,7 +464,8 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart * * @return an unmodifiable collections of stateUpdaters that have been added to this stateManager. */ - Collection getStateUpdaters(){ + @SuppressWarnings("UnusedDeclaration") + Collection getStateUpdaters(){ return Collections.unmodifiableCollection(updaters); } @@ -618,19 +499,10 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart ruinListeners.addListener(ruinListener); } - void removeListener(RuinListener ruinListener){ - ruinListeners.removeListener(ruinListener); - } - void addListener(InsertionListener insertionListener){ insertionListeners.addListener(insertionListener); } - void removeListener(InsertionListener insertionListener){ - insertionListeners.removeListener(insertionListener); - } - - @Override public void informJobInserted(Job job2insert, VehicleRoute inRoute, double additionalCosts, double additionalTime) { // log.debug("insert " + job2insert + " in " + inRoute); diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/state/StateManagerTest.java b/jsprit-core/src/test/java/jsprit/core/algorithm/state/StateManagerTest.java index 669fdf92..bac4f966 100644 --- a/jsprit-core/src/test/java/jsprit/core/algorithm/state/StateManagerTest.java +++ b/jsprit-core/src/test/java/jsprit/core/algorithm/state/StateManagerTest.java @@ -148,7 +148,6 @@ public class StateManagerTest { public void whenProblemStateIsSetAndStateManagerClearedAfterwards_itReturnsNull(){ StateManager stateManager = new StateManager(mock(VehicleRoutingProblem.class)); StateId id = StateFactory.createId("problemState"); - stateManager.addDefaultProblemState(id, Boolean.class, false); stateManager.putProblemState(id, Boolean.class, true); stateManager.clear(); Boolean problemState = stateManager.getProblemState(id, Boolean.class); @@ -183,6 +182,7 @@ public class StateManagerTest { @Test public void whenCreatingAVehicleDependentRouteState_itShouldBeMemorized(){ VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").build(); + //noinspection UnusedDeclaration VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addVehicle(vehicle).build(); VehicleRoute route = getRoute(vehicle); StateManager stateManager = new StateManager(mock(VehicleRoutingProblem.class)); @@ -196,6 +196,7 @@ public class StateManagerTest { @Test public void whenCreatingAVehicleDependentActivityState_itShouldBeMemorized(){ VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").build(); + //noinspection UnusedDeclaration VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addVehicle(vehicle).build(); StateManager stateManager = new StateManager(mock(VehicleRoutingProblem.class)); StateId id = stateManager.createStateId("myState"); @@ -210,6 +211,7 @@ public class StateManagerTest { @Test public void whenMemorizingVehicleInfo_itShouldBeMemorized(){ VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").build(); + //noinspection UnusedDeclaration VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addVehicle(vehicle).build(); VehicleRoute route = getRoute(vehicle); StateManager stateManager = new StateManager(mock(VehicleRoutingProblem.class));