Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import pylab
- import numpy as np
- def initialize_model_matrix():
- matrix = np.zeros((100, 100))
- return matrix
- def set_cells(matrix, count):
- cells_count = count
- random_dict = {}
- for i in range(100):
- random_dict[i] = []
- while cells_count > 0:
- random_row = random.randint(0, 99)
- random_col = random.randint(0, 99)
- if random_col not in random_dict[random_row]:
- matrix[random_row, random_col] = 1
- random_dict[random_row].append(random_col)
- cells_count -= 1
- def check_cells(matrix):
- for i in range(100):
- for j in range(100):
- if matrix[i][j] in [1, 2]:
- return True
- return False
- def count_cells(matrix):
- count = 0
- for i in range(100):
- for j in range(100):
- if matrix[i][j] in [1, 2]:
- count = count + 1
- return count
- def model_cells(matrix, q, p):
- q_cells = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
- iterations_result = 0
- while check_cells(matrix):
- for i in range(100):
- for j in range(100):
- if matrix[i][j] == 0:
- q_count = q
- if (i in [0, 99]) and (j in [0, 99]) and (q_count > 3):
- q_count = 3
- if ((((i in [0, 99]) and (j in [x for x in range(1, 99, 1)])) or
- ((i in [x for x in range(1, 99, 1)]) and (j in [0, 99]))) and (q_count > 5)):
- q_count = 5
- used_indexes = []
- selected_cells = []
- while q_count > 0:
- random_index = random.randint(0, 7)
- if ((random_index not in used_indexes) and (100 > i + q_cells[random_index][0] > -1) and
- (100 > j + q_cells[random_index][1] > -1)):
- used_indexes.append(random_index)
- selected_cells.append(q_cells[random_index])
- q_count -= 1
- for cell in selected_cells:
- if matrix[i + cell[0]][j + cell[1]] in [1, 2]:
- random_p_num = random.random()
- if random_p_num < p:
- matrix[i][j] = 1
- continue
- if matrix[i][j] == 1:
- matrix[i][j] = 2
- continue
- if matrix[i][j] == 2:
- matrix[i][j] = 3
- continue
- if count_cells(matrix) > 50:
- iterations_result += 1
- return iterations_result
- model_matrix = initialize_model_matrix()
- set_cells(model_matrix, 10)
- q_parameters = []
- time = []
- for q in range(1, 9, 1):
- current_matrix = np.copy(model_matrix)
- time.append(model_cells(current_matrix, q, 0.9))
- q_parameters.append(q)
- pylab.plot(q_parameters, time)
- pylab.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement