Ejemplo n.º 1
0
def find_best_parameters(pop_size, max_gen):
    possible_parameters = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
    possible_variances = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
    best_variance = 0
    best_mutation = 0
    best_crossover = 0
    best_average_elite_solution = 0
    populations = initial_populations()
    for p_mutation in possible_parameters:
        for p_crossover in possible_parameters:
            for variance in possible_variances:
                print('Trying: ' + str(variance) + ', ' +
                      str(p_crossover) + ', ' +
                      str(p_mutation))
                elite_solutions = []
                for i in range(3):
                    solution, elite = GA_T(populations[i], pop_size, max_gen,
                                           p_crossover, p_mutation, variance)
                    elite_solutions.append(bump(elite))
                average_elite_solution = mean(elite_solutions)
                print('Average elite solution: ' + str(average_elite_solution))
                if average_elite_solution > best_average_elite_solution:
                    print('Found new best values!')
                    best_average_elite_solution = average_elite_solution
                    best_variance = variance
                    best_crossover = p_crossover
                    best_mutation = p_mutation
    print('We found some great parameters!!')
    print('Variance: ' + str(best_variance))
    print('Probability Crossover: ' + str(best_crossover))
    print('Probability Mutation: ' + str(best_mutation))
Ejemplo n.º 2
0
    print('We found some great parameters!!')
    print('Variance: ' + str(best_variance))
    print('Probability Crossover: ' + str(best_crossover))
    print('Probability Mutation: ' + str(best_mutation))


pop_size = 50
max_gen = 200
p_crossover = 0.7
p_mutation = 0.1
variance = 2

elite_solutions = []
elite_solution_vals = []
all_solutions = []
for population in initial_populations():
    solution, elite = GA_T(population, pop_size, max_gen,
                           p_crossover, p_mutation, variance)
    elite_solutions.append(elite)
    elite_solution_vals.append(bump(elite))
    all_solutions.append(solution)

with open("problem3_results.txt", 'w') as output:
    output.write('Average: ' + str(mean(elite_solution_vals)))
    output.write('\nStandard deviation: ' + str(std(elite_solution_vals)))
    best_value = max(elite_solution_vals)
    output.write('\nBest value: ' + str(best_value))
    output.write('\nBest member: ' +
                 str(elite_solutions[elite_solution_vals.index(best_value)]))
    worst_value = min(elite_solution_vals)
    output.write('Worst value: ' + str(worst_value))