def run(): ptA = HeadlessPetriDish( size=(400, 300), nametag="A", population_size=50, robot_data={ 'max_neighbours': 5, 'max_velocity': 4.0, 'sense_radius': 50.0, 'diffuse_rate': 10, 'act_rate': 1, 'nn_info': { 'hidden_layers': [], 'activation': lambda x: x, # RELU: lambda x : np.maximum(x, 0.0) # TANH: np.tanh # LINEAR: lambda x: x 'modular': True, #TODO: discuss with group why weights should be modular. 'integerWeights': True } }, htg_method=lambda x: selectGSCrandLength(x, min_length=2, max_length=4 ), task=TaskMaxNeighbors(min_fitness=0, max_fitness=5), fitness_selection_method=lambda x: selectProportional(x), mutate_method=lambda x: intMutate(x, 0.1), save_interval=10, save_filename="./outputs/headless1.csv", max_iters=10000) #ptA = PetriDish() ptA.run()
def run(): ptA = PetriDish( size = (400, 300), nametag = "A", population_size=50, robot_data = { 'max_neighbours' : 5, 'max_velocity' : 4.0, 'sense_radius': 50.0, 'diffuse_rate': 10, 'act_rate': 1, 'nn_info': { 'hidden_layers' : [], 'activation' : lambda x : x, # RELU: lambda x : np.maximum(x, 0.0) # TANH: np.tanh # LINEAR: lambda x: x 'modular' : True #TODO: discuss with group why weights should be modular. } }, htg_method = lambda x: selectGSCrandLength(x, min_length=2, max_length=4), task = TaskMaxNeighbors(min_fitness=0, max_fitness=5), fitness_selection_method = lambda x: selectProportional(x), mutate_method = lambda x: flatMutate(x, 0.1), visuals = { 'colour_A' : [100.0, 100.0, 100.0], 'colour_B' : [0.0, 225.0, 0.0], 'background' : [250.0, 220.0, 180.0], 'draw_radius' : False, 'draw_transfer' : True }, save_interval=1000, record_interval = 10, interaction_mode = False, ) #ptA = PetriDish() ptA.run()
from matplotlib import animation from IPython.display import HTML import numpy as np import pygame pygame.init() size = (400, 300) screen = pygame.display.set_mode(size) pygame.display.set_caption("Petri Dish - A") num_neighbours = 5 mng = HtgManager( pop_size=100, htg_method=lambda x: selectGSCrandLength(x, min_length=2, max_length=4), boundary_x=size[0], boundary_y=size[1], max_vel=4.0, sense_radius=50.0, num_neighbours=num_neighbours, diffuse_rate=10, act_rate=1) preset_colors = [] preset_colors.append([100.0, 100.0, 100.0]) rt = 255.0 / num_neighbours if num_neighbours > 1: for i in range(num_neighbours): preset_colors.append([0.0, rt + rt * i, 0.0]) print(preset_colors)
def __init__( self, size=(400, 300), nametag="A", population_size=50, robot_data={ 'max_neighbours': 5, 'max_velocity': 4.0, 'sense_radius': 50.0, 'diffuse_rate': 10, # TODO convert from timesteps to secs. 'act_rate': 1, # TODO convert from timesteps to secs. 'nn_info': { 'hidden_layers': [], 'activation': lambda x: x, 'modular': True } }, htg_method=lambda x: selectGSCrandLength(x, min_length=2, max_length=4 ), task=TaskClump(0, 100), fitness_selection_method=selectAll, mutate_method=lambda x: flatMutate(x, 0.1), visuals={ 'colour_A': [100.0, 100.0, 100.0], 'colour_B': [0.0, 225.0, 0.0], 'background': [250.0, 220.0, 180.0], 'draw_radius': False, 'draw_transfer': True }, save_interval=-1, record_interval=10, interaction_mode=False, ): """ size -- dimension of the screen: (float, float) (default: (400, 300)) nametag -- name of the experiment: string (default: "A") population_size -- number of individuals: int (default: 50) robot_data -- dict. of robot params: dictionary (default: { 'max_neighbours' : 5, -- max. number of neighbours a robot can see. 'max_velocity' : 4.0, -- max. velocity a robot can reach. 'sense_radius': 50.0, -- radius of sensory detection. 'diffuse_rate': 10, -- ticks per gene diffusion. 'act_rate': 1, -- ticks per robot action. nn_info : dict default: { 'hidden_layers' : [], -- number of hidden layers (input and output are defined by default). 'activation' : lambda x : x, -- activation function (e.g. np.tanh, ReLU, or just linear). 'modular' : True -- whether the weights are the same for each robots sensor channel. } are the same. Generally, this makes sense to keep true. }) htg_method -- method describing how genes are selected. visuals -- colours of graphics: dictionary (default: { 'colour_A' : [100.0, 100.0, 100.0], -- colour when there are no neighbours. 'colour_B' : [0.0, 225.0, 0.0], -- colour when there are max. neighbours. 'background' : [250.0, 220.0, 180.0] -- colour of background. 'draw_radius' : False -- whether to draw the visual radius of a robot. 'draw_transfer' : True -- whether to draw the gene transfer lines. }) save_interval -- how often (in frames) should the data of the population be saved. -1 means never: int (default: -1) record_interval -- how ofter (in frames) to record experiment data. If save_interval is -1, nothing is recorded: int (default: 10) interaction_mode -- whether the user can hover over a robot to have its gene data output on the console: bool (default: False) """ pygame.init() self.screen = pygame.display.set_mode(size) pygame.display.set_caption("Petri Dish - " + nametag) self.save_filename = 'outputs/' + 'population_data_' + nametag + '.txt' self.save_interval = save_interval self.record_interval = record_interval self.interaction_mode = interaction_mode self.mng = HtgManager( pop_size=population_size, htg_method=htg_method, task=task, fitness_selection_method=fitness_selection_method, mutate_method=lambda x: flatMutate(x, 0.1), boundary_x=size[0], boundary_y=size[1], max_vel=robot_data['max_velocity'], sense_radius=robot_data['sense_radius'], num_neighbours=robot_data['max_neighbours'], diffuse_rate=robot_data['diffuse_rate'], act_rate=robot_data['act_rate'], nn_info=robot_data['nn_info']) self.visuals = visuals self.preset_colors = [] self.preset_colors.append(visuals['colour_A']) #rt = 225.0 / robot_data['max_neighbours'] + 100.0 rt = [(b - a) / robot_data['max_neighbours'] for a, b in zip(visuals['colour_A'], visuals['colour_B'])] if robot_data['max_neighbours'] > 1: for i in range(1, robot_data['max_neighbours'] + 1): #self.preset_colors.append([0.0, rt + rt*i, 0.0]) self.preset_colors.append([ visuals['colour_A'][0] + rt[0] * i, visuals['colour_A'][1] + rt[1] * i, visuals['colour_A'][2] + rt[2] * i ]) print(self.preset_colors) self.mng.update() x, y, a, c = self.mng.getPopState() self.fps = 60 self.clock = pygame.time.Clock() ### robot statistics display self.robot_data = DataDisplay( { 'ID': 10, 'POS': (10, 10), #'GENE' : [1, 2, 3, 4, 5] }, position=[5.0, 5.0], color=[0.0, 0.0, 0.0], size=12, ) self.pause_button = Button('||', [15.0, size[1] - 15.0], [100.0, 100.0, 100.0], 10) self.up_fps = Button('>', [30.0, size[1] - 15.0], [100.0, 100.0, 100.0], 10, toggle=False) self.down_fps = Button('<', [2.0, size[1] - 15.0], [100.0, 100.0, 100.0], 10, toggle=False) self.color_button = Button('=', [size[0] - 30.0, 7.0], [100.0, 100.0, 0.0], 15) self.color_data = DataDisplay({'VIS': 'neighb.'}, position=[size[0] - 55.0, 0.0], color=[100.0, 100.0, 0.0], size=14) self.fps_data = DataDisplay({ 'FPS': 60, 'PAUSE': 'OFF' }, position=[45.0, size[1] - 22.0], color=[100.0, 100.0, 100.0], size=12) #self.pause_button = DataDisplay( # { # '|' : '|' # }, # position=[0.0, size[1] - 20], # color=[255.0, 255.0, 255.0], # size = 20 # ) # data recorder self.data_recorder = DataRecorder( gene_size=np.shape(self.mng.pop[0].getGenotype())[0]) self.iters = 0
def __init__( self, size=(400, 300), nametag="A", population_size=50, robot_data={ 'max_neighbours': 5, 'max_velocity': 4.0, 'sense_radius': 50.0, 'diffuse_rate': 10, # TODO convert from timesteps to secs. 'act_rate': 1, # TODO convert from timesteps to secs. 'nn_info': { 'hidden_layers': [], 'activation': lambda x: x, 'modular': True } }, htg_method=lambda x: selectGSCrandLength(x, min_length=2, max_length=4 ), task=TaskClump(0, 100), fitness_selection_method=selectAll, mutate_method=lambda x: flatMutate(x, 0.1), visuals={ 'colour_A': [100.0, 100.0, 100.0], 'colour_B': [0.0, 225.0, 0.0], 'background': [250.0, 220.0, 180.0], 'draw_radius': False, 'draw_transfer': True }, save_interval=-1, record_interval=10, max_iters=1000, save_filename='outputs/' + 'headless_run.csv', ): """ size -- dimension of the screen: (float, float) (default: (400, 300)) nametag -- name of the experiment: string (default: "A") population_size -- number of individuals: int (default: 50) robot_data -- dict. of robot params: dictionary (default: { 'max_neighbours' : 5, -- max. number of neighbours a robot can see. 'max_velocity' : 4.0, -- max. velocity a robot can reach. 'sense_radius': 50.0, -- radius of sensory detection. 'diffuse_rate': 10, -- ticks per gene diffusion. 'act_rate': 1, -- ticks per robot action. nn_info : dict default: { 'hidden_layers' : [], -- number of hidden layers (input and output are defined by default). 'activation' : lambda x : x, -- activation function (e.g. np.tanh, ReLU, or just linear). 'modular' : True -- whether the weights are the same for each robots sensor channel. } are the same. Generally, this makes sense to keep true. }) htg_method -- method describing how genes are selected. visuals -- colours of graphics: dictionary (default: { 'colour_A' : [100.0, 100.0, 100.0], -- colour when there are no neighbours. 'colour_B' : [0.0, 225.0, 0.0], -- colour when there are max. neighbours. 'background' : [250.0, 220.0, 180.0] -- colour of background. 'draw_radius' : False -- whether to draw the visual radius of a robot. 'draw_transfer' : True -- whether to draw the gene transfer lines. }) save_interval -- how often (in frames) should the data of the population be saved. -1 means never: int (default: -1) record_interval -- how ofter (in frames) to record experiment data. If save_interval is -1, nothing is recorded: int (default: 10) interaction_mode -- whether the user can hover over a robot to have its gene data output on the console: bool (default: False) """ self.save_filename = save_filename self.save_interval = save_interval self.max_iters = max_iters self.mng = HtgManager( pop_size=population_size, htg_method=htg_method, task=task, fitness_selection_method=fitness_selection_method, mutate_method=lambda x: flatMutate(x, 0.1), boundary_x=size[0], boundary_y=size[1], max_vel=robot_data['max_velocity'], sense_radius=robot_data['sense_radius'], num_neighbours=robot_data['max_neighbours'], diffuse_rate=robot_data['diffuse_rate'], act_rate=robot_data['act_rate'], nn_info=robot_data['nn_info']) self.mng.update() x, y, a, c = self.mng.getPopState() self.iters = 0