AI - Problem Solving - INCOMPLETE

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

Introduction to AI -

Problem solving in AI

Subject : Artificial Intelligence

Course: Bachelor of Computer Science, &


Bachelor of Computer Engineering

Asst. Professor SSS Shameem


SOSE, MIU
Content

Problem reduction & Search Algorithm


oGenerate and test

oDepth first
oBreadth first
oHill climbing
oBest first search
oA* Search
oAO* search

AI
Search Algorithms

• Many traditional search algorithms are used in AI applications.

• For complex problems, traditional algorithms are unable to find solutions


within some practical time and space limits.

• Consequently, many special techniques are developed, using heuristic


functions.
Search Algorithms

Hierarchical Representation of Search Algorithms: representation begins with


two types of search;

• Uninformed Search: Also called blind, exhaustive or brute-force search.


oit uses no information about the problem to guide the search and therefore
may not be very efficient.

• Informed Search: Also called heuristic or intelligent search.


• this uses information about the problem to guide the search.
• usually guesses the distance to a goal state and is therefore efficient, but
the search may not be always possible
Search Algorithms
• Uninformed search algorithms or Brute-force algorithms, search through the
search space all possible candidates for the solution checking whether each
candidate satisfies the problem’s statement.

• Informed search algorithms use heuristic functions that are specific to the
problem, apply them to guide the search through the search space to try to
reduce the amount of time spent in searching.

• Heuristic algorithms are not really intelligent; they appear to be intelligent


because they achieve better performance.

• Heuristic algorithms are more efficient because they take advantage of


feedback from the data to direct the search path.
AI – Sample problems

• games such as 3x3 eight-tile, 4x4 fifteen-tile, and 5x5 twenty four tile puzzles
are single-agent-path-finding challenges.
• consist of a matrix of tiles with a blank tile.
• player is required to arrange the tiles by sliding a tile either vertically or
horizontally into a blank space with the aim of accomplishing some objective.

Other examples,
• Travelling Salesman Problem, Rubik’s Cube, and Theorem Proving.

AI
Means-Ends Analysis (MEA)
• Problem-solving technique to achieve end-goals.

• With MEA, it is possible to control entire process of problem solving.

• Starts from predetermined goal, in which actions are chosen to lead to goal.

• Each executed action leads to next action; everything is connected together


in order to reach end-goal.

• Problems may arise.

• With help of MEA, both forward and backward research can be done to
determine where stagnation is occurring.

• Enables larger parts of problem to be solved first, to subsequently return to


smaller problems afterwards.
Means-ends analysis
• Involves detection of difference between current state and goal state

• Once difference identified, an operator to reduce the difference must be found

• But operator cannot be applied to current state

oSubproblem of getting to state where operator can be applied

• Operator may not result in goal state

oSecond subproblem of getting from new state to goal state

• MEA process applied recursively

• Each rule (operator) has


oLHS preconditions and RHS aspects of problem state changed.

oDifference table of rules and differences they can reduce.


Means-Ends Analysis
• Compare CURRENT to GOAL. If no differences, return.

• Otherwise select most important difference and reduce it by doing the


following until success or failure is indicated.

1) Select an untried operator O that is applicable to current difference. If


there are no such operators then signal failure.

2) Attempt to apply O to current state. Generate descriptions of two states


O-START a state in which O’s preconditions are satisfied and O-RESULT, the
state that would result if O were applied in O-START.

3) If (FIRST-PART MEA (CURRENT,O-START) AND (LAST-PART MEA (O-RESULT,


GOAL) are successful then signal success.
Brute-Force Search Strategies

• They are most simple.

• Do not need any domain-specific knowledge.

• also called uninformed search and blind search.

• Work fine with small number of possible states.

• Requirements −

o Initial state

o State description

o A set of valid operators

o Goal state description AI


Brute-Force Search Strategies
• Proceed in a systematic way by exploring nodes in some predetermined order
or simply by selecting nodes at random.

• Search programs either return only a solution value when a goal is found, or
record_and_return the solution path.

• Important brute-force techniques are as below.

• Generate and test • Bidirectional Search


• Exhaustive search
• Combinatorial Search
o Depth first
o Breadth first • …….
• Hill climbing
• Best first search
AI
Brute-Force Search Strategies

Basic algorithm

• To apply brute-force search to a specific class of problems, one must implement


four procedures, first, next, valid, and output.

• These procedures take data P (for the particular instance of problem that is to be
solved) as parameter, and should do following:

• first (P): generate a first candidate solution for P.

• valid (P, c): check whether candidate c is a solution for P.

• next (P, c): generate the next candidate for P after the current one c.

• output (P, c): use the solution c of P as appropriate to the application.


AI
Brute-Force Search Strategies
• Next procedure must tell when there are no more candidates for the instance P,
after the current one c.

• A convenient way is to return a "null candidate“ (some conventional data value


Λ that is distinct from any real candidate).

• If there are no candidates at all for instance P  first procedure should return Λ

• Brute-force method is then expressed by the algorithm

AI
Brute-Force Search Strategies__ Speed up
• Reduce search space, i.e. set of candidate solutions by using heuristics specific to
the problem class.

• Little bit of analysis reduces number of candidate solutions.

• Reduce to set of valid candidates; that directly enumerates desired solutions,


without wasting time with tests and the generation of invalid candidates.

Example, find all integers between 1 & 1,000,000 that are evenly divisible by 417.

• Can be solved much more efficiently by starting with 417, and repeatedly
adding 417 until the number exceeds 1,000,000

• Takes only 2398 (= 1,000,000 ÷ 417) steps, and no tests.

AI
Brute-Force Search Strategies__ Speed up

Example, EIGHT QUEENS PROBLEM.

place eight queens on a standard chessboard, so that no queen attacks any other.

AI
Brute-Force Search Strategies__ Speed up

Eight queens problem.

• Since each queen can be placed in any of the 64 squares, in principle there are
64^8 = 281,474,976,710,656 possibilities to consider.

• But, queens are all alike, and no two queens can be placed on same square;
possible ways of choosing a set of 8 squares from set all 64 squares is

= 64!/(56!*8!) = 4,426,165,368 candidate solutions

– about 1/60,000 of the previous estimate.

• Further, no arrangement with two queens on same row or same column or


same diagonal direction  reduced the candidate solutions to a very few only
AI
Brute-Force Search Strategies__ Reordering

• If applications require only one solution rather than all solutions,


expected running time of a brute force search often depends on the order in
which candidates are tested.

• one should test most promising candidates first.

example, when searching for a proper divisor of a random number n, it is better to


enumerate candidate divisors in increasing order, from 2 to n- 1.

• probability of a candidate being valid is often affected by previous failed trials.

AI
Generate and test

• Simplest search strategy

• Guarantees to find solution if done systematically.

• Acceptable for simple problems.

• Inefficient for problems with large space.

• Should find a solution eventually, but could take a long time.

• Plan generate‐test:

− Create a list of candidates.


− Apply generate‐and‐test to that list
Generate and test
Generate and test

Algorithm:
(1) Generate a possible solution
(2) Test to see if it is a solution (compare end point of path to goal state)
(3) If solution is found, quit. Else return to (1)
Generate and test _ Example

• Suppose lecture is giving additional class in one of the classroom in level-4.


But, you don’t exactly remember the room number.

• Level-4 for has 10 classrooms which are numbered from 401 to 410
respectively.

• How we can apply generate and test algorithm to find the classroom?

• What is the maximum number of classrooms you may try until the correct
one is found?
Generate and test _ Example
Generate and test _ Example

• Is it good to apply the generate and test algorithm if the level is not known,
and the building has 20 levels??

• What is the maximum number of classrooms you may try until the correct
one is found.

Maximum number of classrooms = 20 × 10 = 200.


Generate and test

• Similar to Depth first search procedure

• Requires all solutions be generated for testing.

• In its systematic form, it is only an exhaustive search of problem space.

• Solutions can also be generated randomly but correctness is not


guaranteed.

• Known as British Museum algorithm:

- finding an object in the British Museum by wandering randomly.


Generate and test

• Generating complete solutions, and generating random solutions are two


extremes; hence exists another approach that lies in between.
• Search process proceeds systematically but some paths that unlikely to
lead to solution; hence not considered.

• Performed by a heuristic function.

• Depth-first search tree with backtracking can be used to implement systematic


generate-and-test procedure.

oIf some intermediate states are likely to appear often in the tree, it would
be better to modify that procedure to traverse a graph rather than a tree.
Generate and test

• Exhaustive generate-and-test is very useful for simple problems.

• Can be effective by combining with other techniques by restricting search space.

• (example) AI program DENDRAL uses plan-Generate-and-test technique.

 First, planning process uses constraint-satisfaction techniques and creates


lists of recommended substructures.
 Then generate-and-test procedure uses lists generated and required to
explore only a limited set of structures.
 This way of Constrained generate-and-test is proved to be highly effective.
Generate and test

Weakness of planning:

• Produces inaccurate solutions.


oBut if used to produce only pieces of solutions, then lack of detailed accuracy
becomes unimportant.

• For complex problems, not very effective technique.


Heuristic Search

A heuristic is a method that

• might not always find best solution,

• but is guaranteed to find a good solution in reasonable time.

• By sacrificing completeness, it increases efficiency.

• Useful in solving tough problems which

o could not be solved any other way.

o solutions take an infinite time or very long time to compute.

• Classic example of heuristic search methods is Travelling Salesman Problem


(TSP).
AI
Informed (Heuristic) Search Strategies

To solve large problems with large number of possible states, problem-specific


knowledge needs to be added to increase the efficiency of search algorithms.

Heuristic Evaluation Functions

• They calculate cost of optimal path between two states.

• A heuristic function for sliding-tiles games is computed by counting number of


moves that each tile makes from its goal state, and adding these number of
moves for all tiles.

AI
Pure Heuristic Search

• It expands nodes in order of their heuristic values.

• Creates two lists, a closed list for already expanded nodes, and open list for
created but unexpanded nodes.

• In each iteration, a node with minimum heuristic value is expanded, all its
child nodes are created and placed in closed list.

• Then, heuristic function is applied to child nodes and they are placed in the
open list according to their heuristic value.

• Shorter paths are saved, and longer ones are disposed.

AI
Heuristic Search

• Direct techniques (blind search) are not always possible (they require too
much time or resource).

• Weak techniques can be effective if applied correctly on the right kinds of


tasks. – Typically require domain specific information.

AI
Heuristic Search - 8 Puzzle

• 8 Puzzle problem  Which move is best?


AI
Heuristic Search - 8 Puzzle

• Blind search techniques use arbitrary ordering of operations.

• Heuristic search techniques use domain specific information - a heuristic.

• heurisitic(s) can be used to decide which 8-puzzle move is “best” (first).

• For now - establish some ordering to possible moves (values of heuristic does
not matter as long as it ranks the moves).

• Later - worry about actual values returned by the heuristic function

AI
Heuristic Search - 8 Puzzle

• Number of tiles in correct position.

– higher the number, the better.

– Easy to compute (fast, and takes lesser memory).

– simplest possible heuristic.

• Count how far away (how many tile movements) each tile is from it’s correct
position.

• Sum up this count over all the tiles.

• This is another estimate on the number of moves away from a solution.

AI
Breadth-First Search

• Searches breadth-wise in the problem space

• Starts from root node  explores neighbouring nodes first  moves towards
next level neighbours.

• Generates one tree at a time until solution is found.

• Can be implemented using FIFO queue data structure.

• Initially, queue contains just root.

• In each iteration, node at the head of the queue is removed and then expanded.

• Generated child nodes are then added to the tail of the queue.
Breadth-First Search
Breadth-First Search

• Provides shortest path to the solution.

• If branching factor (average number of child nodes for a given node) = b, and
depth = d, then number of nodes at level d = bd.
• total no of nodes created in worst case is b + b2 + b3 + … + bd.

AI
Breadth-First Search _ Algorithm

• Create a variable called NODE-LIST and set it to the initial state.

• Loop until the goal state is found or NODE-LIST is empty.

o Remove the first element, say E, from the NODE-LIST. If NODE-LIST was
empty then quit.

o For each way that each rule can match the state described in E do:

i) Apply the rule to generate a new state.

ii) If the new state is the goal state, quit and return this state.

iii) Otherwise add this state to the end of NODE-LIST

AI
Breadth-First Search
Advantage

• Breadth first search will never get trapped exploring useless path forever.

• If there is a solution, BFS will definitely find it out.

• If there is more than one solution then BFS can find the minimal one that
requires less number of steps.

Disadvantage

• Space/memory requirement to store nodes is exponential.

• Complexity depends on the number of nodes.

• If solution is farther away from root, BFS will consume lot of time

AI
Depth-First Search

• DFS searches deeper into the problem space.

• Mostly implemented recursively, with the recursion stack taking place of an


explicit node stack.

• Implemented in recursion with LIFO stack data structure.

• Creates same set of nodes as Breadth-First method, only in different order.

• With branching factor b and depth as m, the storage space is bm.

AI
Depth-First Search
Depth-First Search _ Algorithm

1. If initial state is a goal state, then quit and return success.

2. Else, loop until success or failure is signalled.

a) Generate a state, say E, and let it be the successor of the initial state. If
there is no successor, signal failure.

b) Call Depth-First Search with E as the initial state.

c) If success is returned, signal success. Otherwise continue in this loop.

AI
Depth-First Search

Advantage - Memory requirement is only linear with respect to search graph,


compared to BFS which requires more space.

o only needs to store stack of nodes on path from root to current node.

• Finds solution without exploring much in a path  time taken also very less.

AI
Depth-First Search

Disadvantage − May not terminate, and go on infinitely on one path. The solution
to this issue is to choose a cut-off depth.

o If ideal cut-off is d, & cut-off is lesser than d, then this algorithm may fail.

o If chosen cut-off is more than d, then execution time increases.

• not guaranteed to find the solution.

• no guarantee to find a minimal solution, if more than one solution exists.

• Complexity depends on the number of paths. It cannot check duplicate nodes.

AI
Depth-First ITERATIVE DEEPENING (DFID) Search

• Combines the best features of BFS & DFS

• First performs a DFS to depth 1, then starts over; executing a complete DFS to
depth 2, and continues to run DFS to successively greater depths, until a
solution is found.

• first solution found is guaranteed to be along a shortest path.

• Never creates a node until all lower nodes are generated.

• Only saves a stack of nodes.

• The algorithm ends when it finds a solution at depth d.

• The number of nodes created at depth d is bd and at depth d-1 is bd-1.

AI
Best-First-Search
• Depth‐first search: not all competing branches having to be expanded.

• Breadth‐first search: not getting trapped on dead‐end paths.

⇒ Combining the two: follow a single path at a time, but switch paths whenever
some competing path look more promising than current one.
Best-First-Search

• Traverse the graph (from initial node) following best current path.
• Pick one/best unexpanded nodes on that path and expand it.
• Add its successors to graph and compute for each of them
• Change expanded node’s value to reflect its successors.
• Propagate the change up the graph.
• Reconsider current best solution and repeat until a solution is
found
Best-First-Search _ Algorithm

• OPEN = {initial state}.

• Loop until a goal is found or there are no nodes left in OPEN:


− Pick the best node in OPEN
− Generate its successors

− For each successor:


new →evaluate it, add it to OPEN, record its parent generated before
→change parent, update successors
Best-First-Search

• OPEN: nodes that have been generated, but have not examined.

• This is organized as a priority queue.

• CLOSED: nodes that have already been examined.

• Whenever a new node is generated, check whether it has been generated before

Greedy search:

• h(n) = estimated cost of the cheapest path from node nto a goal state

• Neither optimal nor complete


Uniform Cost Search

g(n) = cost of the cheapest path from the initial state to node n.

• Sorting is done in increasing cost of the path to a node.

• Always expands the least cost node.

• Identical to Breadth First search if each transition has the same cost.

• Explores paths in the increasing order of cost.

• Optimal and complete, but very inefficient

AI
Bidirectional Search

• Searches in two directions at the same time.

• forward from initial state and backward from goal state till both meet to
identify a common state.

• Search stops when searches from both directions meet in the middle.

• Requires an explicit goal state instead of simply a test for a goal condition.

• Path from initial state is concatenated with the inverse path from goal state.

• Each search is done only up to half of the total path.

AI
Bidirectional Search
Advantage

• Speed: Sum of the time taken by two searches (forward and backward).

• It requires less memory.

• guarantees optimal solutions.

Disadvantage

• Implementation is difficult because additional logic must be included to decide


which search tree to extend at each step.

• One should have known the goal state in advance.

• Algorithm must be too efficient to find the intersection of the two search trees.

• Not always possible to search backward


AI through possible states.
Hill-Climbing Search

• A variation of generate-and-test algorithm

• discards all states that don’t look promising or seem unlikely to lead to goal state.

• Next node to expand, is the node with highest value of nodes generated from the
expansion of current node.

• To take such decisions, it uses heuristics (an evaluation function) which indicates
how close the current state is to goal state.
Hill-Climbing = generate-and-test + heuristics (direction to move)

• Key difference between Hill climbing and Generate-&-test: use of heuristic or


evaluation function as a way to inject task specific knowledge into control process.

• heuristic or evaluation function estimates how close a given state is to goal state
AI
Hill-Climbing Search

• In this technique, starting at base of hill walk upwards until reach top of hill.

o start with initial state and keep improving the solution until its optimal.

• Is a variant of generate-and test in which feedback from test procedure is used


to help the generator decide which direction to move in search space.

• Test function is augmented with a heuristic function that provides estimate of


how close given state is to the goal state.

• Computation of heuristic function can be done with negligible amount of


computation.

• Hill climbing is often used when a good heuristic function is available for
evaluating states and when no other useful
AI
knowledge is available.
Hill-Climbing Search
• Heuristic search used for mathematical optimization of problem solving.

• For given large set of inputs and a good heuristic function, it tries to find
sufficiently good solution to the problem.

• Solution May not be global optimal maximum.

• Solves problems where we need to maximize or minimize a given real function by


choosing values from given inputs.

• Iterative algorithm that starts with arbitrary solution to a problem and attempts
to find better solution by changing a single element of the solution incrementally.

• If change produces better solution, incremental change is taken as new solution.

• This process is repeated until there are no further improvements.

• function Hill-Climbing (problem), returns


AI
a state that is a local maximum.
Hill-Climbing Search _ Algorithm

1. Evaluate initial state; if it is goal state quit otherwise current state is initial
state.

2. Select a new operator for this state and generate a new state.

3. Evaluate the new state

o if it is closer to goal state than current state make it current state

o if it is no better ignore

4. If current state is goal state or no new operators available, quit. Otherwise


repeat from 2.

AI
State Space Diagram Hill-Climbing Search

• State space diagram is a graphical representation of the set of states our search
algorithm can reach vs value of objective function (function which we wish to
maximize).

• X-axis : denotes state space i.e. states or configuration our algorithm may reach.

• Y-axis : denotes values of objective function corresponding to a particular state.

• Best solution will be that state space where objective function has maximum
value (global maximum).

AI
State Space Diagram Hill-Climbing Search

AI
Hill-Climbing Search _ regions
• Local maximum: A state which is better than all neighbors, but there exists a
better state which is far from current state.

• Global maximum : It is the best possible state in the state space diagram. This
because at this state, objective function has highest value.

• Plateau//flat local maximum: All neighboring states have same heuristic values,
so it’s unclear to choose the next state by making local comparisons

• Shoulder : A plateau that has an uphill edge

• Ridge: An area which is higher than surrounding states, but it can not be reached
in single move; special kind of local maximum. (example; only four possible
directions to explore (N, E, W, S), and Ridge exists in NE direction)

• Current state : currently present Region


AI of state space diagram during the search.
Hill-Climbing Search _ Disadvantages
• Local maximum : All neighbouring states have values worse than current state.
Since hill climbing uses greedy approach, it will not move to worse state and
terminate itself. Process will end even though better solution may exist.

Solution: Maintain a list of visited states. If search reaches an undesirable


state, it can backtrack to previous configuration and explore a new path.

AI
Hill-Climbing Search _ Disadvantages
• Plateau : All neighbours have same value; not possible to select best direction.

Solution: Make a big jump. Randomly select a state far away from current
state. Chances are that we will land at a non-plateau region

• Ridge : Any point on a ridge can look like peak; & movement in all possible
directions is downward. Algorithm stops when it reaches this state.

Solution: Use two/more rules before testing; moving in several directions


at once. Explore several directions to figure out the correct path.

AI
Hill-Climbing Search _ Example

• Blocks World Domain – Move(X, S,D)

AI
Hill-Climbing Search _ Example

• Key point: choose an appropriate heuristic function.

• h(x) = +1 for all blocks in the support structure of the block is correctly positioned

otherwise -1

• Call any block correctly positioned if it has same support structure as goal state.
AI
Hill-Climbing Search _ Example

AI
Hill-Climbing Search _ Example

AI
Hill-Climbing Search _ Example

AI
Hill-Climbing Search _ Example
• Show the steps of applying hill climbing algorithm on this
tree for initial state (a) to final state (h).

• Can the algorithm lead to the goal state

• Can Hill climbing algorithm guarantee to find a solution (find


the global maxima)?

• How we can solve this problem?


AI

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy