What is Artificial Intelligence
What is Artificial Intelligence
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?”
Modification is not quick and easy. It may lead to affecting Quick and Easy
the program adversely. program modification.
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.
1 Expert Systems
4 Robotics
Task Classification of AI
The domain of AI is classified into Formal tasks, Mundane
tasks, and Expert tasks.
Task Domains of Artificial Intelligence
Language
Translation
Common Sense Verification Financial Analysis
Planing Creativity
Robotics
Locomotive
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.
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.−
It is complicated.
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.
Applications of AI
AI has been dominant in various fields such as −
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.
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.
2. Scrutinize the problem carefully, for some features may have a central
affect on the chosen method of solution.
4. Choose the best solving techniques for the problem to solve a solution.
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 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.
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.
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 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.
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
This example can be solved by the operator sequence UP, RIGHT, UP, LEFT,
DOWN.
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.
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.
Problem Decomposition
Suppose to solve the expression is: + ∫(X³ + X² + 2X + 3sinx)dx
∫(X³ + X² + 2X + 3sinx)dx
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:
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.
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.
Method II:
1. Siva is a worker in the company.
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.
Production systems provide us with good ways of describing the operations that
can be performed in a search for a solution to a problem.
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.
Non-partially
Commutative Chemical Synthesis Bridge
Production systems that are not partially commutative are useful for many
problems in which permanent changes occur.
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.
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.
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
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
Make a representation for the initial state from the given data.
What knowledge about the domain can be used to guide the search?
Additional Problems
The additional problems are given below.
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
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.
Pretest the table’s place is under the bananas The monkey’s place is on the
table
Initial the monkey’s place is place1 the table’s place is place2 The contents of
the monkey’s hand is empty
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.
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
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
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.
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
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.
ii. If the new state is a goal state, quit and return this state.
Implementation:
1. 2.
2. 4.
Properties of BFS:
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.
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:
1. 2.
2. 4.
3. 4.
4. 5.
5. 6.
6. 7.
Properties of DFS
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.
Complete? Yes
Time?(d+1)b0 + d b1 + (d-1)b2 + … + bd = O(bd)
Space?O(bd)
Optimal? Yes, if step cost = 1
“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
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.
Hill Climbing:
Searching for a goal state = Climbing to the top of a hill
Features:
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.
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.
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.
Time bd bm bd/2 bd bd
Space bd bm bd/2 bd bd
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.
loop
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;
k = k + 1;
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.
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.
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.
Example:
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
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 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
1. Tacit knowledge derives from the experience, doing action and subjective insight
Finally Knowledge derives from the Tacit and Explicit Knowledge and Information.
Let’s see the contents of the topology map:
Information
Context
Socialization
Facts Data
Wisdon
Intemalization
Knowledge
Conversion Externalizatin
Combination
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.
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.
Formal
Compute
Representation Output
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:
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
then the answer is easy to get from the data structures and
procedures.
Representational Adequacy
Inferential Adequacy
Inferential Efficiency
Acquisitional Efficiency
Well-defined Syntax and Semantics
Naturalness
Frame problem
Representational 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
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
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.
Inheritable knowledge
age
Adult Male 35
is a
Musician
is a
Jazz Avant
Garde/Jazz
instance instance
Ravi John
bands bands
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:
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.
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.
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
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.
‘either p is true, or q is
disjunction PVQ || | OR true, or both’
Note: The propositions and connectives are the basic elements of propositional logic.
Truth value
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’
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
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
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)}]
P∨Q Q′ ∨ R
(P ∨ Q) θ
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.
(P Λ Q) → R P V Q V R (2)
(S V T) → Q S V Q (3)
T V Q (4)
T T (5)
Unification
i) We can get the inference immediately if we can find a substitution θ such that
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)) {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}
Marcus/x
Pompeion(Marcus) V loyalto(Marcus, Caesar) 2
7 loyalto(Marcus, Caesar)
Marcus/x4, Caesar/y1
tryassassinate(Marcus, Caesar) 8
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
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
Mammal
is a
has_part
Person Head
instance
team_colours
team
Black/Blue Dhoni India
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
gave book
instance
agent object
Sam event 1 book 13
receiver
Ravi
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:
fly
action
has_part
bird wings
instance
action
emu run
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
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
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.
furniture
ako
part
person chair seal
isa isa
owner colour
me my-chair tan
covering isa
leather brown
isa
agent object
John event7 book23
person isa
Mary book
isa ako
Bilbo habbit person
agent
believes
instance
agent
John
event1 object
space 1
earth flat
instance insurance
Object 1 Prop 1
has_property
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
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
SA
Dogs Bite mail carrier
d b m
assailant victim
GS g form
UNIT – IV (6 Lectures)
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.
Rote Learning
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.
FOO (First Operational Operationaliser) tries to convert high level advice (principles,
problems, methods) into effective executable (LISP) procedures.
Hearts:
Although the possible situations are numerous general advice can be given such as:
FOO operationalises the advice by translating it into expressions it can use in the
game. It can UNFOLD avoid and then trick to give:
(scenario
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
(exists c2 (point-cards)
FOO now has to decide: Under what conditions does (take me c2) occur during (take
(trick-winner) c1).
(= (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:
(possible (trick-has-points)))
(low(card-of me)))
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:
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 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
In designing programs it is often difficult to decide on the exact value to give each
weight initially.
Samuel's Checkers programs employed 16 such features at any one time chosen
from a pool of 38.
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.
Consider a blocks world example in which ON(C,B) and ON(A,TABLE) are true.
But it is not very general. The above can be easily generalised with variables used in
place of the blocks.
Learning by Chunking
SOAR is a system that use production rules to represent its knowledge. It also employs
chunking to learn from experience.
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.
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.
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.
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.
decision trees
Disadvantages:
6. Explanation-Based Learning
Basic idea: Use results from one examples problem solving effort next time around.
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.
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.
In this case operational criterion is: We must express concept definition in pure
description language syntax.
result(drive(Cardiff,Brecon),
result(fly(Cardiff), s')))
If we analyse the proof (say with an ATMS). We can learn a few general rules from it.
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:
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.
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.
Finally we could actually abstract out both Brecon and Cardiff to get a general plan:
result(drive(x,y),result(fly(x),s')))
7. Discovery
“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. …”
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
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.
“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 …”
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.
Research issues in this type of learning involve feature selection, adaptation heuristics,
utility of outcomes, uncertainty handling, and so forth.
(a) (b)
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.
High performance
Understandable
Reliable
Highly responsive
Advising
Demonstrating
Deriving a solution
Diagnosing
Explaining
Interpreting input
Predicting results
Knowledge Base
Interface Engine
User Interface
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.
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.
Applies rules repeatedly to the facts, which are obtained from earlier rule application.
Resolves rules conflict when multiple rules are applicable to a particular case.
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 −
The user interface makes it easy to trace the credibility of the deductions.
Its technology should be adaptable to user’s requirements; not the other way round.
Application Description
Expert System Development Environment − The ES development environment includes hardware and
tools. They are −
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 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.
Know and establish the degree of integration with the other systems and databases.
Realize how the concepts can represent the domain knowledge best.
The knowledge engineer uses sample cases to test the prototype for any deficiencies in performance.
Test and ensure the interaction of the ES with all elements of its environment, including end users,
databases, and other information systems.
Maintain the ES
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.
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.
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.
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.
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)
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
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
(/ (* a (+ b c) ) d)
Let us take another example, let us write code for converting Fahrenheit temp of 60o F to the centigrade scale:
(60 * 9 / 5) + 32
Create a source code file named main.lisp and type the following code in it.
140
So, please create new source code file named main.lisp and type the following code in it.
When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:
Hello World
atom
list
string
An atom is a number or string of contiguous characters. It includes numbers and special characters.
hello-from-tutorials-point
name
123008907
*hello*
Block#221
abc123
( i am a list)
(a ( a b c) d e fgh)
(father tom ( susan bill joe))
(sun mon tue wed thur fri sat)
( )
" 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,
When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:
Hello World
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.
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.
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.
Create a file named main.lisp and type the following code into it.
When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:
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.
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:
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.
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.
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.
Example
Create new source code file named main.lisp and type the following code in it.
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.
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.
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
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.
Show Examples
Show Examples
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:
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.
(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))
)
11
12
13
14
15
16
17
18
Please note that without the return statement, the loop macro would produce an infinite loop.
It allows you to
Example 1
Create a new source code file named main.lisp and type the following code in it:
When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:
Example 2
Create a new source code file named main.lisp and type the following code in it:
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:
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 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:
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
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
For example, Create a new source code file named main.lisp and type the following code in it:
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
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.
Example
Create a new source code file named main.lisp and type the following code in it:
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.
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.
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)
Area: 314.1592
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.
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.
LISP - Numbers
Common Lisp defines several kinds of numbers. The number data type includes various kinds of numbers
supported by LISP.
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:
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)))
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))
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.
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)
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
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))
NIL
T
NIL
T
NIL
T
NIL
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< 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<= 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"))
NIL
0
NIL
T
NIL
NIL
8
NIL
4
5
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.
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 "))
Calculating Length
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.
Example
Create a new source code file named main.lisp and type the following code in it.
11
World
#\W
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<))
Reversing 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"))
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"))
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.
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.
(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.
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.
(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.
"Hunger Game"
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.
Example 3
Create a new source code file named main.lisp and type the following code in it.
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)
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.
Example 4
Create a new source code file named main.lisp and type the following code in it.
D
A
(3 4)