[solved] Slice assignment problems


(Luis Mendes) #1

Please see https://play.golang.org/p/ViIwhNoGuSW

If you run the script, you can see, for example, that in the last line of results,
crosspoint: 3 par1: 4 [0 0 1 1 1 1 0 1 0 0] ...
is not correct.

Previously,

parent1 := make([]int, 0)
parent2 := make([]int, 0)

were outside of the for loop. Placed them inside not to have problems with slices, but still it’s not working correctly.

What am I missing?


(Lutz Horn) #2

What would be correct? What is this program supposed to do?


(Ivan Matmati) #3

If you have a golang problem, state this problem. But if it’s a genetic algorithm bug, I doubt we can help.


(Luis Mendes) #4

The crossover function is supposed to generate a child for each pair of parents.
population has all the individuals of the present generation.
pool has the index of each parent that will be used to generate the children.
So, the first two values of pool [4, 4] will be crossed at crosspoint = 3. Indexes [0,3] of the left 4 and the indexes [3, 10] of the right 4 will be used. For this case, as the left and right parent are the same the result will be a similar individual.

As you can see in the first line of results (starting with crosspoint:), par1 is 4 and it really corresponds to the index 4 of pop (or population).
Still, in the first line par2 that also refers to index 4 is also correct.
In the fifth line, par2 (4) is also correct.

But in the last line, par1 (4) [0 0 1 1 1 1 0 1 0 0] is different from what it should be [0 0 1 1 0 1 1 1 1 1].

Why is that?


(Luis Mendes) #5

I’ve already understood this.
child is rewriting pop somewhere.