ai i complete notes by murali krishna

75
Page 1 ARTIFICIAL INTELLIGENCE Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH ARTIFICIAL INTELLEGENCE (AI) UNIT I Introduction & Definitions: Def 1: AI is the study of how to make computers do things which, at the moment, people do better (Rich & Knight) Def 2: The automation of activities that associates with human thinking, activities such as decision-making, problem solving, learning (Bellman) Def 3: The exciting effort to make computers think, ………… , machines with minds (Haugeland) Def 4: The art of creating machines that perform functions, which require intelligence when performed by people (Kurzwell) Def 5: The branch of computer science that is concerned with automation of intelligent behavior (Luger) AI PROBLEMS: AI Techniques used in the problems such as game playing & theorem proving. Game playing involves both checkers-playing program & chess. Theorem proving explored in the area of mathematics, as geometry. Both game playing & theorem proving share the property that people who do well are considered to be displaying intelligence. Computers could perform well at those tasks simply by being fast at exploring a large number of solution paths and then selecting the best one. This process required little knowledge & programmed easily. Task Domains of AI: (i) Mundane tasks (ii) Formal Tasks a) Perception a) Games -Vision -Chess -Speech -Checkers b) Natural Language -GO -Understanding -Backgammon -Generation b) Mathematics -Translation -Geometry c) Commonsense Reasoning -Logic d) Robot Control. -Integral Calculus -Proving Properties of programs

Upload: dereje-dhufera

Post on 02-Jan-2016

55 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ai i Complete Notes by Murali Krishna

Page 1

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

ARTIFICIAL INTELLEGENCE (AI)

UNIT – I

Introduction & Definitions:

Def 1: AI is the study of how to make computers do things which, at the moment, people do

better (Rich & Knight)

Def 2: The automation of activities that associates with human thinking, ac tivities such as

decision-making, problem solving, learning (Bellman)

Def 3: The exciting effort to make computers think, ………… , machines with minds

(Haugeland)

Def 4: The art of creating machines that perform functions, which require intelligence when

performed by people (Kurzwell)

Def 5: The branch of computer science that is concerned with automation of intelligent

behavior (Luger)

AI PROBLEMS:

AI Techniques used in the problems such as game playing & theorem proving.

Game playing involves both checkers-playing program & chess.

Theorem proving explored in the area of mathematics, as geometry.

Both game playing & theorem proving share the property that people who do well are

considered to be displaying intelligence.

Computers could perform well at those tasks simply by being fast at exploring a large number

of solution paths and then selecting the best one.

This process required little knowledge & programmed easily.

Task Domains of AI:

(i) Mundane tasks (ii) Formal Tasks

a) Perception a) Games

-Vision -Chess

-Speech -Checkers

b) Natural Language -GO

-Understanding -Backgammon

-Generation b) Mathematics

-Translation -Geometry

c) Commonsense Reasoning -Logic

d) Robot Control. -Integral Calculus -Proving Properties of programs

Page 2: Ai i Complete Notes by Murali Krishna

Page 2

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Eg on Mundane tasks:

Consider going for shopping

(i) Planning a route and a sequence of shops to visit

(ii) Recognizing (through Vision) buses, people and shops

(iii) Communicating through Natural Language

(iv) Navigating round obstacles on the street, and manipulating objects for purchase

Mundane tasks are hardest to compute (getting bore and bore as visiting shop by shop).

Expert Tasks:

Engineering -Design

-Fault Finding -Manufacturing planning

Scientific Analysis Medical Diagnosis Financial Analysis

What is an AI Technique?

AI problems span a very broad spectrum

They appear to have little in common except that they are hard.

The desirable properties of AI Techniques are

i) It is voluminous.

ii) It is hard to characterize accurately iii) It is constantly changing. iv) It differs from the data by being organized in a way that corresponds to the ways it will be

used.

Four goals of AI

Systems that think like humans Systems that think rationally

Systems that act like humans Systems that act rationally

Rationally : Systems do processes with intelligence and finds solutions to complex problems Human: Solving simple problems as like a human

Page 3: Ai i Complete Notes by Murali Krishna

Page 3

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

THE LEVEL OF MODEL:

Before we set to do something, it’s a good practice to decide exactly “What we are trying to do?”

“What is our goal in trying to produce programs that do the tasks the same way that people do?”

Goal is classified into two ways

To design programs that do To design programs that do task in easiest

Tasks what people do. Way.

Effort to build programs what people do can be classified in two ways.

1) Type: Efforts to build programs that solve problems that do not really fit the definition of AI task. Eg: EPAM (Elementary Perceiver And Memorizer)

Illustration

EPAM is an AI program which gives some syllables for a particular syllabi.

2) Efforts to build program that solve the problems which fit the definition of AI.

The following are the reasons why we want to put our efforts to design 2 type,

a) To test psychological theories of human performance:

E.g. PARRY is an AI program which exploits a model of human paranoid behavior.

(Paranoia is a disease who fears about some thing is about to happen (i.e. more anxiety) For: A person who enters into a flight may fear that the flight may crash.

b) To enable computers to understand human reasoning:

eg: A Computer to be able to read newspaper story and then answer a question such as “why did the terrorists kill hostages” The AI program must be able to stimulate the

reasoning process and gives responses(Answers). c) To enable people to understand computer reasoning:

In many circumstances, people are reluctant(against) to relay on the output of a computer unless they can understand how the machine arrived at its result.

ve

Wa ter ll

ke

Page 4: Ai i Complete Notes by Murali Krishna

Page 4

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

d) If the computer’s reasoning process is similar to that people, then producing an acceptable explanation is much easier.

e) To exploit what knowledge we can glean from people since people are the best known performers of most of the tasks with we are dealing.

CRITERIA FOR SUCEESS:

In 1950, ALAN TURING Proposed a method for determining “WHETHER A MACHINE CAN THINK?” This method has become “TURING TEST”.

TURING TEST:

To conduct Turing test, we need two people and the machine to be evaluated.

In one room one person interrogator and is another room one person and machine.

The interrogator can ask questions of either person or computer by typing and receiving typed responses.

The interrogator knows then as A, B and aims to determine which is person and which is machine.

If the machine succeeds at this, then we will conclude that machine can think.

The machine is allowed to do whatever it can to fool the interrogator..

For e.g. How much is 12,324 times 739817 even though it can answer in microseconds, it will take several minutes and then respond with wrong answer.

Computer takes long time to pass the Turing test. No computer can imitate 100% like a person. So it is necessary to settle at a particular level of imitation.

Rating: We can limit the level of Turing test by setting certain rates to a program.

For e.g. A Program can acquire classes rating based on rating of players whom it can beat.

Turing Test based on level of achievement:

We can limit the level of Turing test by comparing the level of achievements made by program with humans.

For e.g. DENDRAL is a program that analyzes organic compounds to determine their structure.

Turing Test Based on Time:

We can compare the time it takes for a program to complete the task to the time required by a person to do the same thing.

e.g. R1 is a program that requires minutes to perform tasks that previously required hours of a skilled engineer’s time.

PROBLEM AS A STATE SPACE SEARCH:

To solve a particular problem, we need to build a system. To build a system, four things are

required.

Page 5: Ai i Complete Notes by Murali Krishna

Page 5

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

1. Define the problem precisely: This includes precise specifications of what is initial situation as well as what will be

the final situation constitute acceptable solution to the problem.

2. Analyze the problem: Explains about various possible techniques for solving the problem.

3. Isolate and represent the task knowledge that is necessary to solve the problem.

4. Choose the best problem solving technique and apply it to the particular problem.

DEFINING THE PROBLEM ASE A STATE SPACE SEARCH

PLAYING CHESS GAME

To build a program that could “play chess”,

a) We have to specify the starting position of the chess board, b) The rules that define legal moves c) The board positions that represent a win for one side

d) We have to write a program explicitly the goal of not only playing a game of chess but also the winning game.

1. For the problem “Play Chess”, it is fairly to provide a formal and complete description.

The starting position can be described as 8-by-8 array where each position contains a symbol, standing for the appropriate piece in the chess opening position.

Define our goal in any board position in which the opponent does not have a legal move and his/her kind is under attack.

There are several ways in which these rules can be written.

The two practical difficulties in writing the rules are:

a) No person could ever supply a complete set of such rules. It would take too long and could

certainly not to be done without mistakes.

b) No program could easily handle all those rules. Although a hashing scheme could be used to find the relevant rules for each move fairly quickly.

In order to minimize such problems, we have to write the rules describing the legal moves in a

general way.

We have to define the problem of playing chess as a problem of moving around in a state space, where each state corresponds to a legal position of the board.

Page 6: Ai i Complete Notes by Murali Krishna

Page 6

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

WATER – JUG PROBLEM:

Problem: You are given 2 Jugs, a 4-gallon one and a 3-gallon one. Neither has measuring monker’s o it. There is a pump that can be used to fill the jugs with water. How can you get exactly 2 gallons of

water into the 4 gallon jug? Sol: State space can be represented asset of ordered pairs of integers (x, y) such that

x=0, 1, 2, 3 or 4. y=0, 1, 2 or 3. Where x represents the quantity of water in 3-gallon jug.

The start space is (0,0) The goal state is (2mn)

Some assumptions which are not mentioned in the problem statement are, that we can pour water out of a Jug onto the ground.

Production Rules for water jug problem

Rule No & Rule Name Task to be done Meaning

1. (x, y) if x<4 (4, y) Fill the 4 – gallon jug. 2. (x, y) if y<3 (x, 3) Fill the 3-gallon jug. 3. (x, y) if x>0 (x-d, y) Pour some water out of the 4-gallon jug.

4. (x, y) if y>0 (x, y-d) Pour some water out of the 3-gallon jug. 5. (x, y) if x>0 (0,y) Empty the 4-gallon jug on the ground.

6. (x, y) if y>0 (x, 0) Empty the 3-gallon jug on the ground. 7. (x, y) if (x+y)>=4y>0 (4, y-(4-x)) Pour water from the 3-gallon jug into 4

gallon jug until the 4-gallon jug is full.

8. (x, y) if (x+y)>=3&x>0 (x-(3-y), 3) Pour water from the 4-gallon jug into 3- gallon jug until 3-gallon jug is full.

9. (x, y) if (x+y) <=4 & y>0 (x+y, 0) Pour all the water from 3-gallon jug into 4- Gallon jug.

10. (x, y) if (x+y)<=3 & x>0 (0, x+y) Pour all the water from 4-gallon jug into the

3-gallon jug. 11. (0,2) (2, 0) Pour the 2-gallons from the 3-gallons jug

into 4-gallon jug. 12. (2, y) (0, y) Empty the 2-gallons in the 4-gallon jug on

the ground.

Page 7: Ai i Complete Notes by Murali Krishna

Page 7

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Solution to the water jug problem

S.No. Gallons in the 4-gallon jug Gallons in the 3-gallon jug applied

1 0 0 Initial State space

2 0 3 2

3 3 0 9

4 3 3 2

5 4 2 7

6 0 2 5

7 2 0 9 or 11

Operationalization:

In both chess and water jug problem explained the first step (“Define the problem precisely”) towards

the design of a program to solve a problem. For that formal and manipulable description of the problem is required.

Hence we ill be able to write programs which produces these formal description into informal. This process is known as “Operationalization”.

To provide formal description of a problem, we have to do the following tasks. a) Define the state space that contains all the possible configurations of the relevant object.

b) Specify one or more states within that state space that describe possible situation from which the problem solving process start. This is known as “Initial State”.

c) Specify the state that would be acceptable as solution to the problem. This state is known as

“goal state”. d) Specify set of rules that describe the actions available. Doing this will require giving thought

to the following issues. i) What unstated assumptions are present in the informal problem description. ii) How general should the rules be?

iii) How much work is required to solve the problem should be pre computed and represented in the rules?

PRODUCTION SYSTEM:

A production system consists of

a) A set of rules, each consisting of left side that determines the applicability of the rule and right

sides that describes the operation to be performed if the rule is applied.

b) One or more knowledge/databases that contain whatever information is appropriate for the particular task.

Page 8: Ai i Complete Notes by Murali Krishna

Page 8

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

c) A control strategy that specifies the order in which the rules will be compared to the database and a way of resolving the conflicts that arise when general ruels match at once.

CONTROL STRATEGIES

Control Strategies decides which rule to apply during the process of searching for solution to a

problem. The two types of search algorithms are

1. Un informed search 2. Informed Search

Search Algorithms

Uninformed Informed

DFS BFS Cost-First Search Generate & Test Hill Climbing

Best First Problem Constraint Mean-end Search Reduction Satisfaction analysis

A*Search Ao* Search

Priority Queue F(n) = h(n)+g(n).

Priority Queue

n(n)

Page 9: Ai i Complete Notes by Murali Krishna

Page 9

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Uninformed Search: Also called Blind or Exhaustive search. Search and therefore may not be very

efficient.

Informal search: Also called heuristic or intelligent search. Uses information about the problem to

guide the search.

Uninformed search is of two types

i) BFS ii) DFS

i) BFS (Breadth-First Search):

This is an exhaustive search technique.

The search generates all nodes at a particular level before proceeding to the next

level of tree.

The search systematically proceeds testing each node that is reachable from a parent

node before it expands to any child of those nodes.

Search requires considerable memory resource.

The space that is searched is quite large and start node. However, guarantee that if

we find a solution it will be the shortest possible.

Search terminates when a solution is found, the test returns true.

Nodes are explored in the order A B C D E F G H I J K L M N O P

The goal node J will be found before the goal node. M.

Algorithm:

Put the Root Node on a Queue.

While (Queue is not empty)

{

Remove a node from the queue;

If (node is a goal node)

Return success;

Put all children of node onto the queue;

} return failure;

Page 10: Ai i Complete Notes by Murali Krishna

Page 10

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

DFS (Depth –First-Search):

This is also an exhaustive search.

The search systematically proceeds to some depth d, before another path is considered.

If the maximum depth of search tree is three, then if this limit is reached and if the

solution has not been found, then the search back tracks to the previous level, and

explores the remaining alternatives at this level.

Root node

& are goal nodes.

Nodes are explored A B D E H L M I N O F G J K P

After searching A, B, D the search backtracks and tries another path from node ‘B’.

The goal will be found before the goal node

Algorithm:

Put the root node on a stack

While (stack is not empty) { Remove a node from stack;

If (node is goal node) Return success;

Put all children of node onto stack; } Return failure;

A

B C

D E F G

H I J K

L M N O P

A

a J

a

M

a

M

a

J

a

Page 11: Ai i Complete Notes by Murali Krishna

Page 11

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

HEURISTIC SERACH

A Heuristic is a technique that improves the efficiency of search process, possibly by sacrificing claims of completeness. Heuristics are like tourist guides.

They are good to the extent that they point in generally interesting directions.

They are bad to the extent that they miss the points of interest to particular individuals.

For complex problems, traditional algorithms are unable to find the solution with some limits

of time & space.

Hence heuristic functions are used to solve complex problems. Because of : a) Blind search is not always possible, because they require too much space & time.

Heuristics are “Rules of Thumb”. they do not guarantee for a solution to a problem.

Heuristics are knowledge about domain which help search and reasoning in its domain.

Heuristics search function estimates cost from current state to goal, presuming function is efficient.

Example: 8-puzzle

State space:

Configuration of 8-tiles on the board.

Initial Goal

State:

Solution: Optimal sequence of operators

Action: “Move towards blank spaces”

-- condition: The move is with in the board.

--Direction: Left, Right, Up, Down

Problem: Which 8 – puzzle move is best?

What heuristic can decide?

Which move is “best”?

1 2 3

7 8 4

6 5

1 2 3

8 4

7 6 5

Page 12: Ai i Complete Notes by Murali Krishna

Page 12

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Actions: Three possible moves: Left, Up, right

c

Right Left Down

h= 6 h=4 h=5

To find which move is best?

We can apply heuristics, 3 different approaches.

-Count Correct position of each tile, compare to goal state

-Count incorrect position of each tile, compare to goal state.

-Count how far away, each tile is from its correct position.

S.No Approaches Right Left Down

1. Count correct position 6 4 5

2 Count incorrect position 2 4 3

3 Count how far away 2 4 4

1 2 3

8 4

7 6 5

1 2 3

7 8 4

6 5

1 2 3

7 8 4

6 5

1 2 3

7 8 4

6 5

1 2 3

7 4

6 8 5

Page 13: Ai i Complete Notes by Murali Krishna

Page 13

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Heuristics: 3 different approaches

First approach: Count correct position of each tile, from its goal state.

a) The higher the number, the better it is.

b) Easy to compute (fast & takes less memory)

c) Probably the simplest heuristic.

Second approach: Count incorrect position of each tile, compare to goal state

a) The lower the number, the better it is.

b) The best move is where lowest number returned by heuristic.

Third approach: Count how far away, each tile is form its correct position.

a) The best move is where the lowest number returned by heuristic.

PROBLEM CHARACTERSTICS

In order to choose most appropriate method for a particular problem, it is necessary to analyze

the problem such as

i) Is the problem decomposable into a set of independent smaller or easier sub problems?

ii) Can the solution steps be ignored or at least undone if they prove unwise?

iii) Is the problem is universe predictable?

iv) Is the good solution to be problem obviously without comparison to all other possible

solutions? (or) Is the good solution absolute or relative?

v) Is the solution a state of the world or path to a state?

vi) Is a large amount of knowledge absolutely required to solve the problem or is knowledge

important only to constrain the search?

vii) Can a computer that simply given the problem return the solution or will the solution of the

problem require interaction between the computer & person?

PRODUCTION SYSTEM CHARACTISTICS:

These relates with problem characteristics and various classes of problems. Majorly explains

about set of characteristics & how the production system can be easily implemented.

Page 14: Ai i Complete Notes by Murali Krishna

Page 14

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Explain the relationship between problem type and types of production system those are best suited to

solve the problem.

Types of production systems:

1. Monotonic production system: A production system in which the application o f a rule never

prevents the later application of another rule, that could also have been applied at the time the

first rule was selected.

2. A non-monotonic production system: A production system in which mono-tonic production

system is NOT TRUE

3. PARTIALLY COMMUTATIVE PRODUCTION SYSTEM:

A production system with the property that if the application of a particular sequence of rules

transformers state “x” into state ‘y’, then any permutation of those rules that is allowable also

transforms state ‘x’ into state ’y’.

4. Communicative production system:

A production system that is both monotonic & partially cumulative production systems.

Four Categories of production system.

Monotonic Non Monotonic

Partially

Commutative Theorem Proving Robot Navigation

Not Partially

Commutative Chemical synthesis Bridge

WORK BOOK (ANSWER THE FOLLOWING QUESTIONS)

1. What is the criteria of measuring the system success? (MAY 2008(new))

2. Define AI & What is an AI technique?(MAY 2008(old)

3. Define and show a solution for AI problem as state space search? ?(MAY 2008(old)

4. Explain the production system characteristics? ?(MAY 2008(old)

5. Explain the process of Turing Test?(November 2008)

6. Given two jugs, a 4- litre one and a 3- litre one with no measuring marks on them. A pump exists

to fill the jugs with water. How can one get exactly 2 litres of water into the 4- litres jug? Write

Dame
Cross-Out
Commutative
Dame
Highlight
Answer at page 4
Dame
Highlight
Answer at page 13
Page 15: Ai i Complete Notes by Murali Krishna

Page 15

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

the production rules for water jug problem. Explain the process of solving the problem.

(November,2008)

7. What is a production system? Discuss about various types of production system with suitable

examples? (November,2008)

Dame
Highlight
Answer at page 14
Page 16: Ai i Complete Notes by Murali Krishna

Page 16

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

UNIT – II

FORWARD Vs BACKWARD REASONING

The object of a search procedure is to discover a path through a problem space from an initial

configuration to a goal state.

There are two search procedures.

i) Forward Reasoning:

From the start states

Forward rules to encode knowledge

ii) Backward Reasoning:

From the goal states.

Backward rules to encode knowledge about “how to achieve particular goals”

Combining Forward & Backward Reasoning

A1, A2, A3…………, AK-1, AK, AK+1, Ak+2,………….., An

Achieved by forward reasoning achieved by backward reasoning.

Backward-chaining Rule systems:

These rules can be implemented in PROLOG, which are good for goal directed problem

solving.

In PROLOG, rules are restricted and this allows rapid indexing. Rules are matched with

Unification Procedure. Rules in a PRLOG program are matched in the order in which they appear.

Forward-Chaining Rule Systems:

Instead of being directed by goals, sometimes we want to be directed by incoming data. E.g.

suppose you sense searching heat near your hand, you are likely to jerk your hand away. In forward-

chaining systems, left sides of rules are matched against description. Rules that match dump their right

hand side assertations into the state & the process repeats. Matching is typically more complex for

forward-chaining systems than backward-chaining.

Page 17: Ai i Complete Notes by Murali Krishna

Page 17

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

PROBLEM TREES Vs GRAPHS

A Tree is a graph in which any two vertices are connected by exactly one simple path. In other

words, any connected graph without cycles is a tree.

In other words, any connected graph without cycles is a tree.

A tree is an Undirected simple graph G that satisfies any of the following conditions.

G is connected an has no cycles.

G has no cycles, and a simple cycle is formed if any edge is added to G.

Moves in a graph can be undone.

In a graph, there is more than one path to a goal, where as in a tree, a path to a goal is more

cleanly distinguishable.

A goal state may need to appear more than once in a tree. Tree

a e d a

Graph bb c d

b c

Knowledge Representation: e f g h I j

In order to solve the complex problems encountered in AI, one needs both large amount of

knowledge and some mechanisms for manipulating knowledge.

Knowledge representation needs two entities.

Facts

Representation

Facts: Truths in real world. These are the things we want to represent.

Representations of facts in some choosen formalism

Page 18: Ai i Complete Notes by Murali Krishna

Page 18

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

*Mapping between facts & representations *

*

*

Eng. Understanding English generation.

The knowledge level, at which facts are described.

The symbol level, at which representations of objects at the knowledge level are defined in terms of

symbols that can be manipulated by programs.

General Representation of Knowledge:

Logical Representations

Production Rules

Semantic Networks

Conceptual graphs

Frames

Description Logics.

What is Logic?

A Language with concrete rules

No ambiguity in representation

Very Unlike natural languages eg. English

Logics are languages, reasoning is a process.

Facts Internal Representation

English

Representation

Page 19: Ai i Complete Notes by Murali Krishna

Page 19

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Propositional Logic:

Syntax:

Propositions : eg: “It is wet”

Connectives: AND , OR, NOT, IMPLIES, IFF

Brackets, T (True), FL(False)

FRAME PROBLEM:

How to propagate information to the next situation when an action is taken. For example, if a

box is at position ‘P’ and the light is turned off, the box is still at ‘P’.

The name “frame problem” derives from a common technique used by “animated cartoon”

makers called framing where the currently moving parts of the cartoon are super imposed on

the “frame” which depicts the background of the scene, which does not change.

In logical context, actions are typically specified by what they change, with the implicit

assumption that everything else remain unchanged.

The frame problem occurs in simple domains.

A Scenario with a door which can be opened or closed.

Open (0)

On (0)

Locked (0) Open (1)

Open (0) Open (1)

On (0) On (1)

Eg: There might be a table with a plant on it under the window. Suppose if we move the table to the

center of the room. We must also infer that the plant is now in the center of the room too, but the

window is not.

Frame Problem:

Def: The problem of representing the facts that change as well as that does not change is known as

frame problem.

To support this kind of reasoning, some systems make use of an explicit set of axioms, called

frame axioms, which describe all the things that do not change when a particular operator is applied in

state ‘n’ to produce state ‘n+1’

True Open (1)

Page 20: Ai i Complete Notes by Murali Krishna

Page 20

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

MATCHING:

The process of searching to solve the problems as the application of appropriate rules to

individual problem states to generate new states to which the rules can then be applied, until a solution

is found. This can be done by matching.

Matching Through Indexing

One way to select applicable rules is to do a simple search through all the rules, comparing

each one’s pre conditions to the current state and extracting all the ones that match.

Matching with variables:

The problem of selecting applicable rules is made more difficult when preconditions are not

stated as exact descriptions.

The Temporal nature of data. Full matching is only pursued for candidates that could be

affected by incoming or outgoing data.

Structural similarity in rules. Different rules may share a large number of pre-conditions.

Persistence of variable binding consistency

Complex and Approximate Matching:

(X me Y) (X you Y)

(I remember X) (Why do remember X just now)

(My {family-member} is Y) (Who else in your family is Y?)

(X {family-member} Y) (Tell me more about your family)

HEURISTIC FUNCTION

The various heuristic search methods are

(a) Generate – and – test

(b) Hill climbing

(c) Best –First search

(d) Constrain Satisfaction

(e) Means-ends analysis.

Page 21: Ai i Complete Notes by Murali Krishna

Page 21

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

a) Generate and Test:

This is simplest of all approaches.

i) Generate a possible solution. I.e. for some problems, generate a particular point in problem space and for some other problems generate a path from a start state.

ii) Test to see if this is actually a solution by comparing the chosen point or the end point

of the chosen path to the set of all acceptable goal states.

iii) If a solution has found, quit, otherwise return to step1.

If the generation of possible solutions is done systematically, then the procedure will find a solution.

If the problem space is very large, eventually may take very long time.

Generate-and-Test algorithm is a depth-first procedure since complete solution must be generated before they can be tested.

b) Hill Climbing:

Search always moves towards the goal. Using heuristics, it finds direction will tak e to closest

to the goal. The name hill climbing comes from analogy.

A hiker is lost halfway up a mountain at night. His camp is at the top of the mountain. Even

though it is dark, the hiker knows that every step he takes up the mountain is a step towards his

goal. So a hill climbing search always goes to the node closest to the goal. Hill climbing is

actually a combination of “ Generate-and-Test” + Direction to move.

Hill Climbing is a variant of generate-and-test in which feedback from the test procedure is

used to help the generator decide to which direction to move in the search space. In a pure

generate-and-test procedure, the test function responds with only yes or no.

But if the test function is augmented with a heuristic function that provides an estimate of how close a

give state is to a goal state.

This is a nice approach because computation of heuristic function can be done at almost no cost

at the same time that the test for a solution is being performed.

Hill Climbing is often used when a good heuristic function is available for evaluating states but

when no other useful knowledge is available.

Page 22: Ai i Complete Notes by Murali Krishna

Page 22

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Eg: You are in an Unfamiliar city without a map and you want to get downtown, you simply aim for

tall buildings. The heuristic function is just distance between the current location and location of tall

buildings and the desirable states are those in which the distance is minimized.

The heuristic function is to estimate how close a given state to a goal state.

Algorithm:

1. Evaluate the initial state. If it is goal state, then return and quit. Otherwise continue with initial

state as the current state.

2. Loop until a solution is found or until there are no new operators left to be applied in the

current state.

a) Select an operator that has not yet been applied to the current state and apply it to produce a

new state.

b) Evaluate the new state.

i) If it is goal state, then return & quit.

ii) If it is not goal state, but it is better than the current state, then make it as current state.

iii) If it is not better than current state, then continue in the loop.

Steepest ascent hill climbing:

Algorithm

1. Start with initial state and evaluate it

2. Continue in the loop till a solution is found or till a complete iteration produces no change to

current state

2.1 Let SUCCESSOR be a state such that any possible successor of the current state will be

better than SUCCESSOR.

2.2 For all operators that applies to the current state, evaluate the new state, if the new state is

goal state, QUIT, else if it is better than SUCCESSOR, then set SUCCESSOR to this state

2.3 If SUCCESSOR is better than the current state, then set the current state to SUCCESSOR.

Page 23: Ai i Complete Notes by Murali Krishna

Page 23

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

PROBLEMS WITH HILL CLIMBING

(a) Local Maxima

(i) Can’t see higher peak

(ii) Shoulder: can’t see the way out

Diagram to be inserted

A local maxima is a state where we have climbed to the top of the hill, and missed the

mountain. A state that is better than all of its neighbours, but not better than some other

states far away.

(b) Plateau

A plateau is a state where everything around is about as where we are currently. In other words

a flat area of the search space in which all the neighboring states have the same value.

Flat area

(c) Ridge

It’s a special kind of Local Maximum. It is an area of the search space that is higher than

surrounding areas and that itself has a slope. But in orientation of the high region, compared to

the set of available moves and the directions in which they move, makes it impossible to

traverse a ridge by single moves.

Page 24: Ai i Complete Notes by Murali Krishna

Page 24

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

SIMULATED ANNEALAING

Simulate means to To Possess and Annealing means Heat and Cool Slowly

Both in simple hill climbing and steepest ascent hill climbing, we used the heuristic function from the

bottom of the hill to the top of the mountain. i.e., Heuristic function will maximize the options.

Where as in Simulated Annealing, is a process which is one of the valley descending.

Simulated annealing will minimize rather than maximize the value of objective function.

Simulated annealing as a computational process , it is patterned after the physical process of annealing,

in which the physical substances such as metals are melted (i.e., raised to high energy levels) and then

gradually cooled until some solid state is reached. The goal of this process is to produce “Minimal-

energy final state”.

The process is one of valley descending in which the objective function is energy level.

The physical substances usually move from higher energy configuration to lower one.

Similarly valley descending occurs naturally. There is some probability that a transition to higher

energy state will occur.

The probability given by the function

P = e - E / KT

Where E is the positive energy change in energy level.

T is the Temperature and

K is Boltzmann’s Constant.

Best-First Search:

The Two systematic control strategies are Breadth-First Search & Depth-First search. Best-

First Search is the one which is a way of combining the advantages of both depth-first & breadth-first

search into a single method.

Eg: Step 1 Step 2:

(3) (5) ( 1)

A A

B C D

Page 25: Ai i Complete Notes by Murali Krishna

Page 25

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Step 3: Step 4:

(3) (5) (5)

(4) (6) (6) (5) (4) (6)

Step 5:

(5)

(6) (5)

(2) (1)

At each step of best- first search process, we select the most promising of the nodes we have

generated so far.

This is done by applying an appropriate heuristic function to each of them.

We then expand the chosen node by using the rules to generate its successors

If one of them is the solution, quit it

If not, all those new nodes are added to the set of nodes generated so far, again the most

promising node is selected and the process continues.

E F

A

B C D

E F

A

B C D

G H

E F

A

B C D

G H

I J

In the next step, J will be

expanded, since it is most

promising. This process can

continue until a solution is

found.

Page 26: Ai i Complete Notes by Murali Krishna

Page 26

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

ALGORITHM:

1. Start with OPEN containing just the initial state.

2. Until a goal is found or there are no nodes left on OPEN to

a) Pick the best node on OPEN

b) Generate its successors

c) For each Successor do;

i) If it has been generated before, evaluated it, add it to OPEN, and record its parent

ii) If it has been generated before, the parent if this new path is better the previous one. In

that case, update the cost of getting this node and to any successors that this node may

already have.

Informed search methods/Domain Specific Information

Add domain-specific information to select what is the best path to continue searching along.

Define a heuristic function, n(n) that estimates the “goodness” of a node n with respect to

reacting a goal.

Specifically, h(n)=estimated cost (or distance) of minimal cost path from ‘n’ to a goal state.

h(n) is about cost of the future search. g(n) is

about cost of the past search.

h(n) is an estimate (rule of thumb), based on domain specific information that is computable

from the current state description. Heuristics do not guarantee feasible solutions and are often

without theoretical basis.

In general,

n(n)>=0 for all nodes of ‘n’

n(n0=0 implies that ‘n’ is goal node

n(n) = infinity implies that ‘n’ is a dead end from which a goal cannot be reached.

Page 27: Ai i Complete Notes by Murali Krishna

Page 27

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Best First Search:

Order nodes on the OPEN list by increasing value of an evaluation function, f(n), that

incorporates domain specific information in the following way.

f(n) = g(n) (Uniform Cost)

f(n) = h(n) (Greedy Algorithm)

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

A* Algorithm:

The best first search was presented in a simplification of an algorithm called A*, which was

presented by “Hart etal”.

The algorithm uses the functions f’, g, h’ and two lists {OPEN} & {CLOSED}.

Step 1: Start with {Open} Containing only initial node. Set the node’s g value to zero, and its h’ value

to whatever it is, and its f’ value to h’+0 or h’. Set {Closed} to the empty list.

Step 2: Until a goal node is found, repeat the following procedure. If there are no nodes on {OPEN}

report failure. Otherwise, pick the node on {OPEN} with the lowest f’ value. Call it as BESTNODE.

Remove it from OPEN. Place it on CLOSED. See if BESTNODE is goal node. If so, exit and report a

solution, otherwise, generate the successors of BESTNODE but do not set BESTNODE but do not set

BESTNODE to point to them yet. For each successor, do the following.

Idea: avoid expanding paths that are already expensive.

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

g(n) = Cost so far to reach n.

h(n) = Estimated cost from n to goal.

f(n) = estimated total cost of path through n to goal.

a) Set SUCCESSOR to point back to BESTNODE. These backwards links will make it possible

to recover the path once a solution is found.

b) Compute g(SUCCESSOR) = g(BESTNODE) + the cost of getting from BESTNODE to

SUCCESSOR.

c) See if successor is same as any node on OPEN. If so, call that node u. Since this node already

exists in graphs, we can throw successor away and add Old to the list of Best nodes Successors.

Page 28: Ai i Complete Notes by Murali Krishna

Page 28

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

d) If Successor was not already on either OPEN or CLOSED, then put it on OPEN, and add it to

the list of BESTNODE’S successors. Compute

f’ (Successor) = g (Successor) + h’ (Successor)

Summary:

A * is a Greedy best-First graph search algorithm that finds the least-cost path from a given

initial node to goal node.

It Uses a distance-plus-cost heuristic function (f(x)) to determine the order in which the search

visits nodes in the tree. The distance-plus-cost heuristic is a sum of two functions.

The Path –Cost function, which is the cost from the starting node to the current node, usually

denoted by g(x).

And an admissible “Heuristic estimate” of the distance to the goa l, usually denoted by h(x).

The h(x) part of the f(x) function must be an admissible heuristic, i.e. must not over estimate

the distance to the goal. H(x) might represent the straight line distance to the goal, since that is

physically smallest possible distance between any two points.

A* is a search algorithm “distance-to-goal + path-cost”.

Also known as ‘OR’ Graph

PROBLEM REDUCTION:

In the Greedy Best- first search strategies for OR graphs through which we find a single path to

a goal. Such structure represents the fact that will know how to get from a node to a goal state.

AND –OR GRAPHS

Useful for certain problems where

The solution involves decomposing the problem into small problem.

We than solve these smaller problems.

The alternatives often involve branches where some or all most be satisfied before we can progress.

For example If I want to learn to play a Frank Zappa guitar solo I could.

Transcribe it from the Cd (OR)

Buy the “Frankzappa Guitar Book” AND Read it from there.

Page 29: Ai i Complete Notes by Murali Krishna

Page 29

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Step 1: Step 2: 1

3 6 1 3 6

4 6

Step 3: Step 4: 3 3 6 1 6 1

4 7 5 4 6 7 5

2 1 Indicate that one or more nodes must all be satisfied before the parent node is achieved. To find solutions using an AND-OR graph. AO* ALGORITHM:

1. Initialize the graph to start node. 2. Traverse the graph following the current path accumulating nodes that have not yet been expanded or

solved. 3. Pick up any of these nodes and expand it and if it has no successors call this value FUTILITY otherwise

calculate only f’ for each of the successors. 4. If f’ is zero then mark the node as SOLVED. 5. Change the value of f’ for the newly created node to reflect its successors by back propagation. 6. Whenever possible use the most promising routes and if a node is marked as SOLVED then mark the

parent as solved. 7. If starting node is SOLVED or value greater than FUTILITY, stop, else repeat from 2.

Means – ENDS Analysis

Allows both backward and forward searching

This means we could solve major parts of a problem first and then return to smaller problems

when assembling the final solution.

GPS was the first AI program to exploit means –ends analysis.

A STRIP (A robot planner) is an advanced problem solver that incorporates means-ends

analysis and other techniques.

Algorithm

1. Until the goal is reached or no more procedures are available.

Describe the current state, the goal state and the differences between the two.

Use the difference the describe a procedure that will hopefully get nearer to goal.

Use the procedure and update current state.

Page 30: Ai i Complete Notes by Murali Krishna

Page 30

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

2. It goal is reached then success otherwise fail.

Constraint Satisfaction

The General problem is to find a solution that satisfies a set of constraint.

Heuristics used not to estimate the distance to the goal but to decide what node to expand nest.

Example of this technique are Robot path planning, crypt arithmetic puzzles & design

problems.

Algorithm:

1) Propagate available constraints. To do this first set OPEN to the set of all objects that must

have values assigned to then in a complete solution. Then do until an inconsistency is detected

or until OPEN is empty.

a) Select an object OB from OPEN. Strengthen as much as possible the set of constraints that

apply to OB.

b) If this set is different from the set that was assigned the last time OB was examined o r if

this is the first time OB has been examined, then add to OPEN all objects that share any

constraints with OB.

c) Remove OB from OPEN.

2) If the Union of constrains discovered above defines a solution, then quit and report the solution.

3) If the Union of constraints discovered above defines a contradiction, then return failure.

4) If neither of the above occurs, then it is necessary to make a guess at something in order to

proceed. To do this, loop until a solution is found or all possible solutions have been

eliminated.

a) Select an object whose value is not yet determined and select a way of strengthening the

constraints on that object.

b) Recursively invoke constraint satisfaction with the current set of constrains augmented by

the strengthening constraint just selected.

e.g. A) S E N D b) C R O S S

+M O R E + R O A D S Crypt-Arithmetic problems

__________________ _______________

M O N E Y D A N G E R

___________________ ______________

MINIMAX SEARCH PROCEDURE

The Minimax search procedure is a depth-first, depth- limited search procedure.

The idea is to start at the current position and use the plausible-move generator to generate a set

of possible successor positions.

Now we can apply the static evaluation function to those positions and simply choose the best

one.

Page 31: Ai i Complete Notes by Murali Krishna

Page 31

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

After doing so, we can back that value upto the starting position to represent our evaluation of

it.

The starting position is exactly as good for us the position generated by the best move we can

make next.

Here we assume that the static evaluation function returns large values to indicate good

situations for us, so our goal is to maximize the value of static evolution function of the next

board position.

Eg1.

Eg2.

An example of this operation is that, it assumes a static evaluation function that returns values ranging

from -10 to +10, with +10 indicating a win for us, -10 a win for the opponent, and ‘0’ an even ma tch.

Since our goal is maximize the value of heuristic function, we choose to move to ‘B’ backing ‘B’s

value up to A, we can conclude that A’s value is ‘8’.

Adding Alpha-Beta cutoffs

Minimax procedure is a depth-first process.

In general, depth-first process officiency can be improved by using branch-bound technique, in

which partial solutions that are clearly worse than known solutions can be abandoned early.

To modify our minimax search procedure slightly to handle both maximizing and minimizing

and minimizing players, it is also necessary to modify the branch-bound strategy to include two

bounds, one for each of the players. This modified strategy is called Alpha-Beta Pruning.

E

F G H I J K

A

B C D

(9) (6) (0) (0) (-2) (-4) (-3)

Two Ply Search

E

F G H I J K

A

B C D

(9) (-6) (0) (0) (-2) (-4) (-3)

(-2)

(-6) (-2) (-4)

Maximizing Ply

Minimizing Ply

Page 32: Ai i Complete Notes by Murali Krishna

Page 32

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Alpha-Beta Pruning requires maintenance of two threshold values, one representing the

i) Lower bound on the value that a maximizing node may ultimately be assigned (called

Alpha)

ii) Represent a Upper bound on the value that a minimizing node may be assigned (Called

Beta)

The Alpha-Beta procedure works as the follows.

After examining node F, we know that the opponent is guaranteed a score of -% or less at c since the

opponent is minimizing player. We also know that we are guaranteed a source of 3 or greater at node

A.

Additional Refinements:

One of the modifications to the minimax procedure is Alpha-Beta pruning. There are some

other modifications to minimax, other than Alpha-beta pruning are known as additional refinements.

Those are

i) Waiting for Quiescence.

ii) Secondary Search.

iii) Using Book moves.

Waiting for Quiescence:

This is used for determining when to stop going deeper in the search tree is whether the situation is

relatively stable.

Suppose ‘B’ node is expanded one more level. When we booked one move ahead, our estimate of

worth ‘B’ changed drastically. The opponent has significantly improved the immediate appearance of

his/her position by initiating a piece of exchange.

To make sure that such short-term measures do not influence our choice of move, we should continue

the search until no such drastic change occurs from one level to the next called as “waiting for

Quiescence”.

A

B C

E F G D

(3) (5) (-5) (0)

Two Ply Search

A

B C

E F G D

(3) (5) (-5) (0)

Two Ply Search

(3)

(>3)

(-5) (<-5)

Minimizing Ply

Maxmizing Ply

Page 33: Ai i Complete Notes by Murali Krishna

Page 33

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Secondary Search:

Suppose, if we explore a game tree to an average depth of six play and on the basis of that search, choose a particular move. Although it would have been too expensive to search the entire tree to a

depth of eight, it is not very expensive to search the single chosen branches an additional two levels to make sure that it still looks good. This technique is called secondary search.

Using Book moves:

For complicated games taken a whole, which is not feasible to select a move by simply looking up the game configuration in a catalogue and extracting the correct move. The catalogue would be immense and no one knows how to construct it. But for some segments of games, this approach is applicable.

Eg. In Chess, for example, both the beginning (opening) sequences and endgame sequences are highly stylized. In these situation, the performance of a program can often be considerably enhanced if it is

provided with a list of moves known as (Book moves) that should be made. The use of book moves in the opening sequences and endgames, combined with the use of

minimax search procedure for the midgame, provides more effective results (technique).

WORK BOOK (ANSWER THE FOLLOWING QUESTIONS)

1. Explain about Alpha-Beta cutoff with an algorithm and suitable examples(Nov,2008(new))

2. Discuss about Forward Vs Backward reasoning(Nov,2008(old))

3. Differentiate Problem Trees Vs Graphs (Nov,2008(old))

4. Describe the different matching techniques(May,2008(old))

5. Write the MiniMax procedure.

6. Differentiate between AO* and A* algorithm

7. Solve the crypt arithmetic problems

(a) S E N D (b) D O N A L D

M O R E G E R A L D _________ ____________ M O N E Y R O B E R T

__________ ____________

Page 34: Ai i Complete Notes by Murali Krishna

Page 34

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

UNIT – III

Representing the simple facts in logic:

Standard predicate logic symbols are

Implication

Not

OR

AND

For all

There exist.

E.g. 1:

It is raining – Raining

It is sunny – Sunny These are facts.

It is windy – windy

If it is raining, then it is not sunny

E.g. 2.

(i) Marcus was a man

(ii) Marcus was a Pompeian (iii) All Pompeian’s were Romans (iv) Caesar was a ruler

(v) All Romans were either loyal to Caesar or hated him (vi) Every one is loyal to someone.

(vii) People only try to assassinate rulers they are not loyal to (viii) Marcus tried to assassinate Caesar.

Predicate logic of the above facts are (i) Man ( Marcus )

(ii) Pompeian(Marcus) (iii) X: Pompeian (iv) Rules (Ceasar)

(v) x: Roman (x) →loyal to (x,Caesar) V hate (x,Caesar) (vi) X :Зy : loyal to (x,y)

(vii) X: y: person (x) ^ ruler (y) ^ try assassinate (x,y) (viii) Try assassinate (Marcus, Caesar)

Raining Sunny

Page 35: Ai i Complete Notes by Murali Krishna

Page 35

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Computable Functions & Predicates:

Simple facts were expressed as combinations of individual predicates.

Computable functions explains about expressing simple facts, such as greater than and less-than relationships. gt(1,0) lt(0,1)

gt(2,1) lt(1,2) gt(3,2) lt(2,3)

e.g: gt(2+3,1) Compute the value of the plus function given the arguments 2 and 3, then send the argument 5

and 1 to gt.

Compute the following using predicates:

1. Marcus was a Pompeian

2. All Pompeian’s died when the volcano erupted in 79 AD 3. It is now 2004 AD

Q: Is Marcus Alive?

A) The predicates are as follows: 1) Pompeian(Marcus) 2) Erupted (Volcano, 79)^ x: Pompeian(x)→died(x,79)

3) now = 2004. x : t1: t2:died(x,t1) ^ greater-than (t2,t1) →dead(x,t2)

Many English sentences are ambiguous. There is often a choice of how to represent knowledge.

Obvious information may be necessary for reasoning. We may not know in advance which statements to deduce (p or ¬p)

Representing Instance & ISA relationships:

e.g. 1 Pompeian (Marcus) Predicate: x : Pompeian(x) → Roman(x)

e.g.2 Instance (Marcus, Pompeian) Predicate: x: instance (x, Pompeian) → instance (x, roman)

e.g.3 Instance (Marcus, Pompeian) Isa (Pompeian, Roman)

Predicate: x: y : z : Instance (x,y) ^ Isa (y, z) → Instance (x,z)

Page 36: Ai i Complete Notes by Murali Krishna

Page 36

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Resolution:

Def 1: A Resolution is a principle based on machine-oriencted logic.

Def 2: A Resolution is a procedure, which gains its efficiency from the fact that it operates on

statements that have been converted to a very convenient standard form. Resolution produces proofs by “refutation”. i.e. to prove a statement,resolution attempts to

show that the negation of statement produces a contraction with the known statements.

KB l=d KB ^ ¬ £.1 =false (KB stands for knowledge Base) (£ V ¬ b) ^ (r V b) (£ v r)

Sound & complete

(or) To proof £ given knowledge KB Step 1: Negate £ : ¬£

Step 2: Add £ to KB Step 3: Convert KB into CNF

Step 4: Infer contradiction (empty clause) using resolution rule. Basis of Resolution (Using Literals)

Resolution procedure is a simple iterative process.

At each step, 2 clauses called as (a) parent clause 2 resolved or compared. The two clauses yielding a new clause that has been inferred from them.

The new clause represents ways that the two clauses interacts each other.

Eg. Winter Vs summer

¬ Winter V Cold both clauses must be true.

We can observe that one of winter and ¬ winter will be true at any point. From these two clauses, we can deduce. Summer V Cold

Both winter and ¬ winter will produce an empty clause. Example on Resolution:

Knowledge Base: Q) To prove Rich (Me)

1) ¬ Ph.D(x1) V Highly qualified (x1) 2) Ph.d(x2) V Early Earnings (x2) In order to prove the above add 3) ¬ Highly Qualified (x3) V Rich (x3)

4) ¬ Early Earnings (x4) V Rich (x4) 5) ¬ Rich (me)

Basic Idea

¬ Rich(me) to the CNF KB

Page 37: Ai i Complete Notes by Murali Krishna

Page 37

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Proof:

Resolution in propositional logic:

Algorithm:

Step 1: Convert all the propositions of KB (Knowledge base to clause form (s).

Step 2: Negate £ and convert it to clause form. Add it to s.

Step 3: Repeat until either a contradiction is found or no progress can be made.

a. Select two clauses (£ V ¬ P) and (r V P) b. Add the resolvant (£ V r) to s.

e.g. Given knowledge base KB axioms are

KB = {p, (p^q) → R, (S V T) → Q, T }.

(£ = R to be proved)

A. Given Axioms Converted to clause Form

P P (1)

(P^Q) → R ¬ p V ¬ Q V R (2)

(S V T ) → Q ¬ S V Q (3)

¬ T V Q (4)

T (5)

T ¬ R (6)

¬ Ph D(x) V Hq(x) ¬ HQ (x) V Rich (x)

¬ Ph.D (x) V Rich (x) Ph.D(x) V EE(x)

Rich(x) V EE(x) ¬ EE(x) V Rich

(x)

¬ Rich(x) ¬ Rich (x)

given

Page 38: Ai i Complete Notes by Murali Krishna

Page 38

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

¬ p V ¬ Q V R ¬ R

¬ p V ¬ Q P

¬ Q

¬ T V Q

--T T

Simple tips while conversion to clause form

1. Eliminate →

P → Q = ¬ P V Q

2. Reduce the scope of each ¬ to a single term.

¬ (P V Q) = ¬ P ^ ¬ Q

¬ (P ^ Q) = ¬ P V ¬ Q

¬ x:P = x:¬ p

¬ x:P = x:¬p

¬ ¬P=P.

NATURAL DEDUCTION:

Natural deduction is an approach to proof theory that attempts to provide a

deductive system which is a formal model of logical reasoning as it “naturally” occurs.

This approach is in contrast to axiomatic systems which use axioms.

Eg. Suppose we believe that all judges who are not crooked are well-educated, can be represented in

natural deduction as.

But the same statement is converted to clause form as

x:judge(x) ^ ¬ crooked(x) → well-educated(x)

¬judge(x) V crooked(x) V educated (x)

Page 39: Ai i Complete Notes by Murali Krishna

Page 39

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

How? In the form of A → B where A = judge(x) ^ ¬crooked(x) B= well educated (x) A → B = ¬ A V B

¬A = ¬(judge (x)/p ^ ¬crooked x/q) = ¬ judge(x) V ¬(¬crooked (x)) (¬(p^q)=¬pV¬Q)

= ¬ judge(x) V crooked (x) ¬AVB=

Non – monotonic Reasoning:

In Non-Monotonic reasoning, in which the axioms and/or the rules of inference are extended to make it possible to reason with incomplete information. These systems preserve, however, the property that, at

any given time, a statement is either believed to be true, belived to the false, or not believed to be either.

Inferential Monotonicity Property

(P,Ai), {Ai,Ai+1, ………, An} | --p→{Ai, …….An, An+1}1--p

Monotonic Systems:

Respects Inferential monotonicity.

FOL(First Order Logic) with modus ponens (or) Resolution is monotonic Strict semantic net inheritance is monotonic.

Non-Monotonic Systems:

Do not respect Inferential Monotonicity.

“Most (x)” Quantifier leads to non-monotonicity. Default reasoning is non-monotonic. Semantic net inheritance with exceptions is non-monotonic.

If P, then Q is a hypothetical proposition. Then the First half of a hypothetical proposition is known as

Antecedent and second half is known as consequent P is Antecedent and Q is consequent.

E.g. 2 if x is a men, then x is mortal. “X is a man” is the Antecedent of the proposition.

(i) Modes ponens rule written in sequent nota tion as P→Q, P├ Q. It means, P→Q is True and P is True then

Q must be True.

¬judge(x) V crooked(x) V well educated(x)

Wff’s = well-formed formulas

Page 40: Ai i Complete Notes by Murali Krishna

Page 40

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

(ii) Modes ponens written in rule form as P→Q,P/Q

E.g. If today is Tuesday, then I will go to work. Today is Tuesday.Therefore, I will go to work.

Justification thru Truth Table

P Q ¬P P→Q(or)/¬PVQ

T T F T

T F F F

F F T T

F T T T

P→Q is T

P is T

. . Q is T known as Modus ponens.

A Truth Table is a mathematical Table used in logic-specifically in connection with Boolean Algebra,

Boolean functions, and propositional calculus to compute the functional values of logical expressions.

(i) Logical Negation (¬) or ~ :

P ¬P

T F

F T

(ii) Logical Conjunction (^) or (&):

P Q P^Q

T T T

T F F

F F F

F T F

Page 41: Ai i Complete Notes by Murali Krishna

Page 41

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

(iii)Logical Disjunction (V) or (||):

P Q PVQ

T T T

T F T

F F T

F T F

(iv) Logical Implication:

P → Q = ¬ P V Q (P implies Q)

P Q P→Q

T T T

T F F

F F T

F T T

(v) Logical Equality (≡)

P Q P^Q

T T T

T F F

F F T

F T T

(vi) Exclusive Disjunction (XOR)

P Q = P Q1 +QP1.

P Q Q1 P1 PQ1 QP1 P Q

T T F F F F F

T F T F T F T

F F T T F F F

F T F T F T T

Page 42: Ai i Complete Notes by Murali Krishna

Page 42

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

(vii) Logical NAND (NOT AND) ↑

P Q P^Q !(P^Q) (or) P↑Q

T T T F

T F F T

F T F T

F F F T

(viii) Logical NOR (NOT OR) (↓):

P Q P^Q P↓Q

T T T F

T F T F

F T T F

F F F T

A Non-Monotonic logic is a formal logic whose consequence relation is not monotonic.

A Monotonic logic cannot handle various reasoning tasks such as

(i) Reasoning by default (Consequences may be derived only because of lack of evidence of the

contrary)

(ii) Abductie Reasoning: Consequences are only deduced

(iii)Belief Revision (New Knowledge may contradict old beliefs)

STASTICAL & PROBABILITSTIC REASONING

Need for probabilistic Reasoning:

Most everyday reasoning is based on uncertain evidence and inferences.

Classical logic, which only allows conclusions to be strictly true or strict ly false, does not

account for this uncertainty.

Early expert systems employed fairly adhoc methods for reasoning under uncertainty and for

combining evidence.

Page 43: Ai i Complete Notes by Murali Krishna

Page 43

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Axioms of probability theory:

All probabilities ‘0’ and ‘1’ 0<=P(A)<=1

True proposition has probability ‘1’, false has probability ‘0’

P(true)=1, P(false)=0

The probability of disjunction is

P(AVB) = P(A) + P(B) – P(A^B)

Conditional Probability:

P(A|B) is the probability of A given B.

Assumes that B is all and only information known. Defined by P(A|B) = P(A^B)/P(B).

Independence:

A and B are independent if

P(A|B) = P(A)

P(B|A) = P(B)

Therefore, if A and B are independent

P(A|B) = P(A^B)/P(B) = P(A)

P(A^B)=P(A) P(B)

Classification (Categorization):

Given:

A Description of an instance, x€X, where X is the instance space.

A fixed set of categories C = {c1,c2,……Cn}

Determine:

The category of x: c(x) €C, where c(x) is a categorization function whose domain is X and

whose range is C

If C(x) is a binary function C = ({0,1},{true, false},{positive,negative}) then it is called a

concept.

Page 44: Ai i Complete Notes by Murali Krishna

Page 44

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Baye’s Theorem:

P(H|E) = P(E|H) P(H)/P(E) where P(E) probability of evidence & P(H)probability of hypothesis.

Simple proof from the definition of conditional probability.

P(H|E) = P(H^E)/P(E) (def . cond. Prob)

P(E|H) = P(E^H)/P(H) = P(H^E)/P(H) (. Def.cond. prob)

P(H^E) = P(E|H) P(H)

Application on Bayes Theorem:

Suppose there is a school having 60% boys & 40 % girls as students. The female students wear

Trousers or skirts in equal numbers. The boys all wear trousers. An observer sees a student is wearing

trouser. What is the probability that this student is a girl?

Ans: Event A is that student observed is a girl.

Event B is that the student observed is wearing trousers.

We have to compute P (A|B).

For this P(A) i.e. the probability of the student is a girl regardless of any other information.

Fraction of girls among the students is 40% the probability equals 0.4.

P(B|A), i.e probability of student wearing trousers given that the student is a gril. As they are

as likely to wear skirts as trousers, this o.s.

P(B), i.e. the probability of a student wearing trousers regardless of any other information 60%

+ 20% = 80%=0.8%

P(A|B) = P(B|A) P(A)/p(B)= 0.5 X 0.4/0.8=0.25.

Another method for solution:

Assume, for concrete, that there are 100 students, 60 boys and 40 girls. Among these 60 boys and 20

girls wear trousers. All together there are 80 trouser wearers, of which 20 girls.

A chance of getting random trouser wearer girl is equal to 20/80=1/4=0.25

Proving Bayes theorem by

Derivation from conditional probabilities.

P(A|B)=P(B|A)P(A)/P(B)

Page 45: Ai i Complete Notes by Murali Krishna

Page 45

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Proof: P(A|B) = P(A∩B)/P(B) (From def. of conditional probability)

The probability of event B given event A is,

P(B|A)= P(B∩A)/P(A)=P(A∩B)/P(A)

P(A|B) P(B)= P(A∩B) (from 1)

= P(B|A) P(A) (from 2)

Hence proved.

Bayesian Networks:

The main idea is that to describe the real world, it is not necessary to use a huge joint

probability table in which we list the probabilities of all conceivable combinations of events.

There are two different ways that propositions can influence each other. The first is that causes influence the

likelihood of their symptoms. The second is that observing a symptom affects the likelihood of all its possible

causes.

The idea behind the Bayesian network structure is to make a clear distinction between these two kinds of

influence.

Attribute Probability

P(Wet|Sprinkler, Rain) 0.95

P(Wet|Sprinkle,¬ Rain) 0.9

P(Wet|¬Sprinkler, Rain) 0.8

P(Wet|¬Sprinkler, ¬Rain) 0.1

P(Sprinkler| Rainy Season) 0.0

P(Sprinkler|¬ Rainy Season) 1.0

P(Rain| Rainy Season) 0.9

A(A|B) = P(B|A) P(A)/P(B)

Sprinkler Rain

Wet

Raing Season

Rain Sprinkler

Wet

Page 46: Ai i Complete Notes by Murali Krishna

Page 46

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

P(Rain|¬ Rainy Season) 0.1

P(Rainy Season) 0.5

Dempster-Shafer Theory:

This new approach considers set of propositions and assigns to each of then an internal

[Belief, plausibility], in which the degree of belief must lie.

Belief (Usually denoted Bel) measures the strength of the evidence in favor of set of propositions. It ranges

from (zero) (indicating no evidence) to ‘1’ (denoting certainy)

Plausibility (pl) is defined to be

Plausibility also ranges from 0 to 1 and measures the extent to which evidence in favor of ¬ s leaves

room for belief in S. In particular if we have certain evidence in favor of ¬s then bel (¬s) will be 1 and

pl(s) will be 0.

Fuzzy Logic:

Def:

Superset of conventional (Boolean) logic that has been extended to handle the concept of

partial truth.

Central notion of fuzzy systems is that values are indicated by a value on the range[0.0,1.0],

with 0.0 (zero to zero) representing absolute falseness & 1.0 representing absolute truth.

Applications

Expert systems

Automatic Transmissions

Video Cameras

Robotics

Decision Analysis

Comparison to various Logics:

In bivalent logic, BL, Truth is bivalent, implying that every proposition, P is either true or false,

with no degrees of truth allowed.

In multivalent logic, ML, Truth is matter of degree.

In Fuzzy logic, FL

Pl(S) = 1-Bel (¬S)

Page 47: Ai i Complete Notes by Murali Krishna

Page 47

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Everything is (or) is allowed to be, imprecise (approximate)

Everything is, or is allowed to be, granular (linguistic)

Another definition of Fuzzy logic:

Fuzzy logic is aimed to form reasoning which are approximate rather than exact.

Example:

Exact All men are moral

Socrates is a man

Socrates is mortal

Approximate Most Swedes are tall

Magnus is a swede

It is likely that magnus is tall ( May be)

Fuzzy logic was developed by Zadeh in 1979. The statements those are used in Fuzzy logic are called

Fuzzy sets or Zadeh set

Declarative Representation:

Architectures with declarative representations have knowledge in a format that may be manipulated,

decomposed and analyzed by its reasoners.

A Classical example of declarative representation is logic.

True/False

Yes/No 1/0

White/Black

Approximate/possible/maybe

Page 48: Ai i Complete Notes by Murali Krishna

Page 48

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Procedural Representation

Architectures with procedural representations encode how to achieve a particular goal(result).

Advantages of procedural knowledge are possibly faster usage.

WORK BOOK (ANSWER THE FOLLOWING QUESTIONS)

1. What is Baye’s theorem? Explain its usage in reasoning?(Nov,2008(new))

2. Explain the sequence of steps to convert a well formed formula (wff) in predicate logic to clause form?(Nov,2008(old))

3. Differentiate between declarative representation and procedural representation?(May,2008(new))

4. What is meant by non-monotonic reasoning? Explain it?(May 2008,(new))

5. Compare and contrast the statistical and probabilistic reasoning?(May,2008,(new))

6. Explain the need for UNIFICATION with suitable example(Nov,2008(old))

7. Discuss about natural deduction? (Nov,2008(old))

Page 49: Ai i Complete Notes by Murali Krishna

Page 49

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

UNIT-IV

PROLOG

Prolog is a computer programming language, used for solving the problems that involve objects and relationships between objects.

Computer programming in prolog consists of,

Declaring some facts about objects and their relationships. Defining some rules about objects and their relationships.

Asking questions about objects and their relationships.

FACTS

Facts are the statements that contain objects and their relationship Eg: John likes Mary

Obj1 Obj2 Relationship

In prolog, we write facts in standard form as Likes(John, Mary)

Important things to be maintained while writing facts are

1. The names of all relationships and objects must begin with lowercase letter, such as likes, john , mary

2. The relationship is written first, and the objects are written separated by commas, and the

objects are enclosed by a pair of round brackets. 3. The full stop character “.”, must come at the end of a fact.

Examples of some facts and their meanings.

1) Valuable(gold). Gold is valuable 2) Female(lakshmi). Lakshmi is female

3) Owns(john,mary). John owns gold 4) Father(john,mary). John is the father of mary 5) Gives(john,book,mary). John gives the book to mary

The name(s) of the objects that are enclosed with in the round brackets in each fact are called

“arguments”. The name of the relationship, which comes just before the round brackets is called “predicate”. Relationship can have arbitrary number of arguments. If we want to define a predicate called

play,where we mention two players and a game they play each other,we need three arguments.

Eg: Play (john,mary,football)

Page 50: Ai i Complete Notes by Murali Krishna

Page 50

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

In prolog, A collection of facts is called a database.

QUESTIONS

Once we have some facts, we can ask some questions about them, in prolog , a question looks

like just like a fact,except that we put a special symbol for it. The Symbol is written as “question mark and a hyphen”

?- owns (mary, book). means does mary owns the book?

When a question is asked of prolog , it will search through database you typed in before. It prolog finds a fact that matches the question, prolog will respond ‘yes’.

If no such fact exists in the database, prolog will respond ‘no’.

Eg: consider the following database.

Likes(joe,fish). ?-likes(mary,joe). Likes(joe,mary). No Likes(mary,book). ?-likes(mary,book).

Likes(john,book). Yes.

Q: ? - likes(joe,maney). ?-king(john,france). A: no no

Greeks contain the following three facts Human (Socrates).

Human (Aristotle). Athenian (Socrates).

QUESTIONS

?-athenian(socrates). ?- greek(socrates). Yes no

When prolog answers no to a question, it means “not provable”

Variables

Eg: Does john like mary? Does john like books?

i.e does john like x? we don’t know what the object is that x could stand for.

In prolog we can not only name particular objects, but we can also use names like x to stand for objects to be determined by prolog. Names of this second kind are called variables.

When prolog uses a variable, the variable can be either instantiated or not instantiated.

A variable is instantiated when there is an object that the variable stands for. Prolog can distinguish variables from names of particular objects because any name beginning

with capital letter is taken to be a variable.

Page 51: Ai i Complete Notes by Murali Krishna

Page 51

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

E.g.:

We cannot name something that john likes as an objects, so prolog adopts a way of saying this. ?- likes(john,something that john likes).

Instead of asking a question like above, prolog uses the variable ?-likes(john,x).

E.g.: Consider the following database Likes(john,flowers)

Likes(john,mary) Likes(paul.mary)

Q: ?-likes(john,x)

The question asks, is there anything that john likes. Prolog will respond x=flowers(x=flowers)

Important Note:

After X=flowers instantiated.

If we type the computer terminal’s RETURN Key, meaning you are satisfied with just one answer, then prolog will stop searching for more.

If instead you type the ‘;’ key (followed by RETURN),prolog will resume its search through the database as before , starting from where it left the place marker.

E.g.: Likes(john, flowers).

Likes(john, mary). Database. Likes(paul, mary)

Q: ?-likes(x, Mary). Is there an object that likes mary?

?-likes(x,mary). X=john our question x=paul; first answer. We type ‘;’ in reply.second answer. again type ‘;’.

No No more answer

Conjunctions

In order to answer questions about more complicated relationship, such as, Do John and mary like each other?

One way to do this, would be first to ask If john likes mary, and if prolog says yes, then we ask if mary likes john. So the original

problem consists of two separate goals that the prolog system must try to satisfy.

Page 52: Ai i Complete Notes by Murali Krishna

Page 52

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Database:

Likes(mary,food).

Likes(mary,wine). Likes(john,wine).

Likes(john,mary). Asks

Does john like mary? And does mary like john? The and expresses conjunction and is rendered by,(comma).

The comma is pronounced as “and” serves to separate any no. of diffent goals that have to be

specified in order to answer a question.

The answer for the given question is no. because the first goal is true and second goal is no.

since we wanted to know if they both like each other, the whole question is answered no.

Q2: Is there anything that john and mary both like? This consists of two goals.

i) Find out if there is some x that may likes. ii) Then, find out if it john likes whatever x is .

A: In prolog, using conjunction the above question can be written as ?-likes(mary,x),likes(john,x).

?- likes(john,x).

Food Food Likes(mary,food).

Likes(mary,wine). Likes(john,wine).

Likes(john,mary). 1. The first goal succeeds, instantiating x to food. 2. Next, attempt to satisfy the second goal.

?-, likes(john,x).

Food Food Likes(mary,food). Likes(mary,wine).

Likes(john,wine). Likes(john,mary).

3. The second goal fails. 4. Next back track:forget the previous x,and attempt to re_satisfy the firs t goal.

?-likes(john,mary),likes(mary,john).

likes(mary,x)

likes(mary,x)

Page 53: Ai i Complete Notes by Murali Krishna

Page 53

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

?-likes(mary,x), likes(john,x). Wine Wine

Likes(mary,food). Likes(mary,wine).

Likes(john,wine). Likes(john,mary). 5. The first goal succeeds again, instantiating x to wine.

6. Next attempt to satisfy second goal.

?-likes(mary,x), likes(john,x). Wine Wine Likes(mary,food).

Likes(mary,wine). Likes(john,wine).

Likes(john,mary). 7. The second goal succeeds. 8 .Prolog notifies you of success,and waits for a reply.

The behaviour, where prolog repeated attempts to satisfy and re-satisfy goals in a conjunction

is called backtracking. Prolog, must “uninstantiate” any variables that because instantiated at this goal. i.e undo all the variables.

Rules:

In prolog, rules are used when you want to say that “a fact depends on a group of other facts”.

In english, we use the word “IF” to express a rule. Eg:1. We want to state the fact that john likes all people.

Likes(john,alfred). Likes(john,charles). Likes(john,david).

For every in our database, we have to write separate fact, which become tedious, especially if

there are hundreds of people in our prolog program.

Another way to say that john likes all people is to say “john likes any object provided it is a person”. Instead of listing all the people john likes.

Several examples of rules:

1. I use an umbrella if there is a rain. 2. John buys the wine if it is less expensive than the beer. 3. X is a bird if x is an animal, and x has feathers.

4. X is a sister of y if x is female, and x and y have the same parents.

Hence, a rule is a “general statement about objects and their relationships”.

Page 54: Ai i Complete Notes by Murali Krishna

Page 54

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Examples with rules:

A rule using one variable and a conjunction. John likes anyone who likes wine. (or)

John likes anything if it likes wine, (or) with variables, John likes x if x likes wine.

In prolog, a rule consists of a head and a body. The head and body are connected by the symbol’:- ‘,which is made up of colon and hyphen.

The ‘:-‘ is pronounced as if. Eg: likes (john, x) :- likes(x, wine). Head body

A rule also ends with a dot.

The head of the rule likes(john,x), describes what fact the rule is intended to define.

The body of the rule likes(x,wine), describes the conjunction of goals that must be satisfied, one after the other, for the head to be true.

E.g.: 1. John likes anyone who likes wine and food.

Likes(john,x):- likes(x, wine),likes(x,food). 2. John likes any female who likes wine Likes(john, x) :- female(x),likes(x, wine).

Both the examples contains a single variable inside a rule, which is x.

A rule may also consists of more than one variable.

Eg: parents(x, y, z) means the parents of x and y and z. The second argument is the mother and third argument is the father.

Text: X is a sister of y, if

a) X is female, b) X has mother M and father as F and c) Y has the same mother and father as like x.

The text can be written prolog as a rule as,

Sister of(x,y):- female(x), parents(x,m,f),parents(y,m,f).

SYNTAX OF PROLOG:

The syntax of a language describes how we are allowed to fit words together.

In english “I see a zebra” is correct but “zebra see I a” is incorrect.

For exercise on rule refer pages 15, 16 in prolog text book.

Page 55: Ai i Complete Notes by Murali Krishna

Page 55

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Prolog syntax contains various parts. Prolog programs are built from TERMS.

A TERM is either a constant a variable or a structure. Each term is written as a “sequence of characters”.

Characters are divided into four categories.

i) A B C D E F ……………………………….X Y Z. ii) a b c d e f …………………………………x y z.

iii) 0 1 2 3 4 5 6 7 8 9 iv) + - * / \ ~ ^ < > : . ? @ # $ &

The first row (i) consists of upper case letters. The second row (ii) consists of lower case letters.

The third row (iii) consists of digits. The fourth row (iv) consists of sign characters.

Constants:

Constants name specific objects or specific relationships. There are two kinds of constants. Constants

atoms integers

Atoms are the names such as like, mary, john, wine, owns, book. Along with these even special symbols like ‘?-‘ and ‘:-‘.

Atoms are of two types (a) those made up of letters and digits and (b) those made up of signs.

a) The first kind normally begin with a lower-case letter.

b) If an atom is enclosed in single quotes “ ’ ” , then the atom may have any characters in its name.

c) The underline character “_” may be inserted in the middle of an atom to improve legibility. Some examples of atoms are a

Void ‘george_smith’

ieh 2004

prolog allows integers ranging from 0 to 16383.

VARIABLES:

The second kind of term used in prolog is the variable.

Variables look like atoms, except they have names beginning with a capital letter or underscore sign “_”.

Eg: answer input gross_pay _3_blind

?-likes(_,john) Anyone who likes means john, hence who stands as _ and is known as “anonymous

variable”.

Page 56: Ai i Complete Notes by Murali Krishna

Page 56

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

STRUCTURES:

The third kind of term with which prolog programs are written is the structure.

A structure is a single object which consists of a collection of other objects, called components.

The components are grouped together into a single structure for convenience in handling them.

Structures are sometimes called as “components terms” or “complex terms”.

A structure is written in prolog specifying its functor and its components. The functor names

the general kinds of structure , and corresponds to a data type in an ordinary programming language. The components are enclosed in round brackets and separated by commas. The functor is written just before the opening round brackets.

E.g. of a structure.

1. Owns (john, book(wuthering_heights,bronte)).

i.e. inside the owns fact, there is a structure book, which has two components, a title and an author. The meaning is john owns the book called weithering_height written by bronte.

2. Owns(john, book(wuthering_height,author(emily,bronte))). Indicates nested structure relationship.i.e john owns the book, title wuthering_heights,which was by

emily and bronte. ?-owns(john,book(x,author(y,bronte))).

If this is true, the variable x be instantiated to the book title and variable y be instantiated to one of the

author. ?-owns(john,book(_,author(_,bronte))). These are anonymous variables that do not “share” with each other.

CHARACTERS:

The names of constants and variables are built up from strings of characters.

Prolog recognizes two kinds of characters. 1. Printing characters

2. Non printing characters. 1. Printing characters cause a mark to appear on computer terminals display.

E.g.: (i). A B C D E F ……………………………….X Y Z.

(ii). a b c d e f …………………………………x y z. (iii). 0 1 2 3 4 5 6 7 8 9

(iv). !, “,#,$,%,&,’,(,),=,_,~,^,/,\.{,},[,],-,@,+,;,*,:,<,>,,,.,?. 2. Non printing characters do not cause a mark to appear, but cause an action to be carried out.

E.g. printing a blank space, beginning new lines of text

Page 57: Ai i Complete Notes by Murali Krishna

Page 57

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

OPERATORS:

Operators are form of syntax that makes some structures easier to read.

For example the arithmetic expression, “x+y*z” can be written as a structure in prolog as

+(x,*(y,z)).where + is addition operator,* is multiplication operator.

Prolog uses the basic four operators

+, -, *, /, !

Three things should be known about each operator.

a) Position

b) Precedence c) Associativity

a) Position:

The operators are written between their arguments, called as “INFIX” operators.

E.g.: x+y*z. Operators are written before the arguments, called as “PREFIX” operators.

E.g. : x+y, where hyphen(-) before x is used in arithmetic to denote negation.

Some operators are written after the argument(s), known as “POSTFIX” operators. E.g. (x! exclamation sign is used for denoting factorial).

So the position of an operator tells where it is written with relationship to its arguments.

b) Precedence:

When using operators, we need to known the order in which operations are carried out, known

as precedence. Prolog follows BODMAS rule. E.g.: x+y*z, +(x,*(y, z))

a-b/c, -(a,/(b,c)) c ) Associativity:

When we use several operators of the same precedence, the execution of those operators can be

explained by associativity.

E.g. 8/2/2

Is this expression calculated as “(8/2)/2” or “8/(2/2)”?

If it is (8/2)/2,the value is 2 and if it is 8/(2/2),the value is 8.

The user has to distinguish whether the operator is left associative or right associative.

By default, all the arithmetic operations are left associative. i.e. 8/4/4 is treated as (8/4)/4.

5+8/2/2 means 5+ ((8/2)/2).

Page 58: Ai i Complete Notes by Murali Krishna

Page 58

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Hint:

As it is difficult to understand the rules of precedence associativity, users are advised to use round brackets for solving the arithmetic expressions.

EQUALITY AND MATCHING:

Equality, is an infix operator written as”=”.?- x=y pronounced as “x equals y”, prolog attempts to match x and y , and the goal succeeds if they match.

A goal of the form x=y, where x and y are any two terms which are permitted to contain

uninstantiated variables. The rules for deciding whether x and y are equal are as follows.

1. If x is an uninstantiated variable, and if y is instantiated to any term, then x and y are equal. Also, x will become instantiated to whatever y is.

2. Integers and atoms are always equals to themselves.

Police man=police man succeeds. Paper=pencil fails

1066=1066 succeeds. 1206=1583 fails

3. Two structures are equal if they have same functor and number of components, and all the

corresponding components are equal. Rides (clergyman, bicycle)=rides(clergyman, x) The goal succeeds, and causes x to be instantiated to bicycle.

4. Equal (x, y):- x=y.

Prolog provides a predicate “\=” pronounced as “not equal” the goal x\=y succeeds if x=y fails, x\=y means x cannot be made equal to y.

ARITHMATIC:

Arithmetic operations are useful for comparing numbers and for calculating results.

The following relational infix operators are

x=y x and y stand for the same number. x\=y x and y stand for different numbers. x<y x is less than y

x>y x is greater than y x=<y x is less than or equal to y

x>=y x is greater than or equal to y.

Page 59: Ai i Complete Notes by Murali Krishna

Page 59

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

e.g. Density (x,y):-pop (x, p), area(x,a), y is p/a.

The rule can be read as The population density of a counting x is y, if:

The population of x is a, and The area of x is a, and Y is calculated by dividing p by a.

“IS” operator is new. It’s an infix operator. Its right-hand argument is a term which is interpreted as an

arithmetic expression.

The left argument y is unknown The values of right variables of “is” must be known.

The arithmetic infix operators are

x + y sum of x and y x – y difference of x and y

x * y product of x and y x / y x divided by y x mod y the remainder of x divided by y.

SATISFYING GOALS:

Prolog performs a task in response to a question from the programmer. A question provides a conjunction of goals to be satisfied. Prolog uses the known clauses to satisfy the goals.

A fact can cause a goal to be satisfied immediately, where as a rule can only reduce the task to that of satisfying a conjunction of sub goals.

A cause can only be used if it matches the goal under consideration. If a goal cannot be

satisfied, backtracking will be initiated.

Backtracking consists of reviewing what has been done, attempting to re-satisfy the goals by

finding an alternative way to satisfy them. We can initiate backtracking by typing a semicolon.

Satisfaction of a Conjunction of Goals:

Prolog will not attempt to satisfy a goal until its neighbor on the left has been satisfied and when it has been satisfied, prolog will attempt to satisfy its neighbor on the right.

E.g. Parent(C, M, F) :- mother(C,M),father(C,F).

Mother (john, ann). Mother (mary, ann).

Father (mary,fred). Father (john,fred).

Page 60: Ai i Complete Notes by Murali Krishna

Page 60

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Q: Whether mary is a sister of john?

A: In order to answer the question, the following sequence of events are to be done successfully.

?- female(mary),parent(mary,M,F),parent(john,M,F).

i) Female(mary) Parent(mary,M,F) A sequence of sub goals not yet satisfied Parent(john,M,F)

ii) Female(mary)

Parent(mary,ann,fred) The sequence of sub goals has been satisfied and the Parent(john,ann,fred) variables have been instantiated.

MATCHING:

The rules for deciding whether a goal matches the head of a clause are as follows. a) All variables are initially uninstantiated

b) An uninstantiated variable will match any object c) An integer or atom will match only itself. d) A structure will match another structure with the same functor and number of arguments, and

all the corresponding arguments must match.

DATA STRUCTURES:

RECURSION:

Recursion means “A backward movement, return” as per Oxford English dictionary.

This definition is outdated and it is cryptic. Now a day’s recursion is popular and powerful technique in the world of non-numeric

programming.

Recursion is used in two ways.

i) Used to describe structures that have other structures as components ii) Used to describe programs that need to satisfy a copy of themselves before they themselves can

succeed.

Structures and Trees

It is easy to understand the complicated structure representation, as tree. i.e., complicated structure can be represented as a tree.

In a tree, each functor is rendered as a node, and the components are branches

Each branch may point to another structure, i.e structure with in structure

Page 61: Ai i Complete Notes by Murali Krishna

Page 61

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

E.g. (1) The structure parents(Charles, Philip, Elizabeth) is written as tree.

Parents

Charles Elizabeth Philip

E.g. (2) a+b*c structure can be written as tree a+b*c = +(a,*(b,c))

+

a *

b c

E.g. (3) Structure book(moby_dick, author(herman, Melville)) is written as tree

Book

Mody_dick author

herman

A sentence “john likes mary” the syntax of the sentence can be represented as tree

Simple syntax of English is that a sentence consists of a noun by a verb phrase. Additionally, a

verb phrase consists of a verb and another noun

Sentence (noun(x), verb_phrase(verb(y),noun(z))) can be written as tree

Sentence

Noun Verb_phrase

X Verb Noun

Y Z

Page 62: Ai i Complete Notes by Murali Krishna

Page 62

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Eg. John likes mary Sentence

Noun Verb_phrase

John verb noun

Likes mary

LISTS AND OPERATIONS

A list is a common data structure in non-numeric programming. The list is an ordered sequence of elements that can have any length.

The elements of a list may be

Constants

Variables

Structures

Lists can represent any kind of structure, used in symbolic computation.

Lists are widely used in representing parse trees, grammars, city maps, computer programs, and mathematical entities such as graphs, formulae and functions.

LISP is a programming language which uses mostly lists.

In LISP, the only data structures available are the constant and the list.

Where as in PROLOG, a list is simply one particular kind of structure. Lists can be represented as a special kind of tree

A list is either an empty list, having no elements or it is a structure that has two components. Those are head and tail.

The end of a list represented as a tail that is set to empty list.

The empty list is written as [ ]. The head and tail of a list are components of the functor named “.”, which is dot.

E.g. (1). List consisting of one element “ a” is,

. (a,[]) and a tree format looks like

a [ ]

E.g.(2) : List consisting of atoms a,b, and c is written as

. (a, . (b, . (c,[]))). and a tree format looks like

a b

c [ ]

Page 63: Ai i Complete Notes by Murali Krishna

Page 63

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

People like to write the tree diagram of a list with the tree “growing from left to right” and with “branches” hanging down.

[ ]

a b c

The list notation consists of elements of the list separated by commas and the whole list is enclosed in sequence brackets.

E.g. i) List consisting of single element, as [ a ] ii) [ a,b,c]

iii) [ ] empty list iv) [ a,v1,b,[x,y]] [ ]

a vi b [ ]

x y

A common operation with lists is to split a list into its head and tail. The special notation in PROLOG to represent

“ The list with head X and tail Y” is written as [ X | Y ], where the symbol separating X and Y is the vertical bar.

P([1,2,3]). P is a pattern containing list elements 1,2,3 P([the, cat, sat,[on, the , mat]]).

?- p([x|y]).

X=1 and y =[2,3]; X= the and y= [cat, sat,[on, the , mat]]

?- p([_ , _, _ , [ _ |x]]). X = [the, mat]

E.g.: Lists with their head and tail.

E.g. no LIST HEAD TAIL

1 [a, b, c] a [b, c]

2 [ ] (none) (none)

3 [[the, cat],sat] [the, cat] [sat]

4 [the, [cat, sat]] the [[cat, sat]]

5 [ X+Y , x+y] X+Y [x+y]

Page 64: Ai i Complete Notes by Murali Krishna

Page 64

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

WORK BOOK (ANSWER THE FOLLOWING QUESTIONS)

1. Explain the terms in PROLOG (a) facts (b) variables (c) conjunctions (d) rules with examples (Nov,2008(old)

2. List various mathematical operators in PROLOG. Give examples of each(May,2008(new))

3. Write a recursive function to find the factorial of a Number ‘N’ using PROLOG (May,2008(new))

4. Write PROLOG code for (a) to create a LIST (b) to insert an element into LIST (c) to delete an

element from LIST (d) to search for an element (Nov,2008(new))

5. What are the built- in operators in PROLOG. Explain with examples. (May,08(old))

6. Write a PROLOG program for computing Fibonacci series (Nov,08(old))

7. Write a PROLOG program to sort the numbers in ascending order (Nov,08(new))

Page 65: Ai i Complete Notes by Murali Krishna

Page 65

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

UNIT – V

BACKTRACKING:

1. An attempt can be made to satisfy a goal. When we satisfy a goal, we search the database

from the top. During the search process, two things can happen.

a) A matching fact (or rule) can be found. Then we can say the goal has been matched. We mark the place in the database and instantiate any previously un instantiated variables that have matched.

b) No matching fact (or rule) can be found. Then we can say goal has failed. We can attempt to re-satisfy the goal in the box above the arrowhead. If the original goal appears in a

conjunction, then this will be the goal on its left in the program.

2. We can attempt to re-satisfy a goal. We attempt to re-satisfy each of the goals in turn, the

arrow retreating up the page. If no sub goal can be re-satisfied in a suitable way. We attempt to find an alternative clause for the goal itself. In this case, we must make un instantiate any

variables that became instantiated when the previous clause was chosen. i.e. undoing all the work previously done by this goal.

E.g. Generating multiple solutions:

father (x,y) means the father of x is y father (mary, George) father (john,George)

father(sue,harry) father(george,Edward)

Q1: ? – father(x,y) This question will have several possible answers. It we prompt with a semicolon, prolog

will give the answer as, X=mary, y=George;

X=john, y= George; X=sue , y=harry; X=George, y=Edward;

No

Q2: ? – father (-, x). i.e. for which x is x a father?

A: X= George

X=George X=harry X=Edward

Page 66: Ai i Complete Notes by Murali Krishna

Page 66

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

The mechanism for finding multiple solutions to a given question is called as Backtracking.

THE “CUT”:

A special program that is used in prolog is CUT.

CUT allows us to tell prolog which previous choices it need not consider again when it

backtracks though the chain of satisfied goals.

CUT is important because of two reasons.

Program will operate faster because it will not waste time in attempting to satisfy goals

Program may occupy less of the of the computer’s memory space because more economical use

of memory can be made if backtracking points do not have to be recorded to later examination.

Example on CUT explained with Backtracking

Controlling backtracking

Automatic Backtracking can cause inefficiency.

Y 4

2 X

3 6 From the dig, the rules are

a) If X<3 then Y=0

b) If 3<=X and X<6 then Y=2 c) If 6<=X then Y=4

The relation f(x,y) in prolog would be F(x,0) :-X<3 F(x,2):-3<=x,x<6

F(x,4):-6<=x. The procedure assumes that before f(x,y) is executed, x is already instantiated to a number.

The goal:”f(1,y), 2<y” fails, but before prolog replies ‘no’, it tries all ‘3’ rules. The three rules are mutually exclusive so that one of them at most will succeed. If the goal

matches the first rule and then fails, there is no point in trying the others.

The cut mechanism will help us to prevent this.

CUT

A “Cut” prevents backtracking from some point on. Written as a ‘1’ sub goal that always succeeds, but prevents backtracking through it.

Correcting the example. f(x,0) :-x<3,!

f(x,2) :- 3<=x, x<6, ! .

f(x,4) :- 6<=x. whenever the goal f(x,y) is encountered, only the first rule that matches will be tried.

If we now ask again “ f(2,y), 2<y.” , we will get the same answer , ‘no’, but only the first rule of ‘f’ will be tried.

Page 67: Ai i Complete Notes by Murali Krishna

Page 67

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

THE MEANING OF CUT

When matching a goal G to a rule H :- Body, G is called the “parent goal”. When the cut is encountered as a goal it succeeds immediately, but it commits the system to all

choices made between, “the time the parent goal was invoked and the time the cut was encountered”

H :- B1,B2, …………. Bm, !,………… , Bn. When the ! is encountered, the solution to B1, B2,………..Bm is frozen , and all other possible

solutions are discarded.

The parent goal cannot be matched to any other rule.

Examples using CUT The maximum of two elements

max(X, Y, Max) , i.e., Max is the maximum of X and Y.

max(X, Y, X) :- X >=Y. max(X, Y, Y) :- X< Y.

A more economical solution (using “otherwise” logic) is

max( X, Y, X) :- X>=Y, !. max( X, Y, Y).

COMMON USES OF CUT

The common uses of CUT are into 3 main areas.

(a) The first concerns that we want to tell the PROLOG system “CUT” has found the right rule for a particular goal.

(b) CUT is going to tell the PROLOG system to fail a particular goal immediately without trying for alternative solution

(c) We want to terminate the generation of alternative solutions through backtracking. i.e., to found the only solution to the problem, and there is no point in ever looking for alternatives.

INPUT AND OUTPUT OPERATIONS There is a database of world events in 16th century arranged as facts containing dates and headlines.

Atoms begin with an upper-case letter, enclosed in single quotes. Event (1505, [ ‘Elucid’, translated, into, ‘Latin’]).

Event(1523, [‘christian’, ‘II’, flees, from, ‘Denmark’]).

Page 68: Ai i Complete Notes by Murali Krishna

Page 68

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

?- event (1505, X)

X= [Elucid, translated, into , Latin]

Consider the predicate “WHEN” When (X,Y) succeeds if X is mentioned in year Y When (X,Y) :- event(Y,Z), member(X,Z).

?- when(‘Denmark’, D). D = 1523.

READING AND WRITING TERMS

The most useful way to print on computer termina l’s display is to use the build- in predicate WRITE.

Write(x) will cause the term to be printed out on the display

Built- in predicate “nl” is used to force all succeeding output to be printed on the next line of the display.

Built- in predicate “tab” is used print a number of blank spaces across the display screen Tab(x) succeeds only once, it causes the cursor on the display to move the right by x space

characters.

Predicate “pp” such that pp(x, y) prints out the list in a helpful way. Pp means “pretty print”.

Using “pp” predicate we can print the elements in a vertical column. Eg.(i) The list [1,2,3] is pretty printed as

1 2

3 Eg (ii) The list [1,2,[3,4],5,6] is pretty printed as

1 2

3 4 5

6 i.e., remove the separated commas and square brackets. If the element of a list is structure, we do not

have to “get inside” structures to pretty-print their contents. Eg. Pp(X,I) :- tab(I), write(X), nl

Pp([H|T], I) :- !, J is I+3, pp(H,J),ppx(T,J),nl.

Ppx will print each element of the tail of the list all in the same column.

Page 69: Ai i Complete Notes by Murali Krishna

Page 69

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Difference between Display And Write

Write prints a term, as it takes into account what operator declarations have been made.

i.e.,we have declared an atom as an infix operator, then a term with this as a functor and two arguments will be printed out with the atom between the two arguments.

Display will behave as like write, except that it ignores any operator declarations that have been made.

E.g. ?- write(a+b*c*c), nl, display(a+b*c*c), nl.

Output: a+b*c*c write ouput +(a,*(*(b,c),c)) newline followed by display output

Yes READING TERMS

Read will read the next term that you type in from the computer terminal’s keyboard. The term must

be followed by a dot “.” And a non-printing character such a space or RETURN.

If X is un- instantiated, the goal read(X) will cause the next term to be read, and x to be

instantiated to the term

Using read & phh, we can write the prolog program to print historic headlines(phh) from the event database.

E.g. Hello:-

Phh([‘what’, date, do, you,’desire?’]), Read (D), Event (D,5),

Phh(5).

Q. ?-hello A prolog will print “what date do you desire?

READING AND WRITING CHARACTERS

The smallest entity that can be written and read is the character.

WRITING CHARACTERS

It can be done with predicate PUT(X) where X is instantiated to a character (represented as ASCII

code as an integer).

Page 70: Ai i Complete Notes by Murali Krishna

Page 70

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Eg(i) ?-put(104),put(101),put(108),put(108),put(111).

Output: hello Eg(ii) ?-put(104),put(105),nl,put(116),put(104),put(101),put(114),put(101).

Output: hi There

PRINT STRING

Printstring(X) will print the entire string of a list, where X to be instantiated to a list of character codes (a string).

E.g. (i) ?- printstring(“charles V adam at Denmark”). Output: Charles V adam at Denmark

In general put is used to print the head of the list and printstring used to print the tail of of the list.

READING CHARACTERS

Characters may read from keyboard using geto(X) and get(X). These goals will always succeed if their arguments are un-instantiated

geto(X) will instantiate X to the very next character that is typed, no matter what it is

get(x) will skip all non-printing characters and instantiate X to the first printing character.

BUILT-IN PREDICATES Built- in predicates provides facilities that cannot be obtained by definitions in Prolog purely

(OR) Built- in predicates provides convenient facilities just to save each programmer from having to define themselves.

E.g. CUT is treated as built- in predicate

The I/O predicates illustrates the fact that a built- in predicate may have side effects. i.e., satisfying a goal involving the predicate may cause changes apart from the instantiation of the arguments.

E.g. Consider the predicate “ <” (less than)

X<Y succeeds if the number X is less than the number Y. X<Y goal, will simply fail or produce error message, if X and Y are un- instantiated.

There are two basic built- in predicates for reading new clauses are “consult” and “reconsult”

Page 71: Ai i Complete Notes by Murali Krishna

Page 71

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

CONSULT(X):

Consult is meant for those situations when we want the clauses in some file to augment those already in the database.

The argument must be an atom giving the name of the file the clauses are to be taken from

E.g. ?- consult(myfile). ?-conult(‘/usr/john/pl/chat’)

?-consult(‘lib:iorout.pl’). Prolog reads through the file, adding the clauses the clauses it finds at the end of the database. As a

result, the new clauses will appear after already existing clauses for the same predicates.

Reconsult(X):

Reconsult is like consult, except that the clauses read in are taken to supersede all clauses for the same

predicate

Note: the meaning of supersede is useless / effectiveness / acceptance LIST NOTATION

It’s more convenient to specify consult and reconsult goals, especially when we want prolog to look at

more than one file. The notation involves simply putting filenames into a list, and giving that list as a goal to be satisfied

If we want a file to be consulted, we have to put the name into list as it is, and if we want a file to be

reconsulted, the precede the name with a “ –“ sign. E.g. ?- [file1, - file2, ‘fred.1’, -‘bill.2’].

It is equivalent to

?- consult(file1), reconsult(file2), consult(‘fred.1’),reconsult(‘bill.2’).

SUCCESS & FAILURE

Exciting a prolog program, a goal succeeds when it can be satisfied, and it fails when there is no way to satisfy it

The two predicates used for the above two results are true and fail predicates

Page 72: Ai i Complete Notes by Murali Krishna

Page 72

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

TRUE

The goal always succeeds. Clauses and goals can be reordered or recombined to obviate any use of true.

FAIL

The goal always fails. In two places, where fail is helpful. One place is “CUT fail” combination.

i.e., A conjunction of goals of the form

……………………….., !, fail. The conjunction fails due to the fail, and the parent goal fails because of CUT.

Second one, when we want another goal to backtrack through all solutions.

E.g. ?- event(X,Y),Phh(Y),fail.

CLASSIFYING TERMS

1. Var(x):

Eg.(i) ?-var(x) Yes

Eg.(ii) ?-var(23) No Eg(iii) ?- X=Y,Y=23,var(X)

No

2. Nonvar(x):

The goal nonvar(x) succeeds if x is not currently un- instantiated variable. The predicate nonvar is opposite to var. defined in prolog as

E.g. (i) nonvar(x) :- var(x), !, fail. E.g. (ii) nonvar(-).

3. Atom(x):

The goal atom(x) succeeds if x is currently stands for a prolog atom.

E.g. (i) ?- atom(23) No (ii) ?-atom(apples)

Yes (iii) ?-atom(‘/us/chris/pl.123’).

Yes (iv) ?- atom(“a string”). No

(v) ?- atom(x) No

Page 73: Ai i Complete Notes by Murali Krishna

Page 73

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

4. Integer(x)

The goal integer(x) succeeds if x is currently stands for an integer(a whole number). E.g. (i) ?-integer(23)

Yes E.g. (ii) ?-integer(1.4)

No

5. Atomic(x)

The goal atomic(x) succeeds if x is currently stands for either an integer or an atom. E.g.(i) atomic(x) :- atom(x). Atomic(x):- integer(x).

6. Float(x)

The goal float(x) succeeds if x is currently instantiated to a floating point number.

7. Number(x)

The goal number(x) succeeds if x is currently instantiated to an integer or a floating point number.

8. List(x)

The goal list(x) succeeds if x is currently instantiated to a list, i.e., an atom [ ] (empty list) or a

term with principle. Functor ‘.’ /2 and with second argument (the tail) a list.

EQUALITY

Built- in predicates for testing and making things equal in prolog

(i) X =Y

Goal X=Y, it attempts to make X and Y equal by matching them together. If it can match them, goal succeeds otherwise fails.

(ii) X \=Y i.e. X is not equal to Y

X\=Y is opposite to X = Y X \= Y succeeds if X=Y fails and vice versa X \=Y:-X=Y, ! , fail.

X \= Y.

(iii) X ==Y “==” is much stricter equality test than “=”

Page 74: Ai i Complete Notes by Murali Krishna

Page 74

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

EVALUATING ARITHMATIC EXPRESSIONS

(i) X is Y (ii) X-Y (iii) X+Y (iv) X*Y (v) X/Y (vi) XmodY

COMPARING NUMBERS

(i) X=Y (ii) X \=Y (iii) X < Y (iv) X > Y (v) X>=Y (vi) X =<Y

COMPARATIVE STUDY OF LISP & PROLOG FEATURES

FEATURE PROLOG LISP

Basis Unification-based Logic Lambda-based logics

Structures Predicates Lists

Symbols Atoms Atoms

Control Automatic User-defined

Type Declarative Functional

Compositional Yes Yes

Values Parameters to Procedures Function Values

Both languages have their roots in logic, manipulate words(atoms) and not characters, are de-compositional (top-down), have procedure- like units(predicates and functions) and can modify themselves (eg. Add new data and code to a running program to enable the program to learn)

The differences between these two languages are

(i) Prolog uses predicates, eg. Happy(person), where as LISP uses lists, eg.(happy person). (ii) In Prolog, the programmer does not have to specify program control ( the language takes the

order of execution automatically), where as in LISP, as like in most programming languages the control must be specified by the programmer.

(iii) Prolog stores data in a declarative format(easy to read), where LISP uses a functional(procedural) approach which is harder to read.

(iv) PROLOG hints

(v) LISP hints

CODE MEANS

Cond t IF THEN

; (semicolon) Comment

Eql Equal

CODE MEANS

:- IF

% COMMENT

Page 75: Ai i Complete Notes by Murali Krishna

Page 75

ARTIFICIAL INTELLIGENCE – Prepared by T.MURALI KRISHNA, ASSOCIATE PROFESSOR, AURORA‘s SCHOOL OF COMPUTER SCIENCE & RESEARCH

Endp Empty

Nil Return no value

Setq Variable assignment

(vi) Data stored in PROLOG as facts

Eg. Father(john, ann). Father(mark,rob). mother(su, ann).

Mother(jo,rob). (vii) And in LISP, as lists of associated values as

Eg. (setq father ‘((john ann) (mark rob)) (setq mother ‘((su ann) (jo rob))

Both the lists and facts provide a means to store simple simple, static data about objects and their relationships

Structures like these would be used to build semantic nets and frames

(viii) Procedures are stored in PROLOG as E.g. Parent(P,C) :- father(P,C).

Parent (P,C):- mother(P,C). And in LISP as

(defun parent(P C) (cond t ((member (cons P ( cons C nil)) father: test# ‘equal)t)

(t nil))) Its found that Prolog slightly easier to read . This is because it is known as a declarative

language which sets out, deliverately, to make the meaning clear.

Both languages are extremely powerful, LISP is commonly used in USA and PROLOG is used in Europe.

WORK BOOK (ANSWER THE FOLLOWING QUESTIONS)

1. Compare LISP & PROLOG features with examples

2. Explain the use of backtracking in PROLOG

3. Explain the operation of ‘CUT’ in PROLOG

4. Discuss about built- in predicates in PROLOG