Class StateManager

java.lang.Object
com.graphhopper.jsprit.core.algorithm.state.StateManager
All Implemented Interfaces:
IterationStartsListener, SearchStrategyModuleListener, VehicleRoutingAlgorithmListener, InsertionEndsListener, InsertionListener, InsertionStartsListener, JobInsertedListener, RuinListener, RouteAndActivityStateGetter

Manages states.

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 Details

    • StateManager

      public StateManager(VehicleRoutingProblem vehicleRoutingProblem)
      Constructs the stateManager with the specified VehicleRoutingProblem.
      Parameters:
      vehicleRoutingProblem - the corresponding VehicleRoutingProblem
  • Method Details

    • getVrp

      public VehicleRoutingProblem getVrp()
      Returns the VehicleRoutingProblem associated with this StateManager.
      Returns:
      the VRP
    • createStateId

      public StateId createStateId(String name)
      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

      public <T> void putProblemState(StateId stateId, Class<T> type, T state)
      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 state
      type - the type of the problem state
      state - the actual state value
    • getProblemState

      public <T> T getProblemState(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.
      Type Parameters:
      T - the type
      Parameters:
      stateId - the stateId which is the associated key to the problem state
      type - 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

      public <T> T getActivityState(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.

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

      Specified by:
      getActivityState in interface RouteAndActivityStateGetter
      Type Parameters:
      T - the type
      Parameters:
      act - the activity for which a state value is associated to
      stateId - the stateId for which a state value is associated to
      type - 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 value
      IllegalStateException - if act.getIndex()==0 since this suggests that act has no index at all
    • hasActivityState

      public boolean hasActivityState(TourActivity act, Vehicle vehicle, StateId stateId)
      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 to
      vehicle - the vehicle for which a state value is associated to
      stateId - the stateId which is the associated key to the problem state
      Returns:
      true if a state value is associated otherwise false
      Throws:
      IllegalStateException - if act.getIndex()==0 since this suggests that act has no index at all
    • getActivityState

      public <T> T getActivityState(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.

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

      Specified by:
      getActivityState in interface RouteAndActivityStateGetter
      Type Parameters:
      T - the type of the class
      Parameters:
      act - the activity for which a state value is associated to
      vehicle - the vehicle for which a state value is associated to
      stateId - the stateId which is the associated key to the problem state
      type - 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 value
      IllegalStateException - if act.getIndex()==0 since this suggests that act has no index at all
    • getRouteState

      public <T> T getRouteState(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.

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

      Specified by:
      getRouteState in interface RouteAndActivityStateGetter
      Type Parameters:
      T - the type of the class
      Parameters:
      route - the route which the associated route key to the route state
      stateId - the stateId which is the associated key to the route state
      type - 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 value
      IllegalStateException - if !route.isEmpty() and act(0).getIndex()==0 since this suggests that act has no index at all
    • hasRouteState

      public boolean hasRouteState(VehicleRoute route, Vehicle vehicle, StateId stateId)
      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 requested
      vehicle - the vehicle for which the state is requested
      stateId - the stateId(entifier) for the state that is requested
      Returns:
      true if state exists and false otherwise
    • getRouteState

      public <T> T getRouteState(VehicleRoute route, Vehicle vehicle, StateId stateId, Class<T> type)
      Returns the route state that is assigned to the specified route, vehicle and stateId.

      Returns null if no state can be found

      Specified by:
      getRouteState in interface RouteAndActivityStateGetter
      Type Parameters:
      T - the type of the class
      Parameters:
      route - the route for which the state is requested
      vehicle - the vehicle for which the state is requested
      stateId - the stateId(entifier) for the state that is requested
      type - 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 type
      IllegalStateException - if !route.isEmpty() and act(0).getIndex()==0 since this suggests that act has no index at all
    • putActivityState

      public <T> void putActivityState(TourActivity act, StateId stateId, T 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.
      Type Parameters:
      T - the type of the state
      Parameters:
      act - the activity for which a state value is associated to
      stateId - the stateId which is the associated key to the activity state
      state - the state that is associated to the activity and stateId
      Throws:
      IllegalStateException - if act.getIndex() == 0 || stateId.getIndex invalid input: '<' noInternalStates
    • putActivityState

      public <T> void putActivityState(TourActivity act, Vehicle vehicle, StateId stateId, T state)
      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 to
      vehicle - the vehicle for which a state value is associated to
      stateId - the stateId which is the associated key to the activity state
      state - the state that is associated to the activity and stateId
      Throws:
      IllegalStateException - if act.getIndex() == 0 || stateId.getIndex invalid input: '<' noInternalStates
    • putRouteState

      public <T> void putRouteState(VehicleRoute route, StateId stateId, T 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.
      Type Parameters:
      T - the type of the state
      Parameters:
      route - the route for which a state value is associated to
      stateId - the stateId which is the associated key to the activity state
      state - 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

      public <T> void putRouteState(VehicleRoute route, Vehicle vehicle, StateId stateId, T 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.
      Type Parameters:
      T - the type of the state
      Parameters:
      route - the route for which a state value is associated to
      vehicle - the vehicle for which a state value is associated to
      stateId - the stateId which is the associated key to the activity state
      state - the state that is associated to the activity and stateId
      Throws:
      IllegalStateException - if vehicle has no valid index || stateId.getIndex() invalid input: '<' noInternalStates
    • addStateUpdater

      public void addStateUpdater(StateUpdater updater)
      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

      public void addAllStateUpdater(Collection<StateUpdater> updaters)
    • informJobInserted

      public void informJobInserted(Job job2insert, VehicleRoute inRoute, InsertionData insertionData)
      Specified by:
      informJobInserted in interface JobInsertedListener
    • informInsertionStarts

      public void informInsertionStarts(Collection<VehicleRoute> vehicleRoutes, Collection<Job> unassignedJobs)
      Specified by:
      informInsertionStarts in interface InsertionStartsListener
    • reCalculateStates

      public void reCalculateStates(VehicleRoute route)
    • informIterationStarts

      public void informIterationStarts(int i, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions)
      Specified by:
      informIterationStarts in interface IterationStartsListener
    • ruinStarts

      public void ruinStarts(Collection<VehicleRoute> routes)
      Description copied from interface: RuinListener
      informs about ruin-start.
      Specified by:
      ruinStarts in interface RuinListener
      Parameters:
      routes -
    • ruinEnds

      public void ruinEnds(Collection<VehicleRoute> routes, Collection<Job> unassignedJobs)
      Description copied from interface: RuinListener
      informs about ruin-end.
      Specified by:
      ruinEnds in interface RuinListener
      Parameters:
      routes -
      unassignedJobs -
    • removed

      public void removed(Job job, VehicleRoute fromRoute)
      Description copied from interface: RuinListener
      informs if a Job has been removed from a VehicleRoute.
      Specified by:
      removed in interface RuinListener
      Parameters:
      job -
      fromRoute -
    • informInsertionEnds

      public void informInsertionEnds(Collection<VehicleRoute> vehicleRoutes, Collection<Job> badJobs)
      Specified by:
      informInsertionEnds in interface InsertionEndsListener
    • 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.