Commits

dan mackinlay committed 9c5be17

define and use bitwise-like operators on mutation, successfully

Comments (0)

Files changed (2)

privateer_pyevolve/PrivateerMutators.py

             genome[target_i] *= -1
             continue
         #else
-        src_exp = math.frexp(source)[1]
-        delta_exp = src_exp-random.randint(0,52)
+        delta_exp_ceiling = math.frexp(source)[1]
+        delta_exp = delta_exp_ceiling-random.randint(0,52)
         if delta_exp < sys.float_info.min_exp:
             continue
         delta = 2.**(delta_exp)
         
     return mutations
     
-def G1DListCrossoverBitwise(genome, **args):
-    """bitwise uniform crossover for floats. Warning, potentially breaks range
-    constraints."""
-    sister = None
-    brother = None
-    gMom = args["mom"]
-    gDad = args["dad"]
+def G1DListMutatorRealPseudoBitwise(genome, **args):
+    """ The mutator of G1DList, pseudo-bitwise Mutator. Gets range from genome
+    properties
+    """
+    if args["pmut"] <= 0.0: return 0
+    mutations = int(random.expovariate(1./args["pmut"])*53. + 0.5)
 
-    sister = gMom.clone()
-    brother = gDad.clone()
-    sister.resetStats()
-    brother.resetStats()
-    
-    for i in xrange(len(gMom)):
-        mask = random.getrandbits(MANTISSA_BITS) 
-        mantissaDad, expDad = math.frexp(gDad[i])
-        mantissaMom, expMom = math.frexp(gMom[i])
-        baseExp = max(expMom, expDad)
-        # import pdb; pdb.set_trace()
+    delta_exp_ceiling = math.frexp(
+      genome.getParam("rangemax", Consts.CDefRangeMax)-
+      genome.getParam("rangemin", Consts.CDefRangeMax)
+    )[1]
+
+    for it in range(mutations):
+        listSize = len(genome)
+        target_i = random.randint(0, listSize-1)
+        source = genome[target_i]
         
-        mantissaDad = int(mantissaDad*2.**(MANTISSA_BITS-baseExp+expDad))
-        mantissaMom = int(mantissaMom*2.**(MANTISSA_BITS-baseExp+expMom))
+        if  Util.randomFlipCoin(1/53.):
+            #sign flip
+            genome[target_i] *= -1
+            continue
+        #else
+        delta_exp = delta_exp_ceiling-random.randint(0,52)
+        if delta_exp < sys.float_info.min_exp:
+            continue
+        delta = 2.**(delta_exp)
+        delta *= (2.*random.randint(0, 1)-1)
+        print "delta:", delta
+        final_value = source + delta
+        final_value = min(final_value, genome.getParam("rangemax", Consts.CDefRangeMax))
+        final_value = max(final_value, genome.getParam("rangemin", Consts.CDefRangeMin))
+        genome[target_i] = final_value
         
-        #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.**(baseExp-MANTISSA_BITS)
-        brother[i] = bitcross(mantissaMom, mantissaDad, mask) *\
-          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)
-
+    return mutations

privateer_pyevolve/privateer_sim.py

     # Mutator function 
     # genome.mutator.set(Mutators.G1DListMutatorRealGaussian)
     # genome.mutator.set(Mutators.G1DListMutatorRealRange)
-    genome.mutator.set(PrivateerMutators.G1DListMutatorRealPseudoBitwiseAuto)
+    genome.mutator.set(PrivateerMutators.G1DListMutatorRealPseudoBitwise)
     
     # Crossover function must be set to something that can handle 1-elem list
     # genome.crossover.set(Crossovers.G1DListCrossoverUniform)