Class TSPTWProblem
java.lang.Object
org.ddolib.examples.nolayer.tsptw.TSPTWProblem
- All Implemented Interfaces:
Problem<TSPTWState>
Traveling Salesman Problem with Time Windows (TSPTW), using the no-layer modeling API.
Models the TSPTW: starting from and returning to city 0, visit every other city exactly once,
arriving within its time window (waiting if arriving early), while minimizing the total elapsed
time. States are represented by TSPTWState (current city, current time, and set of cities
still to visit).
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal int[][]Travel distance (duration) matrix between cities, indexed by origin and destination city.final intNumber of cities.final TimeWindow[]Time window of each city, indexed by city number. -
Constructor Summary
ConstructorsConstructorDescriptionTSPTWProblem(int[][] distance, TimeWindow[] timeWindows) Creates a new TSPTW instance. -
Method Summary
Modifier and TypeMethodDescriptiondomain(TSPTWState state) Returns the domain of possible labels/actions from a given state.doubleGiven a solution (a sequence of applied labels), returns its value and checks if the solution respects the problem's constraints.static TSPTWProblemReads a TSPTW instance from a file.Returns the initial state of the problem.doubleReturns the initial objective value associated with the initial state.booleanisTarget(TSPTWState state) Checks whether the given state is a target (sink) state.toString()transition(TSPTWState state, int label) Applies a label/action to a state, computing the next state according to the problem's transition function.doubletransitionCost(TSPTWState state, int label) Computes the change in objective value resulting from applying a label to a given state.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.ddolib.nolayer.modeling.Problem
hasNonNegativeCosts, optimalValue
-
Field Details
-
distance
public final int[][] distanceTravel distance (duration) matrix between cities, indexed by origin and destination city. -
timeWindows
Time window of each city, indexed by city number. -
nbVars
public final int nbVarsNumber of cities.
-
-
Constructor Details
-
TSPTWProblem
Creates a new TSPTW instance.- Parameters:
distance- travel distance (duration) matrix between cities, indexed by origin and destination citytimeWindows- time window of each city, indexed by city number
-
-
Method Details
-
fromFile
Reads a TSPTW instance from a file.- Parameters:
fname- path to the instance file- Returns:
- the TSPTW instance described by the file
- Throws:
IOException- if the instance file cannot be read
-
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
-
isTarget
Description copied from interface:ProblemChecks whether the given state is a target (sink) state.- Specified by:
isTargetin interfaceProblem<TSPTWState>- Parameters:
state- the current state- Returns:
- true if the state is a target state, false otherwise
-
domain
Description copied from interface:ProblemReturns the domain of possible labels/actions from a given state.- Specified by:
domainin interfaceProblem<TSPTWState>- Parameters:
state- the current state- Returns:
- an iterator over all feasible labels for the given state
-
transition
Description copied from interface:ProblemApplies a label/action 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 originateslabel- the label to apply- Returns:
- the resulting state after applying the label
-
transitionCost
Description copied from interface:ProblemComputes the change in objective value resulting from applying a label to a given state.- Specified by:
transitionCostin interfaceProblem<TSPTWState>- Parameters:
state- the state from which the transition originateslabel- the label to apply- Returns:
- the incremental objective cost/value associated with this label
-
evaluate
Description copied from interface:ProblemGiven a solution (a sequence of applied labels), returns its value and checks if the solution respects the problem's constraints.- Specified by:
evaluatein interfaceProblem<TSPTWState>- Parameters:
solution- a solution of the problem (sequence of labels)- Returns:
- the value of the input solution
- Throws:
InvalidSolutionException- if the solution does not respect problem's constraints
-
toString
-