2021-06-20 18:02:11 +02:00
|
|
|
import random
|
|
|
|
|
|
|
|
import keyboard as keyboard
|
|
|
|
|
|
|
|
import field as F
|
|
|
|
from ga_methods import *
|
|
|
|
from src import mapschema as maps
|
2021-06-20 13:24:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Genetic Algorithm
|
2021-06-20 18:02:11 +02:00
|
|
|
def genetic_algorithm_setup(field):
|
|
|
|
population_units = ["", "w", "p", "s"]
|
2021-06-20 13:24:12 +02:00
|
|
|
|
2021-06-20 18:02:11 +02:00
|
|
|
# new_population to be
|
|
|
|
population_text = []
|
2021-06-20 13:24:12 +02:00
|
|
|
|
2021-06-20 18:02:11 +02:00
|
|
|
# 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))
|
2021-06-20 13:24:12 +02:00
|
|
|
|
2021-06-20 18:02:11 +02:00
|
|
|
# printer
|
|
|
|
for _ in population_text:
|
|
|
|
print(population_text)
|
2021-06-20 13:24:12 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
Genetic algorithm parameters:
|
|
|
|
Mating pool size
|
|
|
|
Population size
|
|
|
|
"""
|
2021-06-20 18:02:11 +02:00
|
|
|
|
|
|
|
# units per population in generation
|
2021-06-20 13:24:12 +02:00
|
|
|
sol_per_pop = 8
|
|
|
|
num_parents_mating = 4
|
|
|
|
|
2021-06-20 18:02:11 +02:00
|
|
|
population_values = []
|
|
|
|
fitness_row = []
|
2021-06-20 13:24:12 +02:00
|
|
|
|
2021-06-20 18:02:11 +02:00
|
|
|
# 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)
|
2021-06-20 13:24:12 +02:00
|
|
|
|
|
|
|
best_outputs = []
|
2021-06-20 18:02:11 +02:00
|
|
|
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
|
2021-06-20 13:24:12 +02:00
|
|
|
|
|
|
|
# Getting the best solution after iterating finishing all generations.
|
|
|
|
# At first, the fitness is calculated for each solution in the final generation.
|
2021-06-20 18:02:11 +02:00
|
|
|
fitness = cal_pop_fitness(new_population)
|
2021-06-20 13:24:12 +02:00
|
|
|
# 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()
|
2021-06-20 18:02:11 +02:00
|
|
|
|
|
|
|
# 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)
|