def initializePopulation(self): '''Fully random population initialization''' for i in range( int(self.population_size * (1 - self.constructiveHeuristic_percent))): individual = Individual() for j in range(self.dna_size): gene = randint(0, 1) individual.dna.append(gene) self.individuals.append(individual) '''Heuristic population initialization''' for i in range( int(self.population_size * self.constructiveHeuristic_percent)): individual = Individual() individual.dna = self.constructiveHeuristic() self.individuals.append(individual)
def mutate(seed: Individual): y = [None] * len(seed.dna) # loop through and mutate randomly for idx, x in enumerate(seed.dna): r = random.random() if r < .02: from layertype import LayerType y[idx] = LayerType.random_layer() elif r < .05: y[idx] = x[0], random.randrange(10, 1000) else: y[idx] = x # randomly shorten or lengthen dna r = random.random() if r < .02: from layertype import LayerType y.append(LayerType.random_layer()) elif r < .04: del y[-1] seed.dna = y return seed