Class TspSolution

java.lang.Object
edu.cnu.mdi.sim.simanneal.tspdemo.TspSolution
All Implemented Interfaces:
AnnealingSolution<TspSolution>

public class TspSolution extends Object implements AnnealingSolution<TspSolution>
A TSP solution represented as a permutation of city indices.

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
    Modifier and Type
    Field
    Description
    final TspModel
    The immutable TSP model shared by all solutions in a run.
    int[]
    Permutation of city indices [0, cityCount) defining the tour.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Construct an uninitialised solution for the given model.
  • Method Summary

    Modifier and Type
    Method
    Description
    Return an independent deep copy of this solution.
    double
    edgeLength(int a, int b)
    Return the distance between two cities as given by the model.
    double
    Compute the total length of the closed tour.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

  • Method Details

    • copy

      public TspSolution copy()
      Return an independent deep copy of this solution.

      The copy shares the same immutable model reference but has its own clone of the tour array, so mutations to either solution do not affect the other.

      Specified by:
      copy in interface AnnealingSolution<TspSolution>
      Returns:
      a new TspSolution with 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 city
      b - index of the second city
      Returns:
      distance (including any river penalty or bonus)