0% found this document useful (0 votes)
8 views

V03 ProblemSolvingThroughSearch

The document discusses uninformed and heuristic search strategies for problem solving. It covers topics like uninformed search algorithms, their time and space complexity, and how heuristic search can inform the search to solve larger problems.

Uploaded by

carolbdeq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

V03 ProblemSolvingThroughSearch

The document discusses uninformed and heuristic search strategies for problem solving. It covers topics like uninformed search algorithms, their time and space complexity, and how heuristic search can inform the search to solve larger problems.

Uploaded by

carolbdeq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Artificial Intelligence

V03: Problem solving through search

Searching as a problem-solving strategy


Uninformed search
Heuristic (informed) search

Based on material by
• Stuart Russell, UC Berkeley
• Inês de Castro Dutra, Cooperating Intelligent Systems, U. Porto

Zurich University of Applied Sciences and Arts


CAI Centre for Artificial Intelligence (stdm)
Educational objectives

• Know classical search algorithms and selection criteria based


on time and space complexity

• Understand how intelligent behavior evolves out of efficient


algorithms

• Know how to inform search methods by heuristics

• Be able to model a real-world problem to be solved by searching

“In which we see how an agent can look ahead to find a sequence of actions
that will eventually achieve its goal.”

➔ Reading: AIMA, ch. 3


Zurich University of Applied Sciences and Arts 2
CAI Centre for Artificial Intelligence (stdm)
1. SEARCHING AS A PROBLEM-SOLVING STRATEGY

Search = tree data structure


= efficient algorithms

Zurich University of Applied Sciences and Arts 3


CAI Centre for Artificial Intelligence (stdm)
Example: On holiday in Arad, Romania
Task: Catch flight that leaves tomorrow from Bucharest

Initial state
• Currently in Arad

Formulate goal
• be in Bucharest

Formulate problem
• states: various cities
• actions: drive between cities

Find solution
• Sequence of cities, e.g., Arad→Sibiu→Fagaras→Bucharest
• How? See later

Zurich University of Applied Sciences and Arts 4


CAI Centre for Artificial Intelligence (stdm)
Problem formulation
For deterministic & fully observable environments

Problem is defined by four items Selecting a proper state space


• initial state • Real world is very complex
e.g., In(Arad) ➔ state & action space must be abstracted
• successor function S(x) • Abstract state: set of real states
set of action-state pairs, e.g. • Abstract action: complex combination of real
S(Arad) = {<Arad→Zerind;Zerind>, …} actions
• goal test e.g., Arad→Zerind represents a complex set
explicit or implicit, e.g. of possible routes, detours, rest stops, etc.
x = In(Bucharest) or NoDirt(x) • Abstract solution: set of real paths that are
• path cost (additive) solutions in the real world
e.g., sum of distances, number of actions, etc. • For guaranteed realizability, any real state
c(x,a,y)>=0 is the step cost In(Arad) must get to some real state
In(Zerind)
➔ See also appendix on modeling

➔ Each abstract action should be easier


than the original problem
Zurich University of Applied Sciences and Arts 5
CAI Centre for Artificial Intelligence (stdm)
Examples of problems solvable by searching
“Toy” problem: helps to identify strengths and weaknesses of different methods

8-puzzle Note: Optimal solution of n-Puzzle family is NP-hard (→ see appendix)


• States? integer locations of tiles (ignoring intermediate positions)
• Actions? move blank to left, right, up, down (ignoring unjamming etc.)
• Goal test? current state equals given goal state
• Path cost? 1 per move

Real-world problem

Robotic
assembly
• States? real-valued coordinates of robot joint angles; parts to be assembled
• Actions? continuous motions of robot joints
• Goal test? complete assembly
• Path cost? execution time

Zurich University of Applied Sciences and Arts 6


CAI Centre for Artificial Intelligence (stdm)
Protein design: find a sequence of
Other real-world problems amino acids that folds into a structure
with certain properties
→ has been eased a lot recently by
Route-finding (incl. machine learning (AlphaFold; cp. V11)
touring)

Air-travel planning:
much more
complicated than in-
car navigation!

VLSI layout: place


components and
optimize wiring

All TSP-related
problems of finding
a shortest path

Zurich University of Applied Sciences and Arts 7


CAI Centre for Artificial Intelligence (stdm)
Diversity of search approaches
…solving increasingly complex problem types

Uninformed (blind) search


• All it can do: generate successors of tree-nodes, distinguish goal- from non-goal states (see later)
→ this lecture

• Suitable environments: fully observable, deterministic, discrete (episodic, static, single agent)
Extensions of today’s methods exist to non-deterministic and partially observable as
well as (semi-)dynamic environments (online search) (→ see AIMA, ch. 4.3-4.5)
Heuristic (informed) search
• Knows whether one non-goal state is “more promising” than another
• Suitable environments: as above, but larger

More informed search methods


Online search
• Environments are dynamic (i.e., not fully known from the beginning ➔ percepts become important)
Local search
→ next lecture

• Cares only to find a goal state rather then the optimal path
• Suitable environments: also, continuous state/action spaces (hill climbing, simulated annealing)
Adversarial search
• Search in the face of an opponent (i.e., dynamic multi-agent environments; also, stochastic and
partially observable forms)

Zurich University of Applied Sciences and Arts 8


CAI Centre for Artificial Intelligence (stdm)
2. UNINFORMED SEARCH

Zurich University of Applied Sciences and Arts 9


CAI Centre for Artificial Intelligence (stdm)
Uninformed search

Approach
• Tree search: iteratively expand nodes until a goal node is hit
• Different strategies: order of node expansion

Evaluation criteria for strategies


• completeness: does it always find a solution if one exists?
• optimality: does it always find a least-cost solution?
• time complexity: number of nodes generated/expanded
• space complexity: maximum number of nodes in memory

Time and space complexity are measured in terms of


• 𝑏: maximum branching factor of the search tree
• 𝑑: depth of the least-cost solution
• 𝑚: maximum depth of the state space (may be ∞)
Zurich University of Applied Sciences and Arts 10
CAI Centre for Artificial Intelligence (stdm)
Example

Growth of time and memory requirements


• Algorithm: breadth-first search (→ ADS: exponential time & space complexity 𝑂(𝑏𝑑 ))
Assumptions: 𝑏 = 10, 1 mio nodes/sec, 1 kB/node
Question: what 𝑑 is easily manageable?

➔ Practical advice: Exponential-complexity search problems cannot be solved by


uninformed methods for any but the smallest instances

➔ See appendix for some recap on complexity theory

Zurich University of Applied Sciences and Arts 11


CAI Centre for Artificial Intelligence (stdm)
Uninformed search strategies
→ Details: ADS or AIMA ch. 3.4
Try DLS with 𝑙 =
1, 𝑙 = 2, … until
Expand the shallowest Expand node with Expand DFS only goal is reached
unexpanded node lowest path cost 𝑔(𝑛) deepest node up to level 𝑙

Practical advice
• Depth-first tree search is a major work horse for many AI tasks (due to linear space
complexity)
• Iterative deepening is not wasteful (a tree with nearly the same 𝑏 at each level has most nodes
in the bottom level ➔ generating higher-level states multiple times doesn’t matter)
• Iterative deepening is preferred uninformed search method
(for large search space and 𝑑 is unknown)
• Bi-directional search can help a lot, but 𝑂(𝑏 𝑑/2 ) space complexity is major drawback

Zurich University of Applied Sciences and Arts 12


CAI Centre for Artificial Intelligence (stdm)
3. HEURISTIC (INFORMED) SEARCH

Zurich University of Applied Sciences and Arts 13


CAI Centre for Artificial Intelligence (stdm)
See appendix

Tree-/graph search using additional knowledge


…beyond the definition of the problem

Best-first search
• Select the node to be expanded next based on some evaluation function 𝑓(𝑛𝑜𝑑𝑒)
• Typically, 𝒇 is implemented by a heuristic ℎ(𝑛𝑜𝑑𝑒) (measure of “desirability”)
• ℎ(𝑛𝑜𝑑𝑒) facilitates pruning of the search tree: options are eliminated without examination

What could be a good heuristic for the distance to Bucharest (being in Arad)?

Step cost in [km]

Zurich University of Applied Sciences and Arts 14


CAI Centre for Artificial Intelligence (stdm)
Typical implementations

Greedy search
• Expand node with lowest subsequent cost estimate according to some ℎ, i.e., 𝑓(𝑛) = ℎ(𝑛)
• 𝑛 may only appear to be closest to the goal

A*
• Obvious improvement: consider full path cost, i.e., 𝑓 𝑛 = 𝑔 𝑛 + ℎ(𝑛)
(𝑔(𝑛) cost so far to reach 𝑛, ℎ(𝑛) estimated cost to goal from 𝑛, 𝑓(𝑛) estimated total path cost)
• ℎ(𝑛) needs to be admissible (see later): ≤ 𝑡𝑟𝑢𝑒 𝑐𝑜𝑠𝑡 and ≥ 0 (e.g., ℎ𝑠𝑡𝑟𝑎𝑖𝑔ℎ𝑡 𝑙𝑖𝑛𝑒 𝑑𝑖𝑠𝑡𝑎𝑛𝑐𝑒 )
• A* search is optimal, complete
• A* has time complexity 𝑂(2 𝑒𝑟𝑟𝑜𝑟 𝑜𝑓 ℎ ⋅𝑑
) and keeps all nodes in memory

SMA* - simplified memory-bounded A*


• A* usually runs out of space first → SMA* overcomes this by
• …filling the memory up, then starting to forget the worst expanded nodes
• …ancestors of forgotten subtrees remember the value of the best path within them
• …thus, subtrees are only regenerated if no better solution exists

Zurich University of Applied Sciences and Arts 15


CAI Centre for Artificial Intelligence (stdm)
A* Example

Zurich University of Applied Sciences and Arts 16


CAI Centre for Artificial Intelligence (stdm)
Succeeding with search

Learning to search
• Learn a heuristic function: use inductive supervised learning on features of a state

• Alternative: construct a metalevel state space, consisting of all internal states of search program
Example: For A* searching for a route in Romania, the search tree is its internal state
• Actions in metalevel space: computations that alter the metalevel state
In the example: Expanding a node
• Solution in metalevel space: a path as depicted on the last slide
➔ can be input to machine learning algorithms to avoid unnecessary expansions

Practical advice
• A* is impractical for large scale problems
• Practical, robust choice: SMA*
• Have good heuristic functions! A well-designed heuristic would have 𝑏 ∗ ≈ 1
(𝑏 ∗ is the effective branching factor)

Zurich University of Applied Sciences and Arts 17


CAI Centre for Artificial Intelligence (stdm)
A closer look on heuristic functions
Example: 8-puzzle

Two proposals – which is better?


• ℎ1 𝑛 = number of misplaced tiles
• ℎ2 𝑛 = total Manhattan distance (i.e., no. of horizontal/vertical squares from desired location of
each tile)

ℎ1 𝑆 = 6
ℎ2 𝑆 = 4 + 0 + 3 + 3 + 1 + 0 + 2 + 1 = 14

Zurich University of Applied Sciences and Arts 18


CAI Centre for Artificial Intelligence (stdm)
Dominance
The 8-puzzle example continues

If ℎ2 𝑛 ≥ ℎ1 𝑛 ∀𝑛 ➔ ℎ2 dominates ℎ1 and is better for search

Typical search costs


Algorithm #nodes expanded #nodes expanded
with 𝑑 = 14 with 𝑑 = 24
Iterative deepening 3’473’941 ~54’000’000’000
A* (ℎ1 ) 539 39’135
A* (ℎ2 ) 113 1’641

Simple improvement
• Given any admissible heuristics ℎ𝑎 , ℎ𝑏 :
• ℎ(𝑛) = max ℎ𝑎 𝑛 , ℎ𝑏 𝑛 is also admissible and dominates ℎ𝑎 , ℎ𝑏

Zurich University of Applied Sciences and Arts 19


CAI Centre for Artificial Intelligence (stdm)
Relaxed problems
Improving heuristics intelligently

Relaxation as a key
• Admissible heuristics can be derived from the exact solution cost of a relaxed version
of the problem
• A relaxed problem has fewer constraints on the actions
• Relaxation can be automatized!
E.g., «Absolver» by (Prieditis, 1993) found best heuristic for 8-puzzle, first heuristic for Rubik’s cube

Examples of relaxed 8-puzzle rules


• If each tile can move anywhere (in 1 step), then 𝒉𝟏 𝒏 gives the shortest solution
• If each tile can move to any adjacent square, then 𝒉𝟐 𝒏 gives the shortest solution

Intuition
• Removing constraints adds edges to the state graph
• Additional edges might provide „short cuts“
• The optimal solution cost of a relaxed problem (“short cut”) can be no greater than the
optimal solution cost of the real problem

Zurich University of Applied Sciences and Arts 20


CAI Centre for Artificial Intelligence (stdm)
Where’s the intelligence?
Man vs. machine

Uninformed search
• In the abstraction of the problem
• In the choice of algorithm that is optimal for the problem at hand
• In the systematic exploration of the state space graph

Heuristic search
• Additionally, in the heuristic function

Originally written in German during


the author’s research stay at ETH

→ see also: Polya, «How to solve it - a new aspect of mathematical method», 1945

Zurich University of Applied Sciences and Arts 21


CAI Centre for Artificial Intelligence (stdm)
Exercise: Missionaries & cannibals
https://aimacode.github.io/aima-exercises/search-exercises/

Three missionaries and 3 cannibals are on one side of a


river, along with a boat that can hold one or two people.
Find a way to get everyone to the other side, without
ever leaving a group of missionaries in one place
outnumbered by the cannibals in that place.

• Formulate the problem precisely:


Make only those distinctions necessary to ensure a valid
solution. Draw a diagram of the complete state space.

• Implement and solve the problem optimally:


Use an appropriate search algorithm. Is it a good idea to check
for repeated states?

• Why do you think people have a hard time solving this puzzle,
given that the state space is so simple?

Zurich University of Applied Sciences and Arts 22


CAI Centre for Artificial Intelligence (stdm)
Review

• Search as an approach to AI exists in its current form more or less since


AI‘s inception
• Extensions of search algorithms exist to non-deterministic and partially
observable environments as well as online search

• Problem formulation usually requires abstracting away real-world details


to define a state space that can feasibly be explored
• Iterative deepening search uses only linear space and not much more time
than other uninformed algorithms
• Graph search can be exponentially more efficient than tree search

• Good heuristics can dramatically reduce search cost


• A* search expands lowest 𝒈 + 𝒉
➔ complete and optimal, also optimally efficient (up to tie-breaks, for forward search)

• Admissible heuristics can be derived from exact solution of relaxed


problems

Zurich University of Applied Sciences and Arts 23


CAI Centre for Artificial Intelligence (stdm)
APPENDIX

Fun fact: implement depth-first search in a maze by


keeping your left hand on the wall.

Zurich University of Applied Sciences and Arts 24


CAI Centre for Artificial Intelligence (stdm)
On modeling and abstraction

After AIMA, sec. 3.1.2


• A model [is] an abstract mathematical description […] and not the real thing
• The process of removing detail from a representation is called abstraction
• The abstraction is valid if we can expand any abstract solution into a solution in the more
detailed world
• The abstraction is useful if carrying out each of the actions in the abstraction is easier than
the original problem
• The choice of a good abstraction thus involves removing as much detail as possible
while retaining validity and ensuring that the abstract actions are easy to carry out

➔ Were it not for the ability to construct useful abstractions, intelligent agents would be
completely swamped by the real world

Zurich University of Applied Sciences and Arts 25


CAI Centre for Artificial Intelligence (stdm)
Recap on complexity theory

Problems are classified to be part of (attention: only intuitive “definitions”)


• P – can be solved in polynomial time by a deterministic algorithm
→ deemed to be solvable «efficiently»
• NP – can only be solved efficiently (i.e., in polynomial time) by guessing the solution
(i.e., by a non-deterministic algorithm)
When people talk about efficient computation, this always
means (at most) polynomial time: 𝑒𝑓𝑓𝑖𝑐𝑖𝑒𝑛𝑡~𝑝𝑜𝑙𝑦𝑛𝑜𝑚𝑖𝑎𝑙 𝑡𝑖𝑚𝑒.

More terminology
• NP-hard – a problem 𝑥 is said to be NP-hard if all problems in NP can be reduced to (i.e., converted
into / stated as) 𝑥 (i.e., can be solved by an algorithm for 𝑥) efficiently
→ Example: Traveling salesman problem (i.e., any problem in NP is at most as hard as 𝑥)
• NP-complete – a problem 𝑥 is said to be NP-complete if it is NP-hard and in NP
→ Example: The satisfiability problem (SAT) – is there an assignment of truth values to make a given formula of
propositional logic true? (→ see V06 and AIMA ch. 7.5)
…which is all good (i.e., we don’t have to care for efficiency) if 𝑃 = 𝑁𝑃 (tremendously unlikely!)

Further reading
• AIMA appendix A.1 (< 3 pages!)
• J. Koehler’s lecture slides on complexity and AI: https://user.enterpriselab.ch/~takoehle/teaching/ai/ProblemComplexity.pdf
• Some more intuition: http://stackoverflow.com/questions/1857244/what-are-the-differences-between-np-np-complete-and-np-hard

Zurich University of Applied Sciences and Arts 26


CAI Centre for Artificial Intelligence (stdm)
Suitable agent structure

function Simple-Problem-Solving-Agent(percept) returns an action


static: seq, an action sequence, initially empty
state, some description of the current world state
goal, a goal, initially null
problem, a problem formulation Note: this is offline
state  Update-State(state, percept) problem solving; solution
if seq is empty then executed “eyes closed“.
goal  Formulate-Goal(state)
problem  Formulate-Problem(state, goal)
seq  Search(problem)
action  Recommendation(seq, state)
seq  Remainder(seq, state)
return action

➔ If the task is represented as a graph of atomic states, and the solution is a


sequence of state changes → a model-based agent may solve it by searching
Zurich University of Applied Sciences and Arts 27
CAI Centre for Artificial Intelligence (stdm)
Repeated states

Problem
• Failure to detect repeated states can turn a linear problem into an exponential one!

Solution
• Graph search: remember nodes already expanded, and don’t revisit them
➔ keep a list of explored nodes

Practical advice
• All previous strategies can be implemented as both tree- or graph search
• If additional space complexity is affordable determines whether graph search is possible

Zurich University of Applied Sciences and Arts 28


CAI Centre for Artificial Intelligence (stdm)
Pseudocode for general tree- and graph search

function Tree-Search(problem, frontier) returns a solution, or failure


frontier  Insert(Make-Node(Initial-State(problem)), frontier)
loop do
if frontier is empty then return failure
node  Remove-Front(frontier) #choice of picked node defined by strategy
if Goal-Test(problem) applied to State(node) succeeds return node
frontier  InsertAll(Expand(node, problem), frontier)

function Graph-Search(problem, frontier) returns a solution, or failure


frontier  Insert(Make-Node(Initial-State(problem)), frontier)
explored  empty
loop do
if frontier is empty then return failure
node Remove-Front(frontier) #choice of picked node defined by strategy
explored  Insert(node, explored)
if Goal-Test(problem) applied to State(node) succeeds return node
frontier  InsertAll(Expand(node, problem), frontier) only if not in frontier or explored set

➔ Bold italic font shows the additions that handle repeated states in graph search

Zurich University of Applied Sciences and Arts 29


CAI Centre for Artificial Intelligence (stdm)
Missionaries & cannibals (contd.)

States
• 𝜃 = (𝑀, 𝐶, 𝐵) signifies the number of missionaries, cannibals, and boats on the left bank
• The start state is (3,3,1) and the goal state is (0,0,0)

Actions (successor function)


• 10 possible, but only 5 available each move due to boat
• One cannibal/missionary crossing L→R: subtract (0,1,1) or (1,0,1)
• Two cannibals/missionaries crossing L→R: subtract (0,2,1) or (2,0,1)
• One cannibal/missionary crossing R→L: add (1,0,1) or (0,1,1)
• Two cannibals/missionaries crossing R→L: add (2,0,1) or (0,2,1)
• One cannibal and one missionary crossing: add/subtract (1,1,1)

Source: http://www.cse.msu.edu/~michmer3/440/Lab1/cannibal.html

Zurich University of Applied Sciences and Arts 30


CAI Centre for Artificial Intelligence (stdm)
Missionaries & cannibals states

• Assumes that passengers have to get out of the boat after the trip
• Red states = missionaries get eaten

Zurich University of Applied Sciences and Arts 31


CAI Centre for Artificial Intelligence (stdm)
Breadth-first search (4 iterations)
on missionaries & cannibals 
States are generated by applying
• +/- (1,0,1)
• +/- (0,1,1)
• +/- (2,0,1)
• +/- (0,2,1)
• +/- (1,1,1)

Red states = missionaries get eaten


Yellow states = repeated states

Zurich University of Applied Sciences and Arts 32


CAI Centre for Artificial Intelligence (stdm)
Breadth-first search (final state)
on missionaries & cannibals 
• Breadth first search expanded 48 nodes
• This is an optimal solution (minimum number
of crossings)

• Depth-first search expanded 30 nodes


• ...if repeated states are checked, otherwise
we end up in an endless loop

Zurich University of Applied Sciences and Arts 33


CAI Centre for Artificial Intelligence (stdm)

You might also like