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 type
S - Solution type
I - 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 Details

    • SimulatedAnnealingBuilder

      public SimulatedAnnealingBuilder()
  • Method Details

    • withNeighborhood

      public SimulatedAnnealingBuilder<M,S,I> withNeighborhood(RandomizableNeighborhood<M,S,I> neighborhood)
      Neighborhood for the Simulated Annealing. Tip: You may use a neighborhood composed or several others, see Neighborhood.concat(Neighborhood[]) and Neighborhood.interleave(Neighborhood[]).
      Parameters:
      neighborhood - neighborhood
      Returns:
      simulated annealing builder
    • withInitialTempFunction

      public SimulatedAnnealingBuilder<M,S,I> withInitialTempFunction(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

      public SimulatedAnnealingBuilder<M,S,I> withInitialTempValue(double initialTemp)
      Provide a constant initial temperature
      Parameters:
      initialTemp - fixed initial temperature
      Returns:
      simulated annealing builder
    • withInitialTempMaxValue

      public SimulatedAnnealingBuilder<M,S,I> withInitialTempMaxValue(Objective<M,S,I> objective)
      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,I> withInitialTempMaxValue(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,I> withTerminationCriteriaCustom(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

      public SimulatedAnnealingBuilder<M,S,I> withTerminationCriteriaMaxIterations(int n)
      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

      public SimulatedAnnealingBuilder<M,S,I> withTerminationCriteriaConverge()
      End when temperature reaches 0.
      Returns:
      simulated annealing builder
    • withCoolDownCustom

      public SimulatedAnnealingBuilder<M,S,I> withCoolDownCustom(CoolDownControl<M,S,I> coolDownControl)
      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

      public SimulatedAnnealingBuilder<M,S,I> withCoolDownExponential(double ratio)
      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

      public SimulatedAnnealingBuilder<M,S,I> withCycleLength(int cycleLength)
      Set cycle length
      Parameters:
      cycleLength - How many moves should be executed for each temperature level. Defaults to 1.
      Returns:
      simulated annealing builder
    • withObjective

      public SimulatedAnnealingBuilder<M,S,I> withObjective(Objective<M,S,I> objective)
      Set a custom score evaluation function for any move
      Returns:
      simulated annealing builder
    • withAcceptanceCriteriaCustom

      public SimulatedAnnealingBuilder<M,S,I> withAcceptanceCriteriaCustom(AcceptanceCriteria<M,S,I> acceptanceCriteria)
      Configure a custom acceptance criteria.
      Parameters:
      acceptanceCriteria - acceptance criteria.
    • withAcceptanceCriteriaDefault

      public SimulatedAnnealingBuilder<M,S,I> withAcceptanceCriteriaDefault(Objective<M,S,I> objective)
      Set acceptance criteria to default value, based on the metropolis exponential function
    • build

      public SimulatedAnnealing<M,S,I> 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