N-Point Crossover
Description:
In genetic algorithms, a crossover allows 2 chromosomes to exchange part of their genes.
For more details, you can visit these wikipedia links : Genetic algorithm and Crossover.
A chromosome is represented by a list of genes.
Consider for instance 2 chromosomes :
xs (with genes [x,x,x,x,x,x]) and ys (with genes [y,y,y,y,y,y])
A single-point crossover at index 2 would give :
new chromosome1 = [x,x,y,y,y,y] and new chromosome2 = [y,y,x,x,x,x]
A two-point crossover at indices 1 and 3 would give :
new chromosome1 = [x,y,y,x,x,x] and new chromosome2 = [y,x,x,y,y,y]
We want to generalize this to the notion of n-point crossover, and create a generic function with the following signature :
crossover :: [Int] -> [a] -> [a] -> ([a],[a])
crossover ns xs ys = undefined
where ns
would be a list of cross-point indices.
We could compute a triple-point crossover of 2 chromosomes xs and ys the following way :
crossover [2,5,21] xs ys
The transformed first chromosome must appear at the first position of the tuple. the second one at the second position. Therefore :
(crossover [1] ['a','b','c'] ['x','y','z']) `shouldBe` (['a','y','z'],['x','b','c'])
If a cross-point index is repeated, it should be considered only once. Indices can also be provided unordered, so your function could be called with the following indices :
crossover [7,5,3,5] xs ys
In this case, you would have to consider only indices [7,5,3] and deal with the fact they are unordered.
Chromosome indices are 0-based and you can also assume that :
- (length xs) == (length ys)
- crossover indices will never exceed the length of xs or ys.
Similar Kata:
Stats:
Created | May 11, 2016 |
Published | May 11, 2016 |
Warriors Trained | 1928 |
Total Skips | 322 |
Total Code Submissions | 2698 |
Total Times Completed | 670 |
Haskell Completions | 47 |
OCaml Completions | 27 |
Python Completions | 267 |
Go Completions | 220 |
Scala Completions | 121 |
Rust Completions | 18 |
Total Stars | 25 |
% of votes with a positive feedback rating | 94% of 175 |
Total "Very Satisfied" Votes | 157 |
Total "Somewhat Satisfied" Votes | 16 |
Total "Not Satisfied" Votes | 2 |
Total Rank Assessments | 12 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 5 kyu |
Lowest Assessed Rank | 7 kyu |