1

Could anyone explain me a little bit on how I could perform a custom crossover on a list of lists? Let's say I have a candidate like this:

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

I know how I could do a crossover for a single list. But how to do this actually for a list of lists?

Here is the method for a crossover for a single list:

@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]

The above comes from the Python-based inpyred library. I am attempting to make an algorithm for the Vehicle Routing Problem where the above list of lists is an example of a proposed solution.

xfscrypt
  • 16
  • 5
  • 28
  • 59
  • 1
    if you want to do the same kind of crossover you can convert your list of lists to a single flattened list. – guroosh Sep 12 '18 at 12:15
  • actually that's smart. and then convert it back to a list of lists :). thanks! – xfscrypt Sep 12 '18 at 13:45
  • *AFAIK*, [*crossover*](https://en.wikipedia.org/wiki/Crossover_(genetic_algorithm)) is not supposed to *cut through a gene* (e.g. if the genome is an array of int, the crossover cuts only on the edge among one int and the other). Flattening as suggested by @gc7 would *cut through the gene*, because the *crossover* operation would not preserve one of the original *genes*, acting instead like *crossover+mutation*. As a result, this might increase overall *entropy* of the *genetic algorithm* and slow down convergence to a solution. Depending on the problem domain, this could be either good or bad – Patrick Trentin Sep 12 '18 at 22:25
  • @PatrickTrentin whether a candidate is a list of ints or a list of lists, I assume its about the value of the content of the genome rather than the position of its content? operations like crossover, mutation and replacement, would need to be done on values within one candidate rather then the lists of a candidate I guess? lets say u have c = [ [L1], [L2] ] where L1 and L2 are list of ints, there would be only two solutions if u ignore the value stored in the lists whereas its fitness does not depend on the order of the lists, but rather where each value is stored in which list. – xfscrypt Sep 12 '18 at 22:41
  • @apfz the purpose of crossover is to mix potentially good genes from one parent with those of another, not to change the content of such genes. Mutation, instead, does the latter. What 'gene' means in your application only you can say. I was under the impression the internal list was to be considered a gene. Also, i didn't say it's completely wrong: the search should eventually converge anyway. – Patrick Trentin Sep 12 '18 at 22:47
  • @PatrickTrentin thanks. ill have a closer look on it, but I think that part is going ok. A candidate here is a possible solution for the Vehicle Routing Problem. In other words, one candidate consists of multiple routes. so a list of lists here represents which address will go in which route. – xfscrypt Sep 12 '18 at 23:15

0 Answers0