Package org.ddolib.examples.nolayer.tsp
Class TSPProblem
java.lang.Object
org.ddolib.examples.nolayer.tsp.TSPProblem
Traveling Salesman Problem (TSP), using the no-layer modeling API.
Models the classic TSP: starting from and returning to city 0, visit every other city exactly
once while minimizing the total travelled distance. States are represented by TSPState
(current city and set of cities still to visit).
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal double[][]Distance (cost) matrix between cities, indexed by origin and destination city.final intNumber of cities. -
Constructor Summary
ConstructorsConstructorDescriptionTSPProblem(double[][] distanceMatrix) Creates a new unnamed TSP instance from a distance matrix.TSPProblem(String fname) Reads a TSP instance from an XML file. -
Method Summary
Modifier and TypeMethodDescriptionReturns 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.Returns the initial state of the problem.doubleReturns the initial objective value associated with the initial state.booleanChecks whether the given state is a target (sink) state.singleton(int singletonValue) Creates aBitSetcontaining only the given value.toString()transition(TSPState state, int label) Applies a label/action to a state, computing the next state according to the problem's transition function.doubletransitionCost(TSPState 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
-
distanceMatrix
public final double[][] distanceMatrixDistance (cost) matrix between cities, indexed by origin and destination city. -
n
public final int nNumber of cities.
-
-
Constructor Details
-
TSPProblem
public TSPProblem(double[][] distanceMatrix) Creates a new unnamed TSP instance from a distance matrix.- Parameters:
distanceMatrix- distance (cost) matrix between cities, indexed by origin and destination city
-
TSPProblem
Reads a TSP instance from an XML file.- Parameters:
fname- path to the instance file- Throws:
IOException- if the instance file cannot be read or parsed
-
-
Method Details
-
initialState
Description copied from interface:ProblemReturns the initial state of the problem.- Specified by:
initialStatein interfaceProblem<TSPState>- 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<TSPState>- Returns:
- the starting value of the objective function
-
isTarget
Description copied from interface:ProblemChecks whether the given state is a target (sink) state. -
domain
Description copied from interface:ProblemReturns the domain of possible labels/actions from a 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<TSPState>- 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<TSPState>- 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<TSPState>- 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
-
singleton
Creates aBitSetcontaining only the given value.- Parameters:
singletonValue- the value to set in the returned bit set- Returns:
- a bit set with only
singletonValueset
-
toString
-