extended
Class HillClimbing<T>

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

public class HillClimbing<T>
extends UndirectedSearch<T>
implements java.util.Comparator<T>

The hill climbing algorithm is simple local search able to solve any problem implementing the extended.HillClimbingProblem interface.

This algorithm starts with the initial state, selects the best successor of the current state as new current state and continues until no best successor can be found. The current state now holds the found solution, in fact a local minimum (or maximum).
This strategy grants constant space and linear time complexity but neither completeness nor optimality. In detail it is possible to gain completeness with two simple modifications to the algorithm.
The first is, to allow a limited amount of side steps. A side step is an iteration where a state equal (according to the comparator) to the current state is selected as new current state.
This ability is directly implemented in this algorithm and can be used in the following way:


 HillClimbingProblem<MyState> problem=new MyHillClimbingProblem();
 HillClimbing<MyState> search=new HillClimbing(problem, true, EhcStrategy.BEST_CHOICE, 100);
 //creates a hill climbing search where 100 side steps are allowed during the search
 search.setSideSteps(1000);
 //changes the allowed number of side steps for this hill climbing search to 1000
 
The second is, to run the hill climbing search with random initial states for several times and remembering only the best solution of all runs. This ability is not implemented but can easily be implemented in the following way:

 int limit=10; 
 HillClimbingProblem<MyState> problem=new MyHillClimbingProblem();
 MyState best=problem.initial();
 HillClimbing<MyState> search=new HillClimbing(problem);
 for (iteration=0; iteration
Note: 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.

Hill climbing can use different strategies on how to select a better state. There are two default strategies shipped with this implementation. You must check if one of the default strategies fits best to your problem or if you implement your own strategy.
The following list guide you through the different opportunities:

Author:
Eden_06
See Also:
UndirectedSearch, HillClimbingProblem, HillClimbingStrategy, BestChoiceStrategy, FirstChoiceStrategy, SimulatedAnnealing

Nested Class Summary
static class HillClimbing.EStrategy
          Enumeration to select one of the two default hill climbing strategies.
 
Field Summary
protected  HillClimbingProblem<T> problem
          holds a reference to the problem to be solved
protected  HillClimbingStrategy<T> strategy
          holds the strategy used during the search
 
Fields inherited from class core.UndirectedSearch
random
 
Fields inherited from class core.Search
neededSteps, result
 
Constructor Summary
HillClimbing(HillClimbingProblem<T> problem)
          Creates a new hill climbing search, with the given problem.
HillClimbing(HillClimbingProblem<T> problem, boolean minimum, HillClimbing.EStrategy strategy)
          Creates a new hill climbing search, with the given problem and the direction given by the minimum flag.
HillClimbing(HillClimbingProblem<T> problem, boolean minimum, HillClimbing.EStrategy strategy, int sideSteps)
          Creates a new hill climbing search, with the given problem, the direction given by the minimum flag and the allowed number of sideSteps.
HillClimbing(HillClimbingProblem<T> problem, boolean minimum, HillClimbingStrategy<T> strategy, int sideSteps)
          Creates a new hill climbing search, with the given problem, the direction given by the minimum flag and the allowed number of sideSteps.
HillClimbing(HillClimbingProblem<T> problem, HillClimbing.EStrategy strategy)
          Creates a new hill climbing search, with the given problem and the selected strategy.
 
Method Summary
protected  boolean canPrepare()
          Returns true only there is a strategy and an initial state available.
protected  java.util.Comparator<T> comparator()
          This method returns the Comparator the HillClimbingProblem implements.
 int compare(T x, T y)
          This method delegates the compare operation to the correct comparator and then inverts the value if necessary (if minimum search is used).
 HillClimbingProblem<T> getProblem()
          This method returns the problem, with which this search has been created.
 HillClimbingStrategy<T> getStrategy()
          This method returns the actual HillClimbingStrategy used during the search.
 int neededSideSteps()
          This method gives you the number of side steps needed by the last search.
protected  void prepare()
          Initializes the number of needed side steps.
protected  void search()
          This method fills the hook which implements the actual HillClimbing algorithm.
 void setSideSteps(int steps)
          This method resets the number of allowed side steps to the given value.
 void setStrategy(HillClimbing.EStrategy strategy)
          This method replaces the used strategy by the selected default strategy.
 void setStrategy(HillClimbingStrategy<T> strategy)
          This method replaces the used strategy by the given one.
 
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
 
Methods inherited from interface java.util.Comparator
equals
 

Field Detail

problem

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


strategy

protected HillClimbingStrategy<T> strategy
holds the strategy used during the search

Constructor Detail

HillClimbing

public HillClimbing(HillClimbingProblem<T> problem)
Creates a new hill climbing search, with the given problem. The search will look for a global minimum with a best choice strategy and no allowed side steps.

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

HillClimbing

public HillClimbing(HillClimbingProblem<T> problem,
                    boolean minimum,
                    HillClimbing.EStrategy strategy)
Creates a new hill climbing search, with the given problem and the direction given by the minimum flag. The search will use the selected strategy without allowing side steps.

Parameters:
problem - the HillClimbingProblem to be solved
minimum - flag indicating whether the search looks for minimum values or not
strategy - enumeration indicating which of the default strategies to use
Throws:
java.lang.IllegalArgumentException - if problem or strategy is null

HillClimbing

public HillClimbing(HillClimbingProblem<T> problem,
                    boolean minimum,
                    HillClimbing.EStrategy strategy,
                    int sideSteps)
Creates a new hill climbing search, with the given problem, the direction given by the minimum flag and the allowed number of sideSteps. The search will use the selected strategy.

Parameters:
problem - the HillClimbingProblem to be solved
minimum - flag indicating whether the search looks for minimum values or not
strategy - enumeration indicating which of the default strategies to use
sideSteps - the number of allowed side steps during the search
Throws:
java.lang.IllegalArgumentException - if problem or strategy is null and if sideSteps is smaller than zero

HillClimbing

public HillClimbing(HillClimbingProblem<T> problem,
                    boolean minimum,
                    HillClimbingStrategy<T> strategy,
                    int sideSteps)
Creates a new hill climbing search, with the given problem, the direction given by the minimum flag and the allowed number of sideSteps. The search will use the given HillClimbingStrategy during the search.

Parameters:
problem - the HillClimbingProblem to be solved
minimum - flag indicating whether the search looks for minimum values or not
strategy - a custom HillClimbingStrategy instance for the search
sideSteps - the number of allowed side steps during the search
Throws:
java.lang.IllegalArgumentException - if problem or strategy is null and if sideSteps is smaller than zero

HillClimbing

public HillClimbing(HillClimbingProblem<T> problem,
                    HillClimbing.EStrategy strategy)
Creates a new hill climbing search, with the given problem and the selected strategy. The search will look for a global minimum without allowing side steps.

Parameters:
problem - the HillClimbingProblem to be solved
strategy - enumeration indicating which of the default strategies to use
Throws:
java.lang.IllegalArgumentException - if problem or strategy is null
Method Detail

comparator

protected java.util.Comparator<T> comparator()
This method returns the Comparator the HillClimbingProblem implements.

Returns:
the comparator imposed by the HillClimbingProblem instance

compare

public final int compare(T x,
                         T y)
This method delegates the compare operation to the correct comparator and then inverts the value if necessary (if minimum search is used).
Note: This method must be used when your implementing your own hill climbing strategy.

Specified by:
compare in interface java.util.Comparator<T>
Parameters:
x - left side of the comparison
y - right side of the comparison
Returns:
-1,0 or 1 in respect to the comparator and search direction

getProblem

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

Returns:
the problem to be solved

getStrategy

public final HillClimbingStrategy<T> getStrategy()
This method returns the actual HillClimbingStrategy used during the search.

Returns:
the strategy used during the hill climbing search

neededSideSteps

public final int neededSideSteps()
This method gives you the number of side steps needed by the last search.

Returns:
the number of needed side steps

setSideSteps

public final void setSideSteps(int steps)
This method resets the number of allowed side steps to the given value.
Note: This method has also effect during a running or initialized search!

Parameters:
steps - the new number of side steps

setStrategy

public final void setStrategy(HillClimbingStrategy<T> strategy)
This method replaces the used strategy by the given one. Note: This method has only effect if the search has not been initialized!

Parameters:
strategy - the HillClimbingStrategy to set

setStrategy

public final void setStrategy(HillClimbing.EStrategy strategy)
This method replaces the used strategy by the selected default strategy. Note: This method has only effect if the search has not been initialized and the given strategy is not null.

Parameters:
strategy - enumeration indicating which of the default strategies to set

canPrepare

protected boolean canPrepare()
Returns true only there is a strategy and an initial state available.

Specified by:
canPrepare in class Search<T>
Returns:
true if the search can be prepared

prepare

protected void prepare()
Initializes the number of needed side steps.

Specified by:
prepare in class Search<T>

search

protected void search()
This method fills the hook which implements the actual HillClimbing algorithm. The implementation can be described in the following way:

 current = problem.initial();
 while( running() ){
        next = select_best_successor( current );
        value = compare(current, next)
        if (value > 0){ // there was no best successor
                result = current;
                break;
        }
        current = next;
 }
 

Specified by:
search in class Search<T>