basic
Class GreedySearch<T>
java.lang.Object
core.Search<T>
core.DirectedSearch<T>
core.BestFirstSearch<T>
basic.GreedySearch<T>
- Type Parameters:
T
- the specific type of all elements in the search domain.
- All Implemented Interfaces:
- java.lang.Runnable
public class GreedySearch<T>
- extends BestFirstSearch<T>
The greedy search algorithm is a heuristic search able
to solve any problem implementing the core.HeuristicProblem interface.
This search tries to expand those states at first
which are most promising to reach a goal state.
To do this the HeuristicProblem#h(java.lang.Object) method is called for each state in order
to approximate the costs needed to reach a goal.
This strategy grants a time and space complexity of O(b*^n)
(where b* is the effective branching factor and n the depth of the solution).
A drawback of this strategy is the incompleteness, if an infinite path exists in the search domain.
In general this search will not find an optimal solution,
but is able to generate a good approximation to the optimum in a short amount of time.
Note: In some special cases the problem can be divided into subproblems
for which the heuristic function can compute an optimal solution. In these cases the
greedy search will also be optimal.
In most of the cases this search can be used with enabled hashing and without an updating queue.
(These are the default values.)
The only case when you need to create a greedy search with an updating queue,
if the heuristic function returns different values for the same state.
(For example if the goal state is moving around the search domain.)
Note: Using an updating queue will cause massive performance penalties.
- Author:
- eden06
Method Summary |
java.lang.Double |
f(T state)
This method estimates the costs to reach a goal state from the given state. |
Methods inherited from class java.lang.Object |
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
GreedySearch
public GreedySearch(HeuristicProblem<T> problem)
- This method creates a new GreedySearch.
All search parameters will be set to the default values.
- Parameters:
problem
- the HeuristicProblem to be solved
GreedySearch
public GreedySearch(HeuristicProblem<T> problem,
boolean update)
- This method creates a new GreedySearch.
According to the given update flag the search will
use either a queue which is able to update the ordering or not.
Note: In almost all cases using an updating queue is not necessary.
- Parameters:
problem
- the HeuristicProblem to be solvedupdate
- flag indicating whether the states in the queue should be updated or not.
GreedySearch
public GreedySearch(HeuristicProblem<T> problem,
boolean noHash,
boolean update)
- This method creates a new GreedySearch.
According to the given flags the search will
turn off the internal duplicate handling mechanism and
uses a queue which updates its elements or not.
HeuristicProblem problem;
Search s1,s2,s3,s4;
s1 = new GreedySearch(problem,false,false);
s2 = new GreedySearch(problem,true ,false);
s3 = new GreedySearch(problem,false,true );
s4 = new GreedySearch(problem,true ,true );
s1 will be a search with duplicate handling and a queue without updates.
s2 will be a search without duplicate handling and a queue without updates.
s3 will be a search with duplicate handling and a queue which updates its elements.
s4 will be a search without duplicate handling and a queue which updates its elements.
Note: In almost all cases using an updating queue is not necessary.
- Parameters:
problem
- the HeuristicProblem to be solvednoHash
- flag indicating that duplicates should not be handledupdate
- flag indicating whether the states in the queue should be updated or not.
f
public java.lang.Double f(T state)
- This method estimates the costs to reach a goal state from the given state.
Note: This method uses the function core.HeuristicProblem#h(java.lang.Object)
to compute this estimation, and nothing else.
- Specified by:
f
in class BestFirstSearch<T>
- Parameters:
state
- to be evaluated
- Returns:
- the function value
- See Also:
BestFirstSearch.f(java.lang.Object)