-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
68 lines (36 loc) · 1.59 KB
/
main.py
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
import numpy as np
from constants import Constant
from genetic import Genetic
from matplotlib import pyplot
inputs = Constant.EQUATION_INPUTS
length = len(inputs)
solution_count = Constant.SOLUTION_COUNT
population_size = (solution_count, length) #(8, 6)
best_outputs = []
if __name__ == "__main__":
genetic = Genetic()
#Make initial population 8 chromosomes and 6 genes
init_population = np.random.uniform(-4, 4, size=population_size)
print(init_population)
for generation in range(Constant.GENERATION_COUNT):
print("Generation : {}" . format(generation))
fitness = genetic.calcFitness(inputs, init_population)
print("fitness", fitness)
best_outputs.append(np.max(fitness))
print(best_outputs)
parents = genetic.getMating(init_population, Constant.PARENT_MATING_COUNT)
crossover = genetic.crossover(offspring_size=(population_size[0] - parents.shape[0], length))
print("Crossover", crossover)
mutation = genetic.mutation()
print("Mutation", mutation)
# Creating the init population based on the parents and offspring.
init_population[0:parents.shape[0], :] = parents
init_population[parents.shape[0]:, :] = mutation
fitness = genetic.calcFitness(inputs, init_population)
best_match_idx = np.where(fitness == np.max(fitness))
print("Best solution : ", init_population[best_match_idx, :])
print("Best solution fitness : ", fitness[best_match_idx])
pyplot.plot(best_outputs)
pyplot.xlabel("Iteration")
pyplot.ylabel("Fitness")
pyplot.show()