crossover.genetika dengan vb.net

Upload: fosi-hulu

Post on 04-Jun-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 Crossover.genetika Dengan Vb.net

    1/2

    'PMX Crossover is very complicated, check out the oxypedia entry for detailsPublic Sub Permutation_PMX(ByRef Child1 As PChromo, ByRef parent1 As PChromo, ByRef parent2 As PChromo) Static Dim iTemp As Integer Static Dim C As Collection Static Dim HS1, ValuesLeft As Hashtable Static Dim Pair As New Index_Value_Pair Static Dim Position As Integer HS1 = New Hashtable ValuesLeft = New Hashtable C = New Collection

    '1 Setup the Swath Dim randswathsize As Integer = RAND.Next(PMX_Master.Doubles(0) * AlleleCount, PMX_Master.Doubles(1) * AlleleCount) 'Doubles(0) = Min Swath Size || Doubles(1) = Max Swath Size If randswathsize < 2 Then randswathsize = 2

    'X refers to left index of Swath || Y refers to right index of swath Dim X As Integer = RAND.Next(0, AlleleCount - randswathsize) Dim Y As Integer = X + randswathsize - 1

    If Y - X = AlleleCount - 1 Then Y -= 2 'make sure the swath isn't the whole

    chromo

    '2. Fill up ValuesLeft - keeps track of which values can be given to child ' - Also initialize child values to sentinel value of -99999 For j As Integer = 0 To AlleleCount - 1 ValuesLeft.Add(parent1.Alleles(j), Nothing) Child1.Alleles(j) = -99999 Next

    '3.Fill Child from index X to index Y of parent For j As Integer = X To Y If Child1.Alleles(j) -99999 Then MsgBox("Help Me2") If HS1.Contains(parent1.Alleles(j)) Then MsgBox("help me 8")

    Child1.Alleles(j) = parent1.Alleles(j) HS1.Add(parent1.Alleles(j), Nothing) ValuesLeft.Remove(parent1.Alleles(j)) Next

    '3. Figure out which values from Parent2's swath were not put in the child from Parent1 For j As Integer = X To Y If Not HS1.Contains(parent2.Alleles(j)) Then Pair = New Index_Value_Pair Pair.index = j Pair.value = parent2.Alleles(j) C.Add(Pair)

    End If Next

    '4.For Each Pair In C

    '1. find position (in parent2) of value in Pair's position in childFor j As Integer = 0 To AlleleCount - 1

    If parent2.Alleles(j) = Child1.Alleles(Pair.index) Then Position = j Exit For

  • 8/13/2019 Crossover.genetika Dengan Vb.net

    2/2

    End If Next

    '2. If position is taken in child already, find position of item that isin its spot in parent 2 ' Otherwise, put it there While Child1.Alleles(Position) -99999 'A. Find position of value in this spot in the child in parent 2 For j As Integer = 0 To AlleleCount - 1 If parent2.Alleles(j) = Child1.Alleles(Position) Then Position = j Exit For End If Next End While

    'If this is reached, we drop the Pair.value in Position If Child1.Alleles(Position) -99999 Then MsgBox("Help Me4") If HS1.Contains(Pair.value) Then MsgBox("help me 6") Child1.Alleles(Position) = Pair.value HS1.Add(Pair.value, Nothing)

    Next

    '5 Drop the remaining values in For j As Integer = 0 To AlleleCount - 1 If Not HS1.Contains(parent2.Alleles(j)) Then If Child1.Alleles(j) -99999 Then MsgBox("Help Me5") Child1.Alleles(j) = parent2.Alleles(j) HS1.Add(parent2.Alleles(j), Nothing) End If Next

    'HS1.Clear() 'For j As Integer = 0 To AlleleCount - 1 ' HS1.Add(Child1.Alleles(j), Nothing) 'Next

    End Sub