| 380 |
ira |
1 |
#!/usr/bin/env python
|
|
|
2 |
|
|
|
3 |
__author__ = "Ira W. Snyder (devel@irasnyder.com)"
|
|
|
4 |
__copyright__ = "Copyright (c) 2006 Ira W. Snyder (devel@irasnyder.com)"
|
|
|
5 |
__license__ = "GNU GPL v2 (or, at your option, any later version)"
|
|
|
6 |
|
|
|
7 |
# 1) Put the start node N0 on OPEN. Create G with just this node
|
|
|
8 |
# 2) Create the list CLOSED which is empty
|
|
|
9 |
#
|
|
|
10 |
# LOOP:
|
|
|
11 |
#
|
|
|
12 |
# 3) If OPEN is empty, exit with FAILURE
|
|
|
13 |
# 4) Select the first node from OPEN, put it on CLOSED. Call this N
|
|
|
14 |
# 5) If N is a goal node, exit successfully with the solution in G
|
|
|
15 |
# 6) Generate M from the children of N
|
|
|
16 |
# 7) Add anything not in M not in (CLOSED union OPEN) to OPEN
|
|
|
17 |
# 8) Reorder OPEN appropriately
|
|
|
18 |
# 9) goto LOOP
|
|
|
19 |
|
| 387 |
ira |
20 |
from PuzzlePiece import PuzzlePiece
|
| 380 |
ira |
21 |
from Graph import Graph
|
|
|
22 |
import yapgvb
|
|
|
23 |
|
| 387 |
ira |
24 |
class PuzzleSearch (object):
|
| 380 |
ira |
25 |
"""Implements a graph search"""
|
|
|
26 |
|
| 387 |
ira |
27 |
def __init__ (self, start_node, goal_nodes):
|
| 380 |
ira |
28 |
"""Constructor.
|
| 387 |
ira |
29 |
start_node: the node to start at (must have a get_children() function)
|
| 380 |
ira |
30 |
goal_nodes: a list of nodes to end at"""
|
|
|
31 |
|
|
|
32 |
self.__start_node = start_node
|
|
|
33 |
self.__goal_nodes = goal_nodes
|
|
|
34 |
self.__ordering_func = list.sort
|
|
|
35 |
|
|
|
36 |
def set_ordering_func (self, func):
|
|
|
37 |
"""Set the ordering function to use."""
|
|
|
38 |
self.__ordering_func = func
|
|
|
39 |
|
|
|
40 |
def __find_nearest_child (self, children, already_visited):
|
|
|
41 |
"""Find the child that we came from. This necessitates that
|
|
|
42 |
the list already_visited be sorted in the order of nodes visited"""
|
|
|
43 |
for n in reversed(already_visited):
|
|
|
44 |
if n in children:
|
|
|
45 |
return n
|
|
|
46 |
|
|
|
47 |
# This should never happen
|
|
|
48 |
raise ValueError
|
|
|
49 |
|
| 391 |
ira |
50 |
def search (self, add_function, MAX_NODES_CREATED=100):
|
| 380 |
ira |
51 |
|
|
|
52 |
# Create the result graph
|
|
|
53 |
result = Graph ()
|
|
|
54 |
firsttime = True
|
|
|
55 |
counter = 0
|
|
|
56 |
|
|
|
57 |
OPEN = [self.__start_node]
|
|
|
58 |
CLOSED = []
|
|
|
59 |
|
|
|
60 |
while OPEN:
|
|
|
61 |
N = OPEN.pop(0)
|
|
|
62 |
CLOSED.append (N)
|
|
|
63 |
|
|
|
64 |
# Find all possible next paths
|
| 387 |
ira |
65 |
M = N.get_children()
|
| 380 |
ira |
66 |
|
| 387 |
ira |
67 |
###############################################################
|
| 380 |
ira |
68 |
# Add the current place to the result graph
|
| 387 |
ira |
69 |
result.add_vertex (str(N))
|
| 380 |
ira |
70 |
if not firsttime:
|
| 387 |
ira |
71 |
v1 = str(N)
|
|
|
72 |
v2 = str(self.__find_nearest_child (M, CLOSED))
|
| 380 |
ira |
73 |
|
|
|
74 |
result.add_edge (v1, v2)
|
|
|
75 |
result.set_edge_color (v1, v2, yapgvb.colors.red)
|
|
|
76 |
result.set_edge_label (v1, v2, str(counter))
|
|
|
77 |
|
|
|
78 |
else:
|
| 391 |
ira |
79 |
# Set start node shape to be a double circle
|
|
|
80 |
result.set_vertex_shape (str(N), yapgvb.shapes.doublecircle)
|
| 380 |
ira |
81 |
firsttime = False
|
| 387 |
ira |
82 |
###############################################################
|
| 380 |
ira |
83 |
|
|
|
84 |
# Check if we've reached the goal
|
|
|
85 |
if N in self.__goal_nodes:
|
| 391 |
ira |
86 |
# Set the goal node's shape to be a diamond
|
|
|
87 |
result.set_vertex_shape (str(N), yapgvb.shapes.diamond)
|
| 387 |
ira |
88 |
return result
|
| 380 |
ira |
89 |
|
| 388 |
ira |
90 |
# Add the children of N (aka M) to OPEN
|
|
|
91 |
OPEN = add_function (M, OPEN, CLOSED)
|
| 380 |
ira |
92 |
|
|
|
93 |
counter += 1
|
|
|
94 |
|
| 388 |
ira |
95 |
# Check to make sure we don't loop for too long
|
| 391 |
ira |
96 |
if (len(OPEN) + len(CLOSED)) > MAX_NODES_CREATED:
|
| 388 |
ira |
97 |
return 'FAILURE'
|
|
|
98 |
|
| 380 |
ira |
99 |
return 'FAILURE'
|
|
|
100 |
|
|
|
101 |
def add_bfs (M, OPEN, CLOSED):
|
|
|
102 |
for node in M:
|
|
|
103 |
if (node not in OPEN) and (node not in CLOSED):
|
|
|
104 |
OPEN.append (node)
|
|
|
105 |
|
| 382 |
ira |
106 |
return OPEN
|
|
|
107 |
|
| 380 |
ira |
108 |
def add_dfs (M, OPEN, CLOSED):
|
|
|
109 |
for node in M:
|
|
|
110 |
if (node not in OPEN) and (node not in CLOSED):
|
|
|
111 |
OPEN.insert (0, node) # insert node at beginning
|
|
|
112 |
|
| 382 |
ira |
113 |
return OPEN
|
| 380 |
ira |
114 |
|
| 382 |
ira |
115 |
|
| 380 |
ira |
116 |
from Graph import Graph
|
|
|
117 |
from DrawGraph import DrawGraph
|
| 387 |
ira |
118 |
import random
|
| 380 |
ira |
119 |
|
| 387 |
ira |
120 |
def get_nth_child (start, n):
|
|
|
121 |
child = start
|
|
|
122 |
for i in xrange(n):
|
|
|
123 |
child = random.choice(child.get_children())
|
|
|
124 |
|
|
|
125 |
return child
|
|
|
126 |
|
| 380 |
ira |
127 |
def main ():
|
| 387 |
ira |
128 |
initial = [1, 2, 3, 4, 'E', 5, 6, 7, 8]
|
| 380 |
ira |
129 |
|
| 387 |
ira |
130 |
start = PuzzlePiece (initial)
|
| 388 |
ira |
131 |
goal = get_nth_child (start, 10)
|
| 380 |
ira |
132 |
|
| 387 |
ira |
133 |
s = PuzzleSearch (start, (goal, ))
|
| 388 |
ira |
134 |
result = s.search (add_bfs, 1000)
|
| 380 |
ira |
135 |
|
|
|
136 |
if result != 'FAILURE':
|
| 387 |
ira |
137 |
DrawGraph ('result', result).render_graph ('res.svg', yapgvb.engines.dot)
|
| 388 |
ira |
138 |
else:
|
|
|
139 |
print result
|
| 380 |
ira |
140 |
|
|
|
141 |
|
|
|
142 |
if __name__ == '__main__':
|
|
|
143 |
main ()
|