# How can I swap sections of data in two different 2d arrays of the same length, forming a third (child) 2d array?

#### logan291g

##### New member
I am extremely new to coding and I am currently learning C# on my own. I have searched around but I cannot find what I am looking for. I may not be searching for the correct terms.

I am wondering how I could swap sections of 2 different 2d arrays, with each other, and form a new (child) 2d array.

I plan on using this in an evolution simulation, however what I am trying to do is not the same as a Genetic Algorithm. I do not care about spawning populations, fitness functions, etc.

So the basic idea is that I want to take two existing 2d arrays that are each 20 rows (genes) x 2 columns (A/B options), swap random sections, and then spawn the child 2d array of the same size.

Ideally I would like this to be similar to the Uniform Crossover in Genetics where the data isn't simply split in half. I would need the arrays to have corresponding data locations so to be used as "swap points" for the data. I also want the original "parent" arrays to remain in existence, while the child array is created.

A chromosome made of an array 20 genes, with each gene containing an array of 2 alleles.

[Chromosome]--->[Gene]--->[Allele]

Each gene represents things like color, size, and behaviors. Each gene has an A/B option (allele). These A/B options will be used later to determine "behaviors."

Parent 1: [A,A,B,B,B,A,A,B,A,A,A,B,A,B,A,A,A,B,A,A]

Parent 2: [A,B,B,B,A,A,B,B,A,A,A,B,A,B,A,A,A,B,A,A]

Child 1: [A,A,B,B,B,A,B,B,A,A,A,B,A,B,A,A,A,B,A,A]

Notice how the data swapped at random corresponding points (loci). Also notice how a good chunk of the data remains the same. I would like to eventually add a "relatedness" check that determines what percentage the two original arrays have in common. This would be how I will choose the "parents" + their proximity to each other in the simulation.

I would then need to figure out a way to tie individual methods to each A/B for each gene in order to execute the behaviors. (ex. MovementSpeedGene-A = add Random Range (0,2) to Movement Speed)

Any and all help is much appreciated!

Replies
6
Views
769
Replies
6
Views
312
Replies
5
Views
886
Replies
4
Views
545
Replies
13
Views
1K