extended
Class SimulatedAnnealing<T>

java.lang.Object
  extended by core.Search<T>
      extended by core.UndirectedSearch<T>
          extended by extended.SimulatedAnnealing<T>
Type Parameters:
T - the specific type of all elements in the search domain
All Implemented Interfaces:
java.lang.Runnable

public class SimulatedAnnealing<T>
extends UndirectedSearch<T>

The simulated annealing search is an advanced local search able to solve any problem implementing the extended.SimulatedAnnealingProblem interface.

This algorithm starts with the initial state and iteratively selects a random successor of the current state if it is an improvement or with a probability equal to Math.exp( delta / temperature ) (where delta is the difference between the current state and the successor). The temperature is decreased during the search run so the probability of selecting a bad state falls down to zero. In that way the search behaves like a random walk in the beginning and an hill climbing in the end.
The search terminates if there were no better states found since several iterations.
This strategy grants constant space and linear time complexity but no optimal solutions. In contrast to the hill climbing this algorithm is complete if the temperature falls slow enough.
This leads to the fact that the initial temperature and the cooling factor are the most important properties to find good solutions.
When ever the algorithm fails to produce good results, you should modify the initial temperature and cooling factor. For example if your initial temperature is to high, the search starts at a good state and jumps far away from a local optimum. As an other example if your cooling factor is to low, the search will easily get stuck in a local optimum.
Note: There are no easy ways to determine the best search parameters, so you must run experiments until you get good search solutions. A good starting point are the default values, which are an initial temperature of 10.0, a cooling factor of 50 percent and an amount of same states of 100.
Remember that this algorithm is based on probability so the found solution, can vary on successive runs. This algorithm works best in situations, where you only need a good approximation to the optimal solution in a short amount of time.

If you want to improve the time needed for solving your problem, you have to change the way in which the successor states are generated. (For a further discussion look into the description of the extended.SimulatedAnnealingProblem.)

Author:
Eden_06
See Also:
UndirectedSearch, SimulatedAnnealingProblem, HillClimbing

Field Summary
protected  SimulatedAnnealingProblem<T> problem
          holds a reference to the problem to be solved
 
Fields inherited from class core.UndirectedSearch
random
 
Fields inherited from class core.Search
neededSteps, result
 
Constructor Summary
SimulatedAnnealing(SimulatedAnnealingProblem<T> problem)
          Creates a new simulated annealing search with the given problem.
SimulatedAnnealing(SimulatedAnnealingProblem<T> problem, boolean minimum)
          Creates a new simulated annealing search, with the given problem and the direction given by the minimum flag.
SimulatedAnnealing(SimulatedAnnealingProblem<T> problem, boolean minimum, double temperature, double factor)
          Creates a new simulated annealing search, with the given problem, the direction given by the minimum flag, the initial temperature and the cooling factor.
SimulatedAnnealing(SimulatedAnnealingProblem<T> problem, boolean minimum, double temperature, double factor, int maxSame)
          Creates a new simulated annealing search, with the given problem, the direction given by the minimum flag, the initial temperature, the cooling factor and the maximal number of steps without improvements.
SimulatedAnnealing(SimulatedAnnealingProblem<T> problem, double temperature, double factor)
          Creates a new simulated annealing search, with the given problem, the initial temperature and the cooling factor.
 
Method Summary
protected  boolean canPrepare()
          This method checks if the given problem returns valid initial states.
protected  double difference(T x, T y)
          This method delegates the difference operation to the problem description and then inverts the result if necessary (if minimum search is used).
 double getFactor()
          Returns the factor by which the temperature will be lowered on each iteration.
 double getInitialTemperature()
          Returns the temperature at the beginning of each search run
 int getMaxSame()
          Returns the maximum amount of steps without improvement to the search result, before the search terminates.
 SimulatedAnnealingProblem<T> getProblem()
          This method returns the problem, with which this search has been created.
protected  void prepare()
          This method does nothing.
protected  void search()
          In detail the search algorithm can be described in the following way: current = problem.initial(); result = current; while( running() ){ next = select_random_successor(current); delta = difference(current, next); if ( (delta > 0) || (random.nextDouble() < Math.exp( delta / temperature ) ) current = next; if (difference(result,current) > 0){ result = current; else temperature = temperature * factor; }
 void setFactor(double factor)
          This method resets the factor by which the temperature will be lowered during a search run.
 void setInitialTemperature(double temperature)
          This method resets the initial temperature of the search to the given value.
 void setMaxSame(int maxSame)
          This method resets the amount of steps without improvements to the search result.
 
Methods inherited from class core.UndirectedSearch
minimum, searchMaximum, searchMinimum
 
Methods inherited from class core.Search
finalize, getResult, initialize, initialized, neededSteps, run, running, stop
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

problem

protected SimulatedAnnealingProblem<T> problem
holds a reference to the problem to be solved

Constructor Detail

SimulatedAnnealing

public SimulatedAnnealing(SimulatedAnnealingProblem<T> problem)
Creates a new simulated annealing search with the given problem.
The search look for a global minimum and uses the following default values:

Parameters:
problem - the SimulatedAnnleaingProblem to be solved
Throws:
java.lang.IllegalArgumentException - if the problem is null

SimulatedAnnealing

public SimulatedAnnealing(SimulatedAnnealingProblem<T> problem,
                          boolean minimum)
Creates a new simulated annealing search, with the given problem and the direction given by the minimum flag.
The search will use the following default values:

Parameters:
problem - the SimulatedAnnleaingProblem to be solved
minimum - flag indicating whether the search looks for minimum values or not
Throws:
java.lang.IllegalArgumentException - if the problem is null

SimulatedAnnealing

public SimulatedAnnealing(SimulatedAnnealingProblem<T> problem,
                          boolean minimum,
                          double temperature,
                          double factor,
                          int maxSame)
                   throws java.lang.IllegalArgumentException
Creates a new simulated annealing search, with the given problem, the direction given by the minimum flag, the initial temperature, the cooling factor and the maximal number of steps without improvements.

Parameters:
problem - the SimulatedAnnleaingProblem to be solved
minimum - flag indicating whether the search looks for minimum values or not
temperature - a value greater 0 by which the annealing process starts
factor - a value between 0 and 1 by which the temperature will be lowered at each iteration
maxSame - the maximum amount of steps without improvements before the search terminates (Must be a positive value)
Throws:
java.lang.IllegalArgumentException - if the problem is null or one parameter is out of is boundaries

SimulatedAnnealing

public SimulatedAnnealing(SimulatedAnnealingProblem<T> problem,
                          double temperature,
                          double factor)
Creates a new simulated annealing search, with the given problem, the initial temperature and the cooling factor. The search will look for a global minimum and allows maximal 100 steps without improvements during a search run.

Parameters:
problem - the SimulatedAnnleaingProblem to be solved
temperature - a value greater 0 by which the annealing process starts
factor - a value between 0 and 1 by which the temperature will be lowered at each iteration
Throws:
java.lang.IllegalArgumentException - if the problem is null or one parameter is out of is boundaries

SimulatedAnnealing

public SimulatedAnnealing(SimulatedAnnealingProblem<T> problem,
                          boolean minimum,
                          double temperature,
                          double factor)
Creates a new simulated annealing search, with the given problem, the direction given by the minimum flag, the initial temperature and the cooling factor. The search allows maximal 100 steps without improvements during a search run.

Parameters:
problem - the SimulatedAnnleaingProblem to be solved
minimum - flag indicating whether the search looks for minimum values or not
temperature - a value greater 0 by which the annealing process starts
factor - a value between 0 and 1 by which the temperature will be lowered at each iteration
Throws:
java.lang.IllegalArgumentException - if the problem is null or one parameter is out of is boundaries
Method Detail

difference

protected double difference(T x,
                            T y)
This method delegates the difference operation to the problem description and then inverts the result if necessary (if minimum search is used).

Parameters:
x - the operand from which the difference is computed
y - the operand to which the difference is computed
Returns:
a negative double, zero, or a positive double which indicates the difference of state x to state y according to the search direction

getInitialTemperature

public double getInitialTemperature()
Returns the temperature at the beginning of each search run

Returns:
the initial temperature

getFactor

public double getFactor()
Returns the factor by which the temperature will be lowered on each iteration.

Returns:
the cooling factor

getMaxSame

public int getMaxSame()
Returns the maximum amount of steps without improvement to the search result, before the search terminates.

Returns:
amount of steps without improvements

setInitialTemperature

public void setInitialTemperature(double temperature)
This method resets the initial temperature of the search to the given value. This operation has only effect if the value is greater 0.
Note: Invoking this method while running a search will only effect the following search runs.

Parameters:
temperature - a value greater 0 by which the next annealing process starts

setFactor

public void setFactor(double factor)
This method resets the factor by which the temperature will be lowered during a search run. This operation has only effect if the given value is greater then 0 and less then 1.
Note: This operation works also while running a search and can be used to change the scheduling of temperature decrease.

Parameters:
factor - between 0 and 1 by which the temperature will be lowered

setMaxSame

public void setMaxSame(int maxSame)
This method resets the amount of steps without improvements to the search result. This operation has only effect if the given value is greater then 0.
Note: You can reset this property also while running a search.

Parameters:
maxSame - the maximum amount of steps without improvements

getProblem

public SimulatedAnnealingProblem<T> getProblem()
This method returns the problem, with which this search has been created.

Returns:
the problem to be solved

search

protected void search()
In detail the search algorithm can be described in the following way:

 current = problem.initial();
 result = current;
 while( running() ){
        next = select_random_successor(current);
        delta = difference(current, next);
        if ( (delta > 0) || (random.nextDouble() < Math.exp( delta / temperature ) )
                current = next;
        if (difference(result,current) > 0){
                result = current;
        else
                temperature = temperature * factor;
 }
 

Specified by:
search in class Search<T>
See Also:
Search.search()

canPrepare

protected boolean canPrepare()
This method checks if the given problem returns valid initial states.

Specified by:
canPrepare in class Search<T>
Returns:
true if the search can be prepared
See Also:
Search.canPrepare()

prepare

protected void prepare()
This method does nothing.

Specified by:
prepare in class Search<T>
See Also:
Search.prepare()