Commits

dan mackinlay committed d969698

better bitwise crossover for floats

  • Participants
  • Parent commits 77e7994

Comments (0)

Files changed (2)

privateer_pyevolve/PrivateerCrossovers.py

         mask = random.getrandbits(MANTISSA_BITS) 
         mantissaDad, expDad = math.frexp(gDad[i])
         mantissaMom, expMom = math.frexp(gMom[i])
-        mantissaDad = int(MANTISSA_DENOM*mantissaDad)
-        mantissaMom = int(MANTISSA_DENOM*mantissaMom)
+        baseExp = max(expMom, expDad)
+        # import pdb; pdb.set_trace()
+        
+        mantissaDad = int(mantissaDad*2.**(MANTISSA_BITS-baseExp+expDad))
+        mantissaMom = int(mantissaMom*2.**(MANTISSA_BITS-baseExp+expMom))
         
         #Not quite sure why i seem to need the -1 in here, but it makes me
         #distrust the function
         sister[i] = bitcross(mantissaDad, mantissaMom, mask) *\
-          2.**(expDad-MANTISSA_BITS-1)
+          2.**(baseExp-MANTISSA_BITS)
         brother[i] = bitcross(mantissaMom, mantissaDad, mask) *\
-          2.**(expMom-MANTISSA_BITS-1)
-        # if abs(sister[i])>20 or abs(brother[i])>20:
-        #     import pdb; pdb.set_trace()
-        sister[i] = clip(sister[i],
-          gMom.getParam('rangemin'),
-          gMom.getParam('rangemax'))
-        brother[i] = clip(brother[i],
-          gMom.getParam('rangemin'),
-          gMom.getParam('rangemax'))
+          2.**(baseExp-MANTISSA_BITS)
+
+        # sister[i] = clip(sister[i],
+        #   gMom.getParam('rangemin'),
+        #   gMom.getParam('rangemax'))
+        # brother[i] = clip(brother[i],
+        #   gMom.getParam('rangemin'),
+        #   gMom.getParam('rangemax'))
+    
     return (sister, brother)
 
 
        f=rndtrip(f)
 
 def clip(f, lbound, ubound):
-    return min(max(f, lbound), ubound)
+    return min(max(f, lbound), ubound)

privateer_pyevolve/privateer_sim.py

     #genome.mutator.set(Mutators.G1DListMutatorRealRange)
     
     # Crossover function must be set to something that can handle 1-elem list
-    genome.crossover.set(Crossovers.G1DListCrossoverUniform)
+    # genome.crossover.set(Crossovers.G1DListCrossoverUniform)
     # genome.crossover.set(PrivateerCrossovers.G1DListCrossoverMean)
-    # genome.crossover.set(PrivateerCrossovers.G1DListCrossoverBitwise)
+    genome.crossover.set(PrivateerCrossovers.G1DListCrossoverBitwise)
     
     # Genetic Algorithm Instance
     ga_engine = GSimpleGA.GSimpleGA(genome,seed=seed)