SHARE
TWEET

Untitled

a guest Dec 16th, 2018 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     def binary_to_float(self, chromosome):
  2.  
  3.         float_representation = []
  4.         dg = self.range[0]
  5.         gg = self.range[1]
  6.         for binary in chromosome:
  7.             b = 0
  8.             for i in range(self.numOfBits):
  9.                 b += binary[i] * math.pow(2,i)
  10.  
  11.             x = dg + (float) (b / (math.pow(2 ,self.numOfBits) - 1))*(gg-dg)
  12.  
  13.             float_representation.append(x)
  14.         return np.array(float_representation)
  15.  
  16.  
  17.     def binary_crossover(self,p1, p2):
  18.         new = []
  19.         for a,b in zip(p1,p2):
  20.             r = np.random.randint(2, size=self.numOfBits)
  21.  
  22.             x = np.bitwise_or(np.bitwise_and(a,b), np.bitwise_and(r,np.bitwise_xor(a,b)))
  23.  
  24.             new.append(x)
  25.         return new
  26.  
  27.  
  28.     def binary_mutation(self, chromosome):
  29.         mutated = []
  30.         for bin_vec in chromosome:
  31.             mutated_bin_vec = len(bin_vec) * [0]
  32.  
  33.             for i in range(len(bin_vec)):
  34.  
  35.                 if random.random() < self.p:
  36.                     mutated_bin_vec[i] = 1 if bin_vec[i] == 0 else 0
  37.                 else:
  38.                     mutated_bin_vec[i] = bin_vec[i]
  39.  
  40.             mutated.append(np.array(mutated_bin_vec))
  41.         return mutated
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top