這是我在python(相當新的)創建的代碼。For循環不工作,因爲它應該在模擬
我試圖做一個生物膜的模擬,但在我把它實現實際算法(增長的數學公式)和圖形部分我希望我的代碼以期望的方式運行之前。我正在面對生成部分中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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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 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 . . . . . . . . . . . . . . .
----------------------------------------------
謝謝你保佑! :D – DK5 2013-04-09 11:44:17
爲什麼你在'def cheururound(self,i,j)中有'self':'?你是否在課堂上覆制它? – 2013-04-09 11:45:27
@ user2246845隨時歡迎您,歡迎來到StackOverflow :) – 2013-04-09 11:46:16