Is Lecture Notes Unit 01

download Is Lecture Notes Unit 01

of 52

Transcript of Is Lecture Notes Unit 01

  • 8/12/2019 Is Lecture Notes Unit 01

    1/52

    CSE-304-F Intelligent system By Kaushal Shakya

    1

    Lecture 1

    Foundation and history of AI

    Contents1. Definition of AI2. Goals of AI3. AI as Science4. AI as Engineering5. Areas of AI6. Foundation of AI7. History of AI

    Definition of Artificial Intelligence

    Artificial intelligence (AI) is the intelligence of machines and the branch of computer sciencethat aims to create it. AI textbooks define the field as "the study and design of intelligent agents"where an intelligent agent is a system that perceives its environment and takes actions thatmaximize its chances of success. Artificial Intelligence (AI) is the area of computer sciencefocusing on creating machines that can engage on behaviors that humans consider intelligent.

    Goals ofArtificial IntelligenceAI seeks to understand the working of the mind in mechanistic terms, just as medicine seeks tounderstand the working of the body in mechanistic terms.The mind is what the brain does.

    -- Marvin Minsky

    The strong AIposition is that any aspect of human intelligence could, in principle, bemechanized.

    Artificial Intelligence as Science

    Intelligence should be placed in the context of biology:

    Intelligence connects perception to actionto help an organism survive.

    Intelligence is computation in the service of life, just as metabolism is chemistry in the service oflife.

    Intelligence does not imply perfect understanding; every intelligent being has limitedperception, memory, and computation. Many points on the spectrum of intelligence-versus-costare viable, from insects to humans. AI seeks to understand the computations required for intelligentbehaviorand to produce computer systems that exhibit intelligence.

  • 8/12/2019 Is Lecture Notes Unit 01

    2/52

    CSE-304-F Intelligent system By Kaushal Shakya

    2

    Aspects of intelligence studied by AI include perception, motor control, communication usinghuman languages, reasoning, planning, learning, and memory.

    Artificial Intelligence as Engineering

    How can we make computer systems more intelligent?

    Autonomyto perform tasks that currently require human operators without humanintervention or monitoring.

    Flexibilityin dealing with variability in the environment. Ease of use: computers that are able to understand what the user wants from limited

    instructions in natural languages. Learningfrom experience.

    Areas of Artificial Intelligence

    Perceptiono Machine visiono Speech understandingo Touch (tactileor haptic) sensation

    Robotics Natural Language Processing

    o Natural Language Understandingo Speech Understandingo Language Generationo Machine Translation

    Planning

    Expert Systems Machine Learning Theorem Proving Symbolic Mathematics Game Playing

    Foundation of AI

    Although the computer provided the technology necessary for AI, it was notuntil the early 1950's that the link between human intelligence and machines wasreally observed. Norbert Wiener was one of the first Americans to make

    observations on the principle of feedback theory feedback theory. The mostfamiliar example of feedback theory is the thermostat: It controls thetemperature of an environment by gathering the actual temperature of thehouse, comparing it to the desired temperature, and responding by turning theheat up or down. What was so important about his research into feedback loops

    was that Wiener theorized that all intelligent behavior was the result of feedback mechanisms.

  • 8/12/2019 Is Lecture Notes Unit 01

    3/52

    CSE-304-F Intelligent system By Kaushal Shakya

    3

    Mechanisms that could possibly be simulated by machines. This discovery influenced much ofearly development of AI.

    In late 1955, Newell and Simon developed The Logic Theorist, considered by many to be the firstAI program. The program, representing each problem as a tree model, would attempt to solve it

    by selecting the branch that would most likely result in the correct conclusion. The impact thatthe logic theorist made on both the public and the field of AI has made it a crucial steppingstone in developing the AI field.

    In 1956 John McCarthy regarded as the father of AI, organized aconference to draw the talent and expertise of others interested inmachine intelligence for a month of brainstorming. He invited them toVermont for "The Dartmouth summer research project on artificialintelligence." From that point on, because of McCarthy, the field wouldbe known as Artificial intelligence. Although not a huge success,(explain) the Dartmouth conference did bring together the founders inAI, and served to lay the groundwork for the future of AI research.

    Knowledge Expansion

    In the seven years after the conference, AI began to pick upmomentum. Although the field was still undefined, ideas formed at theconference were re-examined, and built upon. Centers for AI research began forming atCarnegie Mellon and MIT, and new challenges were faced: further research was placed uponcreating systems that could efficiently solve problems, by limiting the search, such as the LogicTheorist. And second, making systems that could learn by themselves.

    In 1957, the first version of a new program The General Problem Solver(GPS) was tested. Theprogram developed by the same pair which developed the Logic Theorist. The GPS was anextension of Wiener's feedback principle, and was capable of solving a greater extent ofcommon sense problems. A couple of years after the GPS, IBM contracted a team to researchartificial intelligence. Herbert Gelerneter spent 3 years working on a program for solvinggeometry theorems.

    While more programs were being produced, McCarthy was busy developing a majorbreakthrough in AI history. In 1958 McCarthy announced his new development; the LISPlanguage, which is still used today. LISP stands for LISt Processing, and was soon adopted asthe language of choice among most AI developers.

    In 1963 MIT received a 2.2 million dollar grant from the United States government to be used inresearching Machine-Aided Cognition (artificial intelligence). The grant by the Department of

  • 8/12/2019 Is Lecture Notes Unit 01

    4/52

    CSE-304-F Intelligent system By Kaushal Shakya

    4

    Defense's Advanced research projects Agency (ARPA), to ensure that the US would stay aheadof the Soviet Union in technological advancements. The project served to increase the pace ofdevelopment in AI research, by drawing computer scientists from around the world, andcontinues funding.

    The Multitude of programs

    The next few years showed a multitude of programs,one notably was SHRDLU. SHRDLU was part of themicroworlds project, which consisted of research andprogramming in small worlds (such as with a limitednumber of geometric shapes). The MIT researchersheaded by Marvin Minsky, demonstrated that whenconfined to a small subject matter, computer programscould solve spatial problems and logic problems. Otherprograms which appeared during the late 1960's wereSTUDENT, which could solve algebra story problems, and SIR which could understand simpleEnglish sentences. The result of these programs was a refinement in language comprehensionand logic.

    Another advancement in the 1970's was the advent of the expert system. Expert systems predictthe probability of a solution under set conditions. For example:

    Because of the large storage capacity of computers at the time, expert systems had the potentialto interpret statistics, to formulate rules. And the applications in the market place wereextensive, and over the course of ten years, expert systems had been introduced to forecast thestock market, aiding doctors with the ability to diagnose disease, and instruct miners topromising mineral locations. This was made possible because of the systems ability to storeconditional rules, and a storage of information.

    During the 1970's Many new methods in the development of AI were tested, notably Minsky'sframes theory. Also David Marr proposed new theories about machine vision, for example, howit would be possible to distinguish an image based on the shading of an image, basicinformation on shapes, color, edges, and texture. With analysis of this information, frames ofwhat an image might be could then be referenced. another development during this time wasthe PROLOGUE language. The language was proposed for In 1972,

    During the 1980's AI was moving at a faster pace, and further into the corporate sector. In 1986,US sales of AI-related hardware and software surged to $425 million. Expert systems in

    particular demand because of their efficiency. Companies such as Digital Electronics were usingXCON, an expert system designed to program the large VAX computers. DuPont, GeneralMotors, and Boeing relied heavily on expert systems Indeed to keep up with the demand for thecomputer experts, companies such as Teknowledge and Intellicorp specializing in creatingsoftware to aid in producing expert systems formed. Other expert systems were designed tofind and correct flaws in existing expert systems.

  • 8/12/2019 Is Lecture Notes Unit 01

    5/52

    CSE-304-F Intelligent system By Kaushal Shakya

    5

    The Transition from Lab to Life

    The impact of the computer technology, AI included was felt. No longer was the computertechnology just part of a select few researchers in laboratories. The personal computer made itsdebut along with many technological magazines. Such foundations as the American Association

    for Artificial Intelligence also started. There was also, with the demand for AI development, apush for researchers to join private companies. 150 companies such as DEC which employed itsAI research group of 700 personnel, spend $1 billion on internal AI groups.

    Other fields of AI also made there way into the marketplace during the 1980's. One in particularwas the machine vision field. The work by Minsky and Marr were now the foundation for thecameras and computers on assembly lines, performing quality control. Although crude, thesesystems could distinguish differences shapes in objects using black and white differences. By1985 over a hundred companies offered machine vision systems in the US, and sales totaled $80million.

    The 1980's were not totally good for the AI industry. In 1986-87 the demand in AI systemsdecreased, and the industry lost almost a half of a billion dollars. Companies such asTeknowledge and Intellicorp together lost more than $6 million, about a third of there totalearnings. The large losses convinced many research leaders to cut back funding. Anotherdisappointment was the so called "smart truck" financed by the Defense Advanced ResearchProjects Agency. The projects goal was to develop a robot that could perform many battlefieldtasks. In 1989, due to project setbacks and unlikely success, the Pentagon cut funding for theproject.

    Despite these discouraging events, AI slowly recovered. New technology in Japan was beingdeveloped. Fuzzy logic, first pioneered in the US has the unique ability to make decisions underuncertain conditions. Also neural networks were being reconsidered as possible ways ofachieving Artificial Intelligence. The 1980's introduced to its place in the corporate marketplace,and showed the technology had real life uses, ensuring it would be a key in the 21st century.

    AI put to the Test

    The military put AI based hardware to the test of war during Desert Storm.AI-based technologies were used in missile systems, heads-up-displays, andother advancements. AI has also made the transition to the home. With thepopularity of the AI computer growing, the interest of the public has alsogrown. Applications for the Apple Macintosh and IBM compatiblecomputer, such as voice and character recognition have become available.

    Also AI technology has made steadying camcorders simple using fuzzy logic. With a greaterdemand for AI-related technology, new advancements are becoming available. InevitablyArtificial Intelligence has, and will continue to affecting our lives.

  • 8/12/2019 Is Lecture Notes Unit 01

    6/52

    CSE-304-F Intelligent system By Kaushal Shakya

    6

    History of AI

    1943 McCulloch & Pitts: Boolean circuit model of brain

    1950 Turing's "Computing Machinery and Intelligence"

    1956 Dartmouth meeting: "Artificial Intelligence" adopted

    195269 Look, Ma, no hands!

    1950s Early AI programs, including Samuel's checkersprogram, Newell & Simon's Logic Theorist,Gelernter's Geometry Engine

    1965 Robinson's complete algorithm for logical reasoning 196673 AI discovers computational complexity

    Neural network research almost disappears

    196979 Early development of knowledge-based systems 1980 AI becomes an industry

    1986 Neural networks return to popularity

    1987 AI becomes a science

    1995 The emergence of intelligent agents

  • 8/12/2019 Is Lecture Notes Unit 01

    7/52

    CSE-304-F Intelligent system By Kaushal Shakya

    7

    Lecture 2

    AI Problems and Techniques

    Contents

    1. AI Problems2. AI Techniques

    AI Problems

    The general problem of simulating (or creating) intelligence has been broken down into anumber of specific sub-problems. These consist of particular traits or capabilities thatresearchers would like an intelligent system to display. The traits described below havereceived the most attention.

    Deduction, reasoning, problem solving

    Early AI researchers developed algorithms that imitated the step-by-step reasoning thathumans were often assumed to use when they solve puzzles, play board games or make logicaldeductions. For difficult problems, most of these algorithms can require enormouscomputational resources most experience a "combinatorial explosion": the amount ofmemory or computer time required becomes astronomical when the problem goes beyond acertain size. The search for more efficient problem solving algorithms is a high priority for AIresearch.

    Human beings solve most of their problems using fast, intuitive judgments rather than theconscious, step-by-step deduction that early AI research was able to model. AI has made someprogress at imitating this kind of "sub-symbolic" problem solving: embodied agent approachesemphasize the importance of sensorimotor skills to higher reasoning; neural net researchattempts to simulate the structures inside human and animal brains that give rise to this skill.

    Knowledge representation

    Knowledge representation and knowledge engineering are central to AI research. Many of theproblems machines are expected to solve will require extensive knowledge about the world.Among the things that AI needs to represent are: objects, properties, categories and relations

    between objects; situations, events, states and time; causes and effects; knowledge aboutknowledge (what we know about what other people know); and many other, less wellresearched domains. A complete representation of "what exists" is an ontology (borrowing aword from traditional philosophy), of which the most general are called upper ontologies.Among the most difficult problems in knowledge representations are:

  • 8/12/2019 Is Lecture Notes Unit 01

    8/52

    CSE-304-F Intelligent system By Kaushal Shakya

    8

    Default reasoning and the qualification problem

    Many of the things people know take the form of "working assumptions." For example,if a bird comes up in conversation, people typically picture an animal that is fist sized,sings, and flies. None of these things are true about all birds. John McCarthy identified

    this problem in 1969 as the qualification problem: for any commonsense rule that AIresearchers care to represent, there tend to be a huge number of exceptions. Almostnothing is simply true or false in the way that abstract logic requires. AI research hasexplored a number of solutions to this problem.

    The breadth of commonsense knowledge

    The number of atomic facts that the average person knows is astronomical. Researchprojects that attempt to build a complete knowledge base of commonsense knowledge(e.g., Cyc) require enormous amounts of laborious ontological engineering they mustbe built, by hand, one complicated concept at a time. A major goal is to have the

    computer understand enough concepts to be able to learn by reading from sources likethe internet, and thus be able to add to its own ontology.

    The subsymbolic form of some commonsense knowledge

    Much of what people know is not represented as "facts" or "statements" that they couldexpress verbally. For example, a chess master will avoid a particular chess positionbecause it "feels too exposed" or an art critic can take one look at a statue and instantlyrealize that it is a fake. These are intuitions or tendencies that are represented in thebrain non-consciously and sub-symbolically. Knowledge like this informs, supports andprovides a context for symbolic, conscious knowledge. As with the related problem ofsub-symbolic reasoning, it is hoped that situated AI or computational intelligence willprovide ways to represent this kind of knowledge.

    Planning

    Intelligent agents must be able to set goals and achieve them. They need a way to visualize thefuture (they must have a representation of the state of the world and be able to makepredictions about how their actions will change it) and be able to make choices that maximizethe utility (or "value") of the available choices.

    In classical planning problems, the agent can assume that it is the only thing acting on the worldand it can be certain what the consequences of its actions may be. However, if this is not true, itmust periodically check if the world matches its predictions and it must change its plan as thisbecomes necessary, requiring the agent to reason under uncertainty.

    Multi-agent planning uses the cooperation and competition of many agents to achieve a givengoal. Emergent behavior such as this is used by evolutionary algorithms and swarmintelligence.

  • 8/12/2019 Is Lecture Notes Unit 01

    9/52

    CSE-304-F Intelligent system By Kaushal Shakya

    9

    Learning

    Machine learning has been central to AI research from the beginning. Unsupervised learning isthe ability to find patterns in a stream of input. Supervised learning includes both classificationand numerical regression. Classification is used to determine what category something belongs

    in, after seeing a number of examples of things from several categories. Regression takes a set ofnumerical input/output examples and attempts to discover a continuous function that wouldgenerate the outputs from the inputs. In reinforcement learning the agent is rewarded for goodresponses and punished for bad ones. These can be analyzed in terms of decision theory, usingconcepts like utility. The mathematical analysis of machine learning algorithms and theirperformance is a branch of theoretical computer science known as computational learningtheory.

    Natural language processing

    ASIMO uses sensors and intelligent algorithms to avoid obstacles and navigate stairs.

    Natural language processing gives machines the ability to read and understand the languagesthat humans speak. Many researchers hope that a sufficiently powerful natural languageprocessing system would be able to acquire knowledge on its own, by reading the existing textavailable over the internet. Some straightforward applications of natural language processinginclude information retrieval (or text mining) and machine translation.

    Motion and manipulation

    The field of robotics is closely related to AI. Intelligence is required for robots to be able tohandle such tasks as object manipulation and navigation, with sub-problems of localization(knowing where you are), mapping (learning what is around you) and motion planning.

    Perception

    Machine perception is the ability to use input from sensors (such as cameras, microphones,sonar and others more exotic) to deduce aspects of the world. Computer vision is the ability toanalyze visual input. A few selected subproblems are speech recognition, facial recognition andobject recognition.

    Social intelligence

    Emotion and social skills play two roles for an intelligent agent. First, it must be able to predict

    the actions of others, by understanding their motives and emotional states. (This involveselements of game theory, decision theory, as well as the ability to model human emotions andthe perceptual skills to detect emotions.) Also, for good human-computer interaction, anintelligent machine also needs to display emotions. At the very least it must appear polite andsensitive to the humans it interacts with. At best, it should have normal emotions itself.

  • 8/12/2019 Is Lecture Notes Unit 01

    10/52

    CSE-304-F Intelligent system By Kaushal Shakya

    10

    Creativity

    A sub-field of AI addresses creativity both theoretically (from a philosophical and psychologicalperspective) and practically (via specific implementations of systems that generate outputs thatcan be considered creative, or systems that identify and assess creativity). A related area of

    computational research is Artificial Intuition and Artificial Imagination.

    General intelligence

    Most researchers hope that their work will eventually be incorporated into a machine withgeneral intelligence (known as strong AI), combining all the skills above and exceeding humanabilities at most or all of them. Many of the problems above are considered AI-complete: tosolve one problem, you must solve them all. For example, even a straightforward, specific tasklike machine translation requires that the machine follow the author's argument (reason), knowwhat is being talked about (knowledge), and faithfully reproduce the author's intention (socialintelligence). Machine translation, therefore, is believed to be AI-complete: it may require strongAI to be done as well as humans can do it.

    AI Techniques

    Cybernetics and brain simulation

    There is no consensus on how closely the brain should be simulated. In the 1940s and 1950s, anumber of researchers explored the connection between neurology, information theory, andcybernetics. Some of them built machines that used electronic networks to exhibit rudimentaryintelligence.

    Symbolic

    When access to digital computers became possible in the middle 1950s, AI research began toexplore the possibility that human intelligence could be reduced to symbol manipulation.

    Cognitive simulation Logic based "Anti-logic" or "scruffy" Knowledge based

    Computational Intelligence

    Interest in neural networks and "connectionism" was revived by David Rumelhart and others inthe middle 1980s. These and other sub-symbolic approaches, such as fuzzy systems andevolutionary computation, are now studied collectively by the emerging discipline ofcomputational intelligence.

  • 8/12/2019 Is Lecture Notes Unit 01

    11/52

    CSE-304-F Intelligent system By Kaushal Shakya

    11

    Agent architectures and cognitive architectures

    Researchers have designed systems to build intelligent systems out of interacting intelligentagents in a multi-agent system. A system with both symbolic and sub-symbolic components is ahybrid intelligent system, and the study of such systems is artificial intelligence systems

    integration. A hierarchical control system provides a bridge between sub-symbolic AI at itslowest, reactive levels and traditional symbolic AI at its highest levels, where relaxed timeconstraints permit planning and world modelling. Rodney Brooks' subsumption architecturewas an early proposal for such a hierarchical system.

  • 8/12/2019 Is Lecture Notes Unit 01

    12/52

    CSE-304-F Intelligent system By Kaushal Shakya

    12

    Lecture 3 and 4

    AI Programming Languages: Introduction to LISP

    Contents

    1. Introduction to AI Languages2. LISP

    Introduction to AI Languages

    Programming languages in artificial intelligence (AI) are the major tool for exploring andbuilding computer programs that can be used to simulate intelligent processes such as learning,reasoning and understanding symbolic information in context. Although in the early days ofcomputer language design the primarily use of computers was for performing calculations withnumbers, it was also found out quite soon that strings of bits could represent not only numbers

    but also features of arbitrary objects. Operations on such features or symbols could be used torepresent rules for creating, relating or manipulating symbols. This led to the notion of symboliccomputation as an appropriate means for defining algorithms that processed information of anytype, and thus could be used for simulating human intelligence. Soon it turned out thatprogramming with symbols required a higher level of abstraction than was possible with thoseprogramming languages which were designed especially for number processing, e.g., Fortran.

    In AI, the automation or programming of all aspects of human cognition is considered from itsfoundations in cognitive science through approaches to symbolic and sub-symbolic AI, naturallanguage processing, computer vision, and evolutionary or adaptive systems. It is inherent tothis very complex problem domain that in the initial phase of programming a specific AI

    problem, it can only be specified poorly. Only through interactive and incremental refinementdoes more precise specification become possible. This is also due to the fact that typical AIproblems tend to be very domain specific, therefore heuristic strategies have to be developedempirically through generateandtest approaches (also known as rapid prototyping). In thisway, AI programming notably differs from standard software engineering approaches whereprogramming usually starts from a detailed formal specification. In AI programming, theimplementation effort is actually part of the problem specification process. Due to the fuzzynature of many AI problems, AI programming benefits considerably if the programminglanguage frees the AI programmer from the constraints of too many technical constructions(e.g., low-level construction of new data types, manual allocation of memory). Rather, adeclarative programming style is more convenient using built-in high-level data structures (e.g.,

    lists or trees) and operations (e.g., pattern matching) so that symbolic computation is supportedon a much more abstract level than would be possible with standard imperative languages,such as Fortran, Pascal or C. Of course, this sort of abstraction does not come for free, sincecompilation of AI programs on standard von Neumann computers cannot be done as efficientlyas for imperative languages. However, once a certain AI problem is understood (at least

  • 8/12/2019 Is Lecture Notes Unit 01

    13/52

    CSE-304-F Intelligent system By Kaushal Shakya

    13

    partially), it is possible to reformulate it in form of detailed specifications as the basis forreimplementation using an imperative language.From the requirements of symbolic computation and AI programming, two new basicprogramming paradigms emerged as alternatives to the imperative style: the functional and thelogical programming style. Both are based on mathematical formalisms, namely recursive

    function theory and formal logic.

    The first practical and still most widely used AI programming language is the functionallanguage Lisp developed by John McCarthy in the late 1950s. Lisp is based on mathematicalfunction theory and the lambda abstraction. A number of important and influential AIapplications have been written in Lisp so we will describe this programming language in somedetail in this article. During the early 1970s, a new programming paradigm appeared, namelylogic programming on the basis of predicate calculus.

    Another most important logic programming language is Prolog, developed by AlainColmerauer, Robert Kowalski and Phillippe Roussel. Problems in Prolog are stated as facts,axioms and logical rules for deducing new facts. Prolog is mathematically founded on predicatecalculus and the theoretical results obtained in the area of automatic theorem proving in the late1960s.

    LISP: LIST Processing language

    LISP is an AI language developed in 1958 (J. McCarthy at MIT) Its special focus is on symbolicprocessing and symbol manipulation. It uses: Linked list structures Also programs, functions are represented as listsAt one point special LISP computers with basic LISP functions implemented directly onhardware were available (Symbolics Inc., 80s)

    LISP today: Many AI programs now are written in C,C++, Java

    List manipulation libraries are available

    LISP Competitors: Prolog, Python but LISP keeps its dominance among high level (AI) programming languages

    Current LISP: Common Lisp

    Schemeare the most widely-known general-purpose Lisp dialects

    Common LISP: Interpreter and compiler

    CLOS: object oriented programming

  • 8/12/2019 Is Lecture Notes Unit 01

    14/52

    CSE-304-F Intelligent system By Kaushal Shakya

    14

    Syntax: Prefix notation Operator first, arguments follow E.g. (+ 3 2) adds 3 and 2

    A lot of parentheses are used which define lists and also programsExamples: (a b c d) is a list of 4 elements (atoms) a,b,c,d (defun factorial (num)(cond (( (setq a 10) ;;sets a value of symbol a to 1010

    > a ;; returns the value of a10Special symbols:> t ;; trueT> nil ;; nil stands for false orNIL> ( ) ;; an empty listNIL

    Lists represent function calls as well as basic data structures

    > (factorial 3)6> (+ 2 4)6> (setq a (john peter 34)) ;; quote means: do not eval the argument(john peter 34)

  • 8/12/2019 Is Lecture Notes Unit 01

    15/52

    CSE-304-F Intelligent system By Kaushal Shakya

    15

    > (setq a ((john 1) (peter 2)))((john 1) (peter 2))

    List representation: A singly linked list

    > (setq a (john peter))(john peter)> (car a)

    john> (cdr a)(peter)

    List building functions> (cons b nil) ;; quote means: do not eval the argument(b)> (setq a (cons b (cons c nil)) ;; setq a is a shorthand for set a(b c)> (setq v (list john 34 25))(john 34 25)> (setq v (list a 34 25))((b c) 34 25)> (append (1 2) (2 3))(1 2 2 3)

    List copying> (setq foo (list 'a 'b 'c))(a b c)> (setq bar (cons 'x (cdr foo)))

    (x b c)> foo(a b c) ;; (cdr foo) makes a copy of the remaining list beforecons> bar(x b c) Car and cdr operations are nondestructive.> (setq bar (a b c))(a b c)> (setq foo (cdr bar))(b c)

    > (rplaca foo u) ;; replaces car component of foo (destructive op)(u c)> foo(u c)> bar(a u c)

  • 8/12/2019 Is Lecture Notes Unit 01

    16/52

    CSE-304-F Intelligent system By Kaushal Shakya

    16

    > (rplacd foo (v)) ;; replaces cdr component of foo (destructive)(u v)> bar(a u v)

    The same effect as with rplaca and rplacd can be achievedwith setf> (setq bar (a b c))(a b c)> (setq foo (cdr bar))(b c)> (setf (cadr bar) u)u> bar(a u c)> foo(u c)

    Evaluation rules: A symbol value is sought and substituted A quoted value is kept untouched

    > (setq a 12)12> (setq b (+ a 4))16> (setq b (+ a 4))(+ a 4)> (eval b) ;; explicit evaluation call

    16

    Some useful functions and predicates:> (setq a (1 2 3 4 5))(1 2 3 4 5)> (length a) ;; gives the list length of the argument5> (atom a) ;; checks if the argument is an atomT> (atom a)NIL

    > (listp a) ;; checks if the argument is a listNIL> (listp a)T

    Definition of a function

  • 8/12/2019 Is Lecture Notes Unit 01

    17/52

    CSE-304-F Intelligent system By Kaushal Shakya

    17

    (defun )>(defun square (x)(* x x))SQUARE>(square 2)

    4>(square (square 2))16

    Definition of a function(defun ) can be a sequence of function calls, the function returnsthe value of the last call in the sequence> (defun foo (a)(setq b (+ a 1))(setq c (+ a 2))c)FOO> (foo 2)4

    Cond statement:It sequentially tests conditions; the call associated with the first true condition is executed

    > (defun abs (a)(cond ((> a 0) a)(t (- a))))ABS> (abs 2)

    2> (abs -3)3

    if statement:(if )> (defun abs (a)(if (> a 0) a (- a)))ABS> (abs 2)2

    > (abs -3)3

    4 equality predicates: =, equal, eq, eql> (= 2 4/2) ;; used for numerical values onlyT

  • 8/12/2019 Is Lecture Notes Unit 01

    18/52

    CSE-304-F Intelligent system By Kaushal Shakya

    18

    > (setf a '(1 2 3 4))(1 2 3 4)>(setf b '(1 2 3 4))(1 2 3 4)>(setf c b)

    (1 2 3 4)> (equal a b) ;; equal is true if the two objects are isomorphicT> (equal c b)T

    >(eq a b) ;; eq is true if the two arguments point to thesame objectNIL>(eq b c)T

  • 8/12/2019 Is Lecture Notes Unit 01

    19/52

    CSE-304-F Intelligent system By Kaushal Shakya

    19

    Lecture 4, 5 and 6

    PROLOGContents

    1. Introduction2. PROPLOG Program3. Relations versus Functions4. Facts and Rules5. Goals6. More on Facts7. More on rules8. Unification9. Lists10. Tree

    11. Order in the rules12. Variables in terms13. Backtracking

    Introduction

    Prolog was invented in the early seventies at the University of Marseille. Prolog stands for

    PROgramming in LOGic. It is a logic language that is particularly used by programs that use

    non-numeric objects. For this reason it is a frequently used language in Artificial Intelligence

    where manipulation of symbols is a common task. Prolog differs from the most common

    programming languages because it is declarative language. Traditional programminglanguages are said to be procedural. This means that the programmer specify how to solve a

    problem. In declarative languages the programmers only give the problem and the language

    find himself how to solve the problem. Although it can be, and often is, used by itself, Prolog

    complements traditional languages when used together in the same application.

    Prolog programProgramming in Prolog is very different from programming in a traditional procedural

    language like Pascal. In Prolog you don't say how the program will work.

    Prolog can be separated in two parts :

    The Program

    The program, sometimes called Database is a texte file (*.pl) that contain the facts and rules that

    will be used by the user of the program. It contains all the relations that make this program.

  • 8/12/2019 Is Lecture Notes Unit 01

    20/52

    CSE-304-F Intelligent system By Kaushal Shakya

    20

    The Query

    When you launch a program you are in query modequery mode. This mode is represented by

    the sign ? - at the begining of the line. In query mode you ask questions about relations

    described in the program.

    Loading a program

    For loading First you have to launch your Prolog compiler, for this report we used the SWI-Prolog which is a freeware (if you want to know more about SWI-Prolog go to this page). WhenProlog is launched the ?- should appear meaning you are in query mode. The manner to launcha program depends of your compiler. For SWI-Prolog you can load a program by typing thecommand [file]. when the file of your program is file.pl. If you compiler is not SWI-Prolog youcan also try the command reconsult(file). When you have done this you can use all the facts andrules that are contained in the program. Now let's begin to see what a fact...is.

    Relations versus Functions

    In logic programming we talk about relations rather than functions.

    Consider the append function:

    - fun append (nil, x) = x| append (hd::tl, x) = hd::append(tl, x);

    - append([1, 2], [3, 4]);val it = [1, 2, 3, 4] : int list

    But what if we want to find the result to the following query?

    - append ([1, 2], x) = [1, 2, 3, 4]The desired result would be [3, 4] ; we want the system to fill in the missing information. MLwon't know that we want to find the result of x in this case. We would have to write anotherfunction that dealt with this particular query.

    Another way to look at this concept involves the following definition:- append relation(X, Y, Z):[] [1] [1][1] [] [1][1, 2] [3] [1, 2, 3]. . .. . .

    Where the relation is defined on an infinite set of tuples.

  • 8/12/2019 Is Lecture Notes Unit 01

    21/52

    CSE-304-F Intelligent system By Kaushal Shakya

    21

    We could then ask a number of questions to see if the desired result exists:

    append([], [], [])? yes

    append([], [1], [1])? yes

    append([1,2], [3], [1,2,3])? yesappend([], [], [])? yes

    What if we ask the following?

    append(x, [1], [1])?append([1], x, [1])?append([1], [], x)?

    In the first case, X is an output where Y and Z are inputs. In the second case, Y is an outputwhere X and Z are inputs. In the third case, Z is the output while Xand Y are inputs.

    These all ask a similar question: is there an x such that the given pattern is in therelation append ? The output would be a possible result for x, if one existed. Subsequent querieswould return other possible answers until all are exhausted.

    The key to logic programming is you do not need to specify exactly howa result is computed.You simply need to describe the form of the result. It is assumed that the computer system cansomehow determine how the result is computed.

    With this type of language, it is necessary to provide both the relevant information and amethod of inference for computing the results.

    Arguments and results are treated uniformly. This means there is no distinction between inputand output.

    It is also important to note that Prolog is NOT a typed language.

    Facts and Rules

    Logic programming describes information in terms of facts and rules.

    Fact

    A fact is analogous to a base case in a proof. It is a piece of information that is known and is aspecial case of a rule. A fact holds without any conditions, i.e. they are propositions that areassumed to be true. Facts are also known as unconditional Horn Clauses. The general case of aHorn clause is:

    P if Q1 and Q2 . . . and Qn

  • 8/12/2019 Is Lecture Notes Unit 01

    22/52

    CSE-304-F Intelligent system By Kaushal Shakya

    22

    Where a fact is simply P with no conditional statements Q1, Q2 . . . . Note that horn clausesCANNOT represent negative information. That is, you cannot ask if a tuple is NOT in arelation.

    Rule

    A rule describes a series of conditions followed by a conclusion returned if the conditions aremet. A rule is analogous to inductive steps in proofs.

    Rules are also known as conditional Horn clauses.

    Some Prolog examples:

    Fact: append([], X, X).

    This says that if you append [] and something, you will obtain something.

    Rule: append([a, b], [c, d], [a, b, c, d]) if append([b], [c, d], [b, c, d]).

    In Prolog, capital letters are variables and lower case letters are atoms. A "." (period) is used toend a statement. Another notation convention is described by: [a, b] = [a | [b]] wherethe | operator is equivalent to cons (i.e. it separates the head and tail of a list).

    The above rule says: if the condition is met, return the appended result of the given lists.

    Goals

    A goal is simply a question we want answered by the system based on the knowledge in thedatabase of information. Creating facts and rules builds our database of information. We canthen state goals and get results back based on the database.

    append([], X, X).

  • 8/12/2019 Is Lecture Notes Unit 01

    23/52

    CSE-304-F Intelligent system By Kaushal Shakya

    23

    ?- append([a, b], [c, d], Z).Z = [a, b, c, d] ?yes

    We query the system and it returns the first result it comes across in the relation that satisfiesthe value of Z.

    The question mark after the result is the system asking if we want to search for more answers toour goal. Pressing enter means that we don't want to search for more answers. The system willrespond that "yes" or "no", there are more answers that exist.

    Similar to the above query:

    ?- append(X, [c, d], [a, b, c, d]).X = [a, b] ?yes

    ?- append([a, b], Y, [a, b, c, d]).Y = [c, d] ?yes

    After obtaining an answer to a goal, if the user responds to the "?" prompt with a semi-colon,the next answer will be returned if any other answer exists. A "no" will be returned if allpossibilities have been exhausted.

    A fact and a rule:append([], X, X).append([H | X], Y, [H | Z]) :- append(X, Y, Z).

    Goals:

    | ?- append([a], [b, c, d], X).X = [a, b, c, d] ? ;

  • 8/12/2019 Is Lecture Notes Unit 01

    24/52

    CSE-304-F Intelligent system By Kaushal Shakya

    24

    X = [a, b, c d],Y = [] ? ;no

  • 8/12/2019 Is Lecture Notes Unit 01

    25/52

    CSE-304-F Intelligent system By Kaushal Shakya

    25

    Our interpretation of the father relation father(X, Y) is that X is a father of Y .

    So, when we query the system if john is the father of mary , the answer we get back is yes .Indeed, that fact is in our information database because of the file we loaded.

    Now, we want to know if sam has any children:

    | ?- father(sam, X).X = john ? ;

  • 8/12/2019 Is Lecture Notes Unit 01

    26/52

    CSE-304-F Intelligent system By Kaushal Shakya

    26

    We enter a semi-colon which tells the system we want to look for another possible solution thatsatisfies our goal.

    The system continues tracing through the file attempting to find a match to the goal father(sam,Y) . Another match is found at father(sam, kathy) . The goal has been satisfied again and the

    result X = sam, Y = kathy is returned.

    When prompted for another possibility, the system continues searching for a match to thesecond sub-goal. There is no more information left in the database at this point. However, thepointer to the first sub-goal is still at father(sam, john) . We haven't exhausted all possibilitiesfor the first sub-goal yet.

    Therefore, we continue searching through the database looking for a match to the goal father(X,john) . None is found, so we are done. "No" is returned.

    If another match had been found for the first sub-goal, we would look at the second sub-goal

    again starting from the beginning of the database.

    Following from the same example above, what happens if we do the following:

    | ?- father(X, john), father(X, Y), Y \== john.X = sam,Y = kathy ? ;no

    The operator \== tells the system to ignore any matches where Y equals john . Thus, weeliminate the first solution we had in the previous example.

    Another example:

    father(john, mary).father(sam, john).father(sam, kathy).

    | ?- father(X, Y), father(X, Z).

    X = john,Y = mary,Z = mary, ? ;

    X = sam,Y = john,Z = john ? ;

    X = sam,

  • 8/12/2019 Is Lecture Notes Unit 01

    27/52

    CSE-304-F Intelligent system By Kaushal Shakya

    27

    Y = john,Z = kathy ? ;

    X = sam,Y = kathy,

    Z = kathy ? ;no

    More on Rules

    A rule is defined in the following way:

    :- , , . . . . . , ^ ^| |HEAD CONDITIONS

    The head is the conclusion and the conditions are considered to be "and"-ed together. Note: Afact is just a special rule with no conditions.Example:

    father(john, mary).

  • 8/12/2019 Is Lecture Notes Unit 01

    28/52

    CSE-304-F Intelligent system By Kaushal Shakya

    28

    Unification is similar to pattern matching. However, there is a difference because patternmatching can only happen one way, from left to right. Unification can match both ways; itdepends on where the variables and the atoms are.

    | ?- X is 2+3.X = 5 ?X is instantiated to the value 5. The expression 2+3 is evaluated because we are using theoperator "is" which tells us to evaluate 2+3 and make X that value.

    To unify, we use the operator =.

    | ?- X = 2 + 3. (2+3=X) +X = 2 + 3 ? / \

    2 3The diagram on the right is the term that X is unified with.

    | ?- 5 = 2 + 3.no

    This goal is NOT unifiable.

    | ?- 2 + 3 = 2 + Y. + +Y = 3 ? / \ / \

    2 3 2 YThis goal is unifiable. We are increasing the information of Y to equal 3.

    | ?- f(X, b) = f(a, Y). + +X = a, / \ / \Y = b ? X b a Y

    This goal is unifiable because we can match X to a and Y to b .

    Another example:

    | ?- 2*X = Y*(3+Y). * *X = 3 + 2, / \ / \Y = 2 ? 2 X Y +

    / \3 Y

    The Y on the left is unified with the 2 on the right. The X on the left is the unified with (3 + Y) onthe right which is actually (3 + 2) after the unification of Y with 2.

    Infinite loop examples:

    | ?- X = X + 2.

  • 8/12/2019 Is Lecture Notes Unit 01

    29/52

    CSE-304-F Intelligent system By Kaushal Shakya

    29

    X =Prolog interruption (h for help)? a{Execution aborted}

    What happened? The X on the left is infinitely matched to X + 2 on the right.

    | ?- X = 2 + XX = 2+(2+(2+(2+(2+(2+(2+(2+(2+(2+(2+(2+(2+(2+(Prolog interruption (h for help)? a{Execution aborted}

    What happened? The same thing happened as above, except it knew what to do with eachinstance of 2+( that it found. The problem was that the unification for Xstill went on infinitely.

    Other unification examples:

    | ?- X is 2 + 3, X = 5.X = 5 ?

    | ?- X is 2 + 3, X = 2 + 3.no

    | ?- 2 + 3 is X.ERROR.

    | ?- X is 5, 6 is X + 1.X = 5 ?

    | ?- X = 5, 6 = X + 1.no

    | ?- 6 is X + 1, X = 5.ERROR.

    | ?- Y = 2, 2*X is Y*(Y+3).no

    | ?- Y = 2, Z is Y*(Y+3).Y = 2,Z = 10 ?

    Lists

    Notation:

    [a, b, c] = [a, b, c | []] = [a | [b, c]]This simply shows how lists are constructed with a head and a tail.

  • 8/12/2019 Is Lecture Notes Unit 01

    30/52

    CSE-304-F Intelligent system By Kaushal Shakya

    30

    Here, we try to unify two lists:

    | ?- [H | T] = [a, b, c].H = a,

    T = [b, c] ?

    | ?- [a | T] = [H, b, c].H = a,T = [b, c] ?

    The head and tail is matched and the values are output.

    Trees

    Here is a definition for a binary tree in ML:

    - datatype bintree = empty | node of int * bintree * bintree;

    - fun member(k, empty) = false| member(k, node(n, s, t)) =

    if k < n then member(k, s)else if k < n then member(k, t)else true;

    How might you define a binary tree in Prolog?

    member(K, node(K, _ , _ )).

  • 8/12/2019 Is Lecture Notes Unit 01

    31/52

    CSE-304-F Intelligent system By Kaushal Shakya

    31

    member(M, [M | _]).member(M, [_ | T]) :- member(M, T).

    The fact looks at the next item in the list to see if it exists. The rule checks to see if the item M isin the rest of the list, i.e. T.

    What does the following return?

    | ?- member(a, [a]).yes

    This matches to the fact. What about this:

    | ?- member(a, [b])no

    This fails at the fact because the first item in the list is b not a . The condition in the rule also failsbecause the tail of the list [b] is just b which won't satisfy the fact. Thus, there is no match in therelation.

    Order in the rules

    The order of rules and goals is important to the end result. Consider the following examples:

    overlap(X,Y) :- member(M, X), member(M, Y).

    | ?- overlap(Z, [a, b, c, d]), member(Z, [1, 2, c, d]).(infinite computation)

    | ?- X = [1, 2, 3], member(a, X).no

    | ?- member(a, X), X = [1, 2, 3].(infinite computation)

    The most interesting thing to note here is the difference between the answers of the second andthird queries.

    The second query simply answers "no", but the third query never returns. Why does thishappen? The best way to examine what happens is to look at the associated relation tree whichshows the structure of the possible solutions:

    member(a,X)/ \

    one ------> X=[a|_] X=[_|T]

  • 8/12/2019 Is Lecture Notes Unit 01

    32/52

    CSE-304-F Intelligent system By Kaushal Shakya

    32

    T=[a|_] T=[_|T']X=[_,a,_] ?member(a,T')

    / \T'=[a|_] T'=[_|T'']

    X=[_,_,a,_] ?member(a,T''')

    When we know X , as in the second query, we can compare a to each element in the list. Nomatches occur, so "no" is returned.

    When we don't know X , as in the third query, the system keeps looking for elements in X tocompare to a . . .it never finds any, but it keeps searching indefinitely.

    In essence, it constructs the equivalent of a search tree and traverses it until if finds somethingthat satisfies the goal.

    Notice how the order of the expressions in the goal made a difference in how the result wasdeduced.

    There is a design issue when considering the ordering of facts and rules. Traversing relationtrees could be implemented either in a depth-first manner or a breadth-first manner.

    Prolog uses depth-first "traversal" because each sub-goal must be satisfied before anysubsequent sub-goals are satisfied. The ideal method would use breadth-first "traversal" whereeach sub-goal is examined in parallel.

    However, breadth-first requires a large amount of memory and resources. This is why depth-first was used in Prolog. Unfortunately, this method does have its drawbacks as demonstratedby the following:

    f(X) :- f(X). ?f(1)f(1). / \

    f(1) yes

  • 8/12/2019 Is Lecture Notes Unit 01

    33/52

    CSE-304-F Intelligent system By Kaushal Shakya

    33

    Variables in terms allow you to increase information using unification.

    | ?- L = [a, b | X].L = [a, b | X] ? ;no

    | ?- L = [a, b | X], X = [C, Y].L = [a, b, C, Y],X = [C, Y] ? ;no

    Notice how the information about L is increased by unifying X to the list [C, Y] .

    Unification of a variable representing the end of the list is similar to an assignment to thatvariable.

    Backtracking

    Backtracking simply refers to reconsidering sub-goals that were previously proven/satisfied. Anew solution is found by beginning the search where the previous search for that sub-goalstopped.

    Considering the possibility tree, backtracking refers to retracing you steps as you followbranches back up to previously untraversed branches.

    A similar example to the example in the previous section:

    f(x) :- fail ?f(1)f(1) / \fail yes

    Steps followed:

    1. Trace from f(1) to fail.2. Trace back from fail to f(1). This is the backtracking step.3. Trace from f(1) to yes.

  • 8/12/2019 Is Lecture Notes Unit 01

    34/52

    CSE-304-F Intelligent system By Kaushal Shakya

    34

    Lecture 8, 9 and 10

    Problem Spaces and Searches

    Contents

    1. Formal Description of a Problem2. The Monkey & Bananas Problem3. Missionaries and Cannibals4. Graphs/Trees5. 8 Puzzle Problem6. Search7. Consequences of Search8. Forward vs Backward Search9. Depth-First Search

    10. Breadth-First Search

    Formal Description of a Problem

    In AI, we will formally define a problem as

    a space of all possible configurations where each configuration is called a state thus, weuse the term state space

    an initial state one or more goal states a set of rules/operators which move the problem from one state to the next

    In some cases, we may enumerate all possible states (see monkey & banana problem on the nextslide) but usually, such an enumeration will be overwhelmingly large so we only generate aportion of the state space, the portion we are currently examining

    The Monkey & Bananas Problem

    A monkey is in a cage and bananas are suspended from the ceiling, the monkey wants to eat abanana but cannot reach them

    in the room are a chair and a stick if the monkey stands on the chair and waves the stick, he can knock a banana down to

    eat it what are the actions the monkey should take?

  • 8/12/2019 Is Lecture Notes Unit 01

    35/52

    CSE-304-F Intelligent system By Kaushal Shakya

    35

    Initial state:

    Monkey on ground with empty hand bananas suspended

    Goal state:

    Monkey eating

    Actions:

    climb chair/get off

    grab X

    wave X

    eat X

    Missionaries and Cannibals

    3 missionaries and 3 cannibals are on one side of the river with a boat that can takeexactly 2 people across the river

    o how can we move the 3 missionaries and 3 cannibals across the rivero with the constraint that the cannibals never outnumber the missionaries on either

    side of the river (lest the cannibals start eating the missionaries!)?? We can represent a state as a 6-item tuple:(a, b, c, d, e, f)

    a/b = number of missionaries/cannibals on left shore c/d = number of missionaries/cannibals in boat

  • 8/12/2019 Is Lecture Notes Unit 01

    36/52

    CSE-304-F Intelligent system By Kaushal Shakya

    36

    e/f = number of missionaries/cannibals on right shore where a + b + c + d + e + f = 6 and a >= b unless a = 0, c >= d unless c = 0, and e >= f unless e = 0

    Legal operations (moves) areo 0, 1, 2 missionaries get into boato

    0, 1, 2 missionaries get out of boat

    o 0, 1, 2 cannibals get into boato 0, 1, 2 missionaries get out of boato boat sails from left shore to right shoreo boat sails from right shore to left shore

    drawing the state space will be left as a homework problemGraphs/Trees

    We often visualize a state space (or a search space) as a graph. A tree is a special form of graphwhere every node has 1 parent and 0 to many children, in a graph, there is no parent/childrelationship implied. Some problems will use trees, others can use graphs.

    Here is an example of representing a situation as a graph. On the top is the city of Konigsbergwhere there are 2 shores, 2 islands and 7 bridges. The graph below shows the connectivity. Thequestion asked in this problem was: is there a single path that takes you to both shores andislands and covers every bridge exactly once?

  • 8/12/2019 Is Lecture Notes Unit 01

    37/52

    CSE-304-F Intelligent system By Kaushal Shakya

    37

    By representing the problem as a graph, it is easier to solve the answer by the way is no, thegraph has four nodes whose degree is an odd number, the problem, finding an Euler path, isonly solvable if a graph has exactly 0 or 2 nodes whose degrees are odd.

    8 Puzzle Problem

    The 8 puzzle search space consists of 8! states (40320).

    Problem Characteristics

    o Is the problem decomposable?o if yes, the problem becomes simpler to solve because each lesser problem can be

    tackled and the solutions combined together at the endo Can solution steps be undone or ignored?

    o a game for instance often does not allow for steps to be undone (can you takeback a chess move?)

    o Is the problems universe predictable?o will applying the action result in the state we expect? for instance, in the monkey

    and banana problem, waving the stick on a chair does not guarantee that abanana will fall to the ground!

    o Is a good solution absolute or relative?o for instance, do we care how many steps it took to get there?

    o Is the desired solution a state or a path?o is the problem solved by knowing the steps, or reaching the goal?

  • 8/12/2019 Is Lecture Notes Unit 01

    38/52

    CSE-304-F Intelligent system By Kaushal Shakya

    38

    o Is a large amount of knowledge absolutely required?o Is problem solving interactive?

    Search

    Given a problem expressed as a state space (whether explicitly or implicitly) with

    operators/actions, an initial state and a goal state, how do we find the sequence of operatorsneeded to solve the problem? This requires search.

    Formally, we define a search space as [N, A, S, GD]

    N = set of nodes or states of a graph

    A = set of arcs (edges) between nodes that correspond to the steps in the problem(the legal actions or operators)

    S = a nonempty subset of N that represents start states

    GD = a nonempty subset of N that represents goal states

    Our problem becomes one of traversing the graph from a node in S to a node in GD. We can useany of the numerous graph traversal techniques for this but in general, they divide into twocategories:

    o brute force unguided searcho heuristic guided search

    Consequences of Search

    The 8-puzzle has over 40000 different states. What about the 15 puzzle? A brute force search

    means trying all possible states blindly until you find the solution for a state space for aproblem requiring n moves where each move consists of m choices, there are 2m*n possiblestates. Two forms of brute force search are: depthfirst search, breath first search

    A guided search examines a state and uses some heuristic (usually a function) to determine howgood that state is (how close you might be to a solution) to help determine what state to move to

    o hill climbingo best-first searcho A/A* algorithmo Min Max

    While a good heuristic can reduce the complexity from 2m*n

    to something tractable, there is noguarantee so any form of search is O(2n) in the worst case.

    Forward vs Backward Search

    The common form of reasoning starts with data and leads to conclusions. For instance,diagnosis is data-driven given the patient symptoms, we work toward disease hypotheses. We

  • 8/12/2019 Is Lecture Notes Unit 01

    39/52

    CSE-304-F Intelligent system By Kaushal Shakya

    39

    often think of this form of reasoning as forward chaining through rules. Backward searchreasons from goals to actions. Planning and design are often goal-driven--backward chaining.

    Depth-first Search

    Starting at node A, our search gives us:

    A, B, E, K, S, L, T, F, M, C, G, N, H, O, P, U, D, I, Q, J, R

  • 8/12/2019 Is Lecture Notes Unit 01

    40/52

    CSE-304-F Intelligent system By Kaushal Shakya

    40

    Example:

    Traveling Salesman Problem

  • 8/12/2019 Is Lecture Notes Unit 01

    41/52

    CSE-304-F Intelligent system By Kaushal Shakya

    41

    Breadth-First Search

    Starting at node A, our search would generate the nodes in alphabetical order from A to U.

    Example

  • 8/12/2019 Is Lecture Notes Unit 01

    42/52

    CSE-304-F Intelligent system By Kaushal Shakya

    42

    Lecture 11, 12 and 13

    Heuristic search techniques

    Contents

    1. Definition of Heuristic2. Heuristic search3. Hill Climbing4. Simulated annealing5. Best First Search6. TheA* Algorithm7. AND-OR Graphs8. The AO* Algorithm

    Definition

    A Heuristic is an operationally-effective nugget of information on how to direct search in aproblem space. Heuristics are only approximately correct. Their purpose is to minimize searchon average.

    Heuristic Search

    A heuristicis a method that

    might not always find the best solution butis guaranteed to find a good solution in reasonable time.

    By sacrificing completeness it increases efficiency. Useful in solving tough problems which

    o could not be solved any other way.o solutions take an infinite time or very long time to compute.

    The classicexample of heuristic search methods is the travelling salesman problem.

    Heuristic Search methodsGenerate and Test Algorithm

    1. generate a possible solution which can either be a point in the problem space or a pathfrom the initial state.

    2. test to see if this possible solution is a real solution by comparing the state reached withthe set of goal states.

    3. if it is a real solution, return. Otherwise repeat from 1.

    This method is basically a depth first search as complete solutions must be created beforetesting. It is often called the British Museum method as it is like looking for an exhibit at

  • 8/12/2019 Is Lecture Notes Unit 01

    43/52

    CSE-304-F Intelligent system By Kaushal Shakya

    43

    random. A heuristic is needed to sharpen up the search. Consider the problem of four 6-sidedcubes, and each side of the cube is painted in one of four colours. The four cubes are placed nextto one another and the problem lies in arranging them so that the four available colours aredisplayed whichever way the 4 cubes are viewed. The problem can only be solved if there are atleast four sides coloured in each colour and the number of options tested can be reduced using

    heuristics if the most popular colour is hidden by the adjacent cube.

    Hill climbing

    Here the generate and test method is augmented by an heuristic function which measures thecloseness of the current state to the goal state.

    1. Evaluate the initial state if it is goal state quit otherwise current state is initial state.2. Select a new operator for this state and generate a new state.3. Evaluate the new state

    o if it is closer to goal state than current state make it current stateo

    if it is no better ignore4. If the current state is goal state or no new operators available, quit. Otherwise repeatfrom 2.

    In the case of the four cubes a suitable heuristic is the sum of the number of different colours oneach of the four sides, and the goal state is 16 four on each side. The set of rules is simply choosea cube and rotate the cube through 90 degrees. The starting arrangement can either be specifiedor is at random.

    Simulated Annealing

    This is a variation on hill climbing and the idea is to include a general survey of the scene toavoid climbing false foot hills.

    The whole space is explored initially and this avoids the danger of being caught on a plateau orridge and makes the procedure less sensitive to the starting point. There are two additionalchanges; we go for minimization rather than creating maxima and we use the term objectivefunction rather than heuristic. It becomes clear that we are valley descending rather than hillclimbing. The title comes from the metallurgical process of heating metals and then letting themcool until they reach a minimal energy steady final state. The probability that the metal will

    jump to a higher energy level is given by where kis Boltzmann's constant. The

    rate at which the system is cooled is called the annealing schedule. is called the change inthe value of the objective function and kTis called Ta type of temperature. An example of aproblem suitable for such an algorithm is the travelling salesman. The SIMULATEDANNEALING algorithm is based upon the physical process which occurs in metallurgy wheremetals are heated to high temperatures and are then cooled. The rate of cooling clearly affectsthe finished product. If the rate of cooling is fast, such as when the metal is quenched in a large

  • 8/12/2019 Is Lecture Notes Unit 01

    44/52

    CSE-304-F Intelligent system By Kaushal Shakya

    44

    tank of water the structure at high temperatures persists at low temperature and large crystalstructures exist, which in this case is equivalent to a local maximum. On the other hand if therate of cooling is slow as in an air based method then a more uniform crystalline structure existsequivalent to a global maximum.The probability of making a large uphill move is lower than asmall move and the probability of making large moves decreases with temperature. Downward

    moves are allowed at any time.

    1. Evaluate the initial state.2. If it is goal state Then quit otherwise make the current state this initial state and proceed.3. Make variable BEST_STATEto current state4. Set temperature, T, according to the annealing schedule5. Repeat

    -- difference between the values of current and new states

    1. If this new state is goal state Then quit

    2. Otherwise compare with the current state3. If better set BEST_STATEto the value of this state and make the current the newstate

    4. If it is not better then make it the current state with probabilityp'. This involvesgenerating a random number in the range 0 to 1 and comparing it with a half, if itis less than a half do nothing and if it is greater than a half accept this state as thenext current be a half.

    5. Revise T in the annealing schedule dependent on number of nodes in tree

    Until a solution is found or no more new operators

    6.

    Return BEST_STATEas the answer

    Best First Search

    It is a combination of depth first and breadth first searches.

    Depth first is good because a solution can be found without computing all nodes and breadthfirst is good because it does not get trapped in dead ends. The best first search allows us toswitch between paths thus gaining the benefit of both approaches. At each step the mostpromising node is chosen. If one of the nodes chosen generates nodes that are less promising itis possible to choose another at the same level and in effect the search changes from depth to

    breadth. If on analysis these are no better then this previously unexpanded node and branch isnot forgotten and the search method reverts to the descendants of the first choice and proceeds,backtracking as it were.

    This process is very similar to steepest ascent, but in hill climbing once a move is chosen and theothers rejected the others is never reconsidered whilst in best first they are saved to enable

  • 8/12/2019 Is Lecture Notes Unit 01

    45/52

    CSE-304-F Intelligent system By Kaushal Shakya

    45

    revisits if an impasse occurs on the apparent best path. Also the best available state is selected inbest first even its value is worse than the value of the node just explored whereas in hillclimbing the progress stops if there are no better successor nodes. The best first searchalgorithm will involve an OR graph which avoids the problem of node duplication and assumesthat each node has a parent link to give the best node from which it came and a link to all its

    successors. In this way if a better node is found this path can be propagated down to thesuccessors. This method of using an OR graph requires 2 lists of nodes

    OPEN is a priority queue of nodes that have been evaluated by the heuristic function but whichhave not yet been expanded into successors. The most promising nodes are at the front.CLOSED are nodes that have already been generated and these nodes must be stored because agraph is being used in preference to a tree.

    Heuristics In order to find the most promising nodes a heuristic function is needed called f'wheref' is an approximation tof and is made up of two partsgand h' whereg is the cost ofgoing from the initial state to the current node;gis considered simply in this context to be thenumber of arcs traversed each of which is treated as being of unit weight. h' is an estimate of theinitial cost of getting from the current node to the goal state. The function f' is the approximatevalue or estimate of getting from the initial state to the goal state. Bothgand h' are positivevalued variables. Best First The Best First algorithm is a simplified form of theA* algorithm.FromA* we note thatf' =g+h' wheregis a measure of the time taken to go from the initial nodeto the current node and h' is an estimate of the time taken to solution from the current node.Thusf' is an estimate of how long it takes to go from the initial node to the solution. As an aidwe take the time to go from one node to the next to be a constant at 1.

    Best First Search Algorithm:

    1. Start with OPEN holding the initial state2. Pick the best node on OPEN3. Generate its successors4. For each successor Do

    o If it has not been generated before evaluate it add it to OPEN and record itsparent

    o If it has been generated before change the parent if this new path is better and inthat case update the cost of getting to any successor nodes

    5. If a goal is found or no more nodes left in OPEN, quit, else return to 2.

  • 8/12/2019 Is Lecture Notes Unit 01

    46/52

    CSE-304-F Intelligent system By Kaushal Shakya

    46

    The A* Algorithm

    Best first search is a simplifiedA*.

    1. Start with OPENholding the initial nodes.2. Pick the BESTnode on OPENsuch thatf=g+ h' is minimal.3. If BESTis goal node quit and return the path from initial to BESTOtherwise4. Remove BESTfrom OPENand all of BEST's children, labelling each with its path from

    initial node.

    Graceful decay of admissibility

    If h' rarely overestimates h by more than d then the A* algorithm will rarely find a solutionwhose cost is dgreater than the optimal solution.

  • 8/12/2019 Is Lecture Notes Unit 01

    47/52

    CSE-304-F Intelligent system By Kaushal Shakya

    47

    And-Or Graphs

    Useful for certain problems where

    The solution involves decomposing the problem into smaller problems. We then solve these smaller problems.

    Here the alternatives often involve branches where some or all must be satisfied before we canprogress.

    For example if I want to learn to play a Frank Zappa guitar solo I could (Fig. 2.2.1)

    Transcribe it from the CD. OR Buy the `Frank Zappa Guitar Book'' ANDRead it from there.

  • 8/12/2019 Is Lecture Notes Unit 01

    48/52

    CSE-304-F Intelligent system By Kaushal Shakya

    48

    Notethe use of arcs to indicate that one or more nodes must all be satisfied before the parentnode is achieved. To find solutions using an And-Or GRAPH the best first algorithm is used asa basis with a modification to handle the set of nodes linked by the AND factor.

    Inadequate: CANNOT deal with AND bit well.

    AO* Algorithm

    1. Initialise the graph to start node2. Traverse the graph following the current path accumulating nodes that have not yet

    been expanded or solved3. Pick any of these nodes and expand it and if it has no successors call this value

    FUTILITYotherwise calculate onlyf' for each of the successors.4. Iff' is 0 then mark the node as SOLVED5. Change the value of f' for the newly created node to reflect its successors by back

    propagation.

    6. Wherever possible use the most promising routes and if a node is marked as SOLVEDthen mark the parent node as SOLVED.

    7. If starting node is SOLVEDor value greater than FUTILITY, stop, else repeat from 2.

  • 8/12/2019 Is Lecture Notes Unit 01

    49/52

    CSE-304-F Intelligent system By Kaushal Shakya

    49

    Lecture 14 and 15

    Game Playing

    Contents

    1. Introduction to Game Playing2. Minimax Algorithm3. Alpha-beta pruning

    Introduction to Game PlayingGame playing has been a major topic of AI since the very beginning. Beside the attraction of thetopic to people, it is also because its close relation to "intelligence", and its well-defined statesand rules.

    The most common used AI technique in game is search. In other problem-solving activities,

    state change is solely caused by the action of the agent. However, in Multi-agent games, it alsodepends on the actions of other agents who usually have different goals.

    A special situation that has been studied most is "two-person zero-sum game", where the twoplayers have exactly opposite goals. (Not all competition are zero-sum!)

    There areperfect information games(such as Chess and Go) and imperfect information games(suchas Bridge and games where a dice is used). Given sufficient time and space, usually anoptimum solution can be obtained for the former by exhaustive search, though not for the latter.However, for most interesting games, such a solution is usually too inefficient to be practicallyused.

    MINIMAX Algorithm

    Minimax (sometimes minmax) is a decision rule used in decision theory, game theory, statisticsand philosophy for minimizing the possible loss while maximizing the potential gain.Alternatively, it can be thought of as maximizing the minimum gain (maximin). Originallyformulated for two-player zero-sum game theory, covering both the cases where players takealternate moves and those where they make simultaneous moves, it has also been extended tomore complex games and to general decision making in the presence of uncertainty.

    Game theory

    In the theory of simultaneous games, a minimax strategy is a mixed strategy which is part of thesolution to a zero-sum game. In zero-sum games, the minimax solution is the same as the Nashequilibrium.

    Minimax theorem states:

  • 8/12/2019 Is Lecture Notes Unit 01

    50/52

    CSE-304-F Intelligent system By Kaushal Shakya

    50

    For every two-person, zero-sum game with finite strategies, there exists a value V and a mixedstrategy for each player, such that (a) Given player 2's strategy, the best payoff possible forplayer 1 is V, and (b) Given player 1's strategy, the best payoff possible for player 2 is V.

    Equivalently, Player 1's strategy guarantees him a payoff of V regardless of Player 2's strategy,and similarly Player 2 can guarantee himself a payoff of V. The name minimax arises becauseeach player minimizes the maximum payoff possible for the othersince the game is zero-sum,he also maximizes his own minimum payoff.

    This theorem was established by John von Neumann, who is quoted as saying "As far as I cansee, there could be no theory of games without that theorem I thought there was nothingworth publishing until the Minimax Theorem was proved".

    Example

    The following example of a zero-sumgame, where Aand Bmake

    simultaneous moves,illustrates minimax solutions. Supposeeach player has three choices andconsider the payoffmatrix for Adisplayed at right.Assume the payoff matrix for Bis thesame matrix with the signs reversed(i.e. if the choices are A1 and B1 then Bpays 3 to A). Then, the minimax choice for Ais A2 sincethe worst possible result is then having to pay 1, while the simple minimax choice for Bis B2since the worst possible result is then no payment. However, this solution is not stable, sinceif Bbelieves Awill choose A2 then Bwill choose B1 to gain 1; then if Abelieves Bwill chooseB1 then Awill choose A1 to gain 3; and then Bwill choose B2; and eventually both players willrealize the difficulty of making a choice. So a more stable strategy is needed.

    Some choices are dominatedby others and can be eliminated: Awill not choose A3 since eitherA1 or A2 will produce a better result, no matter what Bchooses; Bwill not choose B3 since B1or B2 will produce a better result, no matter what Achooses.

    A can avoid having to make an expected payment of more than 1/3 by choosing A1 withprobability 1/6 and A2 with probability 5/6, no matter what Bchooses. Bcan ensure anexpected gain of at least 1/3 by using a randomized strategy of choosing B1 with probability1/3 and B2 with probability 2/3, no matter what Achooses. These mixed minimax strategiesare now stable and cannot be improved.

    Alpha-beta pruningAlpha-beta pruning is a search algorithm which seeks to reduce the number of nodes that areevaluated by the minimax algorithm in its search tree. It is a search with adversary algorithmused commonly for machine playing of two-player games (Tic-tac-toe, Chess, Go, etc.). It stopscompletely evaluating a move when at least one possibility has been found that proves themove to be worse than a previously examined move. Such moves need not be evaluated further.

    B chooses B1 B chooses B2 B chooses B3

    A chooses A1 +3 2 +2

    A chooses A2 1 0 +4

    A chooses A3 4 3 +1

  • 8/12/2019 Is Lecture Notes Unit 01

    51/52

    CSE-304-F Intelligent system By Kaushal Shakya

    51

    Alpha-beta pruning is a sound optimization in that it does not change the score of the result ofthe algorithm it optimizes

    Improvements over naive minimax

    An illustration of alpha-beta pruning. The grayed-out subtrees need not be explored (whenmoves are evaluated from left to right), since we know the group of subtrees as a whole yieldsthe value of an equivalent subtree or worse, and as such cannot influence the final result. Themax and min levels represent the turn of the player and the adversary, respectively.

    The benefit of alpha-beta pruning lies in the fact that branches of the search tree can beeliminated. The search time can in this way be limited to the 'more promising' subtree, and adeeper search can be performed in the same time. Like its predecessor, it belongs to the branchand bound class of algorithms. The optimization reduces the effective depth to slightly morethan half that of simple minimax if the nodes are evaluated in an optimal or near optimal order

    (best choice for side on move ordered first at each node).With an (average or constant) branching factor of b, and a search depth of d plies, the maximumnumber of leaf node positions evaluated (when the move ordering is pessimal) is O(b*b*...*b) =O(bd) the same as a simple minimax search. If the move ordering for the search is optimal(meaning the best moves are always searched first), the number of leaf node positions evaluatedis about O(b*1*b*1*...*b) for odd depth and O(b*1*b*1*...*1) for even depth, or . In the latter case,where the ply of a search is even, the effective branching factor is reduced to its square root, or,equivalently, the search can go twice as deep with the same amount of computation.[8] Theexplanation of b*1*b*1*... is that all the first player's moves must be studied to find the best one,but for each, only the best second player's move is needed to refute all but the first (and best)first player move alpha-beta ensures no other second player moves need be considered. If

    b=40 (as in chess), and the search depth is 12 plies, the ratio between optimal and pessimalsorting is a factor of nearly 406 or about 4 billion times.

  • 8/12/2019 Is Lecture Notes Unit 01

    52/52

    An animated pedagogical example that attempts to be human-friendly by substituting initialinfinite (or arbitrarily large) values for emptiness and by avoiding using the negamax codingsimplifications.

    Normally during alpha-beta, the subtrees are temporarily dominated by either a first playeradvantage (when many first player moves are good, and at each search depth the first movechecked by the first player is adequate, but all second player responses are required to try andfind a refutation), or vice versa. This advantage can switch sides many times during the searchif the move ordering is incorrect, each time leading to inefficiency. As the number of positionssearched decreases exponentially each move nearer the current position, it is worth spendingconsiderable effort on sorting early moves. An improved sort at any dep th will exponentiallyreduce the total number of positions searched, but sorting all positions at depths near the rootnode is relatively cheap as there are so few of them. In practice, the move ordering is oftendetermined by the results of earlier, smaller searches, such as through iterative deepening.

    The algorithm maintains two values, alpha and beta, which represent the minimum score thatthe maximizing player is assured of and the maximum score that the minimizing player is

    assured of respectively. Initially alpha is negative infinity and beta is positive infinity. As therecursion progresses the "window" becomes smaller. When beta becomes less than alpha, itmeans that the current position cannot be the result of best play by both players and hence neednot be explored further.

    Additionally, this algorithm can be trivially modified to return an entire principal variation inaddition to the score. Some more aggressive algorithms such as MTD(f) do not easily permitsuch a modification.