Insertion Mutation Operator
'Default Float 0 is "0.01" and represents Minimum Alleles to move. Valid Values are [0, 0.5] representing fraction of Allele Count
'Default Float 1 is "0.5" and represents Maximum Alleles to move. Valid Values are [0, 0.5] representing fraction of Allele Count
Sub Insertion_Mutation(ByRef child1 As PChromo)
Dim Percentage As Double = (RAND.NextDouble * (Insertion_Master.Doubles(1) - Insertion_Master.Doubles(0))) + Insertion_Master.Doubles(0)
Dim IndexOfFirst = 99999
'1. Initialize Flags() - True means that value is selected for insertion. False means it isn't.
' IndexOfFirst tracks the index of the first value selected for insertion
Dim Flags(AlleleCount) As Boolean
For j As Integer = 0 To AlleleCount - 1
If RAND.NextDouble < Percentage Then
If j < IndexOfFirst Then IndexOfFirst = j
Flags(j) = True
Else
Flags(j) = False
End If
Next
If IndexOfFirst = 99999 Then
Return
End If
'2. Fill up NewChild up to IndexOfFirst
Dim NewChild As New PChromo
ReDim NewChild.Alleles(AlleleCount)
For j As Integer = 0 To IndexOfFirst - 1
NewChild.Alleles(j) = child1.Alleles(j)
Next
'3. Now, append all values selected for insertion
Dim marker As Integer = IndexOfFirst 'this variable tracks the point to insert the next value in the newchild
For j As Integer = IndexOfFirst To AlleleCount - 1
If Flags(j) Then
NewChild.Alleles(marker) = child1.Alleles(j)
marker += 1
End If
Next
'4. Now, go back and insert all the ones not selected for insertion
' remember, marker marks where we're at in regards to filling up the new child
For j As Integer = IndexOfFirst To AlleleCount - 1
If Not Flags(j) Then
NewChild.Alleles(marker) = child1.Alleles(j)
marker += 1
End If
Next
'Copy over new kid
child1.setTo(NewChild)
End Sub