Searching. COSC 159 - Fundamentals of AI2 Overview Problem-solving agents Example Problems Searching...

Preview:

Citation preview

Searching

COSC 159 - Fundamentals of AI 2

Overview

• Problem-solving agents

• Example Problems

• Searching

• Measuring performance

• Search strategies

• Partial Information

COSC 159 - Fundamentals of AI 3

Problem Solving Agents• Goal-based agent

Agent

Sensors

Actuators

Environm

ent

Percepts

Actions

What the world is like now

What action I should do now

Goals

What my actions do

How the world evolves

State

What will it be like if I do action A

COSC 159 - Fundamentals of AI 4

Problem Solving

• World is made of up discrete states• A state is the set of characteristics for the world

at a given point in time

• Actions change the world from one state to another. S1

S2 S3 S4

Left Forward

Right

COSC 159 - Fundamentals of AI 5

Problem Solving Steps• Goal formulation

• Determine state or a set of states that will satisfy the agent’s goal

• Problem formulation• Determine actions and states to consider

• Search• Consider possible sequences of actions from current state• Return a solution, a sequence of actions that reaches a goal

state• In general, would like the best sequence of actions w.r.t. the

agent’s performance measure.

COSC 159 - Fundamentals of AI 6

Problem Solving Agent Program

Figure 3.1 page 61

COSC 159 - Fundamentals of AI 7

Example, Route Finding

• Consider a system like MapQuest• Provide instructions for driving from one

location to another

• Agent perspective• Agent simulates driving and identifies best

route given performance measure (time, distance, interstates, etc.)

COSC 159 - Fundamentals of AI 8

Example, Route Finding

Agent Type

Performance Measure

Environment Actuators Sensors

Route Finder

Shortest distance, shortest time, most interstate miles and shortest time, etc.

Map with roads, speed limits, etc.

(Simulated) accelerator, steering, etc.

(Simulated)

Speedometer, odometer, road identifier, etc.

COSC 159 - Fundamentals of AI 9

Example, Route Finding

COSC 159 - Fundamentals of AI 10

Example, Route Finding

• What are the states of the world?• The location of the agent, i.e. the Romanian

cities

• What goal does the agent formulate?• To be in Bucharest

• What is the performance measure?• Shortest distance

COSC 159 - Fundamentals of AI 11

Example, Route Finding

• What is the problem formulation?• States to consider are those between Arad and

Bucharest• Actions, go to a specific city

• Must be adjacent to city agent is located in.

• Search• Try all possible paths between Arad and

Bucharest• Select shortest path

COSC 159 - Fundamentals of AI 12

Formal Definition of Problems• An initial state that the agent starts in.• A description of possible actions for the agent from a

given state.• A successor function, which maps a state to a set of

(action, state) pairs

• A goal test, which determines if a state is a goal state• A path cost, which assigns numeric cost to a given

path.• Optimal solution is one that has the least path cost

amongst all solutions

*:)( statesactionsstatesxS

COSC 159 - Fundamentals of AI 13

Formal Definition for Example

• Initial state• In(Arad)

• Successor function• Maps state to adjacent cities

{<Go(Sibiu), In(Sibiu)>, <Go(Timisoara), In(Timisoara)>, <Go(Zerind), In(Zerind)>}

• Goal test• state is In(Bucharest)

• Path cost• Sum of distances along driving path

COSC 159 - Fundamentals of AI 14

Modeling Problems in Java

public interface Problem { /** The initial problem state */ public State getInitialState();

/** A function that returns a mapping of actions to * a state given a state. */ public Map successor(State state);

/** Return whether or not the state is a goal state */ public boolean goalState(State state);

/** Determine the cost of a path */ public Number pathCost(SearchNode node);}

COSC 159 - Fundamentals of AI 15

When is search a good idea?

• Static• World doesn’t change as agent thinks

• Observable• Assumes initial state is known

• Discrete• Must enumerate courses of action

• Deterministic• Must be able to predict the results of actions

COSC 159 - Fundamentals of AI 16

More Problems

• The 8-puzzle

COSC 159 - Fundamentals of AI 17

Problem Formulation

• State• Location of each tile

• Could be represented with an ordered list

0 1 2

3 4 5

6 7 8

Position numbers

[7,2,4,5,0,6,8,3,1]

Keep track of blank location

COSC 159 - Fundamentals of AI 18

Problem Formulation

• Initial state• Any state can be the initial state

• Any permutation of 0 through 8

• Goal test• State = [1,2,3,4,5,6,7,8,0]

• Path cost• Each step costs 1, so total number of steps

COSC 159 - Fundamentals of AI 19

Problem Formulation

• Successor function• View the blank as movable, so any state

reachable by moving blank left, right, up, or down

[7,2,4,5,0,6,8,3,1]

[7,2,4,0,5,6,8,3,1] [7,2,4,5,6,0,8,3,1] [7,0,4,5,2,6,8,3,1] [7,2,4,5,3,6,8,0,1]

L R U D

COSC 159 - Fundamentals of AI 20

Successor Functionb = position of blank;successors = {}; // empty setif (b-1 % 3 != 2) { create new state by swapping state[b] and state[b-1]; add new state to successors;}if (b+1 % 3 != 0) { create new state by swapping state[b] and state[b+1]; add new state to successors;}if (b-3 >= 0) { create new state by swapping state[b] and state[b-3]; add new state to successors;}if (b+3 < 9) { create new state by swapping state[b] and state[b+3]; add new state to successors;}return successors;

COSC 159 - Fundamentals of AI 21

More Problems

• The phasor measurement unit (PMU) problem

• Consider power system graphs (PSGs)• Busses (nodes)• Lines (edges)

• PMUs observe characteristics of a PSG for monitoring

• Place the fewest number of PMUs on a PSG

COSC 159 - Fundamentals of AI 22

How A PMU Observes a PSG

• Any bus with a PMU is observed

• Any line incident with a bus containing a PMU is observed

• Any bus incident with an observed line is observed

• Any line between two observed busses is observed

• If all the lines incident with an observed bus are observed, save one, then all of the lines incident to that bus are observed.

These all follow from physical laws.

COSC 159 - Fundamentals of AI 23

Problem Formulation

• State• Locations of PMUs and which parts of PSG are observed

• Initial state• No PMUs and nothing observed

• Successor function• States obtained by placing a PMU on a node without a PMU

• Goal test• The entire PSG is observed

• Path cost• The number of PMUs placed

COSC 159 - Fundamentals of AI 24

Searching

• Problem solving involves searching through the state space

• The state space is organized with a state tree• Start with initial state, then apply successor

function repeatedly• More generally, we have a search graph, as states

can be reached by different paths– 8-puzzle: Consider moving blank left and then right

COSC 159 - Fundamentals of AI 25

Search Nodes

• Nodes are a data structure with 5 components

Action: rightDepth: 6Path-Cost: 6

Parent-Node

State

COSC 159 - Fundamentals of AI 26

Search Nodes

public class SearchNode { private SearchNode parent; private State state; private Action action; private int depth; private Number cost;

// Include any necessary constructors, access, setter // and other methods needed by the node class.}

COSC 159 - Fundamentals of AI 27

Tree Search

Expand

Goal test?

Fringe - set of states to be expanded

COSC 159 - Fundamentals of AI 28

Tree Search

• A search strategy determines the order in which nodes are expanded

Figure 3.8 page 71

COSC 159 - Fundamentals of AI 29

Organizing the Fringe

• The fringe is implemented using a queue• Actually a priority queue

• Operations

MakeQueue(element, …); // create queue containing elementsEmpty?(queue); // return true if queue is emptyFirst(queue); // return first element, but leave queuedRemoveFirst(queue); // return first element, dequeueInsert(element,queue); // insert in ordered location into queueInsertAll(elements, queue); // insert multiple elements.

COSC 159 - Fundamentals of AI 30

Tree Search

Figure 3.9 page 72

COSC 159 - Fundamentals of AI 31

Tree Search in Javapublic class TreeSearch { private PriorityQueue fringe; private Problem problem;

/** * Construct a tree search object for the specified problem. */ public TreeSearch(Problem problem, PriorityQueue fringe) { this.problem = problem; this.fringe = fringe; /* Create the root of the search tree */ SearchNode root = new SearchNode(null, // parent problem.getInitialState(), // state null, // action 0, // depth null); // cost fringe.add(root); }

COSC 159 - Fundamentals of AI 32

Tree Search in Java /** * Carry out the search, returning a collection of * actions to perform. */ public Collection execute() { while (!fringe.isEmpty()) { SearchNode node = fringe.removeFirst(); if (problem.goalState(node)) { return solution(node); } expand(node); } return null; // throw exception instead? }

COSC 159 - Fundamentals of AI 33

Tree Search in Java /** * Expand the current search node. */ public void expand(SearchNode node) { Map successors; successors = problem.successor(node.getState()); for (Iterator iter = successors.keySet().iterator(); iter.hasNext(); ) { Action act = (Action) iter.next(); State state = (State) successors.get(act); SearchNode newNode = new Node(node, state, act, node.getDepth() + 1); problem.pathCost(newNode); fringe.add(newNode); } }

COSC 159 - Fundamentals of AI 34

Tree Search in Java /** * Return a sequence of actions for the agent to take. */ public Collection solution(SearchNode node) { LinkedList actions = new LinkedList();

while (node != null) { actions.addFirst(node.getAction()); node = node.getParent(); } return actions; }}

COSC 159 - Fundamentals of AI 35

Measuring Problem Solving Performance

• Completeness• Is the algorithm guaranteed to find a solution?

• Optimality• Does the strategy find an optimal solution?

• Time complexity• How long does it take to find a solution?

• Space complexity• How much memory is needed to perform the search?

COSC 159 - Fundamentals of AI 36

Complexity and Big-Oh

• Read Appendix A.• T(n) is the complexity of an algorithm.• O(f(n)) is defined by

T(n) is O(f(n)) if T(n) <= kf(n) for some k, for all n > n0

• What this means is that the growth in time (or space) complexity is bounded above by some constant times f(n).

COSC 159 - Fundamentals of AI 37

Complexity and Big-Oh

• Example• If T(n) = 2n + 2, then T(n) is O(n) using k = 3

and n0 = 2.

• Use O() to say something about long term behavior of algorithms• O(n) is always better than O(n2) as n

approaches infinity.

COSC 159 - Fundamentals of AI 38

Complexity and Search

• What is n for search strategies?• Usually a measure of the search tree/graph size

• Search tree size is measured by• b, the branching factor, the maximum number

of successors of any node

• d, the depth of the shallowest goal node

• m, the maximum length of any path

COSC 159 - Fundamentals of AI 39

Complexity

• Time complexity is in terms of the number of nodes expanded

• Space complexity is in terms of the maximum number of nodes stored in memory

COSC 159 - Fundamentals of AI 40

Cost

• Search cost, which is generally time complexity but may also include space complexity

• Total cost, which is the search cost plus the path cost of the solution

COSC 159 - Fundamentals of AI 41

Search Strategy

• An uninformed strategy is given no information other than the problem definition.• The algorithms are given no insight into the structure of

the problem

• All non-goal states are equal

• Informed strategies take advantage of additional a priori knowledge of the problem• Identify more promising non-goal states.

• Heuristics, pruning, …

COSC 159 - Fundamentals of AI 42

Uninformed Search Strategies

• Breadth-first search• Uniform-cost search

• Depth-first search

• Depth-limited search

• Iterative deepening search

• Bidirectional search

COSC 159 - Fundamentals of AI 43

Breadth First Search

• Fringe is organized in FIFO queue

1

2 3 4

5 6

1

2

COSC 159 - Fundamentals of AI 44

Breadth First Search

• Complete?• Yes

• Optimal?• Finds shallowest goal node. Only if step costs are all

the same

• Time complexity?• O(bd+1)

• Space complexity?• O(bd+1)

COSC 159 - Fundamentals of AI 45

Uniform Cost Search

• Order fringe by increasing path cost

A

S 140 T 118 Z 75

O 141 A 150

A

Z 75

COSC 159 - Fundamentals of AI 46

Uniform Cost Search

• Complete?• Yes, if each step costs is at least e > 0

• Optimal?• Yes, as above

• Time complexity?• O(bc), where C is cost of optimal solution c = ceil(C/e)• c could be larger than d!

• Space complexity?• O(bc)

COSC 159 - Fundamentals of AI 47

Depth First Search

• Fringe organized in descending order of depth (i.e. deepest node first)

A

S 140 T 118 Z 75

R 220 F 239

A

S140

COSC 159 - Fundamentals of AI 48

Depth First Search• Complete?

• No.

• Optimal?• No.

• Time complexity?• O(bm)

• Space complexity?• O(bm)

COSC 159 - Fundamentals of AI 49

Depth First Search

• Can reduce memory using backtracking• Store only one successor at a time

• Go back if path fails and generate next successor

• Feasible if actions can be easily undone

• Good for problems with large state descriptions.

COSC 159 - Fundamentals of AI 50

Depth-limited search• Organize as depth first, but limit the depth in the tree

searched to level l• Depth-first is when l is infinity

• Complete?• Only if l >= d

• Optimal• Only if l = d

• Time• O(bl)

• Space• O(bl)

COSC 159 - Fundamentals of AI 51

Iterative Deepening Search

• Execute depth limited search for l = 1, 2, 3, …

• Complete?• Yes

• Optimal• Yes, if step costs the same

• Time Complexity?

COSC 159 - Fundamentals of AI 52

Time Complexity of IDS

• Derived on the board.• N(IDS) = (d)b + (d-1)b2 + …+(1)bd

• N(BFS) = b + b2 + … + bd + (bd+1-b)

• Time complexity• O(bd)

• Space complexity• O(bd)

COSC 159 - Fundamentals of AI 53

Bidirectional Search

• Search both forward and backward• Need to have a predecessor function

• Complete• Yes, if BFS used

• Optimal• Yes, with identical step costs and BFS used

• Time• O(bd/2)

• Space• O(bd/2)

COSC 159 - Fundamentals of AI 54

Repeated States

• Repeated states can turn a linear problem into an exponential one!

COSC 159 - Fundamentals of AI 55

Graph Search

• States are open (not visited) or closed (visited)

COSC 159 - Fundamentals of AI 56

Graph Search

• Uses more memory• Proportional to size of state space

• DFS and IDS no longer linear space

• Implement closed list with hash table

• Could lose optimality• Could arrive at a state by a more expensive path

• Particularly a problem with IDS

COSC 159 - Fundamentals of AI 57

Partial Information

• Sensorless problems• Agent doesn’t know initial state

• Each action could lead to several possible successor states

• Contingency problems• Partially observable environments

• Nondeterministic actions

• Exploration• States and actions are unknown

COSC 159 - Fundamentals of AI 58

Sensorless Problems

• The initial state of the agent is completely unknown• One of several possible states

• Agent knows available actions, but can’t see results.

• Can we still design an agent that performs rationally?

COSC 159 - Fundamentals of AI 59

View sets of statesas a single worldstate.

Senseless Monkey

Down

COSC 159 - Fundamentals of AI 60

Exercise (in class)

• Draw the complete state diagram for the senseless monkey. Ignore sleep action.

COSC 159 - Fundamentals of AI 61

Answer

D U

E

U

E

EE

E

U

U

U

U

D

D

D

D

D

E

U,ED,ED

D

D,EU,E

U

COSC 159 - Fundamentals of AI 62

Sensorless Problems

• A set of states in the sensorless problem is a goal state if every state in the set is a goal state

• In general, if the original state space has S states, the sensorless state space has 2S possible states (power set). Not all may be reachable though.

COSC 159 - Fundamentals of AI 63

Contingency Problems

• Information is gained from sensors after acting

• Example: Lawn environment. Mower doesn’t know locations of all obstacles.

• Formulate action sequences like• [Counter, if (Obstacle) then Counter else

Forward, …]• Handled with planning (Chapter 12).

COSC 159 - Fundamentals of AI 64

Summary

• Represent problems in search spaces• Nodes represent physical states• Edges represent actions taken

• Problems have 4 components• Initial state• Description of actions (successor function)• Goal Test• Path cost (e.g., via step cost per action)

COSC 159 - Fundamentals of AI 65

Summary

• Search strategies defined by the order fringe nodes are expanded• Priority queue

• In general, iterative deepening is best approach• However, should study characteristics of each problem

to select best strategy

• Sensorless problems can be solved by mapping to sets of physical states

Recommended