Class StateManager
- All Implemented Interfaces:
IterationStartsListener,SearchStrategyModuleListener,VehicleRoutingAlgorithmListener,InsertionEndsListener,InsertionListener,InsertionStartsListener,JobInsertedListener,RuinListener,RouteAndActivityStateGetter
Some condition, rules or constraints are stateful. This StateManager manages these states, i.e. it offers methods to add, store and retrieve states based on the problem, vehicle-routes and tour-activities.
- Author:
- schroeder
-
Constructor Summary
ConstructorsConstructorDescriptionStateManager(VehicleRoutingProblem vehicleRoutingProblem) Constructs the stateManager with the specified VehicleRoutingProblem. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAllStateUpdater(Collection<StateUpdater> updaters) voidaddStateUpdater(StateUpdater updater) Adds state updater.voidclear()Clears all states, i.e.createStateId(String name) Create and returns a stateId with the specified state-name.<T> TgetActivityState(TourActivity act, StateId stateId, Class<T> type) Returns associated state for the specified activity and stateId, or it returns null if no value is associated.<T> TgetActivityState(TourActivity act, Vehicle vehicle, StateId stateId, Class<T> type) Returns the associated state value to the specified activity, vehicle and stateId, or null if no state value is associated.<T> TgetProblemState(StateId stateId, Class<T> type) Returns mapped state value that is associated to the specified stateId, or null if no value is associated to the specified stateId.<T> TgetRouteState(VehicleRoute route, StateId stateId, Class<T> type) Returns the route state that is associated to the route and stateId, or null if no state is associated.<T> TgetRouteState(VehicleRoute route, Vehicle vehicle, StateId stateId, Class<T> type) Returns the route state that is assigned to the specified route, vehicle and stateId.getVrp()Returns the VehicleRoutingProblem associated with this StateManager.booleanhasActivityState(TourActivity act, Vehicle vehicle, StateId stateId) Returns true if a state value is associated to the specified activity, vehicle and stateId.booleanhasRouteState(VehicleRoute route, Vehicle vehicle, StateId stateId) Returns true if a state is assigned to the specified route, vehicle and stateId.voidinformInsertionEnds(Collection<VehicleRoute> vehicleRoutes, Collection<Job> badJobs) voidinformInsertionStarts(Collection<VehicleRoute> vehicleRoutes, Collection<Job> unassignedJobs) voidinformIterationStarts(int i, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) voidinformJobInserted(Job job2insert, VehicleRoute inRoute, InsertionData insertionData) <T> voidputActivityState(TourActivity act, StateId stateId, T state) Associates the specified activity and stateId to the state value.<T> voidputActivityState(TourActivity act, Vehicle vehicle, StateId stateId, T state) Associates the specified activity, vehicle and stateId to the state value.<T> voidputProblemState(StateId stateId, Class<T> type, T state) Associates the specified state to the stateId.<T> voidputRouteState(VehicleRoute route, StateId stateId, T state) Associates the specified route, vehicle and stateId to the state value.<T> voidputRouteState(VehicleRoute route, Vehicle vehicle, StateId stateId, T state) Associates the specified route, vehicle and stateId to the state value.voidreCalculateStates(VehicleRoute route) voidremoved(Job job, VehicleRoute fromRoute) informs if aJobhas been removed from aVehicleRoute.voidruinEnds(Collection<VehicleRoute> routes, Collection<Job> unassignedJobs) informs about ruin-end.voidruinStarts(Collection<VehicleRoute> routes) informs about ruin-start.booleanvoidUpdates load states.voidUpdates skill states.voidUpdates time-window states.
-
Constructor Details
-
StateManager
Constructs the stateManager with the specified VehicleRoutingProblem.- Parameters:
vehicleRoutingProblem- the corresponding VehicleRoutingProblem
-
-
Method Details
-
getVrp
Returns the VehicleRoutingProblem associated with this StateManager.- Returns:
- the VRP
-
createStateId
Create and returns a stateId with the specified state-name.If a stateId with the specified name has already been created, it returns the created stateId.
If the specified is equal to a name that is already used internally, it throws an IllegalStateException
- Parameters:
name- the specified name of the state- Returns:
- the stateId with which a state can be identified, no matter if it is a problem, route or activity state.
- Throws:
IllegalStateException- if name of state is already used internally
-
putProblemState
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.- Type Parameters:
T- the type of the state value- Parameters:
stateId- the stateId which is the associated key to the problem statetype- the type of the problem statestate- the actual state value
-
getProblemState
Returns mapped state value that is associated to the specified stateId, or null if no value is associated to the specified stateId.- Type Parameters:
T- the type- Parameters:
stateId- the stateId which is the associated key to the problem statetype- the type class of the state value- Returns:
- the state value that is associated to the specified stateId or null if no value is associated
-
clear
public void clear()Clears all states, i.e. set all value to null. -
getActivityState
Returns associated state for the specified activity and stateId, or it returns null if no value is associated.If type class is not equal to the associated type class of the requested state value, it throws a ClassCastException.
- Specified by:
getActivityStatein interfaceRouteAndActivityStateGetter- Type Parameters:
T- the type- Parameters:
act- the activity for which a state value is associated tostateId- the stateId for which a state value is associated totype- the type of class of the associated state value- Returns:
- the state value that is associated to the specified activity and stateId, or null if no value is associated.
- Throws:
ClassCastException- if type class is not equal to the associated type class of the requested state valueIllegalStateException- ifact.getIndex()==0since this suggests that act has no index at all
-
hasActivityState
Returns true if a state value is associated to the specified activity, vehicle and stateId.- Parameters:
act- the activity for which a state value is associated tovehicle- the vehicle for which a state value is associated tostateId- the stateId which is the associated key to the problem state- Returns:
- true if a state value is associated otherwise false
- Throws:
IllegalStateException- ifact.getIndex()==0since this suggests that act has no index at all
-
getActivityState
Returns the associated state value to the specified activity, vehicle and stateId, or null if no state value is associated.If type class is not equal to the associated type class of the requested state value, it throws a ClassCastException.
- Specified by:
getActivityStatein interfaceRouteAndActivityStateGetter- Type Parameters:
T- the type of the class- Parameters:
act- the activity for which a state value is associated tovehicle- the vehicle for which a state value is associated tostateId- the stateId which is the associated key to the problem statetype- the class of the associated state value- Returns:
- the associated state value to the specified activity, vehicle and stateId, or null if no state value is associated.
- Throws:
ClassCastException- if type class is not equal to the associated type class of the requested state valueIllegalStateException- ifact.getIndex()==0since this suggests that act has no index at all
-
getRouteState
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.
- Specified by:
getRouteStatein interfaceRouteAndActivityStateGetter- Type Parameters:
T- the type of the class- Parameters:
route- the route which the associated route key to the route statestateId- the stateId which is the associated key to the route statetype- the class of the associated state value- Returns:
- the route state that is associated to the route and stateId, or null if no state is associated.
- Throws:
ClassCastException- if type class is not equal to the associated type class of the requested state valueIllegalStateException- if!route.isEmpty()andact(0).getIndex()==0since this suggests that act has no index at all
-
hasRouteState
Returns true if a state is assigned to the specified route, vehicle and stateId. Otherwise it returns false.- Parameters:
route- the route for which the state is requestedvehicle- the vehicle for which the state is requestedstateId- the stateId(entifier) for the state that is requested- Returns:
- true if state exists and false otherwise
-
getRouteState
Returns the route state that is assigned to the specified route, vehicle and stateId.Returns null if no state can be found
- Specified by:
getRouteStatein interfaceRouteAndActivityStateGetter- Type Parameters:
T- the type of the class- Parameters:
route- the route for which the state is requestedvehicle- the vehicle for which the state is requestedstateId- the stateId(entifier) for the state that is requestedtype- the type class of the requested state- Returns:
- the actual route state that is assigned to the route, vehicle and stateId
- Throws:
ClassCastException- if specified type is not equal to the memorized typeIllegalStateException- if!route.isEmpty()andact(0).getIndex()==0since this suggests that act has no index at all
-
putActivityState
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.- Type Parameters:
T- the type of the state- Parameters:
act- the activity for which a state value is associated tostateId- the stateId which is the associated key to the activity statestate- the state that is associated to the activity and stateId- Throws:
IllegalStateException- ifact.getIndex() == 0|| stateId.getIndex invalid input: '<' noInternalStates
-
putActivityState
Associates the specified activity, 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.- Type Parameters:
T- the type of the state- Parameters:
act- the activity for which a state value is associated tovehicle- the vehicle for which a state value is associated tostateId- the stateId which is the associated key to the activity statestate- the state that is associated to the activity and stateId- Throws:
IllegalStateException- ifact.getIndex() == 0|| stateId.getIndex invalid input: '<' noInternalStates
-
putRouteState
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.- Type Parameters:
T- the type of the state- Parameters:
route- the route for which a state value is associated tostateId- the stateId which is the associated key to the activity statestate- the state that is associated to the activity and stateId- Throws:
IllegalStateException- if stateId is equal to a stateId that is already used internally.
-
putRouteState
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.- Type Parameters:
T- the type of the state- Parameters:
route- the route for which a state value is associated tovehicle- the vehicle for which a state value is associated tostateId- the stateId which is the associated key to the activity statestate- the state that is associated to the activity and stateId- Throws:
IllegalStateException- if vehicle has no valid index ||stateId.getIndex() invalid input: '<' noInternalStates
-
addStateUpdater
Adds state updater.Note that a state update occurs if route and/or activity states have changed, i.e. if jobs are removed or inserted into a route. Thus here, it is assumed that a state updater is either of type InsertionListener, RuinListener, ActivityVisitor, ReverseActivityVisitor, RouteVisitor, ReverseRouteVisitor.
The following rule pertain for activity/route visitors:These visitors visits all activities/route in a route subsequently in two cases. First, if insertionStart (after ruinStrategies have removed activities from routes) and, second, if a job has been inserted and thus if a route has changed.
- Parameters:
updater- the update to be added
-
addAllStateUpdater
-
informJobInserted
- Specified by:
informJobInsertedin interfaceJobInsertedListener
-
informInsertionStarts
public void informInsertionStarts(Collection<VehicleRoute> vehicleRoutes, Collection<Job> unassignedJobs) - Specified by:
informInsertionStartsin interfaceInsertionStartsListener
-
reCalculateStates
-
informIterationStarts
public void informIterationStarts(int i, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) - Specified by:
informIterationStartsin interfaceIterationStartsListener
-
ruinStarts
Description copied from interface:RuinListenerinforms about ruin-start.- Specified by:
ruinStartsin interfaceRuinListener- Parameters:
routes-
-
ruinEnds
Description copied from interface:RuinListenerinforms about ruin-end.- Specified by:
ruinEndsin interfaceRuinListener- Parameters:
routes-unassignedJobs-
-
removed
Description copied from interface:RuinListenerinforms if aJobhas been removed from aVehicleRoute.- Specified by:
removedin interfaceRuinListener- Parameters:
job-fromRoute-
-
informInsertionEnds
- Specified by:
informInsertionEndsin interfaceInsertionEndsListener
-
updateLoadStates
public void updateLoadStates()Updates load states. -
updateTimeWindowStates
public void updateTimeWindowStates()Updates time-window states. -
timeWindowUpdateIsActivated
public boolean timeWindowUpdateIsActivated() -
updateSkillStates
public void updateSkillStates()Updates skill states.
-