A apresentação está carregando. Por favor, espere

A apresentação está carregando. Por favor, espere

CS 561, Lectures 3-5 1 Estrutura de dados para árvore de busca Estrutura do tipo nó (node) { Estado (no espaço de estados ao qual o nó corresponde); Pai.

Apresentações semelhantes


Apresentação em tema: "CS 561, Lectures 3-5 1 Estrutura de dados para árvore de busca Estrutura do tipo nó (node) { Estado (no espaço de estados ao qual o nó corresponde); Pai."— Transcrição da apresentação:

1 CS 561, Lectures 3-5 1 Estrutura de dados para árvore de busca Estrutura do tipo nó (node) { Estado (no espaço de estados ao qual o nó corresponde); Pai (nó que gerou o nó corrente); Operador (ação realizada para gerar o nó corrente); Profundidade (número de nós desde a raiz); Custo (do caminho desde a raiz); } Nó = (State, Parent-node, Operator, Depth, Path-cost)

2 CS 561, Lectures 3-5 2 Encapsulando informação sobre estado em nós

3 CS 561, Lectures 3-5 3 Relembrando: algoritmo de busca geral Function General-Search(problem, Queuing-Fn) returns a solution, or failure nodes make-queue(make-node(initial-state[problem])) loop do if nodes is empty then return failure node Remove-Front(nodes) if Goal-Test[problem] applied to State(node) succeeds then return node nodes Queuing-Fn(nodes, Expand(node, Operators[problem])) end Queuing-Fn(queue, elements) é uma função que insere um conjunto de elementos numa fila e determina a ordem de expansão dos nós. Variando esta função produz diferentes algoritmos de busca.

4 CS 561, Lectures 3-5 4 Avaliação de estratégias de busca Uma estratégia é definida pela sequência de expansão dos nós. São geralmente avaliados segundo 4 critérios: Completude: encontra sempre uma solução (se existir uma)? Complexidade de tempo: quanto demora (função do número de nós)? Complexidade em memória: quanta memória requer? Optimalidade: garante solução de custo mínimo? Tempo e complexidade são medidos em termos de: b – número máximo de galhos da árvore de busca d – profundidade da solução de custo mínimo m – profundidade máxima da árvore de busca (pode ser infinito)

5 CS 561, Lectures 3-5 5 Estratégias de busca Uninformed Usa apenas informação disponível na formulação do problema, sem número de passos, custo de caminhos Apenas distingue estado objetivo de estado não-objetivo (blind search) Breadth-first (primeiro em largura) Uniform-cost (custo uniforme) Depth-first (primeiro em profundidade) Depth-limited (limitado em profundidade) Iterative deepening (aprofundando iterativamente)

6 CS 561, Lectures 3-5 6 Breadth-first search (primeiro em largura) Expande nó mais raso ainda não expandido Implementação: Function Breadth-first-search (problem) returns a solution or failure return General-search(problem, Enqueue-at-end). Enqueue-at-end = coloca sucessores no final da fila

7 CS 561, Lectures 3-5 7 Exemplo: viajando de Arad para Bucharest

8 CS 561, Lectures 3-5 8 Breadth-first search

9 CS 561, Lectures 3-5 9 Breadth-first search

10 CS 561, Lectures 3-5 10 Breadth-first search

11 CS 561, Lectures 3-5 11 Propiedades do algoritmo breadth-first Complettude: Tempo: Memória: Otimalidade: Search algorithms are commonly evaluated according to the following four criteria: Completeness: does it always find a solution if one exists? Time complexity: how long does it take as function of num. of nodes? Space complexity: how much memory does it require? Optimality: does it guarantee the least-cost solution? Time and space complexity are measured in terms of: b – max branching factor of the search tree d – depth of the least-cost solution m – max depth of the search tree (may be infinity)

12 CS 561, Lectures 3-5 12 Propriedades do algoritmo breadth-first Completude: Sim, se b for finito Tempo:1+b+b 2 +…+b d = O(b d ), exponencial em d Memória:O(b d ), keeps every node in memory Otimalidade:Sim (assumindo custo = 1 por passo) Por que manter todos os nós em memória? Para evitar ter que revisitar nós já visitados, os quais podem levar a loops infinitos.

13 CS 561, Lectures 3-5 13 Complexidade de tempo para o breadth-first search Se um nó objetivo for encontrado em profundidade d da árvore, todos os nós até esta profundidade são criados. m G b d Então: O(b d )

14 CS 561, Lectures 3-5 14 FILA contém todos os nós e. (Então: 4). Em geral: b d Complexidade de memória do breadth-first search Maior número de nós na FILA são atingidos no nível d do nó objetivo. G m b d G

15 CS 561, Lectures 3-5 15 Uniform-cost search (custo uniforme) Então, a função que coloca na fila mantém uma lista de nós ordenada por custo de caminho crescente, e expandimos o primeiro nó não expandido (com menor custo) Custo uniforme é refinamento de breadth-first: Breadth-first = uniform-cost, basta fazer custo = profundidade do nó Expandir nó não expandido de custo mínimo. Implementação: QueuingFN = inserir em ordem de custo de caminho crescente.

16 CS 561, Lectures 3-5 16 Romania com custo de cada passo em KM

17 CS 561, Lectures 3-5 17 Uniform-cost search

18 CS 561, Lectures 3-5 18 Uniform-cost search

19 CS 561, Lectures 3-5 19 Uniform-cost search

20 CS 561, Lectures 3-5 20 Propriedades do algoritmo uniform-cost Completude:Sim, se custo de cada passo >0 Tempo:# nós com g custo da solução ótima, O(b d ) Espaço:# nodes with g cost of optimal solution, O(b d ) Otimalidade:Sim, se custo dos caminhos nunca diminuirem g(n) é o custo do caminho para o nó n Lembre-se: b = número de galhos (branching factor) d = profundidade da solução de custo mínimo

21 CS 561, Lectures 3-5 21 Implementação da busca uniform-cost Initialize Queue with root node (built from start state) Repeat until (Queue is empty) or (first node has Goal state): Remove first node from front of Queue Expand node (find its children) Reject those children that have already been considered, to avoid loops Add remaining children to Queue, in a way that keeps entire queue sorted by increasing path cost If Goal was reached, return success, otherwise failure

22 CS 561, Lectures 3-5 22 Precaução! Uniform-cost search não é ótimo se for terminado quando qualquer nó na fila te o estado objetivo. G 100 5 D 5 10 E 5 15 F 5 20 S A C 1 5 5 1 B 1 2 Uniform cost retorna o caminho com custo 102 (se qq nó contendo objetivo for considerado solução), enquanto há um caminho com custo 25.

23 CS 561, Lectures 3-5 23 Nota: detecção de Loop Vimos que a busca pode falhar ou ser sub-ótima se : - não detectar loop: então o algoritmo roda infinitos ciclos (A -> B -> A -> B -> …) - não tirar da fila um nó contendo um estado já visitado: pode levar a uma solução sub-ótima - simplesmente evitando voltar aos nosso pai: parece promissor, mas não provamos que isso funciona Solução? Não colocar na fila um nó se seu estado casa com o estado de algum de seus pais (assumindo custo de caminho > 0). Assim, se custo de caminho > 0,sempre custará mais considerar um nó com aquele estado novamente do que ele já custou na primeira vez. Isto é suficiente??

24 CS 561, Lectures 3-5 24 Exemplo G

25 CS 561, Lectures 3-5 25 Solução pelo Breadth-First Search

26 CS 561, Lectures 3-5 26 Solução pelo Uniform-Cost Search

27 CS 561, Lectures 3-5 27 Note: Queueing in Uniform-Cost Search In the previous example, it is wasteful (but not incorrect) to queue-up three nodes with G state, if our goal if to find the least-cost solution: Although they represent different paths, we know for sure that the one with smallest path cost (9 in the example) will yield a solution with smaller total path cost than the others. So we can refine the queueing function by: - queue-up node if 1) its state does not match the state of any parent and2) path cost smaller than path cost of any unexpanded node with same state in the queue (and in this case, replace old node with same state by our new node) Is that it??

28 CS 561, Lectures 3-5 28 A Clean Robust Algorithm Function UniformCost-Search(problem, Queuing-Fn) returns a solution, or failure open make-queue(make-node(initial-state[problem])) closed [empty] loop do if open is empty then return failure currnode Remove-Front(open) if Goal-Test[problem] applied to State(currnode) then return currnode children Expand(currnode, Operators[problem]) while children not empty [… see next slide …] end closed Insert(closed, currnode) open Sort-By-PathCost(open) end

29 CS 561, Lectures 3-5 29 A Clean Robust Algorithm [… see previous slide …] children Expand(currnode, Operators[problem]) while children not empty child Remove-Front(children) if no node in open or closed has childs state open Queuing-Fn(open, child) else if there exists node in open that has childs state if PathCost(child) < PathCost(node) open Delete-Node(open, node) open Queuing-Fn(open, child) else if there exists node in closed that has childs state if PathCost(child) < PathCost(node) closed Delete-Node(closed, node) open Queuing-Fn(open, child) end [… see previous slide …]

30 CS 561, Lectures 3-5 30 Example G 100 5 D 5 E 5 F 5 S A C 1 5 B 1 1 # State Depth Cost Parent 1S00-

31 CS 561, Lectures 3-5 31 Example G 100 5 D 5 E 5 F 5 S A C 1 5 B 1 1 # State Depth Cost Parent 1S00- 2A111 3C151 Insert expanded nodes Such as to keep open queue sorted Black = open queue Grey = closed queue

32 CS 561, Lectures 3-5 32 Example G 100 5 D 5 E 5 F 5 S A C 1 5 B 1 1 # State Depth Cost Parent 1S00- 2A111 4B222 3C151 Node 2 has 2 successors: one with state B and one with state S. We have node #1 in closed with state S; but its path cost 0 is smaller than the path cost obtained by expanding from A to S. So we do not queue-up the successor of node 2 that has state S.

33 CS 561, Lectures 3-5 33 Example G 100 5 D 5 E 5 F 5 S A C 1 5 B 1 1 # State Depth Cost Parent 1S00- 2A111 4B222 5C334 6G31024 Node 4 has a successor with state C and Cost smaller than node #3 in open that Also had state C; so we update open To reflect the shortest path.

34 CS 561, Lectures 3-5 34 Example G 100 5 D 5 E 5 F 5 S A C 1 5 B 1 1 # State Depth Cost Parent 1S00- 2A111 4B222 5C334 7D485 6G31024

35 CS 561, Lectures 3-5 35 Example G 100 5 D 5 E 5 F 5 S A C 1 5 B 1 1 # State Depth Cost Parent 1S00- 2A111 4B222 5C334 7D485 8E5137 6G31024

36 CS 561, Lectures 3-5 36 Example G 100 5 D 5 E 5 F 5 S A C 1 5 B 1 1 # State Depth Cost Parent 1S00- 2A111 4B222 5C334 7D485 8E5137 9F6188 6G31024

37 CS 561, Lectures 3-5 37 Example G 100 5 D 5 E 5 F 5 S A C 1 5 B 1 1 # State Depth Cost Parent 1S00- 2A111 4B222 5C334 7D485 8E5137 9F6188 10G7239 6G31024

38 CS 561, Lectures 3-5 38 Example G 100 5 D 5 E 5 F 5 S A C 1 5 B 1 1 # State Depth Cost Parent 1S00- 2A111 4B222 5C334 7D485 8E5137 9F6188 10G7239 6G31024 Goal reached

39 CS 561, Lectures 3-5 39 Depth-first search Expande o nó não expandido de maior profundidade Implementação: QueuingFN = insere sucessores na frente da fila

40 CS 561, Lectures 3-5 40 Romania with step costs in km

41 CS 561, Lectures 3-5 41 Depth-first search

42 CS 561, Lectures 3-5 42 Depth-first search

43 CS 561, Lectures 3-5 43 Depth-first search

44 CS 561, Lectures 3-5 44 Properties of depth-first search Completeness: No, fails in infinite state-space (yes if finite state space) Time complexity:O(b m ) Space complexity:O(bm) Optimality:No Remember: b = branching factor m = max depth of search tree

45 CS 561, Lectures 3-5 45 Time complexity of depth-first: details In the worst case: the (only) goal node may be on the right-most branch, G m b Time complexity == b m + b m-1 + … + 1 = b m+1 -1 Thus: O(b m ) b - 1

46 CS 561, Lectures 3-5 46 Space complexity of depth-first Largest number of nodes in QUEUE is reached in bottom left- most node. Example: m = 3, b = 3 :... QUEUE contains all nodes. Thus: 7. In General: ((b-1) * m) + 1 Order: O(m*b)

47 CS 561, Lectures 3-5 47 Avoiding repeated states In increasing order of effectiveness and computational overhead: do not return to state we come from, i.e., expand function will skip possible successors that are in same state as nodes parent. do not create paths with cycles, i.e., expand function will skip possible successors that are in same state as any of nodes ancestors. do not generate any state that was ever generated before, by keeping track (in memory) of every state generated, unless the cost of reaching that state is lower than last time we reached it.

48 CS 561, Lectures 3-5 48 Depth-limited search Is a depth-first search with depth limit l Implementation: Nodes at depth l have no successors. Complete: if cutoff chosen appropriately then it is guaranteed to find a solution. Optimal: it does not guarantee to find the least-cost solution

49 CS 561, Lectures 3-5 49 Iterative deepening search Function Iterative-deepening-Search(problem) returns a solution, or failure for depth = 0 to do result Depth-Limited-Search(problem, depth) if result succeeds then return result end return failure Combines the best of breadth-first and depth-first search strategies. Completeness: Yes, Time complexity:O(b d ) Space complexity:O(bd) Optimality:Yes, if step cost = 1

50 CS 561, Lectures 3-5 50 Romania with step costs in km

51 CS 561, Lectures 3-5 51

52 CS 561, Lectures 3-5 52

53 CS 561, Lectures 3-5 53

54 CS 561, Lectures 3-5 54

55 CS 561, Lectures 3-5 55

56 CS 561, Lectures 3-5 56

57 CS 561, Lectures 3-5 57

58 CS 561, Lectures 3-5 58

59 CS 561, Lectures 3-5 59 Iterative deepening complexity Iterative deepening search may seem wasteful because so many states are expanded multiple times. In practice, however, the overhead of these multiple expansions is small, because most of the nodes are towards leaves (bottom) of the search tree: thus, the nodes that are evaluated several times (towards top of tree) are in relatively small number. In iterative deepening, nodes at bottom level are expanded once, level above twice, etc. up to root (expanded d+1 times) so total number of expansions is: (d+1)1 + (d)b + (d-1)b^2 + … + 3b^(d-2) + 2b^(d-1) + 1b^d = O(b^d) In general, iterative deepening is preferred to depth-first or breadth-first when search space large and depth of solution not known.

60 CS 561, Lectures 3-5 60 Bidirectional search Both search forward from initial state, and backwards from goal. Stop when the two searches meet in the middle. Problem: how do we search backwards from goal?? predecessor of node n = all nodes that have n as successor this may not always be easy to compute! if several goal states, apply predecessor function to them just as we applied successor (only works well if goals are explicitly known; may be difficult if goals only characterized implicitly). for bidirectional search to work well, there must be an efficient way to check whether a given node belongs to the other search tree. select a given search algorithm for each half.

61 CS 561, Lectures 3-5 61 Comparing uninformed search strategies CriterionBreadth-UniformDepth-Depth-Iterative Bidirectional firstcostfirstlimiteddeepening (if applicable) Timeb^db^db^mb^lb^d b^(d/2) Spaceb^db^dbmblbd b^(d/2) Optimal?YesYesNoNoYes Yes Complete? YesYesNoYes if l dYes Yes b – max branching factor of the search tree d – depth of the least-cost solution m – max depth of the state-space (may be infinity) l – depth cutoff

62 CS 561, Lectures 3-5 62 Summary Problem formulation usually requires abstracting away real-world details to define a state space that can be explored using computer algorithms. Once problem is formulated in abstract form, complexity analysis helps us picking out best algorithm to solve problem. Variety of uninformed search strategies; difference lies in method used to pick node that will be further expanded. Iterative deepening search only uses linear space and not much more time than other uniformed search strategies.


Carregar ppt "CS 561, Lectures 3-5 1 Estrutura de dados para árvore de busca Estrutura do tipo nó (node) { Estado (no espaço de estados ao qual o nó corresponde); Pai."

Apresentações semelhantes


Anúncios Google