Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.10 KB | None | 0 0
  1. import numpy as np
  2. from copy import copy
  3. import matplotlib.pyplot as plt
  4.  
  5.  
  6. MAX_SCHEMATIC_SIZE = 32
  7.  
  8.  
  9. def unpackbits(x, num_bits):
  10.     xshape = list(x.shape)
  11.     x = x.reshape([-1, 1])
  12.     to_and = 2**np.arange(num_bits).reshape([1, num_bits])
  13.     return (x & to_and).astype(bool).astype(int).reshape(xshape + [num_bits])
  14.  
  15.  
  16. def generate_and_count(n, l, masks):
  17.     counts = {}
  18.     count = 0
  19.  
  20.     for _ in range(n):
  21.         arr = np.random.choice([0, 1], l)
  22.         for i in range(0, 2**l):
  23.             temp = copy(arr)
  24.             mask = masks[i]
  25.             temp[np.where(mask)] = 2
  26.             key = 0
  27.  
  28.             for j in range(l):
  29.                 key += temp[j] * 3 ** j
  30.  
  31.             if key not in counts:
  32.                 counts[key] = 1
  33.                 count += 1
  34.  
  35.     return count
  36.  
  37.  
  38. def pop_func(l):
  39.     n_pops, avg_counts = [], []
  40.     masks = [unpackbits(np.array(i), l) for i in range(0, 2 ** l)]
  41.     for n in range(1, 100):
  42.         print('generate and count', n)
  43.         n_pops.append(n)
  44.         counts = []
  45.         for _ in range(10):
  46.             counts.append(generate_and_count(n, l, masks))
  47.         avg_counts.append(np.mean(counts))
  48.  
  49.     for n in range(100, 251, 10):
  50.         print('generate and count', n)
  51.         n_pops.append(n)
  52.         counts = []
  53.         for _ in range(10):
  54.             counts.append(generate_and_count(n, l, masks))
  55.         avg_counts.append(np.mean(counts))
  56.  
  57.     for n in range(300, 500, 50):
  58.         print('generate and count', n)
  59.         n_pops.append(n)
  60.         counts = []
  61.         for _ in range(10):
  62.             counts.append(generate_and_count(n, l, masks))
  63.         avg_counts.append(np.mean(counts))
  64.  
  65.     for n in range(500, 1001, 100):
  66.         print('generate and count', n)
  67.         n_pops.append(n)
  68.         counts = []
  69.         for _ in range(10):
  70.             counts.append(generate_and_count(n, l, masks))
  71.         avg_counts.append(np.mean(counts))
  72.  
  73.     return n_pops, avg_counts
  74.  
  75.  
  76. def main():
  77.     n_pops, avg_counts = pop_func(9)
  78.     plt.plot(n_pops, avg_counts)
  79.     plt.show()
  80.  
  81.  
  82. if __name__ == '__main__':
  83.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement