artificial intelligence · karim bouzoubaa artificial intelligence 9 production system components !...

63
Artificial Intelligence Lecture 3 Karim Bouzoubaa Problem Resolution

Upload: others

Post on 11-Jun-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Artificial Intelligence

Lecture 3 Karim Bouzoubaa

Problem Resolution

Page 2: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 2

¢  A problem is most often posed in the form of an informal statement, in the natural language form. Before solving it, it is necessary then, at least for a resolution by the machine, to carry out a first step of specifying the problem

¢  To solve pbs, AI proposes a different approach from the classical

approach proposed by computer science

¢  Classically: the computer manipulates digital information executes statements

¢  pb à alg (statements) à solution Human being machine

analyzes executes (fast and accurate)

Introduction

Page 3: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 3

¢  In an AI vision: l  The computer manipulates symbolic information (K) l  Reasons on K (resolves ; builds the sol)

¢  Pb statement + reasoning possibility à sol

K elts resolution strategy computer solves ¢  Summary:

l  No statements: say what (declarative reasoning) and not how l  Bug of K

Introduction

Page 4: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 4

Space and Memory Problem

¢  Chess game ¢  Formal description of the pb:

l  Initial config.: matrix 8 x 8 positions, each position indicates a symbol l  Goal: king fails l  Legal movements: set of rules à l  10120 rules: space and memory pb

Page 5: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 5

Example: cans

Its representation l  Possible states (V4, V3) such that

•  V4= 0, 1, 2, 3 or 4 •  V3 = 0, 1, 2 or 3

l  initial state (0,0)

l  final state (2,n)

problem Two cans: can4 and can3, of respective capacity of 4 and 3 l i t e r s . N o n e o f t h e m i s graduated. A pump is available to fill the cans. How can you get exactly 2 liters in can4?

4 L 3 L 2 L

Page 6: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 6

Example: cans – operators K elements

Condition explanation operator N°

12

11

10

9

8

7

6

5

4

3

2

1

Empty 2 water liters from can4 in can3 (V4,V3)→(0,V3)

Empty 2 water liters from can3 in can4 (V4,V3)→(2,0)

If V4+V3≤3 and V4 > 0 Empty all the water of can3 in can4 (V4,V3)→(0,V4+V3)

If V4+V3≤4 and V3 > 0 Empty all the water of can4 in can3 (V4,V3)→(V4+V3,0)

If V4+V3 ≥ 3 and V4 > 0 Empty water from can3 in can4 till can3 is full (V4,V3)→(V4-(3-V3), 3)

If V4+V3 ≥ 4 and V3 > 0 Empty water from can4 in can3 till can3 is full (V4,V3)→(4,V3-(4-V4) )

If V3 > 0 Empty can3 (V4,V3)→(V4,0)

If V4 > 0 Empty can4 (V4,V3)→(0,V3)

If V3 > 0 Withdraw quantity q from can3 (V4,V3)→(V4,V3-q)

If V4 > 0 Withdraw quantity q from can4 (V4,V3)→(V4-q,V3)

If V3 < 3 Fill can3 (V4,V3)→(V4,3)

If V4 < 4 Fill can4 (V4,V3)→(4,V3)

Page 7: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 7

Example: cans – One solution

Final state

Initial state

Remark

(V4,V3)→(2,0)

(V4,V3)→(0,V3)

(V4,V3)→(4,V3-(4-V4) )

(V4,V3)→(V4,3)

(V4,V3)→(V4+V3,0)

(V4,V3)→(V4,3)

Rule

0 2

11 2 0

5 2 4

7 3 3

2 0 3

9 3 0

2 0 0

N° Rule L can3 L can4

K elements are enough à implement a implémenter a resolution strategy

Page 8: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 8

Problem resolution

¢ Algorithmic Approach l Execution system of the solution l Deterministic machine with finite states l At every time during execution, the next

statement to execute is known ¢ Production system approach (PS)

l Resolution system of problems l Search space (state space, goals ..) l Search strategy ; space exploration

Page 9: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 9

Production system components

¢  A search space l  Space of possible states l  Representation of every state (couple, plan, matrix, etc.)

¢  A set of production rules state1 → state2 l  A rule is a behaviour unit l  It allows to transform the state of the space to advance in the

searching process

¢  A control mechanism that selects the rule to trigger among the applicable rules l  A rule is applicable if its head unifies with the current state l  Many rules may be applicable at a given time l  The control mechanism corresponds to the search strategy

Page 10: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 10

Example: cans

(0,0)

(4,3) (4,3)

(4,0)

(0,0) (0,0) (1,3) (0,0) (4-q,0)

(0,3)

(0,3-q) (3,0)

1 2

2 3 5 8 12 1 4 6 9

(2,X)

12

11

10

9

8

7

6

5

4

3

2

1

(V4,V3)→(0,V3)

(V4,V3)→(2,0)

If V4+V3≤3 et V4 > 0 (V4,V3)→(0,V4+V3)

If V4+V3≤4 et V3 > 0 (V4,V3)→(V4+V3,0)

If V4+V3 ≥ 3 et V4 > 0 (V4,V3)→(V4-(3-V3), 3)

If V4+V3 ≥ 4 et V3 > 0 (V4,V3)→(4,V3-(4-V4) )

If V3 > 0 (V4,V3)→(V4,0)

If V4 > 0 (V4,V3)→(0,V3)

If V3 > 0 (V4,V3)→(V4,V3-q)

If V4 > 0 (V4,V3)→(V4-q,V3)

If V3 < 3 (V4,V3)→(V4,3)

If V4 < 4 (V4,V3)→(4,V3)

Page 11: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 11

Prolog Program

¢  A space search l  Every state: cans(V4, V3) l  Possible states: cans(V4, V3) such that V4= 0, 1, 2, 3 or 4 and V3 = 0, 1, 2 or 3

¢  Initial state : cans(0,0) ¢  Final state : cans(2,n)

¢  A set of production rules state1 à state2 update(_state1, _Mvt, _state2)

update(cans(_V4,_V3), empty(can4), cans(0,_V3)).

update(cans(_V4,_V3), fill(can3), cans(_V4,_C3)) :-

capacity(can3,_C3).

¢  A control mechanism selects a rule to trigger among the appliable rules

Page 12: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 12

Control Mechanism

inDepth_resolution(_State, _History, [_Mvt | _Mvts]) :-

apply_operator(_State,_Mvt),

update(_State, _Mvt, _State1),

legal_state(_State1),

not(member(_State1,_History)),

inDepth_resolution(_State1, [_State1|_History], _Mvts).

Page 13: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 13

apply_operator(cans(_V4,_V3), fill(can4)) :-

less(_V4, 4, true). //_V4 < 4.

apply_operator(cans(_V4,_V3), fill(can3)) :-

less(_V3,3, true). //_V3 < 3.

apply_operator(cans(_V4,_V3), empty(can4)) :- greater(_V4,0,true). //_V4 > 0.

apply_operator(cans(_V4,_V3), empty(can3)) :- greater(_V3,0,true). //_V3 > 0.

apply_operator(cans(_V4,_V3), transfill(can3,can4)) :-

greater(_V3, 0, true), //_V3 > 0,

_V5 is _V4 + _V3,

greater_equal(_V5, 4, true). //_V4 + _V3 >= 4.

apply_operator(cans(_V4,_V3), transfill(can4,can3)) :-

greater(_V4, 0, true), //_V4 > 0,

_V5 is _V4 + _V3,

greater_equal(_V5, 3, true). //_V4 + _V3 >= 3.

apply_operator(cans(_V4,_V3), transfill_all(can3,can4)) :-

greater(_V3, 0, true), //_V3 > 0,

_V5 is _V4 + _V3,

less_equal(_V5, 4, true). //_V4 + _V3 <= 4.

apply_operator(cans(_V4,_V3), transfill_all(can4,can3)) :-

greater(_V4, 0, true), //_V4 > 0,

_V5 is _V4 + _V3,

less_equal(_V5, 3, true). //_V4 + _V3 <= 3.

12

11

10

9

8

7

6

5

4

3

2

1

(V4,V3)→(0,V3)

(V4,V3)→(2,0)

Si V4+V3≤3 et V4 > 0 (V4,V3)→(0,V4+V3)

Si V4+V3≤4 et V3 > 0 (V4,V3)→(V4+V3,0)

Si V4+V3 ≥ 3 et V4 > 0 (V4,V3)→(V4-(3-V3), 3)

Si V4+V3 ≥ 4 et V3 > 0 (V4,V3)→(4,V3-(4-V4) )

Si V3 > 0 (V4,V3)→(V4,0)

Si V4 > 0 (V4,V3)→(0,V3)

Si V3 > 0 (V4,V3)→(V4,V3-q)

Si V4 > 0 (V4,V3)→(V4-q,V3)

Si V3 < 3 (V4,V3)→(V4,3)

Si V4 < 4 (V4,V3)→(4,V3)

Production rules

Page 14: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 14

12

11

10

9

8

7

6

5

4

3

2

1

(V4,V3)→(0,V3)

(V4,V3)→(2,0)

Si V4+V3≤3 et V4 > 0 (V4,V3)→(0,V4+V3)

Si V4+V3≤4 et V3 > 0 (V4,V3)→(V4+V3,0)

Si V4+V3 ≥ 3 et V4 > 0 (V4,V3)→(V4-(3-V3), 3)

Si V4+V3 ≥ 4 et V3 > 0 (V4,V3)→(4,V3-(4-V4) )

Si V3 > 0 (V4,V3)→(V4,0)

Si V4 > 0 (V4,V3)→(0,V3)

Si V3 > 0 (V4,V3)→(V4,V3-q)

Si V4 > 0 (V4,V3)→(V4-q,V3)

Si V3 < 3 (V4,V3)→(V4,3)

Si V4 < 4 (V4,V3)→(4,V3)

Production rules

update(cans(_V4,_V3), empty(can4), cans(0,_V3)). update(cans(_V4,_V3), empty(can3), cans(_V4,0)).

update(cans(_V4,_V3), fill(can4), cans(_C4,_V3)) :-

capacity(can4,_C4). update(cans(_V4,_V3), fill(can3),

cans(_V4,_C3)) :- capacity(can3,_C3).

update(cans(_V4,_V3), transfill_all(can3,can4), cans(_W4,_W3)) :- capacity(can4,_C4),

_Liquid_quantity is _V4 + _V3,

_Extra_Liquid is _Liquid_quantity - _C4, adjust(_Liquid_Quantity, _Extra_Liquid, _W4, _W3).

update(cans(_V4,_V3), transfill(can3,can4), cans(_W4,_W3)) :- capacity(can4, _C4),

_Liquid_quantity is _V4 + _V3,

_Extra_Liquid is _Liquid_quantity - _C4,

adjust(_Liquid_Quantity, _Extra_Liquid, _W4, _W3).

update(cans(_V4,_V3), transfill(can4,can3), cans(_W4,_W3)) :- capacity(can3,_C3),

_Liquid_quantity is _V4 + _V3, _Extra_Liquid is _Liquid_quantity - _C3,

adjust(_Liquid_Quantity, _Extra_Liquid, _W3, _W4).

Page 15: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 15

Solution

Page 16: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 16

3 5 8 1 • 6 2 4 7

3 5 8 • 1 6 2 4 7

3 5 8 1 6 • 2 4 7

3 • 8 1 5 6 2 4 7

3 5 8 1 4 6 2 • 7

• 3 8 1 5 6 2 4 7

3 8 • 1 5 6 2 4 7

1 2 3 8 • 4 7 6 5

Another example: The tease

Page 17: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 17

Basic Algorithm

¢  Initialization (current state = initial state)

¢  While (current state ≠ final state) l  Step 1: Find a rule R that can be applied to a current state

l  Step 2: new current state = application of R to current state

¢ Search strategy (Step 1): l Search a rule among the possible ones l Keep track of sequences of rules already tried

Page 18: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 18

Search Strategies

¢  Systematic Non-informed search: Find a path, when exploring space, that leads from the initial state to the final state. l  In-depth and breadthwise

¢  Search with information about node l  Hill-Climbing, Best-First and Beam

¢  Search with information about nodes and edges

l  A*

Page 19: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 19

Depth Systematic Search (depth-first strategy)

E1

E2 E3 E4

E5 E6

E7 E8 E9 Memory is managed as a stack

Page 20: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 20

Depth search avoiding cycles

¢  Add a mechanism to detect cycles

à A node already present in the path from initial to current node cannot be considered a second time

E1

E2 E3 E4

E5 E6

E7 E8 E9

E5 E10

Page 21: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 21

Limited depth search

¢  Pb : The program can be lost in an infinite depth search à infinite state space

¢  Solution : To avoid infinite and non-cyclical branches, we limit the depth of the search: l  depthfirst2(Node, Solution, Maxdepth).

à The search can not go beyond the depth limit = Decrease Maxdepth during each recursive call

Page 22: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 22

Breadth Systematic Search (breath-first strategy)

E1

E2 E3 E4

E5 E6

E7 E8 E9 Memory is managed as a list

Page 23: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 23

¢  Complexity ¢  B : average number of branches en moyenne per node ¢  n : the depth of the tree

Time Space

Depth O(Bn) O(B*n)

Breath O(Bn) O(Bn)

Comparison of the two systematic methods

¢  Problems ¢  Depth-first: pay attention to endless depth ¢  Breath-first: pay attention to space

Page 24: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 24

Heuristic method

¢  Systematic search l  Explore all paths l  Eventually find a solution l  Eventually may take a long time

¢  Heuristic search l  Use of heuristics: choose paths to explore in priority l  Heuristic criterion: associate with each state a certain estimated value (its

approximation of the goal) •  Ex: h(n) = the distance between city n and destination city

l  For each problem, different estimate l  Heuristic search: first explore the successor state representing the lowest

cost l  Efficiency of heuristic research depends on how the knowledge of the field is

exploited

Page 25: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 25

Hill climbing

¢  Hill climbing Alg 1.  Departure state = current state 2.  If current state = final state, stop 3.  Generate children of current state 4.  Evaluate children and select the best one 5.  If the best is better than current state, advance

to that child that becomes the current state, go to step 2

6.  Else stop

Page 26: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 26

Hill Climbing

2 8 3 1 6 4 7 • 5

2 8 3 1 6 4 • 7 5

2 8 3 1 • 4 7 6 5

2 8 3 1 6 4 7 5 •

1 2 3 8 • 4 7 6 5

6

6 3 6

0

Page 27: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 27

Hill Climbing Variants

¢  Problem with simple Hill climbing l  The algorithm considers only one path

à  when the successors are not better than this state, the algorithm stops

¢  Hill climbing v2 l  Choose the state produced by the best move as next state l  Alg

1.  Departure State de départ = current State

2.  If current State = final State, stop

3.  Generate children of current State

4.  Evaluate childre and select the best

5.  Move to that child which becomes the current State, goto step 2

6.  else, stop

Page 28: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 28

Hill Climbing v2 applied on the puzzle

¢  Discussion : l  Depth first

l  Breadth first

l  Hill climbing

l  Hill climbing steep

Page 29: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 29

Hill Climbing summary: Principle of a local search

¢  A local search just keeps some states visited in memory:

l  The simplest case is hill-climbing which just keeps a state (the current state) and iteratively improves it until converging to a solution

¢  In general, there is an objective function to optimize (maximize or minimize)

l  In the case of hill-climbing, it determines the successor state

¢  In general, a local search does not guarantee an optimal solution. Its attractiveness is above all its ability to find an acceptable solution quickly.

Page 30: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 30

Hill Climbing summary

¢  Hill Climbing Problem l  Local Maximum l  The alg can end without finding a goal state but finding a

state from which one can not generate a better state

¢  Hill Climbing l  Local technique, i.e. considers only the immediate

consequences of a state l  No possibility to consider other paths (illustrate on

previous image)

¢  Use of the Best First method

Page 31: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 31

Best First

¢  Best First l  Combination of depth and breath search l  Follow one path at a time but change it as soon as a more

promising path appears

¢  Method l  select the most promising node of all generated nodes l  Perform the expansion of the node using the applicable rules l  Among the successors, if one of them is a solution, stop l  Otherwise, all new nodes are added to all the nodes already

generated l  Again the most promising node becomes the current node

Page 32: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 32

Best First Algorithm

¢  Open contains nodes not yet processed, ie at the boundary of the part of the graph explored so far.

¢  Closed contains the nodes already processed, ie inside the boundary delimited by open.

open = initial state while open != null do

1. Pick the best node on open 2. Create open's successors 3. For each successor do:

a. If it has not been generated before: evaluate it, add it to OPEN, and record its parent

b. Otherwise: change the parent if this new path is better than previous one.

done

Page 33: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 33

A

C B D

E F (4) (6)

(3) (5) A

C B D (1) (3) (5)

A

A

C B D

E F G H

I J (2) (1)

(6) (5) (6)

(5) A

C B D

E F G H (4) (6) (5) (6)

(5)

Example Best First

Page 34: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 34

Beam search

¢ Beam search l  Is an optimization of best-first search l Reduces memory load l  In the beam search method, only a certain

number of solutions are kept as candidates

Page 35: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 35

Generic definition of f

¢  In practice we do not know the distance to the goal! That's what we're looking for

¢  On the other hand, we know the optimal distance in the explored part between the root and a node already explored

¢  It is convenient to separate f(n) into two parts:

l  g(n): the real cost of the optimal path from the root to n in the part already explored

l  h(n): estimated cost of the rest of the path from n to the goal. h(n) is called the heuristic function.

Page 36: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 36

Generic definition of f

¢  Depending on the weight we want to give to either party, we define f as follows:

f(n) = (1-w).g(n) + w.h(n)

where w is a real number greater than or equal to 0 and less than or equal to 1.

¢  Depending on the values given to w, one obtains classical search algorithms:

l  Dijkstra : w = 0 (f(n) = g(n) )

l  Best-first search : w = 1 (f(n) = h(n) )

l  A* : w = 0.5 (f(n) = g(n) + h(n) ) à Information about nodes and edges

Page 37: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 37

Example : A*

v0

v3 v2 v1

v4

v6

v5

9

2

3 2

2 5

0

2 3

1

1 7 2

4 4

4

h(n0)

c(n0,n3)

Distance between cities: v0: departure v6: destination h: distance C: actual distance

1. (v0, 9, void)

Content of closed at the end:

(v4,6), (v3,7), (v2,5), (v1,5), (v0,9)

Exploration:

2. (v1,5,v0) (v2,6,v0), (v3,7,v0) 3. (v2,6,v0) (v3,7,v0), (v5,12,v1) 4. (v3,7,v0),(v4,9,v2),(v5,12,v1) 5. (v2,5,v3),(v4,6,v3),(v5,12,v1) 6. (v4,6,v3),(v5,12,v1) 7. (v6,7,v4), (v5,12,v1) 8. Solution: v0,v3,v4,v6

Page 38: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 38

A* Algorithm : Example

¢  Illustrer the example

l  f(a) = 1.5 + 4

l  f(d) = 2 + 4.5

l  …

Page 39: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 39

Farmer Problem

Page 40: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 40

Farmer Problem

Page 41: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 41

Example: Towers of Hanoi

Page 42: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 42

Demo about searching methods

file:///Users/karimbouzoubaa/Data/Courses/AI/demos%20for%20AI/demos/search/search.html http://bgrins.github.io/javascript-astar/demo/ https://qiao.github.io/PathFinding.js/visual/ Solution with netbeans (Hipster library)

Page 43: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Hipster : Java Library u   Java  open  source  library    

v   for  problem  resolu3on  

v   Hipster-­‐core:  implemen3ng  many  search  algorithms  

v   Hipster  examples:  illustra3ng  the  exploita3on  of  Hipster-­‐core  such  as:   SimpleEightPuzzle,   Directedgraphsearch,   MazeShortestPath,  EightQueensProblem,  etc.    

Page 44: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Hipster core   Non  informed  search  

 Depth  First  Search    

 Breadth  First  Search    

 Bellman-­‐Ford  

 Djikstra  

   informed  search    A*    Itera3ve  deeping  A*    Any3me  Dynamic  A*  

  Local  search    Hill-­‐Climbing    Enforced  Hill-­‐Climbing  

Page 45: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Hipster-core within NetBeans

Page 46: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Production rules private static List<Integer> applyActionToState(Action action, List<Integer> board) { List<Integer> successor = new ArrayList<Integer>(board); switch (action){

// fill can4 case RULE1: successor.set(0, 4); break; … } return successor; }

Page 47: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Production rules Control machanism Step  1:  enumerate  ac3ons  enum Action { RULE1, RULE2, RULE3, RULE4, RULE5, RULE6, RULE7, RULE8, RULE9, RULE10 }

Step  2:    instanciate  a  new  problem  builder:  SearchProblem p = ProblemBuilder.create()

Step  3:  configure  ini3al  state  .initialState(Arrays.asList(0,0))

Step  4:  declare  ac3ons  .defineProblemWithExplicitActions()

 

Page 48: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Step  5:  implement  the  interface  Ac3onFunc3on  with  two  types  

 .useActionFunction(new ActionFunction<Action, List<Integer>>() { public List<Action> actionsFor(List<Integer> state) {

return validMovementsFor(state); } })

Step  6:    to  generate  new  states  from  current  one  

 .useTransitionFunction(new ActionStateTransitionFunction<Action, List<Integer>>() { public List<Integer> apply(Action action, List<Integer> state) {

return applyActionToState(action, state); } })  

 

   

Production rules Control machanism

Page 49: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Step  7:  to  find  the  shortest  path  .useCostFunction(new CostFunction<Action, List<Integer>, Double>() { public Double evaluate(Transition<Action, List<Integer>> transition) {

return 1d; }

.build();

})

Final  step:  launch  the  algorithm  to  find  the  final  state  System.out.println(

Hipster.createDepthFirstSearch(p).search(

Arrays.asList(2,0)));

 

 

Production rules Control machanism

Page 50: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Execution

Page 51: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 51

Two players games

¢ Approaches for building programs that play two-person games l  tic-tac-toe, chess, etc.

¢ Much of the work in this area has been motivated by playing chess l Claude Shannon, the father of information

theory, originated many of the ideas in a 1949 paper

l … l Deep Blue 1997

Page 52: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 52

Two players games

¢  Some board posi t ion represents the initial state

¢  We generate the children of this position by making all of the legal moves available to us.

¢  Then, we consider the moves that our opponent can make to generate the descendants of each of these positions, etc.

¢  Note that these trees are enormous and cannot be explicitly represented in their entirety for any complex game.

Page 53: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 53

Partial game tree for Tic-Tac-Toe

Page 54: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 54

Scoring function

¢  This function assigns a numerical value to a board position

¢  We can think of this value as capturing the likelihood of winning from that position.

¢  Since in these games one person's win is another's person loss, we will use the same scoring function for both players, simply negating the values to represent the opponent's scores.

Page 55: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 55

MINiMAX Strategy

¢  Key idea: limited look-ahead + Min-Max algorithm ¢  Example:

l  depth of 2 l  values at the leaves: "static evaluations“ l  What we want: compute a value for each of the nodes above this one in the

tree by "backing up" these static evaluations in the tree.

¢  The player who is building the tree : maximize the score ¢  The opponent is trying to minimize the score à Each layer of the tree can be classified into either a maximizing layer or

a minimizing layer: MIN-MAX algorithm.

Page 56: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 56

Example

Page 57: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 57

Example

Page 58: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 58

MINIMAX code

Page 59: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 59

A simple Tron bot The University of Waterloo Computer Science Club runs an interesting AI challenge sponsored by Google. The aim of the challenge is to write a computer program that plays Tron.

Page 60: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 60

Importance of look-ahead

¢  The more look-ahead is (the deeper in the tree) the better our evaluation of a position will be

¢  Deep Blue l  256 specialized chess processors l  32 node supercomputer l  30 billion moves per minute l  Search depth [13, 30]

Page 61: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 61

ALPHA-BETA strategy

¢ With MINIMAX, comprehensive exploration of the space, ... According to the needed depth

¢ The technique is called alpha-beta: It is an optimization of Min-Max search

¢ Some values are not worth exploring given the values we already know

Page 62: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 62

http://www.cs.ualberta.ca/~chinook/

The Chinook project began in 1989 with the goal of developing a program capable of defeating the human World Checkers Champion. In 1990, Chinook became the first program in any game to win the right to play for a human World Championship. The program lost the Championship match in 1992, but became Champion in 1994. By 1996, it became clear that the program was much stronger than any human, and Chinook was retired.

Page 63: Artificial Intelligence · Karim Bouzoubaa Artificial Intelligence 9 Production system components ! A search space # Space of possible states # Representation of every state (couple,

Karim Bouzoubaa Artificial Intelligence 63

http://homepage.ufp.pt/jtorres/ensino/ia/alfabeta.html

Demo