Class TspSolution
- All Implemented Interfaces:
AnnealingSolution<TspSolution>
The permutation tour defines a closed Hamiltonian cycle through all
cities in model. City tour[i] is visited immediately before
tour[(i+1) % n], and the last city connects back to the first.
Energy
The energy (cost) of a solution is its total tour length, computed by
getTourLength(). Distances between cities are obtained from
TspModel.getDistance(int, int), which incorporates the optional
river penalty or bonus.
Copying
copy() performs a deep copy: the returned TspSolution
shares the same immutable model reference but has an independent
clone of the tour array. The annealing framework calls
copy() to snapshot the best solution found so far; move operators
mutate the working solution in-place without affecting the snapshot.
Thread safety
Instances are not thread-safe. The working solution is owned exclusively by
the simulation thread. Snapshots produced by copy() are handed to
the view, which reads them on the EDT.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionTspSolution(TspModel model) Construct an uninitialised solution for the given model. -
Method Summary
Modifier and TypeMethodDescriptioncopy()Return an independent deep copy of this solution.doubleedgeLength(int a, int b) Return the distance between two cities as given by the model.doubleCompute the total length of the closed tour.
-
Field Details
-
model
The immutable TSP model shared by all solutions in a run.Holds city coordinates, the river position, and the distance function. Shared by reference — never modified after construction.
-
tour
public int[] tourPermutation of city indices[0, cityCount)defining the tour.The tour visits
tour[0], tour[1], ..., tour[n-1], tour[0]. Modified in-place by move operators (TspMove.apply(edu.cnu.mdi.sim.simanneal.tspdemo.TspSolution)/TspMove.undo(edu.cnu.mdi.sim.simanneal.tspdemo.TspSolution)).
-
-
Constructor Details
-
TspSolution
Construct an uninitialised solution for the given model.tourisnulluntil set byTspAnnealingProblem.randomSolution(java.util.Random).- Parameters:
model- the TSP model (non-null)
-
-
Method Details
-
copy
Return an independent deep copy of this solution.The copy shares the same immutable
modelreference but has its own clone of thetourarray, so mutations to either solution do not affect the other.- Specified by:
copyin interfaceAnnealingSolution<TspSolution>- Returns:
- a new
TspSolutionwith a cloned tour array
-
getTourLength
public double getTourLength()Compute the total length of the closed tour.Sums
TspModel.getDistance(int, int)for every consecutive pair of cities in the tour, including the wrap-around edge from the last city back to the first. If the model includes a river, crossing penalties or bonuses are incorporated by the distance function.- Returns:
- total tour length (may be negative if river crossings carry a bonus that exceeds the Euclidean distances)
-
edgeLength
public double edgeLength(int a, int b) Return the distance between two cities as given by the model.Convenience wrapper used by
TspMove.deltaE(edu.cnu.mdi.sim.simanneal.tspdemo.TspSolution)to avoid exposing the model directly in ΔE calculations.- Parameters:
a- index of the first cityb- index of the second city- Returns:
- distance (including any river penalty or bonus)
-