def nudge(self, max_value, max_nudge, nudge_rate): for i in range(len(self.genes)): if randdouble() < nudge_rate: new_value = self.genes[i] + randint_spread(max_nudge) new_value = max(new_value, 0) new_value = min(new_value, max_value) self.genes[i] = new_value
def run_generation(self): evaluated_chromosomes = self.chromosomes[:self.population_size - self.turnover_size] # Crossover works best on two that are both pretty good. # Currently crossing 0 and 1, 2 and 3, etc. so that none # get their offspring into the pool more than once. for i in range(self.turnover_size / 2): parentA = evaluated_chromosomes[(2 * i)] parentB = evaluated_chromosomes[(2 * i) + 1] evaluated_chromosomes += parentA.crossover(parentB) if len(evaluated_chromosomes) != self.population_size: raise Exception("Noooo we lost or gained chromosomes") for i in range(self.population_size): if randdouble() < self.mutation_rate: evaluated_chromosomes[i].mutate( self.master_chromosome.max_gene) self.chromosomes = evaluated_chromosomes self.sort_population() best = self.chromosomes[0] best_anti_fitness = self.master_chromosome.anti_fitness(best) return best, best_anti_fitness
def run_generation(self): evaluated_chromosomes = self.chromosomes[:self.population_size - self.turnover_size] # Crossover works best on two that are both pretty good. # Currently crossing 0 and 1, 2 and 3, etc. so that none # get their offspring into the pool more than once. for i in range(self.turnover_size / 2): parentA = evaluated_chromosomes[(2 * i)] parentB = evaluated_chromosomes[(2 * i) + 1] evaluated_chromosomes += parentA.crossover(parentB) if len(evaluated_chromosomes) != self.population_size: raise Exception("Noooo we lost or gained chromosomes") for i in range(self.population_size): if randdouble() < self.mutation_rate: evaluated_chromosomes[i].mutate(self.master_chromosome.max_gene) self.chromosomes = evaluated_chromosomes self.sort_population() best = self.chromosomes[0] best_anti_fitness = self.master_chromosome.anti_fitness(best) return best, best_anti_fitness
from random import randint from random import random as randdouble from scikits import audiolab import numpy as np import fitness as fitness_metric num_zero_crossings_per_chunk = ((10,20), (20, 40), (40, 80)) # fitness_metric.use_rms_centroid() randint_spread = lambda x: int(round(randdouble()*2*x - x)) class Chromosome: def __init__(self, genes): self.genes = genes def crossover(self, other): max_crossing_point = min(len(self.genes), len(other.genes)) crossing_point = randint(0, max_crossing_point) child1 = Chromosome(self.genes[:crossing_point] + other.genes[crossing_point:]) child2 = Chromosome(other.genes[:crossing_point] + self.genes[crossing_point:]) return (child1, child2) def mutate(self, max_value): index = randint(0, len(self.genes) - 1) value = randint(0, max_value) self.genes[index] = value def nudge(self, max_value, max_nudge, nudge_rate): for i in range(len(self.genes)): if randdouble() < nudge_rate: new_value = self.genes[i] + randint_spread(max_nudge)
from random import randint from random import random as randdouble from scikits import audiolab import numpy as np import fitness as fitness_metric num_zero_crossings_per_chunk = ((10, 20), (20, 40), (40, 80)) # fitness_metric.use_rms_centroid() randint_spread = lambda x: int(round(randdouble() * 2 * x - x)) class Chromosome: def __init__(self, genes): self.genes = genes def crossover(self, other): max_crossing_point = min(len(self.genes), len(other.genes)) crossing_point = randint(0, max_crossing_point) child1 = Chromosome(self.genes[:crossing_point] + other.genes[crossing_point:]) child2 = Chromosome(other.genes[:crossing_point] + self.genes[crossing_point:]) return (child1, child2) def mutate(self, max_value): index = randint(0, len(self.genes) - 1) value = randint(0, max_value) self.genes[index] = value def nudge(self, max_value, max_nudge, nudge_rate):