3 Probsolver
3 Probsolver
Problem solving
Problem
• It is a collection of information that the
agent will use to decide what to do
• Problems have the general form: given
such-and-such information, find x
• A huge variety of problems are
addressed in AI, both:
– toy problems (e.g. 8-puzzle, vacuum cleaner
world, towers of Hanoi, …)
–real-life problems (like Route finding, Traveling
sales person, etc.)
Example
1: Route-finding problem:
Consider the agent in the city of Arad, toward the end of
touring holiday. Flight leaves tomorrow from city Bucharist
airport. The agent has a ticket to fly out the following day.
The ticket is non-refundable, the agent’s visa is about to
expire after tomorrow and there are no seats available for
six weeks.
2. Vacuum cleaner world problem
An agent vacuum cleaner operates in a simple world with
only two locations to be cleaned.
– Each location may or may not contain dirt
– The agent may be in one location or the other
– The agent can sense
• Location: whether it is in room A or B
• Status: whether the room is Clean or Dirty
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
state UPDATE-STATE(state,percept)
if seq is empty then do
goal FORMULATE-GOAL(state)
problem FORMULATE-PROBLEM(state,goal)
seq SEARCH(problem)
action FIRST(seq)
seq REST(seq)
return action
Example: Route Finding
Scenario: On holiday in Romania: currently in Arad. Flight
leaves tomorrow from Bucharest. Since then no seats
available for the next six weeks. The ticket is non-refundable,
visa is about to expire, …
• Formalize problem:
–Define the possible states involved to solve the problem
• Formulate goal:
–be in Bucharest
• Formulate problem:
–initial state: in Arad
–states: various cities
–Operators/actions: driving between cities
• Find solution:
–Optimal sequence of cities, e.g., Arad, Sibiu, Fagaras,
Bucharest
State Space for Route Finding
Example: Coloring Problem
There are 3 rectangles. Both are initially white. The problem
is to change white color to black color. Color change is one
rectangle at a time. Define state space for coloring
problem?
• Formalize problem:
– Define the possible states involved to solve the problem
• Formulate goal:
– All rectangles colored Black
• Formulate problem:
– initial state: all rectangles white
– states: all rectangles white or all black, one or more black/white
– Operators/actions: fill with black color
• Find solution:
– State Optimal sequence of coloring
Exercise
Missionary-and-cannibal problem:
Three missionaries and three cannibals are on
one side of a river that they wish to cross.
There is a boat that can hold one or two
people. Find an action sequence that brings
everyone safely to the opposite bank (i.e.
Cross the river). But you must never leave a
group of missionaries outnumbered by
cannibals on the same bank (in any place).
Construct the state space of the problem
using suitable representation
Knowledge and types of problems
• There are four types of problems:
–Single state problems (Type 1)
–Multiple state problems (Type 2)
–Exploration problems (Type 3)
–Contingency Problems (Type 4)
• This classification is based on the level of knowledge that
an agent can have concerning its action and the state of
the world
• Thus, the first step in formulating a problem for an agent
is to see what knowledge it has concerning
–The effects of its action on the environment
–Accessibility of the state of the world
• This knowledge depends on how it is connected to the
environment via its percepts and actions
Example: Vacuum world problem
To simplify the problem (rather than the full
version), let;
• The world has only two locations
– Each location may or may not contain dirt
– The agent may be in one location or the other
• Eight possible world states
• Three possible actions (Left, Right, Suck)
– Suck operator clean the dirt
– Left and Right operators move the agent from
location to location
• Goal: to clean up all the dirt
Clean House Task
Vacuum Cleaner state Space
Single state problem
• Accessible: The world is accessible to the agent
– It can determine its exact state through its sensors
– The agent’s sensor knows which state it is in
• Deterministic: The agent knows exactly the effect of its actions
– It can then calculate exactly which state it will be in after any
sequence of actions
• Action sequence is completely planned
Example - Vacuum cleaner world
– What will happen if the agent is initially at state = 5 and
formulates action sequence - [Right, Suck]?
– Agent calculates and knows that it will get to a goal state
• Right {6}
• Suck {8}
Multiple state problems
• Deterministic: The agent knows exactly what each of its actions do
– It can then calculate which state it will be in after any sequence of actions
• Inaccessible: The agent has limited access to the world state
– It might not have sensors to get full access to the environment states or as
an extreme, it can have no sensors at all (due to lack of percepts)
• If the agent has full knowledge of how its actions change the world,
but does not know of the state of the world, it can still solve the task
Example - Vacuum cleaner world
– Agent’s initial state is one of the 8 states: {1,2,3,4,5,6,7,8}
– Action sequence: {right, suck, left, suck}
– Because agent knows what its actions do, it can discover and reach to goal
state.
Right [2.4.6.8.] Suck {4,8}
Left {3,7} Suck {7}
Contingency Problems
• Non-deterministic
– The agent is ignorant of the effect of its actions
• Inaccessible
– The agent has limited access to the world state
• Sometimes ignorance prevents the agent from finding a
guaranteed solution sequence.
• Suppose the agent is in Murphy’s law world
– The agent has to sense during the execution phase, since things
might have changed while it was carrying out an action. This
implies that
• the agent has to compute a tree of actions, rather than a linear sequence of
action
– Example - Vacuum cleaner world:
• action ‘Suck’ deposits dirt on the carpet, but only if there is no dirt already.
Depositing dirt rather than sucking returns from ignorance about the effects
of actions
Contingency Problems (cont…)
• Example - Vacuum cleaner world
– What will happen given initial state {1,3}, and action
sequence: [Suck, Right, Suck]?
{1,3} {5,7} {6,8} {8,6} (failure)
• Is there a way to solve this problem?
– Thus, solving this problem requires local sensing, i.e.
sensing the execution phase,
– Start from one of the states {1,3}, and take improved action
sequence [Suck, Right, Suck (only if there is dirt there)]
• Many problems in the real world are contingency
problems (exact prediction is impossible)
– For this reason many people keep their eyes open while
walking around or driving.
Exploration problem
• The agent has no knowledge of the environment
– World Inaccessible: No knowledge of states (environment)
• Unknown state space (no map, no sensor)
– Non-deterministic: No knowledge of the effects of its actions
– Problem faced by (intelligent) agents (like, newborn babies)
• This is a kind of problem in the real world rather than in a model,
which may involve significant danger for an ignorant agent. If the
agent survives, it learns about the environment
• The agent must experiment, learn and build the model of the
environment through its results, gradually, discovering
– What sort of states exist and What its action do
– Then it can use these to solve subsequent (future) problems
• Example:
–Route finding problem: Drive between cities
drive (city x, city y)
–Coloring problem: Paint black color
paint (color w, color b)
Goal test function
• The agent execute to determine if it has
reached the goal state or not
– Is a function which determines if a single state is a
goal state
• Example:
– Route finding problem:
Reach Bucharest airport on time (IsCity(x,
Bucharest))
– Coloring problem:
All rectangles black (IsBlack(x, y, z)
Path cost function
• A function that assigns a cost to a path (sequence
of actions).
– Is often denoted by g. Usually, it is the sum of the costs
of the individual actions along the path (from one state to
another state)
– A sequence of actions in the state space. For example,
we may prefer paths with fewer or less costly actions
• Example:
– Route finding problem:
• Path cost from initial to goal state
– Coloring problem:
• One for each transition from state to state till goal state is
reached
Example problems
We can group well defined problems into two:
• Toy Problems
–Are problems that are useful to test and demonstrate
methodologies
–Concise and exact description (abstract version) of real
problem
–Can be used by researchers to compare the performance of
different algorithms
Example:
– The vacuum Cleaner world, The 8-puzzle, The 8-queens problem
• Real-World Problem
–More difficult and complex to solve, and there is no single
agreed-upon description
–Have much greater commercial/economic impact if solved
Example:
– Route finding, Traveling sales person
The Vacuum Cleaner World
• Problem formulation
– State : one or both of the eight states shown in the
Figure
– Operators: move left, move right, suck
– Goal test: no dirt left in any square
– Path cost: each action costs 1
The 8-puzzle
• Problem formulation
–State: the location of each of the eight tiles in one of the
nine squares
–Operators: blank space moves left, right, up, or down
–Goal test: state matches the right figure (goal state)
–Path cost: each step costs 1, since we are moving one
step each time.
5 4 1 2 3
6 1 8 8 4
7 3 2 7 6 5
Initial State Goal State
The 8-queens problem
• Problem formulation
– State : any arrangement of 1 to 8 queens on board
– Operators: move a queen to any square
– Goal test: 8 queens on board, none attacked
– Path cost: zero
Searching Strategies
Uninformed Search
Examples of Search Problems
• Route Finding path: Search through set of paths
–Looking for one which will minimize distance
• Machine learning: Search through a set of
concepts
–Looking for a concept which achieves target categorisation
• Chess: search through set of possible moves
–Looking for one which will best improve position
• Theorem proving: Search through sets of
reasoning steps
–Looking for a reasoning progression which proves
theorem
• Missionaries and Cannibals: Search through set
of possible crossing the river
–Looking for one which transports missionaries & cannibals
Problem Spaces
• For the given problem we have to identify:
– A set of states
– Special states: initial state(s), goal state(s)
– Successor function (also called operators)
• In combination, these create problem space or a
graph which can be used for searching
Arad
(b) After expanding Arad generating a new state
Sibiu Timisoara Zerind
choosing one Arad
option
7 2 4 7 2 4
8 6 5 8 6
5 3 1 3 1
2 4 7 2 4 7 2 4 7 2 4 7 2 4 7 2 4
7 8 6 8 6 5 8 6 5 6 5 8 6 5 8 6
5 3 1 5 3 1 3 1 3 8 1 3 1 3 1
Search algorithm
• Two functions needed for conducting search
–Generator (or successors) function: Given a state and action, produces
its successor states (in a state space)
–Tester (or IsGoal) function: Tells whether given state S is a goal state
IsGoal(S) True/False
–IsGoal and Successors functions depend on problem domain.
• Informed search
– Greedy search
– A*-search
– Iterative improvement,
– Constraint satisfaction
– etc.
Breadth first search
•Expand shallowest unexpanded node,
–i.e. expand all nodes on a given level of
the search tree before moving to the next
level
•Implementation: use queue data
structure to store the list:
–Expansion: put successors at the end of
queue
–Pop nodes from the front of the queue
•Properties:
–Takes space: keeps every node in
memory
–Optimal and complete: guarantees to
find solution
Algorithm for Breadth first search
function BFS (problem){
open = (C_0); //put initial state C_0 in the List
closed = {}; /maintain list of nodes examined earlier
while (not (empty (open))) {
f = remove_first(open);
if IsGoal (f) then return (f);
closed = append (closed, f);
l = not-in-set (Successors (f), closed );
open = merge ( rest(open), l); //append to the
list
}
return ('fail')
}
Uniform cost Search
•So far we’ve ignored the issue of edge costs.
•The goal of this technique is to find the shortest path
to the goal in terms of cost.
–It modifies the BFS by always expanding least-cost
unexpanded node
•Implementation: nodes in list keep track of total path
length from start to that node
–List kept in priority queue ordered by path cost
A
S S S
1 10 S
5 B 5
S G 0 A B C A B C A B C
1 5 15 5 15 15
G G G
15 5
11 11 10
C
•Properties:
–This strategy finds the cheapest solution provided the cost of
a path must never decrease as we go along the path
g(successor(n)) ≥ g(n), for every node n
–
Algorithm for Uniform Cost search
function uniform_cost (problem){
open = (C_0); //put initial state C_0 in the List
g(s) = 0;
closed = {}; /maintain list of nodes examined earlier
while (not (empty (open))) {
f = remove_first(open);
if IsGoal (f) then return (f);
closed = append (closed, f);
l = not-in-set (Successors (f), closed );
g(f,li) = g(f) + c(f,li);
open = merge(rest(open), l, g(f,li)); //keep the open list sorted in
ascending order by edge cost
}
return ('fail')
}
Depth-first search
•Expand one of the node at the deepest
level of the tree.
–Only when the search hits a non-goal dead
end does the search go back and expand
nodes at shallower levels
•Implementation: treat the list as stack
–Expansion: push successors at the top of
stack
–Pop nodes from the top of the stack
•Properties
–Incomplete and not optimal: fails in infinite-
depth spaces, spaces with loops.
• Modify to avoid repeated states along the path
–Takes less space (Linear): Only needs to
remember up to the depth exapanded
Algorithm for Depth first search
function DFS (problem){
open = (C_0); //put initial state C_0 in the List
closed = {}; /maintain list of nodes examined earlier
while (not (empty (open))) {
f = remove_first(open);
if IsGoal (f) then return (f);
closed = append (closed, f);
l = not-in-set (Successors (f), closed );
open = merge ( rest(open), l); //prepend to the
list
}
return ('fail')
}
Depth-First & Breadth-First Search
• In the case of DFS and
BFS a search is
determined by the order in
which the nodes are
examined.
• Consider the following
graph:
Start Goal
Bidirectional Search
• Advantages:
– Only need to go to half depth
– It can enormously reduce time complexity, but is not
always applicable
• Difficulties
– Do you really know solution? Unique?
– Cannot reverse operators
– Memory requirements may be important: Record all
paths to check they meet
• Memory intensive
413 =
220+193
Arad 646 Fagaras 415 Oradea 671
Rimnicu Vilcea
415
=317+98
Pitesti Craiova 573
418
=418+0 Bucharest Craiova 615 Rimnicu 607
Properties
• A* search is complete, optimal, and optimally
efficient for any given admissible heuristic function
• Complete
– It guarantees to reach to goal state
• Optimality
– If the given heuristic function is admissible, then A* search
is optimal.
– Proof optimality of A* search
• Takes memory:
– It keeps all generated nodes in memory
– Both time and space complexity is exponential
• Time and space complexity of heuristic algorithms
depends on the quality of the heuristic function.
– Worst case is exponential
Local Search
• In many problems, the path to the goal is irrelevant; the goal state itself is
the solution
• Being able to ignore the path offers two main advantages:
–Less memory is used, generally a constant amount.
–A reasonable solution can be found in infinite search spaces for which
systematic search would be unsuited.
–They are also very useful for optimization problems where the goal is to find the
best state according to a function. Many of these problems are not suitable for
standard search.
• Implementation:
–Evaluate the successors and chose the best state that leads
us closer to the goal (according to the heuristic estimate), and
continue from there
Hill climbing algorithm
Function hill_climbing(problem) returns a solution state
variable: current and next (nodes)
current = initial state
while current ≠ NULL do
next = a highest value successor of current
if (value (next) < value (current)) then
return current
current = next
end loop
end function
Properties
• If an accurate heuristic measure is available in the
domain, and if there are no local maxima, the
algorithm is
–fast to reach to goal state
• it just looks a head to determine if any successor is better than the
current state
–takes less memory
• space requirement is constant since current state is the only node
stored
2 8 3 1 2 3
1 6 4 8 4
7 5 7 6 5
Hill – climbing…
• Traveling Sales person problem: Given a set of cities, with
costs of getting from one to the other, find a minimum cost
route that goes through each of them exactly once.
• Consider the following example: Salesperson lives in city A. He has to
visit all the 5 cities, A to E, and must return home afterwards. Goal is
circle from A to A with minimal cost. Find shortest path for travel
minimize cost and/or time of travel
Problems with hill climbing
• May lead to local maxima – once it reaches to local maxima, it
can’t go to global max. It caught by small foothills.
• Walking in the Plateau - situations where all of the local steps
are indistinguishable; all of them have the same value.
• ridges traversal - there is a direction we would like to move, but
none of the allowed steps actually takes us in that direction.
Possible Improvements
• Random-restart (Simulated Annealing)
–To avoid the problems of hill-climbing, we need to mix some
randomness with the greedy uphill steps such that
• incase the algorithm stuck on a local maximum, we could allow the
search to take some down hill steps to escape the local maximum
• The basic idea is that, instead of always going up the
sharpest incline, pick a random direction.
–If the move improves the situation; i.e. if it goes uphill, go
there and execute it;
–otherwise, go downhill with probability which depends on
how bad the move is (the probability decreases
exponentially with the badness of the move.
Simulated Annealing vs. Hill climbing
Ball on terrain example: The ball is initially placed at a random position on the
terrain. From the current position, the ball should be fired such that it can only
move one step left or right. What algorithm should we follow for the ball to
finally settle at the lowest point on the terrain?
Initial position
of the ball Simulated Annealing explores
more. Chooses this move with a
small probability (Hill Climbing)
Greedy Algorithm
gets stuck here!
Locally Optimum
Solution.
MIN
MAX
MIN
MAX
MIN
Alpha-Beta Pruning
• We can improve on the performance of the min-
max algorithm through alpha-beta pruning
XX
Exercise: mark pruned paths with ‘X’
MAX
MIN
MAX
MIN
MAX
MIN
Effectiveness of alpha-beta
• Alpha-beta is guaranteed to compute the same
value for the root node as computed by min-max,
with less or equal computation
• Worst case: no pruning, examining bm leaf nodes,
where each node has b children and a m-depth
search is performed
• Best case is when each player’s best move is the
first alternative generated
– Best case: examine only bm/2 leaf nodes.
– The result shows that you can search twice as
deep as min-max.
• In Deep Blue (chess program), they found
empirically that alpha-beta pruning meant that the
average branching factor at each node was about 6
instead of about 35!