这是我在python中创建的代码(相当新的代码)。
我正在尝试对生物膜进行模拟,但在我将其用于实现实际算法(增长的数学公式)和图形部分之前,我希望我的代码以所需的方式运行。我正面临着生成部分中for循环的问题。
我面临的问题是,从第1代到第2代(按照打印输出)的增长正在增长得比应有的多得多。增长应该只到相邻的行,但只需一步就可以了。
我们称之为生成函数的矩阵。假设循环开始时有5个节点。现在,当我们在第一个节点上运行generate时,可以说添加了1个节点。现在循环不应该在这一代中新添加的节点上运行生成。但它确实会导致指数级增长。
请帮我在这里找出问题。
代码(在python版本2.7.4中):
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 "----------------------------------------------"
一个输出是这样的:(如果输出未对齐,请复制粘贴上面的代码并运行它,它应该可以正常工作)
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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 . . 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 . . . . 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 . . 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 : 1
0 0 0 0 0 0 0 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 . . . . . . . . . . . 0 0 0
0 0 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 . . . . . . . . . . . . . . .
----------------------------------------------
答案 0 :(得分:2)
在你的主循环中,你使用
grid = undergrid
,这是一个浅层副本。从这一点开始(即在以下迭代中)grid
和undergrid
都是相同的python列表。
请改为grid = deepcopy(undergrid)
。