Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. import numpy as np
  2. import itertools
  3. import matplotlib.pyplot as plt
  4. import multiprocessing as mp
  5. import time
  6.  
  7.  
  8. def save_ascii(population_list, unique_lists):
  9. data = np.array([population_list, unique_lists])
  10. data = data.T
  11.  
  12. with open("output.txt", 'w+') as datafile_id:
  13. np.savetxt(datafile_id, data, fmt=['%d', '%.2f'])
  14.  
  15.  
  16. def to_trinary(binary_string, twos_index):
  17. output = 0
  18. index = 0
  19. for bit in binary_string:
  20.  
  21. if index in twos_index:
  22. output += 2 * (3 ** index)
  23. elif bit:
  24. output += (3 ** index)
  25.  
  26. index += 1
  27. return output
  28.  
  29.  
  30. class TaskParams:
  31. def __str__(self):
  32. return "Population size: " + str(self.population_size) + " String size: " + str(self.string_size)
  33.  
  34. def notifyStart(self):
  35. self.start_time = time.time()
  36.  
  37. def finish(self):
  38. end_time = time.time()
  39. elapsed = end_time - self.start_time
  40. print("Task: " + str(self) + " finished after: " + str(elapsed) + "seconds")
  41.  
  42. def __init__(self, n_population, string_size, mean_times):
  43. self.population_size = n_population
  44. self.mean_times = mean_times
  45. self.string_size = string_size
  46.  
  47.  
  48. class GeneString:
  49. def __init__(self, n):
  50. self.string = np.random.choice([False, True], n)
  51.  
  52.  
  53. def calculate_mean_unique_schemas(pool_task):
  54. print("Calculating for: " + str(pool_task))
  55.  
  56. times = pool_task.mean_times
  57. string_len = pool_task.string_size
  58. n_population = pool_task.population_size
  59.  
  60. pool_task.notifyStart()
  61.  
  62. possible_starred_indexes = range(0, string_len)
  63. possible_starred_indexes_sets = sum([map(list, itertools.combinations(possible_starred_indexes, i)) for i in
  64. range(len(possible_starred_indexes) + 1)], [])
  65.  
  66. results = [None] * times
  67. for i in range(0, times):
  68. hash_set = set()
  69. for k in range(n_population):
  70. g = GeneString(string_len)
  71.  
  72. for possible_starred in possible_starred_indexes_sets:
  73. str_copy = g.string.copy()
  74. hash_set.add(to_trinary(str_copy, possible_starred))
  75.  
  76. results[i] = len(hash_set)
  77.  
  78. pool_task.finish()
  79. return np.mean(results)
  80.  
  81.  
  82. def generate_x_axis():
  83. xs = range(0, 100)
  84. xs += range(100, 250, 10)
  85. xs += range(250, 500, 50)
  86. xs += range(500, 1000, 100)
  87. return xs
  88.  
  89.  
  90. def task_one():
  91. xs = generate_x_axis()
  92. pool_tasks = map(lambda x: TaskParams(x, 16, 10), xs)
  93. pool = mp.Pool(mp.cpu_count())
  94. out = pool.map(calculate_mean_unique_schemas, (x for x in pool_tasks))
  95. plt.plot(xs, out)
  96. plt.xlabel('Population size')
  97. plt.ylabel('Unique schemas')
  98. plt.show()
  99.  
  100. save_ascii(xs, out)
  101.  
  102.  
  103. if __name__ == '__main__':
  104. mp.freeze_support()
  105. task_one()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement