Hey.
GA can handle chromosomes with variable length. Actual Individual can be very complex. for example it can contain some number of fixed length bits, string of characters (no fixed length), and maybe some set of pairs of conjugate complex numbers. Additionally conjugate complex numbers pairs must always preserve some conditions. It can be done, but the more complex individual gets, more complex genetic operations get (e.g. cross over, mutation). Probably Fitness function would take some more lines of code. But it is still possible.
Maybe as suggested you could chose number coded path representation, but it can still be done with your example coded as Osama ALASSIRY suggested: UUUULLLLLLLLDDDRRRDLLLLLLLLLLUUUULUUU.
For cross over:
- let mutating operator read current lenght1 of given individual,
- generate two random numbers x1,y1, both x1,y1 =< lenght1 and x1 != y1 (this are your cutting points for individual 1),
- do the same for individual 2, now you have two pairs (x1,y1) and (x2,y2),
- copy from individual 1 what is between x1 and y1, and insert it into individual 2 between values x2 and y2. New version of Individual 2 may change its length as number of genes between x1y1 and x2y2 can be different, but that is ok,
- what was in original version of individual 2 between x2y2 should be inserted into new version of individual 1 between x1y1 (its lenght will also change),
To make it clear:
parent A: UUUULLLLLLLLDDDRRRDLLLLLLLLLLUUUULUUU
parent B: DRRRRLULUDDDR
you generate random pairs pairA(4,18), pairB(0,5)
assuming you count genes from 0 you swap following strings:
UUUU**LLLLLLLLDDDRRRD**LLLLLLLLLLUUUULUUU
DRRRRLULUDDDR
So after cross over you get
UUUU**DRRRRL**LLLLLLLLLLUUUULUUU
LLLLLLLLDDDRRRDULUDDDR
Now you just made cross over. you can use also one point of cutting, or multiply points.
As for mutation:
- generate number between 0 and length of individual,
- generate number between 1-4 and update that gene. (if 1 was generated change it to U, 2-D, 3-L, 4-R)
you just did mutation. You can also mutate more than one gene.
But as I said, there are other possibilities.