Class VehicleRoutingAlgorithm

java.lang.Object
com.graphhopper.jsprit.core.algorithm.VehicleRoutingAlgorithm

public class VehicleRoutingAlgorithm extends Object
Algorithm that solves a VehicleRoutingProblem.
Author:
stefan schroeder
  • Constructor Details

  • Method Details

    • addInitialSolution

      public void addInitialSolution(VehicleRoutingProblemSolution solution)
      Adds solution to the collection of initial solutions. This method may lead to errors if tour activities in the solution are different to the ones in the VRP (including differences in indexing)
      Parameters:
      solution - the solution to be added
    • addInitialSolution

      public void addInitialSolution(SolutionSpec spec)
      Adds a solution spec as an initial solution.

      The spec will be materialized into a solution using the VRP's vehicles and jobs. This is the preferred way to provide an initial solution as it avoids issues with activity indexing and object references.

      Unlike locked routes set via VRP.Builder.setInitialRouteSpecs(), this initial solution is fully optimizable - jobs can be moved between routes freely.

      Parameters:
      spec - the solution specification
      Throws:
      IllegalArgumentException - if the spec references missing vehicles or jobs
    • setPrematureAlgorithmTermination

      public void setPrematureAlgorithmTermination(PrematureAlgorithmTermination prematureAlgorithmTermination)
      Sets premature termination and overrides existing termination criteria. If existing ones should not be overridden use .addTerminationCriterion(...).
      Parameters:
      prematureAlgorithmTermination - the termination criterion
    • addTerminationCriterion

      public void addTerminationCriterion(PrematureAlgorithmTermination terminationCriterion)
      Adds a termination criterion to the collection of already specified termination criteria. If one of the termination criteria is fulfilled, the algorithm terminates prematurely.
      Parameters:
      terminationCriterion - the termination criterion
    • getSearchStrategyManager

      public SearchStrategyManager getSearchStrategyManager()
      Returns:
      SearchStrategyManager
    • searchSolutions

      public Collection<VehicleRoutingProblemSolution> searchSolutions()
      Runs the vehicle routing algorithm and returns a number of generated solutions.

      The algorithm runs as long as it is specified in nuOfIterations and prematureBreak. In each iteration it selects a searchStrategy according to searchStrategyManager and runs the strategy to improve solutions.

      Note that clients are allowed to observe/listen the algorithm. See VehicleRoutingAlgorithmListener and its according listeners.

      Returns:
      Collection the solutions
    • getAlgorithmListeners

      public VehicleRoutingAlgorithmListeners getAlgorithmListeners()
    • addListener

      public void addListener(VehicleRoutingAlgorithmListener l)
    • addEventListener

      public void addEventListener(AlgorithmEventListener listener)
      Adds an event listener for receiving algorithm events.

      Event listeners provide a unified way to observe all aspects of the algorithm's execution, including iteration lifecycle, ruin/recreate phases, and acceptance decisions.

      Parameters:
      listener - the event listener to add
    • removeEventListener

      public void removeEventListener(AlgorithmEventListener listener)
      Removes an event listener.
      Parameters:
      listener - the event listener to remove
    • emit

      public void emit(AlgorithmEvent event)
      Emits an event to all registered event listeners.

      This method is intended for internal use by the algorithm and its components. It has minimal overhead when no listeners are registered.

      Parameters:
      event - the event to emit
    • hasEventListeners

      public boolean hasEventListeners()
      Returns whether any event listeners are registered.

      This can be used to avoid creating event objects when no listeners are registered, minimizing performance overhead.

      Returns:
      true if at least one event listener is registered
    • setMaxIterations

      public void setMaxIterations(int maxIterations)
      Sets max number of iterations.
      Parameters:
      maxIterations - max number of iteration the algorithm runs
    • getMaxIterations

      public int getMaxIterations()
      Gets max number of iterations.
      Returns:
      max number of iterations
    • getObjectiveFunction

      public SolutionCostCalculator getObjectiveFunction()