0% found this document useful (0 votes)
127 views127 pages

Comprehensive Guide to Artificial Intelligence

The document is a comprehensive guide to modern artificial intelligence, covering topics such as the introduction to AI, rational agents, problem solving, and knowledge representation. It discusses the limitations of human intelligence, the need for AI, and various types of AI systems and applications. Additionally, it explores the history, present state, and philosophical aspects of AI.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
127 views127 pages

Comprehensive Guide to Artificial Intelligence

The document is a comprehensive guide to modern artificial intelligence, covering topics such as the introduction to AI, rational agents, problem solving, and knowledge representation. It discusses the limitations of human intelligence, the need for AI, and various types of AI systems and applications. Additionally, it explores the history, present state, and philosophical aspects of AI.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Artificial Intelligence

A Comprehensive Guide to Modern AI

Amar Jukuntla
Contents
1 Introduction to Artificial Intelligence 1
1.1 Human Intelligence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Limitations of Human Intelligence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 Need for Artificial Intelligence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.4 Philosophy of AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.5 Defining AI: Thinking vs. Acting, Humanly vs. Rationally . . . . . . . . . . . . . . . . 2
1.6 History of AI in NASA and DARPA (2000s) . . . . . . . . . . . . . . . . . . . . . . . . 3
1.6.1 NASA’s AI Contributions in the 2000s . . . . . . . . . . . . . . . . . . . . . . . 3
1.6.2 DARPA’s AI Contributions in the 2000s . . . . . . . . . . . . . . . . . . . . . . 3
1.7 Brief History of Artificial Intelligence . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.8 Present State of AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.9 Types of AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.10 Examples of AI Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.11 AI Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.12 Examples of AI Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

2 Rational Agents and Environments 10


2.1 Rationality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.2 Rationality Examples in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.3 Agent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.4 Autonomy in AI Agents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.4.1 Degrees of Autonomy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.4.2 Examples of Autonomous Agents . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.4.3 Importance of Autonomy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.4.4 Agent Example: Robotic Vacuum Cleaner . . . . . . . . . . . . . . . . . . . . . 13
2.5 Types of Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.6 Types of Agents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.6.1 Simple Reflex Agent Architecture . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.6.2 Model-Based Agent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.6.3 Goal-Based Agent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.6.4 Utility-Based Agent Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.6.5 Learning Agent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.7 Summary of AI Agents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

3 Problem Solving 24
3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

i
CONTENTS ii

3.2 What is a State Space Problem? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24


3.3 Uninformed Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.3.1 Breadth-First Search (BFS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.3.2 Uniform Cost Search (UCS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.3.3 Depth-First Search (DFS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.3.4 Depth-Limited Search (DLS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
3.3.5 Iterative Deepening Search (IDS) . . . . . . . . . . . . . . . . . . . . . . . . . . 43
3.3.6 Bidirectional Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
3.4 Informed Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
3.4.1 Greedy Best First Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.4.2 A* Search Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.4.3 AO* Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
3.5 Depth-First Branch and Bound (DFBnB) . . . . . . . . . . . . . . . . . . . . . . . . . . 56
3.6 Game Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
3.6.1 Hill Climbing Algorithm in AI . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
3.6.2 Minimax Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
3.6.3 Alpha-Beta Pruning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
3.7 Problem Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68

4 Knowledge Representation 70
4.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
4.2 The Role of Knowledge Representation in AI . . . . . . . . . . . . . . . . . . . . . . . 70
4.3 Knowledge vs. Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
4.4 Importance of Logic in Reasoning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
4.5 Types of Knowledge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
4.6 Characteristics of a Good Knowledge Representation System . . . . . . . . . . . . . . . 73
4.7 Knowledge Representation Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
4.7.1 Logic-Based Knowledge Representation . . . . . . . . . . . . . . . . . . . . . . 75
4.7.2 Semantic Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
4.7.3 Frames and Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
4.7.4 Production Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
4.8 Logic in Knowledge Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
4.8.1 Propositional Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
4.8.2 First-Order Logic (FOL) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
4.8.3 Other Logics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
4.9 Inference Mechanisms in KR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
4.10 Practical Applications and Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
4.11 Challenges and Future Directions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

5 Propositional Logic 81
5.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
5.2 Syntax and Semantics of Propositional Logic . . . . . . . . . . . . . . . . . . . . . . . 81
CONTENTS iii

5.2.1 Syntax of Propositional Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . 81


5.2.2 Semantics of Propositional Logic . . . . . . . . . . . . . . . . . . . . . . . . . . 82
5.2.3 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
5.3 Inference in Propositional Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
5.3.1 Entailment, Validity, and Satisfiability . . . . . . . . . . . . . . . . . . . . . . . 83
5.3.2 Rules of Inference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
5.4 Resolution Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
5.4.1 Conjunctive Normal Form (CNF) . . . . . . . . . . . . . . . . . . . . . . . . . 85
5.4.2 Resolution Principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
5.5 Forward Chaining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
5.6 Backward Chaining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

6 First Order Logic 90


6.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
6.2 Syntax and Semantics First-Order Logic . . . . . . . . . . . . . . . . . . . . . . . . . . 90
6.2.1 Components of FOL Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
6.2.2 Semantics of First-Order Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
6.3 Reasoning Patterns in First-Order Logic . . . . . . . . . . . . . . . . . . . . . . . . . . 92
6.4 Resolution in First-Order Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
6.5 Forward Chaining in First-Order Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
6.6 Backward Chaining in First-Order Logic . . . . . . . . . . . . . . . . . . . . . . . . . . 101

7 Planning 105
7.1 Planning Agent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
7.2 What is Planning? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
7.3 Why Planning? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
7.3.1 Key Characteristics of Planning . . . . . . . . . . . . . . . . . . . . . . . . . . 107
7.3.2 Application Areas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
7.4 Planning Problem Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
7.5 The Language of Planning Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
7.5.1 Representation of States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
7.5.2 Representation of Goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
7.5.3 Representation of Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
7.6 Example: Shopping Planning Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
7.6.1 Solution Outline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
7.6.2 Operator Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
7.7 Types of Planning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
7.7.1 Classical Planning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
7.7.2 Non-Classical Planning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
7.8 Planning Approaches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
7.8.1 State Space Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
7.8.2 Partial Order Planning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
CONTENTS iv

7.8.3 Planning Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114


7.9 Representation in Planning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
7.9.1 Representation of States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
7.9.2 Representation of Goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
7.9.3 Representation of Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

8 Forms of learning 117


8.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
8.2 Supervised Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
8.2.1 How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
8.2.2 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
8.3 Unsupervised Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
8.3.1 How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
8.3.2 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
8.4 Semi-Supervised Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
8.4.1 How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
8.4.2 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
8.5 Reinforcement Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
8.5.1 How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
8.5.2 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Chapter 1
Introduction to Artificial Intelligence

1.1 Human Intelligence


Human intelligence encompasses the cognitive abilities that enable individuals to learn, reason, solve
problems, and adapt to new situations. It includes perception, memory, language processing, and
decision-making, rooted in the complex neural structures of the brain. For example, humans excel at
pattern recognition, such as identifying faces in a crowd, and can generalize knowledge across contexts,
like applying mathematical concepts to real-world problems. However, human intelligence is not without
flaws, which we explore next.

1.2 Limitations of Human Intelligence


Despite its strengths, human intelligence has significant limitations:

• Cognitive Biases: Humans are prone to biases, such as confirmation bias, where they favor
information aligning with existing beliefs. For instance, a person might ignore scientific evidence
contradicting their views on a topic.

• Limited Processing Capacity: The human brain processes information slowly compared to
computers. For example, calculating the square root of a large number mentally is time-consuming
and error-prone.

• Memory Constraints: Humans often forget details or misremember events, unlike computers with
precise data storage. A student might struggle to recall specific historical dates during an exam.

• Fatigue and Emotional Influence: Human performance degrades with tiredness or emotional
stress. A tired surgeon may make errors during a complex procedure.

These limitations highlight the need for systems that can augment or surpass human capabilities in specific
domains.

1.3 Need for Artificial Intelligence


Artificial Intelligence (AI) addresses the limitations of human intelligence by providing systems that can
process vast datasets, perform complex computations, and operate consistently without fatigue. The need
for AI arises in scenarios requiring:

1
CHAPTER 1. INTRODUCTION TO ARTIFICIAL INTELLIGENCE 2

• Scalability: AI can analyze millions of data points, such as medical records, in seconds, far beyond
human capability.

• Precision: AI systems, like those used in autonomous vehicles, make split-second decisions based
on sensor data, reducing human error.

• Automation: Repetitive tasks, such as assembly line quality control, benefit from AI’s ability to
work tirelessly.

• Exploration: AI enables discoveries in fields like astronomy, where algorithms identify patterns in
star data that humans might miss.

For example, IBM’s Watson can diagnose diseases by analyzing patient data against thousands of medical
studies, assisting doctors in making informed decisions.

1.4 Philosophy of AI
The philosophy of AI explores fundamental questions about intelligence, consciousness, and ethics.
Drawing from Russell and Norvig, AI can be viewed through four perspectives: systems that think
like humans, act like humans, think rationally, or act rationally. Philosophers like John Searle question
whether AI can truly understand (e.g., the Chinese Room argument), while others, like Russell, emphasize
designing AI that aligns with human values. Ethical concerns include ensuring AI systems are fair and
transparent. For instance, biased AI in hiring algorithms could unfairly exclude candidates based on
flawed training data.

What is Artificial Intelligence?


Artificial Intelligence (AI) is the branch of computer science that is concerned with the design and
development of systems that can perform tasks that normally require human intelligence. These
tasks include learning, reasoning, problem-solving, perception, and natural language understanding.
In simple words, AI aims to create machines that can think, learn, and act intelligently.

1.5 Defining AI: Thinking vs. Acting, Humanly vs. Rationally


Artificial Intelligence can be defined through four approaches, as outlined by Russell and Norvig:

• Thinking Humanly: AI systems that mimic human thought processes, such as cognitive models or
neural networks. For example, early AI like ELIZA simulated human-like conversation, though it
lacked true understanding.

• Acting Humanly: AI systems that perform tasks indistinguishable from humans, as tested by the
Turing Test. For instance, modern chatbots like Siri aim to converse naturally, passing as human in
limited contexts.
CHAPTER 1. INTRODUCTION TO ARTIFICIAL INTELLIGENCE 3

• Thinking Rationally: AI systems that use logical reasoning to solve problems, such as expert
systems. For example, MYCIN diagnosed infections using rule-based logic, achieving accuracy
comparable to human experts.

• Acting Rationally: AI systems that act to achieve goals efficiently, regardless of human-like thought.
Autonomous vehicles, for instance, navigate roads by optimizing safety and efficiency, not by
mimicking human reasoning.

These distinctions guide AI development, balancing human-like behavior with rational efficiency. The
following diagram illustrates these approaches:

AI Definitions

Thinking Humanly Acting Humanly Thinking Rationally

Acting Rationally

Figure 1.1: Four Approaches to Defining AI

1.6 History of AI in NASA and DARPA (2000s)


During the 2000s, NASA and DARPA significantly advanced AI applications, leveraging their respective
missions in space exploration and national security.

1.6.1 NASA’s AI Contributions in the 2000s


NASA utilized AI to enhance autonomous systems for space exploration. In 2003, NASA’s Spirit and
Opportunity rovers landed on Mars, navigating the planet’s surface without human intervention. These
rovers used AI-based path planning and obstacle avoidance algorithms, drawing on techniques like
probabilistic reasoning and machine vision to analyze terrain and select safe routes. Another key project
was the Remote Agent experiment on the Deep Space 1 mission (early 2000s), which demonstrated
autonomous spacecraft operations. The Remote Agent used AI components like planning, scheduling,
and model-based diagnosis to manage spacecraft systems, detect faults, and execute recovery actions,
reducing reliance on ground control.

1.6.2 DARPA’s AI Contributions in the 2000s


DARPA’s AI efforts in the 2000s focused on autonomous systems and intelligent agents for military
applications. The DARPA Grand Challenge (2004–2007) spurred innovation in autonomous vehicles.
The 2005 challenge saw Stanford’s Stanley, an autonomous car, complete a 132-mile desert course using
CHAPTER 1. INTRODUCTION TO ARTIFICIAL INTELLIGENCE 4

AI techniques like machine learning, probabilistic robotics, and real-time collision avoidance. These
advancements influenced modern self-driving cars. DARPA also funded the Urban Challenge (2007),
where autonomous vehicles navigated urban environments, obeying traffic rules, using AI for real-time
decision-making and sensor fusion. Additionally, DARPA’s investment in natural language processing led
to technologies like Siri, a spinoff from SRI International’s AI research.

AI in 2000s

NASA DARPA

Mars Rovers Grand Challenge

Figure 1.2: AI Developments by NASA and DARPA in the 2000s

1.7 Brief History of Artificial Intelligence


The development of Artificial Intelligence (AI) spans several decades, marked by key milestones and
evolving technologies:

• 1943: Warren McCulloch and Walter Pitts proposed a model of artificial neurons, laying the
foundation for neural networks.

• 1950: Alan Turing introduced the concept of a machine that could simulate any human intelligence,
proposing the famous Turing Test to evaluate machine intelligence.

• 1956: The term “Artificial Intelligence” was coined at the Dartmouth Conference, considered the
birth of AI as a field.

• 1960s-1970s: Development of Expert Systems that emulate decision-making abilities of human


experts.

• 1980s: The AI Winter period due to reduced funding and unmet expectations; however, significant
progress in machine learning and knowledge representation continued.

• 1997: IBM’s Deep Blue defeated the reigning world chess champion Garry Kasparov, showcasing
AI’s ability to tackle complex strategy games.

• 2010s: Breakthroughs in deep learning using GPUs led to rapid progress in image and speech
recognition.

• 2016: AlphaGo, developed by DeepMind, defeated Go champion Lee Sedol, demonstrating


advanced reinforcement learning and strategic planning.

• Present and Future: AI continues to grow in diverse applications such as autonomous vehicles,
natural language processing, and medical diagnostics.
CHAPTER 1. INTRODUCTION TO ARTIFICIAL INTELLIGENCE 5

1.8 Present State of AI


As of 2025, AI has become a transformative force across industries, driven by advancements in deep
learning, generative models, and large language models (LLMs). Key developments include:

• Generative AI: Tools like ChatGPT and DALL-E create human-like text and images, used in
creative industries and customer service. For example, AI-generated art is now featured in galleries.

• Autonomous Systems: Self-driving cars (e.g., Waymo) and drones use AI for real-time navigation
and decision-making, building on DARPA’s Grand Challenge.

• Healthcare: AI systems like AlphaFold solve complex problems like protein folding, while
diagnostic tools outperform human doctors in detecting certain cancers.

• Explainable AI: DARPA’s XAI program (2017–2021) developed techniques to make AI decisions
transparent, crucial for military and medical applications.

• Ethical Challenges: Bias in AI models and job displacement remain concerns. For instance, facial
recognition systems have faced scrutiny for misidentification errors.

NASA continues to use AI for Earth observation (e.g., NISAR satellite, launched 2025, for monitoring
surface changes) and space exploration, while DARPA’s AI Next campaign (2018–present) invests in
trustworthy AI, such as the ACE program, which tested AI-piloted F-16s in 2024. The following diagram
illustrates current AI trends:

AI in 2025

Generative AI Autonomous Systems Healthcare

Figure 1.3: Current Trends in AI (2025)

1.9 Types of AI
AI can be classified into types based on its capabilities and functionality, ranging from narrow systems
designed for specific tasks to theoretical systems with broad cognitive abilities. Drawing from Russell
and Norvig and contemporary classifications, the main types of AI are Narrow AI, General AI, and
Superintelligent AI, each with distinct characteristics and examples.

• Narrow AI (Weak AI): Narrow AI is designed to perform specific tasks with high proficiency,
lacking general intelligence. It operates within a predefined scope and excels in specialized domains.
Examples include:
CHAPTER 1. INTRODUCTION TO ARTIFICIAL INTELLIGENCE 6

• Voice Assistants: Siri and Amazon’s Alexa use natural language processing to interpret and
respond to voice commands, such as setting reminders or answering queries about the weather.
• Recommendation Systems: Netflix and Spotify employ AI to analyze user preferences and
suggest movies or music. For instance, Netflix’s algorithm uses viewing history to recommend
shows like *Stranger Things*.
• Image Recognition: Google’s Vision AI identifies objects in images, such as detecting diabetic
retinopathy in medical scans with accuracy rivaling human experts.
• Autonomous Navigation: NASA’s Perseverance rover uses Narrow AI for terrain navigation,
selecting paths on Mars to avoid obstacles.
• Fraud Detection: PayPal’s AI systems analyze transaction patterns to flag suspicious activities,
preventing unauthorized payments in real-time.

• General AI (Strong AI): General AI aims to replicate human-like intelligence, capable of performing
any intellectual task a human can do across diverse domains. While still theoretical, research is
advancing toward this goal. Examples of progress include:

• Multitask Learning Models: Models like DeepMind’s Gato can perform multiple tasks, such
as playing games, answering questions, and controlling robotic arms, showing early versatility.
• Large Language Models: Systems like Grok (developed by xAI) demonstrate broad capabili-
ties in understanding and generating text across contexts, from answering scientific queries to
writing poetry, though they remain task-limited.
• Game-Playing AI: DeepMind’s AlphaCode solves complex programming problems, adapting
to new challenges in a way that mimics human problem-solving flexibility.

• Superintelligent AI: Superintelligent AI, a hypothetical future stage, surpasses human intelligence
in all domains, including creativity and problem-solving. It remains speculative but raises ethical
concerns, as discussed by Russell regarding AI alignment with human values. No concrete examples
exist, but theoretical scenarios include:

• Global Optimization Systems: A superintelligent AI could optimize global logistics, solving


supply chain issues with unprecedented efficiency, but risks unintended consequences if not
aligned with human goals.
• Scientific Discovery: Such an AI might accelerate breakthroughs in physics or medicine,
solving problems like fusion energy or curing complex diseases faster than human researchers.

The following diagram illustrates the types of AI and their relationship:

1.10 Examples of AI Tasks


AI systems perform a wide range of tasks that leverage their ability to process data, learn patterns, and
make decisions. Below are detailed examples of tasks AI can accomplish, categorized by their application
domains, drawing from the frameworks in Russell and Norvig:
CHAPTER 1. INTRODUCTION TO ARTIFICIAL INTELLIGENCE 7

AI Types

Narrow AI General AI Superintelligent AI

Voice Assistants Multitask Models

Figure 1.4: Types of AI and Example Applications

• Image Recognition and Classification: AI can identify objects, people, or patterns in images
with high accuracy. For example, Google’s Vision AI can analyze medical images to detect
diabetic retinopathy, matching or surpassing human radiologists’ accuracy. In retail, AI systems like
Amazon’s Rekognition identify products in photos, enabling automated inventory management.

• Natural Language Processing (NLP): AI excels at understanding and generating human language.
Chatbots like Grok (developed by xAI) answer complex queries in real-time, providing information
or assistance. Another example is Google Translate, which translates text across languages with
near-human fluency, using neural machine translation.

• Predictive Analytics: AI predicts outcomes based on historical data. In finance, algorithms like
those used by PayPal detect fraudulent transactions by analyzing patterns in payment data, flagging
anomalies in milliseconds. In weather forecasting, AI models like IBM’s GRAF predict storms
with high precision by processing satellite and radar data.

• Autonomous Navigation: AI enables systems to navigate complex environments. For instance,


NASA’s Perseverance rover uses AI to autonomously traverse Martian terrain, avoiding obstacles and
selecting scientific targets. Similarly, Waymo’s self-driving cars navigate urban roads, processing
sensor data to avoid collisions.

• Game Playing and Strategy: AI systems excel in strategic tasks. DeepMind’s AlphaGo defeated
world champion Go players by learning optimal strategies through reinforcement learning. In video
games, AI controls non-player characters, adapting to player actions in real-time.

AI Tasks

Image Recognition NLP Predictive Analytics

Navigation

Figure 1.5: Categories of AI Tasks


CHAPTER 1. INTRODUCTION TO ARTIFICIAL INTELLIGENCE 8

1.11 AI Applications
AI has transformed numerous fields. Key applications include:

• Healthcare: AI systems like DeepMind’s AlphaFold solve protein folding problems, aiding drug
discovery.

• Finance: Fraud detection algorithms analyze transaction patterns to flag suspicious activity, e.g.,
detecting unauthorized credit card use.

• Education: Adaptive learning platforms tailor content to individual student needs, improving
engagement.

• Entertainment: Recommendation systems, like Netflix’s, suggest content based on user preferences.

The following diagram illustrates the diversity of AI applications:

AI

Healthcare Finance Education

Figure 1.6: Major AI Application Domains

1.12 Examples of AI Systems


• Virtual Assistants: Examples include Amazon Alexa, Apple Siri, Google Assistant, and Microsoft
Cortana. These systems understand natural language commands, provide weather updates, control
smart devices, set reminders, and perform simple web searches.

• Recommendation Engines: Services like Netflix, YouTube, Amazon, and Spotify use machine
learning models to analyze user behavior and preferences. Based on viewing or purchase history,
these systems recommend content or products, improving user engagement.

• Autonomous Vehicles: Companies like Tesla, Waymo, and Uber use AI to process data from
cameras, LiDAR, GPS, and sensors to navigate safely. These systems use deep learning for lane
detection, obstacle avoidance, and real-time decision-making.

• AI in Games: Games like Chess, Go, and StarCraft have seen AI competitors outperforming
humans. AlphaGo (by DeepMind) defeated world champions in Go, showcasing AI’s capability in
strategic planning and complex decision-making.

• Chatbots and Virtual Agents: Used in customer service (e.g., Drift, Intercom, IBM Watson
Assistant), these systems handle inquiries, book tickets, or offer product support by processing user
input through NLP and responding contextually.
CHAPTER 1. INTRODUCTION TO ARTIFICIAL INTELLIGENCE 9

• Healthcare AI Systems: AI is used for medical imaging analysis, predicting patient deterioration,
drug discovery, and robotic surgeries. Systems like IBM Watson for Oncology assist doctors in
diagnosis and treatment planning.

• Finance and Banking AI: Applications include fraud detection, credit scoring, algorithmic trading,
and chatbots for customer queries (e.g., Erica by Bank of America).

• Smart Home Devices: Thermostats (Nest), lighting systems, and security devices use AI to learn
usage patterns and optimize energy or security.

• AI in Education: Platforms like Coursera and Khan Academy use AI to personalize learning paths.
Intelligent tutoring systems assess student performance and adapt lesson delivery.

• Agricultural AI: AI-driven drones and IoT sensors monitor crops, predict yields, and detect diseases.
Platforms like Blue River Technology use computer vision to apply herbicides precisely.

Real-time AI systems process data and make decisions instantaneously, critical in applications like:

• Autonomous Vehicles: Self-driving cars, such as Tesla’s, use AI to process sensor data (e.g.,
LIDAR, cameras) to navigate roads in real-time, avoiding obstacles.

• Smart Assistants: Devices like Amazon’s Alexa process voice commands instantly, using natural
language processing to respond to user queries.

• Industrial Automation: Real-time AI monitors machinery, detecting faults (e.g., abnormal


vibrations) to prevent failures.

For example, in autonomous vehicles, AI interprets sensor inputs, predicts pedestrian movements, and
adjusts speed within milliseconds.
Chapter 2
Rational Agents and Environments

2.1 Rationality
Rationality in AI refers to the ability of an agent to make decisions that maximize the expected outcome
of its actions, given its knowledge and goals. A rational agent acts to achieve the best possible results or,
when uncertainty is present, the best expected results, as defined by a performance measure. Rationality is
context-dependent, relying on the agent’s perceptions, available actions, and environment. For example, a
chess-playing AI like DeepBlue acts rationally by selecting moves that maximize its chances of winning,
based on a predefined evaluation function.

Definition of Rationality
In Artificial Intelligence, rationality is the property of an agent that chooses actions expected to
maximize its performance measure, based on the percepts and knowledge it has at the time of acting.
A rational agent is not omniscient but always aims to make the best possible decision given the
available information.

2.2 Rationality Examples in Detail


Rationality manifests in various AI applications, where agents optimize their actions based on specific
goals. Here are detailed examples:

• Autonomous Vehicles: A self-driving car, such as Waymo’s, uses AI to choose driving actions (e.g.,
braking, turning) to maximize safety and efficiency. It processes sensor data (LIDAR, cameras) to
avoid obstacles and follow traffic rules, optimizing a performance measure of safe arrival time.

• Medical Diagnosis Systems: IBM’s Watson diagnoses diseases by analyzing patient data against
medical literature. It selects the diagnosis with the highest probability of correctness, maximizing
patient outcomes. For instance, Watson might recommend cancer treatment based on genetic
markers, optimizing survival rates.

• Game-Playing AI: DeepMind’s AlphaGo selects moves in Go to maximize its winning probability.
It evaluates board states using a neural network, choosing actions that lead to optimal long-term
outcomes, as demonstrated in its 2016 victory over Lee Sedol.

10
CHAPTER 2. RATIONAL AGENTS AND ENVIRONMENTS 11

• Logistics Optimization: Amazon’s warehouse robots use AI to optimize package delivery paths.
They choose routes that minimize travel time and energy use, ensuring efficient order fulfillment.

2.3 Agent
An agent is anything that perceives its environment through sensors and acts upon it through actuators
to achieve goals. In AI, agents are computational systems designed to interact with their surroundings
intelligently. For example, a thermostat is a simple agent that senses room temperature and adjusts heating
to maintain a target temperature.

Definition of an Agent in AI
In Artificial Intelligence, an agent is an entity that perceives its environment through sensors and
acts upon that environment through actuators.
An agent’s behavior is defined by the agent function, which maps a sequence of percepts (the
percept history) to an action:
𝑓 : 𝑃∗ → 𝐴

where 𝑃∗ is the set of all percept sequences and 𝐴 is the set of actions.
Examples:

• A robot with cameras (sensors) and motors (actuators).

• A software agent that perceives keyboard input and acts by displaying responses.

Agent Architecture
The architecture of an agent defines its internal structure, integrating sensors, actuators, and decision-
making components. A typical agent architecture includes:

• Sensors: Devices or algorithms that perceive the environment (e.g., cameras in a robot).

• Actuators: Mechanisms that perform actions (e.g., motors in a robot).

• Processor: The computational unit that processes perceptions and selects actions based on an agent
program.

For example, in a self-driving car, cameras and LIDAR are sensors, steering and brakes are actuators, and
the onboard computer processes data to navigate.

Sensors Processor Actuators

Figure 2.1: Generic Agent Architecture


CHAPTER 2. RATIONAL AGENTS AND ENVIRONMENTS 12

Agent Program
The agent program is the software that implements the agent’s decision-making process, mapping
perceptions to actions. It defines how the agent behaves in response to environmental inputs. For example,
a chess AI’s agent program evaluates board positions and selects moves based on a minimax algorithm,
optimizing for a win. The program can be rule-based, learning-based, or a hybrid, depending on the
agent’s design.

2.4 Autonomy in AI Agents

Definition of Autonomy
Autonomy refers to the degree to which an AI agent can operate independently, making decisions
without human intervention based on its perceptions and internal state.
An autonomous agent is capable of performing tasks and achieving goals on its own, while adapting
to environmental changes and improving its behavior through learning and experience.

Key Aspects of Autonomy


• Self-governance: The ability to select actions without external control.

• Perception-driven: Continuously perceives the environment using sensors.

• Decision-making: Makes decisions based on knowledge, goals, and historical percepts.

• Adaptability: Adjusts behavior in response to changes or unexpected events.

• Learning: Improves performance over time through experience.

2.4.1 Degrees of Autonomy


Autonomy exists on a spectrum:

• Full autonomy: The agent operates completely independently (e.g., NASA’s Perseverance rover).

• Semi-autonomy: The agent acts independently but may require occasional human guidance (e.g.,
robotic surgery systems).

• No autonomy: The agent requires full human control.

2.4.2 Examples of Autonomous Agents


• Robotic vacuum cleaner: Navigates rooms, avoids obstacles, and cleans autonomously (e.g.,
Roomba).
CHAPTER 2. RATIONAL AGENTS AND ENVIRONMENTS 13

• Self-driving car: Perceives road conditions, navigates traffic, and makes driving decisions
independently.

• Intelligent personal assistants: Interprets commands and performs tasks without human interven-
tion (e.g., Siri, Alexa).

2.4.3 Importance of Autonomy


• Reduces the need for constant human oversight.

• Enables operation in dynamic and unpredictable environments.

• Supports scalability in complex systems, such as multi-agent systems.

• Critical for applications like space exploration, military drones, and industrial automation.

2.4.4 Agent Example: Robotic Vacuum Cleaner


A robotic vacuum cleaner, such as a Roomba, exemplifies an autonomous AI agent. It operates as follows:

• Sensors: Infrared sensors, bump sensors, and dirt detectors perceive the environment (e.g., detecting
walls or dirt patches).

• Actuators: Motors for movement and suction mechanisms for cleaning.

• Agent program: Processes sensor data to navigate rooms, avoid obstacles, and clean efficiently.
For example, it may follow a spiral pattern to cover a dirty area or reverse when hitting a wall.

• Goal: Maximize floor coverage and cleanliness while minimizing energy use.

• Autonomy: Operates without human intervention, dynamically adapting to room layouts and
obstacles.

This agent acts rationally by selecting actions (e.g., turning, cleaning) that optimize its performance
measure (clean floor area).

2.5 Types of Environments


The environment in which an agent operates significantly affects its design and behavior. Russell and
Norvig categorize environments based on several properties, each with distinct challenges. Below are the
main types with detailed examples:

• Fully Observable vs. Partially Observable:

• Fully Observable: The agent’s sensors capture the complete state of the environment. For
example, a chess-playing AI has full access to the board state, enabling precise decision-making.
CHAPTER 2. RATIONAL AGENTS AND ENVIRONMENTS 14

• Partially Observable: The agent has incomplete information. A self-driving car operates in a
partially observable environment, as it cannot see around corners or detect hidden pedestrians,
requiring probabilistic reasoning.

• Deterministic vs. Stochastic:

• Deterministic: The next state is fully determined by the current state and action. A robotic
arm assembling parts in a factory operates in a deterministic environment, where actions like
“move left” have predictable outcomes.
• Stochastic: Outcomes are uncertain. A weather forecasting AI deals with a stochastic envi-
ronment, as weather patterns involve randomness (e.g., predicting rain based on probabilistic
models).

• Static vs. Dynamic:

• Static: The environment remains unchanged while the agent deliberates. A crossword-solving
AI works in a static environment, as the puzzle does not change during processing.
• Dynamic: The environment changes over time. A robotic vacuum cleaner operates in a
dynamic environment, as furniture may be moved or new dirt appears while it cleans.

• Discrete vs. Continuous:

• Discrete: The environment has a finite number of states and actions. A tic-tac-toe AI operates
in a discrete environment, with a limited set of board configurations.
• Continuous: The environment involves continuous variables. A drone navigating airspace
deals with continuous positions and velocities, requiring real-time control adjustments.

• Single-Agent vs. Multi-Agent:

• Single-Agent: Only one agent interacts with the environment. A thermostat adjusting room
temperature is a single-agent system.
• Multi-Agent: Multiple agents interact, either cooperatively or competitively. In autonomous
vehicle fleets, cars coordinate to optimize traffic flow, acting as a multi-agent system.

2.6 Types of Agents


An agent is an entity that perceives its environment through sensors and acts upon that environment
through actuators. The internal mechanism that maps percepts to actions is called the agent architecture.
Each type of agent uses a different architecture based on the complexity of its tasks and environment.

2.6.1 Simple Reflex Agent Architecture


A Simple Reflex Agent operates based on the current percept only. It uses a set of condition-action rules
to determine its next action. It does not store any internal state or consider the percept history, which
limits its use to fully observable environments.
CHAPTER 2. RATIONAL AGENTS AND ENVIRONMENTS 15

Key Components:

• Sensors: Perceive the current state of the environment.

• Condition-Action Rules: A rule base that maps percepts to actions (e.g., IF condition THEN
action).

• Actuators: Execute the chosen action in the environment.

Working Mechanism:

1. The agent receives the current percept from the environment.

2. It matches the percept with a rule from its rule base.

3. The corresponding action is selected and sent to the actuators.

Formal Function:
Agent Function: 𝑓 : 𝑃 → 𝐴

Where 𝑃 is the set of percepts and 𝐴 is the set of actions.

SIMPLE-REFLEX-AGENT Algorithm
FUNCTION SIMPLE-REFLEX-AGENT(percept) RETURNS an action
PERSISTENT: rules, a set of condition–action rules
state ← INTERPRET-INPUT(percept)
rule ← RULE-MATCH(state, rules)
action ← [Link]
RETURN action

Limitations:

• Works only in fully observable, static environments.

• No ability to learn or adapt.

• Cannot handle uncertainty or incomplete information.

Example: A vacuum robot turns left when it detects an obstacle in front. It doesn’t remember whether
it was there before.

2.6.2 Model-Based Agent


A Model-Based Agent is an intelligent agent that maintains some internal representation (model) of the
environment. It uses this model to keep track of aspects of the world that are not currently perceptible.
This enables the agent to make decisions even in partially observable environments.
CHAPTER 2. RATIONAL AGENTS AND ENVIRONMENTS 16

Figure 2.2: Architecture of a Simple Reflex Agent

What is a Model?
In the context of AI agents, a model is a representation of how the environment works. It includes:

• Knowledge of the effects of actions.

• Information about how the environment changes over time.

• Rules for predicting future percepts based on past actions and observations.

Working Principle
• The agent receives a percept from the environment.

• It updates its internal model of the world.

• It chooses an action based on the current percept and the internal state derived from the model.

• The chosen action is executed through actuators.

Example: Robotic Navigation


Consider a robot navigating a building where it cannot always see all paths:

• Percepts: Sensors detect walls, doors, and movement.

• Model: A map of the building and known rules about movement (e.g., “moving north from room A
leads to room B unless there is a wall”).

• Internal State: Current estimate of the robot’s location.


CHAPTER 2. RATIONAL AGENTS AND ENVIRONMENTS 17

Figure 2.3: Model Based Agent

• Action: Move to the next room or replan if the path is blocked.

MODEL-BASED-REFLEX-AGENT Algorithm
FUNCTION MODEL-BASED-REFLEX-AGENT(percept) RETURNS an action
PERSISTENT:
state, the agent’s current conception of the world state
transition model, how the next state depends on the current state and
action
sensor model, how the current world state is reflected in the agent’s
percepts
rules, a set of condition–action rules
action, the most recent action (initially none)

state ← UPDATE-STATE(state, action, percept, transition model, sensor


model)
rule ← RULE-MATCH(state, rules)
action ← [Link]
RETURN action

Advantages over Simple Reflex Agents


• Can operate in partially observable environments.

• Maintains memory through internal state.

• More flexible and robust in dynamic settings.


CHAPTER 2. RATIONAL AGENTS AND ENVIRONMENTS 18

2.6.3 Goal-Based Agent


A Goal-Based Agent is an intelligent agent that selects actions by considering future consequences and
whether those actions will help achieve a specific goal. Unlike reflex agents that respond only to immediate
conditions, goal-based agents reason about the desirability of outcomes.

Key Characteristics
• Uses a goal to evaluate actions.

• Involves search and planning to determine a sequence of actions that leads to the goal.

• Maintains an internal model to simulate possible future states.

Architecture
A goal-based agent typically includes:

• Perceptual Input: Gathers current state from environment.

• World Model: Represents how the environment evolves.

• Goal Information: A desired state the agent aims to reach.

• Action Selector: Chooses actions that lead toward the goal using search or planning algorithms.

Real-World Example: Delivery Drone


Scenario: A delivery drone needs to deliver a package to a specific location in a city.

• Percepts: GPS coordinates, obstacles, battery level.

• Goal: Deliver the package to the destination address.

• Actions: Move north, south, ascend, descend, avoid obstacle.

• Planning: Uses A* or Dijkstra’s algorithm to find the optimal route avoiding no-fly zones.

Comparison with Other Agents


• Reflex Agent: Reacts without thinking ahead.

• Model-Based Agent: Maintains internal state but may still use fixed rules.

• Goal-Based Agent: Actively plans actions to reach goals.


CHAPTER 2. RATIONAL AGENTS AND ENVIRONMENTS 19

Figure 2.4: Goal Based Agent

Advantages
• More flexible than rule-based agents.

• Can handle new and complex tasks by recomputing plans.

• Better at handling long-term objectives.

Limitations
• Planning can be computationally expensive.

• Requires accurate modeling of environment and outcomes.

2.6.4 Utility-Based Agent Architecture


A Utility-Based Agent is an advanced form of intelligent agent that not only pursues goals but also
evaluates the desirability or utility of different possible states. It selects actions that maximize its expected
utility, enabling it to make better decisions in complex, uncertain, or conflicting environments.

What is Utility?
Utility is a numerical representation of an agent’s preferences over possible outcomes. It helps the agent
to:

• Handle trade-offs among competing goals.

• Make rational decisions under uncertainty.

• Optimize long-term performance, not just immediate outcomes.


CHAPTER 2. RATIONAL AGENTS AND ENVIRONMENTS 20

How It Works
• The agent perceives the environment.

• It considers different possible future states resulting from its actions.

• It assigns a utility value to each state.

• It selects the action that leads to the highest expected utility.

Real-World Example: Autonomous Car


An autonomous vehicle must make decisions that balance several factors:

• Goal: Reach destination safely and efficiently.

• Percepts: Traffic, road conditions, speed limits, obstacles.

• Utility Function: Combines safety, speed, comfort, fuel efficiency, and traffic rules into a single
score.

• Action: Choose route and speed that maximizes the total utility.

Comparison with Other Agents


• Reflex Agent: Reacts to conditions without goals.

• Goal-Based Agent: Chooses actions to achieve goals, but doesn’t compare outcomes.

• Utility-Based Agent: Considers multiple goals and uncertainties, selecting the best possible action.

Advantages
• Makes informed decisions under uncertainty.

• Balances multiple competing objectives.

• Adapts better in dynamic or complex environments.

Limitations
• Defining an accurate and meaningful utility function is difficult.

• Requires more computation and modeling than goal-based agents.

Example: A stock-trading AI chooses actions that maximize expected returns while minimizing risk.
CHAPTER 2. RATIONAL AGENTS AND ENVIRONMENTS 21

Figure 2.5: A model-based, utility-based agent. It uses a model of the world, along with a utility function
that measures its preferences among states of the world. Then it chooses the action that leads to the
best expected utility, where expected utility is computed by averaging over all possible outcome states,
weighted by the probability of the outcome.

2.6.5 Learning Agent


A Learning Agent is an AI system that improves its performance over time by learning from its experiences.
Unlike fixed-rule agents, learning agents adapt to new circumstances and enhance their decision-making
abilities through continuous feedback and interaction with the environment.

Components of a Learning Agent


A typical learning agent consists of the following components:

• Learning Element: Responsible for improving the agent’s performance by learning from experience.

• Performance Element: Executes actions and makes decisions based on current knowledge.

• Critic: Provides feedback by evaluating the performance of the agent.

• Problem Generator: Suggests new actions that lead to better exploration and learning.

Example: Game-Playing Agent


Consider an AI agent designed to play a simple game like tic-tac-toe:

• Initially, the agent has no knowledge of the game strategy and plays randomly.

• After each game, it receives feedback: win, lose, or draw.

• The learning element updates a table of state-action values based on the outcome.
CHAPTER 2. RATIONAL AGENTS AND ENVIRONMENTS 22

Figure 2.6: Learning Agent

• Over time, the agent learns to prefer actions that lead to winning and avoids those that result in
losing.

Each architecture type supports different capabilities. The choice depends on the environment
complexity, observability, and the need for adaptability.

Advantages
• Adaptability to dynamic or unknown environments.

• Improved accuracy and decision-making over time.

• Applicable in various domains such as robotics, autonomous vehicles, and recommendation systems.

2.7 Summary of AI Agents


An Artificial Intelligence (AI) agent is an autonomous entity that perceives its environment through
sensors and acts upon that environment using actuators to achieve specific goals. AI agents can range
from simple rule-based systems to complex learning agents. They are characterized by their ability to
make decisions, adapt over time, and operate with minimal human intervention.

Types of AI Agents
AI agents are commonly categorized as follows:
CHAPTER 2. RATIONAL AGENTS AND ENVIRONMENTS 23

• Simple Reflex Agents: Act solely based on the current percept, without considering the history of
percepts.

• Model-Based Reflex Agents: Maintain an internal model of the world to handle partially observable
environments.

• Goal-Based Agents: Choose actions based on the achievement of predefined goals.

• Utility-Based Agents: Select actions based on a utility function that evaluates different possible
outcomes.

• Learning Agents: Improve their performance over time by learning from experience.

Key Components
An intelligent agent typically consists of:

• Perception: Gathering data from the environment.

• Reasoning: Making decisions based on percepts and goals.

• Action: Executing operations that affect the environment.

• Learning: Enhancing performance through data and experience.

AI agents are foundational in various applications, including robotics, autonomous vehicles, recom-
mendation systems, and personal assistants. As research advances, agents become more autonomous,
adaptive, and capable of operating in complex, dynamic environments.
Chapter 3
Problem Solving

3.1 Introduction
In Artificial Intelligence (AI), many problems can be formulated and solved using the concept of a State
Space. This representation provides a formal model of the problem, allowing AI algorithms to explore
possible solutions through various search techniques.

3.2 What is a State Space Problem?


A State Space Problem is a mathematical abstraction used to represent the set of all possible configurations
(states) an agent or environment can be in. It provides a framework for modeling problems that can be
solved using search algorithms.

A state space is defined as a directed graph where:

• Nodes represent states.

• Edges represent actions or transitions between states.

• Initial State is where the search begins.

• Goal State(s) are desired configurations we aim to reach.

Components of a State Space Problem


A state space problem typically consists of the following elements:

1. Initial State (𝑆0 ): The state in which the agent starts.

2. State Space (𝑆): A set of all possible states.

3. Actions (𝐴): The set of actions available to the agent.

4. Transition Model (𝑇 (𝑠, 𝑎) → 𝑠′): Describes the result of applying action 𝑎 in state 𝑠.

5. Goal Test: A function that checks whether a given state is a goal state.

6. Path Cost: A function that assigns a numeric cost to a sequence of actions.

24
CHAPTER 3. PROBLEM SOLVING 25

Example: 8-Puzzle Problem


Problem: Slide tiles on a 3×3 grid to reach a desired goal configuration.

• State: A 3×3 arrangement of numbers 1–8 and a blank.

• Initial State: A scrambled configuration.

• Actions: Move blank tile up/down/left/right.

• Goal State: The correct tile order.

• Path Cost: Number of moves.

State Space Tree Representation (Example)

Start

A B

C D E F

Figure 3.1: A Simple State Space Tree

Each node represents a state. The edges show actions or transitions. Tree search algorithms like BFS
and DFS explore this space.

Applications in AI
State space problems are foundational to many AI applications, such as:

• Pathfinding: Finding shortest routes (e.g., GPS navigation).

• Game Playing: Chess, checkers, tic-tac-toe.

• Planning: Robot planning tasks.

• Problem Solving: Puzzle solvers like Sudoku, Rubik’s cube.


CHAPTER 3. PROBLEM SOLVING 26

Search Algorithms on State Spaces


To solve state space problems, AI employs various search algorithms, including:

• Uninformed Search: BFS, DFS, Uniform Cost Search.

• Informed Search: A*, Greedy Best-First Search.

These algorithms systematically explore the state space to find paths from the initial state to a goal
state.

Advantages of Using State Space Representation


• Provides a structured way to model diverse problems.

• Enables reusability of search algorithms.

• Supports both deterministic and non-deterministic environments.

State Space Search Overview


A problem is modeled as a state space with:

• States: All possible configurations.

• Initial State: Starting configuration.

• Goal State(s): Desired configuration(s).

• Actions: Operations transforming one state to another.

• Transition Model: Function Result(𝑠, 𝑎) → 𝑠′.

• Goal Test: Checks if a state is a goal.

• Path Cost: Numerical cost of actions (e.g., number of steps).

Uninformed search explores this space systematically, without domain-specific knowledge.

3.3 Uninformed Search


Uninformed search algorithms, also called blind search, explore the state space without problem-specific
knowledge (e.g., heuristics), relying on the structure of the search tree or graph. They systematically
evaluate all possible paths, making them robust but often less efficient than informed search for large
problems.
Characteristics:
CHAPTER 3. PROBLEM SOLVING 27

• Explore nodes based on predefined strategies (e.g., breadth, depth, or cost).

• Complete for finite state spaces (find a solution if one exists).

• May be suboptimal unless designed for optimality (e.g., UCS).

• Used in problems where heuristics are unavailable or unreliable russell2010artificial.

3.3.1 Breadth-First Search (BFS)


Breadth-First Search (BFS) is one of the fundamental graph and tree traversal techniques used in Artificial
Intelligence for problem solving. It systematically explores all the nodes at a given depth level before
moving to the next.
It is a classic example of an uninformed search algorithm, meaning it does not use any domain-specific
knowledge.

Working Principle
BFS starts from the initial node (root), explores all of its neighbors, then moves to the neighbors’ neighbors,
and so on.
It uses a FIFO (First-In-First-Out) queue to keep track of nodes to be explored next.

Algorithm 1 Breadth-First Search


function BFS(problem)
node ← Node(Initial-State(problem))
if Goal-Test([Link]) then return node
end if
frontier ← Queue(node)
explored ← Empty-Set
while frontier is not empty do
node ← Dequeue(frontier)
Add [Link] to explored
for each action in Actions([Link]) do
child ← Child-Node(problem, node, action)
if [Link] not in explored and child not in frontier then
if Goal-Test([Link]) then return child
end if
Enqueue(frontier, child)
end if
end for
end whilereturn failure
end function

Properties:

• Completeness: Complete in finite graphs.


CHAPTER 3. PROBLEM SOLVING 28

A B

C G

Figure 3.2: BFS selects S → B → G (red, shortest path).

• Optimality: Optimal for unweighted graphs (finds shortest path).

• Time Complexity: 𝑂 (𝑏 𝑑 ), where 𝑏 is the branching factor and 𝑑 is the depth of the shallowest goal.

• Space Complexity: 𝑂 (𝑏 𝑑 ), as it stores all nodes at the current depth.

Limitations:

• High memory usage for large branching factors or deep goals.

• Inefficient for weighted graphs (use UCS instead).


Example: Find a path from City S to City G in an unweighted graph:

• Nodes: S, A, B, C, G.
• Edges: S to A, S to B, A to C, B to G, C to G.

Step-by-Step Process:

1. Initialize: Queue = {S}, Visited = {}.


2. Dequeue S. Not goal. Enqueue A, B. Queue = {A, B}, Visited = {S}.
3. Dequeue A. Not goal. Enqueue C. Queue = {B, C}, Visited = {S, A}.
4. Dequeue B. Not goal. Enqueue G. Queue = {C, G}, Visited = {S, A, B}.
5. Dequeue C. Not goal. G already in queue, skip. Queue = {G}, Visited = {S, A, B, C}.
6. Dequeue G. Goal found. Path: S → B → G (shortest, 2 edges).

Step-by-Step Example
Consider the following graph/tree:
CHAPTER 3. PROBLEM SOLVING 29

B C

D E F G

Let the initial node be A, and the goal node be G.

Step-by-step BFS Traversal

1. Start with node A.


Frontier: [A]
Explored: []
2. Expand A: add B and C to frontier.
Frontier: [B, C]
Explored: [A]
3. Dequeue B: add D and E to frontier.
Frontier: [C, D, E]
Explored: [A, B]
4. Dequeue C: add F and G to frontier.
Frontier: [D, E, F, G]
Explored: [A, B, C]
5. Dequeue D: not goal.
Frontier: [E, F, G]
Explored: [A, B, C, D]
6. Dequeue E: not goal.
Frontier: [F, G]
Explored: [A, B, C, D, E]
7. Dequeue F: not goal.
Frontier: [G]
Explored: [A, B, C, D, E, F]
8. Dequeue G: Goal found!
Path: A → C → G
CHAPTER 3. PROBLEM SOLVING 30

Example Graph for BFS

B C

D E F G

This is a simple binary tree structure where:

• A is the root node.


• B and C are children of A.
• D and E are children of B.
• F and G are children of C.

Examples

Example 1: 8-Puzzle

The 8-puzzle is a 3x3 grid with 8 numbered tiles and a blank space, where the goal is to rearrange
the tiles to a target configuration by sliding the blank.

Problem Setup

• States: All possible tile arrangements (9!/2 = 181, 440 solvable states).
• Initial State:
2 8 3
1 6 4
7 _ 5

• Goal State:
1 2 3
4 5 6
7 8 _

• Actions: Move the blank up, down, left, or right (if valid).
• Path Cost: Each move costs 1.
CHAPTER 3. PROBLEM SOLVING 31

Step-by-Step BFS

1. Depth 0: Start with initial state (2, 8, 3, 1, 6, 4, 7, _, 5). Not the goal. Enqueue successors:
• Move blank up: (2, 8, 3, 1, _, 4, 7, 6, 5).
• Move blank left: (2, 8, 3, 1, 6, 5, 7, _, 4).
Frontier: [(2, 8, 3, 1, _, 4, 7, 6, 5), (2, 8, 3, 1, 6, 5, 7, _, 4)].
2. Depth 1: Dequeue (2, 8, 3, 1, _, 4, 7, 6, 5). Not the goal. Generate successors:
• Move blank down: (2, 8, 3, 1, 6, 4, 7, _, 5) (already explored, skip).
• Move blank left: (2, 8, 3, _, 1, 4, 7, 6, 5).
• Move blank right: (2, 8, 3, 1, 4, _, 7, 6, 5).
Frontier: [(2, 8, 3, 1, 6, 5, 7, _, 4), (2, 8, 3, _, 1, 4, 7, 6, 5), (2, 8, 3, 1, 4, _, 7, 6, 5)].
3. Continue exploring all depth 1 nodes, then depth 2, and so on.
4. Suppose the goal is found at depth 10 (a typical solution length for a scrambled 8-puzzle).

BFS guarantees the shortest path (fewest moves), e.g., 10 moves.

Example 2: Water Jug Problem

The water jug problem involves measuring exactly 2 liters using a 4-liter jug (A) and a 3-liter jug
(B).

Problem Setup

• States: (𝑎, 𝑏), where 𝑎 ∈ [0, 4] is water in Jug A, 𝑏 ∈ [0, 3] is water in Jug B.
• Initial State: (0, 0).
• Goal State: (2, 𝑏) or (𝑎, 2).
• Actions:
1. Fill A: (𝑎, 𝑏) → (4, 𝑏).
2. Fill B: (𝑎, 𝑏) → (𝑎, 3).
3. Empty A: (𝑎, 𝑏) → (0, 𝑏).
4. Empty B: (𝑎, 𝑏) → (𝑎, 0).
5. Pour A to B: (𝑎, 𝑏) → (𝑎 − min(𝑎, 3 − 𝑏), 𝑏 + min(𝑎, 3 − 𝑏)).
6. Pour B to A: (𝑎, 𝑏) → (𝑎 + min(𝑏, 4 − 𝑎), 𝑏 − min(𝑏, 4 − 𝑎)).
• Path Cost: Each action costs 1.
CHAPTER 3. PROBLEM SOLVING 32

Step-by-Step BFS

1. Depth 0: Start with (0, 0). Not the goal. Enqueue successors:
• Fill A: (4, 0).
• Fill B: (0, 3).
Frontier: [(4, 0), (0, 3)].
2. Depth 1: Dequeue (4, 0). Not the goal. Successors:
• Fill A: (4, 0) (already explored, skip).
• Empty A: (0, 0) (already explored, skip).
• Pour A to B: (1, 3).
Frontier: [(0, 3), (1, 3)].
3. Dequeue (0, 3). Not the goal. Successors:
• Fill B: (0, 3) (already explored, skip).
• Empty B: (0, 0) (already explored, skip).
• Pour B to A: (3, 0).
Frontier: [(1, 3), (3, 0)].
4. Depth 2: Dequeue (1, 3). Not the goal. Successors:
• Empty B: (1, 0).
• Pour B to A: (4, 0) (already explored, skip).
Frontier: [(3, 0), (1, 0)].
5. Dequeue (3, 0). Not the goal. Successors:
• Fill A: (4, 0) (already explored, skip).
• Empty A: (0, 0) (already explored, skip).
• Pour A to B: (0, 3) (already explored, skip).
Frontier: [(1, 0)].
6. Depth 3: Dequeue (1, 0). Not the goal. Successors:
• Fill A: (4, 0) (already explored, skip).
• Empty A: (0, 0) (already explored, skip).
• Pour A to B: (0, 1).
Frontier: [(0, 1)].
7. Depth 4: Dequeue (0, 1). Not the goal. Successors:
• Fill A: (4, 1).
• Fill B: (0, 3) (already explored, skip).
• Empty B: (0, 0) (already explored, skip).
• Pour B to A: (1, 0) (already explored, skip).
CHAPTER 3. PROBLEM SOLVING 33

Frontier: [(4, 1)].


8. Depth 5: Dequeue (4, 1). Not the goal. Successors:
• Fill A: (4, 1) (already explored, skip).
• Empty A: (0, 1) (already explored, skip).
• Pour A to B: (2, 3).
Frontier: [(2, 3)].
9. Depth 6: Dequeue (2, 3). Goal reached (𝑎 = 2). Path:

(0, 0) → (4, 0) → (1, 3) → (1, 0) → (0, 1) → (4, 1) → (2, 3).

Example 3: Maze Pathfinding

Consider a 4x4 grid maze with the agent starting at (0,0) and aiming for (3,3). Walls block some
paths (e.g., at (1,1), (2,1)).

Problem Setup

• States: Grid coordinates (𝑥, 𝑦), where 𝑥, 𝑦 ∈ [0, 3].


• Initial State: (0, 0).
• Goal State: (3, 3).
• Actions: Move up, down, left, right (if not blocked by walls or grid boundaries).
• Path Cost: Each move costs 1.

Step-by-Step BFS

Assume walls at (1,1), (2,1). Grid:


𝑆 . . .
. W . .
. W . .
. . . 𝐺
(S = start, G = goal, W = wall, . = open)

1. Depth 0: Start at (0, 0). Not the goal. Successors:


• Right: (0, 1).
• Down: (1, 0).
Frontier: [(0, 1), (1, 0)].
2. Depth 1:
• Dequeue (0, 1). Not the goal. Successors:
CHAPTER 3. PROBLEM SOLVING 34

• Right: (0, 2).


• Down: (1, 1) (wall, skip).
• Left: (0, 0) (explored, skip).
• Dequeue (1, 0). Not the goal. Successors:
• Up: (0, 0) (explored, skip).
• Down: (2, 0).
• Right: (1, 1) (wall, skip).
Frontier: [(0, 2), (2, 0)].
3. Depth 2:
• Dequeue (0, 2). Not the goal. Successors:
• Right: (0, 3).
• Left: (0, 1) (explored, skip).
• Dequeue (2, 0). Not the goal. Successors:
• Up: (1, 0) (explored, skip).
• Down: (3, 0).
• Right: (2, 1) (wall, skip).
Frontier: [(0, 3), (3, 0)].
4. Depth 3:
• Dequeue (0, 3). Not the goal. Successors:
• Down: (1, 3).
• Left: (0, 2) (explored, skip).
• Dequeue (3, 0). Not the goal. Successors:
• Up: (2, 0) (explored, skip).
• Right: (3, 1).
Frontier: [(1, 3), (3, 1)].
5. Depth 4:
• Dequeue (1, 3). Not the goal. Successors:
• Up: (0, 3) (explored, skip).
• Down: (2, 3).
• Left: (1, 2).
• Dequeue (3, 1). Not the goal. Successors:
• Left: (3, 0) (explored, skip).
• Right: (3, 2).
Frontier: [(2, 3), (1, 2), (3, 2)].
6. Depth 5:
• Dequeue (2, 3). Not the goal. Successors:
CHAPTER 3. PROBLEM SOLVING 35

• Up: (1, 3) (explored, skip).


• Down: (3, 3) (goal!).
Goal reached. Path:

(0, 0) → (0, 1) → (0, 2) → (0, 3) → (1, 3) → (2, 3) → (3, 3).

3.3.2 Uniform Cost Search (UCS)

Uniform Cost Search (UCS) is a blind (uninformed) search algorithm used in Artificial Intelligence
to find the optimal path from a start node to a goal node in a weighted graph or state space. It is an
extension of Breadth-First Search (BFS) that accounts for varying costs of edges, ensuring the path
with the lowest total cost is found. UCS is particularly useful in problems where the cost of actions
varies, such as pathfinding in navigation systems or resource allocation problems.

Concept of Uniform Cost Search

UCS explores nodes in the order of their cumulative path cost from the start node. Unlike BFS,
which assumes uniform edge costs, UCS prioritizes nodes based on the total cost of the path from
the start node to the current node. It uses a priority queue to always expand the node with the lowest
cumulative cost, ensuring optimality in terms of path cost.

Key Features

• Optimality: UCS guarantees the optimal (least-cost) path to the goal in a weighted graph,
provided all edge costs are non-negative.
• Completeness: UCS is complete, meaning it will find a solution if one exists, assuming the
graph is finite and all costs are positive.
• Exploration Strategy: It explores nodes in order of increasing path cost, using a priority
queue.
• Uninformed: UCS does not use heuristic information about the goal, making it a blind search
algorithm.

Algorithm

The UCS algorithm can be described as follows:

Explanation of Steps

• Priority Queue: The priority queue 𝑄 stores nodes with their cumulative path costs 𝑔(𝑛),
where 𝑔(𝑛) is the total cost from the start node to node 𝑛. Nodes are dequeued in order of
increasing 𝑔(𝑛).
CHAPTER 3. PROBLEM SOLVING 36

Algorithm 2 Uniform Cost Search


1: Initialize an empty priority queue 𝑄 and a set of visited nodes 𝑉.
2: Initialize the start node 𝑠 with cumulative cost 𝑔(𝑠) = 0.
3: Insert 𝑠 into 𝑄 with priority 𝑔(𝑠).
4: while 𝑄 is not empty do
5: Remove the node 𝑛 with the lowest 𝑔(𝑛) from 𝑄.
6: if 𝑛 is the goal node then
7: Return the path to 𝑛.
8: end if
9: if 𝑛 is not in 𝑉 then
10: Add 𝑛 to 𝑉.
11: for each neighbor 𝑚 of 𝑛 do
12: Compute the cost 𝑔(𝑚) = 𝑔(𝑛) + cost(𝑛, 𝑚).
13: Insert 𝑚 into 𝑄 with priority 𝑔(𝑚).
14: end for
15: end if
16: end while
17: Return failure if no solution is found.

• Visited Set: The set 𝑉 tracks explored nodes to avoid cycles and redundant exploration.
• Cost Function: The cost of a path to a node 𝑚 through node 𝑛 is computed as 𝑔(𝑚) =
𝑔(𝑛) + cost(𝑛, 𝑚), where cost(𝑛, 𝑚) is the edge cost between 𝑛 and 𝑚.
• Optimality Check: When a node is dequeued, its cost is the lowest possible for that node,
ensuring the first time the goal is reached, the path is optimal.

Properties of UCS

• Completeness: Guaranteed if the graph is finite and all edge costs are positive (cost(𝑛, 𝑚) > 0).
• Optimality: Finds the least-cost path if all edge costs are non-negative (cost(𝑛, 𝑚) ≥ 0).
• Time Complexity: 𝑂 (𝑏 1+⌊𝐶/𝜖⌋ ), where 𝑏 is the branching factor, 𝐶 is the cost of the optimal
path, and 𝜖 is the minimum edge cost. In practice, it can be exponential in the worst case.
• Space Complexity: 𝑂 (𝑏 1+⌊𝐶/𝜖⌋ ), as it stores all nodes in the priority queue.

Example 1: Pathfinding in a Weighted Graph

Consider a graph representing cities and roads with distances as edge costs. The goal is to find the
shortest path from city 𝑆 (Start) to city 𝐺 (Goal).

Graph Description

The graph has the following nodes and edges with costs:

• 𝑆 → 𝐴: Cost = 2
CHAPTER 3. PROBLEM SOLVING 37

• 𝑆 → 𝐵: Cost = 5
• 𝐴 → 𝐵: Cost = 1
• 𝐴 → 𝐺: Cost = 6
• 𝐵 → 𝐺: Cost = 3

2 6
S A G

5 1
3

Step-by-Step Execution

1. Initialize: 𝑄 = [(𝑆, 0)], 𝑉 = {}.


2. Dequeue 𝑆 (𝑔(𝑆) = 0). Add 𝑆 to 𝑉. Expand 𝑆:
• 𝐴: 𝑔( 𝐴) = 0 + 2 = 2
• 𝐵: 𝑔(𝐵) = 0 + 5 = 5
𝑄 = [( 𝐴, 2), (𝐵, 5)].
3. Dequeue 𝐴 (𝑔( 𝐴) = 2). Add 𝐴 to 𝑉. Expand 𝐴:
• 𝐵: 𝑔(𝐵) = 2 + 1 = 3
• 𝐺: 𝑔(𝐺) = 2 + 6 = 8
𝑄 = [(𝐵, 3), (𝐵, 5), (𝐺, 8)].
4. Dequeue 𝐵 (𝑔(𝐵) = 3). Add 𝐵 to 𝑉. Expand 𝐵:
• 𝐺: 𝑔(𝐺) = 3 + 3 = 6
𝑄 = [(𝐺, 6), (𝐵, 5), (𝐺, 8)].
5. Dequeue 𝐺 (𝑔(𝐺) = 6). 𝐺 is the goal. Return path 𝑆 → 𝐵 → 𝐺 with cost 6.

Result

The optimal path is 𝑆 → 𝐵 → 𝐺 with a total cost of 6. Note that the path 𝑆 → 𝐴 → 𝐺 has a cost
of 8, which is suboptimal.

Example 2: Puzzle Solving (8-Puzzle)

UCS can also be applied to state-space search problems, such as the 8-puzzle, where the goal is to
rearrange tiles from an initial configuration to a goal configuration. Each move (sliding a tile into
the blank space) has a cost based on the tile moved.
CHAPTER 3. PROBLEM SOLVING 38

Problem Description

Consider an 8-puzzle with the following initial and goal states:

   
1 2 3 1 2 3
   
   
Initial State: 4 0 5 , Goal State: 4 5 8
   
   
6 7 8 6 7 0
   
Each move’s cost is equal to the number on the tile moved (e.g., moving tile 5 costs 5).

Step-by-Step Execution

1. Initialize: Start with the initial state, 𝑔(initial) = 0. 𝑄 = [(initial, 0)], 𝑉 = {}.
2. Dequeue initial state. Possible moves: Move tile 4 (cost 4) or tile 5 (cost 5) into the blank
space. New states:
 
1 2 3
 
• State 1 (move 4 left): 0 4 5 , 𝑔 = 4.
 
 
 
6 7 8
 
 
1 2 3
 
• State 2 (move 5 right): 4 5 0 , 𝑔 = 5.
 
 
 
6 7 8
 
𝑄 = [(State 1, 4), (State 2, 5)].
3. Dequeue State 1 (𝑔 = 4). Expand to new states (e.g., move tile 1, 4, or 6). Continue exploring.
4. Eventually, State 2 leads to the goal state by moving tile 8 (cost 8): 𝑔 = 5 + 8 = 13.

Result

The optimal sequence is: Move tile 5 right (cost 5), then move tile 8 down (cost 8). Total cost = 13.

Applications of UCS

• Pathfinding: Finding the shortest route in navigation systems (e.g., GPS).


• Network Routing: Determining the least-cost path in communication networks.
• Puzzle Solving: Solving problems like the 8-puzzle or sliding tile puzzles.
• Robotics: Planning optimal paths for robots in environments with varying costs.
CHAPTER 3. PROBLEM SOLVING 39

Advantages and Limitations

Advantages

• Guarantees the optimal solution in terms of path cost.


• Applicable to any weighted graph with non-negative edge costs.

Limitations

• High time and space complexity in large graphs or state spaces.


• Does not use heuristic information, making it less efficient than informed search algorithms
like A* for large problems.

3.3.3 Depth-First Search (DFS)

Depth-First Search (DFS) is a graph traversal algorithm that explores a path as deep as possible
before backtracking. It uses a stack (either explicitly or via recursion) to keep track of the nodes to
visit next.

Algorithm Steps

Algorithm 3 Depth-First Search (DFS) for AI


1: Initialize an empty stack 𝑆 and a set of visited nodes 𝑉.
2: Initialize the start node 𝑠.
3: Push 𝑠 onto 𝑆.
4: while 𝑆 is not empty do
5: Pop the top node 𝑛 from 𝑆.
6: if 𝑛 is the goal node then
7: Return the path to 𝑛.
8: end if
9: if 𝑛 is not in 𝑉 then
10: Add 𝑛 to 𝑉.
11: for each neighbor 𝑚 of 𝑛 do
12: Push 𝑚 onto 𝑆.
13: end for
14: end if
15: end while
16: Return failure if no solution is found.

Key Characteristics

• Strategy: Dive deep into one branch before exploring alternatives.


• Memory: Requires less memory than BFS; stores only a path from root to leaf.
CHAPTER 3. PROBLEM SOLVING 40

• Completeness: Not complete in infinite graphs or with cycles.


• Optimality: Not optimal — it does not guarantee the shortest path.

Example: 4x4 Maze

𝑆 . . .
. W . .
. W . .
. . . 𝐺

Walls: (1,1), (2,1)


Start: (0, 0)
Goal: (3, 3)
Direction order: Right → Down → Left → Up
DFS Path (one possible path):

(0, 0) → (0, 1) → (0, 2) → (0, 3) → (1, 3) → (2, 3) → (3, 3)

DFS Example: Step-by-Step

Graph Structure

B C

E F
D

DFS Setup

• Start Node: A
CHAPTER 3. PROBLEM SOLVING 41

• Goal Node: G
• Traversal Order: Alphabetical (e.g., B before C, E before F)
• Data Structure: Stack

Step-by-Step Execution

1. Start at A. Stack: [ 𝐴]; Visited: ∅


2. Pop A. Not goal. Add to visited: {𝐴}. Neighbors (B, C) → push in reverse order: Stack:
[𝐶, 𝐵]
3. Pop B. Not goal. Add to visited: {𝐴, 𝐵}. Neighbor: D → push. Stack: [𝐶, 𝐷]
4. Pop D. Not goal. Add to visited: {𝐴, 𝐵, 𝐷}. No unvisited neighbors. Stack: [𝐶]
5. Pop C. Not goal. Add to visited: {𝐴, 𝐵, 𝐷, 𝐶}. Neighbors: E, F → push in reverse order.
Stack: [𝐹, 𝐸]
6. Pop E. Not goal. Add to visited: {𝐴, 𝐵, 𝐷, 𝐶, 𝐸 }. No unvisited neighbors. Stack: [𝐹]
7. Pop F. Not goal. Add to visited: {𝐴, 𝐵, 𝐷, 𝐶, 𝐸, 𝐹}. Neighbor: G → push. Stack: [𝐺]
8. Pop G. Goal found! Path ends here.

Result

One valid DFS path from A to G:


𝐴→𝐶→𝐹 →𝐺

Notes

• DFS explores deeply before backtracking.


• Order of neighbor expansion affects path found.
• DFS does not always find the shortest path.

3.3.4 Depth-Limited Search (DLS)

Depth-Limited Search (DLS) is a variation of Depth-First Search that limits the depth of the search
tree. It explores paths up to a depth limit ℓ.
Key idea: Prevents infinite loops in deep or infinite graphs by stopping exploration beyond a given
depth.

Algorithm Overview

• Perform DFS but abandon any path once it exceeds depth ℓ.


• If the goal is not found within ℓ levels, return failure or cutoff.
CHAPTER 3. PROBLEM SOLVING 42

Example Graph

B C

D E

Problem Setup

• Start node: A
• Goal node: G
• Depth limit: ℓ = 3
• Expansion order: Left to right (e.g., B before C)

Step-by-Step Execution with Limit ℓ = 3

1. Depth 0: Start at A Stack: [( 𝐴, 0)], Visited: ∅


2. Depth 1: Pop A (depth 0), not goal Push children B and C with depth 1 Stack: [(𝐶, 1), (𝐵, 1)],
Visited: {𝐴}
3. Depth 2: Pop B (depth 1), not goal Push D (child of B) with depth 2 Stack: [(𝐶, 1), (𝐷, 2)],
Visited: {𝐴, 𝐵}
4. Pop D (depth 2), not goal No children → backtrack Stack: [(𝐶, 1)], Visited: {𝐴, 𝐵, 𝐷}
5. Depth 3: Pop C (depth 1), not goal Push E (child of C) with depth 2 Stack: [(𝐸, 2)], Visited:
{𝐴, 𝐵, 𝐷, 𝐶}
6. Pop E (depth 2), not goal Push F (child of E) with depth 3 Stack: [(𝐹, 3)], Visited:
{𝐴, 𝐵, 𝐷, 𝐶, 𝐸 }
CHAPTER 3. PROBLEM SOLVING 43

7. Pop F (depth 3), not goal Push G (child of F) would require depth 4 Cutoff: depth limit
exceeded → G not explored Stack: ∅
8. Search ends. Goal not found within depth 3.

Outcome

DLS with ℓ = 3 fails to find goal node G because it is at depth 4.

Key Properties

• Complete? No (if goal is deeper than limit)


• Optimal? No
• Time Complexity: 𝑂 (𝑏 ℓ )
• Space Complexity: 𝑂 (ℓ)

When to Use

• When graph depth is known or can be reasonably limited


• To avoid infinite descent in cyclic or infinite graphs
• Basis for Iterative Deepening Search (IDS)

3.3.5 Iterative Deepening Search (IDS)

What is IDS?

Iterative Deepening Search (IDS) is a search algorithm that performs repeated depth-limited
searches with increasing depth limits.
It combines:

• The low memory usage of Depth-First Search


• The completeness and optimality of Breadth-First Search (for uniform step costs)

How IDS Works

1. Set initial depth limit ℓ = 0.


2. Perform a Depth-Limited Search (DLS) with the current depth limit.
3. If the goal is found, return the solution.
4. Otherwise, increment ℓ and repeat the process.

Repeat DLS until the goal is found or maximum depth is reached.


CHAPTER 3. PROBLEM SOLVING 44

Example Graph

B C

D E

Goal: Find node G starting from node A.

Assumptions

• Expansion order: Left to right


• Goal: G is at depth 4

Step-by-Step Iterations

Iteration 0 (limit = 0): Only node A is explored. Goal not found.


Iteration 1 (limit = 1): Explores A → B, A → C
Goal not found.
Iteration 2 (limit = 2): Explores:
• A→B→D
• A→C→E
Goal not found.
Iteration 3 (limit = 3): Explores:
• A→B→D
• A→C→E→F
Goal not found.
CHAPTER 3. PROBLEM SOLVING 45

Iteration 4 (limit = 4): Explores:


• A→B→D
• A→C→E→F→G
Goal found at depth 4.

Final Path

𝐴→𝐶→𝐸 →𝐹 →𝐺

Key Properties of IDS

• Complete: Yes
• Optimal: Yes (if step costs are uniform)
• Time Complexity: 𝑂 (𝑏 𝑑 ), same as BFS
• Space Complexity: 𝑂 (𝑑), same as DFS
• Tradeoff: Repeats nodes in early iterations but saves memory

Why Use IDS?

• When depth of solution is unknown


• When memory is constrained (compared to BFS)
• To combine the benefits of DFS and BFS

3.3.6 Bidirectional Search

What is Bidirectional Search?

Bidirectional Search is an algorithm to find the shortest path between a start and goal node by
simultaneously performing two searches:

• One forward search from the start node


• One backward search from the goal node

The algorithm stops when the two searches meet in the middle.

Why Use It?

• Reduces time complexity from 𝑂 (𝑏 𝑑 ) to 𝑂 (𝑏 𝑑/2 )


• More efficient for large graphs
CHAPTER 3. PROBLEM SOLVING 46

Example Graph

A B C

G D E

Goal

Find the shortest path from node A (Start) to node E (Goal).

Step-by-Step Execution

Forward Search from A:

• Level 0: A
• Level 1: B, G
• Level 2: C, D, F

Backward Search from E:

• Level 0: E
• Level 1: C, F
• Level 2: B, G

Meeting Point: Node F is discovered from both directions at level 2.

Final Path

From forward search: A → G → F


From backward search: E ← F

Full Path: 𝐴 → 𝐺 → 𝐹 → 𝐸
CHAPTER 3. PROBLEM SOLVING 47

Key Properties

• Complete? Yes (if both searches are BFS)


• Optimal? Yes (for uniform-cost graphs)
• Time Complexity: 𝑂 (𝑏 𝑑/2 )
• Space Complexity: 𝑂 (𝑏 𝑑/2 )

Challenges

• Requires ability to search backward from the goal


• Must efficiently check when frontiers intersect
• Managing two queues and visited sets

When to Use

• When both start and goal are known


• In large state spaces where reducing depth is critical

3.4 Informed Search


Informed search algorithms, also known as heuristic search algorithms, leverage domain-specific
knowledge to guide the search process toward a goal state more efficiently than uninformed search
methods. This knowledge is encapsulated in a heuristic function, which estimates the cost from a
given state to the goal.

Definition

An informed search algorithm uses a heuristic function ℎ(𝑛) to estimate the cost of reaching the
goal from a node 𝑛. This allows the algorithm to prioritize exploring nodes that appear closer to the
goal, reducing the search space compared to uninformed methods like breadth-first search (BFS) or
depth-first search (DFS).

• Heuristic Function: ℎ(𝑛) estimates the cheapest path cost from node 𝑛 to the goal. For
example, in a route-finding problem, ℎ(𝑛) might be the straight-line distance (SLD) from node
𝑛 to the goal.
• Efficiency: By prioritizing promising paths, informed search reduces the number of nodes
explored.
• Examples: Greedy Best-First Search (GBFS), A* Search, Hill Climbing, Beam Search.
CHAPTER 3. PROBLEM SOLVING 48

Key Characteristics

Informed search algorithms typically maintain a priority queue to store nodes, where the priority
is determined by the heuristic function or a combination of heuristic and path cost. The general
process is:

1. Initialize the priority queue with the start node.


2. Select the node with the lowest priority (e.g., lowest h(n) in GBFS or lowest
f(n) = g(n) + h(n) in A*).
3. Expand the node, generate successors, and add them to the queue with their priorities.
4. Repeat until the goal is reached or the queue is empty.

Heuristic Function

The heuristic function h(n) is problem-specific and critical to the algorithm’s performance. Desirable
properties include:

• Admissibility: ℎ(𝑛) ≤ ℎ∗ (𝑛), where ℎ∗ (𝑛) is the true cost to the goal. An admissible heuristic
never overestimates the cost, ensuring optimality in algorithms like A*.
• Consistency (Monotonicity): For any nodes 𝑛 and 𝑛′, where 𝑛′ is a successor of 𝑛, ℎ(𝑛) ≤
𝑐(𝑛, 𝑛′) + ℎ(𝑛′), where 𝑐(𝑛, 𝑛′) is the cost of moving from 𝑛 to 𝑛′. Consistency ensures that
the heuristic is well-behaved across transitions.
• Informativeness: A heuristic closer to ℎ∗ (𝑛) reduces the search space more effectively.

Example Heuristics

• Route Finding: Straight-line distance (SLD) to the goal. For a city 𝑛 and goal 𝑔, ℎ(𝑛) =
SLD(𝑛, 𝑔). This is admissible because the SLD is always less than or equal to the actual path
distance (triangle inequality).
• 8-Puzzle:
• Manhattan Distance: Sum of the distances (in rows and columns) each tile is from its
goal position.
• Misplaced Tiles: Number of tiles not in their goal position.

Key Informed Search Algorithms

We describe two prominent informed search algorithms: Greedy Best-First Search (GBFS) and A*
Search, with detailed pseudocode and examples.
CHAPTER 3. PROBLEM SOLVING 49

3.4.1 Greedy Best First Search

Greedy Best First Search is a graph traversal algorithm that uses a heuristic function to decide the
order in which nodes are explored.
f(n) = h(n)
where:

• h(n): estimated cost from node n to the goal.

Note: It does not consider the path cost from the start node to the current node, unlike A*.

Algorithm Steps
1. Initialize the open list with the start node.
2. While the open list is not empty:
• Choose the node 𝑛 with the lowest h(n) value.
• If 𝑛 is the goal node, return the path.
• Else, expand 𝑛 and add its neighbors to the open list.
3. If the open list is empty and the goal is not found, return failure.

Example

B C

D E F

Heuristic values ℎ(𝑛):


CHAPTER 3. PROBLEM SOLVING 50

Node ℎ(𝑛)
A 6
B 4
C 3
D 4
E 2
F 1
G 0

Goal: Find path from A to G.


Path: A → C → F → G

Properties
• Completeness: No (can get stuck in loops)
• Optimality: No
• Time Complexity: 𝑂 (𝑏 𝑚 )
• Space Complexity: 𝑂 (𝑏 𝑚 )

Example 2

Consider the following graph where the task is to find a path from city S (Start) to city G (Goal).

A D
1 2 2 2

S C G

4 2 3 1
B E

The numbers on edges represent actual costs between nodes. The heuristic values ℎ(𝑛) (straight-line
distances to goal) are given as:

ℎ(𝑆) = 7, ℎ( 𝐴) = 6, ℎ(𝐵) = 5, ℎ(𝐶) = 4, ℎ(𝐷) = 2, ℎ(𝐸) = 1, ℎ(𝐺) = 0


CHAPTER 3. PROBLEM SOLVING 51

Algorithm Steps

1. Initialize: Open list = {S}, Closed list = {}.


2. Step 1: Pick S (h=7), expand → add A(h=6), B(h=5) to Open.
3. Step 2: Choose node with lowest h → B(h=5).
4. Step 3: Expand B → add C(h=4) to Open.
5. Step 4: Choose C(h=4) → expand → add D(h=2), E(h=1).
6. Step 5: Choose E(h=1) → expand → add G(h=0).
7. Step 6: Choose G(h=0) → Goal found.

Search Trace Table

Step Open List (h values) Chosen Node Closed List


1 S(7) S S
2 A(6), B(5) B S, B
3 A(6), C(4) C S, B, C
4 A(6), D(2), E(1) E S, B, C, E
5 A(6), D(2), G(0) G S, B, C, E, G

Result

The path found by Greedy Best-First Search is:

𝑆→𝐵→𝐶→𝐸 →𝐺

This path is found quickly because GBFS prioritizes the smallest heuristic value at each step.
However, note that this path is not guaranteed to be the shortest in terms of actual cost.

Observations

• GBFS is fast because it ignores the cost so far 𝑔(𝑛) and only considers ℎ(𝑛).
• It may find suboptimal paths because it is purely greedy.
• Time complexity depends heavily on the accuracy of ℎ(𝑛).
• If ℎ(𝑛) is admissible and consistent, using A* is usually better for optimality.
CHAPTER 3. PROBLEM SOLVING 52

3.4.2 A* Search Algorithm

A* (pronounced “A star”) is an informed search algorithm used for pathfinding and graph traversal.
It is widely used because it is both complete (it will always find a solution if one exists) and optimal
(it will find the least-cost path), provided the heuristic is admissible.

A* combines:

• The cost to reach the current node: 𝑔(𝑛)


• The estimated cost to reach the goal from that node: ℎ(𝑛)

The evaluation function:


𝑓 (𝑛) = 𝑔(𝑛) + ℎ(𝑛)

Where:

• 𝑔(𝑛): cost from start to node 𝑛


• ℎ(𝑛): heuristic estimate from 𝑛 to goal
• 𝑓 (𝑛): total estimated cost of the cheapest solution through 𝑛

If ℎ(𝑛) is:

• Admissible: never overestimates the true cost to reach the goal


• Consistent (Monotonic): satisfies ℎ(𝑛) ≤ 𝑐(𝑛, 𝑛′) + ℎ(𝑛′) for every edge (𝑛, 𝑛′)

then A* is guaranteed to find an optimal solution.

Pseudocode:

function A_Star(start, goal):


open_set = {start}
g(start) = 0
f(start) = h(start)

while open_set is not empty:


current = node in open_set with lowest f value
if current == goal:
return reconstruct_path(current)

remove current from open_set


add current to closed_set

for each neighbor of current:


tentative_g = g(current) + cost(current, neighbor)
if neighbor in closed_set and tentative_g >= g(neighbor):
CHAPTER 3. PROBLEM SOLVING 53

continue
if neighbor not in open_set or tentative_g < g(neighbor):
parent[neighbor] = current
g(neighbor) = tentative_g
f(neighbor) = g(neighbor) + h(neighbor)
if neighbor not in open_set:
add neighbor to open_set
return failure

Example Problem: Find the shortest path from S to G.

A D
1 2 2 2

S C G

4 2 3 1
B E

Heuristic values ℎ(𝑛) (straight-line estimate to goal):

ℎ(𝑆) = 7, ℎ( 𝐴) = 6, ℎ(𝐵) = 5, ℎ(𝐶) = 4, ℎ(𝐷) = 2, ℎ(𝐸) = 1, ℎ(𝐺) = 0

Step-by-step Execution:

1. Start at 𝑆: 𝑔 = 0, ℎ = 7, 𝑓 = 7. Open list: {S}


2. Expand 𝑆:
• 𝐴: 𝑔 = 1, ℎ = 6, 𝑓 = 7
• 𝐵: 𝑔 = 4, ℎ = 5, 𝑓 = 9
Open list: {A(7), B(9)}
3. Choose 𝐴 (lowest 𝑓 ):
• 𝐶 via A: 𝑔 = 3, ℎ = 4, 𝑓 = 7
Open list: {C(7), B(9)}
4. Choose 𝐶 (lowest 𝑓 ):
• 𝐷: 𝑔 = 5, ℎ = 2, 𝑓 = 7
• 𝐸: 𝑔 = 6, ℎ = 1, 𝑓 = 7
Open list: {D(7), E(7), B(9)}
5. Choose 𝐷 (tie-breaker arbitrary):
• 𝐺 via D: 𝑔 = 7, ℎ = 0, 𝑓 = 7
CHAPTER 3. PROBLEM SOLVING 54

Goal found!

Path:
𝑆→ 𝐴→𝐶→𝐷→𝐺

Total cost: 1 + 2 + 2 + 2 = 7

Properties of A*:

• Complete: Yes, if branching factor is finite and step costs are positive.
• Optimal: Yes, if heuristic is admissible and consistent.
• Time Complexity: Exponential in worst case; depends on heuristic quality.
• Space Complexity: High (keeps all generated nodes in memory).

Advantages:

• Finds optimal path.


• Flexible — performance depends on ℎ(𝑛) quality.

Disadvantages:

• High memory usage.


• Slow for large graphs with weak heuristics.

3.4.3 AO* Algorithm

The AO* (And-Or Star) algorithm is a best-first search method for solving problems represented
as AND–OR graphs. It generalizes the A* search algorithm to handle problem decomposition where
some tasks must be solved together (AND) and others require choosing between alternatives (OR).

Motivation Many AI problems can be broken into subproblems that are either:

• OR nodes: Only one of the successors needs to be solved to solve the parent.
• AND nodes: All successors must be solved to solve the parent.

AO* searches for an optimal solution graph—a minimal-cost subgraph connecting the start node to
solved terminal states.

Example of AND–OR Graph

Start

A G3

G1 G2
CHAPTER 3. PROBLEM SOLVING 55

Legend: Green = OR node, Blue = AND node, White = terminal goal.

Algorithm Steps

1. Start with the initial node in the graph.


2. Expand the most promising node according to 𝑓 (𝑛) = 𝑔(𝑛) + ℎ(𝑛), where:
• 𝑔(𝑛) = cost from start to node 𝑛
• ℎ(𝑛) = heuristic estimate of cost to goal
3. For OR nodes, generate alternative successors; cost is the minimum among them.
4. For AND nodes, generate all required subproblems; cost is the sum of their costs.
5. Update heuristic values for ancestors after each expansion.
6. If all children of an AND node are solved, mark it solved; if any OR alternative is solved, mark
the OR node solved.
7. Repeat until the start node is solved.

Algorithm 4 AO* Search


1: function AO*(𝑛)
2: if 𝑛 is solved then
3: return
4: end if
5: if 𝑛 not expanded then
6: Expand(𝑛)
7: UpdateHeuristic(𝑛)
8: return
9: end if
10: for all children 𝑐 of 𝑛 do
11: AO*(𝑐)
12: end for
13: UpdateHeuristic(𝑛)
14: if 𝑛 is solved then
15: Mark 𝑛 as solved
16: end if
17: end function

Pseudocode

Advantages

• Handles complex problem decomposition with AND and OR structures.


• Finds optimal solution if heuristic is admissible.
• Avoids re-expanding solved subproblems in a graph.
CHAPTER 3. PROBLEM SOLVING 56

Disadvantages

• Requires explicit AND–OR graph representation.


• Can consume significant memory for large graphs.
• Performance depends heavily on heuristic quality.

Applications

• Hierarchical planning in robotics


• Natural language parsing with grammar rules
• Theorem proving with subgoals
• Expert systems for fault diagnosis

3.5 Depth-First Branch and Bound (DFBnB)


Depth-First Branch and Bound (DFBnB) is a search algorithm that combines the memory efficiency
of Depth-First Search (DFS) with the pruning power of the Branch and Bound technique. It is
often used in combinatorial optimization problems such as the Traveling Salesman Problem (TSP),
Knapsack, or scheduling.
The key idea is:

• Explore nodes in a depth-first manner (low memory usage).


• Keep track of the best solution cost found so far, called the upper bound.
• At any node, compute a lower bound on the cost of completing the solution.
• If (current cost + lower bound) ≥ upper bound, prune that branch.

This ensures that only promising branches are expanded, reducing the search space.
CHAPTER 3. PROBLEM SOLVING 57

Algorithm

Algorithm 5 Depth-First Branch and Bound (DFBnB)


1: 𝑏𝑒𝑠𝑡𝐶𝑜𝑠𝑡 ← +∞
2: 𝑏𝑒𝑠𝑡𝑆𝑜𝑙𝑢𝑡𝑖𝑜𝑛 ← ∅
3: function DFBnB(𝑛𝑜𝑑𝑒, 𝑔)
4: if 𝑛𝑜𝑑𝑒 is a goal then
5: if 𝑔 < 𝑏𝑒𝑠𝑡𝐶𝑜𝑠𝑡 then
6: 𝑏𝑒𝑠𝑡𝐶𝑜𝑠𝑡 ← 𝑔
7: 𝑏𝑒𝑠𝑡𝑆𝑜𝑙𝑢𝑡𝑖𝑜𝑛 ← current path
8: end if
9: return
10: end if
11: for each child in successors(𝑛𝑜𝑑𝑒) ordered by lower bound do
12: 𝐿𝐵 ← 𝑔 + 𝑐𝑜𝑠𝑡 (𝑛𝑜𝑑𝑒, 𝑐ℎ𝑖𝑙𝑑) + ℎ𝑒𝑢𝑟𝑖𝑠𝑡𝑖𝑐𝐿𝐵(𝑐ℎ𝑖𝑙𝑑)
13: if 𝐿𝐵 < 𝑏𝑒𝑠𝑡𝐶𝑜𝑠𝑡 then
14: DFBnB(𝑐ℎ𝑖𝑙𝑑, 𝑔 + 𝑐𝑜𝑠𝑡 (𝑛𝑜𝑑𝑒, 𝑐ℎ𝑖𝑙𝑑))
15: else
16: prune this child
17: end if
18: end for
19: end function

Example: Traveling Salesman Problem (TSP)

Consider four cities {𝐴, 𝐵, 𝐶, 𝐷} with the following costs:

𝐴 𝐵 𝐶 𝐷
𝐴 0 2 9 3
𝐵 2 0 6 4
𝐶 9 6 0 1
𝐷 3 4 1 0

Step 1: Initial tour. Start at 𝐴, go depth-first: 𝐴 → 𝐵 → 𝐶 → 𝐷 → 𝐴. Cost = 𝐴𝐵+𝐵𝐶+𝐶𝐷+𝐷𝐴 =


2 + 6 + 1 + 3 = 12. So 𝑏𝑒𝑠𝑡𝐶𝑜𝑠𝑡 = 12.
Step 2: Bounding. Explore 𝐴 → 𝐶. Partial cost = 9. Lower bound estimate of remaining (using
Minimum Spanning Tree of unvisited {𝐵, 𝐷}) = 5. So 𝐿𝐵 = 9 + 5 = 14 ≥ 12, prune branch 𝐴 → 𝐶.
Step 3: Continue DFS. Explore 𝐴 → 𝐷. Partial cost = 3. Remaining bound = 7, so 𝐿𝐵 = 10 < 12,
continue exploring.
This pruning ensures we avoid exploring useless branches.
CHAPTER 3. PROBLEM SOLVING 58

Example: 0/1 Knapsack

Suppose capacity 𝑊 = 10 and items:

{(𝑤 = 6, 𝑣 = 30), (𝑤 = 3, 𝑣 = 14), (𝑤 = 4, 𝑣 = 16), (𝑤 = 2, 𝑣 = 9)}.

DFS explores include/exclude decisions:

• Partial solution: pick item 1 (𝑤 = 6, 𝑣 = 30), capacity left = 4.


• Lower bound: add best fractional item (knapsack relaxation) ⇒ max additional = 14.
• So 𝐿𝐵 = 30 + 14 = 44.
• If 𝑏𝑒𝑠𝑡𝐶𝑜𝑠𝑡 (best value so far) ≥ 44, prune.

Thus bounding prevents unnecessary exploration of suboptimal subsets.

Properties

• Optimality: Always finds the optimal solution if lower bounds are valid.
• Memory: Requires only 𝑂 (𝑑) stack space, where 𝑑 is search depth.
• Efficiency: Strongly depends on quality of lower bound heuristics.

Applications

• Traveling Salesman Problem (exact solver for small instances).


• Job scheduling and resource allocation.
• 0/1 Knapsack problem.
• Puzzle solving with lower bound heuristics.

3.6 Game Search


Game search is a specialized subset of informed search designed for adversarial games, where two or
more players make moves to maximize their own payoff while minimizing their opponent’s. Unlike
single-agent informed search (e.g., GBFS, A*), which seeks a goal state, game search navigates
competitive environments like chess or tic-tac-toe, where each player’s move influences the other’s
strategy. It uses an evaluation function, similar to a heuristic, to estimate the value of game states,
guiding decisions toward optimal moves. Game search assumes perfect information (both players
know the game state) and zero-sum outcomes (one player’s gain is the other’s loss).
Imagine two players in a chess match, each planning moves to outsmart the other. The search
explores a game tree where nodes represent game states, edges represent moves, and leaf nodes are
CHAPTER 3. PROBLEM SOLVING 59

evaluated based on outcomes (e.g., +1 for MAX’s win, -1 for MIN’s win, 0 for a draw). The goal
is to select the move that maximizes a player’s chances of winning, assuming the opponent plays
optimally

Types of Game Search Algorithms

According to Russell and Norvig (2010), the primary game search algorithms for two-player,
zero-sum games with perfect information include:

• Minimax Algorithm: Evaluates all possible moves to a fixed depth, assuming optimal play by
both players, and selects the move that maximizes the minimum payoff (best outcome against
the opponent’s best response).
• Alpha-Beta Pruning: An optimization of Minimax that prunes branches of the game tree that
cannot affect the final decision, reducing computation while maintaining optimality.

Other techniques, such as Monte Carlo Tree Search, are used for complex or imperfect-information
games, but Minimax and Alpha-Beta Pruning are the core algorithms for classical AI game playing.

3.6.1 Hill Climbing Algorithm in AI

The "Hill Climbing algorithm" is a foundational local search method used in artificial intelligence
for optimization. Its name comes from the analogy of climbing a hill: starting from an initial
solution, the algorithm iteratively moves in small steps toward neighboring states that improve the
solution, aiming to reach the peak (i.e., the global maximum)

How It Works

1. Initial State: Begin with an arbitrary or randomly selected starting solution.


2. Neighbor Evaluation: Generate and evaluate neighboring solutions using a heuristic function.
3. Move: Transition to the neighbor with a better evaluation value, if one exists.
4. Repeat: Continue until no better neighbor is found—at which point the algorithm converges
to a local maximum or possibly the global maximum :contentReference[oaicite:1]index=1.

Variants of Hill Climbing

• Simple Hill Climbing: Moves to the first better neighbor encountered; fast but prone to getting
stuck in suboptimal peaks.
• Steepest-Ascent Hill Climbing: Evaluates all neighbors and moves to the best one; more
thorough but computationally heavier.
• Stochastic Hill Climbing: Randomly selects among improving neighbors, introducing
variability that may help escape poor local maxima.
CHAPTER 3. PROBLEM SOLVING 60

Challenges and Mitigation

• Local Maximum: The algorithm may stop at a peak that isn’t globally optimal. Random
restarts—restarting from different initial states—help better explore the search space.
• Plateau: Flat regions make it difficult to detect improvement directions. Random jumps
introduce exploration to escape stalling.
• Ridge: Narrow, slanted peaks may mislead the algorithm into suboptimal paths. Multi-
directional search can help navigate these tricky landscapes :contentReference[oaicite:3]index=3.

Applications Hill Climbing is widely applied in:

• Pathfinding (e.g., optimizing GPS routes)


• Hyperparameter Tuning in machine learning models
• Game AI (selecting moves leading to better game positions)
• Scheduling and Resource Allocation (e.g., optimizing workflows and timelines).

Example: Optimize the function 𝑓 (𝑥, 𝑦) = −(𝑥 2 + 𝑦 2 ) (maximize, so negate for minimization,
seeking the global maximum at (0, 0)). Start at (1, 1), with neighbors at steps of 0.5.

• Initial: (1, 1), 𝑓 (1, 1) = −(12 + 12 ) = −2.


• Neighbors: (1.5, 1), (0.5, 1), (1, 1.5), (1, 0.5).
• Values: 𝑓 (1.5, 1) = −3.25, 𝑓 (0.5, 1) = −1.25, 𝑓 (1, 1.5) = −3.25, 𝑓 (0.5, 0.5) = −0.5.
• Move to (0.5, 0.5), 𝑓 = −0.5. Next neighbors: (1, 0.5), (0, 0.5), (0.5, 1), (0.5, 0).
• Values: 𝑓 (1, 0.5) = −1.25, 𝑓 (0, 0.5) = −0.25, 𝑓 (0.5, 1) = −1.25, 𝑓 (0.5, 0) = −0.25.
• Move to (0, 0.5), 𝑓 = −0.25. Next neighbors include (0, 0), 𝑓 (0, 0) = 0. Move to (0, 0), global
maximum.

𝑓 = −2
(1,1)
Move
𝑓 = −0.5
(0.5,0.5)
Move
𝑓 =0
(0,0)

Figure 3.3: Hill Climbing moves from (1,1) to (0,0) (red).

Step-by-Step Process:

1. Start at (1, 1), 𝑓 = −2.


2. Evaluate neighbors: (1.5, 1), 𝑓 = −3.25; (0.5, 1), 𝑓 = −1.25; (1, 1.5), 𝑓 = −3.25; (0.5, 0.5),
𝑓 = −0.5. Move to (0.5, 0.5), 𝑓 = −0.5.
3. Evaluate neighbors: (1, 0.5), 𝑓 = −1.25; (0, 0.5), 𝑓 = −0.25; (0.5, 1), 𝑓 = −1.25; (0.5, 0),
𝑓 = −0.25. Move to (0, 0.5), 𝑓 = −0.25.
CHAPTER 3. PROBLEM SOLVING 61

4. Evaluate neighbors: (0.5, 0.5), 𝑓 = −0.5; (0, 1), 𝑓 = −1; (-0.5, 0.5), 𝑓 = −0.5; (0, 0), 𝑓 = 0.
Move to (0, 0), 𝑓 = 0.
5. No better neighbors. Stop at (0, 0), global maximum.

The diagram (Figure 3.3) shows Hill Climbing moving from (1, 1) to (0, 0), reaching the global
maximum for this simple function.

Fitness / Objective

Global maximum Legend:


greedy hill-climb (may get stuck

rand
random-restart hill-climb
s
ove

om-
Local maximum al m
loc

resta
by
ch
rea

rt c
ps ’t
ste can

lim
edy plateau/ridge
gre

b
Restart
Start
State (search space)
Region containing a local optimum

Figure 3.4: Hill climbing on a fitness landscape. A greedy climb (red) gets stuck at a local maximum,
while random-restart (green) can reach the global maximum.

Practical Notes: Hill Climbing is simple and memory-efficient, suitable for optimization problems
like scheduling or parameter tuning. However, random-restart or stochastic variants are often needed
to escape local maxima .

3.6.2 Minimax Algorithm

Overview: The Minimax algorithm is designed for two-player, zero-sum games with perfect
information, such as tic-tac-toe or chess. It constructs a game tree, evaluates leaf nodes using an
evaluation function, and propagates values upward to choose the optimal move. The maximizing
player (MAX) aims to maximize the payoff, while the minimizing player (MIN) aims to minimize it.
It’s like playing tic-tac-toe, where you choose the move that ensures the best outcome, assuming
your opponent counters optimally.
Mechanics: Minimax explores the game tree to a fixed depth (due to computational limits). At leaf
nodes, an evaluation function assigns values (e.g., +10 for MAX’s win, -10 for MIN’s win, 0 for
a draw). At MAX nodes, it selects the child with the highest value; at MIN nodes, it selects the
lowest. Values propagate up the tree, alternating between MAX and MIN layers, to determine the
best move from the root russell2010artificial.
Properties:

• Completeness: Complete for finite game trees, as it evaluates all possible outcomes.
• Optimality: Optimal against an optimal opponent, as it assumes both players choose the best
move.
CHAPTER 3. PROBLEM SOLVING 62

• Time Complexity: 𝑂 (𝑏 𝑚 ), where 𝑏 is the branching factor (average number of moves) and 𝑚
is the search depth.
• Space Complexity: 𝑂 (𝑏𝑚), as it uses depth-first exploration, storing only the current path.

Limitations:

• Computationally expensive for deep trees or high branching factors (e.g., chess, with 𝑏 ≈ 35).
• Requires a reliable evaluation function for non-terminal states.
• Impractical for real-time games without optimizations like pruning russell2010artificial.

Example: Consider a simplified tic-tac-toe game tree where MAX (X) moves first, and MIN (O)
responds. The tree is evaluated at depth 2, with leaf node values (+10 for MAX win, -10 for MIN
win, 0 for draw).

Root (MAX)
Move A Move B Move C

A (MIN) B (MIN) C (MIN)

3 5 7 -2 0 6

Figure 3.5: Minimax selects Move A, value 3 (red).

Step-by-Step Process:

1. Start at Root (MAX). Explore children: A, B, C (MIN nodes).


2. For A (MIN): Evaluate children (leaf nodes): 3, 5. MIN chooses min(3, 5) = 3.
3. For B (MIN): Evaluate children: 7, -2. MIN chooses min(7, -2) = -2.
4. For C (MIN): Evaluate children: 0, 6. MIN chooses min(0, 6) = 0.
5. At Root (MAX): Choose max(3, -2, 0) = 3 (Move A). Select Move A, value 3.

The diagram (Figure 3.5) shows Minimax selecting Move A, as it guarantees the best worst-case
outcome (value 3) against MIN’s optimal play russell2010artificial.
Algorithm (Pseudocode):

function Minimax(node, depth, maximizingPlayer):


if depth == 0 or node is terminal:
return evaluation_function(node)
if maximizingPlayer:
value = -infinity
for each child in [Link]:
value = max(value, Minimax(child, depth-1, false))
CHAPTER 3. PROBLEM SOLVING 63

return value
else: // minimizingPlayer
value = +infinity
for each child in [Link]:
value = min(value, Minimax(child, depth-1, true))
return value

Practical Notes: Minimax is foundational for game AI but is computationally intensive for large
games like chess. It assumes optimal opponent play, making it robust but slow without optimizations
russell2010artificial.

3.6.3 Alpha-Beta Pruning

The term ‘pruning’ refers to the removal of unnecessary branches and leaves. In Artificial
Intelligence, alpha-beta pruning involves eliminating redundant branches in decision trees. This
algorithm was independently developed by researchers in the 1900s.
Alpha-beta pruning is a search optimization technique that enhances the performance of the minimax
algorithm. The minimax algorithm is a decision-making process commonly applied in two-player,
zero-sum games such as chess. In these games, one player aims to maximize their score while the
opponent seeks to minimize it.
The minimax algorithm recursively explores all possible game states, represented as a tree structure,
and assigns values to leaf nodes based on potential game outcomes. These values are then propagated
up the tree to determine the optimal move. However, as game complexity increases, the number of
possible states grows exponentially, resulting in high computational costs.
Alpha-beta pruning addresses this challenge by reducing the number of nodes evaluated by the
minimax algorithm, pruning branches that cannot influence the final decision. This simplification
enables faster and more efficient evaluations, making it practical for real-time applications like
game-playing AI, where speed and efficiency are critical.

How Alpha-Beta Pruning Works

The core principle of alpha-beta pruning is to avoid evaluating branches of the game tree that
cannot affect the final decision, based on values already discovered during the search. It utilizes two
parameters: alpha and beta.

Alpha and Beta Values

• Alpha: Represents the best (highest) value that the maximizing player (typically the AI) can
guarantee at that point. It serves as a lower bound, initialized to −∞.
• Beta: Represents the best (lowest) value that the minimizing player (the opponent) can
guarantee at that point. It serves as an upper bound, initialized to +∞.
CHAPTER 3. PROBLEM SOLVING 64

The Pruning Process

• As the AI traverses the tree, it tracks alpha and beta values. When evaluating a node, it
compares the node’s value against these bounds.
• If alpha becomes greater than or equal to beta, the current branch will not influence the final
decision, as the opponent will choose a better path. This branch is pruned, and the algorithm
proceeds to the next branch.
• This process allows the algorithm to skip large sections of the tree, significantly reducing the
number of nodes evaluated.

𝛼 = −∞, 𝛽 = +∞

A MAX

B C MIN

D E F G MAX

2 3 5 9 0 1 7 5 Terminal node

Step 1: Calculation for D

𝛼 = −∞, 𝛽 = +∞
A

B C

D3 E

𝛼 = 3, 𝛽 = +∞
2 3
CHAPTER 3. PROBLEM SOLVING 65

Step 2: Forward Value to B

𝛼 = −∞, 𝛽 = +∞
A

𝛼 = −∞, 𝛽 = 3
B C

D3 E

2 3

Step 3: Calculation for E

𝛼 = −∞, 𝛽 = +∞
A

𝛼 = −∞, 𝛽 = 3
B C

D3 E5

2 3 5 9

Step 4: B Value

𝛼 = −∞, 𝛽 = +∞
A

𝛼 = −∞, 𝛽 = 3
B3 C

D3 E5

2 3 5 9
CHAPTER 3. PROBLEM SOLVING 66

Step 5: A Value

𝛼 = −∞, 𝛽 = +∞
A

𝛼 = −∞, 𝛽 = 3 𝛼 = 3, 𝛽 = +∞
B3 C

D3 E5

2 3 5 9

Step 6: Calculation for F

𝛼 = −∞, 𝛽 = +∞
A

𝛼 = −∞, 𝛽 = 3 𝛼 = 3, 𝛽 = +∞
B3 C

D3 E5 F1

2 3 5 9 0 1

Step 7: Calculation for C

𝛼 = −∞, 𝛽 = +∞
A

𝛼 = −∞, 𝛽 = 3 𝛼 = 3, 𝛽 = 1
B3 C1

D3 E5 F1

2 3 5 9 0 1
CHAPTER 3. PROBLEM SOLVING 67

Step 8: G Pruning

𝛼 = −∞, 𝛽 = +∞
A

𝛼 = −∞, 𝛽 = 3 𝛼 = 3, 𝛽 = 1
B3 C1

D3 E5 F1 G

2 3 5 9 0 1 7 5

Step 9: Final Value for A

𝛼 = −∞, 𝛽 = +∞
A3

𝛼 = −∞, 𝛽 = 3 𝛼 = 3, 𝛽 = 1
B3 C1

D3 E5 F1 G

2 3 5 9 0 1 7 5

Working of Alpha-Beta Pruning

1. Initialize the worst-case scenario with 𝛼 = −∞ and 𝛽 = +∞. If 𝛼 ≥ 𝛽, the node is pruned.
2. Since the initial 𝛼 < 𝛽, no pruning occurs. For the MAX player’s turn at node D, compute
𝛼 = max(2, 3) = 3.
3. At node B (MIN’s turn), set 𝛽 = min(3, ∞) = 3. Thus, at node B, 𝛼 = −∞, 𝛽 = 3. Pass these
values to node E.
4. At node E (MAX’s turn), compute 𝛼 = max(−∞, 5) = 5. Now, 𝛼 = 5, 𝛽 = 5. Since 𝛼 ≥ 𝛽,
prune the right successor of node E.
5. Return to node B, then to node A. Update node A’s 𝛼 = max(−∞, 3) = 3. Pass 𝛼 = 3, 𝛽 = +∞
to node C and node F.
6. At node F, compute 𝛼 = max(0, 3) = 3, then compare with the right child (1), so max(3, 1) = 3.
Node F’s value is 1.
CHAPTER 3. PROBLEM SOLVING 68

7. Node F passes value 1 to node C. For MIN’s turn, compute 𝛽 = min(+∞, 1) = 1. At node C,
𝛼 = 3, 𝛽 = 1. Since 𝛼 ≥ 𝛽, prune node C’s successor, node G.
8. Node C returns value 1 to node A. Compute max(1, 3) = 3 at node A.

The resulting tree includes only computed nodes, with an optimal value of 3 for the maximizer.

Key Benefits of Alpha-Beta Pruning in AI


When game trees are complex, alpha-beta pruning offers significant advantages for AI decision-
making algorithms.

1. Improved Efficiency: By pruning unnecessary branches, the algorithm focuses on relevant


parts of the tree, significantly reducing computation time.
2. Reduced Time Complexity: The minimax algorithm’s time complexity is 𝑂 (𝑏 𝑑 ), where 𝑏 is
the branching factor and 𝑑 is the tree depth. Alpha-beta pruning reduces this to 𝑂 (𝑏 𝑑/2 ) in the
best case, enabling deeper tree exploration.
3. Scalability: For complex AI applications like chess or Go, alpha-beta pruning makes exploring
large search spaces feasible, supporting real-time decision-making.

Applications of Alpha-Beta Pruning in AI


Alpha-beta pruning is widely used in AI applications requiring efficient decision tree exploration.

1. Game AI: In strategic games like chess, checkers, or Go, alpha-beta pruning enables AI to
evaluate millions of possible moves in real time. Systems like Stockfish (chess) and AlphaGo
(Go) rely on it as a core component.
2. Autonomous Systems and Robotics: Decision trees guide real-time decisions in robotics for
movement, navigation, and task execution. Alpha-beta pruning accelerates path and strategy
evaluation in dynamic environments.
3. Financial and Optimization Models: AI systems in financial forecasting, portfolio opti-
mization, and supply chain management use decision trees to evaluate scenarios. Alpha-beta
pruning enhances timely decision-making by processing large datasets efficiently.

3.7 Problem Statements


1. Minimax in Tic-Tac-Toe: Consider a 3x3 tic-tac-toe board where MAX (X) moves first. The
current state has X in the center, and MIN (O) can place an O in any empty cell. Construct
a game tree to depth 2, with leaf values (+10 for X win, -10 for O win, 0 for draw). Apply
the Minimax algorithm to choose MAX’s best move. Draw the game tree and show the
step-by-step process.
CHAPTER 3. PROBLEM SOLVING 69

2. Alpha-Beta Pruning: Using the same tic-tac-toe setup as Problem 1, apply Alpha-Beta
Pruning to select MAX’s move. Identify which branches are pruned and explain why. Provide
the step-by-step process and a diagram showing the pruned branches.
3. Hill Climbing Optimization: Optimize the function 𝑓 (𝑥) = −𝑥 2 + 4𝑥 (maximize) using Hill
Climbing. Start at 𝑥 = 0, with neighbors at steps of 0.5 (i.e., 𝑥 ± 0.5). Show the step-by-step
process, including function values at each step, and draw a diagram of the moves. Does the
algorithm reach the global maximum? Explain.
4. Game Tree Analysis: Given a game tree with root (MAX) and three children A, B, C (MIN),
each with two leaf nodes valued as follows: A (4, 2), B (6, -1), C (3, 5). Apply Minimax to
find the best move. Then, apply Alpha-Beta Pruning, showing which nodes are evaluated and
which are pruned. Provide diagrams for both.
5. Hill Climbing Variants: For the function 𝑓 (𝑥, 𝑦) = −(𝑥 −2) 2 − (𝑦 −1) 2 , apply Steepest-Ascent
Hill Climbing from (0, 0) with step size 0.5. Then, describe how Stochastic Hill Climbing
might differ in one iteration. Show the steps and explain if the global maximum is reached.
Chapter 4
Knowledge Representation

4.1 Overview
Knowledge Representation (KR) is a foundational pillar of Artificial Intelligence (AI) that focuses
on encoding information about the world in a structured, machine-understandable format to enable
reasoning, problem-solving, and decision-making. KR bridges the gap between raw data and
intelligent behavior by providing mechanisms to represent facts, rules, and relationships in a way
that AI systems can process. This chapter explores the role of KR in AI, differentiates knowledge
from data, highlights the importance of logic in reasoning, categorizes types of knowledge, outlines
the characteristics of effective KR systems, and surveys key KR methods, including logic-based
approaches, semantic networks, frames, scripts, and production rules.

4.2 The Role of Knowledge Representation in AI


Knowledge Representation is critical for enabling AI systems to mimic human-like intelligence.
It provides a structured framework for storing, retrieving, and manipulating knowledge, allowing
systems to perform tasks such as reasoning, planning, and communication. The primary roles of
KR in AI include:

• Reasoning and Inference: KR enables systems to derive new knowledge from existing facts
using logical inference mechanisms, such as deducing that a room needs cleaning based on its
state.
• Domain Modeling: KR captures the entities, relationships, and rules of a specific domain
(e.g., medical diagnosis, robotic navigation), enabling specialized reasoning.
• Decision-Making: Structured knowledge supports informed decisions, such as a home
automation system deciding to activate cooling based on temperature data.
• Learning and Adaptation: KR provides a foundation for machine learning by organizing
knowledge in a way that models can interpret and refine.
• Communication: KR facilitates natural language processing (NLP) by encoding linguistic
rules and ontologies, enabling human-like interaction.

70
CHAPTER 4. KNOWLEDGE REPRESENTATION 71

Why Knowledge Representation in AI?


Knowledge Representation (KR) is a fundamental aspect of Artificial Intelligence because it
provides the way in which an intelligent system understands, organizes, and reasons about the
world. Without proper KR, even powerful algorithms cannot make effective decisions.
Importance:

• Allows AI systems to store and organize knowledge systematically.

• Provides a structure for reasoning, inference, and problem-solving.

• Enables agents to learn from experience and update knowledge.

• Bridges the gap between human understanding and machine processing.

• Supports advanced AI applications like natural language processing, planning,


robotics, and expert systems.

In short: KR gives AI systems the “language of thought” needed to act intelligently in


complex environments.

Example: Consider a multi-agent vacuum cleaner system (as discussed in prior contexts). The
knowledge base might include facts like “Room1 is dirty” (Dirty(Room1)) and rules like “If a room is
dirty and an agent is present, clean the room” (∀𝑥(Room(𝑥) ∧Dirty(𝑥) ∧At(Agent, 𝑥) → Clean(𝑥))).
This representation allows agents to reason about which rooms to clean and coordinate tasks
efficiently.

4.3 Knowledge vs. Data


Understanding the distinction between knowledge and data is fundamental to appreciating KR’s role
in AI.

• Data: Raw, unprocessed facts or observations without context or interpretation. Examples


include sensor readings (e.g., “25°C, Room1, 2025-09-02”) or database entries (e.g., “John,
30, fever”).
• Knowledge: Data enriched with meaning, context, and relationships, enabling reasoning and
inference. Knowledge transforms raw data into actionable insights, such as “On September 2,
2025, Room1’s temperature is 25°C, indicating a warm environment requiring cooling.”

Key Difference: Knowledge involves structuring data into formats like logical statements, graphs, or
rules that support reasoning. For instance, a knowledge base might encode the rule “If temperature
exceeds 24°C, activate cooling” to act on temperature data.
Example from Practice: In a medical diagnosis system, raw data like “patient temperature: 38°C”
becomes knowledge when interpreted as “The patient has a fever, indicating a possible infection,”
which can trigger further reasoning about diagnosis and treatment.
CHAPTER 4. KNOWLEDGE REPRESENTATION 72

4.4 Importance of Logic in Reasoning


Logic is the backbone of reasoning in KR systems, providing a formal, unambiguous framework
for representing knowledge and deriving conclusions. It ensures that inferences are sound (valid
conclusions from true premises) and complete (all true conclusions can be derived). Logic supports
various types of reasoning critical to AI:

• Deduction: Deriving specific conclusions from general rules. Example: “All humans are
mortal; Socrates is human → Socrates is mortal.”
• Induction: Generalizing from specific observations. Example: “Room1 and Room2 are dirty
→ All rooms may be dirty.”
• Abduction: Inferring the best explanation for observations. Example: “The room is clean →
An agent likely cleaned it.”

Logical Framework:

• Syntax: Rules for forming valid expressions using symbols (e.g., propositions, predicates)
and connectives (e.g., ∧ (AND), ∨ (OR), ¬ (NOT), → (IMPLIES), ↔ (IF AND ONLY IF)).
• Semantics: Assigns meaning to expressions, defining their truth values in a given model.
• Inference Rules: Mechanisms like modus ponens (from 𝑃 → 𝑄 and 𝑃, infer 𝑄) enable
derivation of new knowledge.

Types of Logic:

• Propositional Logic: Deals with simple true/false statements. Example: “Room1 is dirty”
(𝑃) and “If Room1 is dirty, clean it” (𝑃 → 𝑄).
• First-Order Logic (FOL): Extends propositional logic with predicates, variables, and
quantifiers (∀, ∃). Example: ∀𝑥(Human(𝑥) → Mortal(𝑥)).
• Temporal Logic: Handles time-based reasoning. Example: ^(AllRoomsClean) (“Eventually,
all rooms will be clean”).
• Fuzzy Logic: Manages uncertainty with degrees of truth. Example: “Room is somewhat
dirty” (truth value 0.6).
• Modal Logic: Deals with necessity and possibility. Example: □𝑃 (“It is necessary that the
room is clean”).
• Description Logic: Used for ontologies, representing concepts and roles. Example: “Dirty-
Room ≡ Room ⊓ ∃[Link].”

Example: In the vacuum cleaner scenario, logic enables the system to represent rules like “If a
room is dirty, clean it” (Dirty(Room1) → Clean(Room1)) and infer actions based on current states.
CHAPTER 4. KNOWLEDGE REPRESENTATION 73

4.5 Types of Knowledge


Knowledge in AI can be categorized into three main types, each serving distinct purposes in KR
systems:

• Declarative Knowledge:
• Definition: Factual knowledge about the world, expressed as static assertions or statements.
• Characteristics: Represents “what is true” without specifying actions.
• Example: “Room1 is dirty” (Dirty(Room1)) or “All birds have feathers” (∀𝑥(Bird(𝑥) →
HasFeathers(𝑥))).
• Use: Forms the basis of knowledge bases in expert systems, ontologies, and databases.
• Procedural Knowledge:
• Definition: Knowledge about how to perform tasks or processes, often encoded as rules
or algorithms.
• Characteristics: Focuses on “how to do” something, guiding actions.
• Example: “To clean a room, move to the room, activate the vacuum, and remove dirt” or
a rule like ∀𝑥, 𝑦(Agent(𝑥) ∧ Room(𝑦) ∧ Assigned(𝑥, 𝑦) → Clean(𝑥, 𝑦)).
• Use: Critical for planning, robotic actions, and process automation.
• Meta-Knowledge:
• Definition: Knowledge about the structure, organization, or use of other knowledge.
• Characteristics: Enables systems to reflect on their own knowledge or reasoning
processes.
• Example: Knowing that a knowledge base uses first-order logic or that certain rules
prioritize cleaning dirty rooms.
• Use: Optimizes reasoning strategies, supports learning, and enhances system adaptability.

Example Integration: In a medical diagnosis system:

• Declarative: “Fever is a symptom of flu” (Symptom(Fever, Flu)).


• Procedural: “To diagnose flu, check for fever, cough, and fatigue.”
• Meta-Knowledge: “The system uses a rule-based approach for diagnosis.”

4.6 Characteristics of a Good Knowledge Representation System


An effective KR system must balance several characteristics to ensure it meets the needs of AI
applications. These include:

• Adequacy (Expressiveness):
CHAPTER 4. KNOWLEDGE REPRESENTATION 74

• Definition: The ability to capture all necessary aspects of a domain, including complex
relationships, hierarchies, and nuances.
• Example: A KR system for medical diagnosis must represent symptoms, diseases, and
their interconnections accurately, such as “Fever and cough indicate flu” (Fever(𝑥) ∧
Cough(𝑥) → Flu(𝑥)).
• Trade-Off: Highly expressive systems (e.g., first-order logic) may be computationally
intensive.
• Inferability (Reasoning Efficiency):
• Definition: The ability to perform fast and accurate inference to derive new knowledge
from the knowledge base.
• Example: In a vacuum cleaner system, the system must quickly infer which rooms to
clean based on rules like Dirty(Room1) → Clean(Room1).
• Trade-Off: Efficiency may be compromised in highly expressive systems, requiring
optimization.
• Clarity:
• Definition: Representations should be unambiguous and easy to understand, both for
humans and machines.
• Example: A semantic network with nodes for “Room1” and “Dirt” linked by “is-dirty” is
intuitive and clear.
• Importance: Clarity reduces errors in reasoning and maintenance.
• Extensibility:
• Definition: The system should allow easy updates or additions to the knowledge base as
new information is acquired.
• Example: Adding a new room to a vacuum cleaner’s knowledge base without disrupting
existing rules.
• Importance: Ensures adaptability to changing domains.
• Inferencing Capability:
• Definition: The system must support robust inference mechanisms, including deduction,
induction, and abduction.
• Example: Deducing “Socrates is mortal” from “All humans are mortal” and “Socrates is
human” using modus ponens.
• Importance: Enables the system to generate new knowledge dynamically.
• Additional Characteristics:
• Scalability: Handles large knowledge bases efficiently, critical for real-world applications
like ontologies.
• Consistency: Avoids contradictions within the knowledge base to ensure reliable reason-
ing.
CHAPTER 4. KNOWLEDGE REPRESENTATION 75

4.7 Knowledge Representation Methods


KR methods provide different approaches to structuring knowledge, each with unique strengths and
weaknesses. This section surveys four key methods: logic-based, semantic networks, frames and
scripts, and production rules.

4.7.1 Logic-Based Knowledge Representation

• Description: Represents knowledge using formal logic, such as propositional logic or


first-order logic (FOL), to encode facts, rules, and relationships.
• Key Features:
• Uses logical statements (e.g., 𝑃 → 𝑄 or ∀𝑥(Human(𝑥) → Mortal(𝑥))).
• Supports rigorous reasoning via inference rules like modus ponens, resolution, and
forward/backward chaining.
• Example: In a vacuum cleaner system, facts like Dirty(Room1) and rules like ∀𝑥(Room(𝑥) ∧
Dirty(𝑥) → Clean(𝑥)) guide cleaning actions.
• Advantages:
• Precise and unambiguous, ideal for formal reasoning.
• Supports automated reasoning tools (e.g., theorem provers, SAT solvers).
• Disadvantages:
• Limited expressiveness for uncertain or dynamic knowledge (e.g., fuzzy or temporal
domains).
• Computationally intensive for complex logics like FOL.
• Applications: Expert systems (e.g., MYCIN for medical diagnosis), automated theorem
proving, robotic planning.

4.7.2 Semantic Networks

• Description: Represents knowledge as a graph with nodes (concepts or objects) and edges
(relationships).
• Key Features:
• Nodes represent entities (e.g., “Room1,” “Dirt”).
• Edges represent relationships (e.g., “is-dirty,” “is-a”).
• Supports inheritance, where properties of a general class apply to specific instances.
• Example: A semantic network might link “Room1” to “Dirt” via “is-dirty” and “Agent A” to
“Room1” via “is-in,” representing the vacuum cleaner’s environment.
• Advantages:
CHAPTER 4. KNOWLEDGE REPRESENTATION 76

• Intuitive and visual, ideal for hierarchical and relational knowledge.


• Supports efficient querying and inheritance.
• Disadvantages:
• Limited for complex logical reasoning or procedural knowledge.
• Can become unwieldy for large, interconnected knowledge bases.
• Applications: Knowledge graphs, natural language processing, ontology modeling (e.g.,
Semantic Web).

4.7.3 Frames and Scripts

• Frames:
• Description: Structured templates with slots for attributes, representing objects or
concepts.
• Key Features: Slots hold values or default values (e.g., a “Room” frame with slots for
“size,” “dirt level”).
• Example: A “Room” frame might specify “size: small, dirt level: high” for Room1.
• Advantages: Intuitive for structured knowledge; supports inheritance and defaults.
• Disadvantages: Limited for dynamic or procedural knowledge.
• Applications: Expert systems, cognitive modeling, object-oriented systems.
• Scripts:
• Description: Represent sequences of events or processes, typically for procedural
knowledge.
• Key Features: Describe stereotypical scenarios (e.g., a “cleaning script” with steps:
move to room, vacuum, check dirt).
• Example: A script for a vacuum cleaner might outline the sequence: “Enter room, check
dirt, vacuum if dirty, move to next room.”
• Advantages: Effective for modeling procedural knowledge and expectations.
• Disadvantages: Less flexible for novel situations or non-standard scenarios.
• Applications: Story understanding, AI planning, dialogue systems.

4.7.4 Production Rules

• Description: Represents knowledge as “if-then” rules that trigger actions based on conditions,
often implemented as Horn clauses or definite clauses.
• Key Features:
• Format: “IF condition THEN action” (e.g., “IF Room1 is dirty THEN clean Room1”).
• Supports forward chaining (data-driven) and backward chaining (goal-driven).
CHAPTER 4. KNOWLEDGE REPRESENTATION 77

• Example: A rule like Dirty(Room1) → Clean(Room1) guides the vacuum cleaner’s actions.
• Advantages:
• Simple and modular, easy to update or extend.
• Efficient for rule-based systems and decision-making.
• Disadvantages:
• Risk of rule conflicts or combinatorial explosion in large systems.
• Limited expressiveness for complex relationships or hierarchies.
• Applications: Rule-based expert systems, decision-making systems, control systems.

4.8 Logic in Knowledge Representation


Logic provides a formal foundation for KR, enabling precise representation and reasoning. This
section elaborates on key logical concepts and their application in KR, drawing from the provided
context.

4.8.1 Propositional Logic

• Definition: A branch of logic dealing with propositions (statements) that are either true or
false, combined using connectives like ∧, ∨, ¬, →, ↔.
• Syntax: Atomic propositions (e.g., 𝑃: “Room1 is dirty”) are combined into compound
propositions (e.g., 𝑃 ∧ 𝑄).
• Semantics: Truth values are determined via truth tables.
• Example:
• 𝑃: “It is raining.” 𝑄: “The ground is wet.”
• Rule: 𝑃 → 𝑄 (“If it rains, the ground is wet”).
• Truth Table:

𝑃 𝑄 𝑃→𝑄
T T T
T F F
F T T
F F T

Table 4.1: Truth Table for 𝑃 → 𝑄

• Applications: Simple rule-based systems (e.g., home automation), circuit design, game-playing
AI.
• Limitations: Cannot represent relationships, uncertainty, or time-based sequences; scalability
issues for large systems.
CHAPTER 4. KNOWLEDGE REPRESENTATION 78

4.8.2 First-Order Logic (FOL)

• Definition: Extends propositional logic with predicates, variables, constants, and quantifiers
(∀, ∃), enabling representation of complex relationships.
• Components:
• Predicates: Functions returning true/false (e.g., Parent(𝑥, 𝑦)).
• Variables: Represent objects (e.g., 𝑥, 𝑦).
• Constants: Specific objects (e.g., John).
• Quantifiers: ∀ (“for all”), ∃ (“there exists”).
• Example: ∀𝑥(Human(𝑥) → Mortal(𝑥)) (“All humans are mortal”). Given Human(Socrates),
infer Mortal(Socrates).
• Inference Mechanisms:
• Unification: Matches predicates (e.g., unifying Likes(𝑥, Cake) with Likes(Alice, 𝑦)
yields 𝑥 = Alice, 𝑦 = Cake).
• Lifting: Generalizes rules to variables (e.g., applying Parent(𝑥, 𝑦) → Loves(𝑥, 𝑦) to any
parent-child pair).
• Resolution: Proves statements by contradiction, using conjunctive normal form (CNF).
• Applications: Knowledge bases, NLP, robotic planning.

4.8.3 Other Logics

• Temporal Logic: Represents time-based sequences. Example: ^(AllRoomsClean) (“Eventu-


ally, all rooms will be clean”).
• Fuzzy Logic: Handles uncertainty with degrees of truth. Example: “Room is somewhat dirty”
(truth value 0.6).
• Modal Logic: Deals with necessity and possibility. Example: □𝑃 (“It is necessary that the
room is clean”).
• Description Logic: Used for ontologies. Example: “DirtyRoom ≡ Room ⊓ ∃[Link].”

4.9 Inference Mechanisms in KR


Inference mechanisms enable AI systems to derive new knowledge from existing facts and rules.
Key methods include:

• Forward Chaining:
• Description: Data-driven reasoning that starts with known facts and applies rules to
generate new facts.
CHAPTER 4. KNOWLEDGE REPRESENTATION 79

• Example: Given facts Fever(John), SoreThroat(John), and rule Fever(𝑥)∧SoreThroat(𝑥) →


Flu(𝑥), infer Flu(John).
• Use: Diagnostics, troubleshooting.
• Backward Chaining:
• Description: Goal-driven reasoning that starts with a hypothesis and verifies supporting
facts.
• Example: To prove Flu(John), check if Fever(John) and SoreThroat(John) are true.
• Use: Query systems, expert systems.
• Resolution:
• Description: Proves statements by contradiction, converting formulas to CNF and using
unification in FOL.
• Example: To prove Mortal(John) from ∀𝑥(Person(𝑥) → Mortal(𝑥)) and Person(John),
unify 𝑥 = John and resolve.
• Use: Theorem proving, automated reasoning.

4.10 Practical Applications and Examples


KR is applied across various AI domains, with practical examples illustrating its utility:

• Expert Systems: MYCIN uses production rules to represent medical knowledge for diagnosis
(e.g., “IF fever AND cough THEN flu”).
• Robotics: A vacuum cleaner agent uses a knowledge base with facts (Dirty(Room1)), rules
(Dirty(𝑥) → Clean(𝑥)), and semantic networks to navigate and clean.
• NLP: Semantic networks and description logic encode linguistic rules and ontologies for text
understanding.
• Semantic Web: Ontologies represent structured knowledge for information sharing (e.g.,
“Human ⊑ Mortal”).

Case Study: Vacuum Cleaner Agent:

• Knowledge Base:
• Facts: Dirty(Room1), At(AgentA, Room1).
• Rules: ∀𝑥(Room(𝑥) ∧ Dirty(𝑥) ∧ At(Agent, 𝑥) → Clean(𝑥)).
• Representation Methods:
• Logic-Based: FOL rules for cleaning actions.
• Semantic Network: Nodes for “Room1,” “Dirt,” “AgentA” with edges like “is-dirty,”
“is-in.”
• Frame: A “Room” frame with slots for “dirt level,” “location.”
CHAPTER 4. KNOWLEDGE REPRESENTATION 80

• Production Rule: “IF Room1 is dirty THEN clean Room1.”


• Reasoning: Forward chaining infers cleaning actions; backward chaining verifies if all rooms
are clean.

4.11 Challenges and Future Directions


KR faces several challenges that shape its future development:

• Handling Uncertainty: Representing and reasoning with uncertain or incomplete knowledge


(addressed by fuzzy logic or probabilistic methods).
• Scalability: Managing large knowledge bases efficiently, especially in dynamic domains.
• Expressiveness vs. Efficiency: Balancing the ability to represent complex knowledge with
computational feasibility.
• Integration with Machine Learning: Combining structured KR with data-driven approaches
for hybrid AI systems.

Future Directions:

• Development of hybrid KR systems that integrate logic, networks, and rules.


• Advances in probabilistic KR for handling uncertainty.
• Enhanced ontologies for the Semantic Web and cross-domain knowledge sharing.
Chapter 5
Propositional Logic

5.1 Overview
Propositional Logic (PL), also known as Boolean logic, is a fundamental formalism in Artificial
Intelligence (AI) for representing and reasoning about knowledge using declarative statements that
are either true or false. PL provides a structured way to encode facts and rules, enabling AI systems
to perform logical reasoning, make decisions, and solve problems. This chapter explores the syntax
and semantics of PL, inference mechanisms (entailment, validity, satisfiability, and rules like modus
ponens and modus tollens), the resolution method (including conversion to conjunctive normal form
and solving puzzles), and reasoning strategies such as forward chaining and backward chaining.
These concepts are critical for applications like expert systems, automated decision-making, and
puzzle-solving in AI.

5.2 Syntax and Semantics of Propositional Logic


Propositional Logic deals with propositions—declarative statements that are either true (T) or false
(F). It provides a formal framework for combining these propositions using logical connectives to
represent complex knowledge and perform reasoning.

5.2.1 Syntax of Propositional Logic

The syntax of PL defines the rules for constructing valid logical expressions, known as well-formed
formulas (WFFs).

• Propositions: Atomic statements that represent basic facts or conditions with a binary truth
value (true or false).
• Example: 𝑃: “It is raining.” 𝑄: “The ground is wet.”
• Logical Connectives: Operators that combine propositions to form compound propositions.
• Negation (¬): Inverts the truth value of a proposition (e.g., ¬𝑃: “It is not raining”).
• Conjunction (∧): True if both propositions are true (e.g., 𝑃 ∧ 𝑄: “It is raining and the
ground is wet”).

81
CHAPTER 5. PROPOSITIONAL LOGIC 82

• Disjunction (∨): True if at least one proposition is true (e.g., 𝑃 ∨ 𝑄: “It is raining or the
ground is wet”).
• Implication (→): True unless the antecedent is true and the consequent is false (e.g.,
𝑃 → 𝑄: “If it is raining, then the ground is wet”).
• Biconditional (↔): True if both propositions have the same truth value (e.g., 𝑃 ↔ 𝑄:
“It is raining if and only if the ground is wet”).
• Compound Propositions: Formed by combining atomic propositions with connectives.
• Example: (𝑃 ∧ ¬𝑄) → 𝑅, where 𝑅: “Turn on the heater.”
• Precedence of Connectives: To avoid ambiguity, connectives are evaluated in the following
order:
1. ¬ (highest precedence)
2. ∧
3. ∨
4. →
5. ↔ (lowest precedence)
Parentheses are used to enforce specific evaluation orders (e.g., (𝑃 ∨ 𝑄) ∧ 𝑅).

5.2.2 Semantics of Propositional Logic

The semantics of PL define the meaning of propositions and compound expressions by specifying
their truth values under different assignments.

• Truth Values: Each proposition is either true (T) or false (F).


• Truth Tables: A systematic way to compute the truth value of a compound proposition for all
possible combinations of its atomic propositions.
• Example: Truth table for 𝑃 ∧ 𝑄:

𝑃 𝑄 𝑃∧𝑄
T T T
T F F
F T F
F F F

Table 5.1: Truth Table for 𝑃 ∧ 𝑄

• Example: Truth table for (𝑃 ∨ 𝑄) ∧ 𝑅:


• Logical Equivalence: Two propositions are equivalent (≡) if they have identical truth values
for all assignments.
• Example: De Morgan’s Laws:
CHAPTER 5. PROPOSITIONAL LOGIC 83

𝑃 𝑄 𝑅 (𝑃 ∨ 𝑄) ∧ 𝑅
T T T T
T T F F
T F T T
T F F F
F T T T
F T F F
F F T F
F F F F

Table 5.2: Truth Table for (𝑃 ∨ 𝑄) ∧ 𝑅

• ¬(𝑃 ∧ 𝑄) ≡ (¬𝑃 ∨ ¬𝑄)


• ¬(𝑃 ∨ 𝑄) ≡ (¬𝑃 ∧ ¬𝑄)
• Example: Implication: 𝑃 → 𝑄 ≡ ¬𝑃 ∨ 𝑄

5.2.3 Applications

PL is used in AI for simple rule-based systems, such as:

• Home automation: “If it is hot and windows are closed, turn on the AC” ((𝑃 ∧ ¬𝑄) → 𝑅).
• Game-playing AI: Representing game states and rules (e.g., chess moves).
• Circuit design: Modeling logical gates (e.g., AND gate as 𝐴 ∧ 𝐵).

5.3 Inference in Propositional Logic


Inference in PL involves deriving new propositions from a set of given propositions (premises)
using formal rules. This section covers entailment, validity, satisfiability, and key inference rules.

5.3.1 Entailment, Validity, and Satisfiability

• Entailment (|=): A set of propositions Γ entails a proposition 𝜙 (denoted Γ |= 𝜙) if 𝜙 is true


in every truth assignment where all propositions in Γ are true.
• Example: Γ = {𝑃 → 𝑄, 𝑃}, 𝜙 = 𝑄.
• If 𝑃 → 𝑄 and 𝑃 are true, then 𝑄 must be true.
• Check: In assignments where 𝑃 → 𝑄 and 𝑃 are true (i.e., 𝑃 = 𝑇, 𝑄 = 𝑇), 𝜙 = 𝑄 is
true.
• Thus, Γ |= 𝑄.
• Testing Entailment: Construct a truth table to verify if 𝜙 is true whenever Γ is true, or
check if Γ ∧ ¬𝜙 is unsatisfiable.
CHAPTER 5. PROPOSITIONAL LOGIC 84

• Validity (Tautology): A proposition 𝜙 is valid if it is true under all possible truth assignments.
• Example: 𝑃 ∨ ¬𝑃 (Law of Excluded Middle).

𝑃 𝑃 ∨ ¬𝑃
T T
F T

Table 5.3: Truth Table for 𝑃 ∨ ¬𝑃

• Testing Validity: Construct a truth table; if all rows are true, the proposition is valid.
• Satisfiability: A proposition 𝜙 is satisfiable if there exists at least one truth assignment that
makes it true. It is unsatisfiable (a contradiction) if no such assignment exists.
• Example (Satisfiable): 𝑃 ∧ 𝑄.
• True when 𝑃 = 𝑇, 𝑄 = 𝑇.
• Example (Unsatisfiable): 𝑃 ∧ ¬𝑃.

𝑃 𝑃 ∧ ¬𝑃
T F
F F

Table 5.4: Truth Table for 𝑃 ∧ ¬𝑃

• Testing Satisfiability: Use truth tables or SAT solvers (e.g., DPLL algorithm) to find a
satisfying assignment.

5.3.2 Rules of Inference

Inference rules are formal methods for deriving new propositions from existing ones. Two key rules
are:

• Modus Ponens:
• Form: From 𝑃 → 𝑄 and 𝑃, infer 𝑄.
• Example: Given “If it is raining, the ground is wet” (𝑃 → 𝑄) and “It is raining” (𝑃),
infer “The ground is wet” (𝑄).
• Modus Tollens:
• Form: From 𝑃 → 𝑄 and ¬𝑄, infer ¬𝑃.
• Example: Given “If it is raining, the ground is wet” (𝑃 → 𝑄) and “The ground is not
wet” (¬𝑄), infer “It is not raining” (¬𝑃).
• Other Rules:
• Disjunctive Syllogism: From 𝑃 ∨ 𝑄 and ¬𝑃, infer 𝑄.
• Conjunction Introduction: From 𝑃 and 𝑄, infer 𝑃 ∧ 𝑄.
• Disjunction Introduction: From 𝑃, infer 𝑃 ∨ 𝑄.
CHAPTER 5. PROPOSITIONAL LOGIC 85

5.4 Resolution Method


The resolution method is a powerful inference technique used to prove the validity of propositions
or solve problems by contradiction. It involves converting formulas to conjunctive normal form
(CNF) and applying the resolution principle.

5.4.1 Conjunctive Normal Form (CNF)

A formula is in CNF if it is a conjunction of clauses, where each clause is a disjunction of literals (a


literal is a proposition or its negation).

• Steps to Convert to CNF:


1. Eliminate biconditionals (↔) using 𝑃 ↔ 𝑄 ≡ (𝑃 → 𝑄) ∧ (𝑄 → 𝑃).
2. Eliminate implications (→) using 𝑃 → 𝑄 ≡ ¬𝑃 ∨ 𝑄.
3. Apply De Morgan’s Laws to push negations inward (e.g., ¬(𝑃 ∧ 𝑄) ≡ ¬𝑃 ∨ ¬𝑄).
4. Distribute disjunctions over conjunctions to form clauses (e.g., 𝑃 ∨ (𝑄 ∧ 𝑅) ≡ (𝑃 ∨ 𝑄) ∧
(𝑃 ∨ 𝑅)).
• Example: Convert (𝑃 → 𝑄) ∧ (𝑄 → 𝑅) to CNF.
• Step 1: Replace implications: (¬𝑃 ∨ 𝑄) ∧ (¬𝑄 ∨ 𝑅).
• Step 2: Already in CNF (conjunction of clauses ¬𝑃 ∨ 𝑄 and ¬𝑄 ∨ 𝑅).

5.4.2 Resolution Principle

Resolution proves a proposition by assuming its negation and deriving a contradiction (empty
clause).

• Process:
1. Convert all premises and the negation of the goal to CNF.
2. Repeatedly apply the resolution rule: From two clauses containing complementary literals
(e.g., 𝑃 ∨ 𝐴 and ¬𝑃 ∨ 𝐵), derive a new clause (𝐴 ∨ 𝐵).
3. Continue until an empty clause ({}) is derived (indicating a contradiction) or no new
clauses can be derived.
• Example: Solving a Simple Puzzle:
• Puzzle: Given:
• 𝑃 ∨ 𝑄 (“Either it is raining or sunny”).
• ¬𝑃 (“It is not raining”).
• Prove: 𝑄 (“It is sunny”).
• Resolution Steps:
1. Premises in CNF: 𝑃 ∨ 𝑄, ¬𝑃.
CHAPTER 5. PROPOSITIONAL LOGIC 86

2. Negate goal: ¬𝑄.


3. Resolve 𝑃 ∨ 𝑄 and ¬𝑃: Yields 𝑄.
4. Resolve 𝑄 and ¬𝑄: Yields empty clause ({}).
5. Conclusion: The premises entail 𝑄 (since assuming ¬𝑄 leads to a contradiction).
• Use Case: Resolution is used in automated theorem provers and logic-based AI systems to
solve puzzles, verify software correctness, or prove logical statements.

5.5 Forward Chaining


Forward chaining is a data-driven reasoning strategy that starts with known facts and applies rules
to derive new facts until a goal is reached or no further inferences are possible.

Algorithm

• Input: A knowledge base of facts (propositions) and rules (implications in the form 𝐴∧𝐵 → 𝐶).
• Process:
1. Identify rules whose antecedents (conditions) are satisfied by current facts.
2. Apply these rules to add their consequents (new facts) to the knowledge base.
3. Repeat until the goal is derived or no new facts can be added.
• Example:
• Facts: 𝑃: “Patient has fever.” 𝑄: “Patient has sore throat.”
• Rule: 𝑃 ∧ 𝑄 → 𝑅, where 𝑅: “Patient has flu.”
• Step 1: Check facts 𝑃 and 𝑄 are true.
• Step 2: Apply rule to infer 𝑅.
• Conclusion: The patient has flu.

Properties

• Data-Driven: Starts with available facts and works toward conclusions.


• Bottom-Up: Builds knowledge incrementally from facts.
• Breadth-First Search: Explores multiple rules simultaneously.
• Potential Inefficiency: May apply irrelevant rules, increasing computation.

Use Cases

• Rule-Based Expert Systems: Diagnosing diseases (e.g., MYCIN infers flu from symptoms).
• Troubleshooting: Identifying faults in systems based on observed conditions.
• Prediction Systems: Forecasting outcomes based on current data.
CHAPTER 5. PROPOSITIONAL LOGIC 87

5.6 Backward Chaining


Backward chaining is a goal-driven reasoning strategy that starts with a hypothesis (goal) and works
backward to verify if the available facts support it.

Algorithm

• Input: A knowledge base of facts and rules, and a goal proposition.


• Process:
1. Start with the goal and identify rules whose consequents match the goal.
2. Treat the antecedents of these rules as sub-goals.
3. Recursively verify each sub-goal by checking facts or applying other rules.
4. Continue until all sub-goals are satisfied (goal is proven) or no supporting facts are found
(goal fails).
• Example:
• Goal: 𝑅: “Patient has flu.”
• Rule: 𝑃 ∧ 𝑄 → 𝑅, where 𝑃: “Patient has fever,” 𝑄: “Patient has sore throat.”
• Step 1: To prove 𝑅, check sub-goals 𝑃 and 𝑄.
• Step 2: Verify 𝑃 and 𝑄 are in the knowledge base (both true).
• Conclusion: The goal 𝑅 is proven.

Properties

• Goal-Driven: Focuses on proving a specific hypothesis.


• Top-Down: Works from the goal to supporting facts.
• Depth-First Search: Explores one path deeply before trying others.
• Potential for Infinite Loops: Requires careful handling to avoid recursive traps.

Use Cases

• Prolog-Style Reasoning: Used in logic programming languages like Prolog for query
resolution.
• Expert Systems: Verifying diagnoses by checking if symptoms support a disease.
• Decision-Making Models: Determining if a goal (e.g., “clean room”) is achievable based on
current conditions.
CHAPTER 5. PROPOSITIONAL LOGIC 88

Aspect Forward Chaining Backward Chaining


Approach Data-driven: Starts with facts to derive Goal-driven: Starts with a goal to verify
conclusions. supporting facts.
Search Strategy Breadth-first: Explores multiple rules at Depth-first: Focuses on one path at a
once. time.
Direction Bottom-up: From facts to conclusions. Top-down: From goal to facts.
Efficiency May explore irrelevant rules, reducing Risk of infinite loops if not managed.
efficiency.
Memory Usage Higher, as it processes multiple rules. Lower, as it focuses on specific goals.
Use Cases Diagnostics, troubleshooting, prediction. Query systems, Prolog-style reasoning,
decision-making.

Table 5.5: Comparison of Forward and Backward Chaining

Practical Applications and Examples


Propositional Logic is widely used in AI for tasks requiring clear-cut logical reasoning. Key
applications include:

• Expert Systems: Representing rules for diagnosis (e.g., “If fever and sore throat, then flu”
(𝑃 ∧ 𝑄 → 𝑅)).
• Game-Playing AI: Modeling game states and rules (e.g., chess strategies).
• Home Automation: Decision rules like “If hot and windows closed, turn on AC” ((𝑃 ∧¬𝑄) →
𝑅).
• Puzzle-Solving: Using resolution to solve logical puzzles, as shown in the example above.

Case Study: Home Automation System:

• Knowledge Base:
• Facts: 𝑃: “It is hot.” 𝑄: “Windows are open.”
• Rule: (𝑃 ∧ ¬𝑄) → 𝑅, where 𝑅: “Turn on AC.”
• Forward Chaining:
• Given 𝑃 = 𝑇, 𝑄 = 𝐹, apply rule to infer 𝑅 = 𝑇 (turn on AC).
• Backward Chaining:
• Goal: Prove 𝑅. Check sub-goals 𝑃 and ¬𝑄. Since 𝑃 = 𝑇, ¬𝑄 = 𝑇, conclude 𝑅 = 𝑇.
• Resolution:
• Premises: 𝑃, ¬𝑄, ¬𝑃 ∨ 𝑄 ∨ 𝑅 (CNF of (𝑃 ∧ ¬𝑄) → 𝑅).
• Negate goal: ¬𝑅.
• Resolve: Derive empty clause, proving 𝑅.
CHAPTER 5. PROPOSITIONAL LOGIC 89

Limitations of Propositional Logic


While PL is effective for simple reasoning, it has limitations:

• Limited Expressiveness: Cannot represent relationships or hierarchies (e.g., “All rooms are
dirty” requires first-order logic).
• No Uncertainty Handling: Works only with binary truth values, not probabilities.
• Scalability Issues: Large knowledge bases lead to complex truth tables or resolution processes.
• No Temporal Reasoning: Cannot model time-based sequences (e.g., “Room will be clean
later”).
Chapter 6
First Order Logic

6.1 Overview
First-Order Logic (FOL), also known as predicate logic, is a powerful formalism in Artificial
Intelligence (AI) that extends propositional logic by enabling the representation of complex
relationships and generalizations about objects in a domain. Unlike propositional logic, which is
limited to simple true/false statements, FOL introduces predicates, variables, constants, functions,
and quantifiers, making it suitable for modeling real-world scenarios in knowledge bases, natural
language processing, and robotic planning. This chapter explores the syntax and semantics of FOL,
reasoning patterns (universal instantiation, existential instantiation, generalized modus ponens),
resolution (including skolemization and unification), and reasoning strategies such as forward
chaining and backward chaining, with applications in knowledge bases and Prolog-style reasoning.
Whereas propositional logic assumes the world contains facts, first-order logic (like natural language)
assumes the world contains
– Objects: people, houses, numbers, colors, baseball games, wars, . . .
– Relations: red, round, prime, brother of, bigger than, part of, comes between, . . .
– Functions: father of, best friend, one more than, plus, . . .

6.2 Syntax and Semantics First-Order Logic


The syntax of FOL defines the rules for constructing valid logical expressions, known as well-formed
formulas (WFFs), which allow for the representation of objects, their properties, and relationships.

6.2.1 Components of FOL Syntax

• Constants: Specific objects or individuals in the domain. KingJohn, 2, UofA,...


• Example: John, Room1, Mary.
• Variables: Symbols that represent arbitrary objects in the domain. x, y, a, b,...
• Example: 𝑥, 𝑦, 𝑧.
• Predicates: Functions that represent properties of objects or relationships between objects,
returning true or false. Brother, >,...

90
CHAPTER 6. FIRST ORDER LOGIC 91

• Example: Parent(𝑥, 𝑦) (“𝑥 is a parent of 𝑦”); Dirty(Room1) (“Room1 is dirty”).


• Functions: Mappings that return objects in the domain, used to denote complex terms. Sqrt,
LeftLegOf,...
• Example: fatherOf(𝑥) (returns the father of 𝑥).
• Quantifiers: Symbols that specify the scope of variables.
• Universal Quantifier (∀): “For all.” Example: ∀𝑥(Human(𝑥) → Mortal(𝑥)) (“All
humans are mortal”).
• Existential Quantifier (∃): “There exists.” Example: ∃𝑥(Parent(𝑥, John) (“There exists
someone who is a parent of John”).
• Connectives: Same as propositional logic: ¬, ⇒, ∧, ∨, ⇔.
• Equality: =
• Terms: Constants, variables, or functions applied to terms (e.g., John, 𝑥, fatherOf(John)).
• Atomic Formulas: Predicates applied to terms (e.g., Parent(Mary, John)).
• Compound Formulas: Formed by combining atomic formulas with connectives and quantifiers
(e.g., ∀𝑥(Human(𝑥) → Mortal(𝑥)) ∧ ∃𝑦(Parent(𝑦, 𝑥))).

Example

Consider a vacuum cleaner agent:

• Constants: Room1, AgentA.


• Variables: 𝑥, 𝑦.
• Predicates: Dirty(𝑥), At(𝑥, 𝑦), Clean(𝑥).
• Formula: ∀𝑥(Room(𝑥) ∧ Dirty(𝑥) ∧ At(AgentA, 𝑥) → Clean(𝑥)) (“For all rooms, if the room
is dirty and AgentA is in it, clean the room”).

6.2.2 Semantics of First-Order Logic

The semantics of FOL define the meaning of formulas by specifying their truth values in a model,
which consists of a domain and an interpretation.

Models and Interpretations

• Domain: A non-empty set of objects (e.g., {John, Mary, Room1, Room2}).


• Interpretation: A mapping that assigns:
• Constants to objects in the domain (e.g., John ↦→ John).
• Predicates to relations over the domain (e.g., Parent(𝑥, 𝑦) maps to pairs where 𝑥 is a parent
of 𝑦).
CHAPTER 6. FIRST ORDER LOGIC 92

• Functions to mappings in the domain (e.g., fatherOf(John) ↦→ Bob).


• Truth Values: A formula is true or false in a model based on the interpretation.
• Example: Human(John) is true if John is in the set of humans defined by the interpretation.
• Example: ∀𝑥(Human(𝑥) → Mortal(𝑥)) is true if, for every object 𝑥 in the domain, if 𝑥 is
human, then 𝑥 is mortal.

Satisfiability and Validity

• Satisfiability: A formula is satisfiable if there exists at least one model where it is true.
• Example: ∃𝑥(Parent(𝑥, John) is satisfiable if there is a model where someone is a parent
of John.
• Validity: A formula is valid (a tautology) if it is true in all possible models.
• Example: ∀𝑥(Human(𝑥) → Human(𝑥)) is valid (true for all interpretations).

6.3 Reasoning Patterns in First-Order Logic


FOL supports complex reasoning patterns that leverage quantifiers and predicates to derive new
knowledge. Key patterns include universal instantiation, existential instantiation, and generalized
modus ponens.

Universal Instantiation (UI)

• Definition: From a universally quantified formula ∀𝑥𝑃(𝑥), infer 𝑃(𝑐) for any specific constant
𝑐 in the domain.
• Example:
• Given: ∀𝑥(Human(𝑥) → Mortal(𝑥)).
• Instantiate with 𝑐 = Socrates: Human(Socrates) → Mortal(Socrates).
• If Human(Socrates) is true, infer Mortal(Socrates).
• Use: Applies general rules to specific instances in knowledge bases.

Existential Instantiation (EI)

• Definition: From an existentially quantified formula ∃𝑥𝑃(𝑥), infer 𝑃(𝑐) for some new constant
𝑐 (not previously used in the proof).
• Example:
• Given: ∃𝑥(Parent(𝑥, John).
• Introduce a new constant 𝑐: Parent(𝑐, John).
CHAPTER 6. FIRST ORDER LOGIC 93

• Use 𝑐 to represent the unknown parent in further reasoning.


• Use: Handles cases where specific objects satisfying a property exist but are not explicitly
named.

Generalized Modus Ponens

• Definition: A generalized form of modus ponens that applies to predicates with variables,
using unification to match terms.
• Form: Given ∀𝑥(𝑃1 (𝑥) ∧ · · · ∧ 𝑃𝑛 (𝑥) → 𝑄(𝑥)) and facts 𝑃1 (𝑎), . . ., 𝑃𝑛 (𝑎), infer 𝑄(𝑎).
• Example:
• Rule: ∀𝑥(Fever(𝑥) ∧ SoreThroat(𝑥) → Flu(𝑥)).
• Facts: Fever(John), SoreThroat(John).
• Unify 𝑥 = John, infer: Flu(John).
• Use: Enables reasoning with general rules and specific instances in knowledge bases.

6.4 Resolution in First-Order Logic


Resolution is a powerful inference method in FOL that proves statements by contradiction, extending
the propositional logic resolution method with skolemization and unification.

Skolemization

• Definition: The process of eliminating existential quantifiers by introducing new constants or


functions (Skolem constants or functions) to represent the objects they assert exist.
• Process:
• For ∃𝑥𝑃(𝑥), replace 𝑥 with a new constant (e.g., 𝑐): 𝑃(𝑐).
• For ∃𝑦∀𝑥𝑃(𝑥, 𝑦), replace 𝑦 with a Skolem function dependent on 𝑥: 𝑃(𝑥, 𝑓 (𝑥)).
• Example:
• Formula: ∃𝑦(Parent(𝑦, John).
• Skolemized: Parent(c, John), where c is a new constant.
• Formula: ∀𝑥∃𝑦(Parent(𝑦, 𝑥)).
• Skolemized: ∀𝑥(Parent( 𝑓 (𝑥), 𝑥)), where 𝑓 (𝑥) is a Skolem function.
• Use: Prepares formulas for resolution by eliminating existential quantifiers.
CHAPTER 6. FIRST ORDER LOGIC 94

Unification Algorithm

• Definition: The process of finding a substitution that makes two logical expressions (predicates)
identical, enabling resolution.
• Algorithm:
1. Compare two predicates (e.g., P(𝑥, John) and P(Mary, 𝑦)).
2. If predicates have the same name and arity, find a substitution for variables to make
arguments match.
3. Example: Unify P(𝑥, John) and P(Mary, 𝑦).
• Substitution: 𝑥 = Mary, 𝑦 = John.
• Result: P(Mary, John).
4. Handle conflicts: If arguments cannot be unified (e.g., P(John, 𝑦) and P(Mary, 𝑦)),
unification fails.
• Use: Matches rules and facts in resolution and chaining.

Resolution Refutation in FOL

• Definition: Proves a formula by assuming its negation, converting all formulas to conjunctive
normal form (CNF), and deriving a contradiction (empty clause) using resolution.
• Process:
1. Put the premises or axioms into clause form.
2. Add the negation of what is to be proved, in clause form, to the set of axioms.
3. Resolve these clauses together, producing new clauses that logically follow from them.
4. Produce a contradiction by generating the empty clause.
5. The substitutions used to produce the empty clause are those under which the opposite of
the negated goal is true.
• Example:
• Premises: ∀𝑥(Human(𝑥) → Mortal(𝑥)), Human(Socrates).
• Goal: Mortal(Socrates).
• Step 1: Convert to CNF:
• ∀𝑥(Human(𝑥) → Mortal(𝑥)) becomes ¬Human(𝑥) ∨ Mortal(𝑥).
• Human(Socrates).
• Negated goal: ¬Mortal(Socrates).
• Step 2: Resolve:
• ¬Human(𝑥)∨Mortal(𝑥) and Human(Socrates) with 𝑥 = Socrates: Yields Mortal(Socrates).
• Mortal(Socrates) and ¬Mortal(Socrates): Yields empty clause ({}).
• Conclusion: The premises entail Mortal(Socrates).
• Use: Automated theorem proving, knowledge base querying, planning.
CHAPTER 6. FIRST ORDER LOGIC 95

Examples of Resolution in First-Order Logic

Facts in Propositional Logic

Given Axioms Clause Form


P P (1)
(𝑃 ∧ 𝑄) → 𝑅 ¬𝑃 ∨ ¬𝑄 ∨ 𝑅 (2)
(𝑆 ∨ 𝑇) → 𝑄 ¬𝑆 ∨ 𝑄 (3)
¬𝑇 ∨ 𝑄 (4)
T T (5)
Prove R!

Resolution in Propositional Logic

¬𝑃 ∨ ¬𝑄 ∨ 𝑅 (2)
¬𝑅
¬𝑃 ∨ ¬𝑄
𝑃 (1)
¬𝑄
¬𝑇 ∨ 𝑄 (4)
¬𝑄
¬𝑇
𝑇 (5)

Prove Fido Will Die

1. All dogs are animals.


2. Fido is a dog.
3. All animals will die.

Prove that Fido will die!

Resolution Proof for the “Dead Dog” Problem Clause form:

• 1. ¬𝐷𝑜𝑔(𝑥) ∨ 𝐴𝑛𝑖𝑚𝑎𝑙 (𝑥)


• 2. 𝐷𝑜𝑔(𝐹𝑖𝑑𝑜)
• 3. ¬𝐴𝑛𝑖𝑚𝑎𝑙 (𝑦) ∨ 𝐷𝑖𝑒𝑠(𝑦)
CHAPTER 6. FIRST ORDER LOGIC 96

Negate goal: ¬𝐷𝑖𝑒𝑠(𝐹𝑖𝑑𝑜)


Resolution: - From 2 and 1: 𝐴𝑛𝑖𝑚𝑎𝑙 (𝐹𝑖𝑑𝑜) - From 𝐴𝑛𝑖𝑚𝑎𝑙 (𝐹𝑖𝑑𝑜) and 3: 𝐷𝑖𝑒𝑠(𝐹𝑖𝑑𝑜) - Resolve
with ¬𝐷𝑖𝑒𝑠(𝐹𝑖𝑑𝑜) to derive empty clause (∅).

Lucky Student

1. Anyone passing his history exams and winning the lottery is happy.
2. Anyone who studies or is lucky can pass all his exams.
3. John did not study but he is lucky.
4. Anyone who is lucky wins the lottery.

Prove that John is happy!

One Resolution Refutation for the “Happy Student” Problem Clauses:

• 1. ¬𝑃𝑎𝑠𝑠𝑒𝑠(𝑥) ∨ ¬𝑊𝑖𝑛𝑠𝐿𝑜𝑡𝑡𝑒𝑟 𝑦(𝑥) ∨ 𝐻𝑎 𝑝 𝑝𝑦(𝑥)


• 2a. ¬𝑆𝑡𝑢𝑑𝑖𝑒𝑠(𝑥) ∨ 𝑃𝑎𝑠𝑠𝑒𝑠(𝑥)
• 2b. ¬𝐿𝑢𝑐𝑘 𝑦(𝑥) ∨ 𝑃𝑎𝑠𝑠𝑒𝑠(𝑥)
• 3. ¬𝑆𝑡𝑢𝑑𝑖𝑒𝑠(𝐽𝑜ℎ𝑛) ∧ 𝐿𝑢𝑐𝑘 𝑦(𝐽𝑜ℎ𝑛)
• 4. ¬𝐿𝑢𝑐𝑘 𝑦(𝑥) ∨ 𝑊𝑖𝑛𝑠𝐿𝑜𝑡𝑡𝑒𝑟 𝑦(𝑥)

Negate goal: ¬𝐻𝑎 𝑝 𝑝𝑦(𝐽𝑜ℎ𝑛)


Resolution steps lead to empty clause (∅).

Exciting Life

1. All people that are not poor and are smart are happy.
2. Those people that read are not stupid.
3. John can read and is wealthy.
4. Happy people have exciting lives.

Can anyone be found with an exciting life?

Resolution Proof for the “Exciting Life” Problem Clauses:

• 1. 𝑃𝑜𝑜𝑟 (𝑥) ∨ ¬𝑆𝑚𝑎𝑟𝑡 (𝑥) ∨ 𝐻𝑎 𝑝 𝑝𝑦(𝑥)


• 2. ¬𝑅𝑒𝑎𝑑𝑠(𝑥) ∨ 𝑆𝑚𝑎𝑟𝑡 (𝑥) (not stupid = smart)
• 3. 𝑅𝑒𝑎𝑑𝑠(𝐽𝑜ℎ𝑛) ∧ ¬𝑃𝑜𝑜𝑟 (𝐽𝑜ℎ𝑛) (wealthy = not poor)
• 4. ¬𝐻𝑎 𝑝 𝑝𝑦(𝑥) ∨ 𝐸𝑥𝑐𝑖𝑡𝑖𝑛𝑔(𝑥)

Resolution leads to 𝐸𝑥𝑐𝑖𝑡𝑖𝑛𝑔(𝐽𝑜ℎ𝑛).


CHAPTER 6. FIRST ORDER LOGIC 97

Resolution Example

1. John likes all kinds of food. ∀𝑥 : 𝑓 𝑜𝑜𝑑 (𝑥) → 𝑙𝑖𝑘𝑒𝑠( 𝑗 𝑜ℎ𝑛, 𝑥)


2. Apples are food. 𝑓 𝑜𝑜𝑑 (𝑎 𝑝 𝑝𝑙𝑒)
3. Chicken is food. 𝑓 𝑜𝑜𝑑 (𝑐ℎ𝑖𝑐𝑘𝑒𝑛)
4. Anything anyone eats and isn’t killed by is food. ∀𝑥 : (∃𝑦 : 𝑒𝑎𝑡𝑠(𝑦, 𝑥) ∧ ¬𝑘𝑖𝑙𝑙𝑒𝑑𝑏𝑦(𝑦, 𝑥)) →
𝑓 𝑜𝑜𝑑 (𝑥)
5. Bill eats peanuts and is still alive. 𝑒𝑎𝑡𝑠(𝐵𝑖𝑙𝑙, 𝑝𝑒𝑎𝑛𝑢𝑡𝑠); 𝑎𝑙𝑖𝑣𝑒(𝐵𝑖𝑙𝑙)
6. Sue eats everything Bill eats. ∀𝑥 : 𝑒𝑎𝑡𝑠(𝐵𝑖𝑙𝑙, 𝑥) → 𝑒𝑎𝑡𝑠(𝑆𝑢𝑒, 𝑥)
7. ∀𝑥 : ∀𝑦 : 𝑎𝑙𝑖𝑣𝑒(𝑥) → ¬𝑘𝑖𝑙𝑙𝑒𝑑𝑏𝑦(𝑥, 𝑦)

Clause Form

• 1. ¬ 𝑓 𝑜𝑜𝑑 (𝑥1) ∨ 𝑙𝑖𝑘𝑒𝑠(𝐽𝑜ℎ𝑛, 𝑥1)


• 2. 𝑓 𝑜𝑜𝑑 (𝑎 𝑝 𝑝𝑙𝑒𝑠)
• 3. 𝑓 𝑜𝑜𝑑 (𝑐ℎ𝑖𝑐𝑘𝑒𝑛)
• 4. ¬𝑒𝑎𝑡𝑠(𝑦4, 𝑥4) ∨ 𝑘𝑖𝑙𝑙𝑒𝑑𝑏𝑦(𝑦4, 𝑥4) ∨ 𝑓 𝑜𝑜𝑑 (𝑥4)
• 5. 𝑒𝑎𝑡𝑠(𝐵𝑖𝑙𝑙, 𝑝𝑒𝑎𝑛𝑢𝑡𝑠)
• 6. 𝑎𝑙𝑖𝑣𝑒(𝐵𝑖𝑙𝑙)
• 7. ¬𝑒𝑎𝑡𝑠(𝐵𝑖𝑙𝑙, 𝑥7) ∨ 𝑒𝑎𝑡𝑠(𝑆𝑢𝑒, 𝑥7)
• 8. ¬𝑎𝑙𝑖𝑣𝑒(𝑥8) ∨ ¬𝑘𝑖𝑙𝑙𝑒𝑑𝑏𝑦(𝑥8, 𝑦8)

Resolution Proof that John Likes Peanuts

¬𝑙𝑖𝑘𝑒𝑠(𝐽𝑜ℎ𝑛, 𝑝𝑒𝑎𝑛𝑢𝑡𝑠) (1) ¬ 𝑓 𝑜𝑜𝑑 (𝑥1) ∨ 𝑙𝑖𝑘𝑒𝑠(𝐽𝑜ℎ𝑛, 𝑥1)


¬ 𝑓 𝑜𝑜𝑑 ( 𝑝𝑒𝑎𝑛𝑢𝑡𝑠) (4) ¬𝑒𝑎𝑡𝑠(𝑦4, 𝑥4) ∨ 𝑘𝑖𝑙𝑙𝑒𝑑𝑏𝑦(𝑦4, 𝑥4) ∨ 𝑓 𝑜𝑜𝑑 (𝑥4)
¬𝑒𝑎𝑡𝑠(𝑦4, 𝑝𝑒𝑎𝑛𝑢𝑡𝑠) ∨ 𝑘𝑖𝑙𝑙𝑒𝑑𝑏𝑦(𝑦4, 𝑝𝑒𝑎𝑛𝑢𝑡𝑠) (5) 𝑒𝑎𝑡𝑠(𝐵𝑖𝑙𝑙, 𝑝𝑒𝑎𝑛𝑢𝑡𝑠)
𝑘𝑖𝑙𝑙𝑒𝑑𝑏𝑦(𝐵𝑖𝑙𝑙, 𝑝𝑒𝑎𝑛𝑢𝑡𝑠) (8) ¬𝑎𝑙𝑖𝑣𝑒(𝑥8) ∨ ¬𝑘𝑖𝑙𝑙𝑒𝑑𝑏𝑦(𝑥8, 𝑦8)
¬𝑎𝑙𝑖𝑣𝑒(𝐵𝑖𝑙𝑙) (6) 𝑎𝑙𝑖𝑣𝑒(𝐵𝑖𝑙𝑙)

Substitutions: peanuts/x1; peanuts/x4; Bill/y4; Bill/x8, peanuts/y8

6.5 Forward Chaining in First-Order Logic


Forward chaining in FOL is a data-driven reasoning strategy that applies rules to known facts to
derive new facts until a goal is inferred or no further inferences are possible.
CHAPTER 6. FIRST ORDER LOGIC 98

Algorithm

• Input: A knowledge base of facts (e.g., Fever(John)) and rules (e.g., ∀𝑥(Fever(𝑥) ∧
SoreThroat(𝑥) → Flu(𝑥))).
• Process:
1. Identify rules whose antecedents are satisfied by current facts, using unification to match
variables.
2. Apply these rules to add their consequents to the knowledge base.
3. Repeat until the goal is derived or no new facts can be added.
• Example:
• Facts: Fever(John), SoreThroat(John).
• Rule: ∀𝑥(Fever(𝑥) ∧ SoreThroat(𝑥) → Flu(𝑥)).
• Step 1: Unify 𝑥 = John, check antecedents Fever(John), SoreThroat(John) are true.
• Step 2: Infer Flu(John).

Properties of Forward Chaining

Sound and complete for first-order definite clauses (proof similar to propositional proof)
Datalog = first-order definite clauses + no functions (e.g., crime KB)
FC terminates for Datalog in poly iterations: at most 𝑝 · 𝑛 𝑘 literals
May not terminate in general if 𝛼 is not entailed
This is unavoidable: entailment with definite clauses is semidecidable

Efficiency of Forward Chaining

Simple observation: no need to match a rule on iteration 𝑘 if a premise wasn’t added on iteration
𝑘 −1
⇒ match each rule whose premise contains a newly added literal
Matching itself can be expensive
Database indexing allows 𝑂 (1) retrieval of known facts e.g., query Missile(x) retrieves Missile(M1)
Matching conjunctive premises against known facts is NP-hard
Forward chaining is widely used in deductive databases

Example Knowledge Base: Colonel West

The law says that it is a crime for an American to sell weapons to hostile nations. The country Nono,
an enemy of America, has some missiles, and all of its missiles were sold to it by Colonel West,
who is American.
CHAPTER 6. FIRST ORDER LOGIC 99

Prove that Col. West is a criminal!


... it is a crime for an American to sell weapons to hostile nations:

∀𝑥∀𝑦∀𝑧 𝐴𝑚𝑒𝑟𝑖𝑐𝑎𝑛(𝑥) ∧ 𝑊 𝑒𝑎 𝑝𝑜𝑛(𝑦) ∧ 𝑆𝑒𝑙𝑙𝑠(𝑥, 𝑦, 𝑧) ∧ 𝐻𝑜𝑠𝑡𝑖𝑙𝑒(𝑧) ⇒ 𝐶𝑟𝑖𝑚𝑖𝑛𝑎𝑙 (𝑥)

Nono . . . has some missiles, i.e., ∃𝑥 𝑂𝑤𝑛𝑠(𝑁𝑜𝑛𝑜, 𝑥) ∧ 𝑀𝑖𝑠𝑠𝑖𝑙𝑒(𝑥):

𝑂𝑤𝑛𝑠(𝑁𝑜𝑛𝑜, 𝑀1 ) ∧ 𝑀𝑖𝑠𝑠𝑖𝑙𝑒(𝑀1 )

. . . all of its missiles were sold to it by Colonel West

∀𝑥 𝑀𝑖𝑠𝑠𝑖𝑙𝑒(𝑥) ∧ 𝑂𝑤𝑛𝑠(𝑁𝑜𝑛𝑜, 𝑥) ⇒ 𝑆𝑒𝑙𝑙𝑠(𝑊 𝑒𝑠𝑡, 𝑥, 𝑁𝑜𝑛𝑜)

Missiles are weapons:

𝑀𝑖𝑠𝑠𝑖𝑙𝑒(𝑥) ⇒ 𝑊 𝑒𝑎 𝑝𝑜𝑛(𝑥)

An enemy of America counts as “hostile”:

𝐸𝑛𝑒𝑚𝑦(𝑥, 𝐴𝑚𝑒𝑟𝑖𝑐𝑎) ⇒ 𝐻𝑜𝑠𝑡𝑖𝑙𝑒(𝑥)

West, who is American . . .

𝐴𝑚𝑒𝑟𝑖𝑐𝑎𝑛(𝑊 𝑒𝑠𝑡)

The country Nono, an enemy of America . . .

𝐸𝑛𝑒𝑚𝑦(𝑁𝑜𝑛𝑜, 𝐴𝑚𝑒𝑟𝑖𝑐𝑎)

Forward Chaining Proof

The forward chaining proof is illustrated in the following stages:

American(West) Missile(M1) Owns(Nono,M1) Enemy(Nono,America)


Initial Facts
CHAPTER 6. FIRST ORDER LOGIC 100

Weapon(M1) Missile(M1)

Missile(M1)
Intermediate Steps Sells(West
Owns(Nono

Hostile(Nono) Enemy(Nono

Weapon(M1) Missile(M1)

Missile(M1)
Sells(West
Final Proof Criminal(West) Owns(Nono

Hostile(Nono) Enemy(Nono

American(West)

Final Proof Criminal(West)

Weapon(M1) Sells(West Hostile(Nono) American(West)

Missile(M1) Missile(M1) Owns(Nono Enemy(Nono

Applications in Knowledge Bases

• Diagnosis Systems: Inferring diseases from symptoms (e.g., MYCIN infers flu from fever and
sore throat).
• Planning: Deriving action sequences in robotic systems (e.g., inferring cleaning actions for a
vacuum cleaner).
• Knowledge Base Updates: Automatically adding new facts based on rules (e.g., updating a
medical database with inferred diagnoses).
CHAPTER 6. FIRST ORDER LOGIC 101

6.6 Backward Chaining in First-Order Logic


Backward chaining in FOL is a goal-driven reasoning strategy that starts with a hypothesis (goal)
and recursively decomposes it into sub-goals to verify if supporting facts exist.

Algorithm

• Input: A knowledge base of facts and rules, and a goal (e.g., Flu(John)).
• Process:
1. Start with the goal and identify rules whose consequents match it, using unification.
2. Treat the antecedents as sub-goals and recursively verify them against facts or other rules.
3. Continue until all sub-goals are satisfied (goal is proven) or no supporting facts are found
(goal fails).
• Example:
• Goal: Flu(John).
• Rule: ∀𝑥(Fever(𝑥) ∧ SoreThroat(𝑥) → Flu(𝑥)).
• Step 1: Unify goal with rule consequent: 𝑥 = John.
• Step 2: Sub-goals: Fever(John), SoreThroat(John).
• Step 3: Verify both sub-goals are true in the knowledge base.
• Conclusion: Flu(John) is proven.

Properties of Backward Chaining

Depth-first recursive proof search: space is linear in size of proof


Incomplete due to infinite loops ⇒ fix by checking current goal against every goal on stack
Inefficient due to repeated subgoals (both success and failure) ⇒ fix using caching of previous
results (extra space!)
Widely used (without improvements!) for logic programming

Backward Chaining Proof

The backward chaining proof is illustrated in the following stages:

Criminal(West)
Initial Goal
CHAPTER 6. FIRST ORDER LOGIC 102

American(West)

Weapon(y)
Goal Decomposition Criminal(West)
Sells(West

Hostile(Nono)

American(West)

Weapon(M1) Missile(M1)

Sub-Goal Resolution Criminal(West) Missile(M1)


Sells(West
Owns(Nono

Hostile(Nono) Enemy(Nono

Applications in Prolog Reasoning

• Logic Programming: Used in Prolog to resolve queries by recursively matching goals to


rules and facts (e.g., querying a family tree to find ancestors).
• Expert Systems: Verifying hypotheses (e.g., checking if symptoms support a disease
diagnosis).
• Decision-Making: Determining if a goal (e.g., “clean room”) is achievable based on current
conditions.

Practical Applications and Examples


FOL is widely used in AI for tasks requiring complex relational reasoning. Key applications include:

• Knowledge Bases: Representing and querying structured knowledge (e.g., family trees,
medical databases).
• Natural Language Processing: Modeling linguistic relationships (e.g., Parent(𝑥, 𝑦) for family
relations in text).
CHAPTER 6. FIRST ORDER LOGIC 103

Aspect Forward Chaining Backward Chaining


Approach Data-driven: Starts with facts to derive Goal-driven: Starts with a goal to verify
new facts. supporting facts.
Search Strategy Breadth-first: Applies all applicable Depth-first: Explores one path deeply
rules at each step. before others.
Direction Bottom-up: From facts to conclusions. Top-down: From goal to facts.
Efficiency May explore irrelevant rules, reducing Risk of infinite loops in recursive
efficiency. searches.
Unification Matches facts to rule antecedents. Matches goals to rule consequents.
Applications Knowledge base updates, diagnostics, Prolog-style query resolution, expert sys-
planning. tems.

Table 6.1: Comparison of Forward and Backward Chaining in FOL

• Robotic Planning: Coordinating actions (e.g., vacuum cleaner agent rules like ∀𝑥(Room(𝑥) ∧
Dirty(𝑥) → Clean(𝑥))).

Case Study: Medical Diagnosis System:

• Knowledge Base:
• Facts: Fever(John), SoreThroat(John).
• Rule: ∀𝑥(Fever(𝑥) ∧ SoreThroat(𝑥) → Flu(𝑥)).
• Forward Chaining:
• Unify 𝑥 = John, apply rule, infer Flu(John).
• Backward Chaining:
• Goal: Flu(John).
• Sub-goals: Fever(John), SoreThroat(John).
• Verify sub-goals, conclude Flu(John).
• Resolution:
• Convert to CNF: ¬Fever(𝑥) ∨ ¬SoreThroat(𝑥) ∨ Flu(𝑥), Fever(John), SoreThroat(John).
• Negate goal: ¬Flu(John).
• Resolve to derive empty clause, proving Flu(John).

Limitations of First-Order Logic


While FOL is highly expressive, it has limitations:

• Computational Complexity: Inference in FOL is semi-decidable (may not terminate for


some queries).
CHAPTER 6. FIRST ORDER LOGIC 104

• Handling Uncertainty: Cannot easily represent probabilities or degrees of truth (addressed


by fuzzy logic).
• Scalability: Large knowledge bases require efficient algorithms to manage complexity.
Chapter 7
Planning

7.1 Planning Agent


A planning agent interacts with the environment through sensors and actuators to achieve goals by
executing a sequence of actions. The agent perceives the environment using sensors and acts upon
it using actuators, as illustrated in Figure 7.1. The sequence of actions (A1, A2, A3) represents the
plan the agent follows.

Figure 7.1: Planning Agent interacting with the environment

Purpose of Planning
The purpose of planning is to find a sequence of actions that achieves a given goal when performed
starting in a given state. In other words, given a set of operator instances (defining the possible
primitive actions by the agent), an initial state description, and a goal state description or predicate,
the planning agent computes a plan. This process is depicted in Figure 7.2.

7.2 What is Planning?


Planning in Artificial Intelligence (AI) is the process of finding a sequence of actions that transforms
an initial state into a desired goal state. It is a fundamental component of intelligent systems,

105
CHAPTER 7. PLANNING 106

Operator
Start
Instances PLAN

Final

Figure 7.2: Planning process from Start/Final through Operator Instances to a Plan.

enabling agents to make decisions and achieve objectives in complex environments. Planning
involves reasoning about actions, their preconditions, and their effects to construct a feasible path
from the starting point to the goal.
The primary goal of planning is to generate a plan, which is a sequence or partially ordered set
of actions that, when executed, achieves the desired outcome. For example, in robotics, planning
might involve determining a sequence of movements for a robot to navigate from one location to
another while avoiding obstacles.

7.3 Why Planning?


Intelligent agents must operate in real-world environments, which are often dynamic, uncertain, and
complex. To act effectively, agents require the ability not only to react to immediate stimuli but
also to anticipate the consequences of their actions. This necessity gives rise to the concept of
planning.
Planning is important for several reasons:

1. Operating in the world


Agents are situated in environments where actions have consequences. To function intelligently,
they must determine which actions to take.
2. Selecting intelligent actions
Not all actions are equally beneficial. Planning enables agents to identify and execute actions
that are most likely to achieve their objectives.
3. Composing actions into sequences
Complex goals cannot usually be achieved through a single action. Planning provides a
mechanism to compose multiple actions into a coherent sequence.
4. Changing the world to suit goals
Intelligent agents must sometimes modify their environment in order to satisfy their needs or
to create conditions favorable for achieving future objectives.
CHAPTER 7. PLANNING 107

Figure 7.3: Why Planning-Agent

5. Reasoning about outcomes


Planning involves predicting what the world will look like after executing a given sequence of
actions. This requires explicit reasoning about action effects and world dynamics.
6. Adapting to dynamic environments
Since real environments are rarely static, agents must plan with awareness of potential changes.
This allows them to remain robust in the face of uncertainty.

7.3.1 Key Characteristics of Planning

• Initial State: The starting configuration of the system, describing the current world.
• Goal State: The desired configuration or condition the system aims to achieve.
• Actions/Operators: Operations that transform the state of the system, each with preconditions
(what must be true before the action) and effects (what changes after the action).

7.3.2 Application Areas

Planning is widely used in various domains, including:

• Robotics: Path planning for autonomous robots to navigate environments.


• Automated Reasoning: Solving logical problems or theorem proving.
• Logistics: Optimizing schedules, routes, or resource allocation.
• Game AI: Developing strategies for non-player characters to achieve objectives.
CHAPTER 7. PLANNING 108

7.4 Planning Problem Definition


A planning problem is formally defined by the following components:

• Initial State: A complete description of the starting conditions, often represented as a set of
facts or predicates. For example, in a blocks world, the initial state might include facts like
on(A, Table) and clear(B).
• Goal State: A partial or complete description of the desired outcome. For instance, on(A,
B) and on(B, C) in the blocks world.
• Actions/Operators: A set of possible actions, each defined by:
• Preconditions: Conditions that must hold for the action to be executable.
• Effects: Changes to the state after the action is performed, including additions (new facts)
and deletions (facts that are no longer true).

For example, consider a simple blocks world problem:

• Initial State: Block A is on the table, Block B is on the table, and both are clear.
• Goal State: Block A is on Block B.
• Action: move(A, Table, B)
• Preconditions: clear(A), clear(B), on(A, Table).
• Effects: on(A, B), not on(A, Table), not clear(B).

Goal State

Initial State
A

A B B

Table Table Table

Figure 7.4: Blocks world example: Initial and goal states.

7.5 The Language of Planning Problems


Planning algorithms should take advantage of the logical structure of the problem. The key is to
find a language that is:

• Expressive enough to describe a wide variety of problems, but


• Restrictive enough to allow efficient algorithms to operate over it.

Thus, the problem should be expressed in a suitable logical language.


Planning is considered different from general problem solving because of:
CHAPTER 7. PLANNING 109

• The way states, goals, and actions are represented


• The way action sequences are constructed

7.5.1 Representation of States

Planners decompose the world into logical conditions and represent a state as a conjunction of
positive literals.
Example: In propositional logic,
𝑃∨𝑄

represents a state where either 𝑃 or 𝑄 is true.

7.5.2 Representation of Goals

A goal is a partially specified state, represented as a conjunction of positive ground literals.


Example: The goal is 𝑃 ∧ 𝑄. A state 𝑠 = 𝑃 ∧ 𝑄 ∧ 𝑅 satisfies this goal because it contains all atoms
required by 𝑔.

7.5.3 Representation of Actions

An action is specified by:

• Preconditions: what must hold before it can be executed.


• Effects: how the world changes after execution.

Example Action Schema:


Action(𝐹𝑙 𝑦( 𝑝, 𝐹𝑟𝑜𝑚, 𝑇 𝑜),

PRECOND: 𝐴𝑡 ( 𝑝, 𝐹𝑟𝑜𝑚) ∧ 𝑃𝑙𝑎𝑛𝑒( 𝑝) ∧ 𝐴𝑖𝑟 𝑝𝑜𝑟𝑡 (𝐹𝑟𝑜𝑚) ∧ 𝐴𝑖𝑟 𝑝𝑜𝑟𝑡 (𝑇 𝑜),

EFFECT: ¬𝐴𝑡 ( 𝑝, 𝐹𝑟𝑜𝑚) ∧ 𝐴𝑡 ( 𝑝, 𝑇 𝑜))

This is called an action schema, meaning it represents multiple actions that can be derived by
instantiating variables (𝑝, 𝐹𝑟𝑜𝑚, 𝑇 𝑜).
Action schemas generally have three parts:

1. Action name and parameter list


2. Preconditions: a conjunction of function-free positive literals
3. Effects: a conjunction of function-free literals (state changes)

Some planning systems divide the effects into:

• Add list: positive literals


• Delete list: negative literals
CHAPTER 7. PLANNING 110

Illustration of an Action

State 𝑠: 𝐴𝑡 ( 𝑝, 𝐹𝑟𝑜𝑚) Action: 𝐹𝑙 𝑦( 𝑝, 𝐹𝑟𝑜𝑚, 𝑇 𝑜) State 𝑠′: ¬𝐴𝑡 ( 𝑝, 𝐹𝑟𝑜𝑚) ∧ 𝐴𝑡 ( 𝑝, 𝑇 𝑜)

Starting in state 𝑠, executing an applicable action 𝑎 results in a new state 𝑠′ that is the same as 𝑠
except for the changes described by the action’s effects.

Example: Planning for a Birthday Party

To illustrate how planning problems can be modeled, consider the following scenario.

Goal: Have a birthday party


Current Situation:

• Agent is at home

• Has flour

• Does not have butter

• Does not have sugar

Tasks To Do:

• Invite friends

• Buy butter

• Buy sugar

• Buy balloons

• Decorate house

• Bake cake

7.6 Example: Shopping Planning Problem


Consider the following planning problem.

Start State

𝐴𝑡 (𝐻𝑜𝑚𝑒) ∧ 𝑆𝑒𝑙𝑙𝑠(𝑆𝑀, 𝑀𝑖𝑙 𝑘) ∧ 𝑆𝑒𝑙𝑙𝑠(𝑆𝑀, 𝐵𝑎𝑛𝑎𝑛𝑎) ∧ 𝑆𝑒𝑙𝑙𝑠(𝐻𝑊 𝑆, 𝐷𝑟𝑖𝑙𝑙) ∧ 𝐿𝑜𝑐(𝐻𝑜𝑚𝑒)

∧𝐿𝑜𝑐(𝑆𝑀) ∧ 𝐿𝑜𝑐(𝐻𝑊 𝑆)
CHAPTER 7. PLANNING 111

Operators

Buy(x)
PRE: 𝐴𝑡 (𝑠𝑡𝑜𝑟𝑒) ∧ 𝑆𝑒𝑙𝑙𝑠(𝑠𝑡𝑜𝑟𝑒, 𝑥)

EFF: 𝐻𝑎𝑣𝑒(𝑥)

Go(x, y)
PRE: 𝐴𝑡 (𝑥) ∧ 𝐿𝑜𝑐(𝑦)

EFF: 𝐴𝑡 (𝑦) ∧ ¬𝐴𝑡 (𝑥)

Goal

𝐻𝑎𝑣𝑒(𝑀𝑖𝑙 𝑘) ∧ 𝐻𝑎𝑣𝑒(𝐵𝑎𝑛𝑎𝑛𝑎) ∧ 𝐻𝑎𝑣𝑒(𝐷𝑟𝑖𝑙𝑙)

7.6.1 Solution Outline

Initial state:

𝐴𝑡 (𝐻𝑜𝑚𝑒) ∧ ¬𝐻𝑎𝑣𝑒(𝑀𝑖𝑙 𝑘) ∧ ¬𝐻𝑎𝑣𝑒(𝐵𝑎𝑛𝑎𝑛𝑎) ∧ ¬𝐻𝑎𝑣𝑒(𝐷𝑟𝑖𝑙𝑙) ∧ . . .

Notes:

• A state description does not need to be complete.

• If a positive literal is not mentioned, it is assumed to be false.

• Goals are expressed as conjunctions of literals.

Goal state:

𝐴𝑡 (𝐻𝑜𝑚𝑒) ∧ 𝐻𝑎𝑣𝑒(𝑀𝑖𝑙 𝑘) ∧ 𝐻𝑎𝑣𝑒(𝐵𝑎𝑛𝑎𝑛𝑎) ∧ 𝐻𝑎𝑣𝑒(𝐷𝑟𝑖𝑙𝑙)

Variables in goals: Goals may also contain variables. For example, being at a store that
sells milk can be expressed as:

𝐴𝑡 (𝑥) ∧ 𝑆𝑒𝑙𝑙𝑠(𝑥, 𝑀𝑖𝑙 𝑘)

7.6.2 Operator Schema

An operator schema is a general action with variables, which becomes a family of specific actions
when the variables are instantiated. Each variable must have a value, and the schema specifies:

• Preconditions: Must be true in a state 𝑠 for the action to apply.


CHAPTER 7. PLANNING 112

• Effects: Describe how the state changes when the action is executed.

Example: If the initial situation includes:

𝐴𝑡 (𝐻𝑜𝑚𝑒) ∧ 𝑃𝑎𝑡ℎ(𝐻𝑜𝑚𝑒, 𝑆𝑢 𝑝𝑒𝑟𝑚𝑎𝑟 𝑘𝑒𝑡)

then the action:


𝐺𝑜(𝐻𝑜𝑚𝑒, 𝑆𝑢 𝑝𝑒𝑟𝑚𝑎𝑟 𝑘𝑒𝑡)

is applicable, producing the new situation:

¬𝐴𝑡 (𝐻𝑜𝑚𝑒) ∧ 𝐴𝑡 (𝑆𝑢 𝑝𝑒𝑟𝑚𝑎𝑟 𝑘𝑒𝑡) ∧ 𝑃𝑎𝑡ℎ(𝐻𝑜𝑚𝑒, 𝑆𝑢 𝑝𝑒𝑟𝑚𝑎𝑟 𝑘𝑒𝑡) . . .

The result is:

• All positive literals in Effect(𝑜) hold.


• All literals in the previous state 𝑠 persist.
• Negative literals in Effect(𝑜) are removed.

7.7 Types of Planning


Planning can be broadly categorized into two types: classical and non-classical.

7.7.1 Classical Planning

Classical planning assumes a deterministic, fully observable environment where the outcomes of
actions are predictable, and the agent has complete knowledge of the state. This is the focus of this
chapter. Key assumptions include:

• Deterministic actions: Each action has a known outcome.


• Fully observable: The agent knows the complete state at all times.
• Discrete states and actions: The system can be modeled with finite states and actions.

For example, in a puzzle like the 8-puzzle, the initial configuration, possible moves (actions), and
desired configuration (goal) are fully known, and each move has a predictable outcome.

7.7.2 Non-Classical Planning

Non-classical planning deals with more complex scenarios, such as:

• Non-deterministic Planning: Actions may have uncertain outcomes.


• Partially Observable Planning: The agent has incomplete knowledge of the state.
CHAPTER 7. PLANNING 113

• Temporal Planning: Actions have durations or time constraints.


• Hierarchical Planning: Plans are organized into high-level and low-level tasks.

For this lecture, we focus on classical planning due to its foundational role and simplicity.

Action 1 State 1 Action 3


Initial State Goal State

Action 2 State 2 Action 4

Figure 7.5: State transitions in classical planning.

7.8 Planning Approaches


Several approaches exist for solving planning problems in AI. Below, we outline three key methods
used in classical planning.

7.8.1 State Space Search

State space search explores the space of possible states by applying actions to transition from the
initial state to the goal state. It can be performed using:

• Forward Search (Progression): Start from the initial state and apply actions to reach the goal.
This is like a breadth-first or depth-first search.
• Backward Search (Regression): Start from the goal state and work backward to find actions
that lead to the initial state.

State space search is intuitive but can suffer from large state spaces, making it computationally
expensive. Heuristics, such as A* search, are often used to guide the search efficiently.

7.8.2 Partial Order Planning

Partial order planning (POP) constructs plans by defining a partial order of actions rather than a strict
sequence. It starts with an empty plan and iteratively adds actions to satisfy preconditions, allowing
flexibility in the order of execution. POP is particularly useful when actions can be performed in
parallel or when the exact order is not critical.
For example, in a logistics problem, delivering two packages to different locations can be done in
any order, as long as both are delivered. POP allows such flexibility.
CHAPTER 7. PLANNING 114

7.8.3 Planning Graphs

Planning graphs are a data structure used to efficiently solve planning problems. A planning graph
consists of alternating layers of states (facts) and actions, capturing all possible actions and their
effects at each step. The graph is used to:

• Identify mutex (mutually exclusive) relationships between actions or states.


• Extract a valid plan using algorithms like GraphPlan.

Planning graphs are particularly effective for problems with many parallel actions and help reduce
the search space.

State Layer 0 Action Layer 1 State Layer 1 Action Layer 2 Goal State

Figure 7.6: Structure of a planning graph.

7.9 Representation in Planning


In the field of artificial intelligence (AI), particularly within classical planning, the representation of
states, goals, and actions forms the foundation for effective reasoning and problem-solving. Classical
planning operates under specific assumptions: the environment is fully observable, deterministic,
finite, static (changes occur only due to agent actions), and discrete (in time, actions, objects, and
effects). The representation framework, initially popularized by the STRIPS (Stanford Research
Institute Problem Solver) language, has evolved with extensions like ADL (Action Description
Language) and PDDL (Planning Domain Definition Language) to handle more complex scenarios.

7.9.1 Representation of States

A state describes the configuration of the world at a specific moment. In the STRIPS framework,
states are represented as a conjunction of positive ground literals—function-free atomic propositions
that are true in that state. For example, a state might be 𝑃𝑜𝑜𝑟 ∧ 𝑈𝑛𝑘𝑛𝑜𝑤𝑛, or in a delivery robot
domain, 𝐴𝑡 (𝑅𝑜𝑏𝑜𝑡, 𝐿𝑎𝑏) ∧ ¬𝐻𝑎𝑠𝐶𝑜 𝑓 𝑓 𝑒𝑒 ∧ 𝑊 𝑎𝑛𝑡𝑠𝐶𝑜 𝑓 𝑓 𝑒𝑒(𝑆𝑎𝑚) ∧ ¬𝑀𝑎𝑖𝑙𝑊 𝑎𝑖𝑡𝑖𝑛𝑔 ∧ 𝐻𝑎𝑠𝑀𝑎𝑖𝑙.
The closed-world assumption applies: any unmentioned literal is considered false. This is illustrated
in Figure 7.7, where the state is depicted as a set of active literals with unmentioned ones implicitly
false.
States can also be viewed as assignments to features or variables, such as 𝑅𝐿𝑜𝑐 = 𝑙𝑎𝑏 and
𝑅𝐻𝐶 = 𝑓 𝑎𝑙𝑠𝑒. Advanced representations like ADL allow negative literals, enabling partial
specifications.
CHAPTER 7. PLANNING 115

State
Literals: Implicitly False:
𝐴𝑡 (𝑅𝑜𝑏𝑜𝑡, 𝐿𝑎𝑏) ¬𝐻𝑎𝑠𝐶𝑜 𝑓 𝑓 𝑒𝑒
𝐻𝑎𝑠𝑀𝑎𝑖𝑙 ¬𝑊 𝑎𝑛𝑡𝑠𝐶𝑜 𝑓 𝑓 𝑒𝑒(𝑆𝑎𝑚)

Figure 7.7: Representation of a state with active and implicitly false literals.

7.9.2 Representation of Goals

A goal represents the desired world configuration that the planning agent aims to achieve. It is
typically a conjunction of positive ground literals, such as 𝑅𝑖𝑐ℎ ∧ 𝐹𝑎𝑚𝑜𝑢𝑠 or 𝐴𝑡 (𝑃𝑙𝑎𝑛𝑒2, 𝑇 𝑎ℎ𝑖𝑡𝑖).
A state satisfies a goal if it includes all specified literals, as shown in Figure 7.8.

Goal
Target Literals: Satisfied by State with
𝐴𝑡 (𝑃𝑙𝑎𝑛𝑒2, 𝑇 𝑎ℎ𝑖𝑡𝑖) 𝑅𝑖𝑐ℎ ∧ 𝐴𝑡 (𝑃𝑙𝑎𝑛𝑒2, 𝑇 𝑎ℎ𝑖𝑡𝑖) ∧ 𝐹𝑎𝑚𝑜𝑢𝑠
𝑅𝑖𝑐ℎ

Figure 7.8: Representation of a goal and its satisfaction by a state.

Goals can be achievement goals (true in the final state), maintenance goals (true throughout),
transient goals (true at some point), or resource goals (optimizing resources). ADL extends this
with disjunctions (e.g., 𝑃 ∨ 𝑄), negations, and quantified variables.

7.9.3 Representation of Actions

Actions transform states and are defined using schemas in STRIPS, consisting of:
- Action Name and Parameters: E.g., 𝐹𝑙 𝑦( 𝑝, 𝑓 𝑟𝑜𝑚, 𝑡𝑜).
- Preconditions: Literals that must hold, e.g., 𝐴𝑡 ( 𝑝, 𝑓 𝑟𝑜𝑚) ∧ 𝑃𝑙𝑎𝑛𝑒( 𝑝) ∧ 𝐴𝑖𝑟 𝑝𝑜𝑟𝑡 ( 𝑓 𝑟𝑜𝑚) ∧
𝐴𝑖𝑟 𝑝𝑜𝑟𝑡 (𝑡𝑜).
- Effects: Changes to the state, e.g., ¬𝐴𝑡 ( 𝑝, 𝑓 𝑟𝑜𝑚) ∧ 𝐴𝑡 ( 𝑝, 𝑡𝑜), split into add and delete lists.

Figure 7.9 illustrates an action schema and its application.

Action: 𝐹𝑙 𝑦( 𝑝, 𝑓 𝑟𝑜𝑚, 𝑡𝑜)


Preconditions: Effects:
Resulting State
𝐴𝑡 ( 𝑝, 𝑓 𝑟𝑜𝑚) ¬𝐴𝑡 ( 𝑝, 𝑓 𝑟𝑜𝑚)
𝑃𝑙𝑎𝑛𝑒( 𝑝) 𝐴𝑡 ( 𝑝, 𝑡𝑜)

Figure 7.9: Representation of an action schema with preconditions and effects.


CHAPTER 7. PLANNING 116

An action schema represents ground actions (e.g., 𝐹𝑙 𝑦(𝑃𝑙𝑎𝑛𝑒1, 𝐽𝐹𝐾, 𝐿 𝐴𝑋)). Applicability requires
all preconditions to be true, with the resulting state adding positive effects and removing matching
negative effects.
- Frame Problem: Unchanged elements persist unless deleted (see Figure 7.10). - Qualification
Problem: All preconditions must be listed. - Ramification Problem: Indirect effects are hard to
capture.

State 1 𝐹𝑙 𝑦( 𝑝, 𝑓 𝑟𝑜𝑚, 𝑡𝑜) State 2


𝐴𝑡 ( 𝑝, 𝑓 𝑟𝑜𝑚) ¬𝐴𝑡 ( 𝑝, 𝑓 𝑟𝑜𝑚) ∧ 𝐴𝑡 ( 𝑝, 𝑡𝑜)

Unchanged: 𝑃𝑙𝑎𝑛𝑒( 𝑝) (implicit)

Figure 7.10: Illustration of the frame problem where unchanged literals persist.

Extensions and Enhancements - ADL: Supports negative preconditions, conditional effects, and
types. - PDDL: Adds numeric fluents and durative actions.
Example in Blocks World - Action: 𝑠𝑡𝑎𝑐𝑘 (𝑥, 𝑦) - Precond: ℎ𝑜𝑙𝑑𝑖𝑛𝑔(𝑥) ∧ 𝑐𝑙𝑒𝑎𝑟 (𝑦) - Effect:
¬ℎ𝑜𝑙𝑑𝑖𝑛𝑔(𝑥) ∧ ¬𝑐𝑙𝑒𝑎𝑟 (𝑦) ∧ 𝑜𝑛(𝑥, 𝑦) ∧ 𝑐𝑙𝑒𝑎𝑟 (𝑥) ∧ ℎ𝑎𝑛𝑑𝑒𝑚 𝑝𝑡𝑦
Figure 7.11 shows this action’s transformation.

𝑠𝑡𝑎𝑐𝑘 (𝑥, 𝑦)
𝑥 𝑥

𝑦 𝑦

Preconditions: Effects:
ℎ𝑜𝑙𝑑𝑖𝑛𝑔(𝑥) ∧ 𝑐𝑙𝑒𝑎𝑟 (𝑦) 𝑜𝑛(𝑥, 𝑦) ∧ ¬𝑐𝑙𝑒𝑎𝑟 (𝑦)

Figure 7.11: Transformation in Blocks World due to the 𝑠𝑡𝑎𝑐𝑘 action.

Alternative Representations Actions can be represented as state-space graphs or feature-based causal


rules, though these are less common in classical planning.
Chapter 8
Forms of learning

8.1 Introduction
Machine learning is like teaching a computer to learn from examples, much like how you learn
by practicing or observing. There are different ways machines learn, called learning paradigms,
each suited for specific tasks, like recognizing images, grouping similar items, or making decisions
in games. This chapter explains four main types—supervised, unsupervised, semi-supervised,
and reinforcement learning—in a way that’s easy for students to understand. We’ll use real-world
examples, including recent trends involving ChatGPT and ShellGPT, to show how these methods
work. Key terms and tips are highlighted in colorful boxes to make them stand out.

Machine Learning
Machine learning is a part of artificial intelligence where computers learn patterns from data
to make predictions or decisions, like recognizing a photo or suggesting a movie, without
being explicitly told what to do.

[title= Why Learning Types Matter] Each learning type fits different problems. For example, if
you have clear examples with answers (like labeled photos), supervised learning is best. If you’re
exploring data without labels, like grouping customers, unsupervised learning helps. Choosing the
right type saves time and improves results.

8.2 Supervised Learning


Supervised learning is like learning with a teacher. The computer gets a dataset with inputs (like a
photo) and correct answers (like "cat" or "dog"). It learns to predict answers for new data. There are
two main tasks: classification (picking categories, like spam or not spam) and regression (predicting
numbers, like house prices).

Supervised Learning
Supervised learning uses a dataset with inputs and their correct answers (labels) to train a
model. The model learns to predict answers for new inputs, like identifying if an email is
spam based on examples.

117
CHAPTER 8. FORMS OF LEARNING 118

[title= Need for Labeled Data] Supervised learning needs lots of labeled data, which can be expensive
to create. For example, labeling medical images for cancer detection requires doctors’ expertise,
making it time-consuming.

8.2.1 How It Works

The model looks at examples, finds patterns, and creates a rule to predict answers. For instance, to
classify emails, it might notice that spam emails often contain words like "win" or "free." Common
tools include decision trees (like a flowchart), neural networks (like a brain), and logistic regression
(for yes/no predictions).

8.2.2 Examples

1. ChatGPT for Text Classification (2025): ChatGPT, built by OpenAI, uses supervised learning
to classify text, like detecting if a tweet is positive or negative. It’s trained on massive datasets
of labeled text (e.g., reviews marked as "happy" or "sad"). In 2025, ChatGPT Plus users use this
for real-time sentiment analysis on social media. [Link][]([Link]
2. ShellGPT for Command Prediction (2025): ShellGPT, a command-line tool, predicts
Linux commands from natural language inputs, like "find all text files." It’s trained on
labeled pairs of user prompts and correct commands, helping developers work faster. For
example, typing "rename file [Link] to [Link]" gives the command mv [Link] [Link].
[Link][]([Link]
3. Image Recognition: Apps like Google Photos use supervised learning to tag photos as "beach"
or "party" by training on labeled images.
4. House Price Prediction: Zillow predicts house prices using features like size and location,
trained on past sale prices.
5. Spam Email Filtering: Gmail filters spam by learning from emails users mark as spam or not.
6. Medical Diagnosis: Models predict diseases (e.g., diabetes) from labeled patient data like
blood tests.
7. Stock Price Prediction: Predicting stock prices based on historical data and trends.
8. Fraud Detection: Banks flag suspicious credit card transactions using labeled fraud data.
9. Handwriting Recognition: Converting handwritten notes to text, trained on labeled hand-
writing samples.
10. Weather Forecasting: Predicting tomorrow’s temperature using past weather data.

8.3 Unsupervised Learning


Unsupervised learning is like exploring without a guide. The computer gets data without answers
and finds patterns, like grouping similar items or simplifying complex data. It’s great for discovering
hidden structures.
CHAPTER 8. FORMS OF LEARNING 119

Unsupervised Learning
Unsupervised learning analyzes data without labels to find patterns, like grouping similar
customers or reducing data complexity, without being told what to look for.

No Labels Needed
Unsupervised learning is perfect when you don’t have labeled data or want to explore unknown
patterns. For example, retailers use it to find customer groups without knowing what those
groups are.

8.3.1 How It Works

The model groups data (clustering) or simplifies it (dimensionality reduction). For clustering, it
might group customers by shopping habits. For dimensionality reduction, it shrinks data while
keeping important details, like turning 100 features into 2 for visualization. Common tools include
K-Means (for grouping) and PCA (for simplifying data).

8.3.2 Examples

1. ChatGPT for Topic Discovery (2025): ChatGPT uses unsupervised learning to find
topics in large text datasets, like grouping news articles into politics or sports with-
out labels. In 2025, it helps researchers analyze trends in social media posts. chat-
[Link][]([Link]
2. ShellGPT for Log Analysis (2025): ShellGPT analyzes server logs without labels to find
patterns, like grouping similar errors. For example, piping cat /var/log/[Link] |
sgpt "find error patterns" groups errors like "connection timeout" for developers to
fix. [Link][]([Link]
3. Customer Segmentation: Amazon groups shoppers by purchase habits for targeted ads.
4. Anomaly Detection: Cybersecurity tools spot unusual network activity, like hacks.
5. Recommendation Systems: Netflix suggests movies by grouping users with similar tastes.
6. Image Compression: Reducing image file sizes while keeping quality.
7. Market Basket Analysis: Finding items often bought together, like bread and butter.
8. Genomic Clustering: Grouping genes with similar patterns for medical research.
9. Social Network Analysis: Finding friend groups in social media networks.
10. Topic Modeling: Grouping blog posts into themes like travel or tech.
CHAPTER 8. FORMS OF LEARNING 120

8.4 Semi-Supervised Learning


Semi-supervised learning mixes a small amount of labeled data with lots of unlabeled data. It’s
like having a few answers to guide the learning process when labeling everything is too hard or
expensive.

Semi-Supervised Learning
Semi-supervised learning uses a small set of labeled data and a large set of unlabeled data to
train a model, combining the strengths of supervised and unsupervised learning to improve
predictions.

[title= Cost-Effective] This method saves time and money by needing fewer labels. For example,
labeling a few images for a self-driving car can guide the model to learn from thousands of unlabeled
images.

8.4.1 How It Works

The model starts with labeled data to learn basic patterns, then uses unlabeled data to refine them.
It might assume similar data points have similar labels (smoothness) or form clusters. Techniques
include self-training (guessing labels for unlabeled data) and graph-based methods (spreading labels
like a network).

8.4.2 Examples

1. ChatGPT for Text Labeling (2025): ChatGPT uses semi-supervised learning to clas-
sify text with few labeled examples. For instance, with a few labeled reviews, it la-
bels thousands of unlabeled ones, used in 2025 for analyzing product feedback. chat-
[Link][]([Link]
2. ShellGPT for Command Suggestions (2025): ShellGPT learns from a few labeled command-
prompt pairs and many unlabeled user inputs to suggest commands, like suggesting ls for "list
files." [Link][]([Link]
3. Image Segmentation: Labeling a few pixels in medical images to segment tumors in many
images.
4. Speech Recognition: Using a small set of transcribed audio to improve recognition on
unlabeled audio.
5. Web Page Classification: Labeling a few websites to categorize many others.
6. Protein Structure Prediction: Using a few known protein structures to predict others.
7. Document Classification: Labeling key documents to classify an archive.
8. Medical Image Analysis: Labeling a few scans to guide analysis of many.
CHAPTER 8. FORMS OF LEARNING 121

9. Sentiment Analysis: Labeling some tweets to classify many others.


10. Geospatial Analysis: Labeling some satellite images to classify land use.

8.5 Reinforcement Learning


Reinforcement learning is like training a pet with rewards. A computer "agent" learns by trying
actions in an environment, getting rewards (good) or penalties (bad). It aims to maximize rewards
over time.

Reinforcement Learning
Reinforcement learning involves an agent learning to make decisions by trying actions in an
environment, receiving rewards or penalties, and optimizing for the highest total reward.

[title= Learning by Doing] This method mimics trial-and-error learning, like a child learning to ride
a bike. It’s powerful for tasks like games or robotics but needs careful reward design to avoid bad
habits.

8.5.1 How It Works

The agent observes the environment’s state, chooses an action, and gets a reward. It learns a strategy
(policy) to pick actions that maximize future rewards. Tools include Q-Learning (tracking action
values) and Deep Q-Networks (using neural networks for complex tasks).

8.5.2 Examples

1. ChatGPT for Interactive Learning (2025): ChatGPT’s conversational abilities are enhanced
by reinforcement learning, where it learns from user feedback (e.g., thumbs-up on good
responses) to improve answers. In 2025, this powers real-time voice chats on its app.
[Link][]([Link]
2. ShellGPT for Workflow Optimization (2025): ShellGPT learns to suggest efficient command
sequences by trial and error, rewarded for faster task completion, like optimizing a script for
file processing. [Link][]([Link]
3. Game Playing: AlphaGo learned to play Go by playing millions of games, rewarded for wins.
4. Robotics: A robot arm learns to pick up objects, rewarded for success.
5. Autonomous Driving: Cars learn to navigate safely, rewarded for avoiding crashes.
6. Stock Trading: Agents trade stocks, rewarded for profit.
7. Recommendation Systems: Suggesting products based on user clicks.
8. Drone Navigation: Drones learn to avoid obstacles, rewarded for reaching goals.
CHAPTER 8. FORMS OF LEARNING 122

9. Healthcare Optimization: Adjusting drug doses based on patient recovery.


10. Elevator Control: Reducing wait times in buildings.

Common questions

Powered by AI

Uniform Cost Search (UCS) differs from Breadth-First Search (BFS) as it accounts for varying costs of edges, ensuring the path with the lowest total cost is found, which makes it more efficient for pathfinding in weighted graphs. While BFS assumes uniform edge costs and is optimal for unweighted graphs, UCS's ability to find the least-cost path is particularly useful in navigation systems and resource allocation problems where costs vary .

AI applications enhance decision-making in healthcare by improving diagnostic capabilities, such as using AI systems like AlphaFold to solve complex problems like protein folding, and developing diagnostic tools that outperform human doctors in detecting certain cancers, thus increasing accuracy and speed in patient care .

Autonomous navigation systems, like NASA’s Perseverance rover and Waymo’s self-driving cars, navigate complex environments by processing sensor data to avoid obstacles and select paths. The systems use AI to analyze real-time data for decision-making and to facilitate safe and efficient travel .

AI in facial recognition systems faces ethical challenges primarily related to bias and misidentification errors. These challenges impact society by potentially leading to wrongful identification and subsequent unfair treatment of individuals, raising concerns about privacy and civil rights .

Narrow AI is designed to perform specific tasks with high proficiency but lacks general intelligence, operating within a predefined scope. In contrast, general AI aims to replicate human-like intelligence, capable of performing a wide array of tasks. The distinction is significant as it highlights the current capabilities and limitations of AI, guiding future research and development .

AlphaGo, developed by DeepMind, demonstrated the capabilities of reinforcement learning and strategic planning by defeating Go champion Lee Sedol. This was achieved through learning optimal strategies and making strategic decisions during gameplay, exemplifying advanced AI capabilities in complex tasks .

Learning agents have the advantage of adaptability to dynamic or unknown environments, improving accuracy and decision-making over time as they learn from experience. They are particularly useful in domains such as robotics, autonomous vehicles, and recommendation systems, where adaptability and continuous improvement are crucial .

Explainable AI plays a crucial role in military and medical applications by providing transparency in AI decision-making processes. This transparency is essential for building trust and understanding in critical situations where AI decisions impact safety and outcomes, such as in military operations and medical diagnostics .

The societal impacts of job displacement due to AI advancements include potential economic challenges as workers in automated roles might face unemployment, necessitating retraining and reskilling programs, and could shift societal dynamics by widening the gap between technology-adaptive businesses and traditional industries .

Generative AI tools like ChatGPT and DALL-E have significantly influenced the creative industries by enabling the creation of human-like text and images, which are now used in various applications such as customer service and AI-generated art featured in galleries .

You might also like