18 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 |
|
|
20 |
|
|
21 |
|
import sys |
22 |
import random |
import random |
23 |
from sixdegrees import Graph, Node |
from sixdegrees import Graph, Node |
24 |
|
|
25 |
|
|
26 |
# maximum search depth (DLS limiter) |
# maximum search depth (DLS limiter) |
27 |
MAX_SEARCH_DEPTH = 5 |
MAX_SEARCH_DEPTH = 4 |
28 |
|
|
29 |
# settings for random graph |
# settings for random graph |
30 |
RANDOM_MAX_NODES = 10 |
#RANDOM_MAX_NODES = 10 |
31 |
RANDOM_MAX_CHILDREN_PER_NODE = 5 |
#RANDOM_MAX_CHILDREN_PER_NODE = 5 |
32 |
#RANDOM_MAX_NODES = 1000 |
RANDOM_MAX_NODES = 10000 |
33 |
#RANDOM_MAX_CHILDREN_PER_NODE = 10 |
RANDOM_MAX_CHILDREN_PER_NODE = 20 |
34 |
|
|
35 |
|
|
36 |
|
ENABLE_PROFILING = False |
37 |
|
ENABLE_JIT = True |
38 |
|
|
39 |
|
|
40 |
|
if ENABLE_PROFILING: |
41 |
|
import profile |
42 |
|
from profile import Profile |
43 |
|
|
44 |
|
|
45 |
def operateOnFixedGraph(): |
def operateOnFixedGraph(): |
69 |
|
|
70 |
def buildRandomGraph(graph): |
def buildRandomGraph(graph): |
71 |
|
|
72 |
|
count = 0 |
73 |
for parent_id in range(1, RANDOM_MAX_NODES + 1): |
for parent_id in range(1, RANDOM_MAX_NODES + 1): |
74 |
|
count += 1 |
75 |
|
if count % 100 == 0: |
76 |
|
sys.stderr.write('.') |
77 |
for j in range(1, random.randint(1, RANDOM_MAX_CHILDREN_PER_NODE)): |
for j in range(1, random.randint(1, RANDOM_MAX_CHILDREN_PER_NODE)): |
78 |
child_id = random.randint(1, RANDOM_MAX_NODES) |
child_id = random.randint(1, RANDOM_MAX_NODES) |
79 |
graph.addRelation(parent_id, child_id) |
graph.addRelation(parent_id, child_id) |
80 |
|
sys.stderr.write("\n") |
81 |
|
|
82 |
""" |
""" |
83 |
RANDOM_MAX_NODES = 10 |
RANDOM_MAX_NODES = 10 |
95 |
print '-' * 42 |
print '-' * 42 |
96 |
graph = Graph() |
graph = Graph() |
97 |
buildRandomGraph(graph) |
buildRandomGraph(graph) |
98 |
print graph |
#print graph |
99 |
|
|
100 |
# 2. choose two random distinct nodes |
# 2. choose two random distinct nodes |
101 |
node1 = graph.getNode(random.choice(graph.index.keys())) |
node1 = graph.getNode(random.choice(graph.index.keys())) |
111 |
# 1. calculate paths |
# 1. calculate paths |
112 |
print '-' * 42 |
print '-' * 42 |
113 |
print " Finding paths from %s to %s (depth=%s)" % (source_node.id, target_node.id, MAX_SEARCH_DEPTH) |
print " Finding paths from %s to %s (depth=%s)" % (source_node.id, target_node.id, MAX_SEARCH_DEPTH) |
114 |
|
print " Using JIT (Psyco):", bool(sys.modules.get('psyco')) |
115 |
print '-' * 42 |
print '-' * 42 |
116 |
paths = graph.computePaths(source_node, target_node, MAX_SEARCH_DEPTH) |
|
117 |
|
""" |
118 |
|
def doCompute(): |
119 |
|
#global paths |
120 |
|
paths = graph.computePaths(source_node, target_node, MAX_SEARCH_DEPTH) |
121 |
|
return paths |
122 |
|
""" |
123 |
|
|
124 |
|
if ENABLE_PROFILING: |
125 |
|
global paths |
126 |
|
paths = [] |
127 |
|
p = Profile() |
128 |
|
p.runcall(doCompute) |
129 |
|
p.print_stats() |
130 |
|
else: |
131 |
|
paths = graph.computePaths(source_node, target_node, MAX_SEARCH_DEPTH) |
132 |
|
|
133 |
|
#p.create_stats() |
134 |
|
#a = p.dump_stats() |
135 |
|
#print a |
136 |
|
#print dir(p) |
137 |
|
#for key, value in p.timings.iteritems(): |
138 |
|
# print '%s: %s' % (key, value) |
139 |
|
|
140 |
# 2. output paths |
# 2. output paths |
141 |
#print paths |
#print paths |
153 |
|
|
154 |
|
|
155 |
if __name__ == '__main__': |
if __name__ == '__main__': |
156 |
|
if ENABLE_JIT: |
157 |
|
# Import Psyco if available |
158 |
|
try: |
159 |
|
import psyco |
160 |
|
psyco.log() |
161 |
|
psyco.full() |
162 |
|
except ImportError: |
163 |
|
pass |
164 |
main() |
main() |