Reasoning and Agents: Knowledge Representation in AI
Reasoning and Agents: Knowledge Representation in AI
Chapter 3
Knowledge Representation
in AI
3
Contents
1. Knowledge Representation in AI
6. Types of knowledge
7. AI knowledge cycle
There are three factors which are put into the machine, which makes it valuable:
• Knowledge Representation and Reasoning (KR, KRR) is the part of Artificial intelligence which
concerned with AI agents thinking and how thinking contributes to intelligent behavior of agents.
• An intelligent agent needs knowledge about the real world for taking decisions and reasoning to act
efficiently.
• Knowledge-based agents: are those agents who have the capability of maintaining an internal state
of knowledge, reason over that knowledge, update their knowledge after observations and take
actions.
• These agents can represent the world with some formal representation and act intelligently.
2. Knowledge-Based Agent in Artificial Intelligence
A knowledge-based agent can be viewed at different levels which are given below:
1. Knowledge level
• Knowledge level is the first level of knowledge-based agent, and in this level, we need to specify what
the agent knows, and what the agent goals are.
2. Logical level:
• At this level, we understand that how the knowledge representation of knowledge is stored.
• At this level, sentences are encoded into different logics.
• At the logical level, an encoding of knowledge into logical sentences occurs.
• At the logical level we can expect to the automated taxi agent to reach to the destination B.
4. Various levels of knowledge-based agent
3. Implementation level:
• This is the physical representation of logic and knowledge.
• At the implementation level, agent performs actions as per logical and knowledge level.
• At this level, an automated taxi agent actually implement his knowledge and logic so that he can reach
to the destination.
5. Main Components of KBA
• It is a collection of sentences (here 'sentence' is a technical term and it is not identical to sentence in
English).
• These sentences are expressed in a language which is called a knowledge representation language.
• The Knowledge-base of KBA stores fact about the world.
5.1. Knowledge base
• Knowledge-base is required for updating knowledge for an agent to learn with experiences and take
action as per the knowledge.
• Knowledge and reasoning also play a crucial role in dealing with partially observable environment.
5.2. Inference System
• Inference system generates new facts so that an agent can update the KB.
• An inference system works mainly in two rules which are given as:
1) Forward chaining
2) Backward chaining
6. Types of knowledge
1. Meta-knowledge:
Knowledge about the other types of knowledge is called Meta-
knowledge.
6. Types of knowledge
2. Heuristic knowledge:
3. Procedural Knowledge
4. Declarative Knowledge:
• Declarative knowledge is to know about something.
5. Structural knowledge:
An Artificial intelligence system has the following components for displaying intelligent behavior:
1. Perception
2. Learning
3. Knowledge Representation and Reasoning
4. Planning
5. Execution
8. Operations Performed by KBA
The knowledge-based agent takes percept as input and returns an action as output.
The agent maintains the knowledge base, KB, and it initially has some background knowledge of the
real world.
It also has a counter to indicate the time for the whole process, and this counter is initialized with zero.
Each time when the function is called, it performs its three operations:
• Firstly it TELLs the KB what it perceives.
• Secondly, it asks KB what action it should take
• Third agent program TELLS the KB that which action was chosen.
8. A generic knowledge-based agent
1. Declarative approach: We can create a knowledge-based agent by initializing with an empty knowledge
base and telling the agent all the sentences with which we want to start with. This approach is called
Declarative approach.
2. Procedural approach: In the procedural approach, we directly encode desired behavior as a program
code. Which means we just need to write a program that already encodes the desired behavior or agent.
However, in the real world, a successful agent can be built by combining both declarative and
procedural approaches, and declarative knowledge can often be compiled into more efficient procedural
code.
10. Approaches to store knowledge in knowledge representation
There are mainly four approaches to knowledge representation, which are given below:
• It is the simplest way of storing facts which uses the relational method, and each fact about a set of the
object is set out systematically in columns.
• This approach of knowledge representation is famous in database systems where the relationship
between different entities is represented.
• In the inheritable knowledge approach, all data must be stored into a hierarchy of classes.
• All classes should be arranged in a generalized form or a hierarchal manner.
• In this approach, we apply inheritance property.
• Elements inherit values from other members of a class.
• This approach contains inheritable knowledge which shows a relation between instance and class, and
it is called instance relation.
• Every individual frame can represent the collection of attributes and its value.
• In this approach, objects and values are represented in Boxed nodes.
• We use Arrows which point from objects to their values.
10.2. Inheritable knowledge
Example:
10.3. Inferential knowledge
• Procedural knowledge approach uses small programs and codes which describes how to do specific
things, and how to proceed.
• In this approach, one important rule is used which is If-Then rule.
• In this knowledge, we can use various coding languages such as LISP language and Prolog language.
• We can easily represent heuristic or domain-specific knowledge using this approach.
• But it is not necessary that we can represent all cases in this approach.
11. Ontological engineering
• A machine sounds like an empty box unless it is encoded with some features or information.
• Therefore, to make it a valuable machine, it is required to put the necessary knowledge in it.
Ontological engineering is the engineering of such systems which could represent the complex domains
effectively.
• With the help of ontological engineering, the representation of the general concepts such as actions,
time, physical objects, performance, meta-data, and beliefs becomes possible on a large-scale.
• For special type of representations, we require a special type of ontology known as Special ontology.
But for special ontologies, there is a need to move towards a high-level generality.
11. Ontological engineering
There are two following major characteristics which distinguish general ontologies from the special one:
1. General ontologies should be applicable in every type of special-purpose domains with some domain-
specific axioms.
2. When there is a sufficiently demanding domain, the areas of knowledge should be unified.
• Unfortunately, none of the applications of artificial intelligence make use of the shared/general ontology,
all they use is special ontology.
12. Properties of a Knowledge Representation System
1) Representational Adequacy: It is the ability of the system to represent all kinds of knowledge needed in a
specific domain.
2) Inferential Adequacy: It is the ability of a knowledge representation system to manipulate the current
stored knowledge so that newly gained knowledge could be added.
3) Inferential Efficiency: It is the ability of the system to directly add new knowledge in the system with
efficiency.
4) Acqusitional Efficiency: It is the ability of the system to automatically acquire new knowledge from the
environment. This leads the system to give more productive result as more knowledge adds up with the
current knowledge.
13. Techniques used for Knowledge Representation
• There are mainly four ways of knowledge representation which are given as follows:
• Logical representation is a language with some concrete rules which deals with propositions and has
no ambiguity in representation.
Note: We will discuss Prepositional Logics and Predicate logics in later chapters.
13.1. Logical representation (Logic)
Note: Do not be confused with logical representation and logical reasoning as logical representation is a
representation language and reasoning is a process of thinking logically.
13.2. Semantic Network Representation
• In Semantic networks, we can represent our knowledge in the form of graphical networks (graph).
• This network consists of nodes representing objects and arcs which describe the relationship between
those objects.
• Semantic networks can categorize the object in different forms and can also link those objects.
• Semantic networks are easy to understand and can be easily extended.
Example: Following are some statements which we need to represent in the form of nodes and arcs.
Statements:
• Jerry is a cat.
• Jerry is a mammal.
• Jerry is owned by Priya.
• Jerry is white colored.
• All Mammals are animal.
13.2. Semantic Network Representation
https://github.com/HKUST-KnowComp/ASER
13.3. Frame Representation
• Frames: In this technique, the knowledge is stored via slots and fillers.
• As we have seen in DBMS, we store the information of an employee in the database, where:
Similarly, the Slots are the entities and Fillers are its attributes.
They are together stored in a frame.
So, whenever there is a requirement, the machine infers the necessary information to take the decision.
For example, Tomy is a dog having one tail.
Production rules system consist of (condition, action) pairs which mean, "If condition then action".
In production rules, agent checks for the condition and if the condition exists then production rule fires
and corresponding action is carried out.
The working memory contains the description of the current state of problems-solving and rule can
write knowledge to the working memory.
13.4. Production Rules
Example:
IF (at bus stop AND bus arrives) THEN action (get into the bus)
IF (on the bus AND paid AND empty seat) THEN action (sit down).
IF (on bus AND unpaid) THEN action (pay charges).
IF (bus arrives at destination) THEN action (get down from the bus).
13.4. Production Rules
Using a promising declarative programming paradigm, namely Answer Set Programming (ASP, sometimes
also Answer Set Prolog), offers unexpected advantages over the popular imperative programming
approach, for example:
An answer set program consists of given knowledge formulated as facts, rules (head(X) :- body(X).) or
constraints.
The program is loaded by a solver and returns a ―stable model‖. This so called answer set consists of all
facts that can be derived using the given rules and constraints.
14. Knowledge Representation and Reasoning with Answer Set Programming
(ASP)
• Let us consider a famous example from logic about birds and penguins. It is a well known fact that birds
can fly. This can be encoded as an answer set rule:
canFly(X) :- bird(X).
• The rule is read as ―If X is a bird, then X can fly‖. Now let‘s add more knowledge! For example, facts
that tell the unconditional truth, just as seagull ‗Malvin‘ is a bird, but also penguin ‗Roger‘ is one.
canFly(X) :- bird(X).
bird(malvin).
bird(roger).
== MODEL OUTPUT ==
Anwer: 1
bird(malvin) bird(roger) canFly(malvin) canFly(roger)
SATISFIABLE
14. Knowledge Representation and Reasoning with Answer Set Programming
(ASP)
• As a result, the answer set tells us the known facts that Malvin and Roger are birds but also concluded
that they are able to fly. The biology enthusiasts among us know, that penguins are unable to fly thus
the model is wrong!
• In order to get acceptable results we need to add more knowledge in form of facts and integrity
constraints to the program. Here, the model needs to know that Roger is not only a bird but also a
penguin and that there is no bird that is a penguin which is able to fly.
canFly(X) :- bird(X).
bird(malvin).
bird(roger).
seagull(malvin).
penguin(roger).
:- canFly(X), penguin(X).
== MODEL OUTPUT ==
UNSATISFIABLE
14. Knowledge Representation and Reasoning with Answer Set Programming
(ASP)
• This does not bring the expected result and shows how important it is to think thoroughly and very
carefully about the problem as well as the solution. The model tells, like stated by the programmer, that
a bird can fly but there is no advise for birds that belong to the penguin family. To avoid this, we could
add that only birds that are not penguins are able to fly.
== MODEL OUTPUT ==
Answer: 1
bird(malvin) bird(roger) seagull(malvin) penguin(roger) canFly(malvin)
SATISFIABLE
14. Knowledge Representation and Reasoning with Answer Set Programming
(ASP)
• Adding this knwoledge in Line [1], the stable model consists of the expected outcome.
canFly(X) :- bird(X), not penguin(X).
• Let‘s look at another example to highlight the advantages of ASP mentioned above. Sudoku is a well
known puzzle game and popular for explaining search problems.
• Given an initial 9x9 grid of cells containig numbers between 1 and 9 or blanks, all blanks must be filled
with numbers. You win Sudoko if you find all values such that every row, column, and 3x3 subsquare
contains the numbers 1–9, each with a single occurrence.
15. Imperative versus declarative programming
.. for solving Sudoku
15. Imperative versus declarative programming
.. for solving Sudoku…. 1. Python (imperative)
• The following Code snippet will show how to solve the Sudoku in Python.
size = 9
#empty cells have value zero
matrix = [
[5,3,0,0,7,0,0,0,0],
[6,0,0,1,9,5,0,0,0],
[0,9,8,0,0,0,0,6,0],
[8,0,0,0,6,0,0,0,3],
[4,0,0,8,0,3,0,0,1],
[7,0,0,0,2,0,0,0,6],
[0,6,0,0,0,0,2,8,0],
[0,0,0,4,1,9,0,0,5],
[0,0,0,0,8,0,0,7,9]]
#print Sudoku
def print_sudoku():
for i in matrix:
print (i)
15. Imperative versus declarative programming
.. for solving Sudoku… 1. Python (imperative)
#checking submatrix
for i in range(row_start,row_start+3):
for j in range(col_start,col_start+3):
if matrix[i][j]==n:
return False
return True
#if all cells are assigned then the sudoku is already solved
#pass by reference because number_unassigned will change the values of row and col
a = number_unassigned(row, col)
if a[2] == 0:
return True
row = a[0]
col = a[1]
#number between 1 to 9
for i in range(1,10):
#if we can assign i to the cell or not
#the cell is matrix[row][col]
if is_safe(i, row, col):
matrix[row][col] = i
#backtracking
if solve_sudoku():
return True
15. Imperative versus declarative programming
.. for solving Sudoku… 1. Python (imperative)
69