# makeGraph.py # # generate a graph with random nodes and 2 forward edges # import sarg, random, math pixels = sarg.Int("pixels" ,600) def randomXY (pixels) : bounds = pixels/10 x = bounds + random.random()*(pixels-bounds) y = bounds + random.random()*(pixels-bounds) return int(x),int(y) def distTo (pos1, pos2) : xdist = abs(pos2[0]-pos1[0]) ydist = abs(pos2[1]-pos1[1]) dist = int(math.sqrt(xdist*xdist + ydist*ydist)) return dist def fillGraph (Node, nodes, Edge, edges) : pixels = sarg.Int("pixels", 600) seed = sarg.Int("seed", 0) if seed : random.seed(seed) else : seed = random.randint(101,999) print "Seed is", seed random.seed(seed) n_nodes = sarg.Int("nodes", 6) positions = genPositions(pixels, n_nodes) labels = "SABCDEFGHIJKLMNOPQRSTUVWXYZ" print "# ------------- Copy paste following to graph_%s.py"%seed print "def fillGraph(Node, nodes, Edge, edges) : # seed=%s" % seed lowlbls = labels.lower() npos = len(positions) dist = ", dist=0" for i in range(npos) : label = labels[i]; pos=positions[i] print " %s = Node('%s', %s%s)" % (label.lower(), label, pos, dist) dist = "" if label == 'S' : Node(label, pos, dist=0) else : Node(label, pos) print for i in range(npos) : nodei = nodes[i] dists = [] for j in range(i+1,npos) : nodej = nodes[j] dist = distTo(nodei.pos, nodej.pos) dists.append ((dist, j)) dists.sort() for dist,j in dists[:2] : print " Edge(%s,%s,%d)" % (lowlbls[i],lowlbls[j],dist) Edge(nodei,nodes[j],dist) print "# ------------- End of Copy paste" print def genPositions(pixels, n_nodes) : positions = [] for i in range(n_nodes) : ok = False while not ok : p1 = randomXY(pixels) ok = True for p2 in positions : if distTo(p1,p2) < 100 : ok=False positions.append(p1) positions.sort() return positions if __name__ == "__main__" : from dijkstra import Node, Edge, nodes, edges fillGraph (Node, nodes, Edge, edges)