java - How to "crossover" two strings (1234 & abcd -> 12cd & ab34) -


am developing genetic algorithm in java, of them, requires crossover of 2 parent chromosomes. these chromosomes can quite long, anywhere 30 500 (but whatever length have, same size, if length 80, in ga run 80).

i thought of developing in different ways seem me inefficient, thought might ask new, different points of view , suggestions.

for example, 1 of ways thought of converting string array of characters , iterating start point end of crossover locus (ie s1 & s2[25] s1 & s2[40]) copying temporal arrays each of arrays characters between points , iterating again , swapping them characters temporal array of "partner". said seems program have population of 1000 chromosomes , around 1000 generations extremely slow.


here illustration of two-point crossover looks like:

enter image description here


there easier 1 point crossover:

enter image description here


as not advanced @ in java advice me on approach take, maybe java function unaware of, or algorithm implement?

// chromosomes string s1; string s2;  // crossovers string c1; string c2;  random r = new random();  // 2 random indices int ind1 = r.nextint(s1.length()); int ind2 = r.nextint(s1.length()); // make sure ind2 > ind1... leaving part out;  // break both strings parts in picture string s1part1 = s1.substring(0, ind1); string s1part2 = s1.substring(ind1, ind2); string s1part3 = s1.substring(ind2);  string s2part1 = s2.substring(0, ind1); string s2part2 = s2.substring(ind1, ind2); string s2part3 = s2.substring(ind2);  // combine parts c1 = s1part1 + s2part2 + s1part3; c2 = s2part1 + s1part2 + s2part3; 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

android - layout with fragment and framelayout replaced by another fragment and framelayout -