# Parallel the evaluation step of a genetic algorithm

**URL:** <https://discourse.julialang.org/t/parallel-the-evaluation-step-of-a-genetic-algorithm/29367>\
**Category:** New to Julia\
**Tags:** performance\
**Created:** [October 1, 2019, 1:23pm UTC](https://discourse.julialang.org/t/parallel-the-evaluation-step-of-a-genetic-algorithm/29367 "2019-10-01T13:23:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Volker](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Volker](https://discourse.julialang.org/u/Volker)\
**Post date:** [October 1, 2019, 1:23pm UTC](https://discourse.julialang.org/t/parallel-the-evaluation-step-of-a-genetic-algorithm/29367/1 "2019-10-01T13:23:49Z")

</div>

What is the best way to parallel my evaluation step of the genetic algorithm? I.e. would like to parallel the following code.

```julia
function test_func(pop::Pop, extra_data::Dict)::Pop
    for i in 1:pop.num_size
        pop.obj_func[i, :f1] = (4*pop.individuals[i].parms[1][1]*pop.individuals[i].parms[1][1])+(4*pop.individuals[i].parms[2][1]*pop.individuals[i].parms[2][1])
        pop.obj_func[i, :f2] = ((pop.individuals[i].parms[1][1]-5)*(pop.individuals[i].parms[1][1]-5))+((pop.individuals[i].parms[2][1]-5)*(pop.individuals[i].parms[2][1]-5))
    end
    return pop
end

```

```julia
mutable struct Pop
	num_size::Int64
	individuals::Array{Ind,1}
	obj_func::DataFrame
end

mutable struct Ind
    parms::Array{Array{Number,2},1}
end

```

---

<div class="post-metadata">

**Author:** ![zgornel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zgornel/32/217487_2.png) [@zgornel](https://discourse.julialang.org/u/zgornel)\
**Post date:** [October 1, 2019, 1:50pm UTC](https://discourse.julialang.org/t/parallel-the-evaluation-step-of-a-genetic-algorithm/29367/2 "2019-10-01T13:50:25Z")

</div>

Adding a `Threads.@threads` in front of the for loop should be the easiest way.

---

<div class="post-metadata">

**Author:** ![Volker](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Volker](https://discourse.julialang.org/u/Volker)\
**Post date:** [October 1, 2019, 1:56pm UTC](https://discourse.julialang.org/t/parallel-the-evaluation-step-of-a-genetic-algorithm/29367/3 "2019-10-01T13:56:25Z")

</div>

I already have done it and it didn´t lead to an error, but I´m not sure if it is thread safe, because I read in some topics that not all types are thread safe.

---

<div class="post-metadata">

**Author:** ![Karajan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karajan/32/8545_2.png) [@Karajan](https://discourse.julialang.org/u/Karajan)\
**Post date:** [October 1, 2019, 2:38pm UTC](https://discourse.julialang.org/t/parallel-the-evaluation-step-of-a-genetic-algorithm/29367/4 "2019-10-01T14:38:58Z")

</div>

Yeah, it seems a little unsafe because you can have multiple threads trying to write `pop.obj_func`, that might lead to complications.

---

<div class="post-metadata">

**Author:** ![Volker](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Volker](https://discourse.julialang.org/u/Volker)\
**Post date:** [October 2, 2019, 6:10am UTC](https://discourse.julialang.org/t/parallel-the-evaluation-step-of-a-genetic-algorithm/29367/5 "2019-10-02T06:10:40Z")

</div>

How do I know that it works correctly or not? Just by comparing the result of the parallel executed loop with the serial executed loop? Can I just store the data first in a simple array and than copy it after the `for`-loop to the dataframe `pop.obj_func`. Have I also consider the reading part? Let´s say my evaluation function is a bit more complicated. I.e. I have a mutable struct, which is a temporary variable, changes in every loop, is depended on the `pop.individuals[i].parms[1]` and necessary to calculate the objective functions.

```julia
function test_func(pop::Pop, extra_data::Dict)::Pop
    for i in 1:pop.num_size
        my_struct = MyStruct.create(pop.individuals[i].parms[1], extra_data)

        pop.obj_func[i, :f1] = calculate_f1(my_struct)
        pop.obj_func[i, :f2] = calculate_f2(my_struct)
    end
    return pop
end

```

---

<div class="post-metadata">

**Author:** ![Volker](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Volker](https://discourse.julialang.org/u/Volker)\
**Post date:** [October 2, 2019, 8:52am UTC](https://discourse.julialang.org/t/parallel-the-evaluation-step-of-a-genetic-algorithm/29367/6 "2019-10-02T08:52:30Z")

</div>

I tried it out and compared the serial executed loop and the parallel executed loop. It seems to be thread safe, because I got the same results.
