Skip to content

Instantly share code, notes, and snippets.

@jfalves
Last active April 13, 2019 19:01
Show Gist options
  • Save jfalves/fae314922cb0e91bd038fbfa9ae6ae4b to your computer and use it in GitHub Desktop.
Save jfalves/fae314922cb0e91bd038fbfa9ae6ae4b to your computer and use it in GitHub Desktop.
Genetic-Algorithm-from-Scratch-Snippets
def crossover(self, partner):
child = DNA(self.size)
midpoint = random.randint(self.size)
for idx in range(self.size):
if idx > midpoint:
child.genes[idx] = self.genes[idx]
else:
child.genes[idx] = partner.genes[idx]
return child
def fitness(self, target):
self.score = 0
for idx in range(self.size):
if self.genes[idx] == target[idx]:
self.score += 1
self.score = 2**self.score
import string
import numpy as np
from numpy import random
#Calcula o score de cada indivíduo
def fitness(individual, target, size):
score = 0
for idx in range(size):
if individual[idx] == target[idx]:
score += 1
return 2**score
#Seleciona os índividuos que irão se reproduzir
def mate_selection(population_list, population_fitness, population_size, best_score):
mates = list()
for idx in range(population_size):
individual = population_list[idx]
#Normaliza a pontuação dos indivíduos para uma escala de 0 à 1.
normalized_score = np.interp(population_fitness[idx], (0,best_score), (0,1))
#Probabilidade do indivíduo ser escolhido.
probability = int(normalized_score * 100)
for n in range(probability):
mates.append(individual)
mates_size = len(mates)
return mates
#Realiza o cruazamento dos genes
def crossover(parent_a, parent_b, target_size):
child = list()
midpoint = random.randint(target_size)
for idx in range(target_size):
if idx > midpoint:
child.append(parent_a[idx])
else:
child.append(parent_b[idx])
return child
#Realiza a mutação dos genes
def mutation(child, target_size, mutation_rate):
for idx in range(target_size):
if mutation_rate >= random.random():
child[idx] = random.choice(genes_sample_space, 1)[0]
return child
#Realiza o setup inicial dos dados necessários para começar utilizar o algoritmo
target = 'ser ou nao ser eis a questao'
population_size = 100
mutation_rate = 0.01
genes_sample_space = list(string.whitespace[0] + string.ascii_lowercase)
#Gera uma população inicial aleatória.
target_size = len(target)
population_list = list()
for n in range(population_size):
individual = random.choice(genes_sample_space, target_size)
population_list.append(individual)
while True:
#Calcula a adaptação dos indivíduos.
population_fitness = list()
for idx in range(population_size):
population_fitness.append(fitness(population_list[idx], target, target_size))
#calcula o melhor score de toda população
best_score = 0
for score in population_fitness:
if score > best_score:
best_score = score
#1. Seleciona os indivíduos que irão se reproduzir.
mating_pool = mate_selection(population_list, population_fitness, population_size, best_score)
mating_pool_size = len(mating_pool)
#2. Fase de Reprodução.
for idx in range(population_size):
parent_a = mating_pool[random.randint(mating_pool_size)]
parent_b = mating_pool[random.randint(mating_pool_size)]
#2.1 Reproduz-se
child = crossover(parent_a, parent_b, target_size)
#3. Ocorre uma mutação.
child = mutation(child, target_size, mutation_rate)
#4. Substituimos a população pelo filho gerado.
population_list[idx] = child
print(''.join(population_list[idx]))
def mate_selection(self):
mates = list()
for idx in range(self.size):
individual = self.individuals[idx]
#Normaliza a pontuação dos indivíduos para uma escala de 0 à 1.
normalized_score = np.interp(individual.score, (0,self.best_score), (0,1))
#Probabilidade do indivíduo ser escolhido.
probability = int(normalized_score * 100)
for n in range(probability):
mates.append(individual)
mates_size = len(mates)
return Population(mates_size, self.target, population=mates)
def mutation(self, mutation_rate):
for idx in range(self.size):
if mutation_rate >= random.random():
self.genes[idx] = random.choice(self.genes_sample_space, 1)[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment