def run(method, level, generations=500, popsize=500, visualize_individual=None): shape = (3,3) task = TargetWeightsTask(substrate_shape=shape, noise=level, fitnessmeasure='sqerr') substrate = Substrate() substrate.add_nodes(shape, 'l') substrate.add_connections('l', 'l') if method == 'hyperneat': geno = lambda: NEATGenotype(feedforward=True, inputs=len(shape)*2, weight_range=(-3.0, 3.0), prob_add_conn=0.3, prob_add_node=0.03, types=['sin', 'linear', 'gauss', 'sigmoid', 'abs']) pop = NEATPopulation(geno, popsize=popsize, target_species=8) developer = HyperNEATDeveloper(substrate=substrate, add_deltas=False, sandwich=False) elif method == '0hn': t = [(i, 4) for i in range(4)] geno = lambda: NEATGenotype(feedforward=True, inputs=len(shape)*2, weight_range=(-3.0, 3.0), prob_add_conn=0.0, prob_add_node=0.00, topology=t, types=['sin', 'linear', 'gauss', 'sigmoid', 'abs']) pop = NEATPopulation(geno, popsize=popsize, target_species=8) developer = HyperNEATDeveloper(substrate=substrate, add_deltas=False, sandwich=False) elif method == 'wavelet': geno = lambda: WaveletGenotype(inputs=len(shape)*2) pop = SimplePopulation(geno, popsize=popsize) developer = WaveletDeveloper(substrate=substrate, add_deltas=False, sandwich=False) results = pop.epoch(generations=generations, evaluator=partial(evaluate, task=task, developer=developer) ) return results
def run(method, level, generations=500, popsize=500, visualize_individual=None): shape = (3, 3) task = TargetWeightsTask(substrate_shape=shape, noise=level, fitnessmeasure='sqerr') substrate = Substrate() substrate.add_nodes(shape, 'l') substrate.add_connections('l', 'l') if method == 'hyperneat': geno = lambda: NEATGenotype( feedforward=True, inputs=len(shape) * 2, weight_range=(-3.0, 3.0), prob_add_conn=0.3, prob_add_node=0.03, types=['sin', 'linear', 'gauss', 'sigmoid', 'abs']) pop = NEATPopulation(geno, popsize=popsize, target_species=8) developer = HyperNEATDeveloper(substrate=substrate, add_deltas=False, sandwich=False) elif method == '0hn': t = [(i, 4) for i in range(4)] geno = lambda: NEATGenotype( feedforward=True, inputs=len(shape) * 2, weight_range=(-3.0, 3.0), prob_add_conn=0.0, prob_add_node=0.00, topology=t, types=['sin', 'linear', 'gauss', 'sigmoid', 'abs']) pop = NEATPopulation(geno, popsize=popsize, target_species=8) developer = HyperNEATDeveloper(substrate=substrate, add_deltas=False, sandwich=False) elif method == 'wavelet': geno = lambda: WaveletGenotype(inputs=len(shape) * 2) pop = SimplePopulation(geno, popsize=popsize) developer = WaveletDeveloper(substrate=substrate, add_deltas=False, sandwich=False) results = pop.epoch(generations=generations, evaluator=partial(evaluate, task=task, developer=developer)) return results
def run(method, setup, generations=250, popsize=100): # Create task and genotype->phenotype converter size = 11 task_kwds = dict(size=size) if setup == 'big-little': task_kwds['targetshape'] = ShapeDiscriminationTask.makeshape('box', size//3) task_kwds['distractorshapes'] = [ShapeDiscriminationTask.makeshape('box', 1)] elif setup == 'triup-down': task_kwds['targetshape'] = np.triu(np.ones((size//3, size//3))) task_kwds['distractorshapes'] = [np.tril(np.ones((size//3, size//3)))] task = ShapeDiscriminationTask(**task_kwds) substrate = Substrate() substrate.add_nodes((size, size), 'l') substrate.add_connections('l', 'l') if method == 'wavelet': num_inputs = 6 if deltas else 4 geno = lambda: WaveletGenotype(inputs=num_inputs) pop = SimplePopulation(geno, popsize=popsize) developer = WaveletDeveloper(substrate=substrate, add_deltas=True, sandwich=True) else: geno_kwds = dict(feedforward=True, inputs=6, weight_range=(-3.0, 3.0), prob_add_conn=0.1, prob_add_node=0.03, bias_as_node=False, types=['sin', 'bound', 'linear', 'gauss', 'sigmoid', 'abs']) if method == 'nhn': pass elif method == '0hnmax': geno_kwds['max_nodes'] = 7 elif method == '1hnmax': geno_kwds['max_nodes'] = 8 geno = lambda: NEATGenotype(**geno_kwds) pop = NEATPopulation(geno, popsize=popsize, target_species=8) developer = HyperNEATDeveloper(substrate=substrate, sandwich=True, add_deltas=True, node_type='tanh') # Run and save results results = pop.epoch(generations=generations, evaluator=partial(evaluate, task=task, developer=developer), solution=partial(solve, task=task, developer=developer), ) return results
def run(method, setup, generations=15, popsize=10): """main function that drives the evolution""" # the following 'task_kwds' and 'task' variables should be ignored as they come from an example from the framework - I had no time to remove them yet. task_kwds = dict(field='eight', observation='eight_striped', max_steps=3000, friction_scale=0.1, damping=0.9, motor_torque=3, check_coverage=True, flush_each_step=False, initial_pos=(17, 256, np.pi*0.5)) #TODO: remove task = DetectorTask(**task_kwds) #TODO: remove # Detector has a specific topology: input 21x21; output 1x0 substrate = Substrate() substrate.add_nodes([(r, theta) for r in np.linspace(-10,10,21) for theta in np.linspace(-10, 10, 21)], 'input', is_input=True) substrate.add_nodes([(r, theta) for r in np.linspace(0,0,1) for theta in np.linspace(0, 0, 1)], 'output') substrate.add_connections('input', 'output', -1) # evolutionary parameters geno_kwds = dict(feedforward=True, inputs=441, outputs=1, max_depth=3, max_nodes=MAXIMUM_NODES, weight_range=(-3.0, 3.0), prob_add_conn=0.3, prob_add_node=0.1, bias_as_node=False, types=['sigmoid']) geno = lambda: NEATGenotype(**geno_kwds) pop = NEATPopulation(geno, popsize=popsize, target_species=8) # sigmoid activation is only used for the generated ANN, since 'hnn' package can has only sigmoid activations developer = HyperNEATDeveloper(substrate=substrate, add_deltas=False, sandwich=False, node_type='sigmoid') results = pop.epoch(generations=generations, evaluator=partial(evaluate, task=task, developer=developer), solution=partial(solve, task=task, developer=developer), ) # output best solutions from every generation into a file 'best_solution_N', where 'N' is the ID of the detector fitnesses = list() fifo = open(os.path.join(os.path.dirname(__file__), '../../best_solution_N'), 'a+') for champion in results['champions']: fitnesses.append(champion.stats['fitness']) phenotype = developer.convert(champion) # option to visualise the detector ANN. Use some sort of counter so that the images won't be overwritten #dir = os.path.dirname('../../visual_N.png') #if not os.path.exists(dir): # os.makedirs(dir) #phenotype.visualize('../../visual_N.png', inputs=441, outputs=1) rest = MAXIMUM_NODES - len((champion.get_network_data()[1])[1:]) rest_array = np.linspace(4., 4., rest) for idx, node_type in enumerate((champion.get_network_data()[1])[1:]): if (idx == len((champion.get_network_data()[1])[1:]) - 2): nodes_types_indexes.extend(rest_array) try: nodes_types_indexes.append(float(['sin', 'bound', 'linear', 'gauss', 'sigmoid', 'abs'].index(node_type))) except NameError: print "not defined!" fifo.write('fitness: '+str(champion.stats['fitness'])+' || ' + ' '.join(map(str, node_types)) + ' '+' '.join(map(str, phenotype.get_connectivity_matrix()))+'\n') fifo.close() # Visualize evolution in a graph import matplotlib.pyplot as plt from matplotlib.ticker import MaxNLocator plt.figure() x = range(len(results['champions'])) y = np.asarray(fitnesses) xa = plt.gca().get_xaxis() xa.set_major_locator(MaxNLocator(integer=True)) plt.plot(x, y) plt.axis('on') plt.savefig(os.path.join(os.getcwd(), '../../fitness_evolution_N'), bbox_inches='tight', pad_inches=0) plt.close() return results
def run(method, setup, generations=100, popsize=100): """ Use hyperneat for a walking gait task """ # Create task and genotype->phenotype converter if setup == 'easy': task_kwds = dict(field='eight', observation='eight', max_steps=3000, friction_scale=0.3, damping=0.3, motor_torque=10, check_coverage=False, flush_each_step=False, initial_pos=(282, 300, np.pi*0.35)) elif setup == 'hard': task_kwds = dict(field='eight', observation='eight_striped', max_steps=3000, friction_scale=0.3, damping=0.3, motor_torque=10, check_coverage=False, flush_each_step=True, force_global=True, initial_pos=(282, 300, np.pi*0.35)) elif setup == 'force': task_kwds = dict(field='eight', observation='eight', max_steps=3000, friction_scale=0.1, damping=0.9, motor_torque=3, check_coverage=True, flush_each_step=True, force_global=True, initial_pos=(17, 256, np.pi*0.5)) elif setup == 'prop': task_kwds = dict(field='eight', observation='eight_striped', max_steps=3000, friction_scale=0.3, damping=0.3, motor_torque=10, check_coverage=False, flush_each_step=False, initial_pos=(282, 300, np.pi*0.35)) elif setup == 'cover': task_kwds = dict(field='eight', observation='eight_striped', max_steps=3000, friction_scale=0.1, damping=0.9, motor_torque=3, check_coverage=True, flush_each_step=False, initial_pos=(17, 256, np.pi*0.5)) task = LineFollowingTask(**task_kwds) # The line following experiment has quite a specific topology for its network: substrate = Substrate() substrate.add_nodes([(0,0)], 'bias') substrate.add_nodes([(r, theta) for r in np.linspace(0,1,3) for theta in np.linspace(-1, 1, 5)], 'input') substrate.add_nodes([(r, theta) for r in np.linspace(0,1,3) for theta in np.linspace(-1, 1, 3)], 'layer') substrate.add_connections('input', 'layer',-1) substrate.add_connections('bias', 'layer', -2) substrate.add_connections('layer', 'layer',-3) if method == 'wvl': geno = lambda: WaveletGenotype(inputs=4, layers=3) pop = SimplePopulation(geno, popsize=popsize) developer = WaveletDeveloper(substrate=substrate, add_deltas=False, sandwich=False, node_type='tanh') else: geno_kwds = dict(feedforward=True, inputs=4, outputs=3, weight_range=(-3.0, 3.0), prob_add_conn=0.1, prob_add_node=0.03, bias_as_node=False, types=['sin', 'bound', 'linear', 'gauss', 'sigmoid', 'abs']) if method == 'nhn': pass elif method == '0hnmax': geno_kwds['max_nodes'] = 7 elif method == '1hnmax': geno_kwds['max_nodes'] = 8 geno = lambda: NEATGenotype(**geno_kwds) pop = NEATPopulation(geno, popsize=popsize, target_species=8) developer = HyperNEATDeveloper(substrate=substrate, add_deltas=False, sandwich=False, node_type='tanh') results = pop.epoch(generations=generations, evaluator=partial(evaluate, task=task, developer=developer), solution=partial(solve, task=task, developer=developer), ) return results
def run(method, splits, generations=500, popsize=500): complexity = 'half' splits = int(splits) funcs = [] if complexity in ['half', 'flat', 'slope']: funcs.append((True, np.random.random() * 6. - 3)) for num in range(splits): axis = random_direction_vector() offset = np.random.random() - 0.2 where = partial(area, axis=axis, offset=offset) if complexity == 'half': xs = 0 if num % 2 == 0 else 1 mp = 1 if (num//2) % 2 == 0 else -1 if num < 2: d = 0 elif num < 2 + 4: d = 0.5 elif num < 2 + 4 + 4: d = 0.25 elif num < 2 + 4 + 4 + 4: d = 0.75 where = partial(split, axis=xs, flip=mp, distance=d) what = lambda c, v: v + np.random.random() * 6. - 3 funcs.append((where, what)) task = TargetWeightsTask(substrate_shape=(8,), funcs=funcs, fitnessmeasure='sqerr', uniquefy=True) substrate = Substrate() substrate.add_nodes((8,), 'l') substrate.add_connections('l', 'l') if method == 'hyperneat': geno = lambda: NEATGenotype(feedforward=True, inputs=2, weight_range=(-3.0, 3.0), prob_add_conn=0.3, prob_add_node=0.03, types=['sin', 'ident', 'gauss', 'sigmoid', 'abs']) pop = NEATPopulation(geno, popsize=popsize, target_species=8) developer = HyperNEATDeveloper(substrate=substrate, add_deltas=False, sandwich=False) elif method == '0hnmax': geno = lambda: NEATGenotype(feedforward=True, inputs=2, weight_range=(-3.0, 3.0), max_nodes=3, types=['sin', 'ident', 'gauss', 'sigmoid', 'abs']) pop = NEATPopulation(geno, popsize=popsize, target_species=8) developer = HyperNEATDeveloper(substrate=substrate, add_deltas=False, sandwich=False) elif method == 'wavelet': geno = lambda: WaveletGenotype(inputs=2) pop = SimplePopulation(geno, popsize=popsize) developer = WaveletDeveloper(substrate=substrate, add_deltas=False, sandwich=False) results = pop.epoch(generations=generations, evaluator=partial(evaluate, task=task, developer=developer), ) return results
def run(method, splits, generations=500, popsize=500): complexity = 'half' splits = int(splits) funcs = [] if complexity in ['half', 'flat', 'slope']: funcs.append((True, np.random.random() * 6. - 3)) for num in range(splits): axis = random_direction_vector() offset = np.random.random() - 0.2 where = partial(area, axis=axis, offset=offset) if complexity == 'half': xs = 0 if num % 2 == 0 else 1 mp = 1 if (num // 2) % 2 == 0 else -1 if num < 2: d = 0 elif num < 2 + 4: d = 0.5 elif num < 2 + 4 + 4: d = 0.25 elif num < 2 + 4 + 4 + 4: d = 0.75 where = partial(split, axis=xs, flip=mp, distance=d) what = lambda c, v: v + np.random.random() * 6. - 3 funcs.append((where, what)) task = TargetWeightsTask(substrate_shape=(8, ), funcs=funcs, fitnessmeasure='sqerr', uniquefy=True) substrate = Substrate() substrate.add_nodes((8, ), 'l') substrate.add_connections('l', 'l') if method == 'hyperneat': geno = lambda: NEATGenotype( feedforward=True, inputs=2, weight_range=(-3.0, 3.0), prob_add_conn=0.3, prob_add_node=0.03, types=['sin', 'ident', 'gauss', 'sigmoid', 'abs']) pop = NEATPopulation(geno, popsize=popsize, target_species=8) developer = HyperNEATDeveloper(substrate=substrate, add_deltas=False, sandwich=False) elif method == '0hnmax': geno = lambda: NEATGenotype( feedforward=True, inputs=2, weight_range=(-3.0, 3.0), max_nodes=3, types=['sin', 'ident', 'gauss', 'sigmoid', 'abs']) pop = NEATPopulation(geno, popsize=popsize, target_species=8) developer = HyperNEATDeveloper(substrate=substrate, add_deltas=False, sandwich=False) elif method == 'wavelet': geno = lambda: WaveletGenotype(inputs=2) pop = SimplePopulation(geno, popsize=popsize) developer = WaveletDeveloper(substrate=substrate, add_deltas=False, sandwich=False) results = pop.epoch( generations=generations, evaluator=partial(evaluate, task=task, developer=developer), ) return results