示例#1
0
    def cross(self, p1, p2):
        logger.debug("Crossing: " + str(p1) + " and " + str(p2))

        parent1 = p1.list[:]
        parent2 = p2.list[:]

        index1 = random.randrange(0, len(parent1))
        index2 = random.randrange(0, len(parent1))
        if index1 > index2:
            tmp = index1
            index1 = index2
            index2 = tmp

        parent1fragment = parent1[index1:index2]
        parent2fragment = parent2

        for city in parent1fragment:
            parent2fragment.remove(city)
        genlist = parent1fragment + parent2fragment

        genotype = TSPGenotype(p1.cities)
        genotype.set_list(genlist)

        logger.debug("Crossed genotype: " + str(genotype))

        return genotype
示例#2
0
 def __call__(self):
     agents = {}
     for i in range(self.size):
         agent = EmasAgent(TSPGenotype(self.cities), self.energy,
                           self.naming_service.get_next_agent())
         agents[agent.get_address()] = agent
     return agents
示例#3
0
    def mutate(self, genotype):
        logger.debug("Inverse mutation of {0}".format(str(genotype)))

        new_gen = TSPGenotype(genotype.points, order=inverse(genotype.order))

        logger.debug("Mutated {0}".format(str(new_gen)))
        return new_gen
示例#4
0
    def mutate(self, genotype):
        logger.debug("Mutating (next swap) genotype: " + str(genotype))

        l = genotype.list[:]
        index1 = random.randrange(0, len(l))
        index2 = (index1 + 1) % len(l)
        e1 = l[index1]
        e2 = l[index2]
        l[index2] = e1
        l[index1] = e2
        gen = TSPGenotype(genotype.cities)
        gen.set_list(l)

        logger.debug("Mutated (next swap) genotype: " + str(gen))

        return gen
示例#5
0
    def mutate(self, genotype):
        logger.debug("Mutating (next swap) genotype: " + str(genotype))

        l = genotype.list[:]
        index1 = random.randrange(0, len(l))
        index2 = (index1 + 1) % len(l)
        e1 = l[index1]
        e2 = l[index2]
        l[index2] = e1
        l[index1] = e2
        gen = TSPGenotype(genotype.cities)
        gen.set_list(l)

        logger.debug("Mutated (next swap) genotype: " + str(gen))

        return gen
示例#6
0
 def __init__(self, population_size=1000, filename=None):
     super(TSPInitializer, self).__init__(TSPGenotype)
     self.size = population_size
     if filename:
         self.points = initialize_points(filename)
         self.population = [
             TSPGenotype(self.points) for _ in range(self.size)
         ]
示例#7
0
    def mutate(self, genotype):
        logger.debug("Consecutive mutation of {0}".format(str(genotype)))

        n = len(genotype.order)
        random_choice = random.randrange(n)
        new_gen = TSPGenotype(genotype.points,
                              order=swap(list(genotype.order), random_choice,
                                         (random_choice + 1) % n))

        logger.debug("Mutated {0}".format(str(new_gen)))
        return new_gen
示例#8
0
    def mutate(self, genotype):
        logger.debug("Random mutation of {0}".format(str(genotype)))

        n = len(genotype.order)
        new_gen = TSPGenotype(genotype.points,
                              order=swap(list(genotype.order),
                                         random.randrange(n),
                                         random.randrange(n)))

        logger.debug("Mutated {0}".format(str(new_gen)))
        return new_gen
示例#9
0
    def cross(self, p1, p2):
        logger.debug("Crossing {0} and {1}".format(str(p1), str(p2)))

        p1_order = list(p1.order)
        p2_order = list(p2.order)
        points_size = len(p1_order)

        l = random.randrange(0, points_size)
        r = random.randrange(0, points_size)
        if l > r:
            l, r = r, l

        parent1_order_part = p1_order[l:r]

        for city in parent1_order_part:
            p2_order.remove(city)

        genotype = TSPGenotype(p1.points, order=parent1_order_part + p2_order)

        logger.debug("Crossed: {0}".format(str(genotype)))
        return genotype
示例#10
0
 def generate_population(self, number_of_genotypes, cities):
     return [TSPGenotype(cities) for _ in xrange(0, number_of_genotypes)]