Package es.urjc.etsii.grafo.improve.sa
Class SimulatedAnnealingBuilder<M extends Move<S,I>,S extends Solution<S,I>,I extends Instance>
java.lang.Object
es.urjc.etsii.grafo.improve.sa.SimulatedAnnealingBuilder<M,S,I>
- Type Parameters:
M
- Move typeS
- Solution typeI
- Instance type
public class SimulatedAnnealingBuilder<M extends Move<S,I>,S extends Solution<S,I>,I extends Instance>
extends Object
Create instances of the simulated annealing algorithm. See SimulatedAnnealing
for a detailed description of the algorithm
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbuild()
Build a SimulatedAnnealing using the provided config values.withAcceptanceCriteriaCustom
(AcceptanceCriteria<M, S, I> acceptanceCriteria) Configure a custom acceptance criteria.withAcceptanceCriteriaDefault
(Objective<M, S, I> objective) Set acceptance criteria to default value, based on the metropolis exponential functionwithCoolDownCustom
(CoolDownControl<M, S, I> coolDownControl) Set a custom cool down function.withCoolDownExponential
(double ratio) Use an exponential cool down function.withCycleLength
(int cycleLength) Set cycle lengthwithInitialTempFunction
(InitialTemperatureCalculator<M, S, I> initialTemperatureCalculator) Provide a custom method for calculating the initial temperature.withInitialTempMaxValue
(Objective<M, S, I> objective) Calculate initial temp as the difference between the best and worst moves in the given neighborhoodwithInitialTempMaxValue
(Objective<M, S, I> objective, double ratio) Calculate initial temp as the difference between the best and worst moves in the given neighborhoodwithInitialTempValue
(double initialTemp) Provide a constant initial temperaturewithNeighborhood
(RandomizableNeighborhood<M, S, I> neighborhood) Neighborhood for the Simulated Annealing.withObjective
(Objective<M, S, I> objective) Set a custom score evaluation function for any moveEnd when temperature reaches 0.withTerminationCriteriaCustom
(TerminationCriteria<M, S, I> terminationCriteria) Set a custom termination criteria.End when the maximum number of iterations is reached.
-
Constructor Details
-
SimulatedAnnealingBuilder
public SimulatedAnnealingBuilder()
-
-
Method Details
-
withNeighborhood
public SimulatedAnnealingBuilder<M,S, withNeighborhoodI> (RandomizableNeighborhood<M, S, I> neighborhood) Neighborhood for the Simulated Annealing. Tip: You may use a neighborhood composed or several others, seeNeighborhood.concat(Neighborhood[])
andNeighborhood.interleave(Neighborhood[])
.- Parameters:
neighborhood
- neighborhood- Returns:
- simulated annealing builder
-
withInitialTempFunction
public SimulatedAnnealingBuilder<M,S, withInitialTempFunctionI> (InitialTemperatureCalculator<M, S, I> initialTemperatureCalculator) Provide a custom method for calculating the initial temperature. Example:(solution, neighborhood) -> solution.getNVertex() * 100
- Parameters:
initialTemperatureCalculator
- lambda expression or class implementation- Returns:
- simulated annealing builder
-
withInitialTempValue
Provide a constant initial temperature- Parameters:
initialTemp
- fixed initial temperature- Returns:
- simulated annealing builder
-
withInitialTempMaxValue
Calculate initial temp as the difference between the best and worst moves in the given neighborhood- Returns:
- simulated annealing builder
-
withInitialTempMaxValue
public SimulatedAnnealingBuilder<M,S, withInitialTempMaxValueI> (Objective<M, S, I> objective, double ratio) Calculate initial temp as the difference between the best and worst moves in the given neighborhood- Parameters:
ratio
- Multiply max difference by this parameter- Returns:
- simulated annealing builder
-
withTerminationCriteriaCustom
public SimulatedAnnealingBuilder<M,S, withTerminationCriteriaCustomI> (TerminationCriteria<M, S, I> terminationCriteria) Set a custom termination criteria. SA always ends when there is no move that improves and no movement is accepted Can end sooner if the given method decides to end. Example 1:(solution, neighborhood, currentTemp, currentIter) -> currentIter > 100
Example 2:(solution, neighborhood, currentTemp, currentIter) -> currentTemp < solution.getNElements()
- Parameters:
terminationCriteria
- custom termination criteria- Returns:
- simulated annealing builder
-
withTerminationCriteriaMaxIterations
End when the maximum number of iterations is reached. Can end sooner if we cannot apply any move- Parameters:
n
- a int.- Returns:
- builder
-
withTerminationCriteriaConverge
End when temperature reaches 0.- Returns:
- simulated annealing builder
-
withCoolDownCustom
Set a custom cool down function. Consider submitting a PR if it is generally aplicable. Example (halve each iteration):(solution, neighborhood, currentTemp, currentIter) -> currentTemp / 2
- Parameters:
coolDownControl
- custom cool down function.- Returns:
- simulated annealing builder
-
withCoolDownExponential
Use an exponential cool down function. Example (halve each iteration):(solution, neighborhood, currentTemp, currentIter) -> currentTemp / 2
- Parameters:
ratio
- exponential ratio, i.e temp = initialT * (ratio ^ iteration)- Returns:
- simulated annealing builder
-
withCycleLength
Set cycle length- Parameters:
cycleLength
- How many moves should be executed for each temperature level. Defaults to 1.- Returns:
- simulated annealing builder
-
withObjective
Set a custom score evaluation function for any move- Returns:
- simulated annealing builder
-
withAcceptanceCriteriaCustom
public SimulatedAnnealingBuilder<M,S, withAcceptanceCriteriaCustomI> (AcceptanceCriteria<M, S, I> acceptanceCriteria) Configure a custom acceptance criteria.- Parameters:
acceptanceCriteria
- acceptance criteria.
-
withAcceptanceCriteriaDefault
Set acceptance criteria to default value, based on the metropolis exponential function -
build
Build a SimulatedAnnealing using the provided config values. Default values if not explicitly set: - Acceptance: Metropolis - Initial temp: max difference between moves in neighborhood - Cooling Schedule: Exponential with ratio 0.99 - Termination criteria: temperature convergence - Cycle length: 1 - Objective: Context::getMainObjective- Returns:
- configured and ready to use simulated annealing algorithm
-