Can I swap sections of data in two different 2d arrays of the same length, forming a third 2d array?

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!

I suggest poking your nose around other samples of this sort of stuff:

There’s a part 2 linked above too and I’ve seen other GA done in Unity over the years.

Well, based on your example it looks like you just want the usual genome behaviour. So you just randomly choose a value from either parent. When both parents have the same value anyways, it doesn’t really matter because the child would get this value in either way. However if the parents differ, you simply choose either the one or the other parent. So it’s literally just like this:

for(int i = 0; i < c.Length; i++)
{
    if (Random.Range(0,2) == 0)
        c[i] = p1[i];
    else
        c[i] = p2[i];
}

This assumes “c” is the child array while “p1” and “p2” are the two parents arrays. So this will generate a random mix between the two.