You are on page 1of 93

Prof. Dr.

Ali Hussein Hasan

UNIVERSITY OF SUMER
COLLEGE OF COMPUTER SCIENCE AND INFORMATION
TECHNOLOGY
COMPUTER INFORMATION SYSTEM DEPARTMENT

Lectures of Artificial Intelligence (A.I.)


Third year class 2021-2022
PROF. Dr. ALI HUSSEIN HASAN

Students are sincerely advised to read various books and consult internet sites
for a detailed clarification of topics presented here.

Readings

1- Artificial Intelligence: A Modern Approach; Stuart Jonathan Russell,


Peter Norvig, Prentice Hall, 2010
2- Artificial Intelligence; Elaine Rich, Kevin Knight; Tata McGraw – Hill
Publishing Company, 2005
3- Few internet sites ( With Acknowledgments to known / unknown sites
for figures / useful literature for academic purpose only)
4- Vinod Chandra S.S. and Adnan Hareendran S., Artificial intelligence
and machine learning.
5- Gurdev Sigh and Mandeep Singh Bhatia, Symbolic logic & Prolog.

Syllabus

Subject no. Details


1 Introduction : Overview of Artificial Intelligence:
Definition and Approaches
AI problems
How are AI problems different from other?
Topics of AI
Application of AI
2 AI Agents and their Environments
What are Agent and Environment?
Agent Terminology
Rationality
Turing Test
Properties of Environment
3 Propositional and Predicate Logic

1
Prof. Dr. Ali Hussein Hasan
Tautologies in propositional logic
Translating between English and Logic Notation
Control structures in reasoning
Backward chaining – Goal directed reasoning
Forward chaining – data directed computation or Modus
Ponens Reasoning
Hybrid control structure ( Rule – cycle hybrid)
Theorem proving by propositional logic
Semantic method
Syntactic methods
Theorem proving by Wang’s Algorithm
Resolution in propositional logic
4 Predicate logic – First order logic FOL
Writing a Sentence into Clause Forms
5 Problem solving by intelligent search
Problem Representation
Graphs versus Trees
Search methods
Systematic methods:
Depth first search
Breadth first search:-
Optimal search method
Hill Climbing method
Best first method
6 Knowledge Representation structures
Types of knowledge representation
Production Rules PR
Semantic Networks (SN).
Conceptual Graphs (CG).
Frames.
Scripts.
7 Expert systems
8 Practical programming: PROLOG

2
Prof. Dr. Ali Hussein Hasan
Introduction : Overview of Artificial Intelligence

Artificial intelligence (AI) is the study of solutions for problems that are
difficult or impractical to solve with traditional methods. It is used pervasively
in support of everyday applications such as email, word-processing and search,
as well as in the design and analysis of autonomous agents that perceive their
environment and interact rationally with the environment. The solutions rely
on a broad set of general and specialized knowledge representation schemes,
problem solving mechanisms and learning techniques. They deal with sensing
(e.g., speech recognition, natural language understanding, computer vision),
problem – solving (e.g., search, planning), and acting (e.g., robotics) and the
architectures needed to support them (e.g., agents, multi-agents). The study of
Artificial Intelligence prepares the student to determine when an AI approach
is appropriate for a given problem, identify the appropriate representation and
reasoning mechanism, and implement and evaluate it.

Definition and Approaches

By different Scientists/researchers)

• The goal of work in artificial intelligence is to build machines that


perform tasks normally requiring human intelligence. (Nilsson, Nils J.
(1971), Problem-Solving Methods in Artificial Intelligence (New York:
McGraw-Hill).
• Research scientists in Artificial Intelligence try to get machines to
exhibit behavior that we call intelligent behavior when we observe it
in human beings. (Slagle, James R. (1971), Artificial Intelligence: The
Heuristic Programming Approach (New York: McGraw-Hill): 1.)
• Artificial intelligence (AI) is technology and a branch of computer
science that studies and develops intelligent machines and software.
The exciting new effort to make computers think; machines with
minds, in the full and literal sense. (Haugeland, 1985).
• The automation of activities that we associate with human thinking,
activities such as decision–making, problem solving, learning ...'
(Bellman, 1978).
• The art of creating machines that perform functions that require
intelligence when performed by people (Kurzweil, 1990).
• The study of how to make computers do things at which, at the
moment, people are better (Rich and Knight, 1991).

3
Prof. Dr. Ali Hussein Hasan
• The study of mental faculties through the use of computational
models (Charniak and McDermott, 1985).
• The study of the computations that make it possible to perceive,
reason, and act (Winston, 1992).
• A field of study that seeks to explain and emulate intelligent behavior
in terms of computational processes (Schalkoff, 1990).
• AI seeks to understand the working of the mind in mechanistic terms. The
branch of computer science that is concerned with the automation of
intelligent behavior (Luger and Stubblefield, 1993).

Conclusion about definition of AI:- can be defined as the simulation of


human intelligence on a machine, so as to make the machine efficient to
identify and use the right piece of “knowledge” at a given step of solving a
problem. A system capable of planning and executing the right task at the right
time is generally called ‘rational’. Thus, AI alternatively may be stated as a
subject dealing with computational models that can think and act rationally.

So that AI is the science and engineering of making intelligent machines and


especially intelligent computer programs.

AI problems

Problems for which straightforward mathematical/logical algorithms are not


readily available and which can be solved by intuitive approach only are called
AI problems.

For solving an AI problem, one may employ both AI and non AI algorithms.

Now, What is AI algorithm?

Formally speaking, an AI algorithm generally means a non – conventional


intuitive approach for problem solving. The key to AI approach is intelligent
search and matching. In an intelligent search problem/sub-problem, given a
goal (or starting) state, one has to reach that state from one or more known
starting (or goal) states. The less number of states are generated for reaching
the goal, the better is the AI algorithm. Such method of solving a problem is
referred as “ state space” approach.

How are AI problems different from other?

4
Prof. Dr. Ali Hussein Hasan
1- If a solution to the problem needs symbolic representation in computers,
then we can consider it as an AI problem. Ex. Understanding of English
sentences by a computer when they are represented in the symbolic form.
2- If there is a combinatorial explosion in outputting the result of a
traditional problem. Ex. 8-queens and travelling salesman problems.

Topics of AI

1- Learning systems:- learning is any process by which a system improves


performance from experience. Learning system or process can be treated
as an adaptive learning process where the parameters are adjusted
continuously to get results close enough to the training dataset.
2- Knowledge representation and reasoning:- knowledge is an expertise
and skill acquired by people which is very hard to represent in
computers. The knowledge base is similar to a database which is
necessary for expert system building and reasoning. In reasoning
problems, one has to achieve a pre–defined target state from one or more
given start states. Therefore, less number of transition gives high
efficiency in the reasoning system to reach the goal state. To increase
the efficiency of a reasoning system requires the minimization of
intermediate states, which ultimately calls for an organized and
complete knowledge base. A variety of knowledge representation
techniques are used in AI such as production rules, frames, semantic
nets,… etc.
3- Planning:- problem planning involves the determination of methodology
from which a successful result can be achieved from the known initial
states. Planning is focused in the area of decomposition of an original
problem into appropriate subunits and uses interaction on these subunits
during the problem solving process. Extensive applications in robotics
and navigational problems.
4- Knowledge acquisition:- acquisition or ‘ elicitation’ of knowledge is
uniformly as hard for human being as it is for a computer machine.
Knowledge acquisition includes the making of new pieces of knowledge
from a given knowledge network, setting active structures for existing
knowledge, learning knowledge from the environment and fine tuning
of knowledge.
5- Intelligent search:- the search problems in AI are non – deterministic
and the order of visiting the elements in the search space is completely
dependent on the given data sets.

5
Prof. Dr. Ali Hussein Hasan
6- Logic programming:- Logic programming is a type of programming
paradigm which is largely based on formal logic. Any program written
in a logic programming language is a set of sentences in logical form,
expressing facts and rules about some problem domain. Major logic
programming language families include Prolog, Answer set
programming (ASP) and Datalog. In all of these languages, rules are
written in the form of clauses:
H :- B1, …, Bn.
and are read declaratively as logical implications:
H if B1 and … and Bn.
7- Soft computing:- Soft computing differs from conventional (hard)
computing in that, unlike hard computing, it is tolerant of imprecision,
uncertainty, partial truth, and approximation. In effect, the role model
for soft computing is the human mind.
The principal constituents of Soft Computing (SC) are Fuzzy Logic
(FL), Evolutionary Computation (EC), Machine Learning (ML) and
Probabilistic Reasoning (PR), Artificial Neural Networks.
8- Management of imprecision and uncertainty:- imprecision generally
occurs in database due to the lack of appropriate data and poor
authenticity level of sources. It is uncertainty when the incompleteness
of knowledge is referred that originates in the knowledge base due to the
lack of certainty in the piece of knowledge.

6
Prof. Dr. Ali Hussein Hasan
Application of AI

1- Game playing:- most games are played using a well – defined set of
rules; this makes it easy to generate the search space and frees the
researchers from many of the ambiguities and complexities inherent in
less structure problem.
2- Automated reasoning and theorem proving:- most of the appeal of
automated theorem proving lies in the rigor and generally of logic.
Because it is a formal system, logic lends itself to automation. A wide
variety of problems can be attacked by representing the problem
instances as theorems to be proved.
3- Expert systems:- E.S. is a combination of a theoretical understanding of
the problem and a collection of heuristic problem solving results that
experience has shown to be effective in domain. E.S. are constructed by
obtaining this knowledge from a human expert and coding it into a form
that a computer may apply to similar problems.
4- Natural language processing NLP or understanding:- understanding
natural language involves much more than parsing sentences into their
individual parts of speech and looking these words up in a dictionary.
Real understanding depends on extensive background knowledge about
the domain of discourse and idioms used in that domains as well as an
ability to apply general contextual knowledge to resolve the omission
and ambiguities that are a normal part of human speech.
5- Image understanding:- image understanding is advanced image
processing in which artificial – intelligence techniques are used to
interpret images by locating, characterizing, and recognizing objects and
other features in the scene.
6- Robotics:- AI applied in robotics, in order to see, hear and react to other
sensory stimuli.
7- Machine learning:- Machine learning is a method of data analysis that
automates analytical model building. It is a branch of artificial
intelligence based on the idea that machines should be able to learn and
adapt through experience. It was born from pattern recognition and the
theory that computers can learn without being programmed to perform
specific tasks; researchers interested in artificial intelligence wanted to
see if computers could learn from data. The iterative aspect of machine
learning is important because as models are exposed to new data, they
are able to independently adapt. They learn from previous computations

7
Prof. Dr. Ali Hussein Hasan
to produce reliable, repeatable decisions and results. It’s a science that’s
not new – but one that has gained fresh momentum.
While many machine learning algorithms have been around for a long
time, the ability to automatically apply complex mathematical
calculations to big data – over and over, faster and faster – is a recent
development. Here are a few widely publicized examples of machine
learning applications you may be familiar with:
a- The heavily hyped, self-driving Google car (WAYMO). The essence
of machine learning.
b- Online recommendation offers such as those from Amazon and
Netflix? Machine learning applications for everyday life.
c- Knowing what customers are saying about you on Twitter? Machine
learning combined with linguistic rule creation.
d- Fraud detection. One of the more obvious, important uses in our
world today.
8- Pattern recognition:- is the process of classifying input data into objects
or classes based on key features. There are two classification methods in
pattern recognition: supervised and unsupervised classification.

9- Virtual reality:- Virtual reality (VR) is a computer technology that uses


Virtual reality headsets, sometimes in combination with physical spaces
or multi-projected environments, to generate realistic images, sounds
and other sensations that simulate a user's physical presence in a virtual
or imaginary environment. A person using virtual reality equipment is
able to "look around" the artificial world, and with high quality VR move
about in it and interact with virtual features or items. VR headsets are
head-mounted goggles with a screen in front of the eyes. Programs may
include audio and sounds through speakers or headphones.
VR systems that include transmission of vibrations and other sensations
to the user through a game controller or other devices are known as
haptic systems. This tactile information is generally known as force
feedback in medical, video gaming and military training applications.

8
Prof. Dr. Ali Hussein Hasan
Virtual reality also refers to remote communication environments which
provide a virtual presence of users with through telepresence and
telexistence or the use of a virtual artifact (VA). The immersive
environment can be similar to the real world in order to create a lifelike
experience grounded in reality. Augmented reality systems may also be
considered a form of VR that layers virtual information over a live
camera feed into a headset, or through a smartphone or tablet device.
10- Intelligent control:- Intelligent control is a class of control techniques
that use various artificial intelligence computing approaches like neural
networks, Bayesian probability, fuzzy logic, machine learning,
evolutionary computation and genetic algorithms.

9
Prof. Dr. Ali Hussein Hasan
AI Agents and their Environments
Particular artificial intelligence programs, can be thought of as intelligent
agents that interact with particular environments. In general, intelligent agents
of all types (including rats, people, as well as AI programs) interact with their
environments in two main ways: perception and action.
For purposes of AI, perception is the process of transforming something from
the environment into internal representations (memories, beliefs, etc.). Action
done when the agent, by doing something, changes the environment.
An AI system is composed of an agent and its environment. The agents act in
their environment. The environment may contain other agents.

What are Agent and Environment?

An agent is anything that can perceive its environment through sensors and acts
upon that environment through effectors.

• A human agent has sensory organs such as eyes, ears, nose, tongue and
skin parallel to the sensors, and other organs such as hands, legs, mouth,
for effectors.
• A robotic agent replaces cameras and infrared range finders for the
sensors, and various motors and actuators for effectors.
• A software agent has encoded bit strings as its programs and actions.

For example, if a robot uses its camera to determine that there is a wall in front
of it, then it is using perception. In this example, the camera is a "sensor." If
that robot uses its body to push the wall over, then it is doing action. The body,
in this case, is an "actuator". If the robot uses wheels to back up, this is also an
example of doing action. The environment has changed because the robot is

10
Prof. Dr. Ali Hussein Hasan
now in a different place. Typical robot actuators include arms, wheels, lights,
or speakers.

What about an AI that has no body? Let's take, for example, a music
recommendation system. In this case, the environment for the agent might be
the web page that the user clicks and types into, or perhaps a database of user
preferences. The sensors, in this case, are abstract functions in the agent's
programming, rather than pieces of hardware, as they are on a robot. The
function that detects when a user clicks, or the function that fetches information
from the database might be considered sensors for this agent. What would the
actuators be? When the agent displays recommended music to the user, they
are using actuators to display things on the screen--or to serve the web page.
Another example is the database that the music recommendation system uses-
-should it be considered a part of the environment that the agent interacts with,
or a part of the agent's memory? As you can see from this example, what is
considered an actuator and what is considered part of the environment depends
on what you consider to be the boundaries of the agent. There is not always a
right or wrong boundary. It depends on the context of the discussion.

Agent Terminology

Performance Measure of Agent − It is the criteria, which determines how


successful an agent is.

Behavior of Agent − It is the action that agent performs after any given
sequence of percepts.

Percept − It is agent’s perceptual inputs at a given instance.

Percept Sequence − It is the history of all that an agent has perceived till date.

Agent Function − It is a map from the precept sequence to an action.

11
Prof. Dr. Ali Hussein Hasan
Rationality

Rationality is nothing but status of being reasonable, sensible, and having good
sense of judgment.

Rationality is concerned with expected actions and results depending upon


what the agent has perceived. Performing actions with the aim of obtaining
useful information is an important part of rationality.

What is Ideal Rational Agent?

An ideal rational agent is the one, which is capable of doing expected actions
to maximize its performance measure, on the basis of − Its percept sequence.
Its built–in knowledge base.

Rationality of an agent depends on the following four factors –

• The performance measures, which determine the degree of success.


• Agent’s Percept Sequence till now.
• The agent’s prior knowledge about the environment.
• The actions that the agent can carry out.

A rational agent always performs right action, where the right action means the
action that causes the agent to be most successful in the given percept sequence.

The problem the agent solves is characterized by Performance Measure,


Environment, Actuators, and Sensors (PEAS).

Example: describe PEAS the Agent = taxi driver

Performance measure: Safe, fast, legal, comfortable trip, maximize profits.

Environment: Roads, other traffic, pedestrians, customers

Actuators: Steering wheel, accelerator, brake, signal, horn

Sensors: Cameras, sonar, speedometer, GPS, odometer, engine sensors,


keyboard

Example: Agent = Medical diagnosis system

Performance measure: Healthy patient, minimize costs, lawsuits

Environment: Patient, hospital, staff

Actuators: Screen display (questions, tests, diagnoses, treatments, referrals)

12
Prof. Dr. Ali Hussein Hasan
Sensors: Keyboard (entry of symptoms, findings, patient's answers)

HW: Agent = Part-picking robot

Performance measure: Percentage of parts in correct bins


Environment: Conveyor belt with parts, bins
Actuators: Jointed arm and hand
Sensors: Camera, joint angle sensors

The Structure of Intelligent Agents

13
Prof. Dr. Ali Hussein Hasan
Agent’s structure can be viewed as −

Agent = Architecture + Agent Program


Architecture = the machinery that an agent executes on.
Agent Program = an implementation of an agent function.

Simple Reflex Agents

- They choose actions only based on the current percept.


- They are rational only if a correct decision is made only on the basis of
current precept.
- Their environment is completely observable.
- Condition-Action Rule − It is a rule that maps a state (condition) to an
action.

Model Based Reflex Agents

14
Prof. Dr. Ali Hussein Hasan
They use a model of the world to choose their actions. They maintain an
internal state.

Model − The knowledge about “how the things happen in the world”.

Internal State − It is a representation of unobserved aspects of current state


depending on percept history.

Updating the state requires the information about −

+ How the world evolves.

+ How the agent’s actions affect the world.

Goal Based Agents

15
Prof. Dr. Ali Hussein Hasan
They choose their actions in order to achieve goals. Goal-based approach is
more flexible than reflex agent since the knowledge supporting a decision is
explicitly modeled, thereby allowing for modifications.

Goal − It is the description of desirable situations.

Utility Based Agents

16
Prof. Dr. Ali Hussein Hasan
They choose actions based on a preference (utility) for each state. Goals are
inadequate when –

There are conflicting goals, out of which only few can be achieved.

Goals have some uncertainty of being achieved and you need to weigh
likelihood of success against the importance of a goal.

Nature of Environments

Some programs operate in the entirely artificial environment confined to


keyboard input, database, computer file systems and character output on a
screen.

In contrast, some software agents (software robots or softbots) exist in rich,


unlimited softbots domains. The simulator has a very detailed, complex
environment. The software agent needs to choose from a long array of actions
in real time. A softbot designed to scan the online preferences of the customer
and show interesting items to the customer works in the real as well as an
artificial environment.

The most famous artificial environment is the Turing Test environment, in


which one real and other artificial agents are tested on equal ground. This is a

17
Prof. Dr. Ali Hussein Hasan
very challenging environment as it is highly difficult for a software agent to
perform as well as a human.

Turing Test

The success of an intelligent behavior of a system can be measured with Turing


Test.

Two persons and a machine to be evaluated participate in the test. Out of the
two persons, one plays the role of the tester. Each of them sits in different
rooms. The tester is unaware of who is machine and who is a human. He
interrogates the questions by typing and sending them to both intelligences, to
which he receives typed responses.

This test aims at fooling the tester. If the tester fails to determine machine’s
response from the human response, then the machine is said to be intelligent.

Properties of Environment

The environment has multifold properties −

1- Discrete / Continuous − If there are a limited number of distinct, clearly


defined, states of the environment, the environment is discrete (For
example, chess); otherwise it is continuous (For example, driving).
2- Observable / Partially Observable − If it is possible to determine the
complete state of the environment at each time point from the percepts
it is observable; otherwise it is only partially observable.
3- Static / Dynamic − If the environment does not change while an agent is
acting, then it is static; otherwise it is dynamic.

18
Prof. Dr. Ali Hussein Hasan
4- Single agent / Multiple agents − The environment may contain other
agents which may be of the same or different kind as that of the agent.
5- Accessible / Inaccessible − If the agent’s sensory apparatus can have
access to the complete state of the environment, then the environment is
accessible to that agent.
6- Deterministic / Non-deterministic − If the next state of the environment
is completely determined by the current state and the actions of the
agent, then the environment is deterministic; otherwise it is non-
deterministic.
7- Episodic / Non-episodic − In an episodic environment, each agent’s
performance is the result of a series of independent tasks performed.
There is no link between the agent’s performance and other different
scenarios. In other words, the agent decides which action is best to take,
it will only consider the task at hand and doesn’t have to consider the
effect it may have on future tasks.
Examples:
Episodic environment: mail sorting system
Non-episodic environment: chess game.

Example: describe the agent and environment of ski course as in picture.

there are three different colours of paths leading down the mountain. These
different colours represent three different difficulty levels, blue is for
beginners, red for intermediates and black for experts.

19
Prof. Dr. Ali Hussein Hasan
The idea is that there are multiple agents that represent skiers. The task of each
agent is to make their way from the top of the mountain, down to the lodge in
the fastest time possible. The agents will have different skill levels and a
confidence level that will affect their decision making abilities on which route
to take.

Agents
The agents in this system are represented by skiers.

Environment
The environment is the slope that contains different pistes for different
difficulty settings.

Tasks
The task of all the agents is to make to the lodge as soon as possible.

Effectoric capabilities

The agents will be able to perform particular tasks in order to navigate down
the slope. These include:

• Speeding up.
• Slowing down.
• Stopping.
• Turning.
• Bailing out.

Reasoning
The agents would mainly use practical reasoning. The factors affecting which
route they decide to take will be a combination of the agent's skill level and
their confidence. As the agents progress down the slope, naturally their skill
will improve as will their confidence. This means that they can take different
routes down the slope. But the environment can affect an agents confidence.
For example, if the agent falls then its skill will not decrease but its confidence
will. Similarly if the weather takes a turn for the worse.

20
Prof. Dr. Ali Hussein Hasan
Propositional and Predicate Logic

-Propositional Logic

Predicate calculus provides AI programmers with a well-defined language for


describing and reasoning about qualitative aspects of a system. Using their
words, phrases, and sentences, we can represent and reason about properties
and relationships in the world.

In general, in logic, we are not concerned with the truth of statements, but rather
with their validity. That is to say, although the following argument is clearly
logical, it is not something that we would consider to be true:

All lemons are blue

Mary is a lemon

Therefore, Mary is blue

This set of statements is considered to be valid because the conclusion (Mary


is blue) follows logically from the other two statements, which we often call
the premises. The reason that validity and truth can be separated in this way is
simple: a piece of a reasoning is considered to be valid if its conclusion is true
in cases where its premises are also true. Hence, a valid set of statements such
as the ones above can give a false conclusion, provided one or more of the
premises are also false. We can say: a piece of reasoning is valid if it leads to
a true conclusion in every situation where the premises are true. Logic is
concerned with truth values. The possible truth values are true and false. These
can be considered to be the fundamental units of logic, and almost all logic is
ultimately concerned with these truth values.

Notice:- The propositional logic is not powerful enough to represent all types
of assertions that are used in computer science and mathematics, or to express
certain types of relationship between propositions such as equivalence.

For example, the assertion "x is greater than 1", where x is a variable, is not a
proposition because you can not tell whether it is true or false unless you know
the value of x. Thus the propositional logic can not deal with such sentences.
However, such assertions appear quite often in mathematics and we want to do
inferencing on those assertions.

Also the pattern involved in the following logical equivalences can not be
captured by the propositional logic:

21
Prof. Dr. Ali Hussein Hasan
"Not all birds fly" is equivalent to "Some birds don't fly". "Not all integers are
even" is equivalent to "Some integers are not even". "Not all cars are
expensive" is equivalent to "Some cars are not expensive",..

Each of those propositions is treated independently of the others in


propositional logic. For example, if P represents "Not all birds fly" and Q
represents "Some integers are not even", then there is no mechanism in
propositional logic to find out that P is equivalent to Q. Hence to be used in
inferencing, each of these equivalences must be listed individually rather than
dealing with a general formula that covers all these equivalences collectively
and instantiating it as they become necessary, if only propositional logic is
used.

The first step in describing a language is to introduce the pieces that make it
up, its set of symbols:-

Definition 1:- propositional (predicate) calculus symbols:

a- Propositional symbols : P, Q, R, S,….. . Propositional symbols denote


proposition or statements about the world that may be either true or false,
such as “ the car is red” or “the water is wet”. Propositions are denoted
by uppercase letters near the end of the English alphabet.
b- Truth symbols:- True or False.
c- Connectives symbols: ˄, ˅, ~, and  ….

Sentences in the propositional calculus are formed from these atomic symbols
according to the following rules:-

a- Every propositional symbol and truth symbol is a sentence.


b- The negation of a sentence is a sentence.
c- The conjunction (and) , of two sentences is a sentence. Ex. P˄Q; P & Q
called conjuncts.
d- Disjunction ( or ), of two sentences is a sentence. P˅Q, P & Q called
disjuncts.
e- The implication of one sentence from another is a sentence. P→Q, P is
the premise or antecedent and Q the conclusion or consequent.
f- The equivalence of two sentences is a sentence.

* The bracts ( ) and [ ] used to group symbols into sub expressions.

* Legal sentences are called well-formed formulas or WFF.

22
Prof. Dr. Ali Hussein Hasan
* an expression is a sentence or WFF if and only if it can be formed of legal
symbols through some sequence of these rules.

Definitions 2:-

1- connectives in propositional logic:

Symbol Meaning
˄ And
˅ Or
~ Negation
p→q if p then q, if p means “you live in France” and q means “You
speak
French,” then p→q corresponds to the statement “if you live in
France, then
you speak French.”
𝑝↔𝑞 if p then q and if q then p, If I have 1000 Dinars then only I will
go to buy sandwich. The converse condition that I will go to buy
sandwich if and only if I have 1000 Dinars. The first statement
covers necessity and the second one covers sufficiency.
Implication
Bidirectional implication
≡ Identity
⊢ Derivability, 𝑥 ⊢ 𝑦 means that y has been derived from x by a
procedure.
⊨ Logical entailment, 𝑥 ⊨ 𝑦 mean that y logically follows from x

2- Proposition: is a statement or its negation or group of statements and/or


their negations connected by AND, OR and, IF-THEN operators.
Ex.
P.
It is hot, the sky is cloudy.
It is hot and the sky is cloudy.
It is hot → the sky is cloudy.
3- When the statement can not be logically broken into smaller statements
it is called atomic.
Ex. P, it is hot, the sky is cloudy.
4- A proposition can assume a binary valuation space i.e. for a proposition
P its valuation space 𝑉(𝑃) ∈ {0,1}.

23
Prof. Dr. Ali Hussein Hasan
5- Let r be a propositional formula constructed by connecting atomic
proposition p,q,s, etc. An interpretation for r is a function that maps v(p),
v(q) and v(s) into true or false values that together keep r true.
Ex:
𝑝∧𝑞
The possible interpretation is
v(p) = true and v(q)=true
ex:
∼𝑝∨𝑞
Interpretation :- {v(p)=true, v(q)=true}, {v(p)=false, v(q)= false},
{v(p)=false, v(q)=true}.
6- A propositional formula is called satisfiable if its value is true for some
interpretation.
** generally use ⊨ 𝑝 to denote that p is satisfiable.
7- A propositional formula is called valid or tautology, when it is true for
all possible interpretations.

Tautologies in propositional logic

The tautologies may be directly used for reasoning in propositional logic.

Ex: p1=the sky is cloudy.

p2=it will rain.

p3=if the sky is cloudy then it will rain.

p3=p1→p2.

p1 & p2 represent premise and conclusion respectively for the if-then clause.

Definitions:

1− ∼∼ 𝑝 ≡ 𝑝. 2- 𝑝 ∧ 𝑞 ≡ 𝑞 ∧ 𝑝. 3-𝑝 ∨ 𝑞 ≡ 𝑞 ∨ 𝑝. 4-(𝑝 ∧ 𝑞) ∧ 𝑟 ≡ 𝑝 ∧
(𝑞 ∧ 𝑟).

5-(𝑝 ∨ 𝑞) ∨ 𝑟 ≡ 𝑝 ∨ (𝑞 ∨ 𝑟). 6-𝑝 ∧ (𝑞 ∨ 𝑟) ≡ (𝑝 ∧ 𝑞) ∨ (𝑝 ∧ 𝑟). 7- 𝑝 ∨ (𝑞 ∧


𝑟) ≡ (𝑝 ∨ 𝑞) ∧ (𝑝 ∨ 𝑟).

8− ∼ 𝑝(𝑝 ∧ 𝑞) ≡∼ 𝑝 ∨∼ 𝑞. 9- ∼ (𝑝 ∨ 𝑞) ≡∼ 𝑝 ∧∼ 𝑞. 10-𝑝 ∨ 𝑝 ≡ 𝑝. 11-


𝑝 ∧ 𝑝 ≡ 𝑝.

12- 𝑝 → 𝑞 ≡ ~𝑝⋁𝑞

24
Prof. Dr. Ali Hussein Hasan
Translating between English and Logic Notation

• To use logic, it is first necessary to convert facts and rules about the real
world into logical expressions using the logical operators.

• Without a reasonable amount of experience at this translation, it can seem


quite a daunting task in some cases.

• Let us examine some examples. First, we will consider the simple operators,
∧, ∨, and ~.

• Sentences that use the word and in English to express more than one concept,
all of which is true at once, can be easily translated into logic using the AND
operator, ∧.

-- For example: “It is raining and it is Tuesday.” might be expressed as: R ∧T,
Where R means “it is raining” and T means “it is Tuesday.”

-- For example, if it is not necessary to discuss where it is raining, R is probably


enough.

• If we need to write expressions such as “it is raining in New York” or “it is


raining heavily” or even “it rained for 30 minutes on Thursday,” then R will
probably not suffice. To express more complex concepts like these, we usually
use predicates.

Hence, for example, we might translate “it is raining in New York” as: N(R)
We might equally well choose to write it as: R(N).

• This depends on whether we consider the rain to be a property of New York,


or vice versa. In other words, when we write N(R), we are saying that a property
of the rain is that it is in New York, whereas with R(N) we are saying that a
property of New York is that it is raining. Which we use depends on the
problem we are solving. It is likely that if we are solving a problem about New
York, we would use R(N), whereas if we are solving a problem about the
location of various types of weather, we might use N(R).

• Let us return now to the logical operators. The expression “it is raining in
New York, and I’m either getting sick or just very tired” can be expressed as
follows: R(N) ∧(S(I) ∨T(I)).

25
Prof. Dr. Ali Hussein Hasan
• Here we have used both the ∧ operator, and the ∨ operator to express a
collection of statements. The statement can be broken down into two sections,
which is indicated by the use of parentheses.

• The section in the parentheses is S(I) ∨ T(I), which means “I’m either getting
sick OR I’m very tired”. This expression is “AND’ed” with the part outside the
parentheses, which is R(N).

• Finally, the ~ operator is applied exactly as you would expect—to express


negation.

-- For example, It is not raining in New York, might be expressed as ~R(N).

• It is important to get the ~ in the right place. For example: “I’m either not
well or just very tired” would be translated as ~W(I) ∨ T(I).

• The position of the ~ here indicates that it is bound to W(I) and does not play
any role in affecting T(I).

• Now let us see how the → operator is used. Often when dealing with logic
we are discussing rules, which express concepts such as “if it is raining then I
will get wet.”

• This sentence might be translated into logic as R→W(I).

• This is read “R implies W(I)” or “IF R THEN W(I)”. By replacing the


symbols R and W(I) with their respective English language equivalents, we can
see that this sentence can be read as “raining implies I’ll get wet” or “IF it’s
raining THEN I’ll get wet.”

• Implication can be used to express much more complex concepts than this.

• For example, “Whenever he eats sandwiches that have pickles in them, he


ends up either asleep at his desk or singing loud songs” might be translated as

S(y) ∧P(y) ∧ E(x, y) →A(x) ∨(Ss(x, z) ∧L(z))

• Here we have used the following symbol translations:

S(y) means that y is a sandwich.

E(x,y) means that x (the man) eats y (the sandwich).

P(y) means that y (the sandwich) has pickles in it.

26
Prof. Dr. Ali Hussein Hasan
A(x) means that x ends up asleep at his desk.

Ss(x, z) means that x (the man) sings z (songs).

L(z) means that z (the songs) are loud.

• The important thing to realize is that the choice of variables and predicates is
important, but that you can choose any variables and predicates that map well
to your problem and that help you to solve the problem.

• For example, in the example we have just looked at, we could perfectly well
have used instead S→A ∨ L where S means “he eats a sandwich which has
pickles in it,” A means “he ends up a sleep at his desk,” and L means “he sings
loud songs.”

• The choice of granularity is important, but there is no right or wrong way to


make this choice. In this simpler logical expression, we have chosen to express
a simple relationship between three variables, which makes sense if those
variables are all that we care about—in other words, we don’t need to know
anything else about the sandwich, or the songs, or the man, and the facts we
examine are simply whether or not he eats a sandwich with pickles, sleeps at
his desk, and sings loud songs.

• The first translation we gave is more appropriate if we need to examine these


concepts in more detail and reason more deeply about the entities involved.

• Note that we have thus far tended to use single letters to represent logical
variables. It is also perfectly acceptable to use longer variable names, and thus
to write expressions such as the following:

Fish (x) ∧ living (x) → has_scales (x)

• This kind of notation is obviously more useful when writing logical


expressions that are intended to be read by humans but when manipulated by a
computer do not add any value.

Examples:

English Logic Example

27
Prof. Dr. Ali Hussein Hasan

And, but AND Λ It is hot but/and sunny

A: It is hot
B: It is sunny

AΛB

Not NOT ~ It is not hot: ~A

Or (inclusive) OR V It is hot or sunny

AVB

Or (exclusive) A or B but not It is either hot or sunny


both
(A V B) Λ ~ (A Λ B)

Neither… nor ~AΛ~B It is neither hot nor


sunny

~AΛ~B

Ex:

If it is Sunday we go fishing.

It is Sunday

Therefore we go fishing

P→Q

If it is Sunday we go fishing

We do not go fishing

Therefore it is not Sunday

28
Prof. Dr. Ali Hussein Hasan
P→Q

~Q

~P

If we win the game we will get much money.

If we have money we will go on a trip to China.

Therefore, if we win the game we will go on a trip to China

P→Q

Q→R

P→R

Hw:-

If I am clever then I will pass,

If I will pass then I am clever,

Either I am clever or I will pass.

I am clever and I will pass.

Ex:

Convert the following to propositional logic

If you work hard then you get lucky. Either you get lucky or you work hard, or
both. If you get lucky then, either you are not a rogue or you work hard (but
not both). You are a rogue.

If Mary loves Pat then Mary loves Quincy. If it is Monday then Mary loves Pat
or Quincy (or both). Mary does not love both Pat and Quincy.

If the car has no fuel or it has no spark, then it will not start. The car has a spark.
The car will not start.

29
Prof. Dr. Ali Hussein Hasan
Control structures in reasoning

The manipulation of symbols to produce action is called reasoning.

One way that AI representations differ from computer programs in traditional


languages is that an AI representation typically specifies what needs to be
computed, not how it is to be computed. We might specify that the agent should
find the most likely disease a patient has, or specify that a robot should get
coffee, but not give detailed instructions on how to do these things. Much AI
reasoning involves searching through the space of possibilities to determine
how to complete a task.

We will deal with three methods for control the reasoning in Prolog:

Backward chaining, Forward chaining and Hybrid control structures.

i- Backward chaining - Goal directed reasoning


An inference method where the system starts with what it wants to prove,
e.g. goal, and try to establish the facts it need to prove the goal.
If alternative conclusions are possible, backward chaining can try to prove
the 1st, then try the 2nd if the 1st fails and so on, like an ‘or’.
– Most useful where we have a known conclusion to check.
– Also useful if there are a small number of possible conclusions to check
(e.g., what virus does the patient have?)
– This approach is also useful where fact base is not complete. If data is
missing, the user is asked to provide missing details.

Algorithm:

Idea:– Check whether a particular fact q is true.

Backward Chaining:

Given a fact q to be “proven”,

1. See if q is already in the facts If so, return TRUE.

2. Find all implications, I, whose conclusion “matches” q.

3. Recursively establish the premises of all I in I via backward chaining.

30
Prof. Dr. Ali Hussein Hasan
Ex: prove Z is true from the following knowledge base:

R1: 𝑍: −𝐹 ∧ 𝐵.

R2: F: −𝐶 ∧ 𝐷. RULES

R3: D:-A.

A.

B.

C.

E. FACTS

G.

H.

Solution:

Step1:- the system checks the facts for Z. And when that fails, search the rules
that conclude Z, i.e. have Z on the left side. It find Z in the R1 and decides that
it must establish F & B in order to conclude Z.

Step2:- the system try to establish F, first checking the data base and then
finding a rule R2 that conclude F. From this rule the system must establish C
& D to conclude F.

Step3:- the system try to establish C, first checking the data base and then
finding it.

Step4:- the system try to establish D, first checking the data base and then
finding a rule R3 that conclude D. From this rule the system must establish A
to conclude D.

Step5:- the system tries to establish A, first checking the data base and then
finding it.

Step6:- executes R3 to establish D.

31
Prof. Dr. Ali Hussein Hasan
Step7:- executes R2 to establish F.

Step8:- executes R1 to establish the goal Z.

Goal Z

F B
TRUE
E

C D
TRUE

A
TRUE

Home work :- prove 𝑔1 ∨ 𝑔2(𝑍)

R1: g1:-f1.
R2: g1:-a,b.
R3: g2(X):-c(X).
R4: a:-not(d).
R5: b:-d.
R6: b:- e.
R7: c(2):- not(e).
R8: d:-f2,f3.
R9: e:-f2,f4.
f2.
f3.
not(f1).
not(f4).

32
Prof. Dr. Ali Hussein Hasan
ii- Forward chaining – data directed computation or Modus Ponens
Reasoning
Modus Ponens Reasoning = the way that affirms by affirming.
‫الطريقة التي تؤكد بتأكيدها‬
There is no query, no goal, we just use the facts we know to infer some
new facts.
- In some cases, not very efficient as we might generate many thousands
of conclusions before the one we want is generated
- Useful where we want to know everything which can be derived from
the known facts
- Useful where there are many valid solutions, all potentially valid.
- Can be used where all input data (facts) are assumed to be available
from the beginning of each problem.
Algorithm:
1- Ignore all the false facts or rules.
2- Mark all facts as unused and get a fresh copy of the rule.
3- Until no more unused facts remains, pick the first-listed one, call it F
‘pursue’.
Take the 1st unused fact, let it is F:-
a- Let R is the rule that contains the fact F in the right side. Do the
following steps with all rules that contain F in its right side:
i- Create new fact/facts identical to the rule R except F is
deleted.
ii- If the right side of the R became empty, then put R in the
facts and mark it as unused. Delete all remaining rules that have
the same left side name.
iii- If the right side of R is not empty after deleting F, then a
new rule will be created, and delete R.
b- Mark F as used:- after all steps in (a) was completed, repeat the work
with new unused fact.
4- After finishing all unused facts and mark them as used, search the rules
that contain the ‘not’. If the negation for not given fact in the facts list,
then add the ‘not’ of the fact to the facts list and consider it as true and
mark it as unused then repeat the steps in 3.

33
Prof. Dr. Ali Hussein Hasan
Ex:- R1: 𝑍: −𝐹 ∧ 𝐵 .
R2 : 𝐹: −𝐶 ∧ 𝐷.
R3 : D:-A.
A.
B.
C.
E.
G.
H.

Solution:

1- Mark all facts as unused.


2- Starting with the fact A.
3- Delete A from R3. To produce D. since the right side of R3 is empty,
then D become a fact and added to the facts list. Delete R3.
4- Mark A as used.
5- The knowledge base will become as following:
R1: 𝑍: −𝐹 ∧ 𝐵.
R2 : 𝐹: −𝐶 ∧ 𝐷.
A*.
B.
C.
E.
G.
H.
D.
6- Take the second fact B.
7- B is found in R1 only, delete it from R1. New rule is created R4.
Delete B from the R1 and mark it as used and delete R1.
R2 : 𝐹: −𝐶 ∧ 𝐷.
R4 : Z:-F.
A*.
B*.
C.
E.
G.
H.
D.

34
Prof. Dr. Ali Hussein Hasan
8- Take the fact C. it is found in R2, delete it. New rule is created R5
and delete R2. Mark C as used fact.
R4 : Z:-F.
R5: F:- D.
A*.
B*.
C*.
E.
G.
H.
D.
9- Take E, then G, then H, we notice they are not found in the right side
of any rule. Mark them as used facts
10- Take D, it is found in R5. Delete D from R5. A new fact created
is F, add it to the facts, and delete R5. Mark D as used.
R4 : Z:-F.
A*.
B*.
C*.
E*.
G*.
H*.
D*.
F.
11- Take F, it is found in R4. Delete it from R4. A new fact created
Z add it to the facts, delete R4, mark F as used.
12- There is no rule to infer so the work is stop.

Ex:-

prove 𝑔1 ∨ 𝑔2(𝑍)

R1: g1:-f1.
R2: g1:-a,b.
R3: g2(X):-c(X).
R4: a:-not(d).
R5: b:-d.
R6: b:- e.

35
Prof. Dr. Ali Hussein Hasan
R7: c(2):- not(e).
R8: d:-f2,f3.
R9: e:-f2,f4.
f2.
f3.
Solution:-

1- Take f2, it is found in R8 & R9. Delete it from the rules. New rules
is created.
R10: d:-f3.
R11: e:- f4.
f2*.
f3.
2- Take f3, it is found in R10, delete it from R10, then new fact is created.
Delete R10.
f2*, f3*, d.
3- Take d, it is found in R5 and R4 but in negation, so suspended to the end.
Delete d from R5 to produce the fact b.
R1: g1:-f1.
R2: g1:-a,b.
R3: g2(X):-c(X).
R4: a:-not(d).
R6: b:- e.
R7: c(2):- not(e).
R11: e:- f4.
f2*, f3*, d*, b.
4- Take b, delete it from R2, to get R12, delete R2. Also delete R6 not
needed.
R1: g1:-f1.
R3: g2(X):-c(X).
R4: a:-not(d).
R7: c(2):- not(e).
R11: e:- f4.
f2*, f3*, d*, b*.
5- There is no facts to take, but we have two suspended ‘not’.
6- Take R4, since d is true so R4 is false.
7- Take R7, since e is not found in facts so add not(e ) is added to the facts.
So delete not(e ) from R7 to yield fact c(2).

36
Prof. Dr. Ali Hussein Hasan
8- Take the fact c(2), it is found in R3, and it is true only if X=2, so R3
become true only in g2(2). But can not delete it.
iii- Hybrid control structure ( Rule – cycle hybrid)
The rules are tried in order as with backward chaining, but each rule is used
in forward chaining to assert new facts.
1- Ignoring any rules with ‘not’.
2- If the conditions on the right side of some rule all match the facts, then
the rule succeeds its left side and add to the database as new fact. And
eliminate from further consideration all rules whose left sides are
equivalent to the new fact. If the rule left side has variables, do this for
every possible way of binding these variables.
3- When no new rules succeed on a cycle through all of them, rules with
‘not’ now considered; cycling resumes at the top of rules, with the ‘not’
expressions now succeeding if their arguments are not now facts. Again
we continue until no new rules succeed on a cycle.

Ex:-

R1: g1:-f1.
R2: g1:-a,b.
R3: g2(X):-c(X).
R4: a:-not(d).
R5: b:-d.
R6: b:- e.
R7: c(2):- not(e).
R8: d:-f2,f3.
R9: e:-f2,f4.
f2.
f3.

Sol.:- cycle 1:

1- R1, R2, R3, R5, & R6 are tried respectively. R4 and R7 skipped because
of ‘not’. No one succeed because nothing on any right side matches a
fact that is stated to be true.
2- R8 is tried and it succeeds. Fact d is asserted and eliminate R8.
3- R9 fails.

37
Prof. Dr. Ali Hussein Hasan
Cycle2:-

1- R1, R2 & R3 are tried respectively. R4 is skipped because of ‘not’.


2- R5 now succeeds. Fact b is asserted and eliminate R5.
3- R7 is ignored.
4- R9 is fails.

The rules and facts become:

R1: g1:-f1.
R2: g1:-a,b.
R3: g2(X):-c(X).
R4: a:-not(d).
R7: c(2):- not(e).
R9: e:-f2,f4.
f2.
f3.
d.
b.

Cycle3: All the remaining rules are fail.

Cycle4: include the rules with ‘not’.

1- R1, R2 & R3 fail as before, R4 fails because d is fact.


2- R7 succeeds because e is not a fact, so c(2) is a new fact and eliminate
R7.
3- R9 fails.

Cycle5:

1- R2 & R2 fail as before, R3 succeeds with X=2 only and g2(2) must be a
fact. We can not eliminate R3 because g2(2) is more specific than the
left side of R3.
Now the remaining rules and facts are:-

R1: g1:-f1.
R2: g1:-a,b.
R3: g2(X):-c(X).
R4: a:-not(d).

38
Prof. Dr. Ali Hussein Hasan
R9: e:-f2,f4.
f2.
f3.
d.
b.
not(e).
c(2).
g2(2).

Cycle6:- all remaining rules are fail.

*That is every think we can conclude.

39
Prof. Dr. Ali Hussein Hasan
Theorem proving by propositional logic

i- Semantic method

The conclusion ‘C’ follows from a set of premises P1, P2, … . . Pn ⇒ C or


P1, P2, … . . Pn ⊨ C.

First construct a truth table representing the relationship of P1 through Pn with


‘C’. then test the validity of the theory by checking whether both the forward
and backward chaining methods.

Ex: let P1= the sky is cloudy. P2= it will rain. And 𝑃3 ≡ 𝑃1 → 𝑃2 to be three
propositions.

Attempt to check whether forward and backward chaining holds good for the
following theorem: 𝑃1, 𝑃3 ⟹ 𝑃2.

Sol.:Truth table:-

P1 P2 𝑃3 ≡ 𝑃1 → 𝑃2 ≡ ~𝑃1 ∨ 𝑃2
0 0 1
0 1 1
1 0 0
1 1 1

Forward chaining:- when all premises are true, check whether the conclusion
is true. Under this circumstance we say that forward hold good.

Now, when P1 & P3 are true, check if P2 is true. Note that in the last row,
P1=1, P3=1 yield P2=1. So forward chaining holds good.

Backward chaining:- when all the consequences are false, check whether at
least one of the premises is false.

P2=0 in the 1st & 3rd rows.

In 1st row P2=0 & P1=0.

In the 3rd row P2=0 & P3=0, so backward chaining hold good.

So as forward and backward chaining both satisfied together, then the theorem
𝑃1, 𝑃3 ⟹ 𝑃2 holds good.

Ex:- show that 𝑃2, 𝑃3 ⇏ 𝑃1

40
Prof. Dr. Ali Hussein Hasan
P1=0 then P2=0 (1st row) backward chaining.

P2=1 P3=1 P1=0 (2nd row) forward chaining.

Fails.

ii- Syntactic methods

Standard theorems:-

1- ~𝑝 ∧ (𝑝 ∨ 𝑞) ⇒ 𝑞. 2- ~𝑞 ∧ (𝑝 ⟶ 𝑞) ⇒ ~𝑝.

3- 𝑝 ⟶ 𝑞 ⇒ ~𝑝 ∨ 𝑞. 4- ~(𝑝 ⟶ 𝑞) ⇒ 𝑝 ∧ ~𝑞

5- 𝑝 ⟷ 𝑞 ⇒ (𝑝 ⟶ 𝑞) ∧ (𝑞 ⟶ 𝑝).

6- 𝑝 ⟷ 𝑞 ⇒ (𝑝 ∧ 𝑞 ) ∨ (~𝑝 ∧ ~𝑞) prove?

7-(𝑝 ∨ 𝑞) ∧ (𝑝 ⟶ 𝑟) ∧ (𝑞 ⟶ 𝑟) ⇒ 𝑟. Prove?

8- 𝑝 ⟶ (𝑞 ⟶ 𝑟) ⇒ (𝑝 ∧ 𝑞) ⟶ 𝑟. Prove?

9- ( 𝑝 ⟶ 𝑞) ∧ (𝑞 ⟶ 𝑟) ⇒ 𝑝 ⟶ 𝑟. Prove?

10- 𝑝 ∧ (𝑝 ⟶ 𝑞) ∧ (𝑞 ⟶ 𝑟) ⇒ 𝑟. Prove?

Now the syntactic approach for theorem proving can be done in two ways:

1- method of substitution and 2- Wang’s theorem.

1- Method of substitution

Here, left hand side or right hand side of the statement to be proved is chosen
and the standard formulas (above) are applied selectively to prove the other
side of the statement.

Ex: prove (𝑝 ⟶ 𝑞) ⇔ ~𝑞 ⟶ ~𝑝

LHS 𝑝 ⟶ 𝑞

⟹ ~𝑝 ∨ 𝑞

⟹ 𝑞 ∨ ~𝑝

⟹ ~(~𝑞) ∨ ~𝑝

~𝑞 ⟶ ~𝑝

41
Prof. Dr. Ali Hussein Hasan
Ex: prove 𝑝 ⟶ (𝑞 ⟶ 𝑟) ⟺ (𝑝 ∧ 𝑞) ⟶ 𝑟

Lhs: 𝑝 ⟶ (𝑞 ⟶ 𝑟)

⟹ 𝑝 ⟶ (~𝑞 ∨ 𝑟)

⟹ ~𝑝 ∨ (~𝑞 ∨ 𝑟)

⟹ (~𝑝 ∨ ~𝑞) ∨ 𝑟

⟹ ~(𝑝 ∧ 𝑞) ∨ 𝑟

⟹ (𝑝 ∧ 𝑞) ⟶ 𝑟

2- Theorem proving by Wang’s Algorithm

Any theorem of propositional logic is often represented in the following form:-

𝑝1, 𝑝2, 𝑝3, … . 𝑝𝑛 ⟹ 𝑞1, 𝑞2, 𝑞3, … . , 𝑞𝑚

Where:- pi & qj represent propositions.

Comma ( , ) in the LHS represents AND.

( , ) in the RHS represents OR.

i.e.
𝑝1 ∧ 𝑝2 ∧ 𝑝3 ∨ … .∧ 𝑝𝑛 ⟹ 𝑞1 ∨ 𝑞2 ∨ 𝑞3 ∨ … . 𝑞𝑚

Algorithm:-

S1. Represent all sentences involving only ∧,∨ & ~.


S2. Recursive procedure:- repeat a, b, or c until the stopping condition
in S3:-
a- Negation removal:- in case negated term is presented at any side,
bring it to the other side of implication symbol without its
negation symbol.
i.e. 𝑝, 𝑞, ~𝑟 ⟹ 𝑠
⊨ 𝑝, 𝑞 ⟹ 𝑟, 𝑠.
b- AND & OR removal:- if LHS contains ∧, replace it by comma ( ,
). And if RHS contains ∨, replace it by comma ( , )
𝑝∧𝑞∧𝑟⟹𝑠∨𝑡
⊨ 𝑝, 𝑞, 𝑟 ⟹ 𝑠, 𝑡.

42
Prof. Dr. Ali Hussein Hasan
c- Theorem splitting:- if the LHS contains OR, then split the theorem
into two sub-theorems by replacing OR. If the RHS contains
AND, then split the theorem into two sub-theorems.
𝑝 ∨ 𝑞 ⟹ 𝑠, 𝑡
⊨ 𝑝 ⟹ 𝑠, 𝑡 & 𝑞 ⟹ 𝑠, 𝑡
𝑝, 𝑞 ⟹ 𝑠 ∧ 𝑡
⊨ 𝑝, 𝑞 ⟹ 𝑠 & 𝑝, 𝑞 ⟹ 𝑡 .

S3. Stopping condition:- stop theorem proving process if either a or


b occurs:-
a- If both LHS and RHS contain common atomic terms.
b- If LHS and RHS have been represented as a collection of atomic
terms, and there exit no common terms on both sides.

Now:- in case all the sub-theorems are stooped, satisfying condition


S3-a then the theorem holds good.

Ex:- prove 𝑝, 𝑝 ⟶ 𝑞, 𝑞 ⟶ 𝑟 ⟹ 𝑟 by Wang’s theorem where p, q & r are


propositions (atomic).

Sol:-
𝑝, 𝑝 ⟶ 𝑞, 𝑞 ⟶ 𝑟 ⟹ 𝑟

By S1: 𝑝, ~𝑝 ∨ 𝑞, ~𝑞 ∨ 𝑟 ⟹ 𝑟

By S2c: 𝑝, ~𝑝 ∨ 𝑞, ~𝑞 ⟹ 𝑟 𝑝, ~𝑝 ∨ 𝑞, 𝒓 ⟹ 𝒓

Terminated by S3-a

By S2c: 𝑝, ~𝑝, ~𝑞 ⟹ 𝑟 By S2c: 𝑝, 𝑞, ~𝑞 ⟹ 𝑟

By S2a: 𝒑, ~𝑞 ⟹ 𝒑, 𝑟 By S2a: 𝑝, 𝒒 ⟹ 𝒒, 𝑟

Terminated by S3-a Terminated by S3-a

Since all the terminals of the three have been stopped by using S3-a, then the
theorem holds good.

43
Prof. Dr. Ali Hussein Hasan
Resolution in propositional logic

Resolution theorem:-

for any three clauses p,q and r then:- 𝑝 ∨ 𝑞, ~𝑞 ∨ 𝑟 ⟹ 𝑝 ∨ 𝑟

The idea of Propositional Resolution is simple. Suppose we have the clause {p,
q}. In other words, we know that p is true or q is true. Suppose we also have
the clause {~q, r}. In other words, we know that q is false or r is true. One
clause contains q, and the other contains ~q. If q is false, then by the first clause
p must be true. If q is true, then, by the second clause, r must be true. Since q
must be either true or false, then it must be the case that either p is true or r is
true. So we should be able to derive the clause {p, r}.

Resolution algorithm:-

Input:- set of clauses called axioms and goal.

Output:- to test whether the goal is derivable from the axioms.

1- Construct a set S of axioms plus the negated goal.


2- Represent each element of S into conjunctive normal form
(CNF). By the following:-
a- Replace if-then by ~ and ∨ operations.
b- Bring each modified clause into the following form and
then drop AND operators connected between each square
bracket. The clauses thus obtained in CNF.
[𝑝11 ∨ 𝑝12 ∨ 𝑝13 … … ∨ 𝑝1𝑛] ∧
[𝑝21 ∨ 𝑝22 ∨ 𝑝23 … … ∨ 𝑝2𝑛] ∧


[𝑝𝑚1 ∨ 𝑝𝑚2 ∨ 𝑝𝑚3 … … ∨ 𝑝𝑚𝑛].


3- Repeat:-
a- Select any two clauses from S, such that one clause
contains a negated literal and the other clause contains its
corresponding positive.
b- Resolve these two clauses and call the resulting clause the
resolvent. Remove the parent clauses from S.

44
Prof. Dr. Ali Hussein Hasan
Until a null clause is obtained or no further progress can be
made.

4- If a null clause is obtained, then report:- ‘ goal is proved’.

Ex:- consider the following knowledge base:-

1- The humidity is high ∨ the sky is cloudy.


2- If the sky is cloudy then it will rain.
3- If the humidity is high then it is hot.
4- It is not hot.

Goal:- it will rain.

Sol:-

1- let denote the clauses by the following symbols:


p= The humidity is high. q= the sky is cloudy.
r=it will rain. s=it is hot.
2- CNF:-
a- 𝑝 ∨ 𝑞. b- q→r == ~𝑞 ∨ 𝑟. c- p→s == ~𝑝 ∨ 𝑠. d- ~𝑠.
e-Negated goal:- ~𝑟.
3- Applying Resolution theorem:-

𝑝∨𝑞 ~𝑞 ∨ 𝑟

p∨ 𝑟 ~𝑝 ∨ 𝑠

𝑟∨𝑠 ~𝑠

𝑟 ~𝑟


So the goal is proved and it will rain.

45
Prof. Dr. Ali Hussein Hasan
Strengths and weaknesses of propositional logic

• Propositional logic is easy to represent world knowledge which could be


required by an AI system.
• Propositional logic is simple to deal with and is declarative.
• Propositional logic permits conjunctive/disjunctive/partial/negative
information.
• Real world facts can be written as well-formed formulas (wffs) e.g.

Socrates is a man SOCRATESMAN

Kareem is a man KAREEMMAN

It is cold COLD

• Propositional logic has very limited expressive power e.g.

Search a candle in all locality shops has a clear meaning to search all shops in
the locality for candle. But propositional logic will require separate statement
for each shop.

• Propositions could be deceptive or extremely difficult to draw a


meaningful conclusion e.g. KAREEMMAN and AHMADMAN
produce totally different assertion.
• Propositional logic assumes world is all full of facts so constitute wffs
accordingly.
• Reasoning with propositional logic is difficult.

46
Prof. Dr. Ali Hussein Hasan
Predicate logic – First order logic FOL

Predicate logic (also called first order predicate logic) has a similar formalism
like propositional logic. However, the capability of reasoning and knowledge
representation using predicate logic is higher than propositional logic. The
language of first-order logic is built around objects and relations.

The primary difference between propositional and first-order logic is that, the
propositional logic assumes that there are facts that either hold or do not hold
in the world. Each fact can be in one of two states: true or false, and each model
assigns true or false to each proposition symbol. First-order logic assumes that
the world consists of objects with certain relations among them that do or do
not hold. So that When we look at the syntax of natural language, the most
obvious elements are nouns and noun phrases that refer to objects (squares,
pits, wumpuses) and verbs and verb phrases that refer to relations among
objects (is breezy, is adjacent to, shoots). Some of these relations are
functions—relations in which there is only one “value” for a given “input.” It
is easy to start listing examples of objects, relations, and functions:

• Objects: people, houses, numbers, theories, Ronald McDonald, colors,


baseball games, wars, centuries . . .

• Relations: these can be unary relations or properties such as red, round, bogus,
prime, multistoried . . ., or more general n-ary relations such as brother of,
bigger than, inside, part of, has color, occurred after, owns, comes between, . .
.
• Functions: father of, best friend, third inning of, one more than, beginning of
..

Ex:

“One plus two equals three.”

Objects: one, two, three, one plus two;

Relation: equals;

Function: plus.

(“One plus two” is a name for the object that is obtained by applying the
function “plus” to the objects “one” and “two.” “Three” is another name for
this object.)

47
Prof. Dr. Ali Hussein Hasan
“Squares neighboring the wumpus are smelly.”

Objects: wumpus, squares;

Property: smelly;

Relation: neighboring.

“Evil King John ruled England in 1200.”

Objects: John, England, 1200;

Relation: ruled;

Properties: evil, king.

FOL includes two more quantifiers: ∀ essential quantifier ( for all ). and ∃
existence quantifier ( there exist ).

To illustrate the use of the quantifiers, let us consider the following pieces of
knowledge.

Knowledge 1 : All boys like sweets.

Using predicate logic, we can write the above statement as

∀X ( Boy ( X ) → Likes (X , sweets)).

Knowledge 2 : Some boys like flying kites.

Using predicate logic, the above statement can be represented as


∃X ( Boy (X) → Likes (X, Flying-kites))

Alphabets of FOL

The alphabets of FOL are of the following types:

1. Constants: a, b, c
2. Variables: X, Y, Z
3. Functions: f, g, h
4. Operators: ∧,∨, ~ 𝑎𝑛𝑑 ⟶
5. Quantifiers: ∀ 𝑎𝑛𝑑 ∃
6. Predicate: P, Q, R

Definition : A term is defined recursively as being a constant,


variable or the result of application of a function to a term.

48
Prof. Dr. Ali Hussein Hasan
e.g., a, x, t(x), t(g(x)) are all terms.

To illustrate the difference between functions and predicates, we give their


formal definitions with examples.

Definition : Function denotes relations defined on a domain D. They


map n elements (n >0) to a single element of the domain. “father-of”, “age-of”
represent function symbols. An n-ary function is written as f(t1, t2,.., tn) where
ti s represent terms. A 0-ary function is a constant.

Definition : Predicate symbols denote relations or functional


mappings from the elements of a domain D to the values true or false. Capital
letters such as P,Q, MARRIED, EQUAL are used to represent predicates. P(t1,
t2, ..., tn) represents an n-ary predicate where ti are terms. A 0-ary predicate is
a proposition, that is a constant predicate.

Definition : The sentences of FOL are well-formed-formulas (WFF),


defined as follows:

1. If P (t1, t2, … , tn) is an n-ary predicate, then P is an atomic formula.


2. An atomic formula is a well-formed formula (WFF).
3. If P and Q are WFF then 𝑃 ∧ 𝑄, 𝑃 ∨ 𝑄, ~𝑃, 𝑃 ⟶ 𝑄 are all WFF.
Note that ∀X R (X) is also an WFF.
4. If P is a WFF and X is not a quantified variable in P, then P remains a
WFF even after quantification
e.g., ∀X P or ∃X P are WFF.

Example : Rewrite the following sentences in FOL.


1. Coconut-crunchy is a biscuit.
2. Mary is a child who takes coconut-crunchy.
3. John loves children who take biscuits.
4. John loves Mary.

The above statements can be represented in FOL using two quantifiers


X & Y.

1. Biscuit (coconut-crunchy)
2. Child (mary) ∧ Takes (mary, coconut-crunchy)
3. ∀X ((Child (X ) ∧ ∃Y (Biscuit (Y) ∧ Takes (X, Y) )) →Loves
(john, X)
4. Loves (john, mary)

49
Prof. Dr. Ali Hussein Hasan
Writing a Sentence into Clause Forms

Algorithm for representing a sentence into clauses

Step I: Elimination of if-then operator: Replace “→” operator by ~


& ∨ operator.

∀X (~(Child (X) ∧∃Y (Takes (X, Y) ∧ Biscuit (Y))) ∨(Loves (john, X)

Step II: Reduction of the scope of negation: Replace ~ sign by choosing any
of the following:

a) ~(P ∨ Q) = ~P ∧ ~Q
b) ~(P ∧ Q) = ~P ∨~Q
c) ~( ~P ) = P
d) ~( ∃X P) = ∀X ~ P
e) ~(∀X P) = ∃ X ~P

In the present context, we rewrite the sentence as

∀X (~Child (X) ∨~(∃Y (Takes (X, Y) ∧ Biscuit (Y))) ∨ (Loves (john, X ))


⇒∀X (~Child (X) ∨ ∀Y (~Takes (X, Y) ∨~Biscuit (Y)) ∨ Loves
(john, X).

Step III: Renaming the variables within the scope of quantifiers:

Rename ∃X by ∀Y when {∃X} is a subset / proper subset of {∀ X}. In


the present context, since X and Y are distinct, the above operation cannot
be carried out.

Step IV: Moving of quantifiers in the front of the expression: Bring all
quantifiers at the front of the expression.

Applying this on the example yields:

⇒∀X ∀Y ~Child (X) ∨~Takes (X,Y) ∨~Biscuit (Y) ∨ (Loves (john, X))

Step V: Replacing existential quantifier as Skolem function of essential


quantifiers:

When an existential quantifier (Y) precedes an essential quantifier


(X), replace Y as S (X), where S is the Skolem function. In this example,
since Y is not a subset of X, such a situation does not arise. Also the
essential quantifier is dropped from the sentence.

50
Prof. Dr. Ali Hussein Hasan
Step VI: Putting the resulting expression in conjunctive normal form
(CNF):

For example, if the original expression is in the form P ∨ (Q ∧ R),


then replace it by (P ∨ Q) ∧ ( P ∨ R).

In the present context, the resulting expression corresponding to


expression being in CNF, we need not do any operation at this step.

Step VII: Writing one clause per line: If the original expression is of the
following CNF, then rewrite each clause/ line, as illustrated below.

original expression:

( ~ P11 ∨ ~ P12 ...∨ ~ P1n ∨ Q11 ∨ Q12.... ∨ Q1m ) ∧


( ~ P21 ∨ ~ P22 ...∨ ~ P2n ∨ Q21 ∨ Q22 ... ∨ Q2m) ∧
....
( ~ Pt1 ∨ ~ Pt2 ... ∨ ~ Ptn ∨ Qt1 ∨ Qt2 .... ∨ Qtm).

After writing one clause per line, the resulting expressions become as follows.

P11, P12,..., P1n → Q11, Q12,... ,Q1m


P21, P22,..., P2n → Q21, Q22,..., Q2m
... … … … …. …. …
Pt1, Pt2...., Ptn → Qt1, Qt2,.…., Qtm

With reference to the above, we get the following line as the final expression.
Child (X), Takes (X, Y), Biscuit (Y) → Loves (john, X).

Ex:

∀X ( Loves (john, X) → Female (X) ) ∧∃X (~Loves (X, Brother-of (X)) ∧


Female (X)))

The clause forms for the above expression are:

a) Loves (john, X) → Female (X)

b) Loves (s(X), Brother-of (s (X))), Female (X) →_|_


where the meaning of the first clause is obvious, while the second clause
means that it is impossible that there exists a female X, who loves her
brother. The inverted T is called a Falsum operator, which is opposite to
Truam (T), meaning that the expression is true. The symbol s(X) denotes a
Skolem function of X, which may be read as some of X.

51
Prof. Dr. Ali Hussein Hasan
Ex:- Prove Loves (john, mary)

Sol:-

Let
A1 = Biscuit (coconut-crunchy)

A2 = Child (mary) ∧Takes (mary, coconut-crunchy)

A3 = ∀X (Child(X) ∧ ∃Y (Takes (X,Y) ∧ Biscuit (Y))) →


Loves (john, X)

and Th = Loves (john, mary) = A4 (say).

Expressions A1 and A4 are already in CNF. Expression A2 can be


converted into CNF by breaking it into two clauses:

Child (mary)

and

Takes (mary, coconut-crunchy).

Further, the CNF of expression A3 is

~Child (X) ∨~Takes (X,Y) ∨~Biscuit (Y) ∨(Loves (john, X))

Negate the goal: ~ Loves (john, mary) = ~ A4

52
Prof. Dr. Ali Hussein Hasan

So that the theorem holds good and John loves Mary.

Ex:-

1- Marcus was a man.


2- Marcus was a Pompeian.
3- All Pompeians were Romans.
4- Caesar was a ruler.
5- All Romains were either loyal to Caesar or hated him.
6- Everyone is loyal to someone.
7- People only try to assassinate rulers they are not loyal to.
8- Marcus tried to assassinate Caesar.

Sol:-

Wff:-

1- man(marcus).
2- pompeian(mrcus).

53
Prof. Dr. Ali Hussein Hasan
3- ∀X pompeian(X) → roman(X).
4- ruler(caesar).
5- ∀X romain(X) →loyalto(X,caesar) ∨ hate(X,caesar).
6- ∀X ∃Y loyalto(X,Y).
7- ∀X ∃Y prson(X) ∧ ruler (Y) ∧ tryassasinate(X,Y) →~loyalto(X,Y).
8- tryassasinate(marcus,caesar).

54
Prof. Dr. Ali Hussein Hasan
Problem solving by intelligent search

Problem solving requires two prime considerations:- first representation of the


problem by an appropriately organized state space and then testing the
existence of a well-defined goal state in that space. The state space is then
searched to find a solution to the problem. A state space essentially consists of
a set of nodes representing each state of the problem, arcs between nodes
representing the legal moves from one state to another, an initial state and a
goal state. Each state space takes the form of a tree or a graph.

The search problem is associated with two important issues; first “what to
search” and secondly “where to search”. The first one is generally referred to
as the “the key”, while the second one is termed “search space”.

Problem Representation

A number of factors need to be taken into consideration when developing a


state space representation. Factors that must be addressed are:

• What is the goal to be achieved?


• What are the legal moves or actions?
• What knowledge needs to be represented in the state description?
• Type of problem - There are basically three types of problems. Some
problems only need a representation, e.g. crossword puzzles. Other
problems require a yes or no response indicating whether a solution can
be found or not. Finally, the last type problem are those that require a
solution path as an output, e.g. mathematical theorems, Towers of Hanoi.
In these cases we know the goal state and we need to know how to attain
this state
• Best solution vs. Good enough solution - For some problems a good
enough solution is sufficient. For example, theorem proving , eight
squares. However, some problems require a best or optimal solution, e.g.
the traveling salesman problem.

Ex:- Towers of Hanoi

In a monastery in the deepest Tibet there are three crystal columns and 64
golden rings. The rings are different sizes and rest over the columns. At the
beginning of time all the rings rested on the leftmost column, and since than
the monks have toiled ceaselessly moving the rings one by one between the
columns. In moving the rings a larger ring must not be placed on a smaller ring.
Furthermore, only one ring at a time can be moved from one column to the

55
Prof. Dr. Ali Hussein Hasan
next. A simplified version of this problem which will consider involves only 2
or 3 rings instead of 64.

The legal moves in this state space involve moving one ring from one pole to
another, moving one ring at a time, and ensuring that a larger ring is not placed
on a smaller ring.

Ex:- 8-Puzzles

The 8-Puzzle involves moving the tiles on the board above into a particular
configuration. The black square on the board represents a space. The player
can move a tile into the space, freeing that position for another tile to be moved
into and so on.

1 4 3
7 6
5 8 2

For example, given the initial state above we may want the tiles to be moved
so that the following goal state may be attained.

1 8 3

2 4

7 6 5

56
Prof. Dr. Ali Hussein Hasan

Although a player moves the tiles around the board to change the configuration
of tiles. However, we will define the legal moves in terms of moving the space.
The space can be moved up, down, left and right.

57
Prof. Dr. Ali Hussein Hasan

58
Prof. Dr. Ali Hussein Hasan
Graphs versus Trees

If states in the solution space can be revisited more than once a directed graph
is used to represent the solution space. In a graph more than one move sequence
can be used to get from one state to another. Moves in a graph can be undone.
In a graph there is more than one path to a goal whereas in a tree a path to a
goal is more clearly distinguishable. A goal state may need to appear more than
once in a tree. Search algorithms for graphs have to cater for possible loops and
cycles in the graph. Trees may be more “efficient” for representing such
problems as loops and cycles do not have to be catered for. The entire tree or
graph will not be generated.

Notice that there is more than one path connecting two particular nodes in a
graph whereas this is not so in a tree.

Ex:- prove x+(y+z)=y+(x+z).

Given that

L+(M+N)=(L+M)+N ……. A

M+N=N+M…………..C

Sol:- 1- graph representation:

59
Prof. Dr. Ali Hussein Hasan

2- tree representation

Search methods:-

A- Systematic methods:- generate and test , in which expands the search


space and examines the existence of the goal in that space.
1- Depth first search:-

In depth-first search, we start with the root node and completely


explore the descendants of a node before exploring its siblings (and
siblings are explored in a left-to-right fashion).

60
Prof. Dr. Ali Hussein Hasan

Consider the following tree:-

Each node is not visited more than once. A depth first search of the this tree
produces: A, B, E, K, S, L ,T, F, M, C, G, N, H, O, P, U, D, I, Q, J, R.
Although in this example the tree was generated first and then a search of the
tree was conducted. However, often there is not enough space to generate the
entire tree representing state space and then search it. The algorithm presented
below conducts a depth first search and generates parts of the tree as it goes
along.

Depth First Search Algorithm

def: dfs (in Start, out State)

open = [Start];

closed = [];

61
Prof. Dr. Ali Hussein Hasan
State = failure;

while (open <> []) AND (State <> success)

begin
remove the leftmost state from open, call it X;

if X is the goal, then

State = success

else begin

generate children of X;

put X on closed

eliminate the children of X on open or closed

put remaining children on left end of open

end else

end while

return State;

end def

Example 1: Suppose that the letters A,B, etc represent states in a problem. The
following moves are legal:
A to B and C
B to D and E
C to F and G
D to I and J
I to K and L
Start state: A Goal state: E , J

Let us now conduct a depth first search of the space. Remember that tree will
not be generated beforehand but is generated as part of the search. The search
and hence generation of the tree will stop as soon as success is encountered or
the open list is empty.

62
Prof. Dr. Ali Hussein Hasan

Time Complexity
If we find the goal at the leftmost position at depth d, then the number of
nodes examined = (d +1). On the other hand, if we find the goal at the
extreme right at depth d, then the number of nodes examined include all the
nodes in the tree, which is

1+b+b2 +b3 +…+bd = (bd+1 -1) / (b-1)

So, the total number of nodes examined in an average case

= (d+1) /2 + (bd+1 -1) / 2(b-1)

≅ b( bd + d) / 2 (b -1)

This is the average case time complexity of the depth first search algorithm.
Since for large depth d, the depth first search requires quite a large
runtime, an alternative way to solve the problem is by controlling the depth
of the search tree. Such an algorithm, where the user mentions the initial.

63
Prof. Dr. Ali Hussein Hasan
2- Breadth first search:-

The breadth first search algorithm visits the nodes of the tree along its
breadth, starting from the level with depth 0 to the maximum depth. It can be
easily realized with a queue. For instance, consider the tree, given below
Here, the nodes in the tree are traversed following their ascending ordered
labels.

Procedure Breadth-first-search

Begin

i) Place the starting node in a queue;


ii) Repeat
Delete queue to get the front element;
If the front element of the queue = goal,
return success and stop;
Else do
Begin
insert the children of the front element,
if exist, in any order at the rear end of the queue;
End
Until the queue is empty;
End.

64
Prof. Dr. Ali Hussein Hasan
The breadth first search algorithm, presented above, rests on a simple
principle. If the current node is not the goal add the offspring of the
current in any order to the rear end of the queue and redefine the front
element of the queue as the current. The algorithm terminates, when the goal
is found.

n1

n2 n3 n4

n3 n4

n4 n5 n6

n5 n6 n7 n8

Time Complexity

For the sake of analysis, we consider a tree of equal branching factor from
each node = b and largest depth = d. Since the goal is not located within
depth (d-1), the number of false search [1], [2] is given by

1+b+b2 +b3 + … + bd-1 = (bd-1) / (b-1) , b>>1.

Further, the first state within the fringe nodes could be the goal. On the
other hand, the goal could be the last visited node in the tree. Thus, on an
average, the number of fringe nodes visited is given by
( 1+bd ) / 2.

Consequently, the total number of nodes visited in an average case becomes


(bd-1) / (b-1) + (1+bd ) / 2

≅bd (b+1) / 2(b-1).

Since the time complexity is proportional to the number of nodes visited,


therefore, the above expression gives a measure of time complexity.

65
Prof. Dr. Ali Hussein Hasan
Differences Between Depth First and Breadth First

Breadth first is guaranteed to find the shortest path from the start to the goal.
DFS may get “lost” in search and hence a depth-bound may have to be imposed
on a depth first search. BFS has a bad branching factor which can lead to
combinatorial explosion. The solution path found by the DFS may be long and
not optimal. DFS more efficient for search spaces with many branches, i.e. the
branching factor is high.

The following factors need to be taken into consideration when deciding which
search to use:

• The importance of finding the shortest path to the goal.

• The branching of the state space.

• The available time and resources.

• The average length of paths to a goal node.

• All solutions or only the first solution.

B- Optimal search method:- The ‘generate and test’ type of search


algorithms presented above only expands the search space and examines
the existence of the goal in that space. An alternative approach to solve
the search problems is to employ a function f(x) that would give an
estimate of the measure of distance of the goal from node x. After f(x) is
evaluated at the possible initial nodes x, the nodes are sorted in
ascending order of their functional values and pushed into a stack in the
ascending order of their ‘f’ values. So, the stack-top element has the least
f value. It is now popped out and compared with the goal. If the stack-
top element is not the goal, then it is expanded and f is measured for each
of its children. They are now sorted according to their ascending order
of the functional values and then pushed into the stack. If the stack-top
element is the goal, the algorithm exits; otherwise the process is
continued until the stack becomes empty. Pushing the sorted nodes into
the stack adds a depth first flavor to the present algorithm.

1- Hill Climbing method

66
Prof. Dr. Ali Hussein Hasan
The hill climbing algorithm is formally presented below.

Procedure Hill-Climbing
Begin
1. Identify possible starting states and measure the distance (f) of their
closeness with the goal node; Push them in a stack according to the
ascending order of their f ;
2. Repeat
Pop stack to get the stack-top element;
If the stack-top element is the goal, announce it and exit
Else push its children into the stack in the ascending order of their
f
values;
Until the stack is empty;

End.

.‫ يتم حساب الكلفة بتجميع كلف المسار وصوال" ألى الهدف‬-:‫البحث التجميعي أو تسلق التلة‬

Example: initial state S, goal states G1 & G2

SB=1. SA=4. BF=3. BE=2. FG2=1. FI=2. EG1=3. EH=4. AD=2. AC=1.

Sol:-

4 1

A B

1 2 2 3

C D E F

4 3 2 1
3
H G1 I G2

S(0)

B(1) A(4)

67
Prof. Dr. Ali Hussein Hasan
E(3) F(4) A(4)

A(4) F(4) G1(6) H(7) G1 FOUNDED!

F(4) C(5) D(6) H(7)

C(5) G2(5) I(6) D(6) H(7) G2 FOUNDED!

The hill climbing algorithm too is not free from shortcomings. One
common problem is trapping at local maxima at a foothill. When trapped at
local maxima, the measure of f at all possible next legal states yield less
promising values than the current state. A second drawback of the hill
climbing is reaching a plateau. Once a state on a plateau is reached, all legal
next states will also lie on this surface, making the search ineffective.

2- Best first method

Procedure Best-First-Search

.‫ كلفة كل عقدة هو كلفة الوصول اليها من العقدة السابقة لها فقط‬-:"‫البحث اللحظي أو أالقل أوال‬

Begin
1. Identify possible starting states and measure the distance (f) of their
closeness with the goal; Put them in a list L;

2. While L is not empty do

Begin
a) Identify the node n from L that has the minimum f; If there
exist more than one node with minimum f, select any one of them
(say, n) arbitrarily;

b) If n is the goal

Then return n along with the path from the starting node,
and exit;

Else remove n from L and add all the children of n to the list L,
with their labeled paths from the starting node;

End While;

End

68
Prof. Dr. Ali Hussein Hasan
Ex: repeat last example

S(0)

B(1) A(4)

E(2) F(3) A(4)

F(3) G1(3) A(4) H(4)

G2(1) I(2) G1(3) A(4) H(4)

69
Prof. Dr. Ali Hussein Hasan
Knowledge Representation structures

AI programs depend heavily on the representation of the type of knowledge


that people use to solve complex tasks. AI use structures called knowledge
structures to represent objects, facts, rules, relationships, and procedures.

The main function of the knowledge structure is to provide the needing


expertise and information so that a program can operate in an intelligent
manner.

The types of knowledge that need to be represented in AI system:

1- Objects: these are facts about real time entities in our world domain.
Example, ‘Guitar has string, trumpets are brass instrument’.
2- Events: these are actions that occur in our world. Example, ‘Lionel
Andrés "Leo" Messi is an Argentine professional footballer who plays
as a forward for Spanish club FC Barcelona and the Argentina national
team’. Or ‘Messi played as a forward in FC Barcelona club’.
3- Performance: it represents a behavior. Example, ‘playing the guitar’ ; it
involves knowledge about how to do it.
4- Meta-knowledge: it is the knowledge about knowledge. This knowledge
can read straight signs along the way to find out where it is. For example,
‘Jain’s robot who plan’s a trip’.
5- Facts: these are what truth we need to represent about the real world.
This can be regarded as the knowledge level for a domain.

Types of knowledge representation

There are several Types of knowledge representation structured models. Such


models have immense significance over the non-structured models by the
following counts.

• Firstly, the knowledge base can be easily represented by modular


fashion, thus ensuring a compartmentalization of the related chunks of
knowledge for efficient access.
• Secondly, explanation tracing in knowledge based systems becomes
easier with structured models.
• Thirdly, a structured model offers provision for representing monotonic,
non-monotonic and default logic on a common platform.

70
Prof. Dr. Ali Hussein Hasan
• Fourthly, the fragments of a piece of knowledge can be accessed
concurrently by many modules, thereby providing the reasoning systems
a
scope for sharing of resources. Such sharing of resources call for
minimal
structure and consequently less hardware / software devices for
realization of the knowledge bases.
• Finally, many modules of the structural models can be
made active concurrently, thus providing a scope for massive parallelism
in the reasoning process. The time efficiency of the inference engines
thus can be improved with structured models.

Types:-

1- Production Rules (PR).


2- Semantic Networks (SN).
3- Conceptual Graphs (CG).
4- Frames.

1- Production Rules PR

Knowledge is represented with rules, these rules are expressed in the standard
IF-THEN :-

IF condition THEN action.

IF premise THEN conclusion.

IF proposition p1 and proposition p2 are true THEN proposition p3 is true.

The PR consist of two parts : conditions and a conclusions.

Consider the structure of the PR:

𝑃𝑅: 𝑝1(𝑥) ∧ 𝑝2(𝑦) ∧ ⋯ ⟶ 𝑞1(𝑥) ∨ 𝑞2(𝑦) ∨ ⋯

where pi & qj are predicates,

x,y,… variables.

The LHS of PR is called antecedent/conditional part and the RHS is called the
consequent/conclusion part.

Generally; PRs may be expressed in the following :-

71
Prof. Dr. Ali Hussein Hasan
1- Rule-Number :- IF condition THEN conclusion.
2- Rule-Number:- conclusion IF condition.
3- In PROLOG format:- rule(rule-no.,rule-head,rule-body).

Examples:

1-

Rule: IF Has-fever (Patient) AND Has-rash (Patient) AND Has-high-body-


ache (Patient) THEN Bears-Typhoid (Patient).

2-

R1: IF patient’s temperature is high

AND

Patient’s throat is irritated.

THEN patient has a Cold.

3-

R2: IF patient’s temperature is over 38C

THEN patient’s temperature is high.

4-

R1:- IF person age above 21.

AND person wife Nil.

AND person sex male.

THEN person eligible for marriage.

R2:- IF Rami age 25.

AND Rami wife Nil.

AND Rami sex male.

THEN Rami eligible for marriage.

5-

Water Jug Problem:

72
Prof. Dr. Ali Hussein Hasan
You are given two jugs, a 4-gallon one and a 3-gallon one. Neither has any
measuring mark on it. How can you get exactly 2 gallons of water into the
4-gallon jug.

Solution:

The state space for this problem can be described as the set of ordered pairs
of integers (x,y);

Where,

X represents the quantity of water in the 4-gallon jug X= 0,1,2,3,4

Y represents the quantity of water in 3-gallon jug Y=0,1,2,3

Start State: (0,0);

Goal State: (2,0)

Generate production rules for the water jug problem

Production Rules:
Rule State Process
1 (X,Y | X<4) (4,Y)
{Fill 4-gallon jug}
2 (X,Y |Y<3) (X,3)
{Fill 3-gallon jug}
3 (X,Y |X>0) (0,Y)
{Empty 4-gallon jug}
4 (X,Y | Y>0) (X,0)
{Empty 3-gallon jug}
5 (X,Y | X+Y>=4 ^ Y>0) (4,Y-(4-X))
{Pour water from 3-gallon jug into 4-gallon
jug until 4-gallon jug is full}
6 (X,Y | X+Y>=3 ^ X>0) (X-(3-Y),3)
{Pour water from 4-gallon jug into 3-gallon
jug until 3-gallon jug is full}
7 (X,Y | X+Y<=4 ^ Y>0) (X+Y,0)
{Pour all water from 3-gallon jug into 4-
gallon jug}
8 (X,Y | X+Y <=3^ X>0) (0,X+Y)

73
Prof. Dr. Ali Hussein Hasan
{Pour all water from 4-gallon jug into 3-
gallon jug}
9 (0,2) (2,0)
{Pour 2 gallon water from 3 gallon jug into
4 gallon jug}

Initialization:
Start State: (0,0)
Apply Rule 2:
(X,Y | Y<3) -> (X,3)
{Fill 3-gallon jug}
Now the state is (X,3)

Iteration 1:
Current State: (X,3)
Apply Rule 7:
(X,Y | X+Y<=4^Y>0) (X+Y,0)
{Pour all water from 3-gallon jug into 4-gallon
jug}
Now the state is (3,0)

Iteration 2:
Current State : (3,0)
Apply Rule 2:
(X,Y | Y<3) -> (3,3)
{Fill 3-gallon jug}
Now the state is (3,3)

Iteration 3:
Current State:(3,3)
Apply Rule 5:
(X,Y | X+Y>=4 ^Y>0) (4,Y-(4-X))
{Pour water from 3-gallon jug into 4-gallon
jug until 4-gallon jug is full}
Now the state is (4,2)

Iteration 4:

74
Prof. Dr. Ali Hussein Hasan
Current State : (4,2)
Apply Rule 3:
(X,Y | X>0) (0,Y)
{Empty 4-gallon jug}
Now state is (0,2)

Iteration 5:
Current State : (0,2)
Apply Rule 9:
(0,2) (2,0)
{Pour 2 gallon water from 3 gallon jug into 4
gallon jug}
Now the state is (2,0)

Goal Achieved.

6-

A farmer wants to transfer his three belongings a wolf, a goat, and a cabbage
by a boat from left bank of a river to its right bank. The boat can carry at

75
Prof. Dr. Ali Hussein Hasan
most two items including the farmer. If unwanted, the wolf may eat up the
goat and the goat may eat up the cabbage.

Sol. Let f=farmer; g=goat; w=wolf; c=cabbage and || river.

The illegal states:

1- (w,g||f,c)
2- (g,c||f,w)
3- (f,w||g,c)
4- (f,c||w,g)

Initial state: (f,g,w,c||nil).

Final state: (nil||f,g,w,c).

R1: (f,g,w,c||nil)→(w,c||f,g).

R2: (w,c||f,g)→(f,w,c||g).

R3: (f,w,c||g)→(c||f,w,g).

R4: (c||f,w,g)→(f,g,c||w).

R5: (f,g,c||w)→(g||f,w.c).

R6: (g||f,w,c)→(f,g||w,c).

R7: (f,g||w,c)→(ni||f,g,w,c).

R8: (f,w,c||g)→(w||f,g,c).

R9: (w||f,g,c)→(f,g,w||c).

R10: (f,g,w||c)→(g||f,w,c).

R11: (g||f,w,c)→(f,g||w,c).

R12: (f,g||w,c)→(nil||f,g,w,c).

Forward Reasoning: Given the starting state ( F, G, W, C | | Nil) and the


goal state (Nil | | F, G, W, C), one may expand the state-space, starting with
(F,G,W,C | | Nil) by the supplied knowledge base, as follows:

76
Prof. Dr. Ali Hussein Hasan

Backward Reasoning: The backward reasoning scheme can also be


invoked for the problem. The reasoning starts with the goal and identifies a rule
whose right-hand side contains the goal. It then generates the left side of
the rule in a backward manner. The resulting antecedents of the rules are called
sub-goals. The sub-goals are again searched among the consequent part of the
rules and on a successful match the antecedent parts of the rule are generated
as the new sub-goals. The process is thus continued until the starting node is
obtained.

77
Prof. Dr. Ali Hussein Hasan

2- Semantic Networks (SN).

A semantic network or net is a graph notation for representing knowledge in


patterns of interconnected nodes and arcs. Semantic nets originally used in
understanding natural language, where the semantics (meaning) of the
associated words in a sentence was extracted by employing such nets.

A semantic net consists of two elementary tuples:- nodes and arcs.

A node symbolizes event, object, concept, or situation. An arc represent the


relationship between two objects.

Most relationships are deduced from the given sentences. Some standard
relationships used in semantic nets :-

isa

a kind – of

78
Prof. Dr. Ali Hussein Hasan
instance – of

member – of

has – parts

has

can

examples:-

1- Describe the binary predicate like(X,Y).

like
X Y

2- Jim is a boy.

Instance - of
Jim boy

3- John gave the bagger 10$.

gave

Instance - of
Agent
john event 10$
object
Beneficiary

bagger

79
Prof. Dr. Ali Hussein Hasan
4- Tweety is a yellow bird that has wings and can fly

can
fly

A kind of
bird Tweety yellow
color

has
wings

5- ∀(𝑥)∃(𝑦)(𝑐ℎ𝑖𝑙𝑑 (𝑥 ) ∧ 𝑠𝑤𝑒𝑒𝑡 (𝑦) ⟶ 𝑙𝑖𝑘𝑒(𝑥, 𝑦))

Consequent:- like y
x

Instance – of Instance – of

child sweet
Antecedent

80
Prof. Dr. Ali Hussein Hasan
6-
i- Green – bird (parrot).
ii- Singer – bird(cuckoo).
iii- Bird(green – bird).
iv- Bird(singer – bird).
v- Aves(bird).
vi- ~singer – bird(parrot).
vii- ~green – bird(cuckoo).

Aves
Is a

Is a bird

Green-bird
Is a Is a

~Is a
parrot Is a
Singer-bird
cuckoo

~Is a

7- Represent the knowledge about the birds

81
Prof. Dr. Ali Hussein Hasan

sing
fly Can can
bird
Has isa parrot is green
wings
has isa
Can isa
feathers is large

Animal ostrich can not


fly

Can has can isa


breath skin move mammal
s

8- Represent the knowledge about airplanes:-

82
Prof. Dr. Ali Hussein Hasan

aircraft

isa

airplane

isa isa isa


jet Single engine Twin engine

has a has a has a has a

Jet engine wing propeller


propeller
is
fast

3- Conceptual graphs CG

A conceptual graph (CG) is a graph representation for logic based on the


semantic networks of artificial intelligence.

CG are based upon the following general form:

This may be read as: “ The relation of a Concept-1 is a Concept-2”. The


direction of the arrows assists the direction of the reading. If the arrows were
pointing the other way, then the reading would be the same except that
Concept-1 and Concept-2 would exchange places (i.e. “The relation of
Concept-2 is a Concept-1”).

As an alternative to the above ‘display’ form, the graphs may be written in the
following ‘linear’ text-based form:

[Concept_1] -> (relation) -> [Concept_2].

The full stop '.' signals the end of a particular graph. Consider the following

83
Prof. Dr. Ali Hussein Hasan
example:

[Funding_Request] -> (initiator) -> [Employee].

This example will form a part of an illustrative case study about requests for
funding new business projects in the fictitious enterprise ‘P-H Co.’. The
example graph reads as “The initiator of a funding request is an employee”.

Funding request initiator Employee

The nodes are either:- concept or conceptual relations.

a- Concept nodes represent either :- concrete objects or abstract objects.

cat, book, etc ‫مفاهيم مدركة بالحواس اي تكون لها صورة في الخيال مثل‬

love, beauty, ‫مفاهيم تجريدية اي ليس لها صورة في الخيال مثل‬

b- Conceptual relations nodes: represent relations between concepts, can


only have arcs to conceptual relation, and vice versa.

*we represent concepts as boxes and conceptual relations as ellipses.

*Conceptual relation nodes indicate a relation involving one or more concept.

Ex:- suppose that you have the relations fly, color, parent.

bird
fly

object Color name


color

father
child
parent

mother

84
Prof. Dr. Ali Hussein Hasan

Ex:

1- a dog has a color of brown

dog brown
color

2- Mary gave John the book

mary Event: gave


agent object

john book
Recipient

3- The dog named Emma is brown.

*each concept box is labeled with the names of the type and the individual.
The type and individual labels are separated by a colon “ : “.

dog:emma brown
color

*in CG; every concept is a unique individual of a particular types. CG also let
us indicate a specific but unnamed individuals. A unique taken called a marker
is written as a no. preceded by a #. Markers are different from names in that
they are unique: individuals may have one name, many names or no name at
all, but they have exactly one marker.

Ex:

1- A particular but unnamed dog is brown.

Dog #1 brown
color

85
Prof. Dr. Ali Hussein Hasan
2- A dog named Emma is brown.

Dog #1 brown
color

emma
name

3- A person with three names: Hoby, Lil, Nancy.

name Person #22 name

name
hoby nancy

lil

4- The dog scratches it ear with its paw.

Dog: x name Event: scratch object ear

instrument Part of

paw Part of Dog:x

5- The dog Emma is a big and hungry

86
Prof. Dr. Ali Hussein Hasan

name emma

Dog #1 size big

state hungry

6- There are no pink dogs.

Dog #1 pink
color
neg

7- Tom believes that Jaime like pizza.

Person: Tom Event: believe


agent object

Proposition:

Person: Jaime Event: like


agent object

pizza

87
Prof. Dr. Ali Hussein Hasan
8- Tom believes that Jaime do not like pizza.
Same as 7 except add negative to the proposition.
9- John gave Tom an ice cream can.

Person: John Event: gave


agent object

Person: Tom Ice cream


recipient

10- Paul cat down the tree with axe.

Person: Paul Event: cat down


agent object

axe instrument tree

Changing the conceptual graphs ‘g’ into a predicate calculus


expression:

1- Assign a unique variable x1,x2,….,xn to the n generic concept in ‘g’.


2- Assign a unique constant to each individual concept in ‘g’.
3- Represent each concept node by a unary predicate whose name is the
same as type of that node and whose argument is variable or constant
assigned to that node.
4- Represent each n-ary conceptual relation in ‘g’ as an n-ary predicate
whose name is the same as the name of the relation. Let each argument
of the predicate be variable or constant assigned to the corresponding
concept node like to that relation.
5- Take the conjunction of all atomic sentences formed under 3 & 4. This
is the body of the predicate calculus expression. All variables in the
expression are existentially qualified.

88
Prof. Dr. Ali Hussein Hasan
Examples :1-

bird ∃(𝑥)(𝑏𝑖𝑟𝑑(𝑥) ∧ 𝑓𝑙𝑦(𝑥))


fly

dog Brown
color

∃(𝑥)∃(𝑦){𝑑𝑜𝑔(𝑥) ∧ 𝑐𝑜𝑙𝑜𝑟(𝑥, 𝑦) ∧ 𝑏𝑟𝑜𝑤𝑛(𝑦)}

2-

dog:emma brown
color

∃(𝑥){𝑑𝑜𝑔(𝑒𝑚𝑚𝑎) ∧ 𝑐𝑜𝑙𝑜𝑟(𝑒𝑚𝑚𝑎, 𝑥) ∧ 𝑏𝑟𝑜𝑤𝑛(𝑥)}

3-

Dog pink
color
neg

∃(𝑥)∃(𝑦 ){𝑑𝑜𝑔(𝑥) ∧ 𝑐𝑜𝑙𝑜𝑟(𝑥, 𝑦) ∧ 𝑝𝑖𝑛𝑘(𝑦)}

Using ~ yields

∀(𝑥)∀(𝑦){~𝑑𝑜𝑔(𝑥) ∨ ~𝑐𝑜𝑙𝑜𝑟(𝑥, 𝑦) ∨ ~𝑝𝑖𝑛𝑘(𝑦)}

4-

89
Prof. Dr. Ali Hussein Hasan

father
child
parent

mother

∃(𝑥)∃(𝑦)∃(𝑧){𝑐ℎ𝑖𝑙𝑑(𝑥) ∧ 𝑝𝑎𝑟𝑒𝑛𝑡(𝑥, 𝑦, 𝑧) ∧ 𝑓𝑎𝑡ℎ𝑒𝑟(𝑥, 𝑦) ∧ 𝑚𝑜𝑡ℎ𝑒𝑟(𝑥, 𝑧)}

90
Prof. Dr. Ali Hussein Hasan
4- Knowledge representation by Frames

A frame is defined as a structure that contains a number of slots, where the


attributes of the frame are sorted. Usually the slots in a frame are filled with
values, but it may contain a frame as well. Two important issues in a frame
are i) containment and ii) specialization.

Containment: It means that a slot in the frame contains another frame. For
example, the ‘seat arrangement’ slot of frame B contains the default frame C
and the ‘preferential arrangement’ slot of frame C contains the default frame
D. It is to be noted that default containment is denoted by ---> . A
solid arrow (→) , on the other hand, corresponds to specialization of frame,
presented below.

Specialization: The frame B, describing an examination hall of Sumer


University, is a specialization of the frame A, representing a generic
examination hall. Here, the frame B has some specialized properties, where it
may override the elementary properties of frame A, but otherwise it will
maintain the property of frame A. For instance, the slot of question paper is
inherited in frame B from A, while the slot of answer scripts is specialized in
B and is thus different from A.

Example: A frame describing an examination hall.

A B C D
Exam. Hall of
Examination-Hall Sumer Univ .
Seat arrangement Preferential seat
Seat arrang.: of Computer coll
Seat-Arrangement arrangement of
Invigi : at least 2/ at Sumer univers. Computer dept. of
room. No. of candidate Sumer university
Invigilators
Examinee : / bench : 2 Euclidean
Examinees- 2 group of Candidate in the Distance between
Question
examinees/room . same bench candidate of same
Question paper : 1
Paper : 1Qu/ should be :of group : to be
Question /
Candidate Candidate. different class/ maximized .
Answer script: year.
Answer 1 answer Preferential
Scripts: Script/ Candi. / hall Arrangement.
1script/candidate

*note:- frame A represent a generic examination hall.

Frame B is specialization of frame B.

The ‘→’ represent the specialization of frame.

91
Prof. Dr. Ali Hussein Hasan
The ‘--- >’ represent the containment.

Example: description of airplane.

Airplane

Color
Length
Engine type
Wing spam
Speed
Weight

Inheritance in Tangled Frames

In figure below a hierarchical frame, where a node ‘Tweety’ has


more than one parent. Such a directed acyclic structure is called a tangled
‫ معقد أو متشابك‬hierarchy. The most important issue for tangled hierarchy is how
to inherit the features of parents.

92
Prof. Dr. Ali Hussein Hasan
Expert Systems

93

You might also like