Random Swap Mutation Operator
' This is like single swap, but swaps a random string of consecutive values instead
' Doubles(0) = Minimum Swap Size [0, 0.33]
' Doubles(1) = Maximum Swap Size [0, 0.33]
'This will generate the Y value for the ChildMutationWithRandomSizedSwaps function
Public Function RSS_GetY(ByRef randswapsize As Integer, ByVal XPos As Integer)
Dim done As Boolean = False
'Need to determine the start, end, and width of the invalid area.
'Special case: when X is between 0 and randomswapsize - 1
'Start and End of invalid area variables refer to indexes
Dim StartofInvalidArea, WidthofInvalidArea As Integer
Dim LeftNumberofValid, RightNumberofValid, EndofInvalidArea As Integer
StartofInvalidArea = XPos - randswapsize + 1
If (StartofInvalidArea < 0) Then 'If start pos of invalid area < 0 then it corrects itself + corrects width of invalid area
Dim temp22 As Integer = StartofInvalidArea * -1
StartofInvalidArea = 0
WidthofInvalidArea = (randswapsize + randswapsize - 1) - temp22
Else
WidthofInvalidArea = randswapsize + randswapsize - 1
End If
EndofInvalidArea = StartofInvalidArea + WidthofInvalidArea - 1
'Determine the quantity of valid spots for Y to the left and right of the invalid area
' LeftNumberofValid refers to quantity of valid values left of the invalid area
' Rightnumberofvalid refrs to quantity of valid values right of the invalid area
LeftNumberofValid = StartofInvalidArea
RightNumberofValid = (AlleleCount - randswapsize) - LeftNumberofValid - WidthofInvalidArea + 1
If (LeftNumberofValid < 0) Then 'if left number of valid less than zero then set to 0
LeftNumberofValid = 0
End If
If (RightNumberofValid < 0) Then 'if right number of valid less than zero then set to 0
RightNumberofValid = 0
End If
'Y can either be on the left of the invalid area or the right
' Depending on the random number generated, Y will end up on either side
Dim StartYPos As Integer = RAND.Next(0, (LeftNumberofValid + RightNumberofValid - 1))
If (StartYPos < LeftNumberofValid) Then
Return StartYPos
Else
StartYPos = EndofInvalidArea + (StartYPos - LeftNumberofValid) + 1
Return StartYPos
End If
End Function
Public Sub RandomSwap_Mutation(ByRef Child1 As PChromo)
Dim K As Integer
'X refers to the starting index of the first swap area
'Y refers to the starting index of the second swap area
'These areas are "randomwapsize" long
Dim randswapsize = RAND.Next(RandomSwap_Master.Doubles(0) * AlleleCount, RandomSwap_Master.Doubles(1) * AlleleCount) 'Doubles(0) = min swap size || Doubles(1) = max swap size
Dim X As Integer = RAND.Next(0, AlleleCount - randswapsize)
Dim Y As Integer = RSS_GetY(randswapsize, X) 'Calls purpose built function for determining Y value
'Swap the Values
For counter As Integer = 0 To (randswapsize - 1)
K = Child1.Alleles(X + counter)
Child1.Alleles(X + counter) = Child1.Alleles(Y + counter)
Child1.Alleles(Y + counter) = K
Next
End Sub