I am trying to generate a new child array from two parent arrays (crossover) using the following process.
parentArr1 = [0,1,2,3,4,5,6,7,8,9]
parentArr2 = [9,8,7,6,5,4,3,2,1,0]
parent1Subset = [2,3,4,5]
childArr = [9,8,2,3,4,5,7,6,1,0]
The crossover rules I'm defining are:
- Extract a contiguous subset from
parentArr1
and insert it into a newchildArr
at the same position it was extracted from. - Fill the remaining positions in the
childArr
with elements fromparentArr2
and maintain the order of elements inparentArr2
around the subset. - There should be no duplicates.
Here's another example:
parentArr1 = [0,1,2,3,4,5,6,7,8,9]
parentArr2 = [9,8,7,6,5,4,3,2,1,0]
parent1Subset = [7,8,9]
childArr = [6,5,4,3,2,1,0,7,8,9]
I've had numerous failed attempts to do this. Here's the attempt that came the closest.
const parentArr1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const parentArr2 = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0];
let parent1Subset = [2, 3, 4, 5];
let childArr = crossOver(parentArr1, parentArr2, parent1Subset);
// Expected ouput: 9, 8, 2, 3, 4, 5, 7, 6, 1, 0
function crossOver(pArr1, pArr2, pArr1Subset) {
let _childArr = pArr1Subset.slice(); // suggestion from @r3wt
for (let i = 0; i < pArr1.length; i++) {
for (let j = 0; j < (pArr1.length - _childArr.length); j++) {
if (!_childArr.includes(pArr2[i])) {
_childArr.splice(i, 0, pArr2[i]);
}
}
}
return _childArr;
}
console.log("childArr: " + childArr);
// childArr: 9, 8, 7, 6, 2, 3, 4, 5, 1, 0