def testPop(self): ''' test getting population data for provinces / regions from wiki data ''' #Population.debug=True Population.fromWikiData() for pop in Population.pops: print("%6s;%9s;%40s;%9s;%s;%s" % (pop.isocode, pop.wikiDataId, pop.name, pop.size, pop.location, pop.coords))
def update(self): directions = self.directions[:] random.shuffle(directions) moving = Population(0.0) x, y = next(self.order_iter) curr = self.grid[x, y] if (x, y) == self.order[0]: self.census = self.running_census self.running_census = Population(0.0) curr.update() cpop = curr.pop tpop = cpop.good + cpop.sick + cpop.dead if tpop > 0: for dx, dy in directions: nx, ny = x + dx, y + dy if nx < 0 or nx >= self.width or ny < 0 or ny >= self.height: continue n = self.grid[nx, ny] if curr.walls.get((dx, dy)) or n.walls.get((-dx, -dy)): continue if curr.burning: # FIXME better conditions for catching fire if random.random() < FIRE_SPREAD_CHANCE_PER_FRAME: n.catch_fire() if n.is_blocked: continue attract_coef = min(0.25, n.attract * (1.0 + 100 * cpop.dead / tpop)) moving.good = attract_coef * curr.pop.good moving.sick = attract_coef * curr.pop.sick curr.pop -= moving n.incoming += moving n.incoming_acu[dx, dy] += moving.alive self.running_census += curr.pop caught_fire = curr.burning and self.caught_fire[x, y] if curr.burning: self.caught_fire[x, y] = False return x, y, curr.new_dead, curr.new_sick, caught_fire
def sample(): traf = Traffic(trafPath) pop = Population(popPath, longitudeRange, latitudeRange) def mapToLocation(sp): longi = (1 - sp[0]) * longitudeRange[0] + sp[0] * longitudeRange[1] lati = (1 - sp[1]) * latitudeRange[0] + sp[1] * latitudeRange[1] return np.array([longi, lati]) def computeDensity(sp): loc = mapToLocation(sp) trafDen = traf.estimateDensity(loc, 8e-3) / 350 popDen = pop.estimateDensity(loc) / 4.8e4 den = trafDen * popDen if den != 0: den /= (trafDen + popDen) return den mt = Metropolis(0.1, 0.005) samples = mt.sample(computeDensity, 2, 10000) locs = list(map(mapToLocation, samples)) locTran = np.array(locs).transpose() plt.scatter(locTran[0], locTran[1], s=0.2) df = pd.DataFrame({"Longitude": locTran[0], "Latitude": locTran[1]}) df.to_excel("data/location.xlsx", sheet_name='Locations', index=False) plt.show()
def __init__( self, view, attract=0.0, good=0.0, sick=0.0, dead=0.0, has_walls=False, gates=False, sprite_mask=0, health=0, ): self.view = view self.nview = view self.attract = attract self.is_blocked = False self.pop = Population(good, sick, dead) self.incoming = Population(0.0) self.incoming_acu = { Map.directions[0]: 0.0, Map.directions[1]: 0.0, Map.directions[2]: 0.0, Map.directions[3]: 0.0, } self.has_walls = has_walls self.gates = gates self.walls = {} self.sprite_mask = sprite_mask # new_dead is > 0 when there are new dead bodies in the cell # draw a soul sprite when that happens self.new_dead = 0 self.old_dead = 0 self.ttl_dead = random.randint(1, DEAD_TTL + 1) # new_sick is > 0 when there are new sick people in the cell # draw an infection animation that happens self.new_sick = 0 self.old_sick = 0 self.ttl_sick = random.randint(1, DEAD_TTL + 1) self.health = health self.burning = False self.burnt = False self.reap_infection_factor = 1.0
def plotDensity(): traf = Traffic(trafPath) pop = Population(popPath, longitudeRange, latitudeRange) nSample = 50 X = np.linspace(120.85, 121.98, nSample) Y = np.linspace(30.67, 31.51, nSample) gX, gY = np.meshgrid(X, Y) Z = np.ndarray((nSample, nSample)) for i in range(nSample): for j in range(nSample): trafDen = traf.estimateDensity([X[i], Y[j]], 8e-3) / 350 popDen = pop.estimateDensity([X[i], Y[j]]) / 4.8e4 den = trafDen * popDen if den != 0: den /= (trafDen + popDen) Z[i, j] = den fig = plt.figure() ax = Axes3D(fig) ax.plot_surface(gX, gY, Z, rstride=1, cstride=1, cmap=plt.cm.Blues) plt.show()
def main(score): # make a new population suitable to evolve this particular score population = Population(score) fitness = -100000000000 # some rediculous arbitrarily low fitness count = 0 generation = 0 while fitness < terminationFitness: population.develop() population.evaluate() population.nextGen() fitness = population.fittest.fitness generation += 1 print('generation #', generation) print('highest fitness', fitness) population.fittest.phenotype.write( "musicxml", "output/threevoice/generation" + str(generation) + ".xml")
def update(self): pop = self.pop pop += self.incoming self.incoming = Population(0.0) pop.kill(0.02) self.ttl_dead -= 1 if self.ttl_dead == 0: self.new_dead = int(pop.dead - self.old_dead) self.old_dead = int(pop.dead) self.ttl_dead = DEAD_TTL else: self.new_dead = 0 self.ttl_sick -= 1 if self.ttl_sick == 0: self.new_sick = int(pop.dead - self.old_dead) self.old_sick = int(pop.dead) self.ttl_sick = DEAD_TTL else: self.new_sick = 0 reap_mod = self.reap_infection_factor self.reap_infection_factor = 1.0 if pop.good == 0.0: return ratio = (INFECTION_COEFF_SICK * pop.sick + INFECTION_COEFF_DEAD * pop.dead) / pop.good / INFECTION_SCALE_FACTOR * reap_mod if ratio == 0.0: return pop.infect(min(MAX_INFECT_RATIO_PER_FRAME, ratio)) self.burn(BURN_FACTOR)
return 49 def distance(x, y): ab = np.abs(x - y) return ab[0] + ab[1] trafPath = "data/over.xlsx" popPath = "img/pop_den_trace.jpg" longitudeRange = [120.85, 121.98] latitudeRange = [30.67, 31.51] traf = Traffic(trafPath) pop = Population(popPath, longitudeRange, latitudeRange) sheet = get_sheet() #get des_p , des_in_which_route des_in_which_route = [0 for i in range(50)] p = [0 for i in range(50)] num_ = 0 for i in range(6): bo = 0 for j in sheet[i].values: if bo == 0: bo = 1 else: trafDen = traf.estimateDensity([j[0], j[1]], 8e-3) / 350 popDen = pop.estimateDensity([j[0], j[1]]) / 4.8e4
def main(args): print("IT'S DANGEROUS TO GO ALONE! TAKE THIS.") np.random.seed(0) pt.manual_seed(0) env = BipedalWalker() env.seed(0) obs_dim = env.observation_space.shape[0] act_dim = env.action_space.shape[0] print(f"Initializing agent (device={device})...") rnn = WorldModel(obs_dim, act_dim) ctrl = Controller(obs_dim + rnn.hid_dim, act_dim) # Adjust population size based on the number of available CPUs. num_workers = mp.cpu_count() if args.nproc is None else args.nproc num_workers = min(num_workers, mp.cpu_count()) agents_per_worker = args.popsize // num_workers popsize = num_workers * agents_per_worker print(f"Initializing population with {popsize} workers...") pop = Population(num_workers, agents_per_worker) global_mu = np.zeros_like(ctrl.genotype) loss_logger = ValueLogger('ha_rnn_loss', bufsize=20) best_logger = ValueLogger('ha_ctrl_best', bufsize=100) # Train the RNN with random policies. print(f"Training M model with a random policy...") optimizer = optim.Adam(rnn.parameters(), lr=args.lr) train_rnn(rnn, optimizer, pop, random_policy=True, num_rollouts=args.num_rollouts, logger=loss_logger) loss_logger.plot('M model training loss', 'step', 'loss') # Upload the trained RNN. success = pop.upload_rnn(rnn.cpu()) assert success # Iteratively update controller and RNN. for i in range(args.niter): # Evolve controllers with the trained RNN. print(f"Iter. {i}: Evolving C model...") es = EvolutionStrategy(global_mu, args.sigma0, popsize) evolve_ctrl(ctrl, es, pop, num_gen=args.num_gen, logger=best_logger) best_logger.plot('C model evolution', 'gen', 'fitness') # Update the global best individual and upload them. global_mu = np.copy(ctrl.genotype) success = pop.upload_ctrl(global_mu, noisy=True) assert success # Train the RNN with the current best controller. print(f"Iter. {i}: Training M model...") train_rnn(rnn, optimizer, pop, random_policy=False, num_rollouts=args.num_rollouts, logger=loss_logger) loss_logger.plot('M model training loss', 'step', 'loss') # Upload the trained RNN. success = pop.upload_rnn(rnn.cpu()) assert success # Test run! rollout(env, rnn, ctrl, render=True) success = pop.close() assert success
longitudeRange = [120.85, 121.98] latitudeRange = [30.67, 31.51] airportLoc = [121.808603, 31.142363] nStations = 50 nRoutes = 8 nSelect = 2 nTrafSeg = 5 kDist = 1 kTraf = 3e2 kPop = 2e5 traf = Traffic(trafPath) pop = Population(popPath, longitudeRange, latitudeRange) # Manhattan distance def manDist(pt1, pt2): return np.abs(pt1[0] - pt2[0]) + np.abs(pt1[1] - pt2[1]) def route(): df = pd.DataFrame(pd.read_excel(stationPath, dtype=np.float)) statVals = df.values # Define mapping function def samplesToRoute(samples): # samples must be an (nStations, 2) array # Intialize route routes = [] for _ in range(nRoutes): routes.append([airportLoc])
class Cell(object): def __init__( self, view, attract=0.0, good=0.0, sick=0.0, dead=0.0, has_walls=False, gates=False, sprite_mask=0, health=0, ): self.view = view self.nview = view self.attract = attract self.is_blocked = False self.pop = Population(good, sick, dead) self.incoming = Population(0.0) self.incoming_acu = { Map.directions[0]: 0.0, Map.directions[1]: 0.0, Map.directions[2]: 0.0, Map.directions[3]: 0.0, } self.has_walls = has_walls self.gates = gates self.walls = {} self.sprite_mask = sprite_mask # new_dead is > 0 when there are new dead bodies in the cell # draw a soul sprite when that happens self.new_dead = 0 self.old_dead = 0 self.ttl_dead = random.randint(1, DEAD_TTL + 1) # new_sick is > 0 when there are new sick people in the cell # draw an infection animation that happens self.new_sick = 0 self.old_sick = 0 self.ttl_sick = random.randint(1, DEAD_TTL + 1) self.health = health self.burning = False self.burnt = False self.reap_infection_factor = 1.0 def block(self): self.is_blocked = True def unblock(self): self.is_blocked = False def catch_fire(self): if self.health <= 0 or self.burnt or self.burning: return False self.burning = True return True def burn(self, factor): if not self.burning: return self.health -= 1 self.pop.burn(factor) if self.health <= 0: self.burning = False self.burnt = True def char(self): if self.is_blocked: return "x" return self.chars[self.type] def update(self): pop = self.pop pop += self.incoming self.incoming = Population(0.0) pop.kill(0.02) self.ttl_dead -= 1 if self.ttl_dead == 0: self.new_dead = int(pop.dead - self.old_dead) self.old_dead = int(pop.dead) self.ttl_dead = DEAD_TTL else: self.new_dead = 0 self.ttl_sick -= 1 if self.ttl_sick == 0: self.new_sick = int(pop.dead - self.old_dead) self.old_sick = int(pop.dead) self.ttl_sick = DEAD_TTL else: self.new_sick = 0 reap_mod = self.reap_infection_factor self.reap_infection_factor = 1.0 if pop.good == 0.0: return ratio = (INFECTION_COEFF_SICK * pop.sick + INFECTION_COEFF_DEAD * pop.dead) / pop.good / INFECTION_SCALE_FACTOR * reap_mod if ratio == 0.0: return pop.infect(min(MAX_INFECT_RATIO_PER_FRAME, ratio)) self.burn(BURN_FACTOR)
from blob import Blob import json # with open('blobby.json') as f: # data = json.load(f) # print(data) # blobby = Blob() # # blobby.__dict__ = data # for _ in range(10): # blobby.age() # print(blobby) # print(blobby.__dict__) # with open('blobby.json', 'w') as f: # json.dump(blobby.__dict__, f) from pop import Population ga = Population(20) print(ga) print() ga.run(5) print(ga)
def debug(score): # score.show() p = Population(score) p.develop() p.evaluate() p.nextGen()