def __init__(self, settings): BaseWorld.__init__(self, settings) self.elevation = imread(self.settings['elevationMap'], flatten = True) self.size = self.elevation.shape[0] self.lattice = [[[] for _ in range(self.size)] for _ in range(self.size)] self.agents = [self.generate_agent() for i in range(settings['numberOfAgents'])]
def __init__(self): """Create new world and setup its refresh rate""" # timing control (period = ideal time period of each step) self.period = 1.0 / REFRESH_RATE # seconds # create the world self.world = World(self.period)
def __init__(self): super(Farm, self).__init__(1500,1000) # Set up window self.keyboard = pyglet.window.key.KeyStateHandler() self.push_handlers(self.keyboard) # Set up environment self.living_bugs = [] self.mating_bugs = set() self.world = World(self.width, self.height) print 'width: ', self.width print 'height: ', self.height # Populate world bug = Bug(self.world, self.mating_bugs) self.living_bugs.append(bug)
def run(self): #self.mousePicker = Picker() #self.mousePicker.makePickable(self.renderer.GUI.mapView.mapView) while True: # Figure out logging # Implement pause if self.renderer.GUI.showPause: time.sleep(0.05) else: # Retrieve inputs and/or actions from renderer.window # Process actions #print(helpers.geographer.get_region_terrain((1, 1, 1))) if base.mouseWatcherNode.hasMouse(): x = base.mouseWatcherNode.getMouseX() y = base.mouseWatcherNode.getMouseY() self.renderer.updateCoordinates(x, y) # Update world date = self.update_world() maps = { 0: (World.terrain, 100, World.terrainColorSpace), 1: (World.rain, 1, World.terrainColorSpace), 2: (World.precipitation, 0.35, World.terrainColorSpace), 3: (World.sun, 0.5, World.terrainColorSpace), } self.renderer.updateMapView(maps[self.renderer.GUI.layer]) self.renderer.updateTime(date) if self.renderer.GUI.regen: World.regen() self.renderer.GUI.regen = False # Update scene helpers.chronologist.increment_time() taskMgr.step()
return stats tf.reset_default_graph() global_step = tf.Variable(0, name="global_step", trainable=False) policy_estimator = PolicyEstimator() estimator_value = ValueEstimator() fixed = True if fixed == True: fixed_str = 'fixed' else: fixed_str = 'not_fixed' env = World(fixed=fixed) # Assume that you have 12GB of GPU memory and want to allocate ~4GB: gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.167) num_episodes = 100000 #20000 with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess: sess.run(tf.global_variables_initializer()) # Note, due to randomness in the policy the number of episodes you need to learn a good # policy may vary. ~2000-5000 seemed to work well for me. stats = reinforce(env, policy_estimator, num_episodes, estimator_value=None) with open('episode_lengths_lstm_policy_gradient_strict_lr_00001.pickle',
def main(): env = World(10, 10) creatures = list() env.replenish(20) env.display() num_creatures = 10 for i in range(0, num_creatures): x, y = env.random_location() creatures.append(Cell(x, y)) print(creatures[i]) total_rounds = 10 iterations = 0 for i in range(0, total_rounds): creatures_can_move = True while (creatures_can_move and iterations < 1000000): for creature_i in range(0, len(creatures)): creature = creatures[creature_i] if creature.will_move(): possible_moves = {"left", "right", "up", "down"} while (len(possible_moves) and not creature.satisfied): direction = choice(tuple(possible_moves)) xpos, ypos = creature.pos_with_move(direction) if env.valid_spot(xpos, ypos): print( f'Moving: {creature_i} ({creature.moves_this_turn})' ) creature.move(direction) if env.board[creature.x][ creature.y].total_nutrients() > 0: creature.eat(env.board[creature.x][ creature.y].consume().nutrients) creature.satisfied = True # Conditions for satisfaction will change break possible_moves.remove(direction) iterations += 1 # Continue this round if any creature can continue to move creatures_can_move = False for creature_i in range(0, len(creatures)): creature = creatures[creature_i] if creature.will_move(): # print(f'Creature: {creature_i} still wants to move after doing {creature.moves_this_turn} moves') creatures_can_move = True print("Round ended") env.display() for creature in creatures: print(creature) creature.return_home()
class Farm(pyglet.window.Window): MIN_BUGS = 2 def __init__(self): super(Farm, self).__init__(1500,1000) # Set up window self.keyboard = pyglet.window.key.KeyStateHandler() self.push_handlers(self.keyboard) # Set up environment self.living_bugs = [] self.mating_bugs = set() self.world = World(self.width, self.height) print 'width: ', self.width print 'height: ', self.height # Populate world bug = Bug(self.world, self.mating_bugs) self.living_bugs.append(bug) def run(self): # Start simulation pyglet.clock.schedule_interval(self.update,1/20.0) pyglet.app.run() def update(self, dummy): if self.keyboard[pyglet.window.key.H]: print 'Help requested... sorry.' if self.keyboard[pyglet.window.key.SPACE]: self.living_bugs.append(Bug(self.world, self.mating_bugs)) def on_draw(self): #gl.glClearColor(0, 0.3, 0.5, 0) gl.glClear(gl.GL_COLOR_BUFFER_BIT) # Grow (TODO) self.world.update() self.world.draw() # Update and kill random.shuffle(self.living_bugs) for bug in self.living_bugs: bug.update() if not bug.isAlive: self.living_bugs.remove(bug) # Repopulate while len(self.mating_bugs) > 1: print 'MATING!' bug1 = self.mating_bugs.pop() bug2 = self.mating_bugs.pop() bug1.mate() bug2.mate() self.living_bugs.append(Bug(self.world, self.mating_bugs, bug1, bug2)) while len(self.living_bugs) < self.MIN_BUGS: self.living_bugs.append(Bug(self.world, self.mating_bugs)) # Draw for bug in self.living_bugs: bug.draw()
def __init__(self, settings): BaseWorld.__init__(self, settings)