EA: Пользовательский кроссовер для списка списков в Python - PullRequest
0 голосов
/ 11 сентября 2018

Может ли кто-нибудь немного объяснить мне, как я могу выполнить пользовательский кроссовер в списке списков? Допустим, у меня есть такой кандидат:

candidate = [[0,1,2,3][4,5,6,7,8][9,10,11]]

Я знаю, как я мог бы сделать кроссовер для одного списка. Но как это сделать на самом деле для списка списков?

Вот метод для пересечения для одного списка:

@crossover
def partially_matched_crossover(random, mom, dad, args):
    """Return the offspring of partially matched crossover on the candidates.

    This function performs partially matched crossover (PMX). This type of
    crossover assumes that candidates are composed of discrete values that
    are permutations of a given set (typically integers). It produces offspring
    that are themselves permutations of the set.

    .. Arguments:
       random -- the random number generator object
       mom -- the first parent candidate
       dad -- the second parent candidate
       args -- a dictionary of keyword arguments

    Optional keyword arguments in args:

    - *crossover_rate* -- the rate at which crossover is performed 
      (default 1.0)

    """
    crossover_rate = args.setdefault('crossover_rate', 1.0)
    if random.random() < crossover_rate:
        size = len(mom)
        points = random.sample(range(size), 2)
        x, y = min(points), max(points)
        bro = copy.copy(dad)
        bro[x:y+1] = mom[x:y+1]
        sis = copy.copy(mom)
        sis[x:y+1] = dad[x:y+1]
        for parent, child in zip([dad, mom], [bro, sis]):
            for i in range(x, y+1):
                if parent[i] not in child[x:y+1]:
                    spot = i
                    while x <= spot <= y:
                        print(child[spot])
                        spot = parent.index(child[spot])
                    child[spot] = parent[i]
        return [bro, sis]
    else:
        return [mom, dad]

Вышесказанное взято из библиотеки на основе Python. Я пытаюсь создать алгоритм для проблемы маршрутизации транспортных средств, где приведенный выше список списков является примером предлагаемого решения.

...