Advertisement
Guest User

Untitled

a guest
May 24th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. import copy , time , pygame , sys , random
  2.  
  3. # Get things up and running
  4.  
  5. pygame.init()
  6.  
  7. # Create a window where stuff can happen
  8.  
  9. my_screen = pygame.display.set_mode( ( 800 , 600 ) , 5 )
  10.  
  11. # Load a font so that we can write something
  12.  
  13. #my_font = pygame.font.SysFont( 'monospace' , 36 )
  14.  
  15. # Our grid's width and height
  16.  
  17. grid_width = 100
  18. grid_height = 50
  19.  
  20. # The position and size of the boxes used to display the grid
  21.  
  22. box_width = 8
  23. box_height = 12
  24.  
  25. grid_x = 0
  26. grid_y = 0
  27.  
  28. # Colour of the boxes on the grid
  29.  
  30. r_color = ( 10 , 10 , 10 ) # Colour for Removed
  31. s_color = ( 0 , 0 , 200 ) # Colour for Susceptible
  32. i_color = ( 200 , 0 , 100 ) # Colour for Infectious
  33. w_color = ( 100 , 100 , 100 ) # Colour for Wall
  34.  
  35. # Beta, gamma and mu are the contact rate, the infectiousness and recovery rate of our disease
  36.  
  37. # A cold due to renewed susceptibility
  38.  
  39. beta = 0.4
  40. gamma = 0.6
  41. mu = 0.6
  42.  
  43. # Initialize grid
  44.  
  45. grid = []
  46.  
  47. # This bit makes sure that our grid is the right size
  48.  
  49. if len( grid ) > grid_height:
  50. grid_height = len( grid )
  51. for row in grid:
  52. if len( row ) > grid_width:
  53. grid_width = len( row )
  54.  
  55. if len( grid ) < grid_height:
  56. grid+= [ [] for i in range( grid_height - len( grid ) ) ]
  57.  
  58. for row in grid:
  59. if len( row ) < grid_width:
  60. row+= [ 1 for i in range( grid_width - len( row ) ) ]
  61.  
  62. # Create a firewall
  63.  
  64. firewall_x = int( grid_width / 3 )
  65.  
  66. for row in grid:
  67. row[ firewall_x ] = 3
  68.  
  69. # Now leave some holes
  70.  
  71. porosity = 0.2
  72. num_holes = int( grid_height * porosity )
  73. hole_count = 0
  74.  
  75. while hole_count < num_holes:
  76. hole_y = random.randint( 0 , grid_height - 1 )
  77. if grid[ hole_y ][ firewall_x ] == 3:
  78. grid[ hole_y ][ firewall_x ] = 1
  79. hole_count += 1
  80.  
  81. # Infect a few random people
  82.  
  83. to_infect = 10
  84.  
  85. for i in range( to_infect ):
  86. cell_available = False
  87. while not( cell_available ):
  88. x = random.randint( int( grid_width / 2 ) , grid_width - 1 )
  89. y = random.randint( int( grid_height / 2 ) , grid_height - 1 )
  90. cell_available = ( grid[ y ][ x ] == 1 )
  91. grid[ y ][ x ] = 2
  92.  
  93. # How to find the neighbours of a cell...
  94.  
  95. neighbor_list = []
  96.  
  97. for j in [ -1 , 0 , 1 ]:
  98. for i in [ -1 , 0 , 1 ]:
  99. if not( i == 0 and j == 0 ):
  100. neighbor_list.append( ( i , j ) )
  101.  
  102. # This creates a loop that only ends when you close the window
  103.  
  104. still_looping = True
  105.  
  106. while still_looping:
  107.  
  108. # This looks to see whether the close button has been pressed
  109.  
  110. for event in pygame.event.get():
  111. if event.type == pygame.QUIT:
  112. still_looping = False
  113.  
  114. # Clean the board before we start!!
  115.  
  116. my_screen.fill( ( 100 , 100 , 150 ) )
  117.  
  118. # Reset counters for the state of the system
  119.  
  120. S = 0
  121. I = 0
  122. R = 0
  123.  
  124. # Create a working copy of our grid so that we can iterate
  125.  
  126. ref_grid = copy.deepcopy( grid )
  127.  
  128. # Now let's iterate
  129.  
  130. for y in range( grid_height ):
  131. for x in range( grid_width ):
  132.  
  133. # Start by displaying the current state
  134.  
  135. cell_val = ref_grid[ y ][ x ]
  136.  
  137. if cell_val == 0:
  138. my_color = r_color
  139. if cell_val == 1:
  140. my_color = s_color
  141. if cell_val == 2:
  142. my_color = i_color
  143. if cell_val == 3:
  144. my_color = w_color
  145.  
  146. pygame.draw.rect( my_screen , my_color , ( grid_x + box_width * x , grid_y + box_height * y , box_width , box_height ) , 0 )
  147.  
  148. # Counter for neighbours
  149.  
  150. neighbor_count = 0
  151.  
  152. # Work through neighbours
  153.  
  154. for ( i , j ) in neighbor_list:
  155. neighbor_x = x + i
  156. neighbor_y = y + j
  157.  
  158. # As long as they are on the grid!
  159.  
  160. if neighbor_x >= 0 and neighbor_y >= 0 and neighbor_x < grid_width and neighbor_y < grid_height:
  161.  
  162. # If so then count whether they are infected
  163. if ref_grid[ neighbor_y ][ neighbor_x ] == 2:
  164. neighbor_count = neighbor_count + 1
  165.  
  166. # Then iterate according to our rules
  167.  
  168. # If dead/removed then we should roll a die to come to life
  169.  
  170. if cell_val == 0:
  171. R += 1
  172. if random.random() < mu:
  173. cell_val = 1
  174.  
  175. # If alive with an infected neighbour then roll a die to infect
  176.  
  177. if cell_val == 1 and neighbor_count >0:
  178. S += 1
  179. if random.random() < beta:
  180. cell_val = 2
  181.  
  182. # If infected then roll a die to die
  183.  
  184. if cell_val == 2:
  185. I += 1
  186. if random.random() < gamma:
  187. cell_val = 0
  188.  
  189. # Update the grid
  190.  
  191. grid[ y ][ x ] = cell_val
  192.  
  193. # Show off our handiwork
  194.  
  195. pygame.display.update()
  196.  
  197. # Remind us of the values of the parameters and the current state
  198.  
  199. print( '\n\n beta = ' , beta , '\t\t S = ' , S)
  200. print( 'gamma = ' , gamma , '\t\t I = ' , I )
  201. print( ' mu = ' , mu , '\t\tR = ', R , '\n\n' )
  202.  
  203. # Finish things off
  204. pygame.quit()
  205. sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement