Package backend.TSP
Class TemplateTSP
java.lang.Object
backend.TSP.TemplateTSP
- All Implemented Interfaces:
TSP
- Direct Known Subclasses:
TSP2
Template for solving a TSP instance using branch-and-bound.
This abstract class implements the generic search flow and leaves two
strategy points to subclasses: bound(Long, Collection) and
iterator(Long, Collection, Graph). The methods and fields use a
Graph instance supplied to #chercheSolution(int, Graph).
Notes on behavior:
- All public methods operate on the Graph instance stored in g.
- The search respects a time limit (milliseconds) passed to
#chercheSolution(int, Graph). When the limit is exceeded the
search returns early.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract doublebound(Long sommetCourant, Collection<Long> nonVus) Compute a lower bound for the cost of completing the tour.voidStart the branch-and-bound search to find a TSP solution.doubleReturn the cost of the best solution found by the last search.Return the full solution found by the last search as an ordered list of node idsReturn the full solution found by the last search as an ordered set of node ids.Return the full solution found by the last search as an ordered set of maps of paths between points of interest.iterator(Long sommetCrt, Collection<Long> nonVus, Graph g) Provide an iterator over candidate successors ofsommetCrt.booleanvalidateIncompleteOrder(List<Long> order) Validate the solution order
-
Field Details
-
g
-
-
Constructor Details
-
TemplateTSP
-
-
Method Details
-
chercheSolution
public void chercheSolution()Start the branch-and-bound search to find a TSP solution.- Specified by:
chercheSolutionin interfaceTSP- Parameters:
timeLimit- time limit in milliseconds; if <= 0 the method returns immediately without searchingg- theGraphto solve; stored in this instance Side effects: populatessolutionOrderandcoutMeilleureSolutionwhen a solution is found. The method returns early if the time limit elapses.
-
getSolutionOrder
Return the full solution found by the last search as an ordered list of node ids- Specified by:
getSolutionOrderin interfaceTSP- Returns:
- the solution ordered as a LinkedList of node ids
-
getSolutionOrderWithArrivalTime
Return the full solution found by the last search as an ordered set of node ids.- Specified by:
getSolutionOrderWithArrivalTimein interfaceTSP- Returns:
- the solution ordered as a LinkedHashSet of node ids with their time of arrival
-
getSolutionPath
Return the full solution found by the last search as an ordered set of maps of paths between points of interest.- Specified by:
getSolutionPathin interfaceTSP- Returns:
- the solution ordered as a LinkedHashSet of maps of paths (from node to node)
-
getCoutSolution
public double getCoutSolution()Return the cost of the best solution found by the last search.- Specified by:
getCoutSolutionin interfaceTSP- Returns:
- the cost as a double, or -1 when no graph is set
-
validateIncompleteOrder
Validate the solution order- Parameters:
List-order - Returns:
- true if the solution is valid, false otherwise
-
bound
Compute a lower bound for the cost of completing the tour.- Parameters:
sommetCourant- current node idnonVus- collection of not-yet-visited node ids- Returns:
- a lower bound (minimum possible additional cost) for paths in
the current graph that start at
sommetCourant, visit allnonVusexactly once and return to the start
-
iterator
Provide an iterator over candidate successors ofsommetCrt.- Parameters:
sommetCrt- current node idnonVus- collection of nodes not yet visitedg- the Graph instance (same as this.g)- Returns:
- an Iterator over node ids from
nonVusthat are valid successors ofsommetCrt. Implementations should return an empty iterator rather than null when there are no candidates.
-