这是我在 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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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
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 . . . . . . . . . . . . . . .
----------------------------------------------