Class PDPTWProblem

java.lang.Object
org.ddolib.examples.layered.pdptw.PDPTWProblem
All Implemented Interfaces:
Problem<PDPTWState>

public class PDPTWProblem extends Object implements Problem<PDPTWState>
Represents a Pickup and Delivery Problem (PDP) instance with a single vehicle.

In this problem:

  • Nodes may be grouped into pickup-delivery pairs, where a pickup node must be visited before its associated delivery node.
  • There may also be unrelated nodes that are not part of any pair.
  • The vehicle has a capacity limit that restricts how many pickups can be carried simultaneously.
  • The problem is represented as a TSP-like graph with distances between nodes.

The class implements the Problem interface, providing methods for:

States are represented by PDPTWState, including:

  • The set of currently visited nodes
  • The set of nodes still to visit
  • The current vehicle load
  • Field Details

    • n

      public final int n
      Number of nodes in the problem.
    • timeMatrix

      public final double[][] timeMatrix
      Distance matrix between all nodes.
    • maxCapa

      public final int maxCapa
      Maximum capacity of the vehicle.
    • pickupToAssociatedDelivery

      public final HashMap<Integer,Integer> pickupToAssociatedDelivery
      Map of pickup nodes to their associated delivery nodes.
    • deliveryToAssociatedPickup

      public final HashMap<Integer,Integer> deliveryToAssociatedPickup
      Map of delivery nodes to their associated pickup nodes.
    • unrelatedNodes

      public final Set<Integer> unrelatedNodes
      Set of nodes that are not part of any pickup-delivery pair.
    • timeWindows

      public final TimeWindow[] timeWindows
      Time window associated to each node.
  • Constructor Details

    • PDPTWProblem

      public PDPTWProblem(double[][] timeMatrix, HashMap<Integer,Integer> pickupToAssociatedDelivery, int maxCapa, TimeWindow[] timeWindows, Optional<Double> knownSolutionValue, Boolean strengthenTimeWindows)
      Constructs a PDPTWProblem from a distance matrix, a map of pickup-delivery pairs, and a maximum vehicle capacity.
      Parameters:
      timeMatrix - distance matrix between all nodes
      pickupToAssociatedDelivery - mapping from pickup nodes to delivery nodes
      maxCapa - maximum capacity of the vehicle
      timeWindows - the time window associated to each node
      knownSolutionValue - an optional known solution value of the instance
      strengthenTimeWindows - whether the time windows should be strengthened at construction time
    • PDPTWProblem

      public PDPTWProblem(String fname) throws IOException
      Constructs a PDPProblem by reading an instance from a file.

      The file format should include:

      • The number of nodes and optionally the optimal value.
      • The distance matrix between nodes.
      • The pickup-delivery pairs.
      Parameters:
      fname - path to the instance file
      Throws:
      IOException - if the file cannot be read
  • Method Details

    • strengthenTimeWindows

      public void strengthenTimeWindows()
      Strengthens the time windows of pickup and delivery nodes using the precedence and travel time relations between each pickup and its associated delivery.
    • saveToFile

      public void saveToFile(String fname) throws IOException
      Saves this PDPTW instance to a file in the format expected by PDPTWProblem(String).
      Parameters:
      fname - the path to the output file
      Throws:
      IOException - if an I/O error occurs while writing the file
    • nbVars

      public int nbVars()
      Returns the number of variables (decisions) in the problem.

      Note: the last decision corresponds to returning to the depot (node 0).

      Specified by:
      nbVars in interface Problem<PDPTWState>
      Returns:
      number of variables
    • initialState

      public PDPTWState initialState()
      Description copied from interface: Problem
      Returns the initial state of the problem.
      Specified by:
      initialState in interface Problem<PDPTWState>
      Returns:
      the state representing the starting point of the optimization
    • singleton

      public BitSet singleton(int singletonValue)
      Builds a singleton BitSet containing only the given value.
      Parameters:
      singletonValue - the value to set in the returned set
      Returns:
      a new BitSet with only singletonValue set
    • initialValue

      public double initialValue()
      Description copied from interface: Problem
      Returns the initial objective value associated with the initial state.
      Specified by:
      initialValue in interface Problem<PDPTWState>
      Returns:
      the starting value of the objective function
    • optimalValue

      public Optional<Double> optimalValue()
      Description copied from interface: Problem
      Returns the known optimal value of the problem, if available.

      Note: This value should correspond to the expected output of the solver. For maximization problems, be careful with negative values.

      Specified by:
      optimalValue in interface Problem<PDPTWState>
      Returns:
      an Optional containing the known optimal value, or empty if unknown
    • domain

      public Iterator<Integer> domain(PDPTWState state, int var)
      Description copied from interface: Problem
      Returns the domain of possible values for a given variable when applied to a specific state.
      Specified by:
      domain in interface Problem<PDPTWState>
      Parameters:
      state - the current state
      var - the variable index whose domain is queried
      Returns:
      an iterator over all feasible values for the variable in this state
    • transition

      public PDPTWState transition(PDPTWState state, Decision decision)
      Description copied from interface: Problem
      Applies a decision to a state, computing the next state according to the problem's transition function.
      Specified by:
      transition in interface Problem<PDPTWState>
      Parameters:
      state - the state from which the transition originates
      decision - the decision to apply
      Returns:
      the resulting state after applying the decision
    • transitionCost

      public double transitionCost(PDPTWState state, Decision decision)
      Description copied from interface: Problem
      Computes the change in objective value resulting from applying a decision to a given state.
      Specified by:
      transitionCost in interface Problem<PDPTWState>
      Parameters:
      state - the state from which the transition originates
      decision - the decision to apply
      Returns:
      the incremental objective cost/value associated with this decision
    • evaluate

      public double evaluate(int[] solution) throws InvalidSolutionException
      Description copied from interface: Problem
      Given a solution such that solution[i] is the value of the variable x_i, returns the value of this solution and checks if the solution respects the problem's constraints.

      Note: For maximization problems, the returned value is minus the computed value.

      Specified by:
      evaluate in interface Problem<PDPTWState>
      Parameters:
      solution - a solution of the problem
      Returns:
      the value of the input solution
      Throws:
      InvalidSolutionException - if the solution does not respect the problem's constraints
    • toString

      public String toString()
      Overrides:
      toString in class Object