Package org.ddolib.examples.pdp
Class PDPProblem
java.lang.Object
org.ddolib.examples.pdp.PDPProblem
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:
- Number of variables
nbVars() - Initial state
initialState() - Transition function
transition(PDPState, Decision) - Transition cost
transitionCost(PDPState, Decision) - Domain of possible decisions
domain(PDPState, int)
States are represented by PDPState, including:
- The set of currently visited nodes
- The set of nodes still to visit
- The current vehicle load
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal double[][]Distance matrix between all nodes.final intMaximum capacity of the vehicle.intNumber of nodes in the problem.Map of pickup nodes to their associated delivery nodes.Set of nodes that are not part of any pickup-delivery pair. -
Constructor Summary
ConstructorsConstructorDescriptionPDPProblem(double[][] distanceMatrix, HashMap<Integer, Integer> pickupToAssociatedDelivery, int maxCapa) Constructs a PDPProblem from a distance matrix, a map of pickup-delivery pairs, and a maximum vehicle capacity.PDPProblem(String fname) Constructs a PDPProblem by reading an instance from a file. -
Method Summary
Modifier and TypeMethodDescriptionReturns the domain of possible decisions (nodes to visit) from a given state and variable index.doubleeval(int[] solution) Evaluates a solution represented as an array of node indices.doubleevaluate(int[] solution) Given a solution such thatsolution[i]is the value of the variablex_i, returns the value of this solution and checks if the solution respects the problem's constraints.Returns the initial state of the problem.doubleReturns the initial value of the problem.intnbVars()Returns the number of variables (decisions) in the problem.Returns the known optimal value of the problem, if available.singleton(int singletonValue) toString()Returns a string representation of the PDP instance.transition(PDPState state, Decision decision) Computes the next state given a current state and a decision.doubletransitionCost(PDPState state, Decision decision) Computes the cost of transitioning from a state via a decision.
-
Field Details
-
distanceMatrix
public final double[][] distanceMatrixDistance matrix between all nodes. -
maxCapa
public final int maxCapaMaximum capacity of the vehicle. -
n
public int nNumber of nodes in the problem. -
pickupToAssociatedDelivery
Map of pickup nodes to their associated delivery nodes.
-
-
Constructor Details
-
PDPProblem
public PDPProblem(double[][] distanceMatrix, HashMap<Integer, Integer> pickupToAssociatedDelivery, int maxCapa) Constructs a PDPProblem from a distance matrix, a map of pickup-delivery pairs, and a maximum vehicle capacity.- Parameters:
distanceMatrix- distance matrix between all nodespickupToAssociatedDelivery- mapping from pickup nodes to delivery nodesmaxCapa- maximum capacity of the vehicle
-
PDPProblem
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
-
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).
-
initialState
Returns the initial state of the problem.- Specified by:
initialStatein interfaceProblem<PDPState>- Returns:
- initial
PDPStatewith vehicle at the depot and all other nodes unvisited
-
initialValue
public double initialValue()Returns the initial value of the problem.- Specified by:
initialValuein interfaceProblem<PDPState>- Returns:
- 0
-
domain
Returns the domain of possible decisions (nodes to visit) from a given state and variable index. -
transition
Computes the next state given a current state and a decision. -
transitionCost
Computes the cost of transitioning from a state via a decision.Typically corresponds to the travel distance from the current node to the chosen node.
- Specified by:
transitionCostin interfaceProblem<PDPState>- Parameters:
state- currentPDPStatedecision- theDecisionmade- Returns:
- cost of the transition
-
optimalValue
Returns the known optimal value of the problem, if available.- Specified by:
optimalValuein interfaceProblem<PDPState>- Returns:
- optional optimal value
-
evaluate
Description copied from interface:ProblemGiven a solution such thatsolution[i]is the value of the variablex_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:
evaluatein interfaceProblem<PDPState>- Parameters:
solution- A solution of the problem.- Returns:
- The value of the input solution
- Throws:
InvalidSolutionException- If the solution does not respect problem's constraints.
-
singleton
-
toString
Returns a string representation of the PDP instance. -
eval
public double eval(int[] solution) Evaluates a solution represented as an array of node indices.- Parameters:
solution- array of node indices representing the route- Returns:
- total distance of the solution, or -1 if it violates vehicle capacity
-