Class TspAnnealingProblem

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

public final class TspAnnealingProblem extends Object implements AnnealingProblem<TspSolution>
AnnealingProblem adapter that connects the TSP model to the MDI simulated annealing framework.

This class is responsible for three things the framework requires:

  1. Solution generation — randomSolution(java.util.Random) produces a uniformly random permutation of all city indices using a Fisher-Yates shuffle. This is used both for the initial working solution and, during initialization, by the TemperatureHeuristic to sample the energy distribution for T₀ estimation.
  2. Energy evaluation — energy(edu.cnu.mdi.sim.simanneal.tspdemo.TspSolution) delegates to TspSolution.getTourLength(), which sums edge distances including any river penalty or bonus.
  3. Move generation — randomMove(java.util.Random, edu.cnu.mdi.sim.simanneal.tspdemo.TspSolution) returns a reusable TspMove instance. The move object is allocated once and cached; its random parameters are chosen freshly each step inside TspMove.prepare(TspSolution).

Move reuse

Rather than allocating a new TspMove on every call to randomMove(java.util.Random, edu.cnu.mdi.sim.simanneal.tspdemo.TspSolution), this class caches a single instance in cachedMove. This is safe because the framework's inner loop always calls TspMove.prepare(edu.cnu.mdi.sim.simanneal.tspdemo.TspSolution) before TspMove.deltaE(edu.cnu.mdi.sim.simanneal.tspdemo.TspSolution) and TspMove.apply(edu.cnu.mdi.sim.simanneal.tspdemo.TspSolution) or TspMove.undo(edu.cnu.mdi.sim.simanneal.tspdemo.TspSolution), so the move is always in a freshly prepared state when used. Eliminating per-step allocation avoids generating millions of short-lived objects and the GC pressure that entails.

Thread safety

This class is not thread-safe. It is owned exclusively by the simulation thread and must not be shared across threads.