Advertisement
dsuveges

Untitled

May 2nd, 2016
1,220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.78 KB | None | 0 0
  1. import numpy as np
  2. import seaborn as sns; sns.set()
  3. %matplotlib inline
  4.  
  5. # Steps:
  6. # 1. Generate a large matrix - fill with zeroes
  7. # 2. Initialize some structure as a starting point.
  8. # 3. Calculate elements that are dying.
  9. # 4. Calculate elements that are being born.
  10. # 5. Add new elements and remove the died ones.
  11. # 6. plot....
  12.  
  13.  
  14. def get_dying(matrix):
  15.     '''
  16.    This function returns a list of coordinates of the dying
  17.    cells.
  18.    '''
  19.     nonZero = np.nonzero(matrix)
  20.     dying = []
  21.     for index, x in enumerate(nonZero[0]):
  22.         # Find dying cells:
  23.         y = nonZero[1][index]
  24.         # Get all non-zero element.
  25.        
  26.         # Dying because of of overpopulation (More than 3 neighbour):
  27.         if  get_neighbours(x,y,matrix) > 3:
  28.             dying.append((x,y))
  29.  
  30.         # Dying because of of under population (Less than two neighbour):
  31.         if get_neighbours(x,y,matrix) < 2:
  32.             dying.append((x,y))
  33.  
  34.     # Returning the coordinates of dying cells:
  35.     return dying
  36.  
  37. def get_neighbours(x,y,matrix):
  38.     '''
  39.    given the x and y coordnates of a matrix, this function
  40.    returns the number of non-zero neighbours of given coordinate
  41.    '''
  42.     # print matrix[(x-1):(x+2), (y-1):(y+2)]
  43.     if matrix[x,y] == 1:
  44.         return  np.count_nonzero(matrix[(x-1):(x+2), (y-1):(y+2)]) - 1
  45.     else:
  46.         return np.count_nonzero(matrix[(x-1):(x+2), (y-1):(y+2)])
  47.  
  48. def get_born(matrix):
  49.     '''
  50.    this function returns with a list of tuples containing the
  51.    coordinates of newly borning cells.
  52.    '''
  53.  
  54.     nonZero = np.nonzero(matrix)
  55.     born = []
  56.     for index, x in enumerate(nonZero[0]):
  57.         # Find dying cells:
  58.         y = nonZero[1][index]
  59.  
  60.         # Checking zero elements around the non-zero elements:
  61.         for dx in [x - 1, x, x + 1]:
  62.             for dy in [y - 1, y, y + 1]:
  63.                 if 0 <= dx < width and 0 <= dy < height:
  64.                     if get_neighbours(dx, dy, matrix) == 3:
  65.                         born.append((dx,dy))
  66.     return born
  67.    
  68.  
  69. # initialize starting structure:
  70. def initialize(matrix):
  71.     '''
  72.    This function takes a matrix as an input and at a
  73.    random position creates a five element big "patch of ones"
  74.    '''
  75.    
  76.     # get dimension of the matrix:
  77.     (w, h) = np.shape(matrix)
  78.    
  79.     # Get random coordinate:
  80.     x = np.random.choice(w)
  81.     y = np.random.choice(h)
  82.    
  83.     # Update matrix:
  84.     matrix[x,y] = 1
  85.    
  86.     for i in range(8):
  87.         # Updating x and y:
  88.         (dx, dy) = np.random.choice([-1,0,1], 2)
  89.         if 0 < x + dx < w and 0 < y + dy < h:
  90.             i += 1
  91.             x = x + dx
  92.             y = y + dy
  93.             matrix[x,y] = 1
  94.    
  95.     # Returning the modified matrix
  96.     return matrix
  97.  
  98.  
  99.  
  100. def get_new_gen(matrix):
  101.    
  102.     # Before any modification is done, we get the
  103.     # list of cells:
  104.     dying_cells = get_dying(matrix)
  105.     borning_cells = get_born(matrix)
  106.  
  107.     # Updating matrix:
  108.     for dying in dying_cells:
  109.         # print "dying: ", dying[0], dying[1]
  110.         matrix[dying[0], dying[1]] = 0
  111.  
  112.     for born in borning_cells:
  113.         # print "born: ", born[0], born[1]
  114.         matrix[born[0], born[1]] = 1
  115.  
  116.     return matrix
  117.  
  118.  
  119. # Initialize matrix with a given size:
  120. width = 200
  121. height = 200
  122. matrix = np.zeros((width, height))
  123.  
  124. ## Initialize 20 starting object:
  125. for i in range(60):
  126.     matrix = initialize(matrix)
  127.    
  128. # Plot the initial structure:
  129. sns.heatmap(matrix, xticklabels=False, yticklabels=False, cbar=False)
  130.  
  131. # Create generations (Let's say 250):
  132. for i in range(250):
  133.     get_new_gen(matrix)
  134.     ## Plotting heatmap:
  135.     plot = sns.heatmap(matrix, xticklabels=False, yticklabels=False, cbar=False)
  136.     x = plot.get_figure()
  137.     filename = "output_%02d.png" % i
  138.     x.savefig(filename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement