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 = [] population_text_single = [] population_size = 10 # Populate the population_text array for k in range(population_size): population_text_single = [] for row in range(D.GSIZE): population_text_single.append([]) for column in range(D.GSIZE): population_text_single[row].append(random.choice(population_units)) population_text.append(population_text_single) # # printer # for _ in population_text: # print(population_text) """ Genetic algorithm parameters: Mating pool size Population size """ # units per population in generation num_parents_mating = 4 best_outputs = [] num_generations = 10 num_parents = 2 # iterative var 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. # population Fitness fitness = [] for i in range(0, population_size): fitness.append((i, population_fitness(population_text[i], field, population_size))) print("Fitness") print(fitness) best = sorted(fitness, key=lambda tup: tup[1])[0:num_parents] # Leaderboard only best_outputs.append(best[0][1]) # The best result in the current iteration. print("Best result : ", best[0]) # TODO METODA WYBORU OSOBNIKA - RANKING # Selecting the best parents in the population for mating. parents = [population_text[i[0]] for i in best] 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)