using search in problem solving part ii. 2 search and ai generate and test generate and test...

31
Using Search in Using Search in Problem Solving Problem Solving Part II Part II

Post on 19-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Using Search in Using Search in Problem SolvingProblem Solving

Part IIPart II

2

Search and AISearch and AI

Generate and TestGenerate and Test Planning techniquesPlanning techniques Mini Max algorithmMini Max algorithm Constraint Satisfaction searchConstraint Satisfaction search

3

The Search SpaceThe Search Space

The search space is known in The search space is known in advance e.g. finding a map routeadvance e.g. finding a map route

The search space is created The search space is created while solving the problem - e.g. while solving the problem - e.g. game playinggame playing

4

Generate and Test Generate and Test ApproachApproach

Search tree - built during the search process

Root - corresponds to the initial state

Nodes: correspond to intermediate states (two different nodes may correspond to one and the same state)

Links - correspond to the operators applied to move from one state to the next state

Node description

5

Node DescriptionNode Description

The corresponding stateThe corresponding state The parent nodeThe parent node The operator applied The operator applied The length of the path from the root to The length of the path from the root to

that nodethat node The cost of the pathThe cost of the path

6

Node GenerationNode Generation

To expand a node means to apply operators to the node and obtain next nodes in the tree i.e. to generate its successors.

Successor nodes: obtained from the current node by applying the operators

7

AlgorithmAlgorithmStore root (initial state) in stack/queue

While there are nodes in stack/queue DO:

Take a node

While there are applicable operators DO:

Generate next state/node (by an operator)

If goal state is reached - stop

If the state is not repeated - add into stack/queue

To get the solution: Print the path from the root to the found goal state

8

Example - the Jugs Example - the Jugs problemproblem

We have to decide:

• representation of the problem state, initial and final states

• representation of the actions available in the problem, in terms of how they change the problem state.

• what would be the cost of the solution

Path costSearch cost

9

Problem representationProblem representation

Problem state: a pair of numbers (X,Y): X - water in jar 1 called A, Y - water in jar 2, called B.

Initial state: (0,0)

Final state: (2, _ ) here "_" means "any quantity"

10

Operations for the Jugs Operations for the Jugs problemproblem

Operators: PreconditionsEffects

O1. Fill A X < 4 (4, Y)

O2. Fill B Y < 3 (X, 3)

O3. Empty A X > 0 (0, Y)

O4. Empty B Y > 0 (X, 0)

O5. Pour A into B X > 3 - Y ( X + Y - 3 , 3)

X 3 - Y ( 0, X + Y)

O6. Pour B into A Y > 4 - X (4, X + Y - 4)

Y 4 - X ( X + Y, 0)

11

Search tree built in a Search tree built in a breadth-first mannerbreadth-first manner

0,0

3,0 0,4

3,4 0,3 3,4 3,1

O1

O2

O2

O5

O1

O6

12

ComparisonComparison

Breadth-first finds the best solution but requires more memory In the Jugs problem:

63 nodes generated, solution in 6 steps

Depth first uses less memory but the solution maybe not the optimal one. In the Jugs problem:

23 nodes generated, solution in 8 steps

13

The A* AlgorithmThe A* Algorithm

To use A* with generate and

test approach, we need a

priority queue and an

evaluation function.The basic algorithm is the same

except that the nodes are evaluated

Example: The Eight Puzzle problem

14

Planning TechniquesPlanning Techniques

Means-Ends analysisInitial state, Goal state, intermediate states.

Operators to change the states.Each operator:

PreconditionsEffects

Go backwards : from the goal to the initial state.

15

Put the goal state in a list of subgoals

While the list is not empty, doTake a subgoal.

Is there an operation that has as an effect this goal state?

If no - the problem is not solvable.If yes - put the operation in the plan.

Are the preconditions fulfilled?If yes - we have constructed the planIf no - put the preconditions in the list of subgoals

Basic AlgorithmBasic Algorithm

16

More Complex ProblemsMore Complex Problems

Goal state - a set of sub-states

Each sub-state is a goal.

To achieve the target state we have to achieve each goal in the Target state.

(The algorithm is modified as described on p. 89.)

17

Extensions of the Basic Extensions of the Basic MethodMethod

Planning with goal protectionPlanning with goal protection Nonlinear planningNonlinear planning Hierarchical planningHierarchical planning Reactive planningReactive planning

18

Game PlayingGame Playing

Special feature of games: there is an opponent that tries to destroy our plans.

The search tree (game tree) has to reflect the possible moves of both players

19

Player1

Player2

Player1

20

MiniMax procedureMiniMax procedureBuild the search tree. Assign scores to the leavesAssign scores to intermediate nodes

If node is a leaf of the search tree - return its score Else:

If it is a maximizing node return maximum of scores of the successor nodes

Else - return minimum of scores of the successor nodes

After that, choose nodes with highest score

21

Alpha-Beta PruningAlpha-Beta Pruning

At maximizing level the score is at least the score of any successor.

At minimizing level the score is at most the score of any successor.

22

A:10

B:10 C: 0

E:10 F:10 G: 0 H:10

I:10 J: 0 K:10 L:-10

M:-10 N:0 O:10 P:-10

Maximizing score

Minimizing score

P1's move

P2's move

P1's minimax tree

23

Alpha-Beta Cutoff function MINIMAX-AB(N, A, B) is begin Set Alpha value of N to A and Beta value of N to B; if N is a leaf then return the estimated score of this leaf else For each successor Ni of N loop

Let Val be MINIMAX-AB(Ni, Alpha of N, Beta of N); if N is a Min node then

Set Beta value of N to Min { Beta value of N, Val }; if Beta value of N <= Alpha value of N then

exit loop; Return Beta value of N; else

Set Alpha value of N to Max{Alpha value of N, Val}; if Alpha value of N >= Beta value of N then exit loop; Return Alpha value of N;

end MINIMAX-AB;

24

A

C

B

D

E

10

F

11

G

H I

J

K

L M

N

O P

Q

R

S

T U

V

W X

Y

Z

9 12 14 15 13 14 5 2 4 1 3 24 10 18

Circles are maximizing nodes

The shadowed nodes are cut off

25

Constraint Satisfaction Constraint Satisfaction SearchSearch

State description: a set of variables. A set of constraints on the assigned values of

variables.

Each variable may be assigned a value within its domain, and the value must not violate the constraints.

Initial state - the variables do not have values Goal state: Variables have values and the

constraints are obeyed.

26

ExampleExample

WEEKWEEK

LOANLOAN WEEKWEEK

++ LOAN +LOAN + WEEKWEEK

LOANLOAN WEEKWEEK

------------ -------------------------- --------------

DEBT MONTHDEBT MONTH

Constraints for the second problem:

• All letters should correspond to different digits

• M not equal to zero, as a starting digit in a number

• W not equal to zero as a starting digit in a number

• 4 * K = n1 * 10 + H , where n1 can be

0, 1, 2, 3. Hence K cannot be 0. • 4 * E = n2 * 10 + n1 + T • 4 * E = n3 * 10 + n2 + N • 4 * W = n4 * 10 + n3 + O • M = n4

27

28

29

SummarySummary Search SpaceSearch Space

Initial state Initial state

Goal state Goal state

intermediate states intermediate states

operatorsoperators

costcost

30

SummarySummary

Search methodsSearch methods Exhaustive (depth-first, breadth-Exhaustive (depth-first, breadth-

first)first)

InformativeInformative

Hill climbingHill climbing

Best-firstBest-first

A*A*

31

SummarySummary

Problem solving methods Problem solving methods using searchusing search Generate and TestGenerate and Test Means-Ends Analysis (Planning)Means-Ends Analysis (Planning) Mini-Max with Alpha-Beta Cut Off Mini-Max with Alpha-Beta Cut Off

(2-player determined games) (2-player determined games) Constraint satisfaction searchConstraint satisfaction search