Commits

dan mackinlay committed 536b623

bitwise crossover kinda works but I am not sure I trust it

Comments (0)

Files changed (2)

privateer_pyevolve/PrivateerCrossovers.py

     return (i1 & mask) | (i2 & ~mask)
     
 def G1DListCrossoverBitwise(genome, **args):
-    """bitwise uniform crossover for floats"""
+    """bitwise uniform crossover for floats. Warning, potentially breaks range
+    constraints."""
     sister = None
     brother = None
     gMom = args["mom"]
     sister.resetStats()
     brother.resetStats()
     
-    
     for i in xrange(len(gMom)):
-        numMask = random.randbits(MANTISSA_BITS) 
-        denomMask = random.randbits(MANTISSA_BITS)
-        mantissaDad, exponentDad = math.frexpr(gDad[i])
-        mantissaMom, exponentMom = math.frexpr(gMom[i])
-        mantissaMom = mantissaMom*MANTISSA_DENOM
+        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)
         
-        sister[i] = bitcross(numDad, numMum, numMask) / \
-          bitcross(denomDad, denomMum, denomMask)
-        brother[i] = bitcross(numMum, numDad, numMask) / \
-          bitcross(denomMum, denomDad, denomMask)
-        
+        #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)
+        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'))
     return (sister, brother)
 
 
     for i in range(100):
        print f.hex()
        f=rndtrip(f)
+
+def clip(f, lbound, ubound):
+    return min(max(f, lbound), ubound)

privateer_pyevolve/privateer_sim.py

 from pyevolve import Statistics
 # from pyevolve import DBAdapters
 from pyevolve import Selectors, Crossovers, Consts, Initializators, Mutators, Scaling
+
+
 import numpy as np
 import matplotlib.pyplot as plt
-
 import market
+import PrivateerCrossovers
 
 import argparse
 
     #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(G1DListCrossoverMean)
+    # genome.crossover.set(Crossovers.G1DListCrossoverUniform)
+    # genome.crossover.set(PrivateerCrossovers.G1DListCrossoverMean)
+    genome.crossover.set(PrivateerCrossovers.G1DListCrossoverBitwise)
     
     # Genetic Algorithm Instance
     ga_engine = GSimpleGA.GSimpleGA(genome,seed=seed)