0% found this document useful (0 votes)
32 views143 pages

What is Artificial Intelligence

The document provides an overview of Artificial Intelligence (AI), its importance, techniques, and various problem-solving approaches. It discusses the different categories of AI, including systems that think and act like humans, and outlines the goals and contributions of AI across various fields. Additionally, it highlights the applications of AI in real life, the classification of AI tasks, and the branches of AI research.

Uploaded by

Batool 2002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views143 pages

What is Artificial Intelligence

The document provides an overview of Artificial Intelligence (AI), its importance, techniques, and various problem-solving approaches. It discusses the different categories of AI, including systems that think and act like humans, and outlines the goals and contributions of AI across various fields. Additionally, it highlights the applications of AI in real life, the classification of AI tasks, and the branches of AI research.

Uploaded by

Batool 2002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 143

UNIT I

Overview of A.I: Introduction to AI, Importance of AI, AI and its


related field, AI techniques,Problems, problem space and search:
Defining the problem as a state space search, Productionsystem and
its characteristics, Popular Ai Problems- water Jug Problem,
Missionaries-Cannibal Problem, 8 Puzzle Problem.

A branch of Computer Science named Artificial Intelligence pursues


creating the computers or machines as intelligent as human beings.

What is Artificial Intelligence?


According to the father of Artificial Intelligence, John McCarthy,
itis “The science and engineering of making intelligent machines,
especially intelligent computer programs”.

Artificial Intelligence is a way of making a computer, a computer-


controlled robot, or a software think intelligently, in the similar
manner the intelligent humans think.

AI is accomplished by studying how human brain thinks, and how


humans learn, decide, and work while trying to solve a problem, and
then using the outcomes of this study as a basis of developing
intelligent software and systems.

AI is a study which makes the computers todo things which are, at


the moment, at thehands of human beings.

Views of AI fall into four categories:

 Thinking Humanly(The Cognitive approach)


 Acting Humanly(The Turing Test approach)
 Thinking Rationally(The Laws of Thought approach)
 Acting Rationally(The Rational Agent approach)

Systems that think like humans


―The exciting new effort to make computers think

… machines with minds, in the full and literal


sense.‖(Haugeland,1985)
The cognitive modeling approach
We need to get inside actual working of the human mind :

(a) through introspection – trying to capture our own thoughts as they


go by;
(b) through psychological experiments

Allen Newell and Herbert Simon, who developed GPS,the ―General


Problem Solver tried to trace the reasoning steps to traces of human
subjects solving the same problems The interdisciplinary field of
cognitive science brings together computer models from AI and
experimental techniques from psychology to try to construct precise
and testable theories of the workings of the human mind

Systems that think rationally


―The study of mental faculties through the use of computer models.
(Charniak and McDermont, 1985)

The “laws of thought approach”


The Greek philosopher Aristotle wasone of the first to attempt to
codify ―right thinking that is irrefutable reasoning processes. His
syllogism provided patterns for argument structures that always
yielded correct conclusions when given correct premises—for example,
Socrates is a man; all men are mortal; therefore Socrates is mortal.
These laws of thought were supposed to govern the operation of the
mind; their study initiated a field called logic.

Systems that act like humans


The art of creating machines that performsfunctions that require
intelligence when performed by People. (Kurzweil, 1990)

The Turing Test approach

o Test proposed by Alan Turing in 1950


o The computer is asked questions by a human interrogator.
The computer passes the test if a human interrogator, after posing
some written questions, cannot tell whether the written responses
come from a person or not. Programming a computer to pass, the
computer need to possess the following capabilities:

Natural language processing to enable it to communicate


successfully in English.

Knowledge representation to store what it knows or hears

Automated reasoning to use the stored information to answer


questions and to draw new conclusions.

Machine learning to adapt to new circumstances and to detect


and extrapolate patterns

To pass the complete Turing Test, the computer will need

 Computer vision to perceive the objects, and


 Robotics to manipulate objects and move about

Systems that act rationally


“Computational intelligence is the studyof the design of intelligent
agents. (Poole et al.,1998)

The rational agent approach


An agent is something that acts. Computer agents are not mere
programs, but they are expected to have the following attributes also:
(a) Operating under autonomous control,
(b) Perceiving their environment,
(c) Persisting over a prolonged time period,
(d) Adapting to change.
A rational agent is one that acts so as to achieve the best outcome.

Philosophy of AI
While exploiting the power of the computer systems, the curiosity of
human, lead him to wonder, “Can a machine think and behave like
humans do?”

Thus, the development of AI started with the intention of creating


similar intelligence in machines that we find and regard high in
humans.
Goals of AI
 To Create Expert Systems − The systems which exhibit intelligent
behavior, learn, demonstrate, explain, and advice its users.

 To Implement Human Intelligence in Machines − Creating systems


that understand, think, learn, and behave like humans.

What Contributes to AI?


Artificial intelligence is a science and technology based on disciplines
such as Computer Science, Biology, Psychology, Linguistics,
Mathematics, and Engineering. A major thrust of AI is in the
development of computer functions associated with human
intelligence, such as reasoning, learning, and problem solving.

Out of the following areas, one or multiple areas can contribute to


build an intelligent system.
Programming Without and With AI
The programming without and with AI is different in following ways −

Programming Without AI Programming With


AI

A computer program without AI can answer A computer program


the specific questions it is meant to solve. with AI can answer
the generic questions
it is meant to solve.

Modification in the program leads to change in its AI programs can


structure. absorb new
modifications by
putting highly
independent pieces of
information together.
Hence you can modify
even a minute piece
of information of
program without
affecting its structure.

Modification is not quick and easy. It may lead to affecting Quick and Easy
the program adversely. program modification.

The domain of artificial intelligence is huge in breadth and width.


While proceeding, we consider the broadly common and prospering
research areas in the domain of AI −
Speech and Voice
Recognition
These both terms are common in robotics, expert systems and
natural language processing. Though these terms are used
interchangeably, their objectives are different.

Speech Recognition Voice Recognition

The speech recognition aims at The objective of voice recognition is to


understanding and comprehending recognize WHO is speaking.
WHAT was spoken.

It is used in hand-free computing, map, It is used to identify a person by


or menu navigation. analyzing its tone, voice pitch, and
accent, etc.

Machine does not need training for This recognition system needs training
Speech Recognition as it is not speaker as it is person oriented.
dependent.
Speaker independent Speech Speaker dependent Speech Recognition
Recognition systems are difficult to systems are comparatively easy to
develop. develop.

Speaker independent Speech Speaker dependent Speech Recognition


Recognition systems are difficult to systems are comparatively easy to
develop. develop.

Working of Speech and Voice Recognition Systems


The user input spoken at a microphone goes to sound card of the
system. The converter turns the analog signal into equivalent digital
signal for the speech processing. The database is used to compare
the sound patterns to recognize the words. Finally, a reverse
feedback is given to the database.

This source-language text becomes input to the Translation Engine,


which converts it to the target language text. They are supported
with interactive GUI, large database of vocabulary, etc.

Real Life Applications of AI Research Areas


There is a large array of applications where AI is serving common
people in their day-to-day lives −

Sr.No. Research Areas Example

1 Expert Systems

Examples − Flight-tracking systems,


Clinical systems.

2 Natural Language Processing

Examples: Google Now feature, speech


recognition, Automatic voice output.
3 Neural Networks

Examples − Pattern recognition systems


such as face recognition, character
recognition, handwriting recognition.

4 Robotics

Examples − Industrial robots for moving,


spraying, painting, precision checking,
drilling, cleaning, coating, carving, etc.

5 Fuzzy Logic Systems

Examples − Consumer electronics,


automobiles, etc.

Task Classification of AI
The domain of AI is classified into Formal tasks, Mundane
tasks, and Expert tasks.
Task Domains of Artificial Intelligence

Mundane (Ordinary) Formal Tasks Expert Tasks


Tasks

Perception  Mathematics  Engineering

 Geometry  Fault Finding


 Computer Vision
 Logic  Manufacturing
 Speech, Voice
 Integration and  Monitoring
Differentiation

Natural Language Games Scientific Analysis


Processing
 Go
 Understanding
 Chess (Deep Blue)
 Language
 Ckeckers
Generation

 Language
Translation
Common Sense Verification Financial Analysis

Reasoning Theorem Proving Medical Diagnosis

Planing Creativity

Robotics

 Locomotive

Humans learn mundane (ordinary) tasks since their birth. They


learn by perception, speaking, using language, and locomotives. They
learn Formal Tasks and Expert Tasks later, in that order.

For humans, the mundane tasks are easiest to learn. The same was
considered true before trying to implement mundane tasks in
machines. Earlier, all work of AI was concentrated in the mundane
task domain.

Later, it turned out that the machine requires more knowledge,


complex knowledge representation, and complicated algorithms for
handling mundane tasks. This is the reason why AI work is more
prospering in the Expert Tasks domain now, as the expert task
domain needs expert knowledge without common sense, which can
be easier to represent and handle.

What is AI Technique?
Artificial Intelligence research during the last three decades has
concluded that Intelligence requires knowledge. In the real world , to
compensate overwhelming quality, knowledge possesses less
desirable properties.−

 Its volume is huge, next to unimaginable.

 It is not well-organized or well-formatted. It differs from data by being


organized in a way that corresponds to its application

 It keeps changing constantly.

 It is complicated.

 It is difficult to characterize correctly.

AI Technique is a manner to organize and use the knowledge


efficiently in such a way that −
 It should be perceivable by the people who provide it, even though for
many programs bulk of the data that comes automatically from readings.

 It should be easily modifiable to correct errors and reflect changes in real


conditions.

 It should be useful in many situations though it is incomplete or


inaccurate.

 In many AI domains, how the people understand the same people must
supply the knowledge to a program.

 It can be used to help overcome its own sheer bulk by helping to narrow
the range of possibilities that must be usually considered.

AI techniques elevate the speed of execution of the complex program


it is euipped with. In order to characterize an AI technique let us
consider initially OXO or tictac-toe and use a series of different
approaches to play the game.
The programs increase in complexity, their use of generalizations, and
the clarity of their knowledge and the extensibility of their approach.
In this way they move towards being representations of AI
techniques.

Applications of AI
AI has been dominant in various fields such as −

 Gaming − AI plays crucial role in strategic games such as chess, poker,


tic-tac-toe, etc., where machine can think of large number of possible
positions based on heuristic knowledge.

 Natural Language Processing − It is possible to interact with the


computer that understands natural language spoken by humans.

 Expert Systems − There are some applications which integrate


machine, software, and special information to impart reasoning and
advising. They provide explanation and advice to the users.

 Vision Systems − These systems understand, interpret, and


comprehend visual input on the computer. For example,

o A spying aeroplane takes photographs, which are used to figure


out spatial information or map of the areas.

o Doctors use clinical expert system to diagnose the patient.

o Police use computer software that can recognize the face of


criminal with the stored portrait made by forensic artist.
 Speech Recognition − Some intelligent systems are capable of hearing
and comprehending the language in terms of sentences and their
meanings while a human talks to it. It can handle different accents,
slang words, noise in the background, change in human’s noise due to
cold, etc.

 Handwriting Recognition − The handwriting recognition software


reads the text written on paper by a pen or on screen by a stylus. It can
recognize the shapes of the letters and convert it into editable text.

 Intelligent Robots − Robots are able to perform the tasks given by a


human. They have sensors to detect physical data from the real world
such as light, heat, temperature, movement, sound, bump, and
pressure. They have efficient processors, multiple sensors and huge
memory, to exhibit intelligence. In addition, they are capable of learning
from their mistakes and they can adapt to the new environment.

BRANCHES OF AI
A list of branches of AI is given below. However some branches are surely
missing,because no one has identified them yet. Some of these may be
regarded as concepts or topics rather than full branches.

Logical AI
In general the facts of the specific situation in which it must act, and its goals
are all represented by sentences of some mathematical logical language. The
program decides what to do by inferring that certain actions are appropriate for
achieving its goals.

Search
Artificial Intelligence programs often examine large numbers of possibilities – for
example, moves in a chess game and inferences by a theorem proving program.

Discoveries are frequently made about how to do this more efficiently in various
domains.

Pattern Recognition
When a program makes observations of some kind, it is often planned to
compare what it sees with a pattern. For example, a vision program may try to
match a pattern of eyes and a nose in a scene in order to find a face. More
complex patterns are like a natural language text, a chess position or in the
history of some event. These more complex patterns require quite different
methods than do the simple patterns that have been studied the most.
Representation
Usually languages of mathematical logic are used to represent the facts about
the world.

Inference
Others can be inferred from some facts. Mathematical logical deduction is
sufficient for some purposes, but new methods of non-monotonic inference have
been added to the logic since the 1970s. The simplest kind of non-monotonic
reasoning is default reasoning in which a conclusion is to be inferred by default.
But the conclusion can be withdrawn if there is evidence to the divergent. For
example, when we hear of a bird, we infer that it can fly, but this conclusion can
be reversed when we hear that it is a penguin. It is the possibility that a
conclusion may have to be withdrawn that constitutes the non-monotonic
character of the reasoning. Normal logical reasoning is monotonic, in that the
set of conclusions can be drawn from a set of premises, i.e. monotonic
increasing function of the premises. Circumscription is another form of
non-monotonic reasoning.

Common sense knowledge and Reasoning


This is the area in which AI is farthest from the human level, in spite of the fact
that it has been an active research area since the 1950s. While there has been
considerable progress in developing systems of non-monotonic reasoning and
theories of action, Yet more new ideas are needed.

Learning from experience


There are some rules expressed in logic for learning. Programs can only learn
what facts or behavior their formalisms can represent, and unfortunately
learning systems are almost all based on very limited abilities to represent
information.

Planning
Planning starts with general facts about the world (especially facts about the
effectsof actions), facts about the particular situation and a statement of a goal.
From these, planning programs generate a strategy for achieving the goal. In
the most common cases, the strategy is just a sequence of actions.

Epistemology
This is a study of the kinds of knowledge that are required for solving problems
in the world.
Ontology
Ontology is the study of the kinds of things that exist. In AI the programs and
sentences deal with various kinds of objects and we study what these kinds are
and what their basic properties are. Ontology assumed importance from the
1990s.

Heuristics
A heuristic is a way of trying to discover something or an idea embedded in a
program. The term is used variously in AI. Heuristic functions are used in some
approaches to search or to measure how far a node in a search tree seems to be
froma goal. Heuristic predicates that compare two nodes in a search tree to see
if one is better than the other, i.e. constitutes an advance toward the goal, and
may be more useful.

Genetic programming
Genetic programming is an automated method for creating a working computer
program from a high-level problem statement of a problem. Genetic
programming starts from a high-level statement of ‘what needs to be done’ and
automatically creates a computer program to solve the problem. It is being
developed by John Koza’s group.

UNDERLYING ASSUMPTION OF AI
Newell and Simon presented the Physical Symbol System Hypothesis, which lies
in the heart of the research in Artificial Intelligence.

A physical symbol system consists of a set of entities called symbols, which are
physical patterns that can occur as components of another entity called an
expression or symbol structure. A symbol structure is a collection of instances or
tokens of symbols related in some physical way. At any instant, the system will
contain a collection of these symbol structures. In addition, the system also
contains a collection of processes that operate on expressions to produce other
expressions;processes of creation, modification, reproduction and destruction. A
physical symbol system is a machine that produces an evolving collection of
symbol structures.

Physical Symbol System Hypothesis


A physical symbol system has the necessary and sufficient attributes for general
intelligent action. It is only a hypothesis and there is no way to prove it other
than on empirical validation. Evidence in support of the physical symbol system
hypothesis has come not only from the areas such as game playing but also
from the areas such as visual perception. We will discuss the visual perception
area later.
The importance of the physical symbol system hypothesis is twofold. It is the
major theory of human intelligence and also forms the basic belief that, it is
possible to build programs that can execute intelligent tasks now performed by
people.

Problems, Its Definition as State Space


Search

PROBLEM OF BUILDING A SYSTEM


To solve the problem of building a system you should take the following steps:
1. Define the problem accurately including detailed specifications and what
constitutes a suitable solution.

2. Scrutinize the problem carefully, for some features may have a central
affect on the chosen method of solution.

3. Segregate and represent the background knowledge needed in the


solution of the problem.

4. Choose the best solving techniques for the problem to solve a solution.

Problem solving is a process of generating solutions from observed data.


 a ‘problem’ is characterized by a set of goals,

 a set of objects, and

 a set of operations.
These could be ill-defined and may evolve during problem solving.

A
 ‘problem space’ is an abstract space.
 A problem space encompasses all valid states that can be generated by
the application of any combination of operators on any combination of
objects.

 The problem space may contain one or more solutions. A solution is a


combination of operations and objects that achieve the goals.
A ‘search’ refers to the search for a solution in a problem space.

 Search proceeds with different types of ‘search control strategies’.

 The depth-first search and breadth-first search are the two common
search strategies.
Problem definitions
A problem is defined by its ‘elements’ and their ‘relations’. To provide a formal
description of a problem, we need to do the following:

1. Define a state space that contains all the possible configurations of the
relevant objects, including some impossible ones.

2. Specify one or more states that describe possible situations, from which
the problem-solving process may start. These states are called initial
states.

3. Specify one or more states that would be acceptable solution to the


problem. These states are called goal states.
Specify a set of rules that describe the actions (operators) available.

The problem can then be solved by using the rules, in combination with an
appropriate control strategy, to move through the problem space until a path
from an initial state to a goal state is found.

This process is known as ‘search’. Thus:


 Search is fundamental to the problem-solving process.

 Search is a general mechanism that can be used when a more direct


method is not known.

 Search provides the framework into which more direct methods for
solving subparts of a problem can be embedded. A very large number of
AI problems are formulated as search problems.

 Problem space

A problem space is represented by a directed graph, where nodes represent
search state and paths represent the operators applied to change the state. To
simplify search algorithms, it is often convenient to logically and
programmatically represent a problem space as a tree. A tree usually decreases
the complexity of a search at a cost. Here, the cost is due to duplicating some
nodes on the tree that were linked numerous times in the graph, e.g. node B
and node D.

A tree is a graph in which any two vertices are connected by exactly one path.
Alternatively, any connected graph with no cycles is a tree.

Graph
Trees
Problem solving 

The term, Problem Solving relates to analysis in AI. Problem solving may be
characterized as a systematic search through a range of possible actions to
reach some predefined goal or solution. Problem-solving methods are
categorized as special purpose and general purpose.

A special-purpose method is tailor-made for a particular problem, often exploits


very specific features of the situation in which the problem is embedded.

A general-purpose method is applicable to a wide variety of problems. One


General-purpose technique used in AI is ‘means-end analysis’: a step-by-step,
or incremental, reduction of the difference between current state and final goal.

DEFINING PROBLEM AS A STATE SPACE SEARCH

State Space Search


A state space represents a problem in terms of states and operators that change
states.

A state space consists of:

 A representation of the states the system can be in. For example, in a


board game, the board represents the current state of the game.

 A set of operators that can change one state into another state. In a
board game, the operators are the legal moves from any given state.
Often the operators are represented as programs that change a state
representation to represent the new state.

 An initial state.

 A set of final states; some of these may be desirable, others undesirable.


This set is often represented implicitly by a program that detects terminal
states.

The Water Jug Problem


In this problem, we use two jugs called four and three; four holds a maximum of
four gallons of water and three a maximum of three gallons of water. How can
we get two gallons of water in the four jug?

The state space is a set of prearranged pairs giving the number of gallons of
water in the pair of jugs at any time, i.e., (four, three) where four = 0, 1, 2, 3
or 4 and three = 0, 1, 2 or 3.

The start state is (0, 0) and the goal state is (2, n) where n may be any but it is
limited to three holding from 0 to 3 gallons of water or empty. Three and four
shows the name and numerical number shows the amount of water in jugs for
solving the water jug problem. The major production rules for solving this
problem are shown below:
Initial condition Goal comment
1. (four, three) if four < 4 (4, three) fill four from tap
2. (four, three) if three< 3 (four, 3) fill three from tap
3. (four, three) If four > 0 (0, three) empty four into drain
4. (four, three) if three > 0 (four, 0) empty three into drain
5. (four, three) if four + three<4 (four + three, 0) empty three into four
6. (four, three) if four + three<3 (0, four + three) empty four into three
7. (0, three) If three > 0 (three, 0) empty three into four
8. (four, 0) if four > 0 (0, four) empty four into three
9. (0, 2) (2, 0) empty three into four
10. (2, 0) (0, 2) empty four into three
11. (four, three) if four < 4 (4, three-diff) pour diff, 4-four, into
four from three
12. (three, four) if three < 3 (four-diff, 3) pour diff, 3-three, into
three from four and a solution is given
below four three rule
Fig. 2.2 Production Rules for the Water Jug Problem

Gallons in Four Jug Gallons in Three Jug Rules Applied


0 0 -
0 3 2
3 0 7
3 3 2
4 2 11
0 2 3
2 0 10
Fig. 2.3 One Solution to the Water Jug Problem

The problem solved by using the production rules in combination with an


appropriate control strategy, moving through the problem space until a path
from an initial state to a goal state is found. In this problem solving process,
search is the fundamental concept. For simple problems it is easier to achieve
this goal by hand but there will be cases where this is far too difficult.

2.4 PRODUCTION SYSTEMS


Production systems provide appropriate structures for performing and describing
search processes. A production system has four basic components as
enumerated below.
 A set of rules each consisting of a left side that determines the
applicability of the rule and a right side that describes the operation to be
performed if the rule is applied.

 A database of current facts established during the process of inference.


 A control strategy that specifies the order in which the rules will be
compared with facts in the database and also specifies how to resolve
conflicts in selection of several rules or selection of more facts.

 A rule firing module.


The production rules operate on the knowledge database. Each rule has a
precondition—that is, either satisfied or not by the knowledge database. If the
precondition is satisfied, the rule can be applied. Application of the rule changes
the knowledge database. The control system chooses which applicable rule
should be applied and ceases computation when a termination condition on the
knowledge database is satisfied.

Example: Eight puzzle (8-Puzzle)


The 8-puzzle is a 3 × 3 array containing eight square pieces, numbered 1
through 8, and one empty space. A piece can be moved horizontally or vertically
into the empty space, in effect exchanging the positions of the piece and the
empty space. There are four possible moves, UP (move the blank space up),
DOWN, LEFT and RIGHT. The aim of the game is to make a sequence of moves
that will convert the board from the start state into the goal state:
1 2 3
2 3 4
8 6 2 8 4
7 5 7 6 5

Initial State Goal State

This example can be solved by the operator sequence UP, RIGHT, UP, LEFT,
DOWN.

Example: Missionaries and Cannibals


The Missionaries and Cannibals problem illustrates the use of state space search
for planning under constraints:

Three missionaries and three cannibals wish to cross a river using a two-person
boat. If at any time the cannibals outnumber the missionaries on either side of
the river, they will eat the missionaries. How can a sequence of boat trips be
performed that will get everyone to the other side of the river without any
missionaries being eaten?
State representation:
1. BOAT position: original (T) or final (NIL) side of the river.

2. Number of Missionaries and Cannibals on the original side of the river.

3. Start is (T 3 3); Goal is (NIL 0 0).


Operators:
(MM 2 0) Two Missionaries cross the river.
(MC 1 1) One Missionary and one Cannibal.
(CC 0 2) Two Cannibals.
(M 1 0) One Missionary.
(C 0 1) One Cannibal.
Control Strategies
The word ‘search’ refers to the search for a solution in a problem space.
 Search proceeds with different types of ‘search control strategies’.

 A strategy is defined by picking the order in which the nodes expand.


The Search strategies are evaluated along the following dimensions:
Completeness, Time complexity, Space complexity, Optimality (the search-
related terms are first explained, and then the search algorithms and control
strategies are illustrated next).

Search-related terms
 Algorithm’s performance and complexity

Ideally we want a common measure so that we can compare approaches in
order
 to select the most appropriate algorithm for a given situation.
 Performance of an algorithm depends on internal and external factors.

Internal factors/ External factors


1. Time required to run

2. Size of input to the algorithm

3. Space (memory) required to run

4. Speed of the computer

5. Quality of the compiler


 Complexity is a measure of the performance of an algorithm. Complexity
measures the internal factors, usually in time than space.
 Computational complexity 

It
 is the measure of resources in terms of Time and Space.
 If A is an algorithm that solves a decision problem f, then run-time of A is
the number of steps taken on the input of length n.

 Time Complexity T(n) of a decision problem f is the run-time of the ‘best’


algorithm A for f.

 Space Complexity S(n) of a decision problem f is the amount of memory


used by the ‘best’ algorithm A for f.
 ‘Big - O’ notation

The Big-O, theoretical measure of the execution of an algorithm, usually
indicates the time or the memory needed, given the problem size n, which is
usually the number of items.

 Big-O notation

The Big-O notation is used to give an approximation to the run-time- efficiency
of an algorithm; the letter ‘O’ is for order of magnitude of operations or space at
run-time.

  The Big-O of an Algorithm A
1. If an algorithm A requires time proportional to f(n), then algorithm A is
said to be of order f(n), and it is denoted as O(f(n)).
2. If algorithm A requires time proportional to n2, then the order of the
algorithm is said to be O(n2).

3. If algorithm A requires time proportional to n, then the order of the


algorithm is said to be O(n).
The function f(n) is called the algorithm’s growth-rate function. In other words,
if an algorithm has performance complexity O(n), this means that the run-time t
should be directly proportional to n, ie t • n or t = k n where k is constant of
proportionality. Similarly, for algorithms having performance complexity
O(log2(n)), O(log N), O(N log N), O(2N) and so on.

PROBLEM CHARACTERISTICS

Heuristics cannot be generalized, as they are domain specific. Production


systems provide ideal techniques for representing such heuristics in the form of
IF-THEN rules. Most problems requiring simulation of intelligence use heuristic
search extensively. Some heuristics are used to define the control structure that
guides the search process, as seen in the example described above. But
heuristics can also be encoded in the rules to represent the domain knowledge.
Since most AI problems make use of knowledge and guided search through the
knowledge, AI can be described as the study of techniques for solving
exponentially hard problems in polynomial time by exploiting knowledge about
problem domain.
To use the heuristic search for problem solving, we suggest analysis of the
problem for the following considerations:
 Decomposability of the problem into a set of independent smaller sub
problems.
 Possibility of undoing solution steps, if they are found to be unwise.
 Predictability of the problem universe.
 Possibility of obtaining an obvious solution to a problem without
comparison of all other possible solutions.
 Type of the solution: whether it is a state or a path to the goal state.
 Role of knowledge in problem solving.
 Nature of solution process: with or without interacting with the user.

Problem Decomposition
Suppose to solve the expression is: + ∫(X³ + X² + 2X + 3sinx)dx

∫(X³ + X² + 2X + 3sinx)dx

∫x³dx ∫x²dx ∫2xdx ∫3sinxdx

4
x /4 x³/3 2∫xdx 3∫sinxdx

x² – 3cosx

This problem can be solved by breaking it into smaller problems, each of which
we can solve by using a small collection of specific rules. Using this technique of
problem decomposition, we can solve very large problems very easily. This can
be considered as an intelligent behaviour.
2.5.2 Can Solution Steps be Ignored?
Suppose we are trying to prove a mathematical theorem: first we proceed
considering that proving a lemma will be useful. Later we realize that it is not at
all useful. We start with another one to prove the theorem. Here we simply
ignore the first method.

Consider the 8-puzzle problem to solve: we make a wrong move and realize that
mistake. But here, the control strategy must keep track of all the moves, so that
we can backtrack to the initial state and start with some new move.

Consider the problem of playing chess. Here, once we make a move we never
recover from that step. These problems are illustrated in the three important
classes of problems mentioned below:

1. Ignorable, in which solution steps can be ignored. Eg: Theorem Proving

2. Recoverable, in which solution steps can be undone. Eg: 8-Puzzle

3. Irrecoverable, in which solution steps cannot be undone. Eg: Chess

Is the Problem Universe Predictable?


Consider the 8-Puzzle problem. Every time we make a move, we know exactly
what will happen. This means that it is possible to plan an entire sequence of
moves and be confident what the resulting state will be. We can backtrack to
earlier moves if they prove unwise.

Suppose we want to play Bridge. We need to plan before the first play, but we
cannot play with certainty. So, the outcome of this game is very uncertain. In
case of 8-Puzzle, the outcome is very certain. To solve uncertain outcome
problems, we follow the process of plan revision as the plan is carried out and
the necessary feedback is provided. The disadvantage is that the planning in this
case is often very expensive.

2.5.4 Is Good Solution Absolute or Relative?


Consider the problem of answering questions based on a database of simple
facts such as the following:
 Siva was a man.

 Siva was a worker in a company.

 Siva was born in 1905.

 All men are mortal.

 All workers in a factory died when there was an accident in 1952.

 No mortal lives longer than 100 years. Suppose we ask a question: ‘Is
Siva alive?’
By representing these facts in a formal language, such as predicate logic, and
then using formal inference methods we can derive an answer to this question
easily. There are two ways to answer the question shown below:
Method I:
1. Siva was a man.

2. Siva was born in 1905.

3. All men are mortal.

4. Now it is 2008, so Siva’s age is 103 years.

5. No mortal lives longer than 100 years.

Method II:
1. Siva is a worker in the company.

2. All workers in the company died in 1952.


Answer: So Siva is not alive. It is the answer from the above methods.

We are interested to answer the question; it does not matter which path we
follow. If we follow one path successfully to the correct answer, then there is no
reason to go back and check another path to lead the solution.

CHARACTERISTICS OF PRODUCTION SYSTEMS

Production systems provide us with good ways of describing the operations that
can be performed in a search for a solution to a problem.

At this time, two questions may arise:

i. Can production systems be described by a set of characteristics? And how


can they be easily implemented?

ii. What relationships are there between the problem types and the types of
production systems well suited for solving the problems?
To answer these questions, first consider the following definitions of classes of
production systems:
1. A monotonic production system is a production system in which the
application of 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 nonmonotonic production system is one in which this is not true.

3. A partially communicative production system is a production system with


the property that if the application of a particular sequence of rules
transforms state P into state Q, then any combination of those rules that
is allowable also transforms state P into state Q.

4. A commutative production system is a production system that is both


monotonic and partially commutative.
Table 2.1 Four Categories of Production Systems

Production System Monotonic Non-monotonic

Partially Commutative Theorem Proving Robot Navigation

Non-partially
Commutative Chemical Synthesis Bridge

Partially commutative, monotonic productions systems are useful for solving


ignorable problems. These are important from an implementation point of view
without the ability to backtrack to previous states when it is discovered that an
incorrect path has been followed. Both types of partially commutative production
systems are significant from an implementation point; they tend to lead to many
duplications of individual states during the search process.

Production systems that are not partially commutative are useful for many
problems in which permanent changes occur.

DESIGN OF SEARCH PROGRAMS AND SOLUTIONS

Solutions can be good in different ways. They can be good in terms of time or
storage or in difficulty of the algorithm. In case of the travelling salesman
problem, finding the best path can lead to a significant amount of computation.
The solution of such problems is only possible by using heuristics. In this type of
problem, a path is found of a distance of 8850 miles and another one of 7750. It
is clear that the second is better than the first but is it the best? Infinite time
may be needed and usually heuristics are used to find a very good path in finite
time.

Design of Search Programs


Each search process can be considered to be a tree traversal. The object of the
search is to find a path from the initial state to a goal state using a tree. The
number of nodes generated might be huge; and in practice many of the nodes
would not be needed. The secret of a good search routine is to generate only
those nodes that are likely to be useful, rather than having a precise tree. The
rules are used to represent the tree implicitly and only to create nodes explicitly
if they are actually to be of use.

The following issues arise when searching:


1. The tree can be searched forward from the initial node to the goal state
or backwards from the goal state to the initial state.

2. To select applicable rules, it is critical to have an efficient procedure for


matching rules against states.

3. How to represent each node of the search process? This is the knowledge
representation problem or the frame problem. In games, an array
suffices; in other problems, more complex data structures are needed.
Finally in terms of data structures, considering the water jug as a typical
problem do we use a graph or tree? The breadth-first structure does take note
of all nodes generated but the depth-first one can be modified.
Check duplicate nodes
1. Observe all nodes that are already generated, if a new node is present.

2. If it exists add it to the graph.

3. If it already exists, then


a) Set the node that is being expanded to the point to the
already existing node corresponding to its successor rather
than to the new one. The new one can be thrown away.

b) If the best or shortest path is being determined, check to see


if this path is better or worse than the old one. If worse, do
nothing.
4. Better save the new path and work the change in length through the
chain of successor nodes if necessary.

Example: Tic-Tac-Toe

State spaces are good representations for board games such as Tic-Tac-Toe.
The position of a game can be explained by the contents of the board and the
player whose turn is next. The board can be represented as an array of 9 cells,
each of which may contain an X or O or be empty.
  State:
o Player to move next: X or O.

o Board configuration:

X 0
0
X X

 Operators: Change an empty cell to X or O.

 Start State: Board empty; X’s turn.

 Terminal States: Three X’s in a row; Three O’s in a row; All cells full.
Search Tree

The sequence of states formed by possible moves is called a search tree. Each
level of the tree is called a ply.
Since the same state may be reachable by different sequences of moves, the
state space may in general be a graph. It may be treated as a tree for
simplicity, at the cost of duplicating states.
Solving problems using search

 Given an informal description of the problem, construct a formal


description
 as a state space:
 Define a data structure to represent the state.

 Make a representation for the initial state from the given data.

 Write programs to represent operators that change a given state


representation to a new state representation.

 Write a program to detect terminal states.


  Choose an appropriate search technique:
 How large is the search space?

 How well structured is the domain?

 What knowledge about the domain can be used to guide the search?

Additional Problems
The additional problems are given below.

The Monkey and Bananas Problem

The monkey is in a closed room in which there is a small table. There is a bunch
of bananas hanging from the ceiling but the monkey cannot reach them. Let us
assume that the monkey can move the table and if the monkey stands on the
table, the monkey can reach the bananas. Establish a method to instruct the
monkey on how to capture the bananas.
a) Task Environment
1. Information
Set of places {on the floor, place1, place2, under the bananas}
2. Operators CLIMB
Pretest the monkey is in the table’s place Move the monkey on the table

WALK

Variable x is the set of places

Move the monkey’s place becomes x. MOVE TABLE

Variable x is the set of places

Pretest the monkey’s place is in the set of places The monkey’s place is in the
table’s place Move the monkey’s place becomes x.

The table’s place becomes x. GET BANANAS

Pretest the table’s place is under the bananas The monkey’s place is on the
table

Move the contents of the monkey’s hand are the bananas


3. Differences
d1 is the monkey’s place

d2 is the table’s place

d3 are the contents of the monkey’s hand


4. Differences ordering
d3 is harder to reduce than d2 which is harder to reduce than d1. Specific Task

Goal transform initial object to desired object Objects

Initial the monkey’s place is place1 the table’s place is place2 The contents of
the monkey’s hand is empty

Desired the contents of the monkey’s hand are the bananas.


UNIT – II (6 Lectures)

Blind Search- Depth First Search, Breadth Firth Search.Heuristic search


techniques: Generate and test, hill climbing- Simple Hill Climbing, Steepest
Ascent hill Climbing, best first search.

Search Strategies /Algorithms

Searching is the universal technique of problem solving in AI. There are some
single-player games such as tile games, Sudoku, crossword, etc. The search
algorithms help you to search for a particular position in such games.

Many traditional search algorithms are used in AI applications. For complex


problems, the traditional algorithms are unable to find the solutions within some
practical time and space limits. Consequently, many special techniques are
developed, using heuristic functions. The algorithms that use heuristic functions
are called heuristic algorithms. A Hierarchical representation of most search
algorithms is illustrated below. The representation begins with two types of
search:

Uninformed search algorithms or Brute-force algorithms or Blind Search


Algorithm search through the search space all possible candidates for the
solution checking whether each candidate satisfies the problem’s statement.
Uninformed search strategies use only the information available in the problem
definition. It uses no information about the problem to guide the search and
therefore may not be very efficient.

Different Types of Uninformed Search Strategies are :

Uninformed Search Strategies

a. Breadth-First search
b. Depth-First search
c. Depth-First Iterative Deepening search.
d. Depth-limited search
e. Uniform-Cost search
f. Iterative deepening search
g. Bidirectional
h. Branch and Bound

Informed search algorithms or Intelligent Search Algorithm Also called


heuristic or intelligent search, this uses information about the problem to guide
the search—usually guesses the distance to a goal state and is therefore
efficient, but the search may not be always possible. It use heuristic functions
that are specific to the problem, apply them to guide the search through the
search space to try to reduce the amount of time spent in searching.
 Heuristic algorithms are not really intelligent; they appear to be
intelligent because they achieve better performance.

 Heuristic algorithms are more efficient because they take advantage of
feedback from the data to direct the search path.

 A good heuristic will make an informed search dramatically out perform
any uninformed search: for example, the Traveling Salesman Problem
(TSP), where the goal is to find is a good solution instead of finding the
best solution.
 In such problems, the search proceeds using current information about
the problem to predict which path is closer to the goal and follow it,
although it does not always guarantee to find the best possible solution.
Such techniques help in finding a solution within reasonable time and
space (memory).

Different Types of Uninformed Search Strategies are:

Informed Search Strategies

 Best-first search
 Greedy best-first search
 Heuristics
 Local search algorithms
 Hill-climbing search
 Simulated annealing search
 Local beam search
 Genetic algorithms
 Algorithm A Search
 Algorithm A* Search

Characteristics or Strategies of Search Strategies:

 The first requirement is that it causes motion, in a game playing


program, it moves on the board and in the water jug problem, filling
water is used to fill jugs. It means the control strategies without the
motion will never lead to the solution.

 The second requirement is that it is systematic, that is, it corresponds to


the need for global motion as well as for local motion. This is a clear
condition that neither would it be rational to fill a jug and empty it
repeatedly, nor it would be worthwhile to move a piece round and round
on the board in a cyclic way in a game. We shall initially consider two
systematic approaches for searching. Searches can be classified by the
order in which operators are tried: depth-first, breadth-first, bounded
depth-first.

 A search strategy is defined by picking the order of node expansion


 Strategies are evaluated along the following dimensions:
1. Completeness: does it always find a solution if one exists?
2. Time complexity: number of nodes generated
3. Space complexity: maximum number of nodes in memory
4. Optimality: does it always find a least-cost solution?
 Time and space complexity are measured in terms of
a) b: maximum branching factor of the search tree
b) d: depth of the least-cost solution
c) m: maximum depth of the state space (may be ∞)

Search Terminology
 Problem Space − It is the environment in which the search takes place.
(A set of states and set of operators to change those states)
 Problem Instance − It is Initial state + Goal state.
 Problem Space Graph − It represents problem state. States are shown
by nodes and operators are shown by edges.
 Depth of a problem − Length of a shortest path or shortest sequence of
operators from Initial State to goal state.
 Space Complexity − The maximum number of nodes that are stored in
memory.
 Time Complexity − The maximum number of nodes that are created.
 Admissibility − A property of an algorithm to always find an optimal
solution.
 Branching Factor − The average number of child nodes in the problem
space graph.
 Depth − Length of the shortest path from initial state to goal state.

Uninformed Search Strategies


Brute-Force Search Strategies

They are most simple, as they do not need any domain-specific knowledge. They
work fine with small number of possible states.

Requirements –

 State description
 A set of valid operators
 Initial state
 Goal State description
Single Agent Path finding Problems
 The games such as 3X3 eight-tile, 4X4 fifteen-tile, and 5X5 twenty four
tile puzzles are single-agent-path-finding challenges. They consist of a
matrix of tiles with a blank tile. The player is required to arrange the tiles
by sliding a tile either vertically or horizontally into a blank space with the
aim of accomplishing some objective.
 The other examples of single agent path finding problems are Travelling
Salesman Problem, Rubik’s Cube, and Theorem Proving.
1. Breadth-First Search

A search strategy, in which the highest layer of a decision tree is searched


completely before proceeding to the next layer is called Breadth-First Search
(BFS).
 In this strategy, no viable solutions are omitted and therefore it is
guaranteed that an optimal solution is found.

 This strategy is often not feasible when the search space is large.

It generates one tree at a time until the solution is found. It can be implemented
using FIFO queue data structure. This method provides shortest path to the
solution.
If branching factor (average number of child nodes for a given node) = b and
depth = d, then number of nodes at level d = bd.
The total no of nodes created in worst case is b + b2 + b3 + … + bd.

Algorithm
1. Create a variable called LIST and set it to be the starting state.

2. Loop until a goal state is found or LIST is empty, Do


a) Remove the first element from the LIST and call it E. If the LIST is
empty, quit.

b) For every path each rule can match the state E, Do


i. Apply the rule to generate a new state.

ii. If the new state is a goal state, quit and return this state.

iii. Otherwise, add the new state to the end of LIST.


Advantages
1. Guaranteed to find an optimal solution (in terms of shortest number of
steps to reach the goal).

2. Can always find a goal node if one exists (complete).


Disadvantages

a. High storage requirement: exponential with tree depth.Since each level of


nodes is saved for creating next one, it consumes a lot of memory space.
Space requirement to store nodes is exponential.
b. Its complexity depends on the number of nodes. It can check duplicate
nodes.

Expand shallowest unexpanded node

Implementation:

fringe is a FIFO queue, i.e., new successors go at end.

1. 2.

2. 4.

Properties of BFS:

 Complete? Yes (if b is finite)


 Time?1+b+b2+b3+… +bd + b(bd-1) = O(bd+1)
 Space?O(bd+1) (keeps every node in memory)
 Optimal? Yes (if cost = 1 per step)

Space is the bigger problem (more than time)

1. Depth-first search

A search strategy that extends the current path as far as possible before
backtracking to the last choice point and trying the next alternative path is
called Depth-first search (DFS).
 This strategy does not guarantee that the optimal solution has been
found.

 In this strategy, search reaches a satisfactory solution more rapidly than


breadth first, an advantage when the search space is large.
Algorithm

Depth-first search applies operators to each newly generated state, trying to


drive directly toward the goal.
 If the starting state is a goal state, quit and return success.
 Otherwise, do the following until success or failure is signalled:
 Generate a successor E to the starting state. If there are no more
successors, then signal failure.
 Call Depth-first Search with E as the starting state.
If success is returned signal success; otherwise, continue in the loop.
Advantages
 Low storage requirement: linear with tree depth.
 Easily programmed: function call stack does most of the work of
maintaining state of the search.

Disadvantages
 May find a sub-optimal solution (one that is deeper or more costly than
the best solution).
 Incomplete: without a depth bound, may not find a solution even if one
exists.

Example:

Expand deepest unexpanded node

1. 2.

2. 4.

3. 4.

4. 5.
5. 6.

6. 7.

Properties of DFS

Complete? No: fails in infinite-depth spaces, spaces with loops


 Modify to avoid repeated states along path
complete in finite spaces
Time?O(bm): terrible if m is much larger than d but if solutions are dense, may
be much faster than breadth-first
Space?O(bm), i.e., linear space!
Optimal? No

Bounded Depth-first search

Depth-first search can spend much time (perhaps infinite time) exploring a very
deep path that does not contain a solution, when a shallow solution exists. An
easy way to solve this problem is to put a maximum depth bound on the search.
Beyond the depth bound, a failure is generated automatically without exploring
any deeper.
Problems:
1. It’s hard to guess how deep the solution lies.

2. If the estimated depth is too deep (even by 1) the computer time used is
dramatically increased, by a factor of bextra.

3. If the estimated depth is too shallow, the search fails to find a solution;
all that computer time is wasted.

Iterative deepening search

Iterative deepening search l =0


Iterative deepening search l =1

Iterative deepening search l =2

Iterative deepening search l =3

Properties of Deepening Search:

Complete? Yes
Time?(d+1)b0 + d b1 + (d-1)b2 + … + bd = O(bd)
Space?O(bd)
Optimal? Yes, if step cost = 1

Summary of Blind Algorithms:


Generate and Test:
Algorithm
 Generate a possible solution.
 Test to see if this is actually a solution.
 Quit if a solution has been found.
Otherwise, return to step 1.
Features:
 Acceptable for simple problems.
 Inefficient for problems with large space.

Types of Generate and Test:


 Exhaustive generate-and-test.
 Heuristic generate-and-test: not consider paths that seem unlikely to lead
to a solution.
 Plan generate-test:
- Create a list of candidates.
- Apply generate-and-test to that list.

Example: Colored Blocks

“Arrange four 6-sided cubes in a row, with each side of each cube painted one of
four colours, such that on all four sides of the row one block face of each colour
is showing.”

Heuristic: If there are more red faces than other colours then, when placing a
block with several red faces, use few of them as possible as outside faces

Heuristics
A heuristic is a method that improves the efficiency of the search process. These
are like tour guides. There are good to the level that they may neglect the
points in general interesting directions; they are bad to the level that they may
neglect points of interest to particular individuals. Some heuristics help in the
search process without sacrificing any claims to entirety that the process might
previously had. Others may occasionally cause an excellent path to be
overlooked. By sacrificing entirety it increases efficiency. Heuristics may not find
the best solution every time but guarantee that they find a good solution in a
reasonable time. These are particularly useful in solving tough and complex
problems, solutions of which would require infinite time, i.e. far longer than a
lifetime for the problems which are not solved in any other way.

Heuristic search

To find a solution in proper time rather than a complete solution in unlimited


time we use heuristics. ‘A heuristic function is a function that maps from
problem state descriptions to measures of desirability, usually represented as
numbers’. Heuristic search methods use knowledge about the problem domain
and choose promising operators first. These heuristic search methods use
heuristic functions to evaluate the next state towards the goal state. For finding
a solution, by using the heuristic technique, one should carry out the following
steps:

1. Add domain—specific information to select what is the best path to


continue searching along.
2. Define a heuristic function h(n) that estimates the ‘goodness’ of a node n.
Specifically, h(n) = estimated cost(or distance) of minimal cost path from
n to a goal state.

3. The term, heuristic means ‘serving to aid discovery’ and is an estimate,


based on domain specific information that is computable from the current
state description of how close we are to a goal.
Finding a route from one city to another city is an example of a search problem
in which different search orders and the use of heuristic knowledge are easily
understood.
 State: The current city in which the traveller is located.

 Operators: Roads linking the current city to other cities.

 Cost Metric: The cost of taking a given road between cities.

 Heuristic information: The search could be guided by the direction of the


goal city from the current city, or we could use airline distance as an
estimate of the distance to the goal.
Heuristic search techniques

For complex problems, the traditional algorithms, presented above, are unable
to find the solution within some practical time and space limits. Consequently,
many special techniques are developed, using heuristic functions.
 Blind search is not always possible, because it requires too much time or
Space (memory).
 Heuristics are rules of thumb; they do not guarantee a solution to a
problem.
 Heuristic Search is a weak technique but can be effective if applied
correctly; it requires domain specific information.
Characteristics of heuristic search
 Heuristics are knowledge about domain, which help search and
reasoning in its domain.

 Heuristic search incorporates domain knowledge to improve efficiency


over blind search.

 Heuristic is a function that, when applied to a state, returns value as


 estimated merit of state, with respect to goal.
 Heuristics might (for reasons) underestimate or overestimate the
merit of a state with respect to goal.

 Heuristics that underestimate are desirable and called admissible.


 Heuristic evaluation function estimates likelihood of given state leading
to goal state.

 Heuristic search function estimates cost from current state to goal,


presuming function is efficient.
Heuristic search compared with other search
The Heuristic search is compared with Brute force or Blind search techniques
below:

Hill Climbing:
 Searching for a goal state = Climbing to the top of a hill

Features:

Generate-and-test + direction to move.


Heuristic function to estimate how close a given state is to a goal state.

Simple Hill Climbing:

Algorithm
1. Evaluate the initial state.
2. Loop until a solution is found or there are no new operators left to be
applied:
- Select and apply a new operator
- Evaluate the new state:
goal quit
better than current state  new current state
 Evaluation function as a way to inject task-specific knowledge into the
control process.

Example: coloured blocks


 Heuristic function: the sum of the number of different
 colours on each of the four sides (solution = 16).
Steepest-Ascent Hill Climbing (Gradient Search)

 Considers all the moves from the current state.


 Selects the best one as the next state.
Algorithm
1. Evaluate the initial state.
2. Loop until a solution is found or a complete iteration produces no change
to current state:
- SUCC = a state such that any possible successor of the
current state will be better than SUCC (the worst state).
- For each operator that applies to the current state, evaluate
the new state:
goal quit
better than SUCC  set SUCC to this state
- SUCC is better than the current state  set the current
state to SUCC.
Hill Climbing: Disadvantages
Local maximum
A state that is better than all of its neighbours, but not
better than some other states far away.

Plateau
A flat area of the search space in which all neighbouring states have the same
value.
Ridge
The orientation of the high region, compared to the set of available moves,
makes it impossible to climb up.
However, two moves executed serially may increase the height.
Ways Out
 Backtrack to some earlier node and try going in a different direction.
 Make a big jump to try to get in a new section.

Moving in several directions at once.

 Hill climbing is a local method:


 Decides what to do next by looking only at the “immediate”
consequences of its choices.
 Global information might be encoded in heuristic functions.

Disadvantage −

Depth-First Search
It is implemented in recursion with LIFO stack data structure. It creates the
same set of nodes as Breadth-First method, only in the different order.
As the nodes on the single path are stored in each iteration from root to leaf
node, the space requirement to store nodes is linear. With branching
factor b and depth as m, the storage space is bm.
Disadvantage − This algorithm may not terminate and go on infinitely on one
path. The solution to this issue is to choose a cut-off depth. If the ideal cut-off
is d, and if chosen cut-off is lesser than d, then this algorithm may fail. If
chosen cut-off is more thand, then execution time increases.
Its complexity depends on the number of paths. It cannot check duplicate
nodes.

Bidirectional Search
It searches forward from initial state and backward from goal state till both
meet to identify a common state.
The path from initial state is concatenated with the inverse path from the goal
state. Each search is done only up to half of the total path.
Uniform Cost Search
Sorting is done in increasing cost of the path to a node. It always expands the
least cost node. It is identical to Breadth First search if each transition has the
same cost.
It explores paths in the increasing order of cost.
Disadvantage − There can be multiple long paths with the cost ≤ C*.
Uniform Cost search must explore them all.

Iterative Deepening Depth-First Search


It performs depth-first search to level 1, starts over, executes a complete
depth-first search to level 2, and continues in such way till the solution is
found.
It never creates a node until all lower nodes are generated. It only saves a
stack of nodes. The algorithm ends when it finds a solution at depth d. The
number of nodes created at depth d is bdand at depth d-1 is bd-1.

Comparison of Various Algorithms Complexities


Let us see the performance of algorithms based on various criteria −

Criterion Breadth Depth Bidirectional Uniform Interactive


First First Cost Deepening

Time bd bm bd/2 bd bd

Space bd bm bd/2 bd bd

Optimality Yes No Yes Yes Yes

Completeness Yes No Yes Yes Yes

Informed (Heuristic) Search Strategies


To solve large problems with large number of possible states, problem-specific
knowledge needs to be added to increase the efficiency of search algorithms.
Heuristic Evaluation Functions
They calculate the cost of optimal path between two states. A heuristic function
for sliding-tiles games is computed by counting number of moves that each tile
makes from its goal state and adding these number of moves for all tiles.
Pure Heuristic Search
It expands nodes in the order of their heuristic values. It creates two lists, a
closed list for the already expanded nodes and an open list for the created but
unexpanded nodes.
In each iteration, a node with a minimum heuristic value is expanded, all its
child nodes are created and placed in the closed list. Then, the heuristic
function is applied to the child nodes and they are placed in the open list
according to their heuristic value. The shorter paths are saved and the longer
ones are disposed.
A * Search
It is best-known form of Best First search. It avoids expanding paths that are
already expensive, but expands most promising paths first.
f(n) = g(n) + h(n), where
 g(n) the cost (so far) to reach the node
 h(n) estimated cost to get from the node to the goal
 f(n) estimated total cost of path through n to goal. It is implemented
using priority queue by increasing f(n).
Greedy Best First Search
It expands the node that is estimated to be closest to goal. It expands nodes
based on f(n) = h(n). It is implemented using priority queue.
Disadvantage − It can get stuck in loops. It is not optimal.

Local Search Algorithms


They start from a prospective solution and then move to a neighboring solution.
They can return a valid solution even if it is interrupted at any time before they
end.
Hill-Climbing Search
It is an iterative algorithm that starts with an arbitrary solution to a problem
and attempts to find a better solution by changing a single element of the
solution incrementally. If the change produces a better solution, an incremental
change is taken as a new solution. This process is repeated until there are no
further improvements.
function Hill-Climbing (problem), returns a state that is a local maximum.
inputs: problem, a problem
local variables: current, a node
neighbor, a node
current<-Make_Node(Initial-State[problem])
loop
do neighbor <- a highest_valued successor of current
if Value[neighbor] ≤ Value[current] then
return State[current]
current<- neighbor

end
Disadvantage − This algorithm is neither complete, nor optimal.
Local Beam Search
In this algorithm, it holds k number of states at any given time. At the start,
these states are generated randomly. The successors of these k states are
computed with the help of objective function. If any of these successors is the
maximum value of the objective function, then the algorithm stops.

Otherwise the (initial k states and k number of successors of the states = 2k)
states are placed in a pool. The pool is then sorted numerically. The highest k
states are selected as new initial states. This process continues until a
maximum value is reached.

functionBeamSearch( problem, k), returns a solution state.

start with k randomly generated states

loop

generate all successors of all k states

if any of the states = solution, then return the state

else select the k best successors

end

Simulated Annealing
Annealing is the process of heating and cooling a metal to change its internal
structure for modifying its physical properties. When the metal cools, its new
structure is seized, and the metal retains its newly obtained properties. In
simulated annealing process, the temperature is kept variable.

We initially set the temperature high and then allow it to ‘cool' slowly as the
algorithm proceeds. When the temperature is high, the algorithm is allowed to
accept worse solutions with high frequency.

Start
 Initialize k = 0; L = integer number of variables;

 From i→ j, search the performance difference ∆.

 If ∆ <= 0 then accept else if exp(-/T(k)) > random(0,1) then accept;

 Repeat steps 1 and 2 for L(k) steps.

 k = k + 1;

Repeat steps 1 through 4 till the criteria is met.


End

Travelling Salesman Problem


In this algorithm, the objective is to find a low-cost tour that starts from a city,
visits all cities en-route exactly once and ends at the same starting city.

Start

Find out all (n -1)! Possible solutions, where n is the total number of cities.

Determine the minimum cost by finding out the cost of each of these (n -1)!
solutions.

Finally, keep the one with the minimum cost.

end
UNIT III: Knowledge Representation: Definition and importance of knowledge,
Knowledge Representation, Various approaches used in knowledge representation, Issues in knowledge
representation. Using Predicate Logic : Representing Simple Facts in logic, Representing instances and is
a Relationship.

3.1 KNOWLEDGE REPRESENTATION


Knowledge Representation
(x): animal (x) fi eat (x, fruit) eat (x, meat)

What AI researchers call ‘knowledge’ appears as data at the level of programming. Data
becomes knowledge when a computer program represents and uses the meaning of
some data. Many knowledge-based programs are written in the LISP programming
language, which is designed to manipulate data as symbols.

Knowledge may be declarative or procedural.

Declarative knowledge is represented as a static collection of facts with a set of


procedures for manipulating the facts.

Procedural knowledge is described by an executable code that performs some action.


Procedural knowledge refers to ‘how-to’ do something. Usually, there is a need for both
kinds of knowledge representation to capture and represent knowledge in a particular
domain.

Example:

• Knowledge is a general term.

An answer to the question, ‘how to represent knowledge’, requires an analysis to


distinguish between knowledge ‘how’ and knowledge ‘that’.

knowing ‘how to do something’.
e.g. ‘how to drive a car’ is Procedural knowledge.

knowing ‘that something is true or false’.
e.g. ‘that is the speed limit for a car on a motorway’ is Declarative
knowledge.

First-order predicate calculus (FOPC) is the best-understood scheme for knowledge


representation and reasoning. In FOPC, knowledge about the world is represented as
objects and relations between objects. Objects are real-world things that have individual
identities and properties, which are used to distinguish the things from other objects. In
a first-order predicate language, knowledge about the world is expressed in terms of
sentences that are subject to the language’s syntax and semantics.

Procedural knowledge Declarative knowledge

Examples: procedures, rules, strategies , Example: concepts, objects, facts,


agendas, models. propositions, assertions, semantic
nets, logic and descriptive models.

Focuses on tasks that must be performed to Refers to representations of objects


reach a particular objective
 or goal. and events; knowledge about facts
and relationships

Knowledge about ‘how to do something’; e.g., Knowledge about ‘that something


to determine if Peter or Robert is older, first if is true or false’. e.g., A car has
find their ages.  four tyres; Peter is older than 
Robert. 

Fundamental System Issues


A robust AI system must be able to store knowledge, apply that knowledge to the
solution of problems and acquire new knowledge through experience. Among the
challenges that face researchers in building AI systems, there are three that are
fundamental:

1) Knowledge Representation
2) Reasoning
3) Searching and Learning.

Knowledge and representation are distinct entities that play central but
distinguishable roles in the intelligence system.
■ Knowledge is a description of the world. It determines a system’s competence
by what it knows.

Representation is the way knowledge is encoded. It defines the performance of
a system in doing something.
Different types of knowledge require different kinds of representation.
The Knowledge Representation models/mechanisms are often based on:
■ ■ ■ ■
Logic Rules Frames Semantic Net
Different types of knowledge require different kinds of reasoning.
Knowledge is a general term. Knowledge is a progression that starts with data which is
of limited utility. By organizing or analysing the data, we understand what the data
means, and this becomes information. The interpretation or evaluation of information
yield knowledge. An understanding of the principles within the knowledge is wisdom.

Knowledge Progression

Organizing Interpretation Understanding

Data Information Knowledge Wisdom

Analysing Evolution Principles

Fig. 3.1 Knowledge Progression

Data is viewed as a collection of disconnected facts.


Eg: It is raining.
Information emerges when relationships among facts are established and understood.
Providing answers to ‘who’, ‘what’, ‘where’, and ‘when’ gives the relationships among
facts.
Eg: The temperature dropped 15 degrees and it started raining.
Knowledge emerges when relationships among patterns are identified and understood.
Answering to ‘how’ gives the relationships among patterns.
Eg: If the humidity is very high, temperature drops substantially, and then atmosphere
holds the moisture, so it rains.
Wisdom is the understanding of the principles of relationships that describe patterns.
Providing the answer for ‘why’ gives understanding of the interaction between patterns.
Eg: Understanding of all the interactions that may happen between raining, evaporation,
air currents, temperature gradients and changes.
Let’s look at various kinds of knowledge that might need to represent AI systems:
Objects
Objects are defined through facts.
Eg: Guitars with strings and trumpets are brass instruments.

Events
Events are actions.
Eg: Vinay played the guitar at the farewell party.

Performance
Playing the guitar involves the behaviour of the knowledge about how to do things.
Meta-knowledge
Knowledge about what we know.
To solve problems in AI, we must represent knowledge and must deal with the entities.
Facts
Facts are truths about the real world on what we represent. This can be considered as
knowledge level.

Knowledge Model

The Knowledge Model defines that as the level of ‘connectedness’ and ‘understanding’
increases, our progress moves from data through information and knowledge to wisdom.

The model represents transitions and understanding.


 The transitions are from data to information, information to knowledge, and
finally knowledge to wisdom; 
 The support of understanding is the transitions from one stage to the next stage. 

The distinctions between data, information, knowledge and wisdom are not very discrete.
They are more like shades of gray, rather than black and white.
Data and information deal with the past and are based on gathering facts and adding
context.
Knowledge deals with the present and that enables us to perform.
Wisdom deals with the future vision for what will be rather than for what it is or it was.
Degrees of
Connectedness Wisdom

Understanding
Principles
Knowledge

Understanding
Patterns

Information

Understanding
Degrees of
Relations
Understanding
Data

Fig. Knowledge Model


Knowledge Typology Map
According to the topology map:

1. Tacit knowledge derives from the experience, doing action and subjective insight

2. Explicit knowledge derives from principle, procedure, process and concepts.

3. Information derives from data, context and facts.

4. Knowledge conversion devices from internalization, socialization, wisdom,


combination and externalization.

Finally Knowledge derives from the Tacit and Explicit Knowledge and Information.
Let’s see the contents of the topology map:

Facts are data or instance that is specific and unique.


Concepts are classes of items or words or ideas that are known by a common name and
share common features.
Processes are flow of events or activities that describes how things will work rather than
how to do things.
Procedures are a series of step-by-step actions and decisions that give the result of a
task.
Principles are guidelines, rules and parameters that are going to rule or monitor.

Doing Principles Procedure


Experience Action

Tacit Explicit Knowledge


Knowledge Process

Subjective Knowledge Concept


Insight

Information
Context

Socialization
Facts Data
Wisdon
Intemalization
Knowledge
Conversion Externalizatin

Combination

Fig. Knowledge Typology Map


3.3 REPRESENTATION AND MAPPINGS

When we collect knowledge we are faced with the problem of how to record it. And
when we try to build the knowledge base we have the similar problem of how to
represent it.

The knowledge representation problem concerns the mismatch between human and
computer ‘memory’, i.e. how to encode knowledge so that it is a faithful reflection of the
expert’s knowledge and can be manipulated by the computer.

We call these representations of knowledge - knowledge bases, and the manipulative


operations on these knowledge bases, inference engine programs.

What to Represent?

Facts: Truths about the real world and what we represent. This can be regarded as the
base knowledge level.

Representation of the facts: That which we manipulate. This can be regarded as the
symbol level since we usually define the representation in terms of symbols that can be
manipulated by programs.

Simple Representation: Simple way to store facts. Each fact about a set of objects is set
out systematically in columns. Little opportunity for inference. Knowledge basis for
inference engines.

Framework of Knowledge Representation

A computer requires a well-defined problem description to process and to provide well-


defined acceptable solution. To collect fragments of knowledge we first need to formulate
a description in our spoken language and then represent it in formal language so that the
computer can understand. The computer can then use an algorithm to compute an
answer. This process is illustrated below.
Solve
Problem Solution
Informal
Represent Interpret

Formal

Compute
Representation Output

Fig. Knowledge Representation Framework

The steps are:

  The informal formalism of the problem takes place first. 


 It is then represented formally and the computer produces an output. 

 This output can then be represented in a informally described solution that the user
understands or checks for consistency. 

Note: Problem solving requires 

 Formal knowledge representation, and 

  Conversion of informal (implicit) knowledge to formal (explicit) knowledge. 
 Knowledge and Representation 

Problem solving requires a large amount of knowledge and some mechanism for
manipulating that knowledge. 

Knowledge and Representation are distinct entities and play a central but
 distinguishable roles in the intelligence system. 

Knowledge is a description of the world; it determines a system’s competence
by what it knows.

Representation is the way knowledge is encoded; it defines the system’s
performance in doing something.
Thus, knowledge representation can be considered at two levels:
(a) Knowledge level at which facts are described, and
(b) Symbol level at which the representations of the objects, defined in terms of
symbols, can be manipulated in the programs.

Representation of Facts
Representations of facts are those which we manipulate. This can be regarded as the symbol level
since we normally define the representation in terms of symbols that can be manipulated by
programs. We can structure these entities in two levels:

The knowledge level


At which the facts are going to be described.

The symbol level


At which representations of objects are going to be defined in terms of symbols that can be
manipulated in programs.
Fig. Two Entities in Knowledge Representation

Natural language (or English) is the way of representing and handling the facts. Logic
enables us to consider the following fact:
spot is a dog represented as dog(spot)
We infer that all dogs have tails
: dog(x) has_a_tail(x)
According to the logical conclusion
has_a_tail(Spot)
Using a backward mapping function
Spot has a tail can be generated.

Using Knowledge

Knowledge can be used in various applications like:


Learning

Acquiring knowledge is learning. It simply means adding new


facts to a knowledge base. New data may have to be classified
prior to storage for easy retrieval, interaction and inference with
the existing facts and has to avoid redundancy and replication in
the knowledge. These facts should also be updated.
Retrieval

Using the representation scheme shows a critical effect on the


efficiency of the method. Humans are very good at it. Many AI
methods have tried to model humans.
Reasoning
Get or infer facts from the existing data.

If a system only knows that:


 Ravi is a Jazz musician.

 All Jazz musicians can play their instruments well.

If questions are like this

 Is Ravi a Jazz musician? OR

 Can Jazz musicians play their instruments well?

then the answer is easy to get from the data structures and
procedures.

However, questions like

Can Ravi play his instrument well?

require reasoning. The above are all related. For example, it is


fairly obvious that learning and reasoning involve retrieval, etc.

3.4 APPROACHES TO KNOWLEDGE


REPRESENTATION

Properties for Knowledge Representation Systems


The Knowledge Representation System possesses the following properties:

  Representational Adequacy 

  Inferential Adequacy 
 Inferential Efficiency 

  Acquisitional Efficiency 
 Well-defined Syntax and Semantics 

  Naturalness 
 Frame problem 

Representational adequacy

A knowledge representation scheme must be able to actually represent the knowledge


appropriate to our problem.
Inferential adequacy

A knowledge representation scheme must allow us to make new inference from old
knowledge. It means the ability to manipulate the knowledge represented to produce new
knowledge corresponding to that inferred from the original. It must make inferences that are:
 Sound – The new knowledge actually does follow from the old knowledge. 

 Complete – It should make all the right inferences. 
Here soundness is usually easy, but completeness is very hard.
Example:

Given Knowledge
Tom is a man
All men are mortal.
The inference –
James is mortal
Is not sound, whereas
Tom is mortal
Is sound.

Inferential efficiency

A knowledge representation scheme should be tractable, i.e. make inferences in reasonable


time. Unfortunately, any knowledge representation scheme with interesting expressive
power is not going to be efficient; often, the more general knowledge representation schemes
are less efficient. We have to use knowledge representation schemes tailored to problem
domain i.e. less general but more efficient. The ability to direct the inferential mechanisms
into the most productive directions by storing guides.
Acquisitional efficiency

Acquire new knowledge using automatic methods wherever possible rather than on human
intervention. Till today no single system optimizes all the properties. Now we will discuss
some of the representation schemes.
Well-defined syntax and semantics
It should be possible to tell:
  Whether any construction is ‘grammatically correct’. 
 How to read any particular construction, i.e. no ambiguity. 
Thus a knowledge representation scheme should have well-defined syntax. It should
be possible to precisely determine for any given construction, exactly what its meaning is.
Thus a knowledge representation scheme should have well-defined semantics. Here syntax is
easy, but semantics is hard for a knowledge representation scheme.

Naturalness

A knowledge representation scheme should closely correspond to our way of thinking,


reading and

writing. A knowledge representation scheme should allow a knowledge engineer to read and
check the knowledge base. Simply we can define knowledge representation as knowledge
representation is the problem of representing what the computer knows.
Frame problem

A frame problem is a problem of representing the facts that change as well as those that do
not change. For example, consider a table with a plant on it under a window. Suppose we
move it to the centre of the room. Here we must infer that plant is now in the center but the
window is not. Frame axioms are used to describe all the things that do not change when an
operator is applied in one state to go to another state say n + 1.

Simple Relational Knowledge


 Simple way to store facts. 

Each fact about a set of objects is set out systematically in columns.
  Little opportunity for inference. 
 Knowledge basis for inference engines. 

Musician Style Instrument Age


Ravi Jazz Trumpet 36
John Avant Garde Saxophone 35
Robert Rock Guitar 43
Krishna Jazz Guitar 47
Fig 3.7 Simple Relational Knowledge

We ask things like:


 Who is dead? 

 Who plays Jazz/Trumpet, etc.? 
This sort of representation is popular in database systems.

Inheritable knowledge

age
Adult Male 35

is a

Musician

is a

Jazz Avant
Garde/Jazz

instance instance

Ravi John

bands bands

Ravi's Group Naked City


Ravi's Quintet Hyderabad

Fig. 3.8 Property Inheritance Hierarchy

Relational knowledge is made up of objects consisting of

  attributes 
 corresponding associated values 
We can extend the base by allowing inference mechanisms:
  Property inheritance 

Elements inherit values from being members of a class.

Data must be organized into a hierarchy of classes.
  Boxed nodes – objects and values of attributes of objects. 
 Values can be objects with attributes and so on. 

 Arrows – point from object to its value. 

 This structure is known as a slot and filler structure, semantic network or a collection
of frames. 
The algorithm is to retrieve a value for an attribute in an instance object:
Algorithm: Property Inheritance
1. Find the object in the knowledge base.
2. If there is a value for the attribute report it.
3. Otherwise look for a value of instance if none fail.
4. Otherwise go to that node and find a value for the attribute and then report it.
5. Otherwise search through using isa until a value is found for the attribute.

Inferential knowledge
Represent knowledge as formal logic:
All dogs have tails : dog(x) has_a_tail(x)
Advantages:
  A set of strict rules. 

Can be used to derive more facts.

Truths of new statements can be verified.

Guaranteed correctness.
 Many inference procedures available to implement standard rules of logic. 

 Popular in AI systems. Ex. Automated theorem proving. 
Procedural knowledge

The basic idea of Procedural Knowledge is the Knowledge encoded in some procedures.
Small programs know how to do specific things and how to proceed. For example, a parser
in a natural language understander has the knowledge that a noun phrase may contain
articles, adjectives and nouns. It is represented by calls to routines that know how to process
articles, adjectives and nouns.
Advantages:

  Heuristic or domain specific knowledge can be represented. 


 Extended logical inferences, such as default reasoning facilitated. 

 Side effects of actions may be modelled. Some rules may become false in time.
Keeping track of this in large systems may be tricky. 
Disadvantages:

  Completeness - not all cases may be represented. 


 Consistency - not all deductions may be correct. 

  Modularity is sacrificed. 
 Cumbersome control information. 


ISSUES IN KNOWLEDGE REPRESENTATION
The issues in knowledge representation are discussed below.
Important Attributes and Relationships
There are two main important attributes in Knowledge Representation: Instance and Isa.
They are important because each supports property inheritance.
What about the relationship between the attributes of an object, such as inverses,
existence, techniques for reasoning about values and single valued attributes. We consider an
example of an inverse in
band(John, Naked City)
This can be treated as John plays in the band Naked City or John’s band in Naked City.
Another representation is band = Naked City
Band-members = John, Robert,
The relationship between the attributes of an object is independent of specific knowledge
they encode, and may hold properties like: inverses, existence in an isa hierarchy, techniques for
reasoning about values and single valued attributes.
Inverses
This is about consistency check, while a value is added to one attribute. The entities are
related to each other in many different ways. The figure shows attributes (isa, instance and
team), each with a directed arrow, originating at the object being described and terminating
either at the object or its value.
Existence in an isa hierarchy
This is about generalization-specialization, like classes of objects and Specialized subsets of
those classes, their attributes and specialization of attributes.
Ex.: The height is a specialization of general attribute; physical-size is a specialization of
physical-attribute. These generalization-specialization relationships are important for
attributes because they support inheritance.
Techniques for reasoning about values
This is about reasoning values of attributes not given explicitly. Several kinds of information
are used in reasoning. Like height must be in a unit of length, age of person cannot be
greater than the age of person’s parents. The values are often specified when a knowledge
base is created.
Single-valued attributes
This is about a specific attribute that is guaranteed to take a unique value. For example, a guitar
player can be only a single and be a member of only one team. KR systems take different
approaches to provide support for single valued attributes.

Granularity
At what level the knowledge represents and what are the primitives? Choosing the Granularity of
Representation Primitives are fundamental concepts, such as holding, seeing, playing. English is
a very rich language with over half a million words is
clear and we will find difficulty in deciding upon which words to choose as our primitives in
a series of situations.
If Syam feeds a dog then it could be: feeds(Syam, dog)
If Syam gives the dog a bone will be: gives (Syam, dog, bone)
Are these the same?
In any sense does giving an object food constitute feeding?
If give(x, food) feed(x) then we are making progress. But we need to add certain inferential
rules.
In the famous program on relationships
Ravi is Sai’s cousin.
How do we represent this?
Ravi = daughter (brother or sister (father or mother (Sai)))
Suppose it is Sree then we do not know whether Sree is a male or female and then son
applies as well.
Clearly the separate levels of understanding require different levels of primitives and these
need many rules to link together apparently similar primitives.
Obviously there is a potential storage problem and the underlying question must be what
level of comprehension is needed.
Representing Set of Objects
There are certain properties of objects that are true as member of a set but not as individual;
for example, consider the assertion made in the sentences:
‘There are more sheep than people in India’, and ‘English speakers can be found all
over the world.’
To describe these facts, the only way is to attach assertion to the sets representing
people, sheep and English.
The reason to represent sets of objects is if a property is true to all or most of the
elements of a set, then it is more efficient to associate with the set rather than to associate it
explicitly with every elements of the set. This is done
 in logical representation through the use of a universal quantifier, and 

 in hierarchical structure where nodes represent sets and inheritance propagate a set
level assertion down to the individual. 

For example: assertion large (elephant), remember to make clear distinction between 

 whether we are asserting some property of the set itself, means, the set of elephants is
large, or 

 asserting some property that holds for individual elements of the set means any thing
that is an elephant is large. 
There are three ways in which sets may be represented:
(a)Name, for example the node Musician and the predicates Jazz and Avant Garde in
logical representation (see Fig. 3.8).
(c) Extensional definition is to list the numbers, and Intentional definition is to provide a
rule that returns true or false depending on whether the object is in the set or not.

Finding Right Structure


For accessing the right structure we can describe a particular situation. This requires
selecting an initial structure and then revising the choice. While doing so, it is
necessary to solve the following problems:

  how to perform an initial selection of the most appropriate structure 


 how to fill in appropriate details from the current situations 

 how to find a better structure if the one chosen initially turns out not to be appropriate
what to do if none of the available structures is appropriate 

 when to create and remember a new structure 
There is no good and general-purpose method for solving all these
problems. Some knowledge representation techniques solve some of them.
Let’s consider two problems:
1. How to select initial structure
2. How to find a better structure

Selecting an initial Structure: There are three important approaches for the selection of an
initial structure.
(a) Index the structures directly by specific English words. Let each
verb have own its structure that describeds the meaning.
Disadvantages
(i) Many words may have several different meanings. Eg: 1. John flew to Delhi.
2. John flew the kite.
‘flew’ here had different meaning in two different contexts.
(ii) It is useful only when there is an English description of the problem.
(b) Consider a major concept as a pointer to all of the
structures. Example:
1. The concept, Steak might point to two scripts, one for the
restaurant and the other for the super market.
2. The concept bill might point to as restaurant script or a shopping
script. We take the intersection of these sets; get the structure
that involves all the content words.
Disadvantages
1. If the problem contains extraneous concepts then the intersection
will result as empty.
2. It may require a great deal of computation to compute all the
possible sets and then to intersect them.
(c) Locate the major clue and use that to select an initial structure
Disadvantages
1. We can’t identify a major clue in some situations.
2. It is difficult to anticipate which clues are important and which are not.
(2) Revising the choice when necessary
Once we find a structure and if it doesn’t seem to be appropriate then we would opt
for another choice. The different ways in which this can be done are:
1. Select the fragments of the current structure and match them against the other
alternatives available. Choose the best one.
2. Make an excuse for the failure of the current structures and continue to use that.
There are heuristics such as the fact that a structure is more appropriate if a desired
feature is missing rather than having an inappropriate feature.
Example: A person with one leg is more plausible than a person with a tail.
3. Refer to specific links between the structures in order to follow new directions to
explore.
4. If the knowledge is represented in a ‘isa’ hierarchy then move upward until a
structure is found and they should not conflict with evidence. Use this structure if
it provides the required knowledge or create a new structure below it.

THE BASICS IN LOGIC

Logic is a language for reasoning; a collection of rules are used in logical reasoning. We
briefly discuss the following:
Types of logic
 Propositional logic - logic of sentences 

 Predicate logic - logic of objects 
  Logic involving uncertainties 
 Fuzzy logic - dealing with fuzziness 

 Temporal logic, etc 
Types of logic
Propositional logic and Predicate logic are fundamental to all logic.
Propositional logic

  Propositions are ‘Sentences’; either true or false but not both. 


 A sentence is the smallest unit in propositional logic. 

 If proposition is true, then truth value is ‘true’; else ‘false’. 

 Ex: Sentence – ‘Grass is green’; Truth value ‘true’;
Proposition is ‘yes’ 
Predicate logic
 Predicate is a function, may be true or false for arguments. 

 Predicate logic is rules that govern quantifiers. 

 Predicate logic is propositional logic added with quantifiers. Ex: ‘The
sky is blue’, 

‘The cover of this book is blue’ Predicate is ‘is
blue’, give a name ‘B’; 

Sentence is represented as ‘B(x)’, B(x) reads as ‘x is blue’; Object is
represented as ‘x’ 

Mathematical Predicate Logic is an important area of Artificial Intelligence: 

 Predicate logic is one of the major knowledge representation and reasoning
 methods. 
 Predicate logic serves as a standard of comparison for other representation and
 reasoning methods. 
 Predicate logic has a sound mathematical basis. 
 The PROLOG Language is based on logic. 
The form of predicate logic most commonly used in AI is first order predicate
calculus. Predicate logic requires that certain strong conditions be satisfied by the data being
represented:
 Discrete Objects: The objects represented must be discrete individuals like
 people, trucks, but not 1 ton of sugar. 
 Truth or Falsity: Propositions must be entirely true or entirely false, inaccuracy
 or degrees of belief are not representable. 
 Non-contradiction: Not only must data not be contradictory, but facts derivable
by rules must not contradict. 
In many applications information is to be encoded into certain database originate from
descriptive sentences that are difficult and unnatural to represent by simple structure called
Arrays or Sets. Hence logic is used to represent these sentences which we call predicate
calculus, which represents real-world facts, and has logical propositions written as well-
formed formula

Predicate logic is one in which a statement from a natural language like English is translated
into symbolic structures comprising predicates, functions, variables, constants, qualifiers and
logical connectives.
The syntax for the predicate logic is determined by the symbols that are allowed and
the rules of combination. The semantics is determined by the interpretations assigned to
predicates. The symbols and rules of combination of a predicate logic are:
(1) Predicate Symbol
(2) Constant Symbol
(3) Variable Symbol
(4) Function Symbol
In addition to these symbols, parenthesis, delimiter and comma are also used.
Predicate Symbol: This symbol is used to represent a relation in the domain of discourse.
For example: Valmiki wrote Ramayana – wrote (Valmiki, Ramayana)
In this, wrote is a Predicate. e.g., Rama loves Sita – loves (Rama, Sita).
Constant Symbol: A constant symbol is simple term and is used to represent object or
entities in the domain of discourse. These objects may be physical objects or people or anything
we name. e.g., Rama, Sita, Ramayana, Valmiki
Variable Symbol: These symbols are permitted to have indefinite values about the
entity which is being referred to or specified. e.g., X loves Y – loves(X, Y). In this X, Y are
variables.
Function Symbol: These symbols are used to represent a special type of relationship
or mapping. e.g., Rama’s father is married to Rama’s Mother – Married(father(Rama),
Mother(Rama)). In this Father and Mother are functions and married is Predicate.
Constants, variables and functions are referred to as terms and predicates are referred
to as atomic formulas or atoms. The statements in predicate logic are termed as well-formed
formula. A predicate with no variable is called a ground atom.
REPRESENTING SIMPLE FACTS IN LOGIC

Logic
Logic is concerned with the truth of statements about the world.
Generally each statement is either TRUE or FALSE.
Logic includes: Syntax, Semantics and Inference Procedure.
Syntax: Specifies the symbols in the language about how they can be combined to form
sentences. The facts about the world are represented as sentences in logic.
Semantic: Specifies how to assign a truth value to a sentence based on its meaning in
the world. It specifies what facts a sentence refers to. A fact is a claim about the world, and it
may be TRUE or FALSE.
Inference Procedure: Specifies methods for computing new sentences from an existing
sentence.

Note:
Facts are claims about the world that are True or False.
Representation is an expression (sentence), that stands for the objects and relations.

Sentences can be encoded in a computer program.


 Logic as a Knowledge Representation Language: Logic is a language for
reasoning, a collection of rules used while doing logical reasoning. Logic is
studied as KR languages in artificial intelligence.
 Logic is a formal system in which the formulas or sentences have true
or false values.
 The problem of designing a KR language is a tradeoff between that
which is:
i. Expressive enough to represent important objects and relations in
a problem domain.
ii. Efficient enough in reasoning and answering questions about
implicit information in a reasonable amount of time.
 Logics are of different types: Propositional logic, Predicate logic,
temporal logic, Modal logic, Description logic, etc;
They represent things and allow more or less efficient inference.
 Propositional logic and Predicate logic are fundamental to all logic.
Propositional Logic is the study of statements and their connectivity.
Predicate Logic is the study of individuals and their properties.
4.3.1.1 Logic representation
The Facts are claims about the world that are True or False.
Logic can be used to represent simple facts.
To build a Logic-based representation:
 User defines a set of primitive symbols and the associated semantics.
 Logic defines ways of putting symbols together so that user can define legal
sentences in the language that represent TRUE facts.
 Logic defines ways of inferring new sentences from existing ones.
 Sentences - either TRUE or false but not both are called propositions.
 A declarative sentence expresses a statement with a proposition as content;
Example:
The declarative ‘snow is white’ expresses that snow is white; further, ‘snow is
white’ expresses that snow is white is TRUE.
In this section, first Propositional Logic (PL) is briefly explained and then
the Predicate logic is illustrated in detail.
4.3.2 Propositional logic (PL)
A proposition is a statement, which in English would be a declarative sentence.
Every proposition is either TRUE or FALSE.
Examples: (a) The sky is blue. (b) Snow is cold. , (c) 12 * 12=144

Propositions are ‘sentences’, either true or false but not both.

A sentence is the smallest unit in propositional logic.


If proposition is true, then truth value is ‘true’. if proposition is false, then truth value is ‘false’. 
Example:

Sentence Truth value


Proposition (Y/N) ‘Grass
is green’ ‘true’ Yes
‘2 + 5 = 5’
‘false’ Yes
‘Close the
door’ - No
‘Is it hot
outside?’ -
No
‘x > 2’ where is variable - No (since x is
not defined) ‘x = x’ - No
(don’t know what is
‘x’ and ‘=‘; ‘3 = 3’
or ‘air is equal to
air’ or
‘Water is equal to water’ has no meaning)
– Propositional logic is fundamental to all logic.
– Propositional logic is also called Propositional calculus,
sentential calculus, or Boolean algebra.
– Propositional logic describes the ways of joining and/or
modifying entire propositions, statements or sentences to
form more complicated propositions, statements or
sentences, as well as the logical relationships and
properties that are derived from the methods of
combining or altering statements.
Statement, variables and symbols
These and a few more related terms such as, connective, truth value,
contingencies, tautologies, contradictions, antecedent, consequent
and argument are explained below.
– Statement: Simple statements (sentences), TRUE or FALSE,
that do not contain any other statement as a part, are basic
propositions; lower-case letters, p, q, r, are symbols for simple
statements.
Large, compound or complex statement are constructed from
basic propositions by combining them with connectives.
– Connective or Operator: The connectives join simple
statements into compounds, and join compounds into larger
compounds.
Table 4.1 indicates five basic connectives and their symbols:
– Listed in decreasing order of operation priority;
– An operation with higher priority is solved
first. Example of a formula: ((((a ⊄ ¬b) V c
’! d) ”! ¬ (a V c))

Table 4.1 Connectives and Symbols in Decreasing Order of Operation Priority

Connective Symbols Read as


assertion P ‘p is true’

negation ¬p ~ ! NOT ‘p is false’


conjunction pΛq · && & AND ‘both p and q are true’

‘either p is true, or q is
disjunction PVQ || | OR true, or both’

‘if p is true, then q is


implication p→q ⊃ ⇒ if..then true’ ‘p implies q’

‘p and q are either both


equivalence ↔ ≡ ⇔ if and only if true or both false’

Note: The propositions and connectives are the basic elements of propositional logic.

Truth value

The truth value of a statement is its TRUTH or FALSITY.


Example:
p is either TRUE or FALSE, ~p is either TRUE or FALSE,
p v q is either TRUE or FALSE, and so on. Use ‘ T ‘ or ‘ 1 ‘ to mean TRUE. use ‘ F ‘
or ‘ 0 ‘ to mean FALSE
Truth table defining the basic connectives:

p q ¬p ¬q P^ q pvq p’!q p ”! q q’!p

T T F F T T T T T

T F F T F T F F T

F T T F F T T F F

F F T T F F T T T
Tautologies
A proposition that is always true is called a tautology, e.g. (P v ¬P) is always true
regardless of the truth value of the proposition P.
Contradictions
A proposition that is always false is called a contradiction. e.g., (P ‘“ ¬P) is always false,
regardless of the truth value of the proposition P.
Contingencies
A proposition is called a contingency, if that proposition is neither a tautology nor a
contradiction, e.g. (P v Q) is a contingency.
Antecedent, Consequent
In the conditional statements, p ’! q , the 1st statement or ‘if - clause’ (here p) is called
antecedent, 2nd statement or ‘then - clause’ (here q) is called consequent.
Argument
Any argument can be expressed as a compound statement. Take all the premises, conjoin
them and make that conjunction the antecedent of a conditional and make the conclusion the
consequent. This implication statement is called the corresponding conditional of the
argument.
Note:
 Every argument has a corresponding conditional, and every implication statement has
a corresponding argument. 

 Because the corresponding conditional of an argument is a statement, it is therefore
either a tautology, or a contradiction or a contingency. 

 An argument is valid ‘if and only if ’ its corresponding conditional is a tautology. 

 Two statements are consistent ‘if and only if’ their conjunction is not a contradiction. 

 Two statements are logically equivalent ‘if and only if’ their truth table columns are
identical; ‘if and only if’ the statement of their equivalence using ‘a”’ is a tautology. 
Note
Truth tables are adequate to test validity, tautology, contradiction, contingency, consistency
and equivalence.
Here we will highlight major principles involved in knowledge representation. In
particular, predicate logic will be met in other knowledge representation schemes and
reasoning methods. The following are the standard logic symbols we use in this topic:
There are two types of quantifiers:
1. for all ∀
2. There exists ∃
Connectives:
Implies →
Not 
Or ∨
And ∧
Let us now look at an example of how predicate logic is used to represent knowledge.
There are other ways but this form is popular.
The propositional logic is not powerful enough for all types of assertions;
Example: The assertion ‘x > 1’, where x is a variable, is not a proposition because it is
neither true nor false unless value of x is defined. For x > 1 to be a proposition,
 either we substitute a specific number for x; 
 or change it to something like 
‘There is a number x for which x > 1 holds’;
or ‘For every number x, x > 1 holds’

Consider the following example:


‘All men are mortal.
Socrates is a man.
Then Socrates is mortal’,
These cannot be expressed in propositional logic as a finite and logically valid
argument (formula).
We need languages that allow us to describe properties (predicates) of objects, or a
relationship among objects represented by the variables. Predicate logic satisfies the
requirements of a language.
Predicate logic is powerful enough for expression and reasoning. 

Predicate logic is built upon the ideas of propositional logic. 
Predicate:
Every complete sentence contains two parts: a subject and a predicate. The subject is what
(or whom) the sentence is about. The predicate tells something about the subject;
Example:
A sentence ‘Judy {runs}’.
The subject is Judy and the predicate is runs.
Predicate always includes verb and tells something about the subject. Predicate is a
verb phrase template that describes a property of objects or a relation among objects
represented by the variables.
Example:
‘The car Tom is driving is blue’;
‘The sky is blue’;
‘The cover of this book is blue.’
Predicate is ‘is blue’, describes property.
Predicates are given names; Let ‘B’ be the name for predicate, ‘is blue’.
Sentence is represented as ‘B(x)’, read as ‘x is blue’;
‘x’ represents an arbitrary Object .
Predicate logic expressions:
The propositional operators combine predicates, like
If (p(....) && ( !q(....) || r (....) ) )
Examples of logic operators: disjunction (OR) and conjunction (AND).
Consider the expression with the respective logic symbols || and && x < y || ( y < z
&& z < x)
Which is true || (true && true); Applying truth table, found True
Assignment for < are 3, 2, 1 for x, y, z and then the value can be FALSE or
TRUE
3 < 2 || (2 < 1 && 1 < 3)
It is False
Predicate Logic Quantifiers
As said before, x > 1 is not a proposition and why?
Also said, that for x > 1 to be a proposition, what is required?
Generally, a predicate with variables (is called atomic formula) can be made a proposition
by applying one of the following two operations to each of its variables:
1. Assign a value to the variable; e.g. x > 1, if 3 is assigned to x becomes 3 > 1, and it
then becomes a true statement, hence a proposition.
2. Quantify the variable using a quantifier on formulas of predicate logic (called wff),
such as x > 1 or P(x), by using Quantifiers on variables.
Apply Quantifiers on Variables
Variable x
* x > 5 is not a proposition, its truth depends upon the value of variable x to reason such
statements, x needs to be declared.
Declaration x: a
* x: a declares variable
* x: a read as ‘x is an element of set a’
Statement p is a statement about x
* Q x: a • p is quantification of statement
statement declaration of variable x as element of set a quantifier
* Quantifiers are two types:
Universal quantifiers, denoted by symbol “ and existential quantifiers, denoted by symbol$.
Universe of Discourse
The universe of discourse is also called domain of discourse or universe andindicates:
– a set of entities that the quantifiers deal.
– entities can be set of real numbers, set of integers, set of all cars on a parking lot, the
set of all students in a classroom, etc.
– universe is thus the domain of the (individual) variables.
– propositions in predicate logic are statements on objects of a universe.
The universe is often left implicit in practice, but it should be obvious from the
context.
Examples:
– About natural numbers for All x, y (x < y or x = y or x > y), there is no need to be
more precise and say for All x, y in N, because N is implicit, being the universe of
discourse.
– About a property that holds for natural numbers but not for real numbers, it is
necessary to qualify what the allowable values of x and y is.
Apply Universal quantifier ‘For All’
Universal Quantification allows us to make a statement about a collection of objects.
* Universal quantification: x: a • p
* read ‘for all x in a, p holds’
* a is universe of discourse
* x is a member of the domain of discourse
* p is a statement about x
* In propositional form, it is written as: x P(x)
* read ‘for all x, P(x) holds’
‘for each x, P(x) holds’ or
‘for every x, P(x) holds’ where P(x) is predicate, x means all the objects x in the universe
P(x) is true for every object x in the universe
Example: English language to Propositional form
* ‘All cars have wheels’ x: car • x
has wheel
*x P(x) where P (x) is predicate tells: ‘x has wheels’ x is variable for object ‘cars’ that
populate universe of discourse
Apply Existential quantifier ‘There Exist’
Existential Quantification allows us to state that an object does exist without naming it.
Existential quantification: x: a • p
* read there exists an x such that p holds’
* a is universe of discourse
* x is a member of the domain of discourse.
* p is a statement about x
In propositional form it is written as: x P(x)
* read ‘there exists an x such that P(x)’ or ‘there exists at least one x such that P(x)’
* Where P(x) is predicate, x means at least one object x in the universe P(x) is true for least
one object x in the universe
Example: English language to Propositional form
* ‘Someone loves you’
x: Someone • x loves you
* x P(x),where P(x) is predicate tells : ‘x loves you’ x is variable for object ‘someone’ that
populate universe of discourse
Formula:
In mathematical logic, a formula is a type of abstract object, a token of which is a symbol or
string of symbols which may be interpreted as any meaningful unit in a formal language.
Terms:
Defined recursively as variables, or constants, or functions like f(t1, . . . , tn), where f is an n-
ary function symbol, and t1, . . . , tn are terms. Applying predicates to terms produce atomic
formulas.
Atomic formulas:
An atomic formula (or simply atom) is a formula with no deeper
propositional structure, i.e. a formula that contains no logical
connectives or a formula that has no strict sub-formulas.
Atoms are thus the simplest well-formed formulas of the logic.
– Compound formulas are formed by combining the atomic
formulas using the logical connectives.
– Well-formed formula (‘wiff’) is a symbol or string of
symbols (a formula) generated by the formal grammar of a formal
language. An atomic formula is one of the form:
– t1 = t2, where t1 and t2 are terms, or
– R(t1, . . . , tn), where R is an n-ary relation symbol, and t1, . . . , tn are terms.
– ¬ a is a formula when a is a formula.
– (a ‘“ b) and (a v b) are formula when a and b are formula
Compound formula: example
((((a ‘“ b) ‘“ c) (“ ((¬ a ‘“ b) ‘“ c)) (“ ((a ‘“ ¬ b) ‘“ c))
Example:
Consider the following:
– Prince is a megastar.
– Megastars are rich.
– Rich people have fast cars.
– Fast cars consume a lot of petrol.
and try to draw the conclusion: Prince’s car consumes a
lot of petrol. So we can translate
Prince is a megastar
into: megastar (prince)
and Megastars are rich
into: m: megastar (m) rich (m)
Rich people have fast cars, the third axiom is more difficult:
 Is cars a relation and therefore car (c, m) says that case c is m’s car. OR 
 Is a car a function? So we may have car_of (m). 
Assume cars are a relation then axiom 3
may be written: c, m: car (c, m)
rich (m) fast (c).
The fourth axiom is a general statement about fast cars. Let
consume(c) mean that car c consumes a lot of petrol. Then we may
write:
c: fast (c) m : car (c, m) consume (c) .
Is this enough? NO! — Does prince have a car? We need the
car_of function after all (and addition to car):
c: car (car_of (m), m).
The result of applying car_of to m is m’s car. The final set of
predicates is: megastar (prince)
m: megastar (m) rich (m)
c: car (car_of (m),m).
c, m: car (c, m) rich (m) fast(c).
c: fast(c) m : car (c, m) consume(c) .
Given this, we could conclude:
Consume (car_of (prince)).
Example: A Predicate Logic
1. Marcus was a man.
Man (Marcus)
2. Marcus was a Pompeian.
Pompeian (Marcus)
3. All Pompeians were Romans.
x: Pompeian(x)→Roman(x)
4. Caesar was a ruler.
Ruler (Caesar)
5. All Romans were either loyal to Caesar or hated him.
x: Roman(x) → loyalto(x, Caesar) V hate(x, Caesar)
6. Everyone is loyal to someone.
x: ∃y: loyalto(x, y)
7. People only try to assassinate rulers they aren’t loyal
to.
x:”y:person(x)L ruler(y) L tryassassinate(x, y)®Ø
loyalto(x, y)
8. Marcus tried to
assassinate Caesar.
tryassassinate(Marcu
s, Caesar)
9. All men are people.
x: man(x) → person(x)

REPRESENTING INSTANCE AND ISA


RELATIONSHIPS

The two attributes, isa and instance play an important role in many aspects of knowledge
representation. The reason for this is that they support property inheritance.
isa
Used to show class inclusion. Ex. isa(megastar, rich).
instance
Used to show class membership, Ex. instance (prince, megastar).
From the above it should be simple to see how to represent these in predicate logic

COMPUTABLE FUNCTIONS AND PREDICATES

The objective is to define class of functions C computable in terms of F. This is expressed as


C {F} is explained below using two examples:
(1) ‘evaluate factorial n’ and
(2) ‘expression for triangular functions’.
Ex: (1) A conditional expression to define factorial n or n!
Expression
‘If p1 then e1 else if p2 then e2 . . . else if pn then en’ . i.e. (p1
’!?e1, p2 ’!?e2 . . . pn ’!?en)
Here p1, p2 . . . pn are propositional expressions taking the values T or F for true and
false respectively.
The value of (p1 ’!?e1, p2 ’!?e2 . . . pn ’!?en) is the value of the e corresponding to the
first p that has value T.
The expressions defining n! , n= 5, recursively are: n! = n x
(n-1)! for n e”?1
5! = 1 x 2 x 3 x 4 x 5 = 120 0! = 1
The above definition incorporates an instance that the product of no numbers i.e. 0! =
1, then only, the recursive relation
(n + 1)! =n! X (n+1) works for n = 0. Now, use
conditional expressions
n! = (n = 0 ’!?1, n ‘“ 0 ’!?n. (n – 1)!) to define
functions recursively.
Ex: Evaluate 2! According to above definition. 2! = (2 =
0 ’!?1, 2 ‘“ 0 ’!?2. (2 – 1)! )
= 2 x 1!
= 2 x (1 = 0 ’!?1, 1 ‘“ 0 ’!?1. (1 – 1)! )
= 2 x 1 x 0!
= 2 x 1 x (0 = 0 ’!?1, 0 ‘“ 0 ’!?0. (0 – 1)! )
=2x1x1
=2

RESOLUTION

Resolution is a procedure used in proving that arguments which are expressible in predicate
logic are correct. Resolution is a procedure that produces proofs by refutation or
contradiction. Resolution leads to refute a theorem-proving technique for sentences in
propositional logic and first-order logic.
Resolution is a purely syntactic uniform proof procedure. Resolution does not consider what
predicates may mean, but only what logical conclusions may be derived from the axioms.
The advantage is Resolution is universally applicable to problems that can be described in
first order logic. The disadvantage is Resolution by itself cannot make use of any domain
dependent heuristics. Despite many attempts to improve the efficiency of resolution, if often
takes exponential time.
– Resolution is a rule of inference.
– Resolution is a computerized theorem prover.
– Resolution is so far only defined for Propositional Logic.
The strategy is that the Resolution techniques of Propositional logic be adopted in
Predicate Logic.
1. Proof in predicate logic is composed of a variety of processes including modus
ponens, chain rule, substitution, matching, etc.
2. However, we have to take variables into account, e.g. parent of(Ram, Sam) AND
Not Parent of(Ram, Lakshman) cannot be resolved.
3. In other words, the resolution method has to look inside predicates to discover if
they can be resolved.
Algorithm: Convert to Clausal Form
1. Eliminate→
P → Q ≡ P ∨ Q
2. Reduce the scope of each  to a single term.
(P ∨ Q) ≡ P ∧ Q (P ∧
Q) ≡ P ∨ Q
‘x: P ≡ ∃x: P
x: p ≡ ‘x: P
 P ≡ P
3. Standardize variables so that each quantifier binds a unique variable.
∀x: P(x)) ∨ (∃x: ≡∀
( Q(x)) ( x: P(x)) ∨ (∃y: Q(y))
4. Move all quantifiers to the left without changing their relative order.
∀x: P(x)) ∨ (∃y: ∀x: ∃y: (P(x)
( Q(y)) ≡ ∨ (Q(y))
Eliminate ∃
5. (Solemnization).
∃x: P(x) ≡ P(c) Skolem constant
∀x: ∃y P(x, y) ∀
≡ x: P(x, f(x)) Skolem function
6. Drop ∀ .
∀ x: P(x) ≡ P(x)
7. Convert the formula into a conjunction of disjuncts. (P ∧ Q) ∨
R ≡ (P ∨ R) ∧ (Q ∨ R)
8. Create a separate clause for each conjunction.
9. Standardize apart the variables in the set of obtained clauses.
Example:1
∀ x: [Roman(x) Λ know(x, Marcus)] →[hate(x, Caesar) V ( ∀ y: ∃z:
hate(y, z) → thinkcrazy(x, y))]
1. Eliminate →
∀ x: [Roman(x) Λ know(x, Marcus)] V [hate(x, Caesar) V ( ∀ y: z: hate(y,
z) V thinkcrazy(x, y))]
2. Reduce scope of.
∀ x: [ Roman(x) V  know(x, Marcus)] V [hate(x, Caesar) V ( ∀ y: z: hate(y,
z) V thinkcrazy(x, y))]
3. ‘Standardize’ variables:
∀ x: P(x) V ∀ x: Q(x) converts to ∀ x: P(x) V ∀ y: Q(y)
4. Move quantifiers.
∀ x: ∀ y: ∀ z: [Roman(x) V  know(x, Marcus)] V [hate(x,
Caesar) V (hate(y, z) V thinkcrazy(x, y))]
5. Eliminate existential quantifiers.
y: President(y) will be converted to President(S1)
∀ x: ∃y: father-of(y, x) will be converted to ∀ x: father-of(S2(x), x)) 6. Drop the
prefix.
[ Roman(x)  know(x, Marcus)] V [hate(x, Caesar) V ( hate(y, z) V
thinkcrazy(x, y))]
7. Convert to a conjunction of disjuncts.
 Roman(x) V  know(x, Marcus) V hate(x, Caesar) V  hate(y, z) V
 thinkcrazy(x, y) 
8. Create a separate clause corresponding to each conjunct.
9. Standardize apart the variables in the set of clauses generated step 8.
First-Order Resolution
_ Generalized Resolution Rule
For clauses P V Q and Q′VR with Q, Q′ atomic formulae

_ where θ is a most general unifier for Q and Q′ _ (P v R)


θ is the resolvent of the two clauses
Applying Resolution Refutation
– Negate query to be proven (resolution is a refutation system)
– Convert knowledge base and negated query into CNF and extract clauses
– Repeatedly apply resolution to clauses or copies of clauses until either the empty
clause (contradiction) is derived or no more clauses can be derived (a copy of a clause
is the clause with all variables renamed)
– If the empty clause is derived, answer ‘yes’ (query follows from knowledge base);
otherwise, answer ‘no’ (query does not follow from knowledge base).
Resolution: Example 1
|- ∃x (P(x) →∀xP(x)) CNF
(x(P(x)→∀xP(x)))
1, 2. ∀x(P(x)(∀∀xP(x)) 2. ∀x
(P(x)’xP(x)) 2, 3. ∀x (P(x) ‘∨
∃xP(x))
4. ∀x (P(x)∧∃y P(y))
5. ∀x∃y(P(x)P(y))
6. ∀x (P(x)P( f (x)))
8. P(x), P( f (x))
1. P(x) [ Conclusion]
2. P( f (y)) [Copy of  Conclusion]
3. _ [1, 2 Resolution {x/ f (y)}]

Resolution — Example 2
|- ∃x∀y∀z ((P(y)(∨Q (z)) → (P(x)(∨Q(x)))
1. P ( f (x))(∨Q(g(x)) [ Conclusion]
2. P(x) [ Conclusion]
3. Q(x) [ Conclusion]
4. P(y) [Copy of 2]
5. Q(g(x)) [1, 4 Resolution {y/ f (x)}]
6. Q(z) [Copy of 3]
7. _ [5, 6 Resolution {z/g(x)}]

4.6.2 The Basis of Resolution


The resolution procedure is a simple iterative process: at every step two clauses (parent
clauses) are compared for deriving a new clause that has been inferred from them. The new
clause represents that the two parent clauses interact with each other.
4.6.2.1 Soundness and completeness
The notation p |= q is read ‘p entails q’; it means that q holds in every model in which p
holds.
The notation p |- q means that q can be derived from p by some proof mechanism m.
A proof mechanism m is sound if p |-m q → p |= q.
A proof mechanism m is complete if p |= q → p |-m q. Resolution for
predicate calculus is:
Sound: If it is derived by resolution, then the original set of clauses is unclassifiable.
Complete: If a set of clauses is unclassifiable, resolution will eventually derive.
However, this is a search problem and may take a very long time.
We generally are not willing to give up soundness, since we want our conclusions to be valid.
We might be willing to give up completeness: if a soundproof procedure
will prove the theorem we want, that is enough.
Example: Resolution
– Another type of proof system based on refutation
– Better suited to computer implementation than systems of axioms and rules (can give
correct ‘no’ answers)
– Generalizes to first-order logic (see next week)
– The basis of Prolog’s inference method
– To apply resolution, all formulae in the knowledge base and the query must be in
clausal form (c.f. Prolog clauses)
Resolution Rule of Inference
Resolution Rule

P∨Q Q′ ∨ R

(P ∨ Q) θ

Resolution Rule: Key Idea


_ Consider A V B and B V C
if B is True, B is False and truth of second formula depends only on C if B is False,
truth of first formula depends only on A
Only one of B, B is True, so if both A V B and B V C is True, either A or C is True, i.e.
A V C is True.
Applying Resolution
– The resolution rule is sound (resolvent entailed by two ‘parent’ clauses)
– How can we use the resolution rule? One way:
– Convert knowledge base into clausal form
– Repeatedly apply resolution rule to the resulting clauses
– A query A follows from the knowledge base if and only if each of the clauses in the
CNF of A can be derived using resolution
– There is a better way . . .
Refutation Systems
– To show that P follows from S (i.e. S P) using refutation, start with S and âÿP in
clausal form and derive a contradiction using resolution
– A contradiction is the ‘empty clause’ (a clause with no literals)
– The empty clause _ is unsatisfiable (always False)
– So if the empty clause _ is derived using resolution, the original set of clauses is
unclassifiable (never all True together)
– That is, if we can derive _ from the clausal forms of S and âÿP, these clauses can never
be all True together
– Hence whenever the clauses of S are all True, at least one clause from âÿP must be
False, i.e. âÿP must be False and P must be True
– By definition, S |= P (so P can correctly be concluded from S)
Applying Resolution Refutation
– Negate query to be proven (resolution is a refutation system)
– Convert knowledge base and negated conclusion into CNF and extract clauses
– Repeatedly apply resolution until either the empty clause (contradiction) is derived or
no more clauses can be derived
– If the empty clause is derived, answer ‘yes’ (query follows from knowledge base),
otherwise answer ‘no’ (query does not follow from knowledge base)
Resolution: Example 1
(G V H)→( J Λ K), G J
Clausal form of (G V H) → (J Λ K) is
{G V J, H V J, G V K, H V K}
1. G V J [Premise]
2. H V J [Premise]
3. G V K [Premise]
4. H V K [Premise]
5. G [Premise]
6. J [ Conclusion]
7. G [1, 6 Resolution]
8. _ [5, 7 Resolution]
Resolution: Example 2
P→Q, Q→R |– P’!R
Recall P→R ≡ P V R
Clausal form of (P V R) is {P, R}
1. P V Q [Premise]
2. Q V R [Premise]
3. P [ Conclusion]
4. R [ Conclusion]
5. Q [1, 3 Resolution]
6. R [2, 5 Resolution]
7. _ [4, 6 Resolution]
Resolution: Example 3
((P V Q) ∧ P)→Q
Clausal form of (((P V Q) ∧ P)→Q) is {P V Q, P, Q}
1. P V Q [ Conclusion]
2. P [ Conclusion]
3. Q [ Conclusion]
4. Q [1, 2 Resolution]
5. [3, 4 Resolution]
Soundness and Completeness Again
– Resolution refutation is sound, i.e. it preserves truth (if a set of premises are all true,
any conclusion drawn from those premises must also be true)
– Resolution refutation is complete, i.e. it is capable of proving all consequences of any
knowledge base (not shown here!)
– Resolution refutation is decidable, i.e. there is an algorithm implementing resolution
which when asked whether S |– P, can always answer ‘yes’ or ‘no’ (correctly)
Heuristics in Applying Resolution
– Clause elimination—can disregard certain types of clauses
– Pure clauses: contain literal L where  L doesn’t appear elsewhere
– Tautologies: clauses containing both L and âÿL
– Subsumed clauses: another clause exists containing a subset of the literals
– Ordering strategies
– Resolve unit clauses (only one literal) first
– Start with query clauses
– Aim to shorten clauses
4.6.2.2 Resolution strategies

Different strategies have been tried for selecting the clauses to be resolved. These include:
a. Level saturation or two-pointer method: the outer pointer starts at the negated
conclusion; the inner pointer starts at the first clause. The two clauses denoted by
the pointers are resolved, if possible, with the result added to the end of the list of
clauses. The inner pointer is incremented to the next clause until it reaches the
outer pointer; then the outer pointer is incremented and the inner pointer is reset
to the front. The two-pointer method is a breadth-first method that will generate
many duplicate clauses.
b. Set of support: One clause in each resolution step must be part of the negated
conclusion or a clause derived from it. This can be combined with the two-
pointer method by putting the clauses from the negated conclusion at the end of
the list. Set-of-support keeps the proof process focused on the theorem to be
proved rather than trying to prove everything.
c. Unit preference: Clauses are prioritized, with unit clauses preferred, or more
generally shorter clauses preferred. To reach our goal, this has zero literals and is
only obtained by resolving two unit clauses. Resolution with a unit clause makes
the result smaller.
d. Linear resolution: one clause in each step must be the result of the previous step. This is
a depth-first strategy. It may be necessary to back up to a previous clause if no resolution
with the current clause is possible.

4.6.3 Resolution in Propositional Logic

Algorithm: Propositional Resolution


1. Convert all the propositions of KB to clause form (S).
2. Negate a and convert it to clause form. Add it to S.
3. Repeat until either a contradiction is found or no progress can be made.
a. Select two clauses (α ∨ P) and ( γ ∨ P).
b. Add the resolvent (α ∨ γ) to S.
Ex: 1. KB = {P, (P ∧ Q) → R, (S ∨ T) → Q, T}
α = R then
KB = {P(a), ‘x: (P(x) ∧ Q(x)) → R(x), ‘y: (S(y) ∨ T(y)) →
Q(y), T(a)}
α = R(a)
Ex: 2. A few Facts in Prepositional Logic

Given Axioms 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 T (5)

A substitution set can be represented as either sequential substitutions (done one at a


time in sequence) or as simultaneous substitutions (done all at once). Unification can be
done correctly either way.
4.6.4.2 Unification algorithm
The basic unification algorithm is simple. However, it must be implemented with care to ensure
that the results are correct. We begin by making sure that the two expressions have no variables
in common. If there are common variables, substitute a new variable in one of the expressions.
(Since variables are universally quantified, another variable can be substituted without changing
the meaning.)
Imagine moving a pointer left-to-right across both expressions until parts are
encountered that are not the same in both expressionsL for example, one is a variable and
the other is a term not containing that variable?
1. Substitute the term for the variable in both expressions,
2. substitute the term for the variable in the existing substitution set [This is
necessary so that the substitution set will be simultaneous].
3. add the substitution to the substitution set.
Algorithm: Unify (L1, L2)
1. If L1 or L2 is a variable or constant, then:
(a) If L1 and L2 are identical, then return NIL.
(b) Else if L1 is a variable, then if L1 occurs in L2 then return FAIL, else return
{(L2/L1)}.
(c) Else if L2 is a variable, then if L2 occurs in L1 then return FAIL, else return
{(L1/L2)}.
(d) Else return FAIL.
2. If the initial predicate symbols in L1 and L2 are not identical, then return FAIL.
3. If L1 and L2 have a different number of arguments, then return FAIL
4. Set SUBST to NIL.
5. for i 1 to number of arguments in L1:
a) Call Unify with the ith argument of L1 and the ith argument of L2, putting result
in S.
b) If S = FAIL then return FAIL.
c) If S is not equal to NIL then:
i. Apply S to the remainder of both L1 and L2.
ii. SUBST: = APPEND(S, SUBST).
6. Return SUBST.
4.6.4.3 Unification implementation
1. Initialize the substitution set to be empty. A nil set indicates failure.
2. Recursively unify expressions:
1. Identical items match.
2. If one item is a variable vi and the other is a term ti not containing that variable,
then:
1. Substitute ti / vi in the existing substitutions.
2. Add ti / vi to the substitution set.
3. If both items are functions, the function names must be identical and all
arguments must unify. Substitutions are made in the rest of the expression as
unification proceeds.

Unification
i) We can get the inference immediately if we can find a substitution θ such that

King(x) and Greedy(x) match King (John) and Greedy(y)


θ = {x/John, y/John} works
Unify (α, β) = θ if αθ = βθ
p q θ
Knows (John, x) Knows (John, Jane) ——
Knows (John, x) Knows (y, OJ)
Knows (John, x) Knows (y, Mother(y))
Knows (John, x) Knows (x, OJ)

Standardizing apart eliminates overlap of variables, e.g. Knows (z17, OJ)


ii) We can get the inference immediately if we can find a substitution θ such that
King(x) and Greedy(x) match King (John) and Greedy(y)

θ = {x/John, y/John} works Unify (α, β) = θ if αθ = βθ

P q θ
Knows (John, x) Knows (John,Jane) {x/Jane}}
Knows (John, x) Knows(y, OJ)
Knows (John, x) Knows(y, Mother(y))
Knows (John, x) Knows(x, OJ)

θ = {x/John, y/J Standardizing apart eliminates overlap of variables, e.g., Knows (z17, OJ)

iii) We can get the inference immediately if we can find a substitution θ such that King(x) and
Greedy(x) match King(John) and Greedy(y)

θ = {x/John, y/John} works


Unify (α, β) = θ if αθ = βθ
p q θ
Knows (John, x) Knows (John, Jane) {x/Jane}}
Knows (John, x) Knows (y, OJ) {x/OJ, y/
John}}
Knows (John, x) Knows (y, Mother(y))
Knows (John, x) Knows(x, OJ)

ohn} works Unify (α, β) =


Standardizing apart eliminates overlap of variables, e.g. Knows (z17, OJ)
iv) We can get the inference immediately if we can find a substitution θ such that King(x) and
Greedy(x) match King(John) and Greedy(y)

θ = {x/John,y/John} works
Unify(α,β) = θ if αθ = βθ
p q θ
Knows (John, x) Knows (John, Jane) {x/Jane}}
Knows (John, x) Knows (y, OJ) {x/OJ, y/John}}
Knows (John, x) Knows (y, Mother(y)) {y/John, x/Mother
(John)}}
Knows (John, x) Knows (x, OJ)
Standardizing apart eliminates overlap of variables, e.g. Knows (z17, OJ)
v) We can get the inference immediately if we can find a substitution θ such that King(x) and
Greedy(x) match King (John) and Greedy(y)
θ = {x/John, y/John} works Unify (α, β) = θ if αθ = βθ

p q θ
Knows (John, x) Knows (John, Jane) {x/Jane}}
Knows (John, x) Knows (y, OJ) {x/OJ, y/John}}
Knows (John, x) Knows (y, Mother(y)) {y/John, x/Mother
(John)}}
Knows (John, x) Knows (x, OJ) {fail}

Standardizing apart eliminates overlap of variables, e.g. Knows (z17, OJ)


To unify Knows (John, x) and Knows (y, z), θ = {y/John, x/z } or θ = {y/John, x/John, z/John}
The first unifier is more general than the second.
There is a single most general unifier (MGU) that is unique up to renaming of variables.
MGU = { y/John, x/z }
4.6.5 Resolution in Predicate Logic
Algorithm: Resolution
1. Convert all the propositions of F to clause form.
2. Negate P and convert to clause form. Add it to the set of clauses obtained in 1.
3. Repeat until a contradiction is found, no progress can be made, or a predetermined
amount of effort has been expanded.
a. Select two clauses. Call these parent clauses.
b. Resolve them together. The resolvent will be the disjunction of all the literals of
both parent clauses with appropriate substitutions performed and with the
following exception: If there is one pair of literals T1 and  T2, such that one of
the parent clauses contains T1 and the other contains  T2 and if T1 and T2 are
unifiable, then neither T1 nor  T2 should appear in the resolvent. If there is
more than one pair of complementary literals, only one pair should be omitted
from the resolvent.
c. If the resolvent is the empty clause, then a contradiction has been found. If it is not,
then add it to the set of clauses available to the procedure.
Example:
1. Marcus was a man.
2. Marcus was a Pompeian.
3. All Pompeians were Romans.
4. Caesar was a ruler.
5. All Pompeians were either loyal to Caesar or hated him.
6. Every one is loyal to someone.
7. People only try to assassinate rulers they are not loyal to.
8. Marcus tried to assassinate Caesar.
  Axioms in clause form are: 
1. man(Marcus)
2. Pompeian(Marcus)
3.  Pompeian(x1) v Roman(x1)
4. Ruler(Caesar)
5.  Roman(x2) v loyalto(x2, Caesar) v hate(x2, Caesar)
6. loyalto(x3, f1(x3))
7.  man(x4) v  ruler(y1) v  tryassassinate(x4, y1) v loyalto(x4, y1)
8. tryassassinate(Marcus, Caesar)

Prove: hate (Marcus, Caesar)

Prove: hate (Marcus, Caesar) hate(Marcus, Caesar) 5

3 Roman(Marcus) V loyalto(Marcus, Caesar)

Marcus/x
Pompeion(Marcus) V loyalto(Marcus, Caesar) 2

7 loyalto(Marcus, Caesar)
Marcus/x4, Caesar/y1

1 man(Marcus) V ruler (Caesar)V tryassassinate(Marcus, Caesar)

ruler (Caesar)V tryassassinate(Marcus, Caesar) 4

tryassassinate(Marcus, Caesar) 8

Fig. 4.2 Resolution Proof

Prove: loyalto (Marcus, Caesar) loyalto(marcus, caesar) 5

Marcus/x2
3 Roman(Marcus) V hate(Marcus, Caesar)
Marcus/x1
Pompeion(Marcus) V hate(Marcus, Caesar) 2

hate(Marcus, Caesar)
(a)

hate(Marcus, Caesar) 10

Marcus/x6, Caesar/y3
Presecute(Caesar, Marcus,) 9

Marcus/x5, Caesar/y2 hate(Marcus,


Caesar)
:
(b)
:
Fig. 4.3 An Unsuccessful Attempt at Resolution
SEMANTIC NETS

The simplest kind of structured objects are the semantic nets, which were originally
developed in the early 1960s to represent the meaning of English words. They are important
both historically, and also in introducing the basic ideas of class hierarchies and inheritance.
A semantic net is really just a graph, where the nodes in the graph represent concepts,
and the arcs represent binary relationships between concepts. The most important relations
between concepts are subclass relations between classes and subclasses, and instance
relations between particular objects and their parent class. However, any other relations are
allowed, such as has-part, colour, etc. So, to represent some knowledge about animals (as AI
people so often do) we might have the following network:
This network represents the fact that mammals and reptiles are animals, that mammals
have heads, an elephant is a large grey mammal, Clyde and Nellie are both elephants and
that Nellie likes apples. The subclass relations define a class hierarchy (in this case very
simple).
The subclass and instance relations may be used to derive new information, which is
not explicitly represented. We should be able to conclude that both Clyde and Nellie have a
head and are large and grey. They inherit information from their parent classes. Semantic
networks normally allow efficient inheritance-based inferences using special-purpose
algorithms.
Semantic nets are fine at representing relationships between two objects, but what if we want
to represent a relation between three or more objects? Let us say we want to represent the
fact that ‘John gives Mary the book’. This might be represented in logic as gives(john, mary,
book2) where book2 represents the particular book we are talking about. However, in
semantic networks we have to view the fact as representing a set of binary relationships
between a ‘giving’ event and some objects.
When semantic networks became popular in the 1970s, there was much discussion about what
the nodes and relations really meant. People were using them in subtly different ways, which led to
much confusion. For example, a node such as elephant might be used to represent the class of all
elephants or just a typical elephant. Saying that an elephant has_part head could mean that every
elephant has some particular head, that there exists some elephant that has a head, or (more
reasonably in this case) that they all have some object belonging to the class head. Depending on
what interpretation you choose for your nodes and links, different inferences are valid. For example,
if it’s just a typical elephant, then Clyde may have properties different from general elephant
properties (such as being pink and not grey).
The simplest way to interpret the class nodes is as denoting sets of objects. So, an
elephant node denotes the set of all elephants. Nodes such as Clyde and Nellie denote
individuals. So, the instance relationship can be defined in terms of set membership (Nellie
is a member of the set of all elephants), while the subclass relation can be defined in terms of
a subset relation— the set of all elephants is a subset of the set of all mammals. Saying that
elephants are grey means (in the simple model) that every individual in the set of elephants is
grey (so Clyde can’t be pink). If we interpret networks in this way we have the advantage of
a clear, simple semantics, but the disadvantage of a certain lack of flexibility—maybe Clyde
is pink!
In the debate about semantic nets, people were also concerned about their
representational adequacy (i.e. what sort of facts they were capable of representing). Things
that are easy to represent in logic (such as ‘every dog in town has bitten the constable’) are
hard to represent in nets (at least, in a way that has a clear and well-defined interpretation).
Techniques were developed to allow such things to be represented, which involved
partitioning the net into sections, and introducing a special relationship. These techniques
didn’t really catch on, so we won’t go into them here, but they can be found in many AI
textbooks.
To summarize, nets allow us to simply represent knowledge about an object that can
be expressed as binary relations. Subclass and instance relations allow us to use inheritance
to infer new facts/relations from the explicitly represented one. However, early nets didn’t
have very clear semantics (i.e. it wasn’t clear what the nodes and links really meant). It was
difficult to use nets in a fully consistent and meaningful manner, and still use them to
represent what you wanted to represent. Techniques evolved to get round this, but they are
quite complex, and seem to partly remove the attractive simplicity of the initial idea.
A Semantic Net is a formal graphic language representing facts about entities in some world
about which we wish to reason. The meaning of a concept comes from the ways in which it is
connected to other concepts. Computer languages cannot tolerate the ambiguity of natural
languages. It is therefore necessary to have well-defined sets of predicates and arguments for a
domain. Semantic nets for realistic problems become large and cluttered; the problems need to be
broken down.
Semantic nets are a simple way of representing the relationships between entities and
concepts

The major idea is that:


 The meaning of a concept comes from its relationship to other concepts, and that, 
 The information is stored by interconnecting nodes with labelled arcs. Let’s
discuss the following aspects of Semantic Nets: 

  Representation in a Semantic Net 
 Inference in a Semantic Net 

 Extending Semantic Nets 
5.2.1 Representation in a Semantic Net
The physical attributes of a person can be represented as in Figure 5.1.

Mammal

is a
has_part
Person Head

instance
team_colours
team
Black/Blue Dhoni India

Fig. 5.1 A Semantic Network

These values can also be represented in logic as: isa (person, mammal), instance
(Dhoni, person), team(Dhoni, India).
We have already seen how conventional predicates such as lecturer (Rao) can be
written as instance (Rao, lecturer). Recall that isa and instance represent inheritance and are
popular in many knowledge representation schemes. But we have a problem: How we can
have more than 2 place predicates in semantic nets? E.g. score (India, Australia, 236).
Solution:
 Create new nodes to represent new objects either contained or alluded to in the
knowledge, game and fixture in the current example. Relate information to nodes
and fill up slots (see Fig. 5.2). 
Cricket Match

isa
away-team
score
Australia Fixture 3 236

Home-team

India

Fig. 5.2 A Semantic Network for n-Place Predicate


As a more complex example consider the sentence: Sam gave Ravi a book. Here we have
several aspects of an event ( see Fig. 5.3).

gave book

instance
agent object
Sam event 1 book 13

receiver

Ravi

Fig. 5.3 A Semantic Network for a Sentence

Inference in a Semantic Net


Basic inference mechanism: follow links between nodes.
Two methods to do this:
Intersection search

The notion that spreading activation out of two nodes and finding their intersection finds
relationships among objects. This is achieved by assigning a special tag to each visited node.
The advantages include the entity-based organization and fast parallel implementation.
However, every structured question need highly structured networks.
Inheritance
The isa and instance representations provide a mechanism to implement this. Inheritance
also provides a means of dealing with default reasoning. e.g., we could represent:

  Emus are birds. 

  Typically birds fly and have wings. 


 Emus run. 

In the following Semantic net: 

fly

action
has_part
bird wings

instance
action
emu run

Fig. 5.4 A Semantic Network for a Default Reasoning

In making certain inferences we will also need to distinguish between the link that
defines a new entity and holds its value and the other kind of link that relates two existing
entities. Consider the example shown where the height of two people is depicted and we also
wish to compare them.
We need extra nodes for the concept as well as its value.

Sam Ravi

height height

160 170

Fig. 5.5 Two Heights

Special procedures are needed to process these nodes, but without this distinction the
analysis would be very limited.

Sam Ravi
height height
greater than
H1 H2

value value

160 170

Fig. 5.6 Comparison of Two Heights

Defaults and inheritance


 Defaults and inheritance are ways of achieving some common sense. 

 Inheritance is a way of reasoning by default, that is, when information is missing, fall
 back to defaults. 
 Semantic networks represent inheritance. 


grey 1
skin tail

elephant
4 trunk
legs

isa 1

e1
name

clyde

Using inheritance
 To find the value of a property of e1, first look at e1. 

 If the property is not attached to that node, ‘climb’ the isa link to the node’s parent
 and search there. 

isa signifies set membership:

ako signifies the subset relation:
 Repeat, using isa/ako links, until the property is found or the inheritance hierarchy is
 exhausted. 

  Sets of things in a semantic network are termed types. 


 Individual objects in a semantic network are termed instances. 

Examples of Semantic Networks


State: I own a tan leather chair.

furniture

ako
part
person chair seal

isa isa
owner colour
me my-chair tan

covering isa

leather brown

Event: John gives the book to Mary.


give

isa
agent object
John event7 book23

isa beneficiary isa

person isa
Mary book

Complex event: Bilbo finds the magic ring in Gollum’s cave.

isa ako
Bilbo habbit person

agent

event9 object magicring2 isa ring


isa location
owned-by Gollum

find cave77 cave


isa
Extending Semantic Nets
Here, we will consider some extensions to Semantic nets that overcome a few problems (see
Questions and Exercises) or extend their expression of knowledge.
Partitioned Networks
Partitioned Semantic Networks allow for:
 propositions to be made without commitment to truth. 

 expressions to be quantified. 
Basic idea: Break network into spaces which consist of groups of nodes and arcs and
regard each space as a node.
Consider the following: John believes that the earth is flat. We can encode the
proposition the earth is flat in a space and within it have nodes and arcs that represent the
fact (see Fig. 5.7). We can have the nodes and arcs to link this space with the rest of the
network to represent John’s belief.

believes
instance
agent
John
event1 object
space 1
earth flat
instance insurance
Object 1 Prop 1
has_property

Fig. 5.7 Partitioned Network

Now consider the quantified expression: Every parent loves their child.
To represent this we:
 Create a general statement, GS, special class. 

  Make node g an instance of GS. 
  Every element will have at least 2 attributes: 

a form that states which relation is being asserted.

one or more forall ( ∀) or exists (∃ ) connections—these represent universally
quantifiable variables in such statements e.g. x, y in
→∃ y
∀x parent(x) : child(y) ∧
loves(x, y)
Here we have to construct two spaces one for each x, y.
Note: We can express variables as existentially qualified variables and express the event of
love having an agent p and receiver b for every parent p which could simplify the network.
Also, if we change the sentence to Every parent loves child then the node of the object being
acted on (the child) lies outside the form of the general statement Thus it is not viewed as an
existentially qualified variable whose value may depend on the agent. So we could construct
a partitioned network as in Figure 5.8.
instance
GS

instance

gs1
parent child loves
form

form
gs2
instance instance
forall
c2 i3
exists receiver
space2

instance
agent
p1
space1

Fig. 5.8 ∀ (for all) Partitioned Network


Construct the semantic nets for the following sentences:
1. The dog bit the mail carrier.
2. Every dog has bitten a mail carrier.
3. Every dog in town has bitten the mail carrier.
4. Every dog has bitten every mail carrier.
1. The dog bit the mail carrier.

Dogs Bite mail carrier

isa isa isa

d b m

assailant victim
2. Every dog has bitten a mail carrier.

SA
GS Dogs Bite mail carrier

isa isa
isa isa
form S1
g d b m
assailant victim
3. Every dog in town has bitten the mail carrier.

Dogs

SA
GS Town Dogs Bite mail carrier

isa isa isa isa


form S1
g d b C
assailant victim

Every dog has bitten every mail carrier.

SA
Dogs Bite mail carrier

isa isa isa

d b m
assailant victim

GS g form





























UNIT – IV (6 Lectures)

Learning: Introduction learning, Rote learning, Learning by taking advice,


Learning in problem Solving , Learning from example-induction, Explanation
based learning. Expert System: Introduction, Representing using domain
specific knowledge, Expert system Shells .


 Learning − It is the activity of gaining knowledge or skill by studying,
practising, being taught, or experiencing something. Learning enhances
the awareness of the subjects of the study.

The ability of learning is possessed by humans, some animals, and AI-


enabled systems. Learning is categorized as −

o Auditory Learning − It is learning by listening and hearing. For


example, students listening to recorded audio lectures.

o Episodic Learning − To learn by remembering sequences of


events that one has witnessed or experienced. This is linear and
orderly.

o Motor Learning − It is learning by precise movement of muscles.


For example, picking objects, Writing, etc.

o Observational Learning − To learn by watching and imitating


others. For example, child tries to learn by mimicking her parent.

o Perceptual Learning − It is learning to recognize stimuli that


one has seen before. For example, identifying and classifying
objects and situations.

o Relational Learning − It involves learning to differentiate among


various stimuli on the basis of relational properties, rather than
absolute properties. For Example, Adding ‘little less’ salt at the
time of cooking potatoes that came up salty last time, when
cooked with adding say a tablespoon of salt.

o Spatial Learning − It is learning through visual stimuli such as


images, colors, maps, etc. For Example, A person can create
roadmap in mind before actually following the road.
o Stimulus-Response Learning − It is learning to perform a
particular behavior when a certain stimulus is present. For
example, a dog raises its ear on hearing doorbell.

 Problem Solving − It is the process in which one perceives and tries to


arrive at a desired solution from a present situation by taking some
path, which is blocked by known or unknown hurdles.

Problem solving also includes decision making, which is the process of


selecting the best suitable alternative out of multiple alternatives to
reach the desired goal are available.

 Perception − It is the process of acquiring, interpreting, selecting, and


organizing sensory information.

Perception presumes sensing. In humans, perception is aided by sensory


organs. In the domain of AI, perception mechanism puts the data
acquired by the sensors together in a meaningful manner.

 Linguistic Intelligence − It is one’s ability to use, comprehend, speak,


and write the verbal and written language. It is important in
interpersonal communication.

Rote Learning

When a computer stores a piece of data, it is performing a rudimentary form of


learning. In the case of data caching, we store computed values—such as strongly
successful paths in search—so that we do not have to recompute them later. When an
agent receives an experience episode from another agent and remembers it, it learns.

Rote learning technique can also be used in complex l e a r n i n g s ys t e m s provided


sophisticated techniques are employed to use the stored values faster and there is a
generalization to keep the number of stored information down to a manageable level.
Checkers-playing p r o g r a m, for example, uses this technique to learn the board
positions it evaluates in its look-ahead search.
Research issues in this type of learning include organization of information for efficient
updates and retrieval, and generalization of information for effective updates and retrieval, as
well as interpretation and analysis.

3. Learning by Taking Advice

This type of learning is natural in humans. However, a system that performs this type of learning
must be able to translate advice given into actionable decisions. Thus, in this case, the system
might not be doing any learning at all, as humans probably are the ones who do that for the system.
Research issues in this area involve efficient representation of advice and effective parsing of
advice to “operationalize” them.

For example, we know that “Defense wins championships” is a piece of good advice. But we
need to translate this high-level strategy into actionable tactics, such as playing “man-to-man
defense”, “substitute often”, etc.

Example Learning System - FOO

Learning the game of hearts

FOO (First Operational Operationaliser) tries to convert high level advice (principles,
problems, methods) into effective executable (LISP) procedures.

Hearts:

 Game played as a series of tricks.


 One player - who has the lead - plays a card.
 Other players follow in turn and play a card.
o The player must follow suit.
o If he cannot he play any of his cards.
 The player who plays the highest value card wins the trick and the lead.
 The winning player takes the cards played in the trick.
 The aim is to avoid taking points. Each heart counts as one point the queen of
spades is worth 13 points.
 The winner is the person that after all tricks have been played has the lowest
points score.

Hearts is a game of partial information with no known algorithm for winning.

Although the possible situations are numerous general advice can be given such as:

 Avoid taking points.


 Do not lead a high card in suit in which an opponent is void.
 If an opponent has the queen of spades try to flush it.
In order to receive advice a human must convert into a FOO representation (LISP
clause)

(avoid (take-points me) (trick))

FOO operationalises the advice by translating it into expressions it can use in the
game. It can UNFOLD avoid and then trick to give:

(achieve (not (during

(scenario

(each p1 (players) (play-card p1)) (take-


trick (trick-winner))) (take-points me))))

However the advice is still not operational since it depends on the outcome of trick
which is generally not known. Therefore FOO uses case analysis (on
the during expression) to determine which steps could case one to take points. Step 1 is

ruled out and step 2's take-points is UNFOLDED:

(achieve (not (exists c1 (cards-played)

(exists c2 (point-cards)

(during (take (trick-winner) c1) (take me


c2))))))

FOO now has to decide: Under what conditions does (take me c2) occur during (take

(trick-winner) c1).

A technique, called partial matching, hypothesises that points will be taken if me =

trick-winner and c2 = c1. We can reduce our expression to:

(achieve (not (and (have-points(card-played))

(= (trick-winner) me ))))
This not quite enough a this means Do not win trick that has points. We do not know
who the trick-winner is, also we have not said anything about how

to play in a trick that has point led in the suit. After a few more steps to achieve this
FOO comes up with:

(achieve (>= (and (in-suit-led(card-of me))

(possible (trick-has-points)))
(low(card-of me)))

FOO had an initial knowledge base that was made up of:

 basic domain concepts such as trick, hand, deck suits, avoid, win etc.
 Rules and behavioural constraints -- general rules of the game.
 Heuristics as to how to UNFOLD. FOO has

2 basic shortcomings:

 It lacks a control structure that could apply operationalisation


automatically.
 It is specific to hearts and similar tasks.

4. Learning in Problem Solving

Humans learn from solving problems. Regardless of the forms or representation, or the exact
entity that one learns, usually the underlying principle is based on some sort of reinforcement.
Specifically, one learns to repeat performing an action if that leads to desirable outcomes, and
one learns to avoid performing an action if that leads to undesirable outcomes. Further,
since outcomes have to be evaluated, this type of learning also involves the definition of a
utility function: how much is a particular outcome worth?

Research issues in this area are many: identification of the learning rate, time and algorithm
complexity, convergence, representation (frame and qualification problems), handling of
uncertainty (ramification problem), adaptivity and “unlearning”, and so forth.

In reinforcement learning, usually, the system (and thus the developer) does not know
exactly which actions lead to desirable outcomes, but does know what outcomes are
desirable. In such a problem or domain, the effects of performing the actions are
usually compounded with side-effects and even noise such that it is intractable or
impossible to specify clearly what actions to perform given the situation parameters.

The most popular reinforcement learning algorithm is Q-Learning[2].

The core of the algorithm is a simple value iteration update. For each state, s, from the
state set S, and for each action, a, from the action set A, we can calculate an
update to its expected discounted reward with the following expression:

where rt is an observed real reward at time t, αt(s,a) are the learning rates such
that 0 ≤αt(s,a)≤ 1, and γ is the discount factor such that 0 ≤γ < 1. Three

techniques used are:

 Learning by Parameter Adjustment


 Learning by Macro Operators
 Learning by Chunking

Learning by Parameter Adjustment

Many programs rely on an evaluation procedure to summarise the state of search


etc. Game playing programs provide many examples of this.

However, many programs have a static evaluation function.

In learning a slight modification of the formulation of the evaluation of the problem


is required.

Here the problem has an evaluation function that is represented as a


polynomial of the form such as:
The t terms a values of features and the c terms are weights.

In designing programs it is often difficult to decide on the exact value to give each
weight initially.

So the basic idea of idea of parameter adjustment is to:

 Start with some estimate of the correct weight settings.


 Modify the weight in the program on the basis of accumulated
experiences.
 Features that appear to be good predictors will have their weights
increased and bad ones will be decreased.

Samuel's Checkers programs employed 16 such features at any one time chosen
from a pool of 38.

Learning by Macro Operators

The basic idea here is similar to Rote Learning:

Avoid expensive recomputation

Macro-operators can be used to group a whole series of actions into one.

For example: Making dinner can be described a lay the table, cook dinner, serve
dinner. We could treat laying the table as on action even though it involves a sequence
of actions.

The STRIPS problem-solving employed macro-operators in it's learning phase.

Consider a blocks world example in which ON(C,B) and ON(A,TABLE) are true.

STRIPS can achieve ON(A,B) in four steps:

UNSTACK(C,B), PUTDOWN(C), PICKUP(A), STACK(A,B)


STRIPS now builds a macro-operator MACROP with preconditions ON(C,B),
ON(A,TABLE), postconditions ON(A,B), ON(C,TABLE) and the four steps as its
body.

MACROP can now be used in future operation.

But it is not very general. The above can be easily generalised with variables used in
place of the blocks.

Learning by Chunking

Chunking involves similar ideas to Macro Operators and originates from


psychological ideas on memory and problem solving.

The computational basis is in production systems (studied earlier).

SOAR is a system that use production rules to represent its knowledge. It also employs
chunking to learn from experience.

Basic Outline of SOAR's Method

 SOAR solves problems it fires productions these are stored in long term
memory.
 Some firings turn out to be more useful than others.
 When SOAR detects are useful sequence of firings, it creates chunks.
 A chunk is essentially a large production that does the work of an entire
sequence of smaller ones.
 Chunks may be generalised before storing.

5. Learning from Examples: Induction

Often times, we learn by classifying things into different labels and then
“inferring” the common characteristics of each label. This type of learning is especially
useful in classification tasks: the “learned” or “trained” system can classify a
previously unseen entity (or instance) into one of the group by comparing the
instance’s attribute values to the common characteristics of the groups.
From another perspective, this type of learning can be viewed as learning the decision
path to correctly label an instance. And that decision path can be represented as
part of a decision tree, where a node is a decision point, branches are possible
values of a decision, and leaves are classification labels.

Whereas reinforcement learning dominates intelligent, real-time, adaptive systems,


induction (aka instance-based learning) dominates classification systems. Usually,
a developer is given a set of training instances such that each instance has a feature
vector and a class label. The developer then runs his or her system—with an
implemented learning algorithm—on the set of instances until the system is able to
classify all instances in the set meeting a required level of accuracy. Then the system
is ready for use.

Research issues in this type of learning are wide ranging: feature selection, active
learning, handling noise and incomplete data, size of the training set, size of the
testing set, classification efficiency, and so forth.

Inductive Learning

This involves the process of learning by example -- where a system tries to induce a
general rule from a set of observed instances.
This involves classification -- assigning, to a particular input, the name of a class to
which it belongs. Classification is important to many problem solving tasks.

A learning system has to be capable of evolving its own class descriptions:

 Initial class definitions may not be adequate.


 The world may not be well understood or rapidly changing.

The task of constructing class definitions is called induction or concept learning

A Blocks World Learning Example -- Winston (1975)


 The goal is to construct representation of the definitions of concepts in this
domain.
 Concepts such a house - brick (rectangular block) with a wedge (triangular
block) suitably placed on top of it, tent - 2 wedges touching side by side, or an
arch - two non-touching bricks supporting a third wedge or brick, were learned.
 The idea of near miss objects -- similar to actual instances was
introduced.
 Input was a line drawing of a blocks world structure.
 Input processed (see VISION Sections later) to produce a semantic net
representation of the structural description of the object (Fig. 27)

Fig. 27 House object and semantic net

 Links in network include left-of, right-of, does-not-marry,


supported-by, has-part, and isa.
 The marry relation is important -- two objects with a common touching
edge are said to marry. Marrying is assumed
unless does-not-marry stated.

There are three basic steps to the problem of concept formulation:


1. Select one know instance of the concept. Call this the concept
definition.
2. Examine definitions of other known instance of the concept.
Generalise the definition to include them.
3. Examine descriptions of near misses. Restrict the definition to
exclude these.

Both steps 2 and 3 rely on comparison and both similarities and differences need to be
identified.
Decision Trees

Quinlan in his ID3 system (986) introduced the idea of decision trees.

ID3 is a program that can build trees automatically from given positive and negative
instances.

Basically each leaf of a decision tree asserts a positive or negative concept. To


classify a particular input we start at the top and follow assertions down until we
reach an answer (Fig 28)

Fig. 28 Edible Mushroom decision tree Building

decision trees

 ID3 uses an iterative method.


 Simple trees preferred as more accurate classification is afforded.
 A random choice of samples from training set chosen for initial assembly
of tree -- the window subset.
 Other training examples used to test tree.
 If all examples classified correctly stop.
 Otherwise add a number of training examples to window and start again.

Adding new nodes


When assembling the tree we need to choose when to add a new node:

 Some attributes will yield more information than others.


 Adding a new node might be useless in the overall classification process.
 Sometimes attributes will separate training instances into subsets whose
members share a common label. Here branching can be terminates and a
leaf node assigned for the whole subset.

Decision tree advantages:

 Quicker than version spaces when concept space is large.


 Disjunction easier.

Disadvantages:

 Representation not natural to humans -- a decision tree may find it hard to


explain its classification.

6. Explanation-Based Learning

Humans appear to learn quite a lot from one example.

Basic idea: Use results from one examples problem solving effort next time around.

An EBL accepts 4 kinds of input:

A training example
-- what the learning sees in the world.

A goal concept
-- a high level description of what the program is supposed to learn.

A operational criterion
-- a description of which concepts are usable.

A domain theory
-- a set of rules that describe relationships between objects and actions in a
domain.

From this EBL computes a generalisation of the training example that is sufficient not
only to describe the goal concept but also satisfies the operational criterion.
This has two steps:

Explanation
-- the domain theory is used to prune away all unimportant aspects of the
training example with respect to the goal concept.

Generalisation
-- the explanation is generalised as far possible while still describing the goal
concept.
Explanation-based learning (EBL) is based on the notion of single-example learning.
Recall that instance-based learning requires typically a substantial number of training
instances. However, (1) it is possible that we do not have or afford such a number of
training instances, and (2) it is at times possible to learn well from a single example,
especially when we have enough knowledge.

Unlike instance-based learning which is more data-intensive, data-driven, EBL is


more knowledge-intensive, knowledge-driven.

First, an EBL system accepts a training example. Given its goal concept, an
operationality criterion (a description of which concepts are usable), and domain
theory (which are usually a set of rules that describe relationships between objects
and actions in a domain), it “generalizes” the training example to describe the goal
concept and to satisfy the operationality criterion.

Research issues in this area have involved applications to theorem provers, and
knowledge acquisition and engineering.

EBL example

Goal: To get to Brecon -- a picturesque welsh market town famous for its mountains
(beacons) and its Jazz festival.

The training data is:

near(Cardiff, Brecon), airport(Cardiff)

The Domain Knowledge is:


near(x,y) holds(loc(x),s) holds(loc(y), result(drive(x,y),s))
airport(z) loc(z), result(fly(z),s)))

In this case operational criterion is: We must express concept definition in pure
description language syntax.

Our goal can expressed as follows:

holds(loc(Brecon),s) -- find some situation s for this holds. We can

prove this holds with s defined by:

result(drive(Cardiff,Brecon),

result(fly(Cardiff), s')))

We can fly to Cardiff and then drive to Brecon.

If we analyse the proof (say with an ATMS). We can learn a few general rules from it.

Since Brecon appears in query and binding we could abstract it to give:

holds(loc(x),drive(Cardiff,x),

result(fly(Cardiff), s')))

but this not quite right - we cannot get everywhere by flying to Cardiff.

Since Brecon appears in the database when we abstract things we must explicitly
record the use of the fact:

near(Cardiff,x) holds(loc(x),drive(Cardiff,x), result(fly(Cardiff), s')))

This states if x is near Cardiff we can get to it by flying to Cardiff and then driving.
We have learnt this general rule.

We could also abstract out Cardiff instead of Brecon to get:


near(Brecon,x) airport(x) holds(loc(Brecon),

result(drive(x,Brecon), result(fly(x),s')))

This states we can get top Brecon by flying to another nearby airport and driving from
there.

We could add airport(Swansea) and get an alternative means of travel plan.

Finally we could actually abstract out both Brecon and Cardiff to get a general plan:

near(x,y) airport(y) holds(loc(y),

result(drive(x,y),result(fly(x),s')))

7. Discovery

Learning by discovery is sometimes considered as the highest form of machine


learning because it discovers new knowledge or information. To do that, a system will
need to be able to generate concepts, for example", and will also need to be able to
evaluate and identify these concepts are new and interesting.

There is theory-driven discovery, exemplified by the AM system, a system that


worked from a few basic concepts of set theory. To illustrate:

“In one run, AM discovered the concept of prime numbers. How did it do that?
Having stumbled onto the natural numbers, AM explored operations such as
addition, multiplication, and their inverses. It created the concept of divisibility and
noticed that some numbers had very few divisors. AM has a built-in heuristic that tells
it to explore extreme cases. It attempted to list all numbers with zero divisors (finding
none), one divisor (finding one: 1), and two divisors. …”

Theory Driven Discovery - AM (1976)

AM is a program that discovers concepts in elementary mathematics and set theory.

AM has 2 inputs:
 A description of some concepts of set theory (in LISP form). E.g. set union,
intersection, the empty set.
 Information on how to perform mathematics. E.g. functions. Given

the above information AM discovered:

Integers
-- it is possible to count the elements of this set and this is an the image of this
counting function -- the integers -- interesting set in its own right.

Addition
-- The union of two disjoint sets and their counting function.

Multiplication
-- Having discovered addition and multiplication as laborious
set-theoretic operations more effective descriptions were supplied by hand.

Prime Numbers
-- factorisation of numbers and numbers with only one factor were discovered.

Golbach's Conjecture
-- Even numbers can be written as the sum of 2 primes. E.g. 28 = 17 + 11.

Maximally Divisible Numbers


-- numbers with as many factors as possible. A number k is maximally divisible
is k has more factors than any integer less than k. E.g. 12 has six divisors
1,2,3,4,6,12.

How does AM work?

AM employs many general-purpose AI techniques:

 A frame based representation of mathematical concepts.


o AM can create new concepts (slots) and fill in their values.
 Heuristic search employed
o 250 heuristics represent hints about activities that might lead to
interesting discoveries.
o How to employ functions, create new concepts,
generalisation etc.
 Hypothesis and test based search.
 Agenda control of discovery process.

There is data-driven discovery, exemplified by the BACON system. To illustrate:

“BACON begins with a set of variables for a problem. For example, in the study of
the behavior of gases, some variables are p, the pressure on the gas, V, the volume of
the gas, n, the amount of gas in moles, and T, the temperature of the gas. Physicists
have long known a law, called the ideal gas law, that relates these variables. BACON is
able to derive this law on its own. First, BACON holds the variables n and T constant,
performing experiments at different pressures p1, p2, and p3. BACON notices that as
the pressure increases, the volume V increases. Therefore, it creates a theoretical
term pV. This term is constant. BACON systematically moves on to vary the other
variables …”

There is also clustering, very similar to induction. In induction, a system learns to


classify instances based on the labels provided by an “oracle”. In clustering, no such
labels are available.

There are supervised and unsupervised clustering techniques.

Research issues in this type of learning include the evaluation of “interesting” concepts,

number of clusters, cluster validity, size of the training set, and so forth.

8. Analogy
Learning by analogy has two general
methods: transformational and derivational. Figure 1 shows the differences.

One technique in learning by analogy is case-based reasoning (CBR). In CBR, a case


holds a problem statement and its solution. When a new problem is encountered, the
system searches its casebase for the best case—typically one that matches the new
problem most closely. Then based on the differences between the new problem and the
best case’ problem statement, the system adapts the best case’ solution accordingly.

Research issues in this type of learning involve feature selection, adaptation heuristics,
utility of outcomes, uncertainty handling, and so forth.

(a) (b)

Figure 1. (a) transformational analogy, (b) derivational analogy


9. Neural Net Learning and Genetic Learning

Neural net learning is a type of learning that aims at mimicking animal learning at a neural level, based on
a computational “brain metaphor”. Genetic learning is based on evolution.

Neural networks have been used to solve quite a few real-world problems successfully. However, the
“learned” knowledge is usually not comprehensible to the developer.

Genetic learning is based on genetic algorithms, which we have discussed previously this semester.

Research issues in neural net learning include the number of hidden layers, the number of input nodes, the
backpropagation algorithm, the updating function, and the evaluation metric. Those in genetic learning
include feature selection, genetic string representation, the design of mutation and other operators, and so
forth.

Expert systems (ES) are one of the prominent research domains of AI. It is introduced by the researchers at
Stanford University, Computer Science Department.

What are Expert Systems?


The expert systems are the computer applications developed to solve complex problems in a particular domain, at
the level of extra-ordinary human intelligence and expertise.

Characteristics of Expert Systems

 High performance

 Understandable

 Reliable

 Highly responsive

Capabilities of Expert Systems


The expert systems are capable of −

 Advising

 Instructing and assisting human in decision making

 Demonstrating

 Deriving a solution

 Diagnosing

 Explaining

 Interpreting input

 Predicting results

 Justifying the conclusion

 Suggesting alternative options to a problem

They are incapable of −

 Substituting human decision makers

 Possessing human capabilities


 Producing accurate output for inadequate knowledge base

 Refining their own knowledge

Components of Expert Systems


The components of ES include −

 Knowledge Base

 Interface Engine

 User Interface

Let us see them one by one briefly −

Knowledge Base
It contains domain-specific and high-quality knowledge. Knowledge is required to exhibit intelligence. The success
of any ES majorly depends upon the collection of highly accurate and precise knowledge.

What is Knowledge?
The data is collection of facts. The information is organized as data and facts about the task domain. Data,
information, and past experience combined together are termed as knowledge.

Components of Knowledge Base


The knowledge base of an ES is a store of both, factual and heuristic knowledge.

 Factual Knowledge − It is the information widely accepted by the Knowledge Engineers and scholars in
the task domain.

 Heuristic Knowledge − It is about practice, accurate judgement, one’s ability of evaluation, and guessing.

Knowledge representation
It is the method used to organize and formalize the knowledge in the knowledge base. It is in the form of IT-THEN-
ELSE rules.

Knowledge Acquisition
The success of any expert system majorly depends on the quality, completeness, and accuracy of the information
stored in the knowledge base.
The knowledge base is formed by readings from various experts, scholars, and the Knowledge Engineers. The
knowledge engineer is a person with the qualities of empathy, quick learning, and case analyzing skills.

He acquires information from subject expert by recording, interviewing, and observing him at work, etc. He then
categorizes and organizes the information in a meaningful way, in the form of IF-THEN-ELSE rules, to be used by
interference machine. The knowledge engineer also monitors the development of the ES.

Interface Engine
Use of efficient procedures and rules by the Interface Engine is essential in deducting a correct, flawless solution.

In case of knowledge-based ES, the Interface Engine acquires and manipulates the knowledge from the
knowledge base to arrive at a particular solution.

In case of rule based ES, it −

 Applies rules repeatedly to the facts, which are obtained from earlier rule application.

 Adds new knowledge into the knowledge base if required.

 Resolves rules conflict when multiple rules are applicable to a particular case.

To recommend a solution, the interface engine uses the following strategies −

 Forward Chaining

 Backward Chaining

Forward Chaining
It is a strategy of an expert system to answer the question,“What can happen next?”

Here, the interface engine follows the chain of conditions and derivations and finally deduces the outcome. It
considers all the facts and rules, and sorts them before concluding to a solution.

This strategy is followed for working on conclusion, result, or effect. For example, prediction of share market status
as an effect of changes in interest rates.

Backward Chaining
With this strategy, an expert system finds out the answer to the question, “Why this happened?”
On the basis of what has already happened, the interface engine tries to find out which conditions could have
happened in the past for this result. This strategy is followed for finding out cause or reason. For example,
diagnosis of blood cancer in humans.

User Interface
User interface provides interaction between user of the ES and the ES itself. It is generally Natural Language
Processing so as to be used by the user who is well-versed in the task domain. The user of the ES need not be
necessarily an expert in Artificial Intelligence.

It explains how the ES has arrived at a particular recommendation. The explanation may appear in the following
forms −

 Natural language displayed on screen.

 Verbal narrations in natural language.

 Listing of rule numbers displayed on the screen.

The user interface makes it easy to trace the credibility of the deductions.

Requirements of Efficient ES User Interface

 It should help users to accomplish their goals in shortest possible way.

 It should be designed to work for user’s existing or desired work practices.

 Its technology should be adaptable to user’s requirements; not the other way round.

 It should make efficient use of user input.

Expert Systems Limitations


No technology can offer easy and complete solution. Large systems are costly, require significant development
time, and computer resources. ESs have their limitations which include −

 Limitations of the technology

 Difficult knowledge acquisition

 ES are difficult to maintain

 High development costs


Applications of Expert System
The following table shows where ES can be applied.

Application Description

Design Domain Camera lens design, automobile design.

Medical Domain Diagnosis Systems to deduce cause of disease from


observed data, conduction medical operations on
humans.

Monitoring Systems Comparing data continuously with observed system or


with prescribed behavior such as leakage monitoring
in long petroleum pipeline.

Process Control Systems Controlling a physical process based on monitoring.

Knowledge Domain Finding out faults in vehicles, computers.

Finance/Commerce Detection of possible fraud, suspicious transactions,


stock market trading, Airline scheduling, cargo
scheduling.

Expert System Technology


There are several levels of ES technologies available. Expert systems technologies include −

 Expert System Development Environment − The ES development environment includes hardware and
tools. They are −

o Workstations, minicomputers, mainframes.

o High level Symbolic Programming Languages such as LISt Programming (LISP) and
PROgrammation LOGique (PROLOG).

o Large databases.

 Tools − They reduce the effort and cost involved in developing an expert system to large extent.

o Powerful editors and debugging tools with multi-windows.

o They provide rapid prototyping

o Have Inbuilt definitions of model, knowledge representation, and inference design.


 Shells − A shell is nothing but an expert system without knowledge base. A shell provides the developers
with knowledge acquisition, inference engine, user interface, and explanation facility. For example, few
shells are given below −

o Java Expert System Shell (JESS) that provides fully developed Java API for creating an expert
system.

o Vidwan, a shell developed at the National Centre for Software Technology, Mumbai in 1993. It
enables knowledge encoding in the form of IF-THEN rules.

Development of Expert Systems: General Steps


The process of ES development is iterative. Steps in developing the ES include −

Identify Problem Domain

 The problem must be suitable for an expert system to solve it.

 Find the experts in task domain for the ES project.

 Establish cost-effectiveness of the system.

Design the System

 Identify the ES Technology

 Know and establish the degree of integration with the other systems and databases.

 Realize how the concepts can represent the domain knowledge best.

Develop the Prototype


From Knowledge Base: The knowledge engineer works to −

 Acquire domain knowledge from the expert.

 Represent it in the form of IF-THEN-ELSE rules.

Test and Refine the Prototype

 The knowledge engineer uses sample cases to test the prototype for any deficiencies in performance.

 End users test the prototypes of the ES.

Develop and Complete the ES

 Test and ensure the interaction of the ES with all elements of its environment, including end users,
databases, and other information systems.

 Document the ES project well.

 Train the user to use ES.

Maintain the ES

 Keep the knowledge base up-to-date by regular review and update.

 Cater for new interfaces with other information systems, as those systems evolve.
Benefits of Expert Systems
 Availability − They are easily available due to mass production of software.

 Less Production Cost − Production cost is reasonable. This makes them affordable.

 Speed − They offer great speed. They reduce the amount of work an individual puts in.

 Less Error Rate − Error rate is low as compared to human errors.

 Reducing Risk − They can work in the environment dangerous to humans.

 Steady response − They work steadily without getting motional, tensed or fatigued.

UNIT – V (6 Lectures)
Programming in AI using LISP- Building Blocks of LISP-atom, list, string. Functions in
LISP- setq, car,cdr,cons,list. Loops and Defining Functions in LISP.

LISP - Overview
John McCarthy invented LISP in 1958, shortly after the development of FORTRAN. It was first implement by
Steve Russell on an IBM 704 computer.

It is particularly suitable for Artificial Intelligence programs, as it processes symbolic information effectively.

Common Lisp originated, during the 1980s and 1990s, in an attempt to unify the work of several
implementation groups, which were successors to Maclisp like ZetaLisp and NIL (New Implementation of
Lisp) etc.

It serves as a common language, which can be easily extended for specific implementation.

Programs written in Common LISP do not depend on machine-specific characteristics, such as word length etc.

Features of Common LISP


 It is machine-independent
 It uses iterative design methodology, and easy extensibility.
 It allows updating the programs dynamically.
 It provides high level debugging.
 It provides advanced object-oriented programming.
 It provides convenient macro system.
 It provides wide-ranging data types like, objects, structures, lists, vectors, adjustable arrays, hash-tables,
and symbols.
 It is expression-based.
 It provides an object-oriented condition system.
 It provides complete I/O library.
 It provides extensive control structures.

Applications Built in LISP


Large successful applications built in Lisp.

 Emacs
 G2
 AutoCad
 Igor Engraver
 Yahoo Store

LISP - Environment
CLISP is the GNU Common LISP multi-architechtural compiler used for setting up LISP in Windows. The
windows version emulates a unix environment using MingW under windows. The installer takes care of this
and automatically adds clisp to the windows PATH variable.

You can get the latest CLISP for Windows from here -
http://sourceforge.net/projects/clisp/files/latest/download

It creates a shortcut in the Start Menu by default, for the line-by-line interpreter.

How to use CLISP


During installation, clisp is automatically added to your PATH variable if you select the option
(RECOMMENDED) This means that you can simply open a new Command Prompt window and type “clisp”
to bring up the compiler.

To run a *.lisp or *.lsp file, simply use -

clisp hello.lisp
LISP - Program Structure
LISP expressions are called symbolic expressions or s-expressions. The s-expressions are composed of three
valid objects, atoms, lists and strings.

Any s-expression is a valid program.

LISP programs run either on an interpreter or as compiled code.

The interpreter checks the source code in a repeated loop, which is also called the read-evaluate-print loop
(REPL). It reads the program code, evaluates it, and prints the values returned by the program.

A Simple Program
Let us write an s-expression to find the sum of three numbers 7, 9 and 11. To do this, we can type at the
interpreter prompt.

(+7911)

LISP returns the result:

27

If you would like to run the same program as a compiled code, then create a LISP source code file named
myprog.lisp and type the following code in it.

(write (+ 7 9 11))

When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:

27

LISP Uses Prefix Notation


You might have noted that LISP uses prefix notation.

In the above program the + symbol works as the function name for the process of summation of the numbers.

In prefix notation, operators are written before their operands. For example, the expression,

a * ( b + c ) / d

will be written as:

(/ (* a (+ b c) ) d)

Let us take another example, let us write code for converting Fahrenheit temp of 60o F to the centigrade scale:

The mathematical expression for this conversion will be:

(60 * 9 / 5) + 32

Create a source code file named main.lisp and type the following code in it.

(write(+ (* (/ 9 5) 60) 32))


When you click the Execute button, or type Ctrl+E, MATLAB executes it immediately and the result returned
is:

140

Evaluation of LISP Programs


Evaluation of LISP programs has two parts:

 Translation of program text into Lisp objects by a reader program


 Implementation of the semantics of the language in terms of these objects by an evaluator program

The evaluation process takes the following steps:

 The reader translates the strings of characters to LISP objects or s-expressions.


 The evaluator defines syntax of Lisp forms that are built from s-expressions. This second level of
evaluation defines a syntax that determines which s-expressions are LISP forms.
 The evaluator works as a function that takes a valid LISP form as an argument and returns a value. This
is the reason why we put the LISP expression in parenthesis, because we are sending the entire
expression/form to the evaluator as arguments.

The 'Hello World' Program


Learning a new programming language doesn't really take off until you learn how to greet the entire world in
that language, right!

So, please create new source code file named main.lisp and type the following code in it.

(write-line "Hello World")

(write-line "I am at 'Tutorials Point'! Learning LISP")

When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:

Hello World

I am at 'Tutorials Point'! Learning LISP

LISP - Basic Syntax


Basic Building Blocks in LISP
LISP programs are made up of three basic building blocks:

 atom
 list
 string

An atom is a number or string of contiguous characters. It includes numbers and special characters.

Following are examples of some valid atoms:

hello-from-tutorials-point
name
123008907
*hello*
Block#221
abc123

A list is a sequence of atoms and/or other lists enclosed in parentheses.

Following are examples of some valid lists:

( i am a list)
(a ( a b c) d e fgh)
(father tom ( susan bill joe))
(sun mon tue wed thur fri sat)
( )

A string is a group of characters enclosed in double quotation marks.

Following are examples of some valid strings:

" I am a string"
"a ba c d efg #$%^&!"
"Please enter the following details :"
"Hello from 'Tutorials Point'! "

Adding Comments
The semicolon symbol (;) is used for indicating a comment line.

For Example,

(write-line "Hello World") ; greet the world

; tell them your whereabouts

(write-line "I am at 'Tutorials Point'! Learning LISP")

When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:

Hello World

I am at 'Tutorials Point'! Learning LISP

Some Notable Points before Moving to Next


Following are some of the important points to note:

 The basic numeric operations in LISP are +, -, *, and /


 LISP represents a function call f(x) as (f x), for example cos(45) is written as cos 45
 LISP expressions are case-insensitive, cos 45 or COS 45 are same.
 LISP tries to evaluate everything, including the arguments of a function. Only three types of elements
are constants and always return their own value
o Numbers
o The letter t, that stands for logical true.
o The value nil, that stands for logical false, as well as an empty list.

Little More about LISP Forms


In the previous chapter, we mentioned that the evaluation process of LISP code takes the following steps.

 The reader translates the strings of characters to LISP objects or s-expressions.


 The evaluator defines syntax of Lisp forms that are built from s-expressions. This second level of
evaluation defines a syntax that determines which s-expressions are LISP forms.
Now, a LISP forms could be.

 An Atom
 An empty or non-list
 Any list that has a symbol as its first element

The evaluator works as a function that takes a valid LISP form as an argument and returns a value. This is the
reason why we put the LISP expression in parenthesis, because we are sending the entire expression/form to
the evaluator as arguments.

Naming Conventions in LISP


Name or symbols can consist of any number of alphanumeric characters other than whitespace, open and
closing parentheses, double and single quotes, backslash, comma, colon, semicolon and vertical bar. To use
these characters in a name, you need to use escape character (\).

A name can have digits but not entirely made of digits, because then it would be read as a number. Similarly a
name can have periods, but can't be made entirely of periods.

Use of Single Quotation Mark


LISP evaluates everything including the function arguments and list members.

At times, we need to take atoms or lists literally and don't want them evaluated or treated as function calls.

To do this, we need to precede the atom or the list with a single quotation mark.

The following example demonstrates this.

Create a file named main.lisp and type the following code into it.

(write-line "single quote used, it inhibits evaluation")


(write '(* 2 3))
(write-line " ")
(write-line "single quote not used, so expression evaluated")
(write (* 2 3))

When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:

single quote used, it inhibits evaluation


(* 2 3)
single quote not used, so expression evaluated
6

LISP - Data Types


In LISP, variables are not typed, but data objects are.

LISP data types can be categorized as.

 Scalar types - for example, number types, characters, symbols etc.


 Data structures - for example, lists, vectors, bit-vectors, and strings.

Any variable can take any LISP object as its value, unless you have declared it explicitly.

Although, it is not necessary to specify a data type for a LISP variable, however, it helps in certain loop
expansions, in method declarations and some other situations that we will discuss in later chapters.
The data types are arranged into a hierarchy. A data type is a set of LISP objects and many objects may belong
to one such set.

The typep predicate is used for finding whether an object belongs to a specific type.

The type-of function returns the data type of a given object.

Type Specifiers in LISP


Type specifiers are system-defined symbols for data types.

array fixnum package simple-string


atom float pathname simple-vector
bignum function random-state single-float
bit hash-table ratio standard-char
bit-vector integer rational stream
character keyword readtable string
[common] list sequence [string-char]
compiled-function long-float short-float symbol
complex nill signed-byte t
cons null simple-array unsigned-byte
double-float number simple-bit-vector vector

Apart from these system-defined types, you can create your own data types. When a structure type is defined
using defstruct function, the name of the structure type becomes a valid type symbol.

Example 1

Create new source code file named main.lisp and type the following code in it.

(setq x 10)
(setq y 34.567)
(setq ch nil)
(setq n 123.78)
(setq bg 11.0e+4)
(setq r 124/2)
(print x)
(print y)
(print n)
(print ch)
(print bg)
(print r)

When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:

10
34.567
123.78
NIL
110000.0
62

Example 2

Next let's check the types of the variables used in the previous example. Create new source code file named
main. lisp and type the following code in it.

(setq x 10)
(setq y 34.567)
(setq ch nil)
(setq n 123.78)
(setq bg 11.0e+4)
(setq r 124/2)
(print (type-of x))
(print (type-of y))
(print (type-of n))
(print (type-of ch))
(print (type-of bg))
(print (type-of r))

When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:

(INTEGER 0 281474976710655)
SINGLE-FLOAT
SINGLE-FLOAT
NULL
SINGLE-FLOAT
(INTEGER 0 281474976710655)

LISP - Macros
Macros allow you to extend the syntax of standard LISP.

Technically, a macro is a function that takes an s-expression as arguments and returns a LISP form, which is
then evaluated.

Defining a Macro
In LISP, a named macro is defined using another macro named defmacro. Syntax for defining a macro is:

(defmacro macro-name (parameter-list))


"Optional documentation string."
body-form

The macro definition consists of the name of the macro, a parameter list, an optional documentation string, and
a body of Lisp expressions that defines the job to be performed by the macro.

Example

Let us write a simple macro named setTo10, which will take a number and set its value to 10.

Create new source code file named main.lisp and type the following code in it.

(defmacro setTo10(num)
(setq num 10)(print num))
(setq x 25)
(print x)
(setTo10 x)

When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:

25
10

LISP - Variables
In LISP, each variable is represented by a symbol. The variable's name is the name of the symbol and it is
stored in the storage cell of the symbol.
Global Variables
Global variables have permanent values throughout the LISP system and remain in effect until a new value is
specified.

Global variables are generally declared using the defvar construct.

For example
(defvar x 234)
(write x)

When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is

234

Since there is no type declaration for variables in LISP, you directly specify a value for a symbol with the setq
construct.

For Example
->(setq x 10)

The above expression assigns the value 10 to the variable x. You can refer to the variable using the symbol
itself as an expression.

The symbol-value function allows you to extract the value stored at the symbol storage place.

For Example

Create new source code file named main.lisp and type the following code in it.

(setq x 10)
(setq y 20)
(format t "x = ~2d y = ~2d ~%" x y)
(setq x 100)
(setq y 200)
(format t "x = ~2d y = ~2d" x y)

When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is.

x = 10 y = 20
x = 100 y = 200

Local Variables
Local variables are defined within a given procedure. The parameters named as arguments within a function
definition are also local variables. Local variables are accessible only within the respective function.

Like the global variables, local variables can also be created using the setq construct.

There are two other constructs - let and prog for creating local variables.

The let construct has the following syntax.

(let ((var1 val1) (var2 val2).. (varn valn)))


Where var1, var2, ..varn are variable names and val1, val2, .. valn are the initial values assigned to the
respective variables.

When let is executed, each variable is assigned the respective value and lastly the s-expression is evaluated.
The value of the last expression evaluated is returned.

If you don't include an initial value for a variable, it is assigned to nil.

Example

Create new source code file named main.lisp and type the following code in it.

(let ((x 'a) (y 'b)(z 'c))


(format t "x = ~a y = ~a z = ~a" x y z))

When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is.

x = A y = B z = C

The prog construct also has the list of local variables as its first argument, which is followed by the body of the
prog, and any number of s-expressions.

The prog function executes the list of s-expressions in sequence and returns nil unless it encounters a function
call named return. Then the argument of the return function is evaluated and returned.

Example

Create new source code file named main.lisp and type the following code in it.

(prog ((x '(a b c))(y '(1 2 3))(z '(p q 10)))


(format t "x = ~a y = ~a z = ~a" x y z))

When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is.

x = (A B C) y = (1 2 3) z = (P Q 10)

LISP - Constants
In LISP, constants are variables that never change their values during program execution. Constants are
declared using the defconstant construct.

Example
The following example shows declaring a global constant PI and later using this value inside a function named
area-circle that calculates the area of a circle.

The defun construct is used for defining a function, we will look into it in the Functions chapter.

Create a new source code file named main.lisp and type the following code in it.

(defconstant PI 3.141592)
(defun area-circle(rad)
(terpri)
(format t "Radius: ~5f" rad)
(format t "~%Area: ~10f" (* PI rad rad)))
(area-circle 10)

When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is.
Radius: 10.0
Area: 314.1592

LISP - Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. LISP
allows numerous operations on data, supported by various functions, macros and other constructs.

The operations allowed on data could be categorized as:

 Arithmetic Operations
 Comparison Operations
 Logical Operations
 Bitwise Operations

Arithmetic Operations
The following table shows all the arithmetic operators supported by LISP. Assume variable A holds 10 and
variable B holds 20 then:

Show Examples

Operator Description Example


+ Adds two operands (+AB) will give 30
- Subtracts second operand from the first (- A B) will give -10
* Multiplies both operands (* A B) will give 200
/ Divides numerator by de-numerator (/ B A) will give 2
mod,rem Modulus Operator and remainder of after an integer division (mod B A )will give 0
Increments operator increases integer value by the second argument
incf (incf A 3) will give 13
specified
Decrements operator decreases integer value by the second argument
decf (decf A 4) will give 9
specified

Comparison Operations
Following table shows all the relational operators supported by LISP that compares between numbers.
However unlike relational operators in other languages, LISP comparison operators may take more than two
operands and they work on numbers only.

Assume variable A holds 10 and variable B holds 20, then:

Show Examples

Operator Description Example


Checks if the values of the operands are all equal or not, if yes then
= (= A B) is not true.
condition becomes true.
Checks if the values of the operands are all different or not, if values
/= (/= A B) is true.
are not equal then condition becomes true.
> Checks if the values of the operands are monotonically decreasing. (> A B) is not true.
< Checks if the values of the operands are monotonically increasing. (< A B) is true.
Checks if the value of any left operand is greater than or equal to the
>= (>= A B) is not true.
value of next right operand, if yes then condition becomes true.
Checks if the value of any left operand is less than or equal to the
<= (<= A B) is true.
value of its right operand, if yes then condition becomes true.
max It compares two or more arguments and returns the maximum value. (max A B) returns 20
min It compares two or more arguments and returns the minimum value. (min A B) returns 20

Logical Operations on Boolean Values


Common LISP provides three logical operators: and, or, and not that operates on Boolean values. Assume A
has value nil and B has value 5, then:

Show Examples

Operator Description Example


It takes any number of arguments. The arguments are evaluated
and left to right. If all arguments evaluate to non-nil, then the value (and A B) will return NIL.
of the last argument is returned. Otherwise nil is returned.
It takes any number of arguments. The arguments are evaluated
or left to right until one evaluates to non-nil, in such case the (or A B) will return 5.
argument value is returned, otherwise it returns nil.
It takes one argument and returns t if the argument evaluates to
not (not A) will return T.
nil.

Bitwise Operations on Numbers


Bitwise operators work on bits and perform bit-by-bit operation. The truth tables for bitwise and, or, and xor
operations are as follows:

Show Examples

p q p and q p or q p xor q
000 0 0
010 1 1
111 1 0
100 1 1
Assume if A = 60; and B = 13; now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
-----------------
A and B = 0000 1100
A or B = 0011 1101
A xor B = 0011 0001
not A = 1100 0011

The Bitwise operators supported by LISP are listed in the following table. Assume variable A holds 60 and
variable B holds 13, then:

Operator Description Example


This returns the bit-wise logical AND of its arguments. If no
logand argument is given, then the result is -1, which is an identity for (logand a b)) will give 12
this operation.
This returns the bit-wise logical INCLUSIVE OR of its
logior arguments. If no argument is given, then the result is zero, which (logior a b) will give 61
is an identity for this operation.
logxor This returns the bit-wise logical EXCLUSIVE OR of its (logxor a b) will give 49
arguments. If no argument is given, then the result is zero, which
is an identity for this operation.
This returns the bit-wise NOT of its arguments. If no argument
lognor is given, then the result is -1, which is an identity for this (lognor a b) will give -62,
operation.
This returns the bit-wise logical EQUIVALENCE (also known
logeqv as exclusive nor) of its arguments. If no argument is given, then (logeqv a b) will give -50
the result is -1, which is an identity for this operation.

LISP - Decision Making


Decision making structures require that the programmer specify one or more conditions to be evaluated or
tested by the program, along with a statement or statements to be executed if the condition is determined to be
true, and optionally, other statements to be executed if the condition is determined to be false.

Following is the general form of a typical decision making structure found in most of the programming
languages:

LISP provides following types of decision making constructs. Click the following links to check their detail.

Construct Description
This construct is used for used for checking multiple test-action clauses. It can be compared to the
cond
nested if statements in other programming languages.
The if construct has various forms. In simplest form it is followed by a test clause, a test action and
if some other consequent action(s). If the test clause evaluates to true, then the test action is executed
otherwise, the consequent clause is evaluated.
In simplest form it is followed by a test clause, and a test action. If the test clause evaluates to true,
when
then the test action is executed otherwise, the consequent clause is evaluated.
This construct implements multiple test-action clauses like the cond construct. However, it
case
evaluates a key form and allows multiple action clauses based on the evaluation of that key form.

LISP - Loops
There may be a situation, when you need to execute a block of code numbers of times. A loop statement allows
us to execute a statement or group of statements multiple times and following is the general form of a loop
statement in most of the programming languages.

LISP provides the following types of constructs to handle looping requirements. Click the following links to
check their detail.

Construct Description
The loop construct is the simplest form of iteration provided by LISP. In its simplest form, it allows
loop
you to execute some statement(s) repeatedly until it finds a return statement.
The loop for construct allows you to implement a for-loop like iteration as most common in other
loop for
languages.
The do construct is also used for performing iteration using LISP. It provides a structured form of
do
iteration.
dotimes The dotimes construct allows looping for some fixed number of iterations.
dolist The dolist construct allows iteration through each element of a list.

LISP - Loop Construct


The loop construct is the simplest form of iteration provided by LISP. In its simplest form It allows you to
execute some statement(s) repeatedly until it finds a return statement.

It has the following syntax:

(loop (s-expressions))

Example
Create a new source code file named main.lisp and type the following code in it.

(setq a 10)
(loop
(setq a (+ a 1))
(write a)
(terpri)
(when (> a 17) (return a))
)

When you execute the code, it returns the following result:

11
12
13
14
15
16
17
18

Please note that without the return statement, the loop macro would produce an infinite loop.

LISP - Loop For Construct


The loop for construct allows you to implement a for-loop like iteration as most common in other languages.

It allows you to

 set up variables for iteration


 specify expression(s) that will conditionally terminate the iteration
 specify expression(s) for performing some job on each iteration
 specify expression(s), and expressions for doing some job before exiting the loop

The for loop for construct follows several syntax:

(loop for loop-variable in <a list>


do (action)
)

(loop for loop-variable from value1 to value2


do (action)
)

Example 1
Create a new source code file named main.lisp and type the following code in it:

(loop for x in '(tom dick harry)


do (format t " ~s" x)
)

When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:

TOM DICK HARRY

Example 2
Create a new source code file named main.lisp and type the following code in it:

(loop for a from 10 to 20


do (print a)
)

When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:

10
11
12
13
14
15
16
17
18
19
20

Example 3
Create a new source code file named main.lisp and type the following code in it:

(loop for x from 1 to 20


if(evenp x)
do (print x)
)

When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:

2
4
6
8
10
12
14
16
18
20

LISP - Do Construct
The do construct is also used for performing iteration using LISP. It provides a structured form of iteration.

The syntax for do statement:

(do (variable1 value1 updated-value1)


(variable2 value2 updated-value2)
(variable3 value3 updated-value3)
...
(test return-value)
(s-expressions)
)

The initial values of each variable is evaluated and bound to the respective variable. The updated value in each
clause corresponds to an optional update statement that specifies how the values of the variables will be
updated with each iteration.

After each iteration, the test is evaluated, and if it returns a non-nil or true, the return-value is evaluated and
returned.

The last s-expression(s) is optional. If present, they are executed after every iteration, until the test value
returns true.

Example
Create a new source code file named main.lisp and type the following code in it:

(do ((x 0 (+ 2 x))


(y 20 ( - y 2)))
((= x y)(- x y))
(format t "~% x = ~d y = ~d" x y)
)

When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:

x = 0 y = 20
x = 2 y = 18
x = 4 y = 16
x = 6 y = 14
x = 8 y = 12

LISP - Dotimes Construct


The dotimes construct allows looping for some fixed number of iterations.

For example, Create a new source code file named main.lisp and type the following code in it:

(dotimes (n 11)
(print n) (prin1 (* n n))
)

When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:

0 0
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100

LISP - Dolist Construct


The dolist construct allows iteration through each element of a list.

For example, Create a new source code file named main.lisp and type the following code in it:

(dolist (n '(1 2 3 4 5 6 7 8 9))


(format t "~% Number: ~d Square: ~d" n (* n n))
)

When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:

Number: 1 Square: 1
Number: 2 Square: 4
Number: 3 Square: 9
Number: 4 Square: 16
Number: 5 Square: 25
Number: 6 Square: 36
Number: 7 Square: 49
Number: 8 Square: 64
Number: 9 Square: 81

Gracefully Exiting From a Block


The block and return-from allows you to exit gracefully from any nested blocks in case of any error.

The block function allows you to create a named block with a body composed of zero or more statements.
Syntax is:

(block block-name(
...
...
))

The return-from function takes a block name and an optional (the default is nil) return value.

The following example demonstrates this:

Example
Create a new source code file named main.lisp and type the following code in it:

(defun demo-function (flag)


(print 'entering-outer-block)
(block outer-block
(print 'entering-inner-block)
(print (block inner-block
(if flag
(return-from outer-block 3)
(return-from inner-block 5)
)
(print 'This-wil--not-be-printed))
)
(print 'left-inner-block)
(print 'leaving-outer-block)
t)
)
(demo-function t)
(terpri)
(demo-function nil)

When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:

ENTERING-OUTER-BLOCK
ENTERING-INNER-BLOCK

ENTERING-OUTER-BLOCK
ENTERING-INNER-BLOCK
5
LEFT-INNER-BLOCK
LEAVING-OUTER-BLOCK

LISP - Functions
A function is a group of statements that together perform a task.

You can divide up your code into separate functions. How you divide up your code among different functions
is up to you, but logically the division usually is so each function performs a specific task.

Defining Functions in LISP


The macro named defun is used for defining functions. The defun macro needs three arguments:

 Name of the function


 Parameters of the function
 Body of the function

Syntax for defun is:

(defun name (parameter-list) "Optional documentation string." body)

Let us illustrate the concept with simple examples.

Example 1

Let's write a function named averagenum that will print the average of four numbers. We will send these
numbers as parameters.

Create a new source code file named main.lisp and type the following code in it.

(defun averagenum (n1 n2 n3 n4)


(/ ( + n1 n2 n3 n4) 4)
)
(write(averagenum 10 20 30 40))

When you execute the code, it returns the following result:

25

Example 2

Let's define and call a function that would calculate the area of a circle when the radius of the circle is given as
an argument.

Create a new source code file named main.lisp and type the following code in it.

(defun area-circle(rad)
"Calculates area of a circle with given radius"
(terpri)
(format t "Radius: ~5f" rad)
(format t "~%Area: ~10f" (* 3.141592 rad rad))
)
(area-circle 10)

When you execute the code, it returns the following result:

Area: 314.1592

Please note that:

 You can provide an empty list as parameters, which means the function takes no arguments, the list is
empty, written as ().
 LISP also allows optional, multiple, and keyword arguments.
 The documentation string describes the purpose of the function. It is associated with the name of the
function and can be obtained using the documentation function.
 The body of the function may consist of any number of Lisp expressions.
 The value of the last expression in the body is returned as the value of the function.
 You can also return a value from the function using the return-from special operator.

LISP - Predicates
Predicates are functions that test their arguments for some specific conditions and returns nil if the condition is
false, or some non-nil value is the condition is true.
The following table shows some of the most commonly used predicates:

Predicate Description
atom It takes one argument and returns t if the argument is an atom or nil if otherwise.
equal It takes two arguments and returns t if they are structurally equal or nil otherwise
It takes two arguments and returns t if they are same identical objects, sharing the same memory
eq
location or nil otherwise
It takes two arguments and returns t if the arguments are eq, or if they are numbers of the same type
eql with the same value, or if they are character objects that represent the same character, or nil
otherwise
evenp It takes one numeric argument and returns t if the argument is even number or nil if otherwise.
oddp It takes one numeric argument and returns t if the argument is odd number or nil if otherwise.
zerop It takes one numeric argument and returns t if the argument is zero or nil if otherwise.
null It takes one argument and returns t if the argument evaluates to nil, otherwise it returns nil.
listp It takes one argument and returns t if the argument evaluates to a list otherwise it returns nil.
It takes one or more argument and returns t if either there is a single argument or the arguments are
greaterp
successively larger from left to right, or nil if otherwise.
It takes one or more argument and returns t if either there is a single argument or the arguments are
lessp
successively smaller from left to right, or nil if otherwise..
numberp It takes one argument and returns t if the argument is a number or nil if otherwise.
symbolp It takes one argument and returns t if the argument is a symbol otherwise it returns nil.
integerp It takes one argument and returns t if the argument is an integer otherwise it returns nil.
It takes one argument and returns t if the argument is rational number, either a ratio or a number,
rationalp
otherwise it returns nil.
It takes one argument and returns t if the argument is a floating point number otherwise it returns
floatp
nil.
realp It takes one argument and returns t if the argument is a real number otherwise it returns nil.
complexp It takes one argument and returns t if the argument is a complex number otherwise it returns nil.
characterp It takes one argument and returns t if the argument is a character otherwise it returns nil.
stringp It takes one argument and returns t if the argument is a string object otherwise it returns nil.
arrayp It takes one argument and returns t if the argument is an array object otherwise it returns nil.
packagep It takes one argument and returns t if the argument is a package otherwise it returns nil.

Example 1
Create a new source code file named main.lisp and type the following code in it.

(write (atom 'abcd))


(terpri)
(write (equal 'a 'b))
(terpri)
(write (evenp 10))
(terpri)
(write (evenp 7 ))
(terpri)
(write (oddp 7 ))
(terpri)
(write (zerop 0.0000000001))
(terpri)
(write (eq 3 3.0 ))
(terpri)
(write (equal 3 3.0 ))
(terpri)
(write (null nil ))
When you execute the code, it returns the following result:

T
NIL
T
NIL
T
NIL
NIL
NIL
T

Example 2
Create a new source code file named main.lisp and type the following code in it.

(defun factorial (num)


(cond ((zerop num) 1)
(t ( * num (factorial (- num 1))))
)
)
(setq n 6)
(format t "~% Factorial ~d is: ~d" n (factorial n))

When you execute the code, it returns the following result:

Factorial 6 is: 720

LISP - Numbers
Common Lisp defines several kinds of numbers. The number data type includes various kinds of numbers
supported by LISP.

The number types supported by LISP are:

 Integers
 Ratios
 Floating-point numbers
 Complex numbers

The following diagram shows the number hierarchy and various numeric data types available in LISP:
Various Numeric Types in LISP
The following table describes various number type data available in LISP:

Data type Description


This data type represents integers which are not too large and mostly in the range -215 to 215-
fixnum
1 (it is machine-dependent)
These are very large numbers with size limited by the amount of memory allocated for LISP,
bignum
they are not fixnum numbers.
Represents the ratio of two numbers in the numerator/denominator form. The / function
ratio
always produce the result in ratios, when its arguments are integers.
float It represents non-integer numbers. There are four float data types with increasing precision.
It represents complex numbers, which are denoted by #c. The real and imaginary parts could
complex
be both either rational or floating point numbers.

Example

Create a new source code file named main.lisp and type the following code in it.

(write (/ 1 2))
(terpri)
(write ( + (/ 1 2) (/ 3 4)))
(terpri)
(write ( + #c( 1 2) #c( 3 -4)))

When you execute the code, it returns the following result:

1/2
5/4
#C(4 -2)

Number Functions
The following table describes some commonly used numeric functions:

Function Description
+, -, *, / Respective arithmetic operations
sin, cos, tan, acos,
Respective trigonometric functions.
asin, atan
sinh, cosh, tanh,
acosh, asinh, Respective hyperbolic functions.
atanh
exp Exponentiation function. Calculates ex
expt Exponentiation function, takes base and power both.
sqrt It calculates the square root of a number.
Logarithmic function. It one parameter is given, then it calculates its natural logarithm,
log
otherwise the second parameter is used as base.
It calculates the complex conjugate of a number. In case of a real number, it returns the
conjugate
number itself.
abs It returns the absolute value (or magnitude) of a number.
gcd It calculates the greatest common divisor of the given numbers
lcm It calculates the least common multiple of the given numbers
It gives the greatest integer less than or equal to the exact square root of a given natural
isqrt
number.
All these functions take two arguments as a number and returns the quotient; floor returns
the largest integer that is not greater than ratio, ceiling chooses the smaller integer that is
floor, ceiling,
larger than ratio, truncate chooses the integer of the same sign as ratio with the largest
truncate, round
absolute value that is less than absolute value of ratio, and round chooses an integer that is
closest to ratio.
ffloor, fceiling,
Does the same as above, but returns the quotient as a floating point number.
ftruncate, fround
mod, rem Returns the remainder in a division operation.
float Converts a real number to a floating point number.
rational,
Converts a real number to rational number.
rationalize
numerator,
Returns the respective parts of a rational number.
denominator
realpart, imagpart Returns the real and imaginary part of a complex number.

Example

Create a new source code file named main.lisp and type the following code in it.

(write (/ 45 78))
(terpri)
(write (floor 45 78))
(terpri)
(write (/ 3456 75))
(terpri)
(write (floor 3456 75))
(terpri)
(write (ceiling 3456 75))
(terpri)
(write (truncate 3456 75))
(terpri)
(write (round 3456 75))
(terpri)
(write (ffloor 3456 75))
(terpri)
(write (fceiling 3456 75))
(terpri)
(write (ftruncate 3456 75))
(terpri)
(write (fround 3456 75))
(terpri)
(write (mod 3456 75))
(terpri)
(setq c (complex 6 7))
(write c)
(terpri)
(write (complex 5 -9))
(terpri)
(write (realpart c))
(terpri)
(write (imagpart c))

When you execute the code, it returns the following result:

15/26
0
1152/25
46
47
46
46
46.0
47.0
46.0
46.0
6
#C(6 7)
#C(5 -9)
6
7

LISP - Characters
In LISP, characters are represented as data objects of type character.

You can denote a character object preceding #\ before the character itself. For example, #\a means the character
a.

Space and other special characters can be denoted by preceding #\ before the name of the character. For
example, #\SPACE represents the space character.

The following example demonstrates this:

Example
Create a new source code file named main.lisp and type the following code in it.

(write 'a)
(terpri)
(write #\a)
(terpri)
(write-char #\a)
(terpri)
(write-char 'a)

When you execute the code, it returns the following result:

A
#\a
a
*** - WRITE-CHAR: argument A is not a character

Special Characters
Common LISP allows using the following special characters in your code. They are called the semi-standard
characters.

 #\Backspace
 #\Tab
 #\Linefeed
 #\Page
 #\Return
 #\Rubout

Character Comparison Functions


Numeric comparison functions and operators, like, < and > do not work on characters. Common LISP provides
other two sets of functions for comparing characters in your code.

One set is case-sensitive and the other case-insensitive.

The following table provides the functions:

Case Sensitive Case-insensitive


Description
Functions Functions
Checks if the values of the operands are all equal or not, if yes
char= char-equal
then condition becomes true.
Checks if the values of the operands are all different or not, if
char/= char-not-equal
values are not equal then condition becomes true.
Checks if the values of the operands are monotonically
char&#60; char-lessp
decreasing.
Checks if the values of the operands are monotonically
char> char-greaterp
increasing.
Checks if the value of any left operand is greater than or equal
char&#60;= char-not-greaterp to the value of next right operand, if yes then condition becomes
true.
Checks if the value of any left operand is less than or equal to
char>= char-not-lessp the value of its right operand, if yes then condition becomes
true.

Example

Create a new source code file named main.lisp and type the following code in it.

; case-sensitive comparison
(write (char= #\a #\b))
(terpri)
(write (char= #\a #\a))
(terpri)
(write (char= #\a #\A))
(terpri)
;case-insensitive comparision
(write (char-equal #\a #\A))
(terpri)
(write (char-equal #\a #\b))
(terpri)
(write (char-lessp #\a #\b #\c))
(terpri)
(write (char-greaterp #\a #\b #\c))

When you execute the code, it returns the following result:

NIL
T
NIL
T
NIL
T
NIL

String Comparison Functions


Numeric comparison functions and operators, like, < and > do not work on strings. Common LISP provides
other two sets of functions for comparing strings in your code. One set is case-sensitive and the other case-
insensitive.

The following table provides the functions:

Case Sensitive
Case-insensitive Functions Description
Functions
Checks if the values of the operands are all equal or not, if
string= string-equal
yes then condition becomes true.
Checks if the values of the operands are all different or not,
string/= string-not-equal
if values are not equal then condition becomes true.
Checks if the values of the operands are monotonically
string&#60; string-lessp
decreasing.
Checks if the values of the operands are monotonically
string> string-greaterp
increasing.
Checks if the value of any left operand is greater than or
string&#60;= string-not-greaterp equal to the value of next right operand, if yes then condition
becomes true.
Checks if the value of any left operand is less than or equal
string>= string-not-lessp to the value of its right operand, if yes then condition
becomes true.

Example

Create a new source code file named main.lisp and type the following code in it.

; case-sensitive comparison
(write (string= "this is test" "This is test"))
(terpri)
(write (string> "this is test" "This is test"))
(terpri)
(write (string< "this is test" "This is test"))
(terpri)
;case-insensitive comparision
(write (string-equal "this is test" "This is test"))
(terpri)
(write (string-greaterp "this is test" "This is test"))
(terpri)
(write (string-lessp "this is test" "This is test"))
(terpri)
;checking non-equal
(write (string/= "this is test" "this is Test"))
(terpri)
(write (string-not-equal "this is test" "This is test"))
(terpri)
(write (string/= "lisp" "lisping"))
(terpri)
(write (string/= "decent" "decency"))

When you execute the code, it returns the following result:

NIL
0
NIL
T
NIL
NIL
8
NIL
4
5

Case Controlling Functions


The following table describes the case controlling functions:

Function Description
string-upcase Converts the string to upper case
string-downcase Converts the string to lower case
string-capitalize Capitalizes each word in the string

Example

Create a new source code file named main.lisp and type the following code in it.

(write-line (string-upcase "a big hello from tutorials point"))


(write-line (string-capitalize "a big hello from tutorials point"))

When you execute the code, it returns the following result:

A BIG HELLO FROM TUTORIALS POINT


A Big Hello From Tutorials Point

Trimming Strings
The following table describes the string trimming functions:

Function Description
It takes a string of character(s) as first argument and a string as the second argument
string-trim and returns a substring where all characters that are in the first argument are removed
off the argument string.
It takes a string of character(s) as first argument and a string as the second argument
String-left-trim and returns a substring where all characters that are in the first argument are removed
off the beginning of the argument string.
It takes a string character(s) as first argument and a string as the second argument and
String-right-trim returns a substring where all characters that are in the first argument are removed off
the end of the argument string

Example
Create a new source code file named main.lisp and type the following code in it.

(write-line (string-trim " " " a big hello from tutorials point "))
(write-line (string-left-trim " " " a big hello from tutorials point "))
(write-line (string-right-trim " " " a big hello from tutorials point "))
(write-line (string-trim " a" " a big hello from tutorials point "))

When you execute the code, it returns the following result:

a big hello from tutorials point


a big hello from tutorials point
a big hello from tutorials point
big hello from tutorials point

Other String Functions


Strings in LISP are arrays and thus also sequences. We will cover these data types in coming tutorials. All
functions that are applicable to arrays and sequences also apply to strings. However, we will demonstrate some
commonly used functions using various examples.

Calculating Length

The length function calculates the length of a string.

Extracting Sub-string

The subseq function returns a sub-string (as a string is also a sequence) starting at a particular index and
continuing to a particular ending index or the end of the string.

Accessing a Character in a String

The char function allows accessing individual characters of a string.

Example

Create a new source code file named main.lisp and type the following code in it.

(write (length "Hello World"))


(terpri)
(write-line (subseq "Hello World" 6))
(write (char "Hello World" 6))

When you execute the code, it returns the following result:

11
World
#\W

Sorting and Merging of Strings

The sort function allows sorting a string. It takes a sequence (vector or string) and a two-argument predicate
and returns a sorted version of the sequence.

The merge function takes two sequences and a predicate and returns a sequence produced by merging the two
sequences, according to the predicate.

Example

Create a new source code file named main.lisp and type the following code in it.
;sorting the strings
(write (sort (vector "Amal" "Akbar" "Anthony") #'string<))
(terpri)
;merging the strings
(write (merge 'vector (vector "Rishi" "Zara" "Priyanka") (vector "Anju" "Anuj" "Avni")
#'string<))

When you execute the code, it returns the following result:

#("Akbar" "Amal" "Anthony")


#("Anju" "Anuj" "Avni" "Rishi" "Zara" "Priyanka")

Reversing a String

The reverse function reverses a string.

For example, Create a new source code file named main.lisp and type the following code in it.

(write-line (reverse "Are we not drawn onward, we few, drawn onward to new era"))

When you execute the code, it returns the following result:

are wen ot drawno nward ,wef ew ,drawno nward ton ew erA

Concatenating Strings

The concatenate function concatenates two strings. This is generic sequence function and you must provide the
result type as the first argument.

For example, Create a new source code file named main.lisp and type the following code in it.

(write-line (concatenate 'string "Are we not drawn onward, " "we few, drawn onward to new
era"))

When you execute the code, it returns the following result:

Are we not drawn onward, we few, drawn onward to new era

LISP - Lists
Lists had been the most important and the primary composite data structure in traditional LISP. Present day's
Common LISP provides other data structures like, vector, hash table, classes or structures.

Lists are single linked lists. In LISP, lists are constructed as a chain of a simple record structure named cons
linked together.

The cons Record Structure


A cons is a record structure containing two components called the car and the cdr.

Cons cells or cons are objects are pairs of values that are created using the function cons.

The cons function takes two arguments and returns a new cons cell containing the two values. These values can
be references to any kind of object.

If the second value is not nil, or another cons cell, then the values are printed as a dotted pair enclosed by
parentheses.
The two values in a cons cell are called the car and the cdr. The car function is used to access the first value
and the cdr function is used to access the second value.

Example

Create a new source code file named main.lisp and type the following code in it.

(write (cons 1 2))


(terpri)
(write (cons 'a 'b))
(terpri)
(write (cons 1 nil))
(terpri)
(write (cons 1 (cons 2 nil)))
(terpri)
(write (cons 1 (cons 2 (cons 3 nil))))
(terpri)
(write (cons 'a (cons 'b (cons 'c nil))))
(terpri)
(write ( car (cons 'a (cons 'b (cons 'c nil)))))
(terpri)
(write ( cdr (cons 'a (cons 'b (cons 'c nil)))))

When you execute the code, it returns the following result:

(1 . 2)
(A . B)
(1)
(1 2)
(1 2 3)
(A B C)
A
(B C)

The above example shows how the cons structures could be used to create a single linked list, e.g., the list (A B
C) consists of three cons cells linked together by their cdrs.

Diagrammatically, it could be expressed as:

Lists in LISP
Although cons cells can be used to create lists, however, constructing a list out of nested cons function calls
can't be the best solution. The list function is rather used for creating lists in LISP.

The list function can take any number of arguments and as it is a function, it evaluates its arguments.

The first and rest functions give the first element and the rest part of a list. The following examples
demonstrate the concepts.

Example 1

Create a new source code file named main.lisp and type the following code in it.

(write (list 1 2))


(terpri)
(write (list 'a 'b))
(terpri)
(write (list 1 nil))
(terpri)
(write (list 1 2 3))
(terpri)
(write (list 'a 'b 'c))
(terpri)
(write (list 3 4 'a (car '(b . c)) (* 4 -2)))
(terpri)
(write (list (list 'a 'b) (list 'c 'd 'e)))

When you execute the code, it returns the following result:

(1 2)
(A B)
(1 NIL)
(1 2 3)
(A B C)
(3 4 A B -8)
((A B) (C D E))

Example 2

Create a new source code file named main.lisp and type the following code in it.

(defun my-library (title author rating availability)


(list :title title :author author :rating rating :availabilty availability)
)
(write (getf (my-library "Hunger Game" "Collins" 9 t) :title))

When you execute the code, it returns the following result:

"Hunger Game"

List Manipulating Functions


The following table provides some commonly used list manipulating functions.

Function Description
car It takes a list as argument, and returns its first element.
cdr It takes a list as argument, and returns a list without the first element.
It takes two arguments, an element and a list and returns a list with the element inserted at the first
cons
place.
It takes any number of arguments and returns a list with the arguments as member elements of the
list
list.
append It merges two or more list into one.
last It takes a list and returns a list containing the last element.
It takes two arguments of which the second must be a list, if the first argument is a member of the
member
second argument, and then it returns the remainder of the list beginning with the first argument.
reverse It takes a list and returns a list with the top elements in reverse order.

Please note that all sequence functions are applicable to lists.

Example 3

Create a new source code file named main.lisp and type the following code in it.

(write (car '(a b c d e f)))


(terpri)
(write (cdr '(a b c d e f)))
(terpri)
(write (cons 'a '(b c)))
(terpri)
(write (list 'a '(b c) '(e f)))
(terpri)
(write (append '(b c) '(e f) '(p q) '() '(g)))
(terpri)
(write (last '(a b c d (e f))))
(terpri)
(write (reverse '(a b c d (e f))))

When you execute the code, it returns the following result:

A
(B C D E F)
(A B C)
(A (B C) (E F))
(B C E F P Q G)
((E F))
((E F) D C B A)

Concatenation of car and cdr Functions


The car and cdr functions and their combination allows extracting any particular element/ member of a list.

However, sequences of car and cdr functions could be abbreviated by concatenating the letter a for car and d
for cdr within the letters c and r.

For example we can write cadadr to abbreviate the sequence of function calls - car cdr car cdr.

Thus, (cadadr '(a (c d) (e f g))) will return d

Example 4

Create a new source code file named main.lisp and type the following code in it.

(write (cadadr '(a (c d) (e f g))))


(terpri)
(write (caar (list (list 'a 'b) 'c)))
(terpri)
(write (cadr (list (list 1 2) (list 3 4))))
(terpri)

When you execute the code, it returns the following result:

D
A
(3 4)

You might also like