Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. import random
  2. import pylab
  3. import numpy as np
  4.  
  5.  
  6. def initialize_model_matrix():
  7. matrix = np.zeros((100, 100))
  8. return matrix
  9.  
  10.  
  11. def set_cells(matrix, count):
  12. cells_count = count
  13. random_dict = {}
  14. for i in range(100):
  15. random_dict[i] = []
  16. while cells_count > 0:
  17. random_row = random.randint(0, 99)
  18. random_col = random.randint(0, 99)
  19. if random_col not in random_dict[random_row]:
  20. matrix[random_row, random_col] = 1
  21. random_dict[random_row].append(random_col)
  22. cells_count -= 1
  23.  
  24.  
  25. def check_cells(matrix):
  26. for i in range(100):
  27. for j in range(100):
  28. if matrix[i][j] in [1, 2]:
  29. return True
  30. return False
  31.  
  32.  
  33. def count_cells(matrix):
  34. count = 0
  35. for i in range(100):
  36. for j in range(100):
  37. if matrix[i][j] in [1, 2]:
  38. count = count + 1
  39. return count
  40.  
  41.  
  42. def model_cells(matrix, q, p):
  43. q_cells = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
  44. iterations_result = 0
  45. while check_cells(matrix):
  46. for i in range(100):
  47. for j in range(100):
  48. if matrix[i][j] == 0:
  49. q_count = q
  50. if (i in [0, 99]) and (j in [0, 99]) and (q_count > 3):
  51. q_count = 3
  52. if ((((i in [0, 99]) and (j in [x for x in range(1, 99, 1)])) or
  53. ((i in [x for x in range(1, 99, 1)]) and (j in [0, 99]))) and (q_count > 5)):
  54. q_count = 5
  55. used_indexes = []
  56. selected_cells = []
  57. while q_count > 0:
  58. random_index = random.randint(0, 7)
  59. if ((random_index not in used_indexes) and (100 > i + q_cells[random_index][0] > -1) and
  60. (100 > j + q_cells[random_index][1] > -1)):
  61. used_indexes.append(random_index)
  62. selected_cells.append(q_cells[random_index])
  63. q_count -= 1
  64. for cell in selected_cells:
  65. if matrix[i + cell[0]][j + cell[1]] in [1, 2]:
  66. random_p_num = random.random()
  67. if random_p_num < p:
  68. matrix[i][j] = 1
  69. continue
  70. if matrix[i][j] == 1:
  71. matrix[i][j] = 2
  72. continue
  73. if matrix[i][j] == 2:
  74. matrix[i][j] = 3
  75. continue
  76. if count_cells(matrix) > 50:
  77. iterations_result += 1
  78. return iterations_result
  79.  
  80.  
  81. model_matrix = initialize_model_matrix()
  82. set_cells(model_matrix, 10)
  83. q_parameters = []
  84. time = []
  85. for q in range(1, 9, 1):
  86. current_matrix = np.copy(model_matrix)
  87. time.append(model_cells(current_matrix, q, 0.9))
  88. q_parameters.append(q)
  89. pylab.plot(q_parameters, time)
  90. pylab.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement