示例#1
0
文件: ann.py 项目: yfzanna/neural-mmo
    def visVals(self, food='max', water='max'):
        from forge.blade.core import realm
        posList, vals = [], []
        R, C = self.world.shape
        for r in range(self.config.BORDER, R - self.config.BORDER):
            for c in range(self.config.BORDER, C - self.config.BORDER):
                colorInd = int(self.config.NPOP * np.random.rand())
                color = Neon.color12()[colorInd]
                color = (colorInd, color)
                ent = entity.Player(-1, color, self.config)
                ent._r.update(r)
                ent._c.update(c)
                if food != 'max':
                    ent._food = food
                if water != 'max':
                    ent._water = water
                posList.append(ent.pos)

                self.world.env.tiles[r, c].addEnt(ent.entID, ent)
                stim = self.world.env.stim(ent.pos, self.config.STIM)
                #_, _, val = self.net(stim, ent)
                val = np.random.rand()
                self.world.env.tiles[r, c].delEnt(ent.entID)
                vals.append(float(val))

        vals = list(zip(posList, vals))
        return vals
示例#2
0
    def spawn(self, realm, iden, pop, name):
        '''Adds an entity to the given environment

      Args:
         realm : An environment Realm object
         iden  : An identifier index to assign to the new agent
         pop   : A population index to assign to the new agent
         name  : A string to prepend to iden as an agent name
      '''

        assert iden is not None
        assert self.pops[pop] <= self.popSz
        assert self.ents <= self.nEnt

        #Too many total entities
        if self.ents == self.nEnt:
            return None

        if self.pops[pop] == self.popSz:
            return None

        self.pops[pop] += 1
        self.ents += 1

        color = self.palette.colors[pop]
        ent = entity.Player(self.config, iden, pop, name, color)
        assert ent not in realm.desciples

        r, c = ent.base.pos
        realm.desciples[ent.entID] = ent
        realm.world.env.tiles[r, c].addEnt(iden, ent)
        realm.world.env.tiles[r, c].counts[ent.base.population.val] += 1
示例#3
0
    def visVals(self, food='max', water='max'):
        posList, vals = [], []
        R, C = self.world.shape
        for r in range(self.config.BORDER, R - self.config.BORDER):
            for c in range(self.config.BORDER, C - self.config.BORDER):
                colorInd = int(12 * np.random.rand())
                color = Neon.color12()[colorInd]
                color = (colorInd, color)
                ent = entity.Player(-1, color, self.config)
                ent._pos = (r, c)

                if food != 'max':
                    ent._food = food
                if water != 'max':
                    ent._water = water
                posList.append(ent.pos)

                self.world.env.tiles[r, c].addEnt(ent.entID, ent)
                stim = self.world.env.stim(ent.pos, self.config.STIM)
                s = torchlib.Stim(ent, stim, self.config)
                val = self.valNet(s).detach()
                self.world.env.tiles[r, c].delEnt(ent.entID)
                vals.append(float(val))

        vals = list(zip(posList, vals))
        return vals
示例#4
0
    def dense(self):
        '''Simulates an agent on every tile and returns observations

      This method is used to compute per-tile visualizations across the
      entire map simultaneously. To do so, we spawn agents on each tile
      one at a time. We compute the observation for each agent, delete that
      agent, and go on to the next one. In this fashion, each agent receives
      an observation where it is the only agent alive. This allows us to
      isolate potential influences from observations of nearby agents

      This function is slow, and anything you do with it is probably slower.
      As a concrete example, consider that we would like to visualize a
      learned agent value function for the entire map. This would require
      computing a forward pass for one agent per tile. To cut down on
      computation costs, we omit lava tiles from this method

      Returns:
         (dict, dict):

         observations:
            A dictionary of agent observations as specified by step()

         ents:
            A corresponding dictionary of agents keyed by their entID
      '''
        config = self.config
        R, C = self.realm.map.tiles.shape

        entID = 100000
        pop = 0
        name = "Value"
        color = (255, 255, 255)

        observations, ents = {}, {}
        for r in range(R):
            for c in range(C):
                tile = self.realm.map.tiles[r, c]
                if not tile.habitable:
                    continue

                current = tile.ents
                n = len(current)
                if n == 0:
                    ent = entity.Player(self.realm, (r, c), entID, pop, name,
                                        color)
                else:
                    ent = list(current.values())[0]

                obs = self.realm.dataframe.get(ent)
                if n == 0:
                    self.realm.dataframe.remove(Static.Entity, entID, ent.pos)

                observations[entID] = obs
                ents[entID] = ent
                entID += 1

        return observations, ents
示例#5
0
    def spawn(self):
        if len(self.desciples) >= self.config.NENT:
            return

        entID, color = self.god.spawn()
        ent = entity.Player(entID, color, self.config)
        self.desciples[ent.entID] = ent

        r, c = ent.pos
        self.world.env.tiles[r, c].addEnt(entID, ent)
        self.world.env.tiles[r, c].counts[ent.colorInd] += 1
示例#6
0
    def spawn(self, realm, iden, pop, name):
        assert self.pops[pop] <= self.popSz
        assert self.ents <= self.nEnt

        #Too many total entities
        if self.ents == self.nEnt:
            return None

        if self.pops[pop] == self.popSz:
            return None

        self.pops[pop] += 1
        self.ents += 1

        color = self.palette.colors[pop]
        ent = entity.Player(self.config, iden, pop, name, color)
        assert ent not in realm.desciples

        r, c = ent.base.pos
        realm.desciples[ent.entID] = ent
        realm.world.env.tiles[r, c].addEnt(iden, ent)
        realm.world.env.tiles[r, c].counts[ent.base.population.val] += 1