Python Genetic Algorithm

wes oweston at earthlink.net
Mon Jan 28 08:42:26 EST 2008


Max,

     def GeneticNextGen(self):
         numsets   = len(self.WtSets)
         numwts    = len(self.WtSets[0].Lis)

         self.WtSets.sort(CompByCurrentFitness)
         index_lis = []
         K = 100.0

         N    = float(numwts)

         #if RISE(slope) is too high, concentration occurs too fast and
         #you lose many quickly

         RISE = -0.01*K
         RUN  = N - 1.0
         m    = RISE/RUN
         for i in range( numsets ):
             x = float(i)
             numin = int(m * x + K)
             for k in range(numin):
                 index_lis.append( i )

         new_wtset_list = WtSetListClass()

         while len(new_wtset_list.WtSets) < numsets:
             #split in a number of placeses
             splitPoints = []                                #empty list of places where dna's are crossed
             numSplitPoints = random.randint( 2, 4 )         #number of places to cross at(not to hot & not to cold)
             while len(splitPoints) < numSplitPoints:        #get required num of points at random
                 split_pt = random.randint( 0, numwts - 1 )
                 if split_pt not in splitPoints:
                     splitPoints.append(split_pt)

             i1 = random.choice( index_lis )                 #get two old weight sets at random from a biased list
             while( 1 ):
                 i2 = random.choice( index_lis )
                 if i2 <> i1:
                     break
             wts1 = self.WtSets[ i1 ]
             wts2 = self.WtSets[ i2 ]

             list1 = wts1.Lis[0:]                            #just size new weight sets
             list2 = wts1.Lis[0:]

             flip  = False                                   #copy into new weight sets from old alternating the
             for k in range(len(wts1.Lis)):                  #  the source on 2 to 4 flip points
                 if k in splitPoints:
                     flip = not flip

                 if flip:
                     list1[k] = wts2.Lis[k]
                     list2[k] = wts1.Lis[k]
                 else:
                     list1[k] = wts1.Lis[k]
                     list2[k] = wts2.Lis[k]

             split_pt1 = random.choice(splitPoints)          #capture a place to mutate at low probabilty

             x = random.randint( 0, 1000 ) #.1 % of time mutate at boundry
             if x == 5:
                 list1[ split_pt1 ] = RandomFloat(LOWWT,HIGHWT)
                 list2[ split_pt1 ] = RandomFloat(LOWWT,HIGHWT)

             wt = WtSetClass( list1 )
             wt.FoldParentFitnesses( wts1,wts2 )
             new_wtset_list.WtSets.append( wt )
             if len(new_wtset_list.WtSets) < numsets:
                 wt = WtSetClass( list2 )
                 wt.FoldParentFitnesses( wts1,wts2 )
                 new_wtset_list.WtSets.append( wt )
         x = random.randint(0,10000)
         if x == 5:
             new_wtset_list.RandomizeRandomWt() #0.01% of time made an entire new random wt
         self.WtSets = new_wtset_list.WtSets



More information about the Python-list mailing list