Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment setup #12

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.vscode/
evoman/
__pycache__/
*.py[cod]
*.txt
72 changes: 72 additions & 0 deletions base_evolutionary_algorithm.py
Original file line number Diff line number Diff line change
@@ -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")
50 changes: 50 additions & 0 deletions specialist_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from typing import NewType
import numpy as np
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))


evolutionaryAlgorithm = EvolutionaryAlgorithm(_experiment_name='solution1',
_population_size=100,
_generations_number=50,
_selection=selection,
_crossover=crossover,
_mutation=1,
_insertion=insertion)


controller = evolutionaryAlgorithm.findSolution()