Home > database >  How do I format this return?
How do I format this return?

Time:10-27

I have this function that generate the initial population for a genetic algorithm.

import random


def initial_pop(pop_size):
    pop = list()
    for i in range(pop_size):
        aux = list()
        for j in range(2):
            signal = bin(random.randint(0, 1))[2:].zfill(1)
            int_part = bin(random.randint(0, 2))[2:].zfill(2)
            real_part = bin(random.randint(0, 5000))[2:].zfill(13)
            x = ''.join([signal, int_part, real_part])
            aux.append(x)
        pop.append(aux)
    return pop


population = initial_pop(5)
print(population)

The function returns something like this:

[['1001000001111101', '1100111001001010'], ['0000111110100111', '0011001000100011'], ['1000010101001101', '0000101001100011'], ['1100000011011010', '0011000010001110'], ['0100010101001010', '1001000010001110']]

And I have this function that make the crossover between parents:

def crossover(pai1, pai2, cross_rate):
    p1x = pai1[0]
    p1y = pai1[1]
    p2x = pai2[0]
    p2y = pai2[1]
    if np.random.rand() < tx_cruzamento:
        f1x = p1x[:PONTO_CRUZAMENTO_1]   p2x[PONTO_CRUZAMENTO_1:PONTO_CRUZAMENTO_2]   p1x[:PONTO_CRUZAMENTO_2]
        f1y = p1y[:PONTO_CRUZAMENTO_1]   p2y[PONTO_CRUZAMENTO_1:PONTO_CRUZAMENTO_2]   p1y[:PONTO_CRUZAMENTO_2]
        f2x = p2x[:PONTO_CRUZAMENTO_1]   p1x[PONTO_CRUZAMENTO_1:PONTO_CRUZAMENTO_2]   p2x[:PONTO_CRUZAMENTO_2]
        f2y = p2y[:PONTO_CRUZAMENTO_1]   p1y[PONTO_CRUZAMENTO_1:PONTO_CRUZAMENTO_2]   p2y[:PONTO_CRUZAMENTO_2]
        f1 = [f1x]   [f1y]
        f2 = [f2x]   [f2y]
        return [f1]   [f2]
    else:
        return [pai1]   [pai2]

The call for this function is:

children.append(crossover(selected[j], selected[j 1], CROSS_RATE))

However, this function return something like this:

[[['1100101011000010', '1101100011010000'], ['1010001010101010', '1010000010101000']], [['1010101011010010', '1010100011001100'], ['1100010000100001', '1011000111101000']], [['1100010011000010', '1101000111010000'], ['1100001011000100', '1011000010110001']]]

How do I format the output to look exactly like the first one? The one that generated the initial population? I tried everything ([f1] [f2], [[f1] [f2]], [f1, f2], etc)

CodePudding user response:

The problem seems to be in how selected is defined. When I call your cross-over function like this:

crossover(population[0],population[1],0.3)

I get output which looks like:

[['0000110000101001', '1010100101111111'], ['0010101110111110', '0001001010110001'], ['0100100110000010', '0100111100011001'], ['0011000100010010', '0010110110111111'], ['0010010111000110', '0010010101111011']]
[['0000110000101001', '1010100101111111'], ['0010101110111110', '0001001010110001']]

which matches the form of your intended output.

This suggests that the problem lies with code that you didn't show. Alternatively, crossover needs to do some preprocessing to extract the data that it needs from that which is passed to it. If this isn't the case, you really need to provide the missing details that would allow others to replicate the problem.

  • Related