-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
71 lines (56 loc) · 2.26 KB
/
main.py
File metadata and controls
71 lines (56 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import numpy as np
from population import Population
from ga import GA
import matplotlib.pyplot as plt
import time
GENERATIONS = 500
MUTATION_RATE = 0.1
CROSSOVER_RATE = 0.7
POPULATION_SIZE = 8
OPENAI_EVALUATIONS = 500
def populate_fitness(population):
'''Calculate the fitness for all members of the population'''
for ind in population:
ind.fitness = genetic_algorithm.calculate_fitness(ind.weights)
def get_best_individue(population):
'''Get the best individue and its fitness'''
return population[np.argmax([x.fitness for x in population])]
def run_best(file):
weights = np.load('best/' + str(file) + '.npy', allow_pickle=True)
genetic_algorithm.openAI.run_result(weights)
# Create First Population
population = Population(POPULATION_SIZE)
genetic_algorithm = GA(MUTATION_RATE, CROSSOVER_RATE, OPENAI_EVALUATIONS)
# run_best('1575216646.5449824np_294')
populate_fitness(population.get())
best_individue = None
best_individues = []
mean = []
for i in range(1, GENERATIONS):
best_individue = get_best_individue(population.get())
best_individues.append(best_individue)
new_poulation = genetic_algorithm.generation(population.get())
new_poulation.get()[0] = best_individue
population = new_poulation
populate_fitness(population.get())
print(i,best_individue.fitness)
if best_individue.fitness == OPENAI_EVALUATIONS:
np.save('best/' + str(time.time()) + 'np_' + '.npy', best_individue.weights)
genetic_algorithm.openAI.run_result(best_individue.weights)
break
# start = time.time()
# genetic_algorithm.openAI.run_result(best_individue.weights)
# end = time.time()
# print(end - start, ' seconds')
# Plot evaluation data
# if best_individue.fitness > 300:
# # plt.figure()
# # plt.axhline(y=OPENAI_EVALUATIONS, color='r', linestyle='dashed')
# # plt.plot([x.fitness for x in best_individues], color='b', linestyle='solid')
# # # plt.plot(mean, color='g', linestyle='dotted')
# #
# # plt.ylabel('Fitness Value')
# # plt.xlabel('Generation')
# # plt.legend(['Maximum', 'Best individue'])
# # plt.savefig('imgs/' + str(time.time()) + '_' + str(kkk) + '.png')
# plt.show(block=False)