UserPreferences

SimpleDiffusionCode


  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
import random
import Tkinter

# Set up the graphics window
maxheight = 800
maxwidth = 800
window = Tkinter.Tk()
canvas = Tkinter.Canvas(window, width=maxwidth, height=maxheight, background="white")
canvas.pack()

class agent:
    x = 0
    y = 0
    oldx = 0
    oldy = 0

xincr = 2
yincr = 2
popsize = 500
totalrun = 100

def coin(prob):
    return (random.random() < prob)


pop = []
for indiv in range(popsize):
    pop.append(agent())

for time in range(totalrun):
    for indiv in range(len(pop)):

        if coin(0.5):
            pop[indiv].x = pop[indiv].x + xincr
        else:
            pop[indiv].x = pop[indiv].x - xincr

        if coin(0.5):
            pop[indiv].y = pop[indiv].y + yincr
        else:
            pop[indiv].y = pop[indiv].y - yincr
        canvas.create_line(400+pop[indiv].oldx,400+pop[indiv].oldy,400+pop[indiv].x,400+pop[indiv].y)
        pop[indiv].oldx = pop[indiv].x
        pop[indiv].oldy = pop[indiv].y
    print time + 1