Commits

dan mackinlay committed cfde387

split crossovers into separate module, and create a new rational-fraction bsed btiwise crossover

  • Participants
  • Parent commits c5f3c66

Comments (0)

Files changed (2)

File privateer_pyevolve/PrivateerCrossovers.py

+from __future__ import division
+import random
+import math
+
+def G1DListCrossoverMean(genome, **args):
+    """Ultra-stupid crossover taking the mean of both parents. Not very useful -
+    AFAICT it results in pressure to converge to the population mean rather than
+    to the solution."""
+
+    sister = None
+    brother = None
+    gMom = args["mom"]
+    gDad = args["dad"]
+
+    sister = gMom.clone()
+    brother = gDad.clone()
+    sister.resetStats()
+    brother.resetStats()
+
+    for i in xrange(len(gMom)):
+      sister[i] = brother[i] = (gMom[i]+gDad[i])/2.0
+        
+    return (sister, brother)
+
+def bitcross(i1, i2, mask):
+    """return a new int, containing its chosen from i1 or i2 depending on whther the mask string is 1 or 0"""
+    return (i1 & mask) | (i2 & ~mask)
+    
+def G1DListCrossoverBitwise(genome, **args):
+    """bitwise uniform crossover for floats"""
+
+    sister = None
+    brother = None
+    gMom = args["mom"]
+    gDad = args["dad"]
+
+    sister = gMom.clone()
+    brother = gDad.clone()
+    sister.resetStats()
+    brother.resetStats()
+    
+    
+    for i in xrange(len(gMom)):
+        numMask = random.randbits(53) #maximal python mantissa length
+        denomMask = random.randbits(53)
+        numDad, denomDad = gDad[i].as_integer_ratio()
+        numMom, denomMom = gMom[i].as_integer_ratio()
+        
+        sister[i] = bitcross(numDad, numMum, numMask) / \
+          bitcross(denomDad, denomMum, denomMask)
+        brother[i] = bitcross(numMum, numDad, numMask) / \
+          bitcross(denomMum, denomDad, denomMask)
+        
+    return (sister, brother)
+
+

File privateer_pyevolve/privateer_sim.py

 
 import argparse
 
-def G1DListCrossoverMean(genome, **args):
-    """Ultra-stupid crossover taking the mean of both parents. Not very useful -
-    AFAICT it results in pressure to converge to the population mean rather than
-    to the solution."""
-
-    sister = None
-    brother = None
-    gMom = args["mom"]
-    gDad = args["dad"]
-
-    sister = gMom.clone()
-    brother = gDad.clone()
-    sister.resetStats()
-    brother.resetStats()
-
-    for i in xrange(len(gMom)):
-      sister[i] = brother[i] = (gMom[i]+gDad[i])/2.0
-        
-    return (sister, brother)
    
 def main():
     import privateers