예제 #1
0
    def mate(self, pairs):
        """ Mates """
        families = [] # Build us some real estate
        for mother, father in pairs:
            i = random.randint(0, self.params["NumGenes"])

            # Create new children
            child1 = Chromosome(''.join(mother.genes[:i] + father.genes[i:]))
            child1.cost = self.compute_cost(child1)
            child2 = Chromosome(''.join(mother.genes[i:] + father.genes[:i]))
            child2.cost = self.compute_cost(child2)

            # Add a happy new family
            families += [child1, child2, mother, father]

        self.population = families # Create new families
예제 #2
0
    def init_pop(self):

        for i in range(self.params["NumChromesInit"]):
            # Create a new random gene
            random.shuffle(self.genes)
            chrome = Chromosome(self.genes)
            chrome.cost = self.compute_cost(chrome)

            # And add to pop
            self.population.append(chrome)
예제 #3
0
    def init_pop(self):

        for i in range(self.params["NumChromesInit"]):

            # Create a new random gene
            chars = self.random_letters(self.params["NumGenes"], False)
            chrome = Chromosome(''.join(chars))
            chrome.cost = self.compute_cost(chrome)

            # And add to pop
            self.population.append(chrome)