| 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
46
47
48
49
50
51
52
53
54
| 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:
def __init__(self,x1,y1):
self.x = x1
self.y = y1
self.oldx = x1
self.oldy = y1
xincr = 2
yincr = 2
popsize = 100
totalrun = 100
reproprob = 0.01
def coin(prob):
return (random.random() < prob)
pop = []
for indiv in range(popsize):
pop.append(agent(maxwidth/2,maxheight/2))
canvas.create_oval(maxwidth/2,maxheight/2,2+maxwidth/2,2+maxheight/2,fill='black')
for time in range(totalrun):
indiv = 0
x1 = pop[indiv].x
y1 = pop[indiv].y
while indiv < len(pop):
if coin(reproprob):
pop.append(agent(x1,y1))
canvas.create_oval(x1,y1,2+x1,2+y1,fill='black')
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.coords(indiv+1,pop[indiv].x,pop[indiv].y,2+pop[indiv].x,2+pop[indiv].y)
pop[indiv].oldx = pop[indiv].x
pop[indiv].oldy = pop[indiv].y
indiv = indiv + 1
print time + 1 |