Advertisement
Guest User

Untitled

a guest
Jan 8th, 2010
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.78 KB | None | 0 0
  1. import random
  2. from matplotlib.pyplot import show, imshow
  3.  
  4. #random_fun = lambda: random.gauss(0.5, 0.2)
  5. random_fun = random.random
  6.  
  7. class T(object):
  8.     @staticmethod
  9.     def regen(x,y):
  10.         return [[(random_fun(), random_fun(), 1.0) for i in xrange(y)] for j in xrange(x)]
  11.  
  12.     @staticmethod
  13.     def eachpair(x,y):
  14.         for i in xrange(x):
  15.             for j in xrange(y):
  16.                 yield (i,j)
  17.  
  18.     @staticmethod
  19.     def diagonally(x,y):
  20.         for s in xrange(x+y):
  21.             for i in xrange(max(0, s-x), min(s,x)):
  22.                 yield i, s-i-1
  23.  
  24.     @staticmethod
  25.     def iterchange(tab, steps, ev=10):
  26.         changes = 0
  27.         x = len(tab)
  28.         y = len(tab[0])
  29.         for s in xrange(steps):
  30.             if s%ev==0:
  31.                 print s,T.measure(tab)
  32.             for i,j in T.diagonally(x, y):
  33.                 if i+1<x and tab[i][j][0] < tab[i+1][j][0]:
  34.                     tab[i][j], tab[i+1][j] = tab[i+1][j], tab[i][j]
  35.                     changes+=1
  36.                 if j+1<y and tab[i][j][1] < tab[i][j+1][1]:
  37.                     tab[i][j], tab[i][j+1] = tab[i][j+1], tab[i][j]
  38.                     changes+=1
  39.         print s+1, T.measure(tab), changes
  40.  
  41.     @staticmethod
  42.     def measure(tab):
  43.         count = 0;
  44.         x = len(tab)
  45.         y = len(tab[0])
  46.         for i in xrange(x-1):
  47.             for j in xrange(y-1):
  48.                 if tab[i][j][0] < tab[i+1][j][0]:
  49.                     count+=1
  50.                 if tab[i][j][1] < tab[i][j+1][1]:
  51.                     count+=1
  52.         return count
  53.  
  54.     @staticmethod
  55.     def display(tab):
  56.         imshow(tab)
  57.         show()
  58.  
  59. # usage:
  60. # launch `ipython`
  61. # enter `%run thisfile.py`
  62. # then enter lines below
  63. tab = T.regen(100, 100)
  64. T.iterchange(tab, 1000, 50)
  65. T.display(tab)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement