Class SimulatedAnnealingSimulation<S extends AnnealingSolution<S>>

java.lang.Object
edu.cnu.mdi.sim.simanneal.SimulatedAnnealingSimulation<S>
Type Parameters:
S - concrete solution type for the annealing problem
All Implemented Interfaces:
Simulation

public final class SimulatedAnnealingSimulation<S extends AnnealingSolution<S>> extends Object implements Simulation
A Simulation implementation that performs Simulated Annealing (SA) to minimize an energy (cost) function over a problem-defined solution space.

This class is part of the MDI simulation framework integration:

Core algorithm

Each iteration proposes a random move from the current solution (via AnnealingProblem.randomMove(Random, AnnealingSolution)), applies it, evaluates the resulting energy, and accepts or rejects the move using the Metropolis criterion:

   accept if ΔE ≤ 0
   otherwise accept with probability exp(-ΔE / T)
 

The temperature T decreases over time according to a schedule; this implementation delegates cooling to the configured AnnealingSchedule. An initial temperature T0 is estimated by a TemperatureHeuristic.

Threading / UI

The simulation engine invokes init(SimulationContext) once and then calls step(SimulationContext) repeatedly until it returns false. Any UI-related updates (messages, progress, refresh) are posted to the engine, which is responsible for EDT marshalling and listener notification.

Move models

A problem explicitly returns either a ReversibleAnnealingMove, which mutates in place and is undone on rejection, or a CandidateAnnealingMove, which leaves the current solution unchanged and returns a separate proposal.

  • Constructor Details

    • SimulatedAnnealingSimulation

      public SimulatedAnnealingSimulation(AnnealingProblem<S> problem, SimulatedAnnealingConfig cfg, AnnealingSchedule schedule, TemperatureHeuristic<S> tempHeuristic)
      Construct a simulated annealing simulation with explicit configuration and policies.
      Parameters:
      problem - the annealing problem definition (non-null)
      cfg - configuration controlling cooling and pacing (non-null)
      schedule - stopping and temperature reporting policy (non-null)
      tempHeuristic - heuristic for estimating T0 (non-null)
      Throws:
      NullPointerException - if any argument is null
  • Method Details

    • setFeedback

      public void setFeedback(AnnealingFeedback feedback)
      Set the optional output channel used by this simulation.

      The MDI SimulationContext is intentionally minimal and does not provide message, progress, or refresh APIs; the feedback channel supplies them.

      Typical usage from the hosting view:

      
       SimulatedAnnealingSimulation<?> sim =
           (SimulatedAnnealingSimulation<?>) getSimulationEngine().getSimulation();
       sim.setFeedback(AnnealingFeedback.forEngine(getSimulationEngine()));
       
      Parameters:
      feedback - output channel, or null to discard output
    • getState

      public SimulatedAnnealingState getState()
      Get a snapshot of the current annealing state.

      The returned value is suitable for display or logging. The temperature reported here is the same absolute temperature used for acceptance decisions: the schedule's relative value multiplied by the estimated T0.

      Returns:
      a state snapshot
    • getBestSolutionCopy

      public S getBestSolutionCopy()
      Return a defensive copy of the best solution found so far.
      Returns:
      a copy of the best solution, or null if initialization has not occurred
    • init

      public void init(SimulationContext ctx)
      Initialize the simulation.

      This method:

      1. Initializes the RNG (seeded or non-deterministic based on config).
      2. Estimates the initial temperature T0 using TemperatureHeuristic.
      3. Generates a starting solution using AnnealingProblem.randomSolution(Random).
      4. Computes initial energy and resets counters.
      5. Posts initialization output through the configured feedback channel.
      Specified by:
      init in interface Simulation
      Parameters:
      ctx - simulation context (cancellation/timing bookkeeping)
    • step

      public boolean step(SimulationContext ctx)
      Perform one annealing iteration.

      The engine repeatedly calls this method until it returns false. This method returns false when:

      Progress and refresh notifications are throttled according to SimulatedAnnealingConfig.progressEverySteps() and SimulatedAnnealingConfig.refreshEverySteps().

      Specified by:
      step in interface Simulation
      Parameters:
      ctx - simulation context (cancellation/timing bookkeeping)
      Returns:
      true to continue running, false to stop
      Throws:
      IllegalStateException - if the problem returns an unsupported move implementation or a non-finite energy
    • getAbsoluteTemperature

      public double getAbsoluteTemperature()
      Current absolute temperature used in Metropolis acceptance (includes T0).
    • addAcceptedMoveListener

      public void addAcceptedMoveListener(IAcceptedMoveListener listener)
      Add an AcceptedMoveListener. Duplicate registrations are ignored. This method is safe to call while the simulation and EDT notification drain are active.
      Parameters:
      listener - the AcceptedMoveListener to add.
    • removeAcceptedMoveListener

      public void removeAcceptedMoveListener(IAcceptedMoveListener listener)
      Remove an AcceptedMoveListener. This method is safe to call from any thread and has no effect when the listener is not registered.
      Parameters:
      listener - the AcceptedMoveListener to remove.