关于python:for循环在模拟中不能正常工作

For loop not working as it should in the simulation

下面是我在Python中创建的一个代码(这是一个相当新的代码)。

我正在尝试对生物膜进行模拟,但在将其应用于实现实际算法(生长的数学公式)和图形部分之前,我希望我的代码以所需的方式运行。我在生成部分面临for循环的问题。

我面临的问题是,从第1代到第2代的增长(根据打印输出)比应该增长的多。增长应该只到相邻的行,但在一个步骤中,它会变得很多。

我们称之为生成函数的矩阵。假设循环开始时有5个节点。现在,当我们在第一个节点上运行generate时,假设添加了1个节点。现在,循环不应该在这一代中新添加的节点上运行generate。但确实如此,这导致了指数增长。

请帮我找出这里的问题。

代码(在Python版本2.7.4中):

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import math
from random import choice
from copy import deepcopy

width=20
height=20

def checksuround(self,i,j):
    memory=0
    if(i+1<width and j+1<height and j-1>=0 and i-1>=0):
        for m in range(-1,2):
            for n in range(-1,2):
                if(self[i+m][j+n]==0):
                    memory=1
        if memory==1:
            return 1
        else:
            return 0

#comment
def add(matrix,m,n,sites):
    count=0
    if(m+1<width and n+1<height and n-1>=0 and m-1>=0):
        for q in range(-1,2):
            for w in range(-1,2):
                if(matrix[m+q][n+w]==0 and count<sites):
                    matrix[m+q][n+w]='.'
                    count=count+1


def generate(substrate,self,i,j):
    if(i+1<width and j+1<height and j-1>=0 and i-1>=0):
        if(substrate[i][j]==1):
            pick=[2,3,4,5,6]
            add(self,i,j,choice(pick))
        else:
            add(self,i,j,1)

print"-----------------------------------------------"
print"Basic floor for growth"
grid=[]
for x in range(width):
    grid.append([])
    for y in range(height):
        grid[x].append(0)

for x in range(width):
    print
    for y in range(height):
        print grid[x][y],


print"-----------------------------------------------"
print"Substrate matrix (1 represents sites with favorable growth conditions)"
arr=[0,1,2,3,4,5,6,7]
substrate=[]
for x in range(width):
    substrate.append([])
    for y in range(height):
        substrate[x].append(choice(arr))

for x in range(width):
    print
    for y in range(height):
        print substrate[x][y],

print"-----------------------------------------------"

for x in range(10,12):
    for y in range(10,12):
        grid[x][y]='.'


for x in range(width):
    print
    for y in range(height):
        print grid[x][y],


print"-----------------------------------------------"
generation=5
undergrid=deepcopy(grid)
flag=0

for g in range(generation):
    print"generation :",g
    for x in range(width):
        for y in range(height):
            flag=checksuround(grid,x,y)
            if (grid[x][y]!=0 and flag==1):
                generate(substrate,undergrid,x,y)
    for x in range(width):
        print
        for y in range(height):
            print undergrid[x][y],
    grid=undergrid
    print
    print"----------------------------------------------"

一个输出是这样的:(如果输出没有对齐,请复制粘贴上面的代码并运行它,它应该可以正常工作)

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
generation :  0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0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0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

----------------------------------------------

generation :  2

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 . . 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 . . . . . . . . 0 0 0 0 0
0 0 0 0 0 0 0 0 . . . . . . . 0 0 0 0 0
0 0 0 0 0 0 0 0 . . . . . . . 0 0 0 0 0
0 0 0 0 0 0 0 0 . . . . . . . . 0 0 0 0
0 0 0 0 0 0 0 0 . . . . . . . . . 0 0 0
0 0 0 0 0 0 0 . . . . . . . . . . 0 0 0
0 0 0 0 0 0 0 . . . . . . . . . . 0 0 0
0 0 0 0 0 0 0 . . . . . . . . . . 0 0 0
0 0 0 0 0 0 . . . . . . . . . . . 0 0 0
0 0 0 0 0 0 . . . . . . . . . . . 0 0 0
0 0 0 0 0 0 0 . . . . . . . . . 0 0 0 0
0 0 0 0 0 0 0 . . . . . 0 0 0 0 0 0 0 0

----------------------------------------------

generation :  3

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 . . 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 . . . . . . . . . . 0 0 0 0
0 0 0 0 0 0 0 . . . . . . . . . 0 0 0 0
0 0 0 0 0 0 0 . . . . . . . . . 0 0 0 0
0 0 0 0 0 0 0 . . . . . . . . . . 0 0 0
0 0 0 0 0 0 0 . . . . . . . . . . . . 0
0 0 0 0 0 0 . . . . . . . . . . . . . 0
0 0 0 0 0 . . . . . . . . . . . . . . 0
0 0 0 0 0 . . . . . . . . . . . . . . 0
0 0 0 0 0 . . . . . . . . . . . . . . 0
0 0 0 0 0 . . . . . . . . . . . . . . .
0 0 0 0 0 . . . . . . . . . . . . . . .
0 0 0 0 0 0 . . . . . . . . . . . . . 0
0 0 0 0 0 0 . . . . . . . . . . . . . 0

----------------------------------------------

generation :  4

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 . . . . 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 . . . . . . . . . . . . 0 0 0
0 0 0 0 0 . . . . . . . . . . . . 0 0 0
0 0 0 0 0 0 . . . . . . . . . . . 0 0 0
0 0 0 0 0 0 . . . . . . . . . . . . 0 0
0 0 0 0 0 . . . . . . . . . . . . . . .
0 0 0 0 0 . . . . . . . . . . . . . . .
0 0 0 0 . . . . . . . . . . . . . . . .
0 0 0 0 . . . . . . . . . . . . . . . .
0 0 0 0 . . . . . . . . . . . . . . . .
0 0 0 0 . . . . . . . . . . . . . . . .
0 0 0 0 . . . . . . . . . . . . . . . .
0 0 0 0 . . . . . . . . . . . . . . . .
0 0 0 0 0 . . . . . . . . . . . . . . .
0 0 0 0 0 . . . . . . . . . . . . . . .

----------------------------------------------


在主循环中,使用grid = undergrid,是一个肤浅的拷贝。从现在开始(即在下面的迭代中),gridundergrid都是同一个python列表。尝试EDCOX1×3代替。