SHARE
TWEET

Untitled

a guest Oct 21st, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #%%
  2. # Code based on:
  3. # *** Forest fire ***
  4. #
  5. # Copyright 2008-2012 Hiroki Sayama
  6. # sayama@binghamton.edu
  7. #
  8. # Ported to Python 3 and added parameter setters
  9. # by Przemyslaw Szufel & Bogumil Kaminski
  10. # {pszufe, bkamins}@sgh.waw.pl
  11. #
  12. # Modified for ISZ course purposes by Grzegorz Szeremeta and Dariusz Szymczyk
  13. #
  14. import matplotlib
  15. #matplotlib.use('qt4agg')
  16.  
  17. import pylab as PL
  18. import random as RD
  19. import scipy as SP
  20.  
  21. RD.seed()
  22.  
  23. width = 100
  24. height = 100
  25. bad_guys = 50
  26. good_guys = 200
  27.  
  28. good = 1
  29. bad = -1
  30. empty = 0
  31.  
  32. def is_bad(val):
  33.     return 0>val>-1
  34.  
  35. def is_good(val):
  36.     return 0<val<1
  37.  
  38. def init():
  39.     global time, config, nextConfig
  40.     bad_cnt = bad_guys
  41.     good_cnt = good_guys
  42.     time = 0
  43.  
  44.     config = SP.zeros([height, width])
  45.     for x in range(width):
  46.         for y in range(height):
  47.             if 0<RD.random() < 0.1 and bad_cnt>0:
  48.                 state = bad
  49.                 bad_cnt-=1
  50.             elif 0.1<RD.random()<0.5 and good_cnt>0:
  51.                 state = good
  52.                 good_cnt-=1
  53.             else:
  54.                 state = empty
  55.             config[y, x] = state
  56.     while bad_cnt>0:
  57.         x = abs(RD.random()*(width-1))
  58.         y = abs(RD.random()*(height-1))
  59.         if config[y,x] == empty:
  60.             config[y,x]=bad
  61.             bad_cnt-=1
  62.     while good_cnt>0:
  63.         x = abs(RD.random()*(width-1))
  64.         y = abs(RD.random()*(height-1))
  65.         if config[y,x] == empty:
  66.             config[y,x]=good
  67.             good_cnt-=1
  68.     nextConfig = SP.zeros([height, width])
  69.  
  70. def draw():
  71.     PL.cla()
  72.     PL.pcolor(config, vmin = -1, vmax = 1, cmap = PL.cm.binary)
  73.     PL.axis('image')
  74.     PL.title('t = ' + str(time))
  75.  
  76. def find_empty(x,y):
  77.     global config
  78.     for dx in range(-1, 2):
  79.         new_x = (x+dx)%width
  80.         for dy in range(-1, 2):
  81.             new_y=(y+dy)%height
  82.             if config[new_y,new_x] == empty:
  83.                 return new_y,new_x
  84.     return -1,-1
  85. def create_offspring(x,y):
  86.     global config,nextConfig, good_guys, bad_guys
  87.     new_y,new_x = find_empty(x,y)
  88.     if RD.random()<0.5:
  89.         nextConfig[new_y,new_x] = bad
  90.     else:
  91.         nextConfig[new_y,new_x] = good
  92.        
  93.        
  94. def check_surrounding_good(y,x):
  95.     global config
  96.     for dx in range(-1, 2):
  97.         for dy in range(-1, 2):
  98.             if config[(y+dy)%height, (x+dx)%width] == good:
  99.                 create_offspring(x,y)
  100.                 config[y,x]-=.1
  101.                 return
  102.  
  103. def check_surrounding_bad(y,x):
  104.     global config, good_guys
  105.     for dx in range(-1, 2):
  106.         nb_x = (x+dx)%width
  107.         for dy in range(-1, 2):
  108.             nb_y = (y+dy)%height
  109.             if config[nb_y,nb_x] == good:
  110.                 nextConfig[nb_y,nb_x] = empty
  111.                 good_guys-=1
  112.                 config[y,x] -=.1
  113.                 return
  114.                            
  115.    
  116. def step():
  117.     global time, config, nextConfig,good_guys,bad_guys
  118.  
  119.     time += 1
  120.  
  121.     height, width = config.shape
  122.  
  123.     for x in range(width):
  124.         for y in range(height):
  125.             state = config[y, x]
  126.             if state == good:
  127.                 check_surrounding_good(y,x)
  128.                 new_y,new_x = find_empty(x,y)
  129.                 if config[y,x]>0.2:
  130.                     nextConfig[new_y,new_x] = (config[y,x] -0.1)
  131.                 else:
  132.                     good_guys-=1
  133.                     nextConfig[new_y,new_x] = empty
  134.             elif state == bad:
  135.                 check_surrounding_bad(y,x)
  136.                 new_y,new_x = find_empty(x,y)
  137.                 if config[y,x]<-0.2:
  138.                     nextConfig[new_y,new_x] = (config[y,x] +0.1)
  139.                 else:
  140.                     bad_guys-=1
  141.                     nextConfig[new_y,new_x] = empty
  142.                
  143.  
  144.     config, nextConfig = nextConfig, config
  145.  
  146. import pycxsimulator
  147. pycxsimulator.GUI().start(func=[init,draw,step])
  148.  
  149.  
  150.  
  151. #%%
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top