Exemplo n.º 1
0
def evaluate_individual(i, error_function, rand_gen, argmap = {'reuse-errors' : True, 
                                                             'print-history' : False,
                                                             'total-error-method' : 'sum',
                                                             'normalization' : 'none',
                                                             'max-error' : 1000}):
    '''
    Returns the given individual with errors, total-errors, and hah-errors,
    computing them if necessary.
    '''
    p = i['program']
    if i['errors'] !=None and argmap['reuse-errors']:
        e = i['errors']
    else:
        Pysh.globals.evaluations_count += 1
        e = normalize_errors(error_function(p), argmap['normalization'], argmap['max-error'])
    if i['total-error']!=None and argmap['reuse-errors']:
        te = i['total-error']
    else:
        te = compute_total_error(e)
    if argmap['total-error-method'] is 'sum':
        we = None
    elif argmap['total-error-method'] is 'ifs':
        we = None
    elif argmap['total-error-method'] is 'hah':
        we = compute_hah_error(i['errors'])
    elif argmap['total-error-method'] is 'rmse':
        we = compute_root_mean_square_error(i['errors    '])
    
    if argmap['print-history']:
        his = te + i['history']
    else:
        his = i['history']
        
    return individual.make_induvidual(program =p, genome = i['genome'], errors =e, total_error =te, weighted_error =we,
                                      history = his, ancestors = i['ancestors'], parent = i['parent'])
Exemplo n.º 2
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.º 3
0
def auto_simplify_from_program(p, error_function, steps, printBool, progress_interval):
    errs = error_function(p)
    terrs = 0
    for e in errs:
        terrs += e
    return auto_simplify(individual.make_induvidual(program = p, errors = errs, total_error = terrs),
                         error_function, steps, printBool, progress_interval)