| Artificial Intelligence Search Techniques in Java by Mark Watson
|
Chapter 1 - A brief history of AIWhen computers were first built, their speed of numerical computations convinced most people that the following fallacy was true:
Now, computers are millions of times faster than they were fifty years ago. Still, I would argue that for many tasks, this statement is still a fallacy. Human brains seem to be far "faster" than computers for a wide variety of tasks; for example:
Still, greater computational speed does make some so-called AI systems seem smarter. Very good examples of this are computer games that use search techniques. I have been interested in writing computer chess and Go programs for a long time (I wrote the BASIC chess program on the demo cassette tape for the Apple II computer, and I wrote and marketed a commercial Go program, Honninbo Warrior, also for the Apple II). This short "book" covers the very rudiments of AI search techniques: we initially cover depth first and then breadth first search of networks. We will discuss how to add heuristics to the breadth first search. A network is defined as a set of nodes and a set of bi-directional links connecting these nodes. Unless you are not at all curious, you have already tried running the two example Java applets that appeared to the right of the table of contents of this book. Each applet has a network of nine nodes, with several links (e.g., connecting node 0 to node 1, 2 to 3, 2 to 4, etc.). Each Java applet tries to find a path from the starting node to the goal node. In the two sample applets, you can change the starting node and the goal node by using the Java AWT (abstract window toolkit) choice controls. The development of search techniques was considered to be one of the early successes of AI research. There are many techniques of AI programming that are not covered here (e.g., expert systems, neural networks, planning, etc.). Also, the material on search in this "book" is very rudimentary, and is written for home hobbiest-programmers. Chapter 2 - Why search algorithms are not AISearch algorithms are a technique, but, in my opinion, do not represent AI at all. However, search algorithms are useful for building some types of AI systems. For example, search is an important part in computer programs for game playing (like chess and Go) and planning systems. Planning systems usually involve the automatic generation and evaluation of plans to accomplish specific goals. It a very simple form, one might (or might not) call our two sample Java applets simple planning systems (to answer questions like "what route will take me from node 0 to node 9?"). However, real AI planning systems involve more complex tasks such as planning routes for robots to take through real (physical) environment, etc. So, we will consider search to be a tool, or technique, that can be used in AI planning systems, computer games, et. One idea that came out of early AI research was the notion of separating domain independent code from domain dependent data. Probably the best example of this technique is so-called "expert systems". There are many good domain independent "engines" like OPS5, CLIPS, and Jess that are completely independent of any knowledge for solving specific problems. Instead, these "engines" use data in the for of "if/then" rules to solve problems. As an example (from my Java AI book) we might want to write an expert system to diagnose medical problems associated with skin diving and scuba diving. Here we would develop a large set of domain specific (i.e., specific to solving the problem of diving medical problems) rules. An example rule might be (translated to "English"): If there are two puncture marks and the surronding area is red, then there is a strong probablity of an octopus bite. If there are two puncture marks and the surronding area is not red, then there is a weak probablity of an octopus bite. Most people consider so-called "expert systems" to be AI. Another example of AI is natural language processing. There are many aspects to automatically processing (with computer software) natural language text. Most systems start by calculating parts of speech for input text. For example (from my free Open Source NLPServer will calculate parts of speech for any input sentence (or at least try to!). The NLPserver outputs results in either plain text, HTML, or XML. For example: Output type: TEXT Part of speech query: the dog ran down the street art, the; noun, dog; verb, ran; adj, down; art, the; noun, street; Other types of AI research involve the use of artificial neural networks, genetic algorithms, and genetic programming. Chapter 3 - How we can use search algorithms in AI systemsIn Chapter 2, we indicated that search techniques are a powerful tool for building some types of AI systems. Here are two example applications of search techniques:
There are many other possible uses for the search, but I think that one of these examples would be a good way for you to apply what you learn in this "book". Chapter 4. A Java Framework for Search EnginesPurposeWe want a Java class framework for implementing different search algorithms. Our goals (or requirements) are to design and implement:
We also want a nice way to display networks and to visualize how the search algorithms work for a given network, and for a given starting and ending path point. We will design and implement a Graphics User Interface (GUI) class SearchPanel that can be created with any instance of a sub-class of Search. As a final task, we will write two Java applets (that is shown on the first page) that demonstrates both depth first and breadth first search. These three Java classes are Open Source software; please feel free to remove the search specific code from the two test applets, and use it in your own applications! (Also, please feel free to re-distribute this document!)
Chapter 5 - Implementing Depth First SearchThere are two active Java applets shown to the right of the table of contents of this book showing a demonstration of depth first and breadth first search . These two Java applets show a simple network:
Before we design and implement any search programs, we will "walk through" a search of this simple network, trying to find a path from node "0" to node "9". Specifically, we will manually perform a depth first search. In the file DepthFirstSearch.java, we define the positions of nine test nodes:
addNode("0", 0.0f, 0.0f);
addNode("1", 1.0f, 1.0f);
addNode("2", 5.0f, 2.0f);
addNode("3", 2.0f, 5.0f);
addNode("4", 7.0f, 5.0f);
addNode("5", 8.0f, 8.0f);
addNode("6", 10.0f, 5.0f);
addNode("7", 8.0f, 2.0f);
addNode("8", 12.0f, 8.0f);
addNode("9", 13.0f, 5.0f);
In the above figure showing our sample network, we define the "links" in the following order:
addLink(0,1);
addLink(1,2);
addLink(2,3);
addLink(2,4);
addLink(4,5);
addLink(4,6);
addLink(6,8);
addLink(8,9);
addLink(2,7);
addLink(7,9);
The order that the links are defined is important in a depth first search. if we start at node "0" and search to node "9", then we find the first link leaving node "0" (in this case link "0" to "1"). Then we take the first link leaving node "1" (in this case link "1' to "2"), etc. When we "run out of" links to explore from a given node, then we "back up" to the preceeding node, and explore all other links that leave that node. I am sure that you have already tried running the depth first search applet, right? Then you will have noticed that it found a rather long path from node "0" to "node "9". The depth first search applet stops when it has found a path. The breadth first search applet in Chapter 6 finds better paths, in general. I assume that you know how to program in Java, so I will just briefly explain what each method in classes SearchApplet and DepthFirstSearch. We will discuss the implementation of BreadthFirstSearch in Chapter 6. Methods in SearchApplet
Methods defined in class DepthFirstSearchThe class DepthFirstSearch is derived from the class SearchApplet and defines the following methods:
If you have not already done so, return to the table of contents page, and experiment with the depth first search applet. Chapter 6 - Implementing Breadth First SearchWe saw in Chapter 5 how networks are initialized in the base class SearchApplet and the implementation of the derived class DepthFirstSearch. In this chapter, we will derive another class BreadthFirstSearch from class SearchApplet that calculates, in general, better paths that the depth first version. You can use this new class in your own applications, optionally adding search heuristics for the particular type of problem that you are solving. The breadth first search uses a Queue data structure (a queue is a sequence that is "first in, first out"; that is, you remove items from a queue in the same order that they were added). If we want to perform a breadth first search from node "0" to node "9", we start by putting all of the nodes connected to node "0" on the queue. Then we add the nodes connected to the items currently in the queue, and continue this process until we arrive at the goal node. For many applications, you would want to add heuristics to a breadth first search by controlling the "order in which nodes are added to the queue. For example, let us look again at our sample network:
If we are starting at node "4" and searching for node "9", then we would first add the following nodes to the queue:
The BreadthFirstSearch class adds these nodes in an arbitrary order. However, you might want to add (this is an exercise for the reader) the heuristic that you add nodes that are closer to the goal node first. For most problems, the ordering of nodes added to the queue will have a dramatic effect on performance. If we ordered the nodes connected to node "4" in order of closeness to the goal node "9", then we would add them in this order:
Methods defined in class BreadthFirstSearchThe class BreadthFirstSearch is derived from the class SearchApplet and defines the following methods:
This concludes my "book" on simple search techniques in Java. Really, I have just skimmed the surface of this material, but I hope that reading this book has provided you with both some understanding of search techniques, and some Open Source Java source code that you can use for both education and in your own software products. aa |