Problem with loops or to modify the argumments of a function

I’m coding a Genetic Algorithm. When i put:

new_generation = [crossing(population[p[1]], population[p[2]]) for p in parents]

My code works just fine, but I think that line, always declaring a new vector to every iteration, isn’t efficient. So i rewrite the crossover function to accept a already declared individual.

for i in eachindex(population)
    crossing!(population[parents[i][1]], population[parents[i][2]], new_generation[i])

But by doing that, something doesn’t goes right and I don’t have a clue of what is happening.

Here is the code used in the two cases:

function crossing!(parent_1::Array{Int64,2}, parent_2::Array{Int64,2}, son = copy(parent_1))
    global I
    son .= parent_1 # In the case the individual was already declared
    p_1, p_2 = sample(I, 2, replace = false, ordered = true)
    @views son[p_1+1:p_2, :] .= parent_2[p_1+1:p_2, :] 
    return son # This line doesn't exist in the second case.

Can you be more specific? For example:

  1. What do you expect to happen?
  2. What actually happens instead?

I expect to son, an already declared variable, receives the values ​​of parent_1, then to generate the points p_1 and p_2 for, in the end, son receives, in the interval [p_1 + 1: p_2,:], the values ​​of parent_2 . But I don’t know what is happening.

If I use both methods in crossing !, having a variable that receives a already declared variable and another that is the copy of parent_1, I’ll get the same results in the two variables, so I don’t think that the problem is with crossing! .

Despite this, if I use the first structure to call crossing !, in 500 runs of the code, I don’t find 6 ~ 8 times the right answer (always creating new variable “son” to every run of crossing!); if I use the second version, I don’t find the right answer 15 ~ 18 times.

But I don’t know what is happening.

Why? Cannot you print the both parents and the son to check if the values are right?

I am not sure you have a problem. Seems like you have a non-deterministic code (sample uses rand I suppose) and a heuristic (so it does not have the guarantee to find the “right” answer, I suppose you meant “optimal” by “right”). If I would guess some problem you may be having, I would guess that population and new_generation are the same vector, so the results are different because the code does different things.

I don’t know what is happening because the crossing is the only thing that I changed, and although it doesn’t seens wrong, after the change, from 6~8 times that I didn’t find the optimal answer with my algorithm, to 15~18 times.

Yes, when I said “right”, I meaned “optimal”. No, population isn’t the same vector of new_generation.

Unfortunately, it is hard to debug this if I cannot execute your code with your input data. I suppose you set the seed at the start of the algorithm correct? Can you make some very verbose printing (e.g., every new population generated is printed), run both versions of the code, and then diff the outputs (i.e., look at the first point where they diverge)? This should give you some insight about what is different between the two.


Ok, I’ll try this. Thanks.