Class SimulatedAnnealingSimulation<S extends AnnealingSolution<S>>
- Type Parameters:
S- concrete solution type for the annealing problem
- All Implemented Interfaces:
Simulation
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:
- The algorithm itself is UI-agnostic and runs under
SimulationEngine. SimulationContextis intentionally minimal (cancellation + timing + step count).- Optional UI feedback is posted through an injected
AnnealingFeedbackchannel.
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 Summary
ConstructorsConstructorDescriptionSimulatedAnnealingSimulation(AnnealingProblem<S> problem, SimulatedAnnealingConfig cfg, AnnealingSchedule schedule, TemperatureHeuristic<S> tempHeuristic) Construct a simulated annealing simulation with explicit configuration and policies. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAcceptedMoveListener(IAcceptedMoveListener listener) Add an AcceptedMoveListener.doubleCurrent absolute temperature used in Metropolis acceptance (includes T0).Return a defensive copy of the best solution found so far.getState()Get a snapshot of the current annealing state.voidinit(SimulationContext ctx) Initialize the simulation.voidRemove an AcceptedMoveListener.voidsetFeedback(AnnealingFeedback feedback) Set the optional output channel used by this simulation.booleanstep(SimulationContext ctx) Perform one annealing iteration.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface edu.cnu.mdi.sim.Simulation
cancel, shutdown
-
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 estimatingT0(non-null)- Throws:
NullPointerException- if any argument is null
-
-
Method Details
-
setFeedback
Set the optional output channel used by this simulation.The MDI
SimulationContextis 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, ornullto discard output
-
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
Return a defensive copy of the best solution found so far.- Returns:
- a copy of the best solution, or
nullif initialization has not occurred
-
init
Initialize the simulation.This method:
- Initializes the RNG (seeded or non-deterministic based on config).
- Estimates the initial temperature
T0usingTemperatureHeuristic. - Generates a starting solution using
AnnealingProblem.randomSolution(Random). - Computes initial energy and resets counters.
- Posts initialization output through the configured feedback channel.
- Specified by:
initin interfaceSimulation- Parameters:
ctx- simulation context (cancellation/timing bookkeeping)
-
step
Perform one annealing iteration.The engine repeatedly calls this method until it returns
false. This method returnsfalsewhen:- Cancellation is requested via
SimulationContext.isCancelRequested() - The schedule indicates stopping via
AnnealingSchedule.shouldStop(long, SimulatedAnnealingConfig) - The temperature falls below
SimulatedAnnealingConfig.minTemperature()
Progress and refresh notifications are throttled according to
SimulatedAnnealingConfig.progressEverySteps()andSimulatedAnnealingConfig.refreshEverySteps().- Specified by:
stepin interfaceSimulation- Parameters:
ctx- simulation context (cancellation/timing bookkeeping)- Returns:
trueto continue running,falseto stop- Throws:
IllegalStateException- if the problem returns an unsupported move implementation or a non-finite energy
- Cancellation is requested via
-
getAbsoluteTemperature
public double getAbsoluteTemperature()Current absolute temperature used in Metropolis acceptance (includes T0). -
addAcceptedMoveListener
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
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.
-