Exemplo n.º 1
0
def auto_simplify(ind, error_function, steps, printBool, progress_interval, maintain_ancestors = False):
    '''
    Auto-simplifies the provided individual.
    '''
    
    if printBool:
        print '\nAuto-simplifying with starting size: ' + str(util.count_points(ind['program']))
    looping = True
    step = 0
    program = ind['program']
    errors = ind['errors']
    total_errors = ind['total-error']
    while looping:
        if printBool and (step >= steps or step % progress_interval == 0):
            print '\nstep: ' + step + '\nprogram: ' + str(program) + '\nerrors: ' + str(errors) + '\ntotal: ' + str(total_errors) + '\nsize: ' + str(util.count_points(program))
        if step >= steps:
            if maintain_ancestors:
                ancestors = [ind['program']] + ind['ancestors']
            else:
                ancestors = ind['ancestors']
            return individual.make_induvidual(program = program, errors = errors, total_errors = total_errors, history = ind['history'], ancestors = ancestors)
        else:
            if random_push.lrand_int(5)< 4:
                p = program
                how_many = random_push.lrand_int(2)+1
                looping2 = True
                while looping2:
                    if how_many == 0:
                        new_program = p
                        looping2 = False
                    else:
                        p = util.remove_code_at_point(p, random_push.lrand_int(util.count_points(p)))
                        how_many -= 1
            point_index = random_push.lrand_int(util.count_points(program))
            point = util.code_at_point(program, point_index)
            if type(point) == list:
                new_program = util.insert_code_at_point(program, point_index, flatten_seqs(point))
            else:
                new_program = program
            new_errors = error_function(new_program)
            new_total_errors = evaluate.compute_total_error(new_errors)
            if new_errors == errors:
                step += 1
                program = new_program
                errors = new_errors
                total_errors = new_total_errors
            else:
                step += 1
Exemplo n.º 2
0
def choose_node_index_by_tournament(tree, node_selection_tournament_size):
    '''
    Returns an index into tree, choosing the largest subtree found in
    a tournament of size node-selection-tournament-size.
    '''
    c = util.count_points(tree)
    tournament_set = []
    for i in range(node_selection_tournament_size):
        point_index = random_push.lrand_int(c)
        subtree_size = util.count_points(util.code_at_point(tree, point_index))
        tournament_set.append({'i' : point_index, 'size' : subtree_size})
    biggest = tournament_set[0]
    for s in tournament_set:
        if biggest['size'] < s['size']:
            biggest = s
    return biggest['i']
Exemplo n.º 3
0
def select_node_index(tree, keys):
    '''
    Returns an index into tree using the node selection method indicated
    by node-selection-method.
    '''
    if keys['node-selection-method'] == 'unbiased':
        return random_push.lrand_int(util.count_points(tree))
    elif keys['node-selection-method'] == 'leaf-probability':
        return choose_node_index_with_leaf_probability(tree, keys['node-selection-leaf-probability'])
    elif keys['node-selection-method'] == 'size-tournament':
        return choose_node_index_by_tournament(tree, keys['node-selection-tournament-size'])
    else:
        print 'ERROR: :node-selection-method set to unrecognized value '