Пример #1
0
 def store(self, fname = None):
     if fname is None:
         f = sys.stdout
     else:
         try:
             f = open(fname, "w")
         except:
             fractal.errorMessage("Couldn't open file '%s' for writing" % (fname))
             return False
     elist = self.edgeList()
     if len(self.regionList) == 0:
         fractal.showComments(self.commentList + ["", "Width Height Edges"], f)
         f.write("%d %d %d\n" % (self.width, self.height, len(self.edges)))
     else:
         fractal.showComments(self.commentList + ["", "Width Height Edges Regions"], f)
         f.write("%d %d %d %d\n" % (self.width, self.height, len(self.edges), len(self.regionList)))
     for i in range(len(self.nodeList)):
         f.write("n %d %.5f\n" % (i, self.nodeList[i]))
     for e in elist:
         f.write("e %d %d\n" % e)
     for r in self.regionList:
         f.write("r %d %d %d %d\n" % (r.x, r.y, r.w, r.h))
     if fname != "":
         f.close()
     return True
Пример #2
0
 def makeRats(self, fname = "", mode = RatMode.uniform, load = 1, seed = rutil.DEFAULTSEED):
     nodeCount = self.width * self.height
     clist = []
     tgen = datetime.datetime.now()
     clist.append("# Generated %s" % tgen.ctime())
     clist.append("# Parameters: load = %d, mode = %s, seed = %d" % (load, RatMode.modeNames[mode], seed))
     rng = rutil.RNG([seed])
     if fname == "":
         f = sys.stdout
     else:
         try:
             f = open(fname, "w")
         except:
             "Couldn't open output file '%s'"
             return False
     rlist = []
     if mode == RatMode.uniform:
         rlist = range(nodeCount)
     elif mode == RatMode.diagonal:
         if self.width >= self.height:
             aspect = float(self.height)/self.width
             for c in range(self.width):
                 r = int(aspect * c)
                 rlist.append(self.id(r,c))
         else:
             aspect = float(self.width)/self.height
             for r in range(self.height):
                 c = int(aspect * r)
                 rlist.append(self.id(r,c))
     elif mode == RatMode.center:
         r = self.height // 2
         c = self.width  // 2
         rlist = [self.id(r,c)]
     elif mode == RatMode.random:
         pass
     else:
         fractal.errorMessage("makeRats: Invalid rat mode (%d)" % mode)
         return False
     ratCount = nodeCount * load
     if mode == RatMode.random:
         fullRlist = [self.rng.randInt(0, nodeCount-1) for r in range(ratCount)]
     else:
         factor = ratCount // len(rlist)
         fullRlist = rlist * factor
         fullRlist = self.rng.permute(fullRlist)
     # Print it out
     f.write("%d %d\n" % (nodeCount, len(fullRlist)))
     for c in clist:
         f.write(c + '\n')
     for id in fullRlist:
         f.write("%d\n" % id)
     if fname != "":
         f.close()
     return True
Пример #3
0
 def makeHub(self, x, y, w, h):
     hlist = self.hubList(x, y, w, h)
     for cx, cy in hlist:
         cid = self.id(cy, cx)
         for j in range(w):
             for i in range(h):
                 id = self.id(y+i, x+j)
                 self.addEdge(cid, id)
                 if self.errorCount > self.errorLimit:
                     fractal.errorMessage("... makeHub.  Too many errors x = %d, y = %d, w = %d, h = %d" % (x, y, w, h))
                     return
Пример #4
0
    def generate(self, tree, expansion = 1, ilf = None, seed = rutil.DEFAULTSEED, doRegion = False):
        self.clear()
        self.rng = rutil.RNG([seed])
        self.tree = tree
        self.expansion = expansion
        if ilf is not None:
            self.ilfRange = ilf
        self.commentList = []
        self.regionList = []
        tgen = datetime.datetime.now()
        self.commentList.append("Generated %s" % tgen.ctime())
        self.commentList.append("Parameters: expansion = %d, ilf = (%.2f,%.2f)" % (expansion, self.ilfRange[0], self.ilfRange[1]))
        self.commentList.append("Region tree structure")
        self.commentList += tree.headerList()
        self.width = expansion * tree.root.width
        self.height = expansion * tree.root.height
        nodeCount = self.width * self.height
        self.nodeList = [self.assignIlf(i) for i in range(nodeCount)]
        self.edges = {}
        # Generate grid edges
        errCount = 0
        for r in range(self.height):
            for c in range(self.width):
                own = self.id(r, c)
                north = self.id(r-1, c)
                if north >= 0:
                    self.addEdge(own, north)
                south = self.id(r+1, c)
                if south >= 0:
                    self.addEdge(own, south)
                west = self.id(r, c-1)
                if west >= 0:
                    self.addEdge(own, west)
                east = self.id(r, c+1)
                if east >= 0:
                    self.addEdge(own, east)
                if self.errorCount > self.errorLimit:
                    fractal.errorMessage("... generate: Too many errors (r = %d, c = %d)" % (r,c))
                    return

        rid = 0
        for n in tree.leafList():
            rwidth = n.width   * expansion
            rheight = n.height * expansion
            rleftX = n.leftX   * expansion
            rupperY = n.upperY * expansion
            if doRegion:
                self.regionList.append(Region(self, rid, rleftX, rupperY, rwidth, rheight))
                rid += 1
            self.makeHub(rleftX, rupperY, rwidth, rheight)
            if self.errorCount > self.errorLimit:
                return
Пример #5
0
 def addEdge(self, i, j):
     nodeCount = len(self.nodeList)
     if i < 0 or i >= nodeCount:
         fractal.errorMessage("Invalid from node id %d" % i)
         self.errorCount += 1
     if j < 0 or j >= nodeCount:
         fractal.errorMessage("Invalid to node id %d" % j)
         self.errorCount += 1
     if i != j and (i,j) not in self.edges:
         self.edges[(i,j)] = True
         self.edges[(j,i)] = True
         return True
     return False
Пример #6
0
 def load(self, fname = ""):
     self.clear()
     if fname == "":
         f = sys.stdin
     else:
         try:
             f = open(fname, "r")
         except:
             fractal.errorMessage("Could not open file '%s'" % fname)
             return False
     expectedEgeCount = 0
     expectedNodeCount = 0
     expectedRegionCount = 0
     realEdgeCount = 0
     realNodeCount = 0
     realRegionCount = 0
     for line in f:
         if fractal.isComment(line):
             continue
         args = line.split()
         cmd = args[0]
         # Header information
         if self.width == 0:
             ifields = []
             if len(args) < 3 or len(args) > 4:
                 fractal.errorMessage("Invalid header line '%s'" % line)
                 return False
             try:
                 ifields = [int(s) for s in args]
             except:
                 fractal.errorMessage("Invalid header line '%s'" % line)
                 return False
             self.width = ifields[0]
             self.height = ifields[1]
             expectedEdgeCount = ifields[2]
             expectedNodeCount = self.width * self.height
             expectedRegionCount = ifields[3] if len(args) == 4 else 0
             self.nodeList = [1.5 for i in range(expectedNodeCount)]
         elif cmd == 'n':
             ilf = float(args[1])
             self.nodeList[realNodeCount] = ilf
             realNodeCount += 1
         elif cmd == 'e':
             i = int(args[1])
             j = int(args[2])
             if self.addEdge(i,j):
                 # Since addEdge puts both (i,j) and (j,i) into set, only half of the
                 # edges will return True from addEdge
                 realEdgeCount += 2 
         elif cmd == 'r':
             x, y, w, h = [int(s) for s in args[1:]]
             r = Region(self, realRegionCount, x, y, w, h)
             self.regionList.append(r)
             realRegionCount += 1
         else:
             fractal.errorMessage("Couldn't read graph file '%s'.  Invalid line: '%'" % fname, fractal.trim(line))
             self.clear()
             return False
     if fname is not None:
         f.close()
     if realNodeCount != expectedNodeCount:
         fractal.errorMessage("Couldn't read graph file '%s'.  Expected %d nodes.  Found %d" % (fname, expectedNodeCount, realNodeCount))
         return False
     if realEdgeCount != expectedEdgeCount:
         fractal.errorMessage("Couldn't read graph file '%s'.  Expected %d edges.  Found %d" % (fname, expectedEdgeCount, realEdgeCount))
         return False
     if realRegionCount != expectedRegionCount:
         fractal.errorMessage("Couldn't read graph file '%s'.  Expected %d regions.  Found %d" % (fname, expectedRegionCount, realRegionCount))
         return False
     else:
         fractal.infoMessage("Read %d X %d graph with %d edges" % (self.width, self.height, realEdgeCount))
         return True