import random import keyboard as keyboard import field as F from ga_methods import * from src import mapschema as maps # Genetic Algorithm def genetic_algorithm_setup(field): population_units = ["", "w", "p", "s"] # new_population to be population_text = [] # Populate the population_text array for row in range(D.GSIZE): population_text.append([]) for column in range(D.GSIZE): population_text[row].append(random.choice(population_units)) # printer for _ in population_text: print(population_text) """ Genetic algorithm parameters: Mating pool size Population size """ # units per population in generation sol_per_pop = 8 num_parents_mating = 4 population_values = [] fitness_row = [] # population Fitness for i in range(0, D.GSIZE): for j in range(D.GSIZE): fitness_row.append(local_fitness(field, i, j, population_text)) population_values.append(fitness_row) best_outputs = [] num_generations = 10 generation = 0 while generation < num_generations: if keyboard.is_pressed('space'): generation += 1 print("Generation : ", generation) # Measuring the fitness of each chromosome in the population. fitness = cal_pop_fitness(population_values) print("Fitness") print(fitness) # best_outputs.append(best_Output(new_population)) # The best result in the current iteration. # print("Best result : ", best_Output(new_population)) # Selecting the best parents in the population for mating. parents = select_mating_pool(new_population, fitness, num_parents_mating) print("Parents") print(parents) # Generating next generation using crossover. offspring_crossover = crossover(parents, offspring_size=(pop_size[0] - parents.shape[0], num_weights)) print("Crossover") print(offspring_crossover) # Adding some variations to the offspring using mutation. offspring_mutation = mutation(offspring_crossover, num_mutations=2) print("Mutation") print(offspring_mutation) # Creating the new population based on the parents and offspring. new_population[0:parents.shape[0], :] = parents new_population[parents.shape[0]:, :] = offspring_mutation # Getting the best solution after iterating finishing all generations. # At first, the fitness is calculated for each solution in the final generation. fitness = cal_pop_fitness(new_population) # Then return the index of that solution corresponding to the best fitness. best_match_idx = numpy.where(fitness == numpy.max(fitness)) print("Best solution : ", new_population[best_match_idx, :]) print("Best solution fitness : ", fitness[best_match_idx]) import matplotlib.pyplot matplotlib.pyplot.plot(best_outputs) matplotlib.pyplot.xlabel("Iteration") matplotlib.pyplot.ylabel("Fitness") matplotlib.pyplot.show() # return best iteration of field return 0 if __name__ == "__main__": # Define the map of the field mapschema = maps.createField() # Create field array field = [] # Populate the field array for row in range(D.GSIZE): field.append([]) for column in range(D.GSIZE): fieldbit = F.Field(row, column, mapschema[column][row]) field[row].append(fieldbit) genetic_algorithm_setup(field)