# Problem with loops or to modify the argumments of a function

**URL:** <https://discourse.julialang.org/t/problem-with-loops-or-to-modify-the-argumments-of-a-function/40834>\
**Category:** General Usage\
**Created:** [June 5, 2020, 8:32pm UTC](https://discourse.julialang.org/t/problem-with-loops-or-to-modify-the-argumments-of-a-function/40834 "2020-06-05T20:32:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josa9321](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josa9321/32/12170_2.png) [@Josa9321](https://discourse.julialang.org/u/Josa9321)\
**Post date:** [June 5, 2020, 8:32pm UTC](https://discourse.julialang.org/t/problem-with-loops-or-to-modify-the-argumments-of-a-function/40834/1 "2020-06-05T20:32:08Z")

</div>

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

```julia
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.

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

```

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:

```julia
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.
end

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [June 5, 2020, 8:39pm UTC](https://discourse.julialang.org/t/problem-with-loops-or-to-modify-the-argumments-of-a-function/40834/2 "2020-06-05T20:39:32Z")

</div>

> [@Josa9321](#):
>
> something doesn’t goes right

Can you be more specific? For example:

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

---

<div class="post-metadata">

**Author:** ![Josa9321](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josa9321/32/12170_2.png) [@Josa9321](https://discourse.julialang.org/u/Josa9321)\
**Post date:** [June 5, 2020, 9:12pm UTC](https://discourse.julialang.org/t/problem-with-loops-or-to-modify-the-argumments-of-a-function/40834/3 "2020-06-05T21:12:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 5, 2020, 9:39pm UTC](https://discourse.julialang.org/t/problem-with-loops-or-to-modify-the-argumments-of-a-function/40834/4 "2020-06-05T21:39:25Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![Josa9321](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josa9321/32/12170_2.png) [@Josa9321](https://discourse.julialang.org/u/Josa9321)\
**Post date:** [June 6, 2020, 12:05am UTC](https://discourse.julialang.org/t/problem-with-loops-or-to-modify-the-argumments-of-a-function/40834/5 "2020-06-06T00:05:01Z")

</div>

> [@Henrique\_Becker](#):
>
> Why? Cannot you print the both parents and the `son` to check if the values are right?

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`.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 6, 2020, 12:19am UTC](https://discourse.julialang.org/t/problem-with-loops-or-to-modify-the-argumments-of-a-function/40834/6 "2020-06-06T00:19:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Josa9321](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josa9321/32/12170_2.png) [@Josa9321](https://discourse.julialang.org/u/Josa9321)\
**Post date:** [June 6, 2020, 12:21am UTC](https://discourse.julialang.org/t/problem-with-loops-or-to-modify-the-argumments-of-a-function/40834/7 "2020-06-06T00:21:53Z")

</div>

Ok, I’ll try this. Thanks.
