Class TspMove
java.lang.Object
edu.cnu.mdi.sim.simanneal.tspdemo.TspMove
- All Implemented Interfaces:
AnnealingMove<TspSolution>,DeltaEnergyMove<TspSolution>,ReversibleAnnealingMove<TspSolution>
A 2-opt move for TSP that supports O(1) ΔE computation.
The move reverses a contiguous segment [i..k] in-place.
It is parameterized by the endpoints i < k chosen in
prepare(TspSolution). The same prepared parameters are used
by deltaE(TspSolution) and apply(TspSolution).
Undo is performed by reversing the same segment again.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidapply(TspSolution sol) Apply the prepared 2-opt reversal in-place.doubledeltaE(TspSolution sol) Compute the exact ΔE for the prepared 2-opt reversal.voidprepare(TspSolution sol) Choose a non-degenerate 2-opt segment for the next move.voidundo(TspSolution sol) Undo by reversing the same segment again.
-
Constructor Details
-
TspMove
Create a reusable random 2-opt move.- Parameters:
rng- source of randomness
-
-
Method Details
-
prepare
Choose a non-degenerate 2-opt segment for the next move.- Specified by:
preparein interfaceDeltaEnergyMove<TspSolution>- Parameters:
sol- current solution (non-null)- Throws:
IllegalArgumentException- if the solution or tour is null, or the tour contains fewer than four cities
-
deltaE
Compute the exact ΔE for the prepared 2-opt reversal.- Specified by:
deltaEin interfaceDeltaEnergyMove<TspSolution>- Parameters:
sol- current solution (non-null)- Returns:
- ΔE = E_after - E_before for the prepared segment
- Throws:
IllegalStateException- if called beforeprepare(edu.cnu.mdi.sim.simanneal.tspdemo.TspSolution)
-
apply
Apply the prepared 2-opt reversal in-place.- Specified by:
applyin interfaceReversibleAnnealingMove<TspSolution>- Parameters:
sol- current solution (non-null)- Throws:
IllegalStateException- if called beforeprepare(edu.cnu.mdi.sim.simanneal.tspdemo.TspSolution)
-
undo
Undo by reversing the same segment again.- Specified by:
undoin interfaceReversibleAnnealingMove<TspSolution>- Parameters:
sol- current solution (non-null)
-