|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcore.Search<T>
core.UndirectedSearch<T>
extended.SimulatedAnnealing<T>
T
- the specific type of all elements in the search domainpublic class SimulatedAnnealing<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.)
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 |
---|
protected SimulatedAnnealingProblem<T> problem
Constructor Detail |
---|
public SimulatedAnnealing(SimulatedAnnealingProblem<T> problem)
problem
- the SimulatedAnnleaingProblem to be solved
java.lang.IllegalArgumentException
- if the problem is nullpublic SimulatedAnnealing(SimulatedAnnealingProblem<T> problem, boolean minimum)
problem
- the SimulatedAnnleaingProblem to be solvedminimum
- flag indicating whether the search looks for minimum values or not
java.lang.IllegalArgumentException
- if the problem is nullpublic SimulatedAnnealing(SimulatedAnnealingProblem<T> problem, boolean minimum, double temperature, double factor, int maxSame) throws java.lang.IllegalArgumentException
problem
- the SimulatedAnnleaingProblem to be solvedminimum
- flag indicating whether the search looks for minimum values or nottemperature
- a value greater 0 by which the annealing process startsfactor
- a value between 0 and 1 by which the temperature will be lowered at each iterationmaxSame
- the maximum amount of steps without improvements before the search terminates (Must be a positive value)
java.lang.IllegalArgumentException
- if the problem is null or one parameter is out of is boundariespublic SimulatedAnnealing(SimulatedAnnealingProblem<T> problem, double temperature, double factor)
problem
- the SimulatedAnnleaingProblem to be solvedtemperature
- a value greater 0 by which the annealing process startsfactor
- a value between 0 and 1 by which the temperature will be lowered at each iteration
java.lang.IllegalArgumentException
- if the problem is null or one parameter is out of is boundariespublic SimulatedAnnealing(SimulatedAnnealingProblem<T> problem, boolean minimum, double temperature, double factor)
problem
- the SimulatedAnnleaingProblem to be solvedminimum
- flag indicating whether the search looks for minimum values or nottemperature
- a value greater 0 by which the annealing process startsfactor
- a value between 0 and 1 by which the temperature will be lowered at each iteration
java.lang.IllegalArgumentException
- if the problem is null or one parameter is out of is boundariesMethod Detail |
---|
protected double difference(T x, T y)
x
- the operand from which the difference is computedy
- the operand to which the difference is computed
public double getInitialTemperature()
public double getFactor()
public int getMaxSame()
public void setInitialTemperature(double temperature)
temperature
- a value greater 0 by which the next annealing process startspublic void setFactor(double factor)
factor
- between 0 and 1 by which the temperature will be loweredpublic void setMaxSame(int maxSame)
maxSame
- the maximum amount of steps without improvementspublic SimulatedAnnealingProblem<T> getProblem()
protected void search()
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;
}
search
in class Search<T>
Search.search()
protected boolean canPrepare()
canPrepare
in class Search<T>
Search.canPrepare()
protected void prepare()
prepare
in class Search<T>
Search.prepare()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |