basic
Class UniformCostSearch<T>
java.lang.Object
core.Search<T>
core.DirectedSearch<T>
core.BestFirstSearch<T>
basic.UniformCostSearch<T>
- Type Parameters:
T
- the specific type of all elements of the search domain.
- All Implemented Interfaces:
- java.lang.Runnable
public class UniformCostSearch<T>
- extends BestFirstSearch<T>
The uniform cost search algorithm is a blind search able
to solve any problem implementing the core.HeuristicProblem interface.
The uniform cost search tries to expand those states at first
which are cheap to reach from the initial state.
This strategy grants completeness and a time and space complexity
of O(b^n)
(where b is the branching factor and n the depth of the solution).
In fact the solution found is optimal only if the following constraint holds:
For all states a
in the search domain and all s
in
heuristicProblem.expand(a)
the sum of the path costs of s
is greater or equal to the sum of the path costs of a
(or short where heuristicProblem.g(s) >= heuristicProblem.g(a)
).
In all cases where two or more paths from the initial state to an other state exists
you have to take care of updated states.
To cope with this situation you can select one of the following implementation patterns:
- Disable hashing:
A simple solution would be to create a uniform cost search without hashing and without updating of the queue.
In this way all states will be inserted into the queue and ordered according to their current sum of path costs
without checking whether they have already been visited or not.
So if we revisit a state with lower path costs, we can simply add it again to the queue.
Because the queue will not update its elements the state with the lowest path costs will
be placed before its duplicates.
Pros: The major benefit of this solution is its simplicity and the fact that it uses the
fast java.util.PriorityQueue instead of the slow util.SortedQueue.
An other benefit is that you do not need to take care of duplicate states by your self.
Cons: If there exists no shortest path in the search domain, this solution
produces lots of unnecessary expansions of already expanded notes.
In fact this makes the search more space and time consuming.
Example:
HeuristicProblem problem = new MyHeuristicProblem();
Search search = new UniformCostSearch(problem, true, false);
-
User defined hashing:
Experienced users can create a uniform cost search with custom hashing and with an updating queue.
The idea is that you keep track of all the visited states in a map,
where you can simply update each visited state whenever you found a better path.
You only have to make sure that the HeuristicProblem#expand(java.lang.Object) will only
return a list of states not visited before.
The util.SortedQueue implementation ensures, that the queue is updated whenever a state in it has changed.
(Please note that the sorted queue works slightly different, as described here!)
Pros: This solution saves lots of memory by prohibiting expansion of already visited states.
Further more it is a general solution applicable for all problems with alternative paths in the search space.
Cons: This solution is more time consuming because it must frequently update the queue
and the states itself.
In fact the util.SortedQueue is damn slow compared to the java.util.PriorityQueue.
Example:
class MyCustomProblem<T> extends MyHeuristicProblem<T>{
private Map<Integer,T> visited=new HashMap<Integer,T>();
public List expand(T state){
List result = new LinkedList();
for (T newState : super.expand(state) ){
oldState = visited.get( newState.hashCode() );
if (oldState == null){
result.add(newState);
visited.put(newState.hashCode(), newState);
}else{
update(oldState, newState);
}
}
return result;
}
public update(T oldState,T newState){
if ( g(oldState) > g(newState) ){
// update the state of the oldState Object
// ...
}
}
}
// ...
HeuristicProblem problem = new MyCustomProblem();
Search search = new UniformCostSearch(problem, false, false);
- Author:
- eden06
Method Summary |
java.lang.Double |
f(T state)
This method evaluates a state according to the sum of the path costs from the initial state. |
Methods inherited from class java.lang.Object |
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
UniformCostSearch
public UniformCostSearch(HeuristicProblem<T> problem)
- This method creates a new UniformCostSearch.
All search parameters will be set to the default values.
- Parameters:
problem
- the HeuristicProblem to be solved
UniformCostSearch
public UniformCostSearch(HeuristicProblem<T> problem,
boolean update)
- This method creates a new UniformCostSearch.
According to the given update flag the search will
use either a queue which is able to update the ordering or not.
Note: The performance is significantly improved,
if the queue must not update its elements.
- Parameters:
problem
- the HeuristicProblem to be solvedupdate
- flag indicating whether the states in the queue should be updated or not.
UniformCostSearch
public UniformCostSearch(HeuristicProblem<T> problem,
boolean noHash,
boolean update)
- This method creates a new UniformCostSearch.
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 UniformCostSearch(problem,false,false);
s2 = new UniformCostSearch(problem,true ,false);
s3 = new UniformCostSearch(problem,false,true );
s4 = new UniformCostSearch(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: The performance is significantly improved,
if the queue must not update its elements.
- 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 evaluates a state according to the sum of the path costs from the initial state.
Note: This method uses the function core.HeuristicProblem#g(java.lang.Object)
to compute the sum of the path costs, 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)