From bfd8c98823829286e24e446313ec6ad244dbfb72 Mon Sep 17 00:00:00 2001 From: Dymitr Date: Wed, 8 Sep 2021 15:44:34 +0200 Subject: [PATCH 1/5] Initial commit --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..7c40f525de --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +venv/ +evoman/ +__pycache__/ +*.py[cod] \ No newline at end of file From 87f4a10c9d42062c66f30c741e9186757fb796b5 Mon Sep 17 00:00:00 2001 From: Dymitr Date: Wed, 8 Sep 2021 20:39:07 +0200 Subject: [PATCH 2/5] Update gitignore --- .gitignore | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 7c40f525de..1c45f5f067 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ -venv/ +.vscode/ evoman/ __pycache__/ -*.py[cod] \ No newline at end of file +*.py[cod] +*.txt \ No newline at end of file From bbd93493ad4ecbe51977bcd6a57ebd8a7fa059d9 Mon Sep 17 00:00:00 2001 From: Dymitr Date: Sun, 12 Sep 2021 17:57:51 +0200 Subject: [PATCH 3/5] Add experiment with randomly controlled player --- specialist_1.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 specialist_1.py diff --git a/specialist_1.py b/specialist_1.py new file mode 100644 index 0000000000..3aacea1a17 --- /dev/null +++ b/specialist_1.py @@ -0,0 +1,28 @@ +import sys +sys.path.insert(0, 'evoman') + +from environment import Environment +from demo_controller import player_controller +import numpy as np +import os + + +experiment_name = 'individual_1' +if not os.path.exists(experiment_name): + os.makedirs(experiment_name) + + +# initializes simulation in individual evolution mode, for single static enemy. +env = Environment(experiment_name=experiment_name, + enemies=[1], + playermode="ai", + player_controller=player_controller(0), # no hidden layer + enemymode="static", + level=2, + speed="normal") + +# 5 is a number of possible outputs +# env.get_num_sensors() is input size, +1 for bias +genome_length = 5 * (env.get_num_sensors() + 1) +genome = np.random.uniform(-1, 1, size=(genome_length,)) +env.play(pcont=genome) From dfbd204dfa099468349c73379b5c0dbfb0886351 Mon Sep 17 00:00:00 2001 From: Dymitr Date: Tue, 14 Sep 2021 12:30:59 +0200 Subject: [PATCH 4/5] Add EvolutionaryAlgorithm class --- base_evolutionary_algorithm.py | 72 ++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 base_evolutionary_algorithm.py diff --git a/base_evolutionary_algorithm.py b/base_evolutionary_algorithm.py new file mode 100644 index 0000000000..8280e8d147 --- /dev/null +++ b/base_evolutionary_algorithm.py @@ -0,0 +1,72 @@ +import sys +sys.path.insert(0, 'evoman') + +from environment import Environment +from demo_controller import player_controller +import numpy as np +import os + + +class EvolutionaryAlgorithm: + def __init__(self, + _experiment_name, + _population_size, + _generations_number, + _selection, + _crossover, + _mutation, + _insertion): + + self.experiment_name = _experiment_name + self.population_size = _population_size + self.generations_number = _generations_number + self.selection = _selection + self.crossover = _crossover + self.mutation = _mutation + self.insertion = _insertion + self.initialiseEnvironment() + + def findSolution(self): + generation = 1 + self.initialisePopulation() + while(generation <= self.generations_number): + fitness = self.getFitness() + selected_individuals = self.selection(fitness, self.population) + newcomers = self.crossover(selected_individuals) + self.population = self.insertion( + fitness, self.population, newcomers) + + generation += 1 + + return self.selection(fitness, self.population)[0] + + def getFitness(self): + fitness = np.array([]) + + for i in range(self.population_size): + f, pl, el, t = self.env.play(pcont=self.population[i]) + fitness = np.append(fitness, f) + + return fitness + + def initialisePopulation(self): + genome_length = 5 * (self.env.get_num_sensors() + 1) + self.population = np.random.uniform(-1, 1, + self.population_size * genome_length,) + + self.population = self.population.reshape( + self.population_size, genome_length) + + def initialiseEnvironment(self): + os.environ["SDL_VIDEODRIVER"] = "dummy" + + if not os.path.exists(self.experiment_name): + os.makedirs(self.experiment_name) + + self.env = Environment(experiment_name=self.experiment_name, + enemies=[1], + playermode="ai", + player_controller=player_controller(0), + enemymode="static", + level=2, + speed="fastest") From 3f075b721083870c15ab8607cffcb5cfa083d1b2 Mon Sep 17 00:00:00 2001 From: Dymitr Date: Tue, 14 Sep 2021 12:31:57 +0200 Subject: [PATCH 5/5] Add demo file for EvolutionaryAlgorithm class --- specialist_1.py | 66 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 22 deletions(-) diff --git a/specialist_1.py b/specialist_1.py index 3aacea1a17..551917cf98 100644 --- a/specialist_1.py +++ b/specialist_1.py @@ -1,28 +1,50 @@ -import sys -sys.path.insert(0, 'evoman') - -from environment import Environment -from demo_controller import player_controller +from typing import NewType import numpy as np -import os +from base_evolutionary_algorithm import EvolutionaryAlgorithm + + +def selection(fitness, population): + i = np.flip(np.argsort(fitness), axis=None) + + return population[i[:(i.size // 5)], :] + + +def crossover(selected_individuals): + selected_count = selected_individuals.shape[0] + offsprings = np.array([]) + offspring_count = selected_count // 2 + genome_length = selected_individuals.shape[1] + + for i in range(offspring_count): + partner = np.random.randint(selected_count) + crossing_point = np.random.randint(genome_length) + + father_genes = selected_individuals[i, : crossing_point] + mother_genes = selected_individuals[partner, crossing_point:] + + offspring = np.concatenate((father_genes, mother_genes), axis=None) + offsprings = np.concatenate((offsprings, offspring), axis=None) + + mutants_count = selected_count // 10 + mutants = np.random.uniform(-1, 1, mutants_count * genome_length) + + newcomers_count = mutants_count + offspring_count + return np.concatenate((offsprings, mutants), axis=None).reshape(newcomers_count, genome_length) + +def insertion(fitness, population, newcomers): + i = np.argsort(fitness, axis=None) + population = np.delete(population, i[: newcomers.shape[0]], 0) + return np.concatenate((population, newcomers)) -experiment_name = 'individual_1' -if not os.path.exists(experiment_name): - os.makedirs(experiment_name) +evolutionaryAlgorithm = EvolutionaryAlgorithm(_experiment_name='solution1', + _population_size=100, + _generations_number=50, + _selection=selection, + _crossover=crossover, + _mutation=1, + _insertion=insertion) -# initializes simulation in individual evolution mode, for single static enemy. -env = Environment(experiment_name=experiment_name, - enemies=[1], - playermode="ai", - player_controller=player_controller(0), # no hidden layer - enemymode="static", - level=2, - speed="normal") -# 5 is a number of possible outputs -# env.get_num_sensors() is input size, +1 for bias -genome_length = 5 * (env.get_num_sensors() + 1) -genome = np.random.uniform(-1, 1, size=(genome_length,)) -env.play(pcont=genome) +controller = evolutionaryAlgorithm.findSolution()