2
$\begingroup$

Suppose that:

y={{"11111010101010111111", "00010011110010001110001011", 
   "000011111110001111101100", "11000100111101110011", 
   "0011001000011101010111", "1110101110001010001110", 
   "00010001011101001100110", "001101011011001100111100", 
   "11011010111001101101", "10100000000001000011011", 
   "11000001001000101110", "101101110001010110000", 
   "00111000100101010010111", "110111101101100011100", 
   "101010010001101001100", "1101001000000001101011111", 
   "001110000110001001011011", "10011011100001110100011", 
   "01100000111001100100110", "111100001101100110001", 
   "100010001001110000111", "010100111101111010100", 
   "11110011101110000100", "111101011001111110000", 
   "1100001110001101001001", "10000011011100001100", 
   "010101110110001111110010", "11101010101100011001", 
   "0110000101101110000011", "11011010101011011000", 
   "10111001101001101011", "100110111000001111110010", 
   "10101011101100011010", "00111010011001100011110", 
   "011000010101001011101", "1111100001001111011011", 
   "010010110101010101010", "1001101011001001001000", 
   "11010001011010111000", "011101111111100011100", 
   "0110010111001011110000", "1110101100001100000011", 
   "00000101010000100000001011101000", "011101010001101100000", 
   "01111110100001011101010", "110100000101000101010", 
   "011011010011111011110", "0110101110000001111001100", 
   "11100000011010101100", "10001001001100010001", 
   "0010101110011001100010", "001011001101001101101000", 
   "1101100100001001111010", "1011010101001011101010", 
   "100011110101000011100", "000010011010111001011111", 
   "011110001101001110100", "01100011001001010100111", 
   "1100111011001001111011", "10100010101010101010", 
   "00111111100001101011011", "11110101001100010110", 
   "10011110001000100011", "00101000101001010111010", 
   "111001010001100100111", "111011010001010000100", 
   "11010001111111001000", "11100111011110110110", 
   "10000110011001101111", "00011111100011101101010", 
   "00000010000000101110010011", "111011011101000011001", 
   "0101011011101101111001", "1011001101001001100010", 
   "0011100100001110011110", "001011011010001010001000", 
   "000010111100101010011100", "011111000111011110000", 
   "0101011111001110011111", 
   "000111001011001010001000"}, {"10101111111010110110", 
   "11010111111111000100", "000010010011101000001100", 
   "10001101111100011110", "0101101001101100010011", 
   "11011110111111011110", "1011101110000001010101000", 
   "10010011011000001101", "11111101000000001110101010", 
   "0100111000001001101001", "1111101001001001011001", 
   "11000010111010100111", "110010101101101010000", 
   "110011011001000100010", "001101101010001000111100", 
   "10111000111101000010", "11010010001011000110", 
   "0100000111001001000000", "0000010001011101001101111", 
   "11001110101110110110", "00000011010010011110001000", 
   "010010000111010111001", "010100101111111010100", 
   "101101010000001001110001", "001000101011001101111010", 
   "00011001101001011011100", "110011100101011111110", 
   "1111111110001010011111", "011001000001110001010", 
   "011000101111100001100", "11001001001010110011", 
   "00101001110001100110001", "110101110101010010001", 
   "010011000101000101101", "0011110001001101111101", 
   "00011100011011111011001", "0011000001101000000101", 
   "000111010010100001101011110", "1110100010001000101010", 
   "1101101111001000000011", "00011100111101001101101", 
   "10101000101100001000", "1001000001001101011000", 
   "0000101000111001111100001", "10111011010001110010101", 
   "000000110100000101111010111", "11101111011000001101", 
   "111110110001111111101", "011110000001000100110", 
   "001010110011001011010111", "00000010100101001011100011", 
   "10010101101010001010", "000101000101001011110000", 
   "00111011010001001100100", "000001111110011001111101111", 
   "010101011011000010110", "0010001010011101001011", 
   "11011101101011010110", "1011011111001111011100", 
   "00011011111101101001111", "010001000111010011001", 
   "11111010011000101010", "00010111111111010010111", 
   "10110011101101110000", "110100100001000011010", 
   "10001011011011000100", "00011111111000000001010010000", 
   "11000011100001100101100", "0001111101111001110111000", 
   "00111001110101000110101", "011001101001000010010", 
   "1111101001001100000110", "011000011011111111011", 
   "10001101101011001001", "010001100101011110110", 
   "0000110110000101010000011", "0100101101101111001110", 
   "000100101010000000001010100001", "0111110101101001001101", 
   "10100101011011010100"}};

I am currently working on a genetic algorithm, and what I am trying to do is create "offsprings". So the crossoverLocation is what location to cut the string. Essentially what I am doing is taking [[i,1]][[i,2]] and join them together after the cut. I am not really sure how to explain it. I have written the following code:

offSpring = {};
For[i = 1, i <= 20, i++,
 crossoverLocation = RandomInteger[{0, Min[StringLength[parentPopF]]}];
 
 firstChild = 
  StringJoin[StringTake[parentPopF[[i, 1]], crossoverLocation], 
   StringTake[
    parentPopF[[i, 2]], {crossoverLocation + 1, 
     StringLength[parentPopF[[i, 2]]]}]];
 
 secondChild = 
  StringJoin[StringTake[parentPopF[[i, 2]], crossoverLocation], 
   StringTake[
    parentPopF[[i, 1]], {crossoverLocation + 1, 
     StringLength[parentPopF[[i, 1]]]}]];
 
 AppendTo[offSpring, {firstChild, secondChild}]];

So currently this code outputs 4 values because it is not taking into consideration the {78} dimension. For example [[i,3]][[i,4]], etc I tried adding another For loop but it didn't seem to work. Is there a way to get the missing values? Many thanks!

$\endgroup$

1 Answer 1

1
$\begingroup$

Say you want the crossover point at position cut=3:

cut = 3;
z = Flatten[{y[[1, Range[1, cut]]], y[[2, Range[cut + 1, Length[y[[2]]]]]]}];

The above takes the first cut elements from y[[1]] and the remaining elements from y[[2]]. Reverse the 1 and the 2 to do the opposite. You should probably make it into a function so you can have the column specification (1 and 2 in the above) as variables. For instance, to use the same two columns at different cut points you might define

z[cut_] := Flatten[{y[[1, Range[1, cut]]], y[[2, Range[cut + 1, Length[y[[2]]]]]]}];

Then you could call this to get 5 random cuts:

z[#] & /@ RandomInteger[{1, Length[y[[1]]]}, 5]

Here /@ is the short form for Map.

$\endgroup$
3
  • $\begingroup$ So, I would need to define a function and the run a for loop for get more of the answers? $\endgroup$
    – fernando1
    Apr 7, 2022 at 19:05
  • $\begingroup$ In Mathematica you rarely need to run loops. For this case, an idiomatic approach would be to define a function (as suggested above) and then "Map" that function onto the data set, that is, onto all the pairs of columns of y and cut points that you want. $\endgroup$
    – bill s
    Apr 7, 2022 at 20:04
  • $\begingroup$ So to create a function of it, it would be z[col_]:=Flatten[{y[[col, Range[col, cut]]], y[[2, Range[cut + col, Length[y[[2]]]]]]}];.? $\endgroup$
    – fernando1
    Apr 7, 2022 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.