Merge pull request 'GA implementation in env' (#1) from Paweł into master

Reviewed-on: #1
This commit is contained in:
Paweł Lewicki 2021-06-20 18:03:50 +02:00
commit 6c905621ca
3 changed files with 129 additions and 60 deletions

View File

@ -1,85 +1,91 @@
import numpy import random
import keyboard as keyboard
import field as F
from ga_methods import *
from src import mapschema as maps
import ga_methods
# Genetic Algorithm # Genetic Algorithm
if __name__ == "__main__": def genetic_algorithm_setup(field):
population_units = ["", "w", "p", "s"]
""" # new_population to be
The y=target is to maximize this equation ASAP: population_text = []
y = w1x1+w2x2+w3x3+w4x4+w5x5+6wx6
where (x1,x2,x3,x4,x5,x6)=(4,-2,3.5,5,-11,-4.7)
What are the best values for the 6 weights w1 to w6?
We are going to use the genetic algorithm for the best possible values after a number of generations.
"""
# Inputs of the equation. # Populate the population_text array
equation_inputs = [4, -2, 3.5, 5, -11, -4.7] for row in range(D.GSIZE):
population_text.append([])
for column in range(D.GSIZE):
population_text[row].append(random.choice(population_units))
# Number of the weights we are looking to optimize. # printer
num_weights = len(equation_inputs) for _ in population_text:
print(population_text)
""" """
Genetic algorithm parameters: Genetic algorithm parameters:
Mating pool size Mating pool size
Population size Population size
""" """
# units per population in generation
sol_per_pop = 8 sol_per_pop = 8
num_parents_mating = 4 num_parents_mating = 4
# Defining the population size. population_values = []
pop_size = (sol_per_pop, fitness_row = []
num_weights) # The population will have sol_per_pop chromosome where each chromosome has num_weights genes.
# Creating the initial population.
new_population = numpy.random.uniform(low=-4.0, high=4.0, size=pop_size)
print(new_population)
""" # population Fitness
new_population[0, :] = [2.4, 0.7, 8, -2, 5, 1.1] for i in range(0, D.GSIZE):
new_population[1, :] = [-0.4, 2.7, 5, -1, 7, 0.1] for j in range(D.GSIZE):
new_population[2, :] = [-1, 2, 2, -3, 2, 0.9] fitness_row.append(local_fitness(field, i, j, population_text))
new_population[3, :] = [4, 7, 12, 6.1, 1.4, -4] population_values.append(fitness_row)
new_population[4, :] = [3.1, 4, 0, 2.4, 4.8, 0]
new_population[5, :] = [-2, 3, -7, 6, 3, 3]
"""
best_outputs = [] best_outputs = []
num_generations = 1000 num_generations = 10
for generation in range(num_generations):
print("Generation : ", generation)
# Measuring the fitness of each chromosome in the population.
fitness = ga_methods.cal_pop_fitness(equation_inputs, new_population)
print("Fitness")
print(fitness)
best_outputs.append(numpy.max(numpy.sum(new_population * equation_inputs, axis=1))) generation = 0
# The best result in the current iteration.
print("Best result : ", numpy.max(numpy.sum(new_population * equation_inputs, axis=1)))
# Selecting the best parents in the population for mating. while generation < num_generations:
parents = ga_methods.select_mating_pool(new_population, fitness, if keyboard.is_pressed('space'):
num_parents_mating) generation += 1
print("Parents")
print(parents)
# Generating next generation using crossover. print("Generation : ", generation)
offspring_crossover = ga_methods.crossover(parents, # Measuring the fitness of each chromosome in the population.
offspring_size=(pop_size[0] - parents.shape[0], num_weights))
print("Crossover")
print(offspring_crossover)
# Adding some variations to the offspring using mutation. fitness = cal_pop_fitness(population_values)
offspring_mutation = ga_methods.mutation(offspring_crossover, num_mutations=2) print("Fitness")
print("Mutation") print(fitness)
print(offspring_mutation)
# Creating the new population based on the parents and offspring. # best_outputs.append(best_Output(new_population))
new_population[0:parents.shape[0], :] = parents # The best result in the current iteration.
new_population[parents.shape[0]:, :] = offspring_mutation # 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. # Getting the best solution after iterating finishing all generations.
# At first, the fitness is calculated for each solution in the final generation. # At first, the fitness is calculated for each solution in the final generation.
fitness = ga_methods.cal_pop_fitness(equation_inputs, new_population) fitness = cal_pop_fitness(new_population)
# Then return the index of that solution corresponding to the best fitness. # Then return the index of that solution corresponding to the best fitness.
best_match_idx = numpy.where(fitness == numpy.max(fitness)) best_match_idx = numpy.where(fitness == numpy.max(fitness))
@ -92,3 +98,24 @@ if __name__ == "__main__":
matplotlib.pyplot.xlabel("Iteration") matplotlib.pyplot.xlabel("Iteration")
matplotlib.pyplot.ylabel("Fitness") matplotlib.pyplot.ylabel("Fitness")
matplotlib.pyplot.show() 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)

View File

@ -1,13 +1,49 @@
import numpy import numpy
import src.dimensions as D
# Genetic Algorithm methods # Genetic Algorithm methods
x = "Hello world"
def local_fitness(field, x, y, plants):
soil_value = 0
if field[x][y].field_type == "soil":
soil_value = 1
else:
soil_value = 0.5
if plants[x][y] == "":
plant_value = 0
elif plants[x][y] == "w":
plant_value = 1
elif plants[x][y] == "p":
plant_value = 2
elif plants[x][y] == "s":
plant_value = 3
neighbour_bonus = 1
if x - 1 >= 0:
if plants[x][y] == plants[x - 1][y]:
neighbour_bonus += 1
if x + 1 < D.GSIZE:
if plants[x][y] == plants[x + 1][y]:
neighbour_bonus += 1
if y - 1 >= 0:
if plants[x][y] == plants[x][y - 1]:
neighbour_bonus += 1
if y + 1 < D.GSIZE:
if plants[x][y] == plants[x][y + 1]:
neighbour_bonus += 1
# TODO * multiculture_bonus
local_fitness_value = (soil_value + plant_value) * (0.5 * neighbour_bonus + 1)
return local_fitness_value
def cal_pop_fitness(equation_inputs, pop): def cal_pop_fitness(pop):
# Calculating the fitness value of each solution in the current population. # Calculating the fitness value of each solution in the current population.
# The fitness function calulates the sum of products between each input and its corresponding weight. # The fitness function calulates the sum of products between each input and its corresponding weight.
fitness = numpy.sum(pop * equation_inputs, axis=1) fitness = sum(map(sum, pop))
return fitness return fitness
@ -50,3 +86,7 @@ def mutation(offspring_crossover, num_mutations=1):
offspring_crossover[idx, gene_idx] = offspring_crossover[idx, gene_idx] + random_value offspring_crossover[idx, gene_idx] = offspring_crossover[idx, gene_idx] + random_value
gene_idx = gene_idx + mutations_counter gene_idx = gene_idx + mutations_counter
return offspring_crossover return offspring_crossover
def best_Output(new_population):
return numpy.max(numpy.sum(new_population * equation_inputs, axis=1))

View File

@ -43,13 +43,15 @@ if __name__ == "__main__":
fieldbit = F.Field(row, column, mapschema[column][row]) fieldbit = F.Field(row, column, mapschema[column][row])
field[row].append(fieldbit) field[row].append(fieldbit)
# genetic_algorithm_setup(field)
# Create Tractor object # Create Tractor object
tractor = T.Tractor(field, [0, 0]) tractor = T.Tractor(field, [0, 0])
# Define the map of plants # Define the map of plants
mapschema = maps.createPlants() mapschema = maps.createPlants()
# Createt plants array # Create plants array
plants = [] plants = []
# Populate the plants array # Populate the plants array