Package org.ddolib.examples.tsptw
Class TSPTWProblem
java.lang.Object
org.ddolib.examples.tsptw.TSPTWProblem
- All Implemented Interfaces:
Problem<TSPTWState>
Class representing an instance of the Traveling Salesman Problem with Time Windows (TSPTW).
Each node has a time window during which it must be visited, and travel between nodes
is defined by a distance matrix. This class implements the Problem interface
for use in decision diagram solvers.
The problem instance can optionally provide the known optimal solution.
Data file format
- First line: number of nodes (variables). Optionally, a second value may be provided for the optimal objective.
- Next lines: the distance matrix, one row per node.
- Following lines: time windows for each node, specified as two integers (start and end) per line.
The distance matrix defines the travel times between nodes, and the TimeWindow array defines
the allowed visit times for each node.
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal int[][]Distance matrix between nodes.Optional known optimal value for the instance.final TimeWindow[]Time windows for each node. -
Constructor Summary
ConstructorsConstructorDescriptionTSPTWProblem(String fname) Constructs a TSPTW problem instance from a data file. -
Method Summary
Modifier and TypeMethodDescriptiondomain(TSPTWState state, int var) Returns the domain of possible values for a given variable when applied to a specific state.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 objective value associated with the initial state.intnbVars()Returns the known optimal value of the problem, if available.toString()transition(TSPTWState state, Decision decision) Applies a decision to a state, computing the next state according to the problem's transition function.doubletransitionCost(TSPTWState state, Decision decision) Computes the change in objective value resulting from applying a decision to a given state.
-
Field Details
-
distance
public final int[][] distanceDistance matrix between nodes. distance[i][j] is the travel time from node i to node j. -
timeWindows
Time windows for each node. -
optimal
Optional known optimal value for the instance.
-
-
Constructor Details
-
TSPTWProblem
Constructs a TSPTW problem instance from a data file.- Parameters:
fname- Path to the input file containing the TSPTW instance.- Throws:
IOException- If an error occurs while reading the file.
-
-
Method Details
-
toString
-
nbVars
public int nbVars()- Specified by:
nbVarsin interfaceProblem<TSPTWState>- Returns:
- the total number of decision variables in this problem
-
initialState
Description copied from interface:ProblemReturns the initial state of the problem.- Specified by:
initialStatein interfaceProblem<TSPTWState>- Returns:
- the state representing the starting point of the optimization
-
initialValue
public double initialValue()Description copied from interface:ProblemReturns the initial objective value associated with the initial state.- Specified by:
initialValuein interfaceProblem<TSPTWState>- Returns:
- the starting value of the objective function
-
domain
Description copied from interface:ProblemReturns the domain of possible values for a given variable when applied to a specific state.- Specified by:
domainin interfaceProblem<TSPTWState>- Parameters:
state- the current statevar- the variable index whose domain is queried- Returns:
- an iterator over all feasible values for the variable in this state
-
transition
Description copied from interface:ProblemApplies a decision to a state, computing the next state according to the problem's transition function.- Specified by:
transitionin interfaceProblem<TSPTWState>- Parameters:
state- the state from which the transition originatesdecision- the decision to apply- Returns:
- the resulting state after applying the decision
-
transitionCost
Description copied from interface:ProblemComputes the change in objective value resulting from applying a decision to a given state.- Specified by:
transitionCostin interfaceProblem<TSPTWState>- Parameters:
state- the state from which the transition originatesdecision- the decision to apply- Returns:
- the incremental objective cost/value associated with this decision
-
optimalValue
Description copied from interface:ProblemReturns 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:
optimalValuein interfaceProblem<TSPTWState>- Returns:
- an
Optionalcontaining the known optimal value, or empty if unknown
-
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<TSPTWState>- 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.
-