|
1 | 1 | import numpy
|
2 |
| -import GA |
| 2 | +import ga |
3 | 3 |
|
4 | 4 | """
|
5 | 5 | The y=target is to maximize this equation ASAP:
|
|
43 | 43 | for generation in range(num_generations):
|
44 | 44 | print("Generation : ", generation)
|
45 | 45 | # Measuring the fitness of each chromosome in the population.
|
46 |
| - fitness = GA.cal_pop_fitness(equation_inputs, new_population) |
| 46 | + fitness = ga.cal_pop_fitness(equation_inputs, new_population) |
47 | 47 | print("Fitness")
|
48 | 48 | print(fitness)
|
49 | 49 |
|
|
52 | 52 | print("Best result : ", numpy.max(numpy.sum(new_population*equation_inputs, axis=1)))
|
53 | 53 |
|
54 | 54 | # Selecting the best parents in the population for mating.
|
55 |
| - parents = GA.select_mating_pool(new_population, fitness, |
| 55 | + parents = ga.select_mating_pool(new_population, fitness, |
56 | 56 | num_parents_mating)
|
57 | 57 | print("Parents")
|
58 | 58 | print(parents)
|
59 | 59 |
|
60 | 60 | # Generating next generation using crossover.
|
61 |
| - offspring_crossover = GA.crossover(parents, |
| 61 | + offspring_crossover = ga.crossover(parents, |
62 | 62 | offspring_size=(pop_size[0]-parents.shape[0], num_weights))
|
63 | 63 | print("Crossover")
|
64 | 64 | print(offspring_crossover)
|
65 | 65 |
|
66 | 66 | # Adding some variations to the offspring using mutation.
|
67 |
| - offspring_mutation = GA.mutation(offspring_crossover, num_mutations=2) |
| 67 | + offspring_mutation = ga.mutation(offspring_crossover, num_mutations=2) |
68 | 68 | print("Mutation")
|
69 | 69 | print(offspring_mutation)
|
70 | 70 |
|
|
74 | 74 |
|
75 | 75 | # Getting the best solution after iterating finishing all generations.
|
76 | 76 | #At first, the fitness is calculated for each solution in the final generation.
|
77 |
| -fitness = GA.cal_pop_fitness(equation_inputs, new_population) |
| 77 | +fitness = ga.cal_pop_fitness(equation_inputs, new_population) |
78 | 78 | # Then return the index of that solution corresponding to the best fitness.
|
79 | 79 | best_match_idx = numpy.where(fitness == numpy.max(fitness))
|
80 | 80 |
|
|
0 commit comments