|
| 1 | +import numpy |
| 2 | +import GA |
| 3 | + |
| 4 | +""" |
| 5 | +The y=target is to maximize this equation ASAP: |
| 6 | + y = w1x1+w2x2+w3x3+w4x4+w5x5+6wx6 |
| 7 | + where (x1,x2,x3,x4,x5,x6)=(4,-2,3.5,5,-11,-4.7) |
| 8 | + What are the best values for the 6 weights w1 to w6? |
| 9 | + We are going to use the genetic algorithm for the best possible values after a number of generations. |
| 10 | +""" |
| 11 | + |
| 12 | +# Inputs of the equation. |
| 13 | +equation_inputs = [4,-2,3.5,5,-11,-4.7] |
| 14 | + |
| 15 | +# Number of the weights we are looking to optimize. |
| 16 | +num_weights = 6 |
| 17 | + |
| 18 | +""" |
| 19 | +Genetic algorithm parameters: |
| 20 | + Mating pool size |
| 21 | + Population size |
| 22 | +""" |
| 23 | +sol_per_pop = 8 |
| 24 | +num_parents_mating = 4 |
| 25 | + |
| 26 | +# Defining the population size. |
| 27 | +pop_size = (sol_per_pop,num_weights) # The population will have sol_per_pop chromosome where each chromosome has num_weights genes. |
| 28 | +#Creating the initial population. |
| 29 | +new_population = numpy.random.uniform(low=-4.0, high=4.0, size=pop_size) |
| 30 | +print(new_population) |
| 31 | + |
| 32 | +""" |
| 33 | +new_population[0, :] = [2.4, 0.7, 8, -2, 5, 1.1] |
| 34 | +new_population[1, :] = [-0.4, 2.7, 5, -1, 7, 0.1] |
| 35 | +new_population[2, :] = [-1, 2, 2, -3, 2, 0.9] |
| 36 | +new_population[3, :] = [4, 7, 12, 6.1, 1.4, -4] |
| 37 | +new_population[4, :] = [3.1, 4, 0, 2.4, 4.8, 0] |
| 38 | +new_population[5, :] = [-2, 3, -7, 6, 3, 3] |
| 39 | +""" |
| 40 | + |
| 41 | +best_outputs = [] |
| 42 | +num_generations = 1000 |
| 43 | +for generation in range(num_generations): |
| 44 | + print("Generation : ", generation) |
| 45 | + # Measuring the fitness of each chromosome in the population. |
| 46 | + fitness = GA.cal_pop_fitness(equation_inputs, new_population) |
| 47 | + print("Fitness") |
| 48 | + print(fitness) |
| 49 | + # Selecting the best parents in the population for mating. |
| 50 | + parents = GA.select_mating_pool(new_population, fitness, |
| 51 | + num_parents_mating) |
| 52 | + print("Parents") |
| 53 | + print(parents) |
| 54 | + |
| 55 | + # Generating next generation using crossover. |
| 56 | + offspring_crossover = GA.crossover(parents, |
| 57 | + offspring_size=(pop_size[0]-parents.shape[0], num_weights)) |
| 58 | + print("Crossover") |
| 59 | + print(offspring_crossover) |
| 60 | + |
| 61 | + # Adding some variations to the offsrping using mutation. |
| 62 | + offspring_mutation = GA.mutation(offspring_crossover) |
| 63 | + print("Mutation") |
| 64 | + print(offspring_mutation) |
| 65 | + |
| 66 | + # Creating the new population based on the parents and offspring. |
| 67 | + new_population[0:parents.shape[0], :] = parents |
| 68 | + new_population[parents.shape[0]:, :] = offspring_mutation |
| 69 | + |
| 70 | + best_outputs.append(numpy.max(numpy.sum(new_population*equation_inputs, axis=1))) |
| 71 | + # The best result in the current iteration. |
| 72 | + print("Best result : ", numpy.max(numpy.sum(new_population*equation_inputs, axis=1))) |
| 73 | + |
| 74 | +# Getting the best solution after iterating finishing all generations. |
| 75 | +#At first, the fitness is calculated for each solution in the final generation. |
| 76 | +fitness = GA.cal_pop_fitness(equation_inputs, new_population) |
| 77 | +# Then return the index of that solution corresponding to the best fitness. |
| 78 | +best_match_idx = numpy.where(fitness == numpy.max(fitness)) |
| 79 | + |
| 80 | +print("Best solution : ", new_population[best_match_idx, :]) |
| 81 | +print("Best solution fitness : ", fitness[best_match_idx]) |
| 82 | + |
| 83 | + |
| 84 | +import matplotlib.pyplot |
| 85 | +matplotlib.pyplot.plot(best_outputs) |
| 86 | +matplotlib.pyplot.xlabel("Iteration") |
| 87 | +matplotlib.pyplot.ylabel("Fitness") |
| 88 | + |
0 commit comments