def initialize(config): if not config['color']: config['state'] = np.zeros((config['width'], config['height'])) else: config['state'] = np.zeros((config['width'], config['height'], 3)) if not config['start']: if not config['color']: config['start'] = utils.spawn_random_point( np.zeros((config['width'], config['height']))) else: config['start'] = utils.spawn_random_point( np.zeros((config['width'], config['height'], 3))) if not config['target_start']: if not config['color']: config['target_start'] = utils.spawn_random_point( np.zeros((config['width'], config['height']))) else: config['target_start'] = utils.spawn_random_point( np.zeros((config['width'], config['height'], 3))) state = config['state'] pt = config['start'] if config['color']: state[pt[0], pt[1], :] = [1, 0, 0] else: state[pt[0], pt[1]] = 1 config['state'] = state return config
def main(): width = 250 height = 250 field = np.zeros((width, height, 3)) n_rabbits = 100 n_predators = 1 start = [] rabbits = [] # Populate the field with rabbits for rabbit in range(n_rabbits): [x, y] = utils.spawn_random_point(np.zeros((width, height))) field[x, y, :] = [1, 1, 1] rabbits.append([x, y]) # Optionally, give predators a start: if n_predators == 1: start = [int(width / 2), int(height / 2)] field[start[0] - 2:start[0], start[1] - 2:start[1], :] = [1, 0, 0] # If not assign predator random start if len(start) <= 1: start = utils.spawn_random_point(field) f = plt.figure() # Determine direction to head, and define # how many steps before checking fov simulation = [] [pxs, pys] = np.array(field[:, :, 1]).nonzero() steps = 0 while n_rabbits > (n_rabbits / 2) and steps < 10: for position in rabbits: directions = { 1: [position[0] - 1, position[1] - 1], 2: [position[0], position[1] - 1], 3: [position[0] + 1, position[1] - 1], 4: [position[0] - 1, position[1]], 5: position, 6: [position[0] + 1, position[1]], 7: [position[0] - 1, position[1] + 1], 8: [position[0], position[1] + 1], 9: [position[0] + 1, position[1] + 1] } steps += 1 simulation.append([plt.imshow(field)]) a = animation.ArtistAnimation(f, simulation, interval=190, blit=True, repeat_delay=900) plt.show()
def initialize(self, config): self.state = config['map'] if 'start' not in config.keys(): self.point = point(utils.spawn_random_point(self.state)) else: self.point = point(config['start']) self.state[self.point.x, self.point.y] = 2
def initialize(config): n_points = config['n_obstacles'] if config['color']: state = utils.add_random_points_color( np.zeros((config['width'], config['height'], 3)), n_points, 'r') if config['start'] == []: start = utils.spawn_random_point( np.zeros((config['width'], config['height']))) state[start[0], start[1], :] = [0, 0, 1] else: state = utils.add_random_points( np.zeros((config['width'], config['height'])), n_points) if config['start'] == []: start = utils.spawn_random_point( np.zeros((config['width'], config['height']))) state[start[0], start[1]] = -1 config['initial_state'] = state if config['start'] == []: config['start'] = np.array( utils.spawn_random_point(config['initial_state'])) config['steps'] = utils.spawn_random_walk(config['start'], config['n_steps']) return high_speed_crawler(config)
def build_map(config): state = np.zeros((config['width'], config['height'])) for point in range(config['n_obstacles']): pt = utils.spawn_random_point(state) state[pt[0], pt[1]] = 1 return state
def init(config): c = config c['state'] = build_map(config) if config['start'] == []: c['start'] = np.array(utils.spawn_random_point(c['initial_state'])) return c