You are on page 1of 17

Search

Search:
It is the process of navigating from a start state to a
goal state by transitioning through intermediate states.

A search problem consists of:


• State Space: All possible states
• Start State: The state where the search begins.
• Goal State: The state where the search ends. (Desired
state)
• Goal Test: Returns whether the current state is the goal
state or not.
• Successor Function: Returns the available states that can
be accessed and its associated costs.

Solution:
A sequence of actions that will lead from the start state to the
goal state.

State Space graph:


A mathematical representation of a search problem
• Nodes represent states.
• Edges represent successor functions.
• Goal test is a goal node or a set of goal nodes.
Search Trees:
• The start state is the root node.
• Children correspond to successors.
• Nodes are states.
Search Strategies:
Strategies are evaluated based on these criteria:
Completeness: If a solution is possible, will it always be found?
Optimality: Does it find the least cost solution?
Time Complexity: Number of nodes expanded.
Space Complexity: Number of nodes stored in memory.

Time and Space complexity is measured using these terms:


b – max branching factor
d – depth of least cost solution
m – maximum depth of solution

• Frontier: to store the nodes waiting for expansion.


• Expansion: to find and display the children of the node.
• Expansion strategy: to decide which node in the Frontier to
expand.
Breadth First Search (BFS):
• The root node is placed in the Frontier (open) and then
expanded (placed into closed list).
• The successors of the root node are placed in the Frontier
(open) to be expanded.
• Then, each node’s successors are placed in the Frontier
(open) to be expanded.
• Repeat until open = {} or Goal state is reached
The expansion follows a First in First Out (FIFO)
Example:

Open = {A} Closed = {}


Open = {B, E} Closed = {A}
Open = {E, C, D} Closed = {B, A}
Open = {C, D, F, G} Closed = {E, B, A}
Open = {D, F, G} Closed = {C, E, B, A}
Open = {F, G} Closed = {D, C, E, B, A}
Open = {G} Closed = {F, D, C, E, B, A}
Open = {} Closed = {G, F, D, C, E, B, A}

Criteria measurements for BFS:


• Complete: Yes, it will find a solution (if one exists)
• Optimal: Yes, it will find the closest goal node to the root
node (optimal solution)
• Time Complexity: 𝑂(𝑏 𝑑 ), As b x b x b … until a solution is
found i.e. when the least-cost solution depth is reached.
• Space Complexity: 𝑂(𝑏 𝑑 ),It will store only the required
nodes to reach the least cost solution depth.

Depth First Search (DFS):


• The root node is placed in the Frontier (open) and then
expanded (placed into closed list).
• The successors of the root node are placed in the Frontier
(open) to be expanded.
• Then, left-most node is placed in the Frontier (open) to be
expanded.
• Once a leaf node is reached, we start to expand the
siblings of the left most leaf node and then go back up to
the parent node.
• Repeat until open = {} or Goal state is reached
The expansion follows a First in Last Out (FILO)

Example:
Open = {A} Closed = {}
Open = {B, E} Closed = {A}
Open = {C, D, E} Closed = {A, B}
Open = {D, E} Closed = {A, B, C}
Open = {E} Closed = {A, B, C, D}
Open = {F, G} Closed = {A, B, C, D, E}
Open = {G} Closed = {A, B, C, D, E, F}
Open = {} Closed = {A, B, C, D, E, F, G}

Criteria measurements for DFS:


• Complete: No, as a tree could have an infinite depth so a
solution may not be found. (In practice, if a tree can be
built, it is complete)
• Optimal: No, it might seek a “deeper goal” while potentially
missing a “shallow goal”
• Time Complexity: 𝑂(𝑏 𝑚 ), The worst-case scenario is that
the goal is non-existent, or it is the right most child i.e.
generates the entire tree.
• Space Complexity: 𝑂(𝑏𝑚),After exploring a line of the left-
most node you delete from memory. Therefore, the only
saved nodes are the sequence of the solution.

Constraint Satisfaction Problems (CSPs):


• CSPs are Identification problems that have constraints
that need to be satisfied.
• Constraints are rules that a legal solution cannot violate.
• Preferences are soft constraints mainly used in
optimizations problems.

CSPs consist of:


• A set of variables
• A domain for each variable
• A set of constraints

An assignment is complete if all the variables have values


within the domain, otherwise it is partial.

A solution is a complete assignment with satisfies all the


constraints.

Types of CSPs:
• Finite domains i.e. where variables have finite domains
(discrete)
• Infinite domains i.e. variables have infinite domains
(continuous)
If a CSP has n variables, the size of the domain is d, then there
are 𝑑𝑛 complete assignments.
Example:
Variables: X, Y
Domain: {1,2,3}
Constraints:
X+Y=4
X*Y=3
Solution:
X=1, Y=3

Example 2:
Problem:
Map colouring problem is to paint a map (e.g. via three colours
red, green and blue) in such a way that none of adjacent
regions can have the same colour.
Variables:
WA, NT, Q, NSW, V, SA, T
Domain:
D = {red, green, blue}
Constraints:
Adjacent regions must have different
colours.
WA ≠ NT, WA ≠ SA, NT ≠ SA, NT ≠ Q, ...
Solutions:
e.g. {WA=red, NT=green, Q=red,
NSW=green, V=red, SA=blue, T=green}
Solving CSPs:
• Generate and Test
• Breadth First Search (BFS)
• Depth First Search (DFS)
o Backtracking
▪ Filtering
• Forward Checking
▪ Ordering
• Local Search Method

Generate and Test:


The exhaustive generate-and-test algorithm is used to generate
all the complete assignments, then test them in turn, and return
the first one that satisfies all of the constraints. It stores 𝑑𝑛
complete assignments where d is the domain size and n is the
number of variables.

Breadth First Search (BFS):


CSPs can be solved using BFS.
Example 1:
Variables: A, B, and C
Domain: {0,1,2}
Constraint: A+B+C=1
Although BFS can be used to solve CSPs, it was not efficient in
solving the example above since you have to traverse 17 nodes
to reach a solution.

Depth First Search:

Using DFS for Example 1, we observe that we have to traverse


only 4 nodes before we reach a solution. For this example, DFS
was a more efficient search method than BFS.
Backtracking Search:
It is a Depth-First Search Method with 2 additional properties:
▪ Check constraints as we go.
▪ Consider 1 variable at a layer.
Example:
Variables:
A, B, and C
Domain:
{0,1,2}
Constraints:
A<B<C

Using the rules in backtracking, we don’t need to generate as


many nodes as in Depth First Search (DFS). This method also
checks the constrains at each layer therefore, removing
constraint violating nodes.

To improve the Backtracking method, two ideas are


implemented:
▪ Filtering: Detects failure early by keeping track of domains
for unassigned variables and crossing off bad options.

▪ Ordering: Choosing the next variable to assign a value to


by choosing the variable with the least legal values.
Filtering:
One of the Filtering methods is called Forward Checking
which crosses off values that violate a constraint.

Forward Checking:
Example:
Variables:
A, B, and C
Domain:
{0, 1, 2}
Constraint: Assigning A the value 0 resulted in C
B>A>C having no legal values to be assigned to
therefore this assignment is illegal.

Ordering:
Variables:
A, B, and C
Domain:
{0, 1, 2} Assigning A the value of 0 constricts C from
Constraint: having three values to having two values and in
the next step we assign C the value 1 which then
A≤B<C
constricts B to only 0. The solution would be A =
0, B = 0, C = 1
Using Backtracking, Forward Checking, and Ordering results in
a very efficient search method.

Local Search:
It is a search method (generally used when a tree cannot be
generated fully) which randomly generates a complete
assignment met with no regard to constraints. Then a random
constraint-violating variable is chosen and assigned a value
which violates the least constraints

Optimization problem:
It is a search problem with preferences.
They consist of variables, domains, and objective functions
(instead of constraints).
Objectives in optimization problems can be either single-
objective or multi-objective optimization problems.
Constraints could be present in optimization problems.
Local Search methods are more effective than Tree Search
methods.
Game:
Game is an adversarial search where there is an opponent
trying to minimize your chances of winning.

Formalization:
▪ States: S (Start state 𝑆0 )
▪ Actions: 𝐴 (may depend on player/state)
▪ Transition function: 𝑆 × 𝐴 → 𝑆
▪ Terminal test: 𝑆 → (𝑡ru𝑒, 𝑓𝑎𝑙𝑠𝑒)
▪ Players: 𝑃 = (1, ..., 𝑁) (usually take turns)
▪ Utilities: 𝑆𝑡𝑒𝑟𝑚𝑖𝑛𝑎𝑙 × 𝑃 → 𝑅 (values on outcomes)
A utility function assigns a value to the result of the game for a
player p at terminal state S.
Value of state: The best achievable outcome (utility) for a state
Agent x is maximizing while Agent O is minimizing the value of
state.(minimax)
MiniMax example:

We fill out the empty nodes from bottom up


On the 2nd layer we are minimizing the utility which means we
will be taking the smallest value from the leaf nodes. (3,2,1
respectively).
On the 1st layer we are maximizing the utility which means we
will take the largest value from the child nodes which is 3.

Criteria of MiniMax:
Complete? Yes, it will find a solution in the finite search tree (if it
exists)
Optimal? Yes, if both players play optimally.
Time complexity? 𝑂(𝑏 𝑚 )
Space Complexity? 𝑂(𝑏𝑚)
Alpha-Beta Pruning Algorithm
▪ α is set to - ∞ and β is set to ∞ at the root node
▪ α only changes at MAX nodes
▪ β only changes at MIN nodes
▪ a node is pruned when α ≥ β.
Example:
Fin.

You might also like