def render(self, trans, update): env = self.realm.world.env tiles = env.tiles R, C = tiles.shape counts = np.array([tile.counts for tile in tiles.ravel()]) nCounts = counts.shape[1] counts = counts.reshape(R, C, counts.shape[1]) counts = [counts[:, :, i] for i in range(counts.shape[2])] if nCounts <= 12: colors = Neon.color12() else: colors = Color256.colors[0::256 // nCounts] for idx, count in enumerate(counts): counts[idx] = 20 * counts[idx][:, :, np.newaxis] * np.array( colors[idx].value) / 255.0 sumCounts = sum(counts) R, C, _ = sumCounts.shape counts = np.clip(sumCounts, 0, 255).astype(np.uint8) counts = np.clip(counts, 0, 255).astype(np.uint8) valCrop, txSz = embyr.mapCrop(self, counts, self.tileSz, trans) txSz = embyr.mipKey(txSz, self.mipLevels) embyr.renderMap(self, valCrop, txSz) return self.surf
def visDeps(self): from forge.blade.core import realm from forge.blade.core.tile import Tile colorInd = int(self.config.NPOP * np.random.rand()) color = Neon.color12()[colorInd] color = (colorInd, color) ent = realm.Desciple(-1, self.config, color).server targ = realm.Desciple(-1, self.config, color).server sz = 15 tiles = np.zeros((sz, sz), dtype=object) for r in range(sz): for c in range(sz): tiles[r, c] = Tile(enums.Grass, r, c, 1, None) targ.pos = (7, 7) tiles[7, 7].addEnt(0, targ) posList, vals = [], [] for r in range(sz): for c in range(sz): ent.pos = (r, c) tiles[r, c].addEnt(1, ent) #_, _, val = self.net(tiles, ent) val = np.random.rand() vals.append(float(val)) tiles[r, c].delEnt(1) posList.append((r, c)) vals = list(zip(posList, vals)) return vals
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
def plots(logs): colors = Neon.color12() logs = reversed([e for e in logs.items()]) for idx, kv in enumerate(logs): k, v = kv color = colors[idx].norm plot(v, k, color)
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
def plot(x, idxs, label, idx, path): colors = Neon.color12() loglib.dark() c = colors[idx % 12] loglib.plot(x, inds=idxs, label=str(idx), c=c.norm) loglib.godsword() loglib.save(path + label + '.png') plt.close()
def plots(x, label, idx, path, split): colors = Neon.color12() loglib.dark() for idx, item in enumerate(x.items()): annID, val = item c = colors[idx % 12] idxs, val = compress(val, split) loglib.plot(val, inds=idxs, label=str(annID), c=c.norm) loglib.godsword() loglib.save(path + label + '.png') plt.close()
def agents(): exps = list(experiments.exps.keys()) loglib.dark() colors = Neon.color12() maxVal = 0 for idx, exp in enumerate(exps): name, log = exp c = colors[idx] loglib.plot(log['lifespan'], name, c.norm) maxVal = max(maxVal, np.max(log['lifespan'])) loglib.limits(ylims=[0, 50*(1+maxVal//50)]) loglib.godsword() loglib.save(logDir+'/agents.png') plt.close()
def joints(exps): print('Joints...') keys = reversed('return lifespan value value_loss pg_loss entropy grad_mean grad_std grad_min grad_max'.split()) colors = Neon.color12() for key in keys: loglib.dark() maxVal = 0 for idx, dat in enumerate(exps): name, _, log = dat loglib.plot(log[key], name, colors[idx].norm, lw=3) maxVal = max(maxVal, np.max(log[key])) loglib.limits(ylims=[0, 50*(1+maxVal//50)]) loglib.godsword() loglib.save(logDir+'joint/'+key) plt.close()
def __init__(self, ent): self.ent = ent self.frame = 0 #self.pos = ent.lastPos #self.newPos = ent.pos self.lastPos = ent.lastPos if not ent.alive: self.lastPos = ent.pos r, c = self.lastPos self.posAnim = self.lastPos rNew, cNew = self.ent.pos self.rOff, self.cOff = np.sign(rNew - r), np.sign(cNew - c) #self.targ = ent.attack.args nNames = len(names) self.entName = names[int(ent.entID) % nNames] self.nameColor = Neon.color12()[int(ent.entID) % 12]
def gen_plot(log, keys, savename, train=True): loglib.dark() if len(keys) > 12: colors = Color256.colors else: colors = Neon.color12() pops = [] for i, key in enumerate(keys): c = colors[i] if not train: log[key] = np.cumsum(np.array(log[key])) / (1+np.arange(len(log[key]))) if i == 0: loglib.plot(log[key], key, (1.0, 0, 0)) else: loglib.plot(log[key], key, c.norm) loglib.godsword() loglib.save(savename) plt.close()
def individual(log, label, npop, logDir='resource/data/exps/', train=True): if train: split = 'train' else: split = 'test' savedir = osp.join(logDir, label, split) if not osp.exists(savedir): os.makedirs(savedir) if len(log['return']) > 0: loglib.dark() keys = reversed('return lifespan value value_loss pg_loss entropy grad_mean grad_std grad_min grad_max'.split()) colors = Neon.color12() fName = 'frag.png' for idx, key in enumerate(keys): if idx == 0: c = colors[idx] loglib.plot(log[key], key, (1.0, 0, 0)) else: c = colors[idx] loglib.plot(log[key], key, c.norm) maxLife = np.max(log['return']) loglib.limits(ylims=[0, 50*(1+maxLife//50)]) loglib.godsword() savepath = osp.join(logDir, label, split, fName) loglib.save(savepath) print(savepath) plt.close() # Construct population specific code pop_mean_keys = ['lifespan{}_mean'.format(i) for i in range(npop)] savefile = osp.join(logDir, label, split, 'pop_mean.png') gen_plot(log, pop_mean_keys, savefile, train=train) # Per population movement probability pop_move_keys = ['pop{}_move'.format(i) for i in range(npop)] savefile = osp.join(logDir, label, split, 'pop_move.png') gen_plot(log, pop_move_keys, savefile, train=train) # Attack probability plots pop_move_keys = ['pop{}_range'.format(i) for i in range(npop)] savefile = osp.join(logDir, label, split, 'pop_range.png') gen_plot(log, pop_move_keys, savefile, train=train) pop_move_keys = ['pop{}_melee'.format(i) for i in range(npop)] savefile = osp.join(logDir, label, split, 'pop_melee.png') gen_plot(log, pop_move_keys, savefile, train=train) pop_move_keys = ['pop{}_mage'.format(i) for i in range(npop)] savefile = osp.join(logDir, label, split, 'pop_mage.png') gen_plot(log, pop_move_keys, savefile, train=train) # Movement tile entropy pop_move_keys = ['pop{}_entropy'.format(i) for i in range(npop)] savefile = osp.join(logDir, label, split, 'pop_move_entropy.png') gen_plot(log, pop_move_keys, savefile, train=train) # Population attack probabilities when action is selected pop_move_keys = ['pop{}_melee_logit'.format(i) for i in range(npop)] savefile = osp.join(logDir, label, split, 'pop_melee_logit.png') gen_plot(log, pop_move_keys, savefile, train=train) pop_move_keys = ['pop{}_range_logit'.format(i) for i in range(npop)] savefile = osp.join(logDir, label, split, 'pop_range_logit.png') gen_plot(log, pop_move_keys, savefile, train=train) pop_move_keys = ['pop{}_mage_logit'.format(i) for i in range(npop)] savefile = osp.join(logDir, label, split, 'pop_mage_logit.png') gen_plot(log, pop_move_keys, savefile, train=train) # Sum up all the logits to check if they actually sum to zero for i in range(npop): logit_sum = np.array(log['pop{}_melee_logit'.format(i)]) + np.array(log['pop{}_range_logit'.format(i)]) + np.array(log['pop{}_mage_logit'.format(i)]) log['pop{}_sum_logit'.format(i)] = logit_sum pop_move_keys = ['pop{}_sum_logit'.format(i) for i in range(npop)] savefile = osp.join(logDir, label, split, 'pop_sum_logit.png') gen_plot(log, pop_move_keys, savefile, train=train) # Tile exploration statistics pop_move_keys = ['pop{}_grass_tiles'.format(i) for i in range(npop)] savefile = osp.join(logDir, label, split, 'pop_grass_tiles.png') gen_plot(log, pop_move_keys, savefile, train=train) pop_move_keys = ['pop{}_forest_tiles'.format(i) for i in range(npop)] savefile = osp.join(logDir, label, split, 'pop_forest_tiles.png') gen_plot(log, pop_move_keys, savefile, train=train) pop_move_keys = ['pop{}_forest_tiles_depleted'.format(i) for i in range(npop)] savefile = osp.join(logDir, label, split, 'pop_forest_depleted.png') gen_plot(log, pop_move_keys, savefile, train=train) # pop_move_keys = ['pop{}_forest_tiles_other'.format(i) for i in range(npop)] # savefile = osp.join(logDir, label, 'pop_forest_tiles_other.png') # gen_plot(log, pop_move_keys, savefile, train=train) for i in range(npop): forest_tiles = np.array(log['pop{}_forest_tiles'.format(i)]) other_tiles = np.array(log['pop{}_grass_tiles'.format(i)]) + np.array(log['pop{}_forest_tiles_depleted'.format(i)]) + forest_tiles forage_percent = forest_tiles / other_tiles log['pop{}_forage_success'.format(i)] = forage_percent pop_move_keys = ['pop{}_forage_success'.format(i) for i in range(npop)] savefile = osp.join(logDir, label, split, 'pop_forage_success.png') gen_plot(log, pop_move_keys, savefile, train=train)
def __init__(self, config): self.index = config.PATH_THEME_WEB self.colors = Neon.color12()