# ARTIFICIAL INTELLIGENCE SEARCH ALGORITHMS

Document Sample

```					Artificial Intelligence: Search Algorithms

ARTIFICIAL INTELLIGENCE
&

SEARCH ALGORITHMS
A study of common search algorithms used in AI for three simple problems

By M Kamali
Javan Systems –London

http://www.javansystems.co.uk

By M Kamali                            http://www.javansystems.co.uk
Artificial Intelligence: Search Algorithms

Introduction and Definitions………….…………………………………………page 2

Problem 1 (Map)………………………………………………………………….page 5

Problem 2 (M&C)..……………………………………………………………….page 9

Problem 3 (Chains)……………………………………………………………….page 14

Part 2 (Heuristic function) ………………………………………………………page 21

By M Kamali                         http://www.javansystems.co.uk
Artificial Intelligence: Search Algorithms

There are three problems in this paper, which have been solved by AI techniques and
search algorithms using a pre-compiled Java code in the form of a package named
“item040.search”. This package provides implementations of various standard search
algorithms of AI.

Definitions:

Uninformed or brute force search algorithms are common forms of search that
only require a starting state, a set of legal operators and a goal state. Although
these algorithms have a simplistic implementation, they may require lots of
memory and are inefficient in more complex problems; since they can explore a
very large number of possible states until the goal state is reached. However,
when there is no limit on how long the search takes and on the memory available,
then it might be logical to rely on the brute force algorithms, as they will always
reach the goal state.

Two basic types of brute force search algorithms are the breadth-first and depth-
first. The following diagrams show the sequence of the search in each case:

Breath-First algorithm node expansion

Depth-First algorithm node expansion

− DL Search
A Depth Limited Search is simply a Depth First Search that has a limit on depth
of states to investigate. If a state is deeper than the set limit from the start state it
is never investigated. Usually this search is used in search spaces where you know
that the goal is within a certain distance from the start state, or in situations where
a goal state farther away is not of interest.

− ID Search
Iterative Deepening Search has many of the advantages of Depth First Search and
many of the advantages of Breadth First Search. It uses the same amount of

By M Kamali                                    http://www.javansystems.co.uk
Artificial Intelligence: Search Algorithms

memory as a Depth First Search on the same search space, and is complete, and
optimal (under appropriate conditions).
The search begins by doing a Depth Limited Search with a limit and if a goal is
not found incrementing this limit (the increment may be greater than one) and
trying again until a goal is found.
This may sound like a horrible idea for a search; after all we are repeatedly
searching the same space over and over again. States that are investigated early
will be investigated multiple times before a goal is found. However if we do the
math we find that this concern, while valid, does not have a huge impact.

* Source http://ai.squeakydolphin.com

− Greedy Search (minimum cost to goal)
This algorithm uses an approach that is very similar to the brute-force algorithms.
Basically, it takes the closest node to the goal state and continues searching from
there. However, it is usualy difficult to compute the exact distance from the goal
state. Therefore, a heuristic function is used to estimate this distance. Thus, a
better estimation function will result in a better performance, and consequently,
will find the solution faster. Although Greedy algorithms have many common
directing the search makes it more preferable.

Greedy Search is not ‘complete’ as it can get stuck in loops. In terms of space
complexity is not much suitable since it keeps all the nodes in memory. And for
time complexity it usually be improved dramatically using a heuristic function.

− A Star (minimum total path cost)
Another type of algorithm is ‘A Star’ (A*) This is usually suitable for finding the
best route from A to B. The A* algorithm finds the lowest cost path between the
initial state and the goal state where changing from one state to another involves
some cost. A* requires a heuristic function in order to evaluate the cost of path
that passes through a particular state. This is defined below as:

f(n) = g(n) + h(n)

* Source: http://www.generation5.org

As you can see, f(n) provides an estimate of a lowest cost path from the start to
the goal state that passes though node n. Therefore the states with the lowest f(n)
are checked first and the algorithm ends when the goal state is chosen for
evaluation.

While the theory of A* is relatively simple, implementing the algorithm can be
very complicated. However once this is done you have an efficient path finding
algorithm that can be reused in many cases.

By M Kamali                                   http://www.javansystems.co.uk
Artificial Intelligence: Search Algorithms

− Optimality

A search is considered optimal if it always finds the solution of the least cost and
the highest quality.

Often Search Optimality may be traded for performance. for example it might be
good enough to find a route that is no more than twice more expensive as the
cheapest route. If so, we may be able to find it in half the time or say by half the
memory usage.

For every search algorithms, there is a trade-off between complexity and efficiency.
Efficient algorithms are usually harder to implement. On the other hand, which algorithm
we choose, depends largely on the type and size of the problem. A combination of breath-
first and depth-first algorithms is more appropriate for problems where the states are
associated with some additional information that affects the search. A combination of two
or more search algorithms can be used to benefit from the advantages of each of them and
minimize the effect of the disadvantages.

In this coursework each problem will be explained in that how they are represented in
terms of states, operators, goal test and path cost. Then the results from running the
programs will be analyzed for optimality, time complexity and space complexity (Using
the data returned by the search package)

At the end, one of the implemented search algorithms will be suggested as the one most
suitable for each particular problem.

By M Kamali                                  http://www.javansystems.co.uk
Artificial Intelligence: Search Algorithms

Problem 1: Map (from Arad to Bucharest)

This problem involves an agent that has to find its way from an initial state in the city of
Arad to a goal state in Bucharest. The other towns on the way each represent a unique
state in the system. There is more than one way to go from Arad to Bucharest and the
agent can move in both directions in every path. The agent’s job is to find the most
economic route in terms of ‘path cost.’

The initial state is defined in the Main file by ‘new MapState(“Arad”)’
The goal state is also defined by ‘new MapState("Bucharest")’

In the file MapState.java, the String (Arad or Bucharest) is matched against the array of
places and will return an error message if the place does not exist.

Otherwise continues by defining the goal and how to test it. ‘goalTest’ checks whether
the current position, is that of the one defined as the goal state. If so, the function returns
true, otherwise False:

public boolean goalTest(State state) {
return pos == ((MapState) state).pos;
}

The path cost is fetched each time from the ‘distances’ array, which holds the mileages
between the towns, and will it be the sum of all the routes traveled by the agent. The
search is carried out depending on the type uncommented.

The following search algorithms where applied to this problem and the results follow:

By M Kamali                                     http://www.javansystems.co.uk
Artificial Intelligence: Search Algorithms

1. Breath-First Search

BFSearch: starting search
BFSearch: search complete

Goal found at depth 3
Bucharest
Start -> Sibiu -> Fagaras -> Bucharest
Cost 450.00
Nodes created     = 40
Max Queue length = 25

2. Depth-First Search

Depth First Search object created

3. Depth-Limited Search

>> with a max depth of 5:
Depth Limited Search object created with maximum depth 5
DLSearch: starting search
DLSearch: search finished

Goal found at depth 5
Bucharest
Start -> Sibiu -> Arad -> Sibiu -> Fagaras -> Bucharest
Cost 730.00
Nodes created     = 20
Max Queue length = 13

>>with a max depth of 3:
Depth Limited Search object created with maximum depth 3
DLSearch: starting search
DLSearch: search finished

Goal found at depth 3
Bucharest
Start -> Sibiu -> Fagaras -> Bucharest
Cost 450.00
Nodes created     = 13
Max Queue length = 8

>>with a max depth of 2:
Depth Limited Search object created with maximum depth 2
DLSearch: starting search
DLSearch: search finished

Nodes created         = 12
Max Queue length      = 6

4. Iterative Deepening Search

By M Kamali                         http://www.javansystems.co.uk
Artificial Intelligence: Search Algorithms

Iterative   Deepening DLSearch
IDSearch:   starting search
IDSearch:   searching to depth     0
IDSearch:   searching to depth     1
IDSearch:   searching to depth     2
IDSearch:   searching to depth     3
IDSearch:   search finished

Goal found at depth 3
Bucharest
Start -> Sibiu -> Fagaras -> Bucharest
Cost 450.00
Nodes created     = 31
Max Queue length = 8

5. Greedy Search
Greedy Search object created
GreedySearch: starting search
GreedySearch: search complete

Goal found at depth 3
Bucharest
Start -> Sibiu -> Fagaras -> Bucharest
Cost 450.00
Nodes created     = 10
Max Queue length = 7

6. A* Search
A* Search object created
A*: starting search
Trying Sibiu f is 393.00
Trying Rimnicu Vilcea f is 413.00
Trying Pitesi f is 415.00
Trying Fagaras f is 417.00
Trying Bucharest f is 418.00
A* Search: search complete

Goal found at depth 4
Bucharest
Start -> Sibiu -> Rimnicu Vilcea -> Pitesi -> Bucharest
Cost 418.00
Nodes created     = 16
Max Queue length = 11

Analysis

The following table summarizes the results obtained from the above search methods:

Method                Cost                    Nodes created       Max queue length
BF Search             450                     40                  25
DF Search             NA                      NA                  NA

By M Kamali                                http://www.javansystems.co.uk
Artificial Intelligence: Search Algorithms

DL Search (D = 5)      730                    20                     13
DL Search (D = 3)      450                    13                     8
DL Search (D = 2)      NA                     12                     6
ID Search              450                    31                     8
G Search               450                    10                     7
A* Search              418                    16                     11

• Optimality
Optimality is determined by the path cost which in this problem is the distance
between the towns.

According to the above table, A* Search has the found the least path cost to the goal
state.

• Time complexity
Time complexity is determined by the number of nodes expanded. The fewer nodes
expanded, the less is the time complexity.

According to the above table, Greedy Search has the least number of nodes expanded
and therefore is the least complex one.

• Space complexity
Space complexity is related to the maximum queue length obtained from each search
algorithm. In the table above, Greedy Search has the least queue length is therefore
more efficient in this case.

Conclusion:
As seen above, A* Search is the most optimal Search method for this problem. But it
seems the greedy search algorithm is a more suitable one since it is very conservative in
terms of opening nodes and only goes through those that are estimated to be closest to the
goal state and therefore creates less time and space complexity which adds to the overall
efficiency of the search.

By M Kamali                                  http://www.javansystems.co.uk
Artificial Intelligence: Search Algorithms

Problem 2: Missionaries and Cannibals

M                                         M

M                                         M

M                                         M

C                                          C

C                                          C

C                                          C

In this problem there are three missionaries and three cannibals in one aide of the river, a
boat is going to transfer all six people to other side of the river. However there are some
rules that make the process a bit tricky. The boat can only carry two people at the same
time and at any one time, and at both sides of the river, the missionaries and the cannibals
must not outnumber each other.

The initial state is defined in the Main file by ‘new MandCState(3,3,true)’
The goal state is also defined by ‘new MandCState(0,0,false)’

The first integer (m) represents missionaries, the second (c) is for cannibals and the
Boolean value is indication whether the boat is on this side of the river or not.

The initial state says there are 3 missionaries and 3 cannibals present on this side of the
river and the boat is also on this side. While the goal state says there should be no
missionaries and no cannibals left on this side and the boat should also be at the other
side.

‘goalTest’ checks whether in the current state of the program (the number of
missionaries and cannibals on this side of the river and the position of the boat) match
that of the goal state. If all the conditions are satisfied, the function returns true, otherwise
false:

public boolean goalTest(State state) {
MandCState s = (MandCState) state;

By M Kamali                                     http://www.javansystems.co.uk
Artificial Intelligence: Search Algorithms

return (missionaries == s.missionaries
&& cannibals == s.cannibals
&& boat == s.boat);
}

The program makes use of ‘legal and illegal’ states. Meaning that every time the state has
changed, it checks whether the overall system complies with the rules of the game or not.
If so, it carries on to the goal test and then if necessary to the next move; however if the
new state is considered illegal, it will go back and create a new state and the loop goes on
for every movement.

Path cost is defined by the number of movements of the boat or in other words the
number of states that we have to go through before we reach to the goal state. The search
is carried out depending on the type uncommented.

The following search algorithms where applied to this problem and the results follow:

1. Breath-First Search

BFSearch: starting search
BFSearch: search complete

Goal found at depth 9
[M 0]:[C 0]:[B false]
Start
-> moving 1 M and 1 C across
-> moving 0 M and 1 C back
-> moving 2 M and 0 C across
-> moving 1 M and 0 C back
-> moving 1 M and 1 C across
-> moving 0 M and 1 C back
-> moving 0 M and 2 C across
-> moving 1 M and 0 C back
-> moving 1 M and 1 C across
Cost 9.00
Nodes created     = 2145
Max Queue length = 1274

2. Depth-First Search

Depth First Search object created

3. Depth-Limited Search

>> with a max depth of 3:
Depth Limited Search object created with maximum depth 3
DLSearch: starting search
DLSearch: search finished

By M Kamali                                   http://www.javansystems.co.uk
Artificial Intelligence: Search Algorithms

Nodes created       = 10
Max Queue length    = 4

>>with a max depth of 9:
Depth Limited Search object created with maximum depth 9
DLSearch: starting search
DLSearch: search finished

Goal found at depth 9
[M 0]:[C 0]:[B false]
Start
-> moving 1 M and 1 C across
-> moving 0 M and 1 C back
-> moving 2 M and 0 C across
-> moving 1 M and 0 C back
-> moving 1 M and 1 C across
-> moving 0 M and 1 C back
-> moving 0 M and 2 C across
-> moving 1 M and 0 C back
-> moving 1 M and 1 C across
Cost 9.00
Nodes created     = 443
Max Queue length = 14

>>with a max depth of 12:
Depth Limited Search object created with maximum depth 12
DLSearch: starting search
DLSearch: search finished

Goal found at depth 11
[M 0]:[C 0]:[B false]
Start
-> moving 1 M and 1 C across
-> moving 0 M and 1 C back
-> moving 0 M and 1 C across
-> moving 0 M and 1 C back
-> moving 2 M and 0 C across
-> moving 1 M and 0 C back
-> moving 1 M and 1 C across
-> moving 0 M and 1 C back
-> moving 0 M and 2 C across
-> moving 1 M and 0 C back
-> moving 1 M and 1 C across
Cost 11.00
Nodes created     = 1121
Max Queue length = 19

4. Iterative Deepening Search

Iterative   Deepening DLSearch
IDSearch:   starting search
IDSearch:   searching to depth 0
IDSearch:   searching to depth 1
IDSearch:   searching to depth 2

By M Kamali                          http://www.javansystems.co.uk
Artificial Intelligence: Search Algorithms

IDSearch:   searching to depth   3
IDSearch:   searching to depth   4
IDSearch:   searching to depth   5
IDSearch:   searching to depth   6
IDSearch:   searching to depth   7
IDSearch:   searching to depth   8
IDSearch:   searching to depth   9
IDSearch:   search finished

Goal found at depth 9
[M 0]:[C 0]:[B false]
Start
-> moving 1 M and 1 C across
-> moving 0 M and 1 C back
-> moving 2 M and 0 C across
-> moving 1 M and 0 C back
-> moving 1 M and 1 C across
-> moving 0 M and 1 C back
-> moving 0 M and 2 C across
-> moving 1 M and 0 C back
-> moving 1 M and 1 C across
Cost 9.00
Nodes created     = 1517
Max Queue length = 14

5. Greedy Search
Greedy Search object created

6. A* Search
Trying [M 0]:[C 2]:[B false]     f is   8.00
Trying [M 1]:[C 1]:[B false]     f is   8.00
Trying [M 2]:[C 0]:[B false]     f is   8.00
...
...
Trying [M 2]:[C 0]:[B false]     f is   8.00
Trying [M 3]:[C 0]:[B false]     f is   8.00
Trying [M 0]:[C 0]:[B false]     f is   8.00
A* Search: search complete

Goal found at depth 9
[M 0]:[C 0]:[B false]
Start
-> moving 1 M and 1 C across
-> moving 1 M and 0 C back
-> moving 0 M and 2 C across
-> moving 0 M and 1 C back
-> moving 1 M and 1 C across
-> moving 1 M and 0 C back
-> moving 2 M and 0 C across
-> moving 0 M and 1 C back
-> moving 1 M and 1 C across
Cost 9.00
Nodes created     = 2464

By M Kamali                             http://www.javansystems.co.uk
Artificial Intelligence: Search Algorithms

Max Queue length       = 1473

Analysis

The following table summarizes the results obtained from the above search methods:

Method                Cost                    Nodes created           Max queue length
BF Search             9                       2145                    1274
DF Search             NA                      NA                      NA
DL Search (D = 3)     NA                      10                      4
DL Search (D = 9)     9                       443                     14
DL Search (D = 12)    11                      1129                    19
ID Search             9                       1515                    14
G Search              NA                      NA                      NA
A* Search             9                       2464                    1473

• Optimality
Optimality is determined by the path cost.
According to the above table, A* Search, BF Search, DL Search (d=9) and ID Search
have all found the least path cost to the goal state. However some (namely DF and
Greedy algorithms) have not found the goal state and therefore are not optimal.

ID Search seems the best technique if the path cost is initially unknown for this
problem.

• Time complexity
Time complexity is determined by the number of nodes expanded. The fewer nodes
expanded, the less is the time complexity.

DL Search with 443 nodes, is the least complex from this perspective. It should be
noted however that in this method the depth was already given to the software.
A* has expanded 2464 nodes, the highest among all algorithms and therefore is the
least efficient one.

It is also obvious that for those cases where the goal state has not been achieved, the
time complexity is inefficient and in DF Search and Greedy Search the complexity
has been so high that the memory became full and an exception was thrown.

• Space complexity
Space complexity is related to the maximum queue length obtained from each search
algorithm. In the table above, DL Search with a maximum depth of 9 has the least
queue length of 14 and is therefore more efficient in this case.
A* is the least efficient one among those that have not had an infinite loop to cause
the memory become full.

By M Kamali                                  http://www.javansystems.co.uk
Artificial Intelligence: Search Algorithms

In case of BF Search, although it is expensive in terms of space complexity,
sometimes it might turn out to be ok to use it since we know for sure that it will reach
the goal state at some point. However long it takes; because it will open all the nodes
and one of them will be the answer finally.

Conclusion:
As seen above, DL Search is overall the most optimal Search method for this problem.

By M Kamali                                  http://www.javansystems.co.uk
Artificial Intelligence: Search Algorithms

Problem 3: The Chains

In this problem there are four sets of chains each containing three links. The problem is to
make a large chain of 12 hoops connected at both ends to form a circle. Operators can
open and close each link and this represents the cost of the operation.

The initial state is defined as four chains each with three lines.
The goal state is also defined by ‘State goal = (ChainState) new
ChainState(12, g)’

‘goalTest’ accepts an parameter of type State as input and checks whether in this
particular state of the system, there is only one hoop of 12 links present and if they are all
closed and connected at both ends. If this condition is satisfied, the function returns true,
otherwise false:

public boolean goalTest(State state) {

ChainState thisState = new ChainState(this);
ChainState otherState =
new ChainState(((ChainState) state));
boolean allFound = true;
Iterator i1 = thisState.lines.iterator();
while(i1.hasNext() && allFound) {
Line l1 = (Line) i1.next();
boolean thisFound = false;
Iterator i2 = otherState.lines.iterator();
while(i2.hasNext() && !thisFound) {
Line l2 = (Line) i2.next();
if(l1.equals(l2)) {
i1.remove();
i2.remove();
thisFound = true;
}

By M Kamali                                    http://www.javansystems.co.uk
Artificial Intelligence: Search Algorithms

}
if(!thisFound)
allFound = false;
}
allFound = false;
return allFound;
}

The following search algorithms where applied to this problem and the results follow:

1. Breath-First Search

Start State
-----------
This ChainState has 0 open link(s) and 4 line(s)/loop(s)
Line: 000
Line: 000
Line: 000
Line: 000
Difference from goal is    0

Goal State
----------
This ChainState has 0 open link(s) and 1 line(s)/loop(s)
Loop: 000000000000
Difference from goal is    0

2. Depth-First Search

Start State
-----------
This ChainState has 0 open link(s) and 4 line(s)/loop(s)
Line: 000
Line: 000
Line: 000
Line: 000
Difference from goal is    0

Goal State
----------
This ChainState has 0 open link(s) and 1 line(s)/loop(s)
Loop: 000000000000
Difference from goal is    0

Depth First Search object created

3. Depth-Limited Search

>> with a max depth of 2:

By M Kamali                                 http://www.javansystems.co.uk
Artificial Intelligence: Search Algorithms

Start State
-----------
This ChainState has 0 open link(s) and 4 line(s)/loop(s)
Line: 000
Line: 000
Line: 000
Line: 000
Difference from goal is    0

Goal State
----------
This ChainState has 0 open link(s) and 1 line(s)/loop(s)
Loop: 000000000000
Difference from goal is    0

Depth Limited Search object created with maximum depth 2
DLSearch: starting search
DLSearch: search finished

Nodes created      = 213
Max Queue length   = 35

>>with a max depth of 6:
Start State
-----------
This ChainState has 0 open link(s) and 4 line(s)/loop(s)
Line: 000
Line: 000
Line: 000
Line: 000
Difference from goal is    0

Goal State
----------
This ChainState has 0 open link(s) and 1 line(s)/loop(s)
Loop: 000000000000
Difference from goal is    0

Depth Limited Search object created with maximum depth 6
DLSearch: starting search
DLSearch: search finished

Goal found at depth 6

This ChainState has 0 open link(s) and 1 line(s)/loop(s)
Loop: 000000000000
Difference from goal is    0
Start --> Opened link 1 of Line: 000 --> Opened link 1 of Line:
00 --> Opened link 1 of Line: 0 --> Closed link round Line: 000
and Line: 000 --> Closed link round Line: 000 and Line: 0000000 -
-> Closed link to make Loop: 000000000000
Cost 6.00
Nodes created     = 1436213
Max Queue length = 176

By M Kamali                         http://www.javansystems.co.uk
Artificial Intelligence: Search Algorithms

4. Iterative Deepening Search
Start State
-----------
This ChainState has 0 open link(s) and 4 line(s)/loop(s)
Line: 000
Line: 000
Line: 000
Line: 000
Difference from goal is    0
Goal State
----------
This ChainState has 0 open link(s) and 1 line(s)/loop(s)
Loop: 000000000000
Difference from goal is    0
IDSearch: starting search
IDSearch: searching to depth 0
IDSearch: searching to depth 1
IDSearch: searching to depth 2
IDSearch: searching to depth 3
IDSearch: searching to depth 4
IDSearch: searching to depth 5
IDSearch: searching to depth 6
IDSearch: search finished
Goal found at depth 6
This ChainState has 0 open link(s) and 1 line(s)/loop(s)
Loop: 000000000000
Difference from goal is    0
Start --> Opened link 1 of Line: 000 --> Opened link 1 of Line:
00 --> Opened link 1 of Line: 0 --> Closed link round Line: 000
and Line: 000 --> Closed link round Line: 000 and Line: 0000000 -
-> Closed link to make Loop: 000000000000
Cost 6.00
Nodes created     = 2630112
Max Queue length = 176

5. Greedy Search
Start State
-----------
This ChainState has 0 open link(s) and 4 line(s)/loop(s)
Line: 000
Line: 000
Line: 000
Line: 000
Difference from goal is    0
Goal State
----------
This ChainState has 0 open link(s) and 1 line(s)/loop(s)
Loop: 000000000000
Difference from goal is    0
Greedy Search object created
java.lang.OutOfMemoryError

6. A* Search

By M Kamali                         http://www.javansystems.co.uk
Artificial Intelligence: Search Algorithms

A* Search object created
Start State
-----------
This ChainState has 0 open        link(s) and 4 line(s)/loop(s)
Line: 000
Line: 000
Line: 000
Line: 000
Difference from goal is           0
Goal State
----------
This ChainState has 0 open        link(s) and 1 line(s)/loop(s)
Loop: 000000000000
Difference from goal is           0
java.lang.OutOfMemoryError

Analysis

The following table summarizes the results obtained from the above search methods:

Method                Cost                    Nodes created          Max queue length
BF Search             NA                      NA                     NA
DF Search             NA                      NA                     NA
DL Search (D = 2)     NA                      213                    35
DL Search (D = 6)     6                       1436213                176
ID Search             6                       2630112                176
G Search              NA                      NA                     NA
A* Search             NA                      NA                     NA

• Optimality
Optimality is determined by the path cost which in this problem is the distance
between the towns.

According to the above table, DL Search with a limit of 6 and the ID Search hve both
found the least cost of 6 to the problem. However, the DL Search had to be first
supplied with a realistic value for the depth otherwise it would not return the desired
solution (as happened when tried with depth 2!)

• Time complexity
Time complexity is determined by the number of nodes expanded. The fewer nodes
expanded, the less is the time complexity.
Of the only two algorithms that were successful in reaching the goal state, DL Search
with limit of 6, has the fewer nodes expanded which makes it more efficient in this
regard.

•   Space complexity

By M Kamali                                  http://www.javansystems.co.uk
Artificial Intelligence: Search Algorithms

Space complexity is related to the maximum queue length obtained from each search
algorithm.
For this problem, again both DL and ID searches have an equal number of queue
lengths in memory which makes both of them suitable. Not be forgotten that this is
only relevant if the goal state is achieved which hasn’t been the case for the rest of the
algorithms.

Conclusion:
Overall it seems, Depth Limited search is more suitable for this problem.

By M Kamali                                   http://www.javansystems.co.uk
Artificial Intelligence: Search Algorithms

Part two
Development of a heuristic function

The Heuristic function employs an informed search method. Specific information is given
to this function by means of a knowledge base in order to optimize the search. This
directs the agent to the goal state and also prevents unnecessary nodes to be expanded
thus saving memory space. A heuristic function does not guarantee to discover the goal
state. A good heuristic function should discover the goal state with the best possible path
cost, time complexity and space complexity.

To develop this function, ChainState.java, has to be modified. The function will be
placed in the evaluate() method.

The heuristic function will be tested with tow search algorithms: Greedy search and A*
search. Greedy search will give the fastest outcome but with little optimality.

//Returns the length of the current states to the evaluate method
public static void setGoalVector( Vector e)
{
goal_lines=e;
}

public double evaluate() {

// compare number of "differences" to goal
//double differences;

Line L1;
for(int i=0;i<lines.size();i++){

/* The following, loops through the current state and
assigns the length, using the length() method in
Line.java to the double value numLinks.*/

L1=(Line) lines.elementAt(i);
}

/* Variable ‘num’ holds the value of the length in the goal
state, which is initialized at the start of the class. The
vector goal_lines is fetched from ChainMain.java and
assigned by the new method setGoalVector().*/

Line L2=(Line) goal_lines.elementAt(0);
double num = L2.length();

/* Calculate the difference. The larger the difference the
further it is away from the goal and the higher the
probability that it will be disregarded.*/

By M Kamali                                  http://www.javansystems.co.uk
Artificial Intelligence: Search Algorithms

System.out.println("the difference is "+differences);

// return the difference
return differences;

}

The following search algorithms where applied to this problem and the results follow:

1. Greedy Search

Greedy Search object created
Start State
-----------
This ChainState has 0 open link(s) and 4 line(s)/loop(s)
Line: 000
Line: 000
Line: 000
Line: 000
Difference from goal is    9
the difference is 0.0
Goal State
----------
This ChainState has 0 open link(s) and 1 line(s)/loop(s)
Loop: 000000000000
Difference from goal is    0
the difference is 10.0
the difference is 11.0
...
the difference is 6.0
the difference is 0.0
GreedySearch: starting search
GreedySearch: search complete
Goal found at depth 8
This ChainState has 0 open link(s) and 1 line(s)/loop(s)
Loop: 000000000000
Difference from goal is    0
Start --> Opened link 1 of Line: 000 --> Closed link round
Line: 000 and Line: 000 --> Opened link 1 of Line: 0000000 -->
Closed link round Line: 000 and Line: 000000 --> Opened link 1 of
Line: 0000000000 --> Closed link round Line: 00 and Line:
to make Loop: 000000000000
Cost 8.00
Nodes created     = 114
Max Queue length = 102

2. A* Search
Start State
-----------
This ChainState has 0 open link(s) and 4 line(s)/loop(s)
Line: 000

By M Kamali                                 http://www.javansystems.co.uk
Artificial Intelligence: Search Algorithms

Line: 000
Line: 000
Line: 000
Difference from goal is    9
the difference is 0.0
Goal State
----------
This ChainState has 0 open link(s) and 1 line(s)/loop(s)
Loop: 000000000000
Difference from goal is    0
the difference is 9.0
the difference is 9.0
the difference is 10.0
...
Trying
This ChainState has 0 open link(s) and 1 line(s)/loop(s)
Loop: 000000000000
Difference from goal is    0 f is   8.00
A* Search: search complete
Goal found at depth 8
This ChainState has 0 open link(s) and 1 line(s)/loop(s)
Loop: 000000000000
Difference from goal is    0
Start --> Opened link 1 of Line: 000 --> Closed link round Line:
000 and Line: 000 --> Opened link 1 of Line: 0000000 --> Closed
link round Line: 000 and Line: 000000 --> Opened link 1 of Line:
0000000000 --> Closed link round Line: 00 and Line: 000000000 -->
Opened link 1 of Line: 000000000000 --> Closed link to make Loop:
000000000000
Cost 8.00
Nodes created     = 137
Max Queue length = 121

It is apparent from the following table how the efficiency can be improved using the
Method                  Cost                   Nodes created         Max queue length
Greedy Search           8                      114                   102
A* Search               8                      137                   121
DL Search (D = 6)       6                      1436213               176
ID Search               6                      2630112               176

It is interesting to note that when the same search algorithms where applied without a
heuristic function, they both ran out of memory and where unable to reach the goal state.
And also when compared to the previous two methods (DL Search and ID Search) there
has been significant optimization and improvement in the use of system resources as is
clear from the number of nodes expanded and the maximum length of the queue. It terms
of optimality, it was seen previously that DL Search returned a minimum cost of 6. This
was however achieved by expanding 1436213 nodes which is much more expensive
compared to only 114 in Greedy search or 137 in A*.

Overall it can be concluded that among these two, Greedy search is even more suitable
because of fewer nodes and less queue length and therefore less time/space complexity.

By M Kamali                                 http://www.javansystems.co.uk

```
DOCUMENT INFO
Shared By:
Categories:
Stats:
 views: 186 posted: 4/2/2010 language: English pages: 24