예제 #1
0
파일: sim.py 프로젝트: xinkeyu/asst4-s20
 def __init__(self, id, ilf = 1.5):
     self.id = id
     self.ilf = ilf
     self.rng = rutil.RNG()
     # Region is own node + adjacency list
     self.region = [self]
     self.reset()
예제 #2
0
파일: sim.py 프로젝트: xinkeyu/asst4-s20
 def __init__(self, id, node, seed=rutil.DEFAULTSEED):
     self.id = id
     self.rng = rutil.RNG()
     self.node = node
     self.newNode = None
     node.addRat(self)
     self.reset(seed)
예제 #3
0
 def generateTree(self,
                  width,
                  height,
                  targetCount,
                  seed=None,
                  hilbert=False):
     self.clear()
     if seed is not None:
         self.seed = seed
     self.rng = rutil.RNG([seed])
     self.root = HilbertNode(self, width,
                             height) if hilbert else FractalNode(
                                 self, width, height)
     leafSet = [self.root]
     while len(leafSet) > 0 and len(leafSet) < targetCount:
         weights = [
             n.weight(self.areaExponent, self.distanceExponent)
             for n in leafSet
         ]
         idx = self.rng.weightedIndex(weights)
         node = leafSet[idx]
         leafSet = leafSet[:idx] + leafSet[idx + 1:]
         if node.branch():
             leafSet += node.children
     if len(leafSet) == 0:
         print("Failed to generate tree")
         self.clear()
     self.finish()
     return self
예제 #4
0
    def generate(self,
                 k=12,
                 gtype=GraphType.uniform,
                 ilf=None,
                 seed=rutil.DEFAULTSEED,
                 doZone=False):
        gt = GraphType()
        if not gt.feasible(gtype, k):
            print "Cannot generate graph of type %s for k = %d" % (
                gt.modeNames[gtype], k)
            return
        self.rng = rutil.RNG([seed])
        if ilf is not None:
            self.ilfRange = ilf
        self.commentList = []
        tgen = datetime.datetime.now()
        self.commentList.append("# Generated %s" % tgen.ctime())
        self.commentList.append(
            "# Parameters: k = %d, type = %s, ilf = (%.2f,%.2f)" %
            (k, gt.modeNames[gtype], self.ilfRange[0], self.ilfRange[1]))
        self.k = k
        self.nodeCount = k * k
        self.nodeList = [self.assignIlf(i) for i in range(self.nodeCount)]
        self.edges = {}
        self.doZone = doZone
        self.zoneList = []
        # Generate grid edges
        for r in range(k):
            for c in range(k):
                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 gtype in [gt.tiled, gt.vertical, gt.horizontal]:
            cells = 6 if gtype == gt.tiled else 12
            unit = self.k / cells
            tileX = unit if gtype in [gt.tiled, gt.vertical] else self.k
            tileY = unit if gtype in [gt.tiled, gt.horizontal] else self.k
            self.tile(tileX, tileY)

        elif gtype == gt.parquet:
            self.parquet()
        elif gtype == gt.irregular:
            self.irregular()
        elif gtype == gt.uniform and self.doZone:
            for r in range(k):
                for c in range(k):
                    self.zoneList.append((c, r, 1, 1))
        elif gtype != gt.uniform:
            print "Unknown graph type %d" % gtype
예제 #5
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
예제 #6
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
예제 #7
0
 def generateTree(self, width, height, targetCount, seed=None):
     self.clear()
     if seed is not None:
         self.seed = seed
     self.rng = rutil.RNG([seed])
     self.root = FractalNode(self, width, height)
     leafSet = [self.root]
     while len(leafSet) < targetCount:
         weights = [n.weight(self.exponent) for n in leafSet]
         idx = self.rng.weightedIndex(weights)
         node = leafSet[idx]
         if node.branch():
             leafSet = leafSet[:idx] + leafSet[idx + 1:]
             leafSet += node.children
     self.finish()
     return self
예제 #8
0
 def makeRats(self,
              fname="",
              mode=RatMode.uniform,
              load=1,
              seed=rutil.DEFAULTSEED):
     nodeCount = len(self.nodeList)
     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:
         rlist = [(self.k + 1) * i for i in range(self.k)]
     elif mode == RatMode.upleft:
         rlist = [0]
     elif mode == RatMode.lowright:
         rlist = [nodeCount - 1]
     else:
         sys.stderr.write("ERROR: Invalid rat mode\n")
         return False
     factor = nodeCount * load / len(rlist)
     fullRlist = rlist * factor
     if len(rlist) > 0:
         fullRlist = 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