Esempio n. 1
0
    def crossover(self, parent1, parent2):
        child = Tour(len(self._cities_list))

        start_pos = int(random.random() * parent1.tour_size())
        end_pos = int(random.random() * parent1.tour_size())

        for i in xrange(child.tour_size()):
            if start_pos < end_pos and i > start_pos and i < end_pos:
                child.set_city(i, parent1.get_city(i))
            elif start_pos > end_pos:
                if not (i < start_pos and i > end_pos):
                    child.set_city(i, parent1.get_city(i))

        for i in xrange(parent2.tour_size()):
            if not child.contains_city(parent2.get_city(i)):
                for j in xrange(child.tour_size()):
                    if not child.get_city(j):
                        child.set_city(j, parent2.get_city(i))
                        break

        return child
    def crossover(self, parent1, parent2):
        child = Tour(len(self._cities_list))

        start_pos = int(random.random() * parent1.tour_size())
        end_pos = int(random.random() * parent1.tour_size())

        for i in xrange(child.tour_size()):
            if start_pos < end_pos and i > start_pos and i < end_pos:
                child.set_city(i, parent1.get_city(i))
            elif start_pos > end_pos:
                if not (i < start_pos and i > end_pos):
                    child.set_city(i, parent1.get_city(i))

        for i in xrange(parent2.tour_size()):
            if not child.contains_city(parent2.get_city(i)):
                for j in xrange(child.tour_size()):
                    if not child.get_city(j):
                        child.set_city(j, parent2.get_city(i))
                        break

        return child