|
||||||||||
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.HillClimbing<T>
T
- the specific type of all elements in the search domain.public class HillClimbing<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.
HillClimbingProblem<State> problem = new MyHillClimbingProblem();
//1.) Creates a search which uses the first choice strategy
HillClimbing<State> search = new HillClimbingSearch<State>(problem, EStrategy.BEST_CHOICE);
//2.) Changes the search strategy of an HillClimbingSearch to the first choice strategy
search.setStrategy( EStrategy.BEST_CHOICE );
HillClimbingProblem<State> problem = new MyHillClimbingProblem();
//1.) Creates a search which uses the first choice strategy
HillClimbing<State> search = new HillClimbingSearch<State>(problem, EStrategy.FIRST_CHOICE);
//2.) Changes the search strategy of an HillClimbingSearch to the first choice strategy
search.setStrategy( EStrategy.FIRST_CHOICE );
HillClimbingProblem<State> problem = new MyHillClimbingProblem();
HillClimbingStrategy<State> strategy = new MyHillClimbingStrategy();
//1.) Creates a search which uses your custom strategy
HillClimbing<State> search = new HillClimbingSearch<State>(problem, true, strategy, 100);
//2.) Changes the search strategy of an HillClimbingSearch to your custom strategy
search.setStrategy( strategy );
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 |
---|
protected HillClimbingProblem<T> problem
protected HillClimbingStrategy<T> strategy
Constructor Detail |
---|
public HillClimbing(HillClimbingProblem<T> problem)
problem
- the HillClimbingProblem to be solved
java.lang.IllegalArgumentException
- if the problem is nullpublic HillClimbing(HillClimbingProblem<T> problem, boolean minimum, HillClimbing.EStrategy strategy)
problem
- the HillClimbingProblem to be solvedminimum
- flag indicating whether the search looks for minimum values or notstrategy
- enumeration indicating which of the default strategies to use
java.lang.IllegalArgumentException
- if problem or strategy is nullpublic HillClimbing(HillClimbingProblem<T> problem, boolean minimum, HillClimbing.EStrategy strategy, int sideSteps)
problem
- the HillClimbingProblem to be solvedminimum
- flag indicating whether the search looks for minimum values or notstrategy
- enumeration indicating which of the default strategies to usesideSteps
- the number of allowed side steps during the search
java.lang.IllegalArgumentException
- if problem or strategy is null and if sideSteps is smaller than zeropublic HillClimbing(HillClimbingProblem<T> problem, boolean minimum, HillClimbingStrategy<T> strategy, int sideSteps)
problem
- the HillClimbingProblem to be solvedminimum
- flag indicating whether the search looks for minimum values or notstrategy
- a custom HillClimbingStrategy instance for the searchsideSteps
- the number of allowed side steps during the search
java.lang.IllegalArgumentException
- if problem or strategy is null and if sideSteps is smaller than zeropublic HillClimbing(HillClimbingProblem<T> problem, HillClimbing.EStrategy strategy)
problem
- the HillClimbingProblem to be solvedstrategy
- enumeration indicating which of the default strategies to use
java.lang.IllegalArgumentException
- if problem or strategy is nullMethod Detail |
---|
protected java.util.Comparator<T> comparator()
public final int compare(T x, T y)
compare
in interface java.util.Comparator<T>
x
- left side of the comparisony
- right side of the comparison
public final HillClimbingProblem<T> getProblem()
public final HillClimbingStrategy<T> getStrategy()
public final int neededSideSteps()
public final void setSideSteps(int steps)
steps
- the new number of side stepspublic final void setStrategy(HillClimbingStrategy<T> strategy)
strategy
- the HillClimbingStrategy to setpublic final void setStrategy(HillClimbing.EStrategy strategy)
strategy
- enumeration indicating which of the default strategies to setprotected boolean canPrepare()
canPrepare
in class Search<T>
protected void prepare()
prepare
in class Search<T>
protected void search()
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;
}
search
in class Search<T>
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |