Artificial Intelligence

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 160

Artificial

Intelligence
Spring – 2023
Course Details
• Teaching Methodology: Teacher-Centered
• Text / Reference Book: S. Russell and P. Norvig: Artificial Intelligence: A
Modern Approach. Pearson, 2010, 3rd Edition
• Course Assessment
• Quiz: 10% (3-5)
• Assignments: 5% (3-5)
• Midterm Exam: 30%
• Projects: 15% (1-2)
• Final Exam: 40%
• Mode of Communication: Email: huma.jabeen@dsu.edu.pk
• Submissions: Via LMS Or Hard Copies
Department of Computer Science – DHA Suffa University 2
Origin of Artificial
Intelligence
The purpose of the first computer is to
break the German communication system.
Department of Computer Science – DHA Suffa University 3
Alan Turing
Major Contribution

• Computing Machinery and


Intelligence is a seminal paper
published in 1950 in Mind
written by Alan Turing on the
topic of artificial intelligence.
• Turing's paper considers the
question

"Can machines think?"

Department of Computer Science – DHA Suffa University 4


Can machines think?

We are still working on it

Department of Computer Science – DHA Suffa University 5


After all, what is intelligence?
(from Wikipedia)

• Intelligence is an umbrella term used to describe a property


of the mind that encompasses many related abilities, such as
the capacities
oto reason,
oto plan,
oto solve problems,
oto think abstractly,
oto comprehend ideas,
oto use language, and
oto learn.

Department of Computer Science – DHA Suffa University 6


Intelligence can be defined as the
ability for solving problems
• Problem solving is to find the “best” solution in the problem space.
• Reasoning is to interpret or justify solutions or sub solutions.
• Planning is to find ways for solving the problem.
• Thinking abstractly is to simulate the problem solving process inside
the system (brain).
• Idea/language comprehension is a way (or means) for
data/problem/knowledge representation;
• Learning is the process to find better ways for solving a problem (or a
class of problems).
Department of Computer Science – DHA Suffa University 7
Department of Computer Science – DHA Suffa University 8
AI Augmented Intelligence

Department of Computer Science – DHA Suffa University 9


Based on Strength, Breadth, and
Application, AI can be described in different ways
Weak or Narrow AI Strong AI or Generalized AI Super AI or Conscious AI
is AI that is applied to a specific is AI that can interact and operate is AI with human-level
domain. For example, language a wide variety of independent and consciousness, which would
translators, virtual assistants, unrelated tasks. require it to be self-aware.
self-driving cars, AI-powered web It can learn new tasks to solve new Because we are not yet able to
searches, recommendation problems, and it does this by adequately define what
engines, and intelligent spam teaching itself new strategies. consciousness is, it is unlikely that
filters. Generalized Intelligence is the we will be able to create a
Applied AI can perform specific combination of many AI strategies conscious AI in the near future.
tasks, but not learn new ones, that learn from experience and
making decisions based on can perform at a human level of
programmed algorithms, and intelligence.
training data.

Department of Computer Science – DHA Suffa University 10


AI means different things to different
people.
For a videogame designer For a screenwriter For a data scientist
AI means writing the code that AI means a character that acts like AI is a way of exploring and
affects how bots play, and how a human, with some trope of classifying data to meet specific
the environment reacts to the computer features mixed in. goals.
player.

Department of Computer Science – DHA Suffa University 11


Definition of AI
“Intelligence: The ability to learn and solve problems”
Webster’s Dictionary

“Artificial intelligence (AI) is the intelligence exhibited by machines or software”


Wikipedia

“The science and engineering of making intelligent machines”


McCarthy

“The study and design of intelligent agents, where an intelligent agent is a system
that perceives its environment and takes actions that maximize its chances of
success.”
Russel and Norvig AI book

Department of Computer Science – DHA Suffa University 12


Why AI?
“Just as the Industrial Revolution freed up a lot of humanity from
physical drudgery, I think AI has the potential to free up humanity
from a lot of the mental drudgery.”
Andrew Ng.

Department of Computer Science – DHA Suffa University 13


Artificial intelligence is not about making computers intelligent.
Department of Computer Science – DHA Suffa University 14
A rough classification of AI
(from “Artificial Intelligence: A Modern Approach”)

Department of Computer Science – DHA Suffa University 15


What is AI?
Thinking humanly: cognitive approach

Requires to determine how humans think!


1960’s “cognitive revolution”.
Requires scientific theories of internal activities of the brain
• What level of abstraction? “Knowledge” or “circuits”?
• How to validate?
Today, Cognitive Science and Artificial Intelligence are distinct disciplines.
Department of Computer Science – DHA Suffa University 16
What is AI?
Acting humanly:
• Turing test (Alan Turing 1950): A computer passes the test of
intelligence, if it can fool a human interrogator.

• Major components of AI: knowledge, reasoning, language,


understanding, and learning.
Department of Computer Science – DHA Suffa University 17
What is AI?
Acting humanly:

Department of Computer Science – DHA Suffa University 18


What is AI?
Thinking rationally: Laws of thoughts.
• Codify “right thinking” with logic.

• Several Greek schools developed various forms of logic:notation and


rules of derivation for thoughts.

• Problems:
1. Not all knowledge can be expressed with logical notations.
2. Computational blow up.

Department of Computer Science – DHA Suffa University 19


What is AI?
Acting rationally:
• The right thing: that which is expected to maximize goal achievement,
given the available information.

• A rational agent is one that acts so as to achieve the best outcome, or


when there is uncertainty, the best expected outcome.

• Aristotle (Nicomachean Ethics):


“Every art and every inquiry, and similarly every action and pursuit, is thought
to aim at some good.”

Department of Computer Science – DHA Suffa University 20


Foundation of AI
Economics

Mathematics Neuroscience

Linguistics
AI Psychology

Control
Theory & Philosophy
Cybernetics

Department of Computer Science – DHA Suffa University 21


Foundation of AI
• Philosophy
• Logic, methods of reasoning.
• Mind as physical system that operates as a set of rules.
• Foundations of learning, language, rationality.
• Mathematics
• Logic: Formal representation and proof.
• Computation, algorithms.
• Probability.
• Economics
• Formal theory of rational decisions.
• Combined decision theory and probability theory for decision making under
uncertainty.
• Game theory.
• Markov decision processes.
Department of Computer Science – DHA Suffa University 22
Foundation of AI
• Neuroscience
• Study of brain functioning.
• How brains and computers are (dis)similar.
• Psychology
• How do we think and act?
• Cognitive psychology perceives the brain as an information processing machine.
• Led to the development of the field cognitive science: how could computer models
be used to study language, memory, and thinking from a psychological perspective.
• Computer engineering
• Cares about how to build powerful machines to make AI possible.
• E.g., Self-driving cars are possible today thanks to advances in computer engineering.

Department of Computer Science – DHA Suffa University 23


Foundation of AI
• Control theory and cybernetics
• Design simple optimal agents receiving feedback from the environment.
• Modern control theory design systems that maximize an objective function
over time.
• Linguistics
• How are language and thinking related.
• Modern linguistics + AI = Computational linguistics (Natural language
processing).

Department of Computer Science – DHA Suffa University 24


Applications of AI

Department of Computer Science – DHA Suffa University 25


Applications of AI
Speech recognition

• Virtual assistants: Siri (Apple), Echo (Amazon),


Google Now, Cortana (Microsoft).

• “They” helps get things done: send an email,


make an appointment, find a restaurant,
tell you the weather and more.

• Leverage deep neural networks to handle


speech recognition and natural language
understanding.

Department of Computer Science – DHA Suffa University 26


In May 2018, Google Duplex: An AI System for Accomplishing Real-
World Tasks Over the Phone.

Department of Computer Science – DHA Suffa University 27


Applications of AI
Handwriting recognition (check, zipcode)

Department of Computer Science – DHA Suffa University 28


Applications of AI
Machine translation
• MT has gone through ups and downs.

• Today, Statistical Machine Translation leverages the vast amounts of


available translated corpuses.

• While there is room for improvement, machine translation has made


significant progress.

Department of Computer Science – DHA Suffa University 29


Department of Computer Science – DHA Suffa University 30
Applications of AI
Robotics: Awesome robots today! NAO, ASIMO, and more!

Department of Computer Science – DHA Suffa University 31


Applications of AI
Recommendation systems (collaborative filtering)

Department of Computer Science – DHA Suffa University 32


Applications of AI
Search engines

Department of Computer Science – DHA Suffa University 33


Applications of AI
Email Spamming

Department of Computer Science – DHA Suffa University 34


Applications of AI
Face detection

Department of Computer Science – DHA Suffa University 35


Applications of AI
Face recognition

Department of Computer Science – DHA Suffa University 36


Applications of AI
Detection of breast cancer in mammography images

Department of Computer Science – DHA Suffa University 37


In June 2018 in Beijing China, an Artificial Intelligence (AI) system
scored 2:0 against elite human physicians in two rounds of
competitions in diagnosing brain tumors and predicting hematoma
expansion.

Department of Computer Science – DHA Suffa University 38


Applications of AI
Chess (1997): Kasparov vs. IBM Deep Blue
Powerful
search
algorithms!

Department of Computer Science – DHA Suffa University 39


Applications of AI
Jeopardy! (2011): Humans vs. IBM Watson
Natural
Language
Understanding
and
information
extraction!

Department of Computer Science – DHA Suffa University 40


Applications of AI
Go (2016): Lee Sedol versus Google AlphaGo

Deep Learning, reinforcement learning, and search algorithms!

Department of Computer Science – DHA Suffa University 41


In March 2016, Alpha-Go of DeepMind defeated Lee Sedol, who was
the strongest human GO player at that time.

Department of Computer Science – DHA Suffa University 42


Applications of AI
Autonomous driving
DARPA Grand Challenge
• 2005: 132 miles
• 2007: Urban challenge
• 2009: Google self-driving car

Department of Computer Science – DHA Suffa University 43


In October 2015, Tesla Released the Autopilot software for self
deriving cars.

Department of Computer Science – DHA Suffa University 44


Course roadmap
1. Rational intelligent agents
2. Search agents (uninformed search, informed search)
3. Adversarial search/games
4. Machine Learning (ML)
5. Constraint satisfaction problems (CSPs)
6. Logic (propositional logic, first order logic)
7. Markov Decision Processes (MDPs) and Reinforcement Learning(RL)
8. Application to Natural language Processing (NLP)
9. Application to vision and robotics

Department of Computer Science – DHA Suffa University 45


Rational intelligent agents
• This course is about designing intelligent agents.
• An agent perceives the environment and act upon that environment
to achieve some task.
• An agent is function from percepts to actions.
• We care specifically about rational agents.
• Rationality is relative to how to act to maximize a performance
measure.
• AI aims to design the best agents (programs) that achieve the best
performance given the computational limitations.
Agent = Architecture + Program
Department of Computer Science – DHA Suffa University 46
Search agents
• Agents that work towards a goal.
• Agents consider the impact of actions on future states.
• Agent’s job is to identify the action or series of actions that lead to
the goal.
• Paths come with different costs and depths.
• Two kinds of search:
Uninformed Search (use no domain knowledge): BFS, DFS, UCS, etc.

Informed Search (use heuristic to reach the goal faster): Greedy search, A*,
etc.

Department of Computer Science – DHA Suffa University 47


Search agents

The 8-queen problem: on a chess board, place 8 queens so that no queen is


attacking any other horizontally, vertically or diagonally.
Department of Computer Science – DHA Suffa University 48
Search agents
Start: Las Vegas
Goal: Calgary

Explore + Execute
Department of Computer Science – DHA Suffa University 49
Adversarial Search: games
Solved games!
Checkers:
• Chinook ended 40-year-reign of human world champion Marion Tinsley in
1994.
• Used an endgame database defining perfect play for all positions involving 8
or fewer pieces on the board, a total of 443,748,401,247 positions.

Department of Computer Science – DHA Suffa University 50


Adversarial Search: games
• Adversarial search problems game
• Adversarial There is an opponent we can’t control!
• Game vs. search: optimal solution is not a sequence of actions but a
strategy (policy). If opponent does a, agent does b, else if opponent
does c, agent does d, etc.
• Tedious and fragile if hard-coded (i.e., implemented with rules).
• Concepts/methods: Minimax algorithm, 𝛼 − 𝛽 pruning, stochastic
games.

Department of Computer Science – DHA Suffa University 51


Machine learning

“How do we create computer programs that improve with


experience?”
Tom Mitchell

Department of Computer Science – DHA Suffa University 52


Machine learning
Binary classification (categorization)
Input: “Examples” with labels.
𝑥1 , 𝑦1 , . . ., 𝑥𝑛 , 𝑦𝑛 𝑥𝑖 ∈ 𝑋 ⊂ ℝ𝑛 , 𝑦𝑖 ∈ 𝑌 = { −1, +1}
Output: ℎ ∶ 𝑋 ⟶ 𝑌
Example: Approve credit yes/no, spam/ham

Concepts/methods: Supervised learning, classification, K nearest neighbors, perceptron's,


neural networks, linear regression, etc.
Department of Computer Science – DHA Suffa University 53
Machine learning
Data segmentation or Clustering
Input: “Examples” without labels.
𝑥1 , . . . , 𝑥𝑛 , 𝑥𝑖 ∈ 𝑋 ⊂ ℝ𝑛
Output: 𝑓 ∶ 𝑋 ⟶ { 𝐶1 , . . . , 𝐶𝑘 } (set of clusters).

Concepts/methods: Unsupervised learning, clustering, k-means, association rules, etc.

Department of Computer Science – DHA Suffa University 54


Constraint satisfaction
• A search problem too!

• We don’t care about the path but about the goal itself.

• All paths are of same depth.

• Problem is formulated using variables, domains and constraints.

• Solving the CSP: finding the assignment(s) that satisfy all constraints.

• Concepts/methods: problem formalization, backtracking search, arc


consistency, etc.
Department of Computer Science – DHA Suffa University 55
Constraint satisfaction

Variables: 𝑋𝑙,𝑐 for 1 ≤ 𝑙 ≤ 9 and 1 ≤ 𝑐 ≤ 9 .


Constraints: All 3x3 grid, row, column, must contain digits 1..9 and all of them!
Solution: Find the assignments to the variables that satisfy the constraints.
Department of Computer Science – DHA Suffa University 56
Constraint satisfaction

Variables: 𝑋𝑙,𝑐 for 1 ≤ 𝑙 ≤ 9 and 1 ≤ 𝑐 ≤ 9 .


Constraints: All 3x3 grid, row, column, must contain digits 1..9 and all of them!
Solution: Find the assignments to the variables that satisfy the constraints.
Department of Computer Science – DHA Suffa University 57
Logical Agents
• Logic can be used by an agent to model the world.
• Sentences in PL and FOL have a fixed syntax.
• With symbols and connectives we can form logical sentences:
Example: ℎ𝑜𝑡 𝑠𝑢𝑛𝑛𝑦 ⇒ 𝑏𝑒𝑎𝑐ℎ 𝑝𝑜𝑜𝑙
• Syntax and Semantic represent two important and distinct aspects in
logic.
• Inference: Given a Knowledge Base (KB) (set of sentences in logic),
given a query 𝜶, output whether KB entails 𝜶, noted:
𝐾𝐵 ⊨ 𝜶
• Concepts/methods: Modus Ponens, sound and complete inference,
horn clauses, etc.
Department of Computer Science – DHA Suffa University 58
Reinforcement learning
• Agent evolves in a stochastic and uncertain environment.

• Agent learns from reinforcement or delayed reward.

• Learning approaches for decision making in situations where outcomes are


stochastic.

• Agent continues to plan and learn to affect its environment.

• Reinforcement learning agents are driven by maximizing their rewards on


the long run.
Department of Computer Science – DHA Suffa University 59
Intelligent Agents

Department of Computer Science – DHA Suffa University 60


Agents and environments
• Agent: An agent is anything that can be viewed as:
―perceiving its environment through sensors and
―acting upon that environment through actuators.
Agent Sensor

Environment
Percepts

?
Action
Actuators
• An agent program runs in cycles of: (1)perceive, (2)think, and (3)act
• Agent = Architecture + Program
Department of Computer Science – DHA Suffa University 61
Agents and environments
• Human agent:
―Sensors: eyes, ears, and other organs.
―Actuators: hands, legs, mouth, and other body parts.
• Robotic agent:
―Sensors: Cameras and infrared range finders.
―: Various motors.
• Software Agents:
―Sensors: keystrokes, file contents, and network packets
―Actuators: displaying on the screen, writing files, and sending network packets
• Agents everywhere!
―Thermostat
―Cell phone
―Vacuum cleaner
―Robot
―Alexa Echo
―Self-driving car
―Human
Department of Computer Science – DHA Suffa University 62
Vacuum cleaner

• Percepts: location and contents e.g., [A, Dirty]


• Actions: Left, Right, Suck, NoOp
• Agent function: mapping from percepts to actions.

Department of Computer Science – DHA Suffa University 63


Well-behaved agents
Rational Agent:

“For each possible percept sequence, a rational agent


should select an action that is expected to maximize its
performance measure, given the evidence provided by
the percept sequence and whatever built-in knowledge
the agent has.”

Department of Computer Science – DHA Suffa University 64


Rationality
• Rationality is relative to a performance measure.
• Judge rationality based on:
―The performance measure that defines the criterion of success.
―The agent prior knowledge of the environment.
―The possible actions that the agent can perform.
―The agent’s percept sequence to date.

Department of Computer Science – DHA Suffa University 65


PEAS
• When we define a rational agent, we group these properties under
PEAS, the problem specification for the task environment.
• The rational agent we want to design for this task environment is the
solution.
• PEAS stands for:
―Performance
―Environment
―Actuators
―Sensors

Department of Computer Science – DHA Suffa University 66


PEAS
What is PEAS for a self-driving car?

• Performance
• Environment
• Actuators
• Sensors
Department of Computer Science – DHA Suffa University 67
PEAS
What is PEAS for a self-driving car?

• Performance: Safety, time, legal drive, comfort.


• Environment
• Actuators
• Sensors
Department of Computer Science – DHA Suffa University 68
PEAS
What is PEAS for a self-driving car?

• Performance: Safety, time, legal drive, comfort.


• Environment: Roads, other cars, pedestrians, road signs.
• Actuators
• Sensors
Department of Computer Science – DHA Suffa University 69
PEAS
What is PEAS for a self-driving car?

• Performance: Safety, time, legal drive, comfort.


• Environment: Roads, other cars, pedestrians, road signs.
• Actuators: Steering, accelerator, brake, signal, horn.
• Sensors
Department of Computer Science – DHA Suffa University 70
PEAS
What is PEAS for a self-driving car?

• Performance: Safety, time, legal drive, comfort.


• Environment: Roads, other cars, pedestrians, road signs.
• Actuators: Steering, accelerator, brake, signal, horn.
• Sensors: Camera, sonar, GPS, Speedometer, odometer, accelerometer, engine sensors,
keyboard.
Department of Computer Science – DHA Suffa University 71
PEAS
How about a vacuum cleaner?

• Performance
• Environment
• Actuators
• Sensors

Department of Computer Science – DHA Suffa University 72


PEAS
How about a vacuum cleaner?

• Performance: cleanness, efficiency: distance traveled to clean, battery life,


security.
• Environment
• Actuators
• Sensors
Department of Computer Science – DHA Suffa University 73
PEAS
How about a vacuum cleaner?

• Performance: cleanness, efficiency: distance traveled to clean, battery life,


security.
• Environment: room, table, wood floor, carpet, different obstacles.
• Actuators
• Sensors
Department of Computer Science – DHA Suffa University 74
PEAS
How about a vacuum cleaner?

• Performance: cleanness, efficiency: distance traveled to clean, battery life,


security.
• Environment: room, table, wood floor, carpet, different obstacles.
• Actuators: wheels, different brushes, vacuum extractor.
• Sensors
Department of Computer Science – DHA Suffa University 75
PEAS
How about a vacuum cleaner?

• Performance: cleanness, efficiency: distance traveled to clean, battery life,


security.
• Environment: room, table, wood floor, carpet, different obstacles.
• Actuators: wheels, different brushes, vacuum extractor.
• Sensors: camera, dirt detection sensor, cliff sensor, bump sensor infrared wall
sensors.
Department of Computer Science – DHA Suffa University 76
Department of Computer Science – DHA Suffa University 77
Department of Computer Science – DHA Suffa University 78
Environment types
• Fully observable (vs. partially observable): An agent’s sensors give it
access to the complete state of the environment at each point in
time.
• Deterministic (vs. stochastic): The next state of the environment is
completely determined by the current state and the action executed
by the agent. (If the environment is deterministic except strategic for
the actions of other agents, then the environment is)
• Episodic (vs. sequential): The agent’s experience is divided into
atomic ”episodes” (each episode consists of the agent perceiving and
then performing a single action), and the choice of action in each
episode depends only on the episode itself.

Department of Computer Science – DHA Suffa University 79


Environment types
• Static (vs. dynamic): The environment is unchanged while an agent is
deliberating. (The environment is semi-dynamic if the environment itself
does not change with the passage of time but the agent’s performance
score does.)
• Discrete (vs. continuous): A limited number of distinct, clearly defined
percepts and actions. E.g., checkers is an example of a discrete
environment, while self-driving car evolves in a continuous one.
• Single agent (vs. multi-agent): An agent operating by itself in an
environment.
• Known (vs. Unknown): The designer of the agent may or may not have
knowledge about the environment makeup. If the environment is unknown
the agent will need to know how it works in order to decide.
Department of Computer Science – DHA Suffa University 80
Environment types

Department of Computer Science – DHA Suffa University 81


Agent types
• Four basic types in order of increasing generality:
―Simple reflex agents
―Model-based reflex agents
―Goal-based agents
―Utility-based agents
• All of which can be generalized into learning agents that can improve
their performance and generate better actions.

Department of Computer Science – DHA Suffa University 82


The Structure of Agents

Department of Computer Science – DHA Suffa University 83


Simple reflex agents
• Simple reflex agents select an action based on the current state only
ignoring the percept history.
• Simple but limited.
• Can only work if the environment is fully observable, that is the
correct action is based on the current percept only.
Agent Sensor

Environment
Percepts
What the world is like now?

Condition-
What action I should do now?
action-rule

Action
Actuators
Department of Computer Science – DHA Suffa University 84
Simple reflex agents

Department of Computer Science – DHA Suffa University 85


Vacuum (reflex) agent

• Let’s write the algorithm for the Vacuum cleaner...


• Percepts: location and content (location sensor, dirt sensor).
• Actions: Left, Right, Suck, NoOp

What if the vacuum agent is deprived from its location sensor?

Department of Computer Science – DHA Suffa University 86


Vacuum (reflex) agent

Department of Computer Science – DHA Suffa University 87


Model-based reflex agents
• Handle partial observability by keeping track of the part of the world
it can’t see no
• Internal state depending on the percept history (best guess).
• Model of the world based on (1) how the world evolves
independently from the agent, and (2) how the agent actions affects
the world. Agent Sensor Percepts

Environment
State
How the world What the world is like now?
evolves
What my actions do

Condition-
What action I should do now?
action-rule
Action
Actuators
Department of Computer Science – DHA Suffa University 88
Model-based reflex agents

Department of Computer Science – DHA Suffa University 89


Goal-based agents
• Knowing the current state of the environment is not enough. The
agent needs some goal information.
• Agent program combines the goal information with the environment
model to choose the actions that achieve that goal.
• Consider the future with “What will happen if I do A?”
• Flexible as knowledge supporting the decisions is explicitly
represented and can be modified.

Department of Computer Science – DHA Suffa University 90


Goal-based agents
Agent Sensor Percepts
State What the world is like now?
How the world
evolves
What my actions do

Environment
What it will be like if I do the
action A

Goals What action I should do now?

Action
Actuators
Department of Computer Science – DHA Suffa University 91
Utility-based agents
• Sometimes achieving the desired goal is not enough. We may look for
quicker, safer, cheaper trip to reach a destination.
• Agent happiness should be taken into consideration. We call it utility.
• A utility function is the agent’s performance measure
• Because of the uncertainty in the world, a utility agent choses the
action that maximizes the expected utility.

Department of Computer Science – DHA Suffa University 92


Utility-based agents
Agent Sensor Percepts
State What the world is like now?
How the world
evolves
What my actions do

Environment
What it will be like if I do the
action A

How happy I will be in such a


Utility
state

What action I should do now?

Action
Actuators
Department of Computer Science – DHA Suffa University 93
Learning agents
• Programming agents by hand can be very tedious. “Some more
expeditious method seem desirable” Alan Turing, 1950.
• Four conceptual components:
―Learning element: responsible for making improvements
―Performance element: responsible for selecting external actions. It is what we
considered as agent so far.
―Critic: How well is the agent is doing w.r.t. a fixed performance standard.
―Problem generator: allows the agent to explore.

Department of Computer Science – DHA Suffa University 94


Learning agents
Performance Standard

Agent Critic Sensor Percepts

Feedback

Environment
changes
Learning Performance
Elements Elements
Knowledge
Learning
Goals

Problem Action
Generator Actuators

Department of Computer Science – DHA Suffa University 95


Agent’s organization
a) Atomic Representation: Each state of the world is a black box that
has no internal structure. E.g., finding a driving route, each state is a
city.
i. AI algorithms: search, games, Markov decision processes, hidden Markov
models, etc.

B A

Department of Computer Science – DHA Suffa University 96


Agent’s organization
b) Factored Representation: Each state has some attribute value
properties. E.g., GPS location, amount of gas in the tank.
i. AI algorithms: constraint satisfaction, and Bayesian networks.

Department of Computer Science – DHA Suffa University 97


Agent’s organization
c) Structured Representation: Relationships between the objects of a
state can be explicitly expressed.
i. AI algorithms: first order logic, knowledge-based learning, natural language
understanding

Department of Computer Science – DHA Suffa University 98


Intelligent agents
• The concept of intelligent agent is central in AI.
• AI aims to design intelligent agents that are useful, reactive, autonomous
and even social and pro-active.
• An agent perceives its environment through percept and acts through
actuators.
• A performance measure evaluates the behavior of the agent.
• An agent that acts to maximize its expected performance measure is called
a rational agent.
• PEAS: A task environment specification that includes Performance
measure, Environment, Actuators and Sensors.
Agent = Architecture + Program
Department of Computer Science – DHA Suffa University 99
Intelligent agents
• Four types of agents: Reflex agents, model-based agents, goal based
agents, and utility-based agents.
• Agents can improve their performance through learning.
• This is a high-level present of agent programs.
• States representations: atomic, factored, structured. Increasing
expressiveness power
Search Problems
Markov decision model Constraint Satisfaction Problems
Adversarial Games Bayesian networks
Reflex States Variables Logics

“Low Level intelligence” “High Level intelligence”


Department of Computer Science – DHA Suffa University 100
Example: Romania
Imagine an agent You are in Arad and want to go to Bucharest
in the city of Arad,
Romania, enjoying
a touring holiday

How to design an intelligent agent to find the way between 2 cities?


Department of Computer Science – DHA Suffa University 101
Example: The 8-puzzle

How to design an intelligent agent to solve the 8-puzzel?

Department of Computer Science – DHA Suffa University 102


Motivation
• Solve a problem by searching for a solution. Search strategies are
important methods
• Problem formulation. The use of search requires an abstract
formulation of the problem and the available steps to construct
solutions. for many approaches to problem-solving.
• Optimizing Search. Search algorithms are the basis for many
optimization and planning methods.

Department of Computer Science – DHA Suffa University 103


Problem Formulation
• To solve a problem by search, we need to first formulate the problem.
HOW?
• Our textbook suggest the following schema to help us formulate
problems
1. State
2. Initial state
3. Actions or Successor Function
4. Goal Test
5. Path Cost
6. Solution

Department of Computer Science – DHA Suffa University 104


Problem Formulation (The Romania Example)
• State: We regard a problem as state space here a state is a City
• Initial State: the state to start from In(Arad)
• Successor Function: description of the possible actions, give state x, S(X)
returns a set of <action, successor> ordered pairs.
S(x)= { <Go(Sibiu), In(Sibiu)>, <Go(Timisoara), In(Timisoara)>,
<Go(Zerind),In(Zerind)> }
• Goal Test: determine a given state is a goal state.
In(Sibiu) ->No. In(Zerind) ->No.…. In(Bucharest)->Yes!
• Path Cost: a function that assigns a numeric cost to each path.
– e.g., sum of distances, number of actions executed, etc.
– c(x,a,y) is the step cost, assumed to be ≥ 0
• Solution: a sequence of actions leading from the initial state to a goal
state
{Arad -> Sibiu -> Rimnicu Vilcea -> Pitesti -> Bucharest}

Department of Computer Science – DHA Suffa University 105


Problem Formulation (The 8- Puzzle
Example)
• State: The location of the eight tiles, and the blank
• Initial State: {(7,0), (2,1), (4,2), (5,3), (_,4), (6,5), (8,6), (3,7), (1,8)}
• Successor Function: one of the four actions (blank moves Left, Right,
Up, Down).
• Goal Test: determine a given state is a goal state.
• Path Cost: each step costs 1
• Solution: {(_,0),(1,1),(2,2) ,(3,3) ,(4,4) ,(5,5) ,(6,6) ,(7,7) ,(8,8)}

Department of Computer Science – DHA Suffa University 106


State Space Search
S : { S, A, Action (s), Result (s,a), Cost (s,a) }
S = Start, Goal 2 3 4 1 2 3

• Precise Left, Right, Up, Down 5 1 8 4


8 7 6 7 6 5
• Analyze Start Goal

Department of Computer Science – DHA Suffa University 107


Search Algorithms in Artificial
Intelligence
• Problem-solving agents:
• In Artificial Intelligence, Search techniques are universal problem-solving
methods.
• Rational agents or Problem-solving agents in AI mostly used these search
strategies or algorithms to solve a specific problem and provide the best
result.
• Problem-solving agents are the goal-based agents and use atomic
representation.

Department of Computer Science – DHA Suffa University 108


Search Algorithm Terminologies
• Search: Searching is a step by step procedure to solve a search-
problem in a given search space. A search problem can have three
main factors:
• Search Space: Search space represents a set of possible solutions, which a
system may have.
• Start State: It is a state from where agent begins the search.
• Goal test: It is a function which observe the current state and returns
whether the goal state is achieved or not.
• Search tree: A tree representation of search problem is called Search
tree. The root of the search tree is the root node which is
corresponding to the initial state.

Department of Computer Science – DHA Suffa University 109


Search Algorithm Terminologies
• Search tree: A tree representation of search problem is called Search tree.
The root of the search tree is the root node which is corresponding to the
initial state.
• Actions: It gives the description of all the available actions to the agent.
• Transition model: A description of what each action do, can be
represented as a transition model.
• Path Cost: It is a function which assigns a numeric cost to each path.
• Solution: It is an action sequence which leads from the start node to the
goal node.
• Optimal Solution: If a solution has the lowest cost among all solutions.
Department of Computer Science – DHA Suffa University 110
Properties of Search Algorithms
Following are the four essential properties of search algorithms to compare
the efficiency of these algorithms:
• Completeness: A search algorithm is said to be complete if it guarantees to
return a solution if at least any solution exists for any random input.
• Optimality: If a solution found for an algorithm is guaranteed to be the
best solution (lowest path cost) among all other solutions, then such a
solution for is said to be an optimal solution.
• Time Complexity: Time complexity is a measure of time for an algorithm to
complete its task.
• Space Complexity: It is the maximum storage space required at any point
during the search, as the complexity of the problem.

Department of Computer Science – DHA Suffa University 111


Types of search algorithms
Based on the search problems we can classify the search algorithms
into uninformed (Blind search) search and informed search (Heuristic
search) algorithms.
• Uninformed/Blind Search:
• The uninformed search does not contain any domain knowledge such as
closeness, the location of the goal.
• It operates in a brute-force way as it only includes information about how to
traverse the tree and how to identify leaf and goal nodes.
• Uninformed search applies a way in which search tree is searched without any
information about the search space like initial state operators and test for the
goal, so it is also called blind search.
• It examines each node of the tree until it achieves the goal node.

Department of Computer Science – DHA Suffa University 112


Types of search algorithms
• Uninformed/Blind Search: It can be divided into five main types:
• Breadth-first search

• Uniform cost search

• Depth-first search

• Iterative deepening depth-first search

• Bidirectional Search

Department of Computer Science – DHA Suffa University 113


Types of search algorithms
• Informed Search:
• Informed search algorithms use domain knowledge.
• Problem information is available which can guide the search.
• Informed search strategies can find a solution more efficiently than an uninformed
search strategy.
• Informed search is also called a Heuristic search.
• A heuristic is a way which might not always be guaranteed for best solutions but
guaranteed to find a good solution in reasonable time.
• Informed search can solve much complex problem which could not be solved in
another way.
• An example of informed search algorithms is a traveling salesman problem.
• Greedy Search
• A* Search

Department of Computer Science – DHA Suffa University 114


Breadth-first Search (BFS)
• Breadth-first search is the most common search strategy for
traversing a tree or graph.
• BFS algorithm starts searching from the root node of the tree and
expands all successor node at the current level before moving to
nodes of next level.
• The breadth-first search algorithm is an example of a general-graph
search algorithm.
• Breadth-first search implemented using FIFO queue data structure.

Department of Computer Science – DHA Suffa University 115


Breadth-first Search (BFS)
• Advantages:
• BFS will provide a solution if any solution exists.
• If there are more than one solutions for a given problem, then BFS will
provide the minimal solution which requires the least number of steps.
• Disadvantages:
• It requires lots of memory since each level of the tree must be saved into
memory to expand the next level.
• BFS needs lots of time if the solution is far away from the root node.

Department of Computer Science – DHA Suffa University 116


Example

Department of Computer Science – DHA Suffa University 117


Breadth-first Search (BFS)
• Time Complexity: Time Complexity of BFS algorithm can be obtained
by the number of nodes traversed in BFS until the shallowest Node.
Where the d= depth of shallowest solution and b is a node at every
state.
T (b) = 1+b2+b3+.......+ bd= O (bd)
• Space Complexity: Space complexity of BFS algorithm is given by the
Memory size of frontier which is O(bd).
• Completeness: Yes (if b is finite), a solution will be found if exists.
• Optimality: Yes (if cost = 1 per step)

Department of Computer Science – DHA Suffa University 118


• Suppose the branching factor b=10, and the goal is at depth d=12:
– Then we need O𝟏𝟎𝟏𝟐 time to finish.
– If O is 0.001 second, then we need 1 billion seconds (31 year).
– And if each O costs 10 bytes to store, then we also need 1 terabytes.

Not suitable for searching large graphs

Department of Computer Science – DHA Suffa University 119


Depth-first Search (DFS)
• Depth-first search is a recursive algorithm for traversing a tree or
graph data structure.
• It starts from the root node and follows each path to its greatest
depth node before moving to the next path.
• DFS uses a stack data structure for its implementation.
• The process of the DFS algorithm is similar to the BFS algorithm.

Note: Backtracking is an algorithm technique for finding all possible solutions using recursion.

Department of Computer Science – DHA Suffa University 120


Depth-first Search (DFS)
• Advantage:
• DFS requires very less memory as it only needs to store a stack of the nodes
on the path from root node to the current node.
• It takes less time to reach to the goal node than BFS algorithm (if it traverses
in the right path).
• Disadvantage:
• There is the possibility that many states keep re-occurring, and there is no
guarantee of finding the solution.
• DFS algorithm goes for deep down searching and sometime it may go to the
infinite loop.

Department of Computer Science – DHA Suffa University 121


Example

Department of Computer Science – DHA Suffa University 122


Depth-first Search (DFS)
• Completeness: No: fails in infinite-depth spaces, spaces with loops
• Modify to avoid repeated states along path
• complete in finite spaces
• Time Complexity: O(𝑏 𝑚 ): terrible if m is much larger than d
• but if solutions are dense, may be much faster than breadth-first
• Space Complexity: O(bm), i.e., linear space!
• Optimal: No, DFS search algorithm is non-optimal, as it may generate
a large number of steps or high cost to reach to the goal node.

Department of Computer Science – DHA Suffa University 123


Uniform-cost Search Algorithm
• It is used for traversing a weighted tree or graph.
• This algorithm comes into play when a different cost is available for each
edge.
• The primary goal of the uniform-cost search is to find a path to the goal
node which has the lowest cumulative cost.
• It expands nodes according to their path costs from the root node.
• It can be used to solve any graph/tree where the optimal cost is in demand.
• It is implemented by the priority queue.
• It gives maximum priority to the lowest cumulative cost.
• UCS is equivalent to BFS algorithm if the path cost of all edges is the same.
Department of Computer Science – DHA Suffa University 124
Uniform-cost Search Algorithm
• Advantages:
• Uniform cost search is optimal because at every state the path with the least
cost is chosen.
• Disadvantages:
• It does not care about the number of steps involve in searching and only
concerned about path cost.
• Due to which this algorithm may be stuck in an infinite loop.

Department of Computer Science – DHA Suffa University 125


Example

Department of Computer Science – DHA Suffa University 126


Uniform-cost Search Algorithm
• Completeness: Yes (if b is finite, and step cost is positive)

• Time Complexity: much larger than 𝑏 𝑑 , and just 𝑏 𝑑 , if all steps have the
same cost.

• Space Complexity: as above

• Optimal: Yes

Department of Computer Science – DHA Suffa University 127


Iterative deepening depth-first Search
• It is a combination of DFS and BFS algorithms. It finds out the best
depth limit and does it by gradually increasing the limit until a goal is
found.
• It performs depth-first search up to a certain "depth limit", and it
keeps increasing the depth limit after each iteration until the goal
node is found.
• It combines the benefits of Breadth-first search's fast search and
depth-first search's memory efficiency.
• It is useful uninformed search when search space is large, and depth
of goal node is unknown.
Department of Computer Science – DHA Suffa University 128
Iterative deepening depth-first Search
• Advantages:
• It combines the benefits of BFS and DFS search algorithm in terms of fast
search and memory efficiency.
• Disadvantages:
• The main drawback of IDDFS is that it repeats all the work of the previous
phase.

Department of Computer Science – DHA Suffa University 129


Example
1'st Iteration-----> A

2'nd Iteration----> A, B, C

3'rd Iteration------>A, B, D, E, C, F, G

4'th Iteration------>A, B, D, H, I, E, C, F, K, G

In the fourth iteration, the algorithm will find


the goal node.

Department of Computer Science – DHA Suffa University 130


Iterative deepening depth-first Search
• Completeness: Yes (if the b is finite)

• Time Complexity: O(bd).

• Space Complexity: O(bd).

• Optimal: Yes, if step cost = 1.

Department of Computer Science – DHA Suffa University 131


Iterative deepening Search
• When searching a binary tree to depth 7:
• DFS requires searching 255 nodes
• Iterative deepening requires searching 502 nodes
• Iterative deepening takes only about twice as long
• When searching a tree with branching factor of 4 (each node may
have four children):
• DFS requires searching 21845 nodes
• Iterative deepening requires searching 29124 nodes
• Iterative deepening takes about 4/3 = 1.33 times as long
The higher the branching factor, the lower the relative cost of iterative
deepening depth first search
Department of Computer Science – DHA Suffa University 132
Bidirectional Search Algorithm
• It runs two simultaneous searches, one form initial state called as
forward-search and other from goal node called as backward-search,
to find the goal node.
• It replaces one single search graph with two small subgraphs in which
one starts the search from an initial vertex and other starts from goal
vertex.
• The search stops when these two graphs intersect each other.
• It can use search techniques such as BFS, DFS, etc.

Department of Computer Science – DHA Suffa University 133


Bidirectional Search Algorithm
• Advantages:
• Bidirectional search is fast.
• Bidirectional search requires less memory
• Disadvantages:
• Implementation of the bidirectional search tree is difficult.
• In bidirectional search, one should know the goal state in advance.

Department of Computer Science – DHA Suffa University 134


Example

Department of Computer Science – DHA Suffa University 135


Bidirectional Search Algorithm
• Completeness: Yes (b finite, breadth-first for both directions)

• Time Complexity: Time complexity of bidirectional search using BFS


is O(bd/2).

• Space Complexity: Space complexity of bidirectional search is O(bd/2).

• Optimal: yes (all step costs identical, breadth-first for both


directions).
Department of Computer Science – DHA Suffa University 136
Summary (When to use what)
• Breadth-First Search:
• Some solutions are known to be shallow
• Uniform-Cost Search:
• Actions have varying costs
• Least cost solution is the required
This is the only uninformed search that worries about costs.
• Depth-First Search:
• Many solutions exist
• Know (or have a good estimate of) the depth of solution
• Iterative-Deepening Search:
• Space is limited and the shortest solution path is required

Department of Computer Science – DHA Suffa University 137


Informed Search Algorithms
• It contains an array of knowledge such as how far we are from the
goal, path cost, how to reach to goal node, etc.

• This knowledge help agents to explore less to the search space and
find more efficiently the goal node.

• The is more useful for large search space.

• It uses the idea of heuristic, so it is also called Heuristic search.


Department of Computer Science – DHA Suffa University 138
How to determine the minimum number of coins to give while making change?

-> The coin of the highest value first ?

Department of Computer Science – DHA Suffa University 139


Given a list of cities and their pair wise distances,
the task is to find a shortest possible tour that
visits each city exactly once.

Any idea how to improve this type of search?

• What type of information we may use to improve our


search?

• Do you think this idea is useful: (At each stage visit


the unvisited city nearest to the current city)?

Department of Computer Science – DHA Suffa University 140


Heuristics function
• Heuristic is a function which is used in Informed Search, and it finds the
most promising path.
• It takes the current state of the agent as its input and produces the
estimation of how close agent is from the goal.
• The heuristic method, however, might not always give the best solution,
but it guaranteed to find a good solution in reasonable time.
• It is represented by h(n), and it calculates the cost of an optimal path
between the pair of states.
• The value of the heuristic function is always positive.
• Admissibility of the heuristic function is given as:
• h(n) <= h*(n) Here h(n) is heuristic cost, and h*(n) is the estimated cost. Hence heuristic
cost should be less than or equal to the estimated cost.
Department of Computer Science – DHA Suffa University 141
Pure Heuristic Search
• It expands nodes based on their heuristic value h(n).

• It maintains two lists, OPEN and CLOSED list.


• CLOSED list, it places those nodes which have already expanded
• OPEN list, it places nodes which have yet not been expanded.

• On each iteration, each node n with the lowest heuristic value is


expanded and generates all its successors and n is placed to the
closed list.

• The algorithm continues unit a goal state is found.


Department of Computer Science – DHA Suffa University 142
How to Calculate Heuristics Value?
Start State
• Euclidean Distance 1 3 2
6 5 4
• Manhattan Distance
8 7
• Etc
Goal State
1 2 3
4 5 6
7 8

Department of Computer Science – DHA Suffa University 143


8-Puzzle Problem with Heuristic
Start State Goal State
1 2 3 1 2 3
4 6 4 5 6
7 5 8 7 8

Department of Computer Science – DHA Suffa University 144


Types Informed Search Algorithms

Department of Computer Science – DHA Suffa University 145


Best-first Search Algorithm (Greedy
Search)
• Always selects the path which appears best at that moment.
• It is the combination of DFS and BFS algorithms.
• It uses the heuristic function and search.
• Best-first search allows us to take the advantages of both algorithms.
• With the help of best-first search, at each step, we can choose the
most promising node.
• In the best first search algorithm, we expand the node which is
closest to the goal node and the closest cost is estimated by heuristic
function, i.e.
• f(n)= g(n) Were, h(n)= estimated cost from node n to the goal.
• The greedy best first algorithm is implemented by the priority queue.
Department of Computer Science – DHA Suffa University 146
Best first search algorithm
• Step 1: Place the starting node into the OPEN list.
• Step 2: If the OPEN list is empty, Stop and return failure.
• Step 3: Remove the node n, from the OPEN list which has the lowest value
of h(n), and places it in the CLOSED list.
• Step 4: Expand the node n, and generate the successors of node n.
• Step 5: Check each successor of node n, and find whether any node is a
goal node or not. If any successor node is goal node, then return success
and terminate the search, else proceed to Step 6.
• Step 6: For each successor node, algorithm checks for evaluation function
f(n), and then check if the node has been in either OPEN or CLOSED list. If
the node has not been in both list, then add it to the OPEN list.
• Step 7: Return to Step 2.
Department of Computer Science – DHA Suffa University 147
Best first search algorithm
• Advantages:
• Best first search can switch between BFS and DFS by gaining the advantages
of both the algorithms.
• This algorithm is more efficient than BFS and DFS algorithms.
• Disadvantages:
• It can behave as an unguided depth-first search in the worst case scenario.
• It can get stuck in a loop as DFS.
• This algorithm is not optimal.

Department of Computer Science – DHA Suffa University 148


Example
Expand the nodes of S and put in the CLOSED list

Initialization: Open [A, B], Closed [S]

Iteration 1: Open [A], Closed [S, B]

Iteration 2: Open [E, F, A], Closed [S, B]


: Open [E, A], Closed [S, B, F]

Iteration 3: Open [I, G, E, A], Closed [S, B, F]


: Open [I, E, A], Closed [S, B, F, G]

Hence the final solution path will be: S----> B----->F----> G

Department of Computer Science – DHA Suffa University 149


Best first search algorithm
• Time Complexity: The worst case time complexity of Greedy best first
search is O(bm).
• Space Complexity: The worst case space complexity of Greedy best
first search is O(bm). Where, m is the maximum depth of the search
space.
• Complete: Greedy best-first search is also incomplete, even if the
given state space is finite.
• Optimal: Greedy best first search algorithm is not optimal.

Department of Computer Science – DHA Suffa University 150


A* Search Algorithm
• A* search is the most commonly known form of best-first search.
• It uses heuristic function h(n), and cost to reach the node n from the
start state g(n).
• It has combined features of UCS and greedy best-first search, by
which it solve the problem efficiently.
• It finds the shortest path through the search space using the heuristic
function.
• It expands less search tree and provides optimal result faster.
• It is a similar to UCS except that it uses g(n)+h(n) instead of g(n).

Department of Computer Science – DHA Suffa University 151


A* Search Algorithm
• In A* search algorithm, we use search heuristic as well as the cost to
reach the node.
• Hence we can combine both costs as following, and this sum is called
as a fitness number.

Department of Computer Science – DHA Suffa University 152


Algorithm of A* search
• Step1: Place the starting node in the OPEN list.
• Step 2: Check if the OPEN list is empty or not, if the list is empty then
return failure and stops.
• Step 3: Select the node from the OPEN list which has the smallest value of
evaluation function (g+h), if node n is goal node then return success and
stop, otherwise
• Step 4: Expand node n and generate all of its successors, and put n into the
closed list. For each successor n', check whether n' is already in the OPEN
or CLOSED list, if not then compute evaluation function for n' and place
into Open list.
• Step 5: Else if node n' is already in OPEN and CLOSED, then it should be
attached to the back pointer which reflects the lowest g(n') value.
• Step 6: Return to Step 2.
Department of Computer Science – DHA Suffa University 153
A* Search Algorithm
• Advantages:
• Best algorithm than other search algorithms.
• Optimal and complete.
• This algorithm can solve very complex problems.
• Disadvantages:
• It does not always produce the shortest path as it mostly based on heuristics
and approximation.
• It has some complexity issues.
• The main drawback of A* is memory requirement as it keeps all generated
nodes in the memory, so it is not practical for various large-scale problems.

Department of Computer Science – DHA Suffa University 154


Example

Initialization: {(S, 5)}

Iteration1: {(S--> A, 4), (S-->G, 10)}

Iteration2: {(S--> A-->C, 4), (S--> A-->B, 7), (S-->G, 10)}

Iteration3: {(S--> A-->C--->G, 6), (S--> A-->C--->D, 11), (S--> A-->B, 7), (S-->G, 10)}

Iteration 4 will give the final result, as S--->A--->C--->G it provides the optimal path with cost 6.

Department of Computer Science – DHA Suffa University 155


Solution
Points to remember:

•A* algorithm returns the path which occurred first, and it does not
search for all remaining paths.

•The efficiency of A* algorithm depends on the quality of heuristic.

•A* algorithm expands all nodes which satisfy the condition f(n)

Department of Computer Science – DHA Suffa University 156


A* Search Algorithm
• Complete: A* algorithm is complete as long as:
• Branching factor is finite.
• Cost at every action is fixed.
• Optimal: A* search algorithm is optimal if it follows below two conditions:
• Admissible: the first condition requires for optimality is that h(n) should be an
admissible heuristic for A* tree search. An admissible heuristic is optimistic in nature.
• Consistency: Second required condition is consistency for only A* graph-search.
• If the heuristic function is admissible, then A* tree search will always find
the least cost path.
• Time Complexity: The time complexity depends on heuristic function, and
the number of nodes expanded is exponential to the depth of solution d.
• So the time complexity is O(𝒃𝒅 ), where b is the branching factor.
• Space Complexity: The space complexity of A* search algorithm is O(𝒃𝒅 )
Department of Computer Science – DHA Suffa University 157
How to make A* Admissible
ℎ(𝑛) ≤ ℎ∗ 𝑛 . - underestimation
ℎ 𝑛 ≥ ℎ∗ 𝑛 - overestimation

Department of Computer Science – DHA Suffa University 158


Adversarial Search

Department of Computer Science – DHA Suffa University 159


Behavior from Computation

Department of Computer Science – DHA Suffa University 160

You might also like