コード例 #1
0
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
コード例 #2
0
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