For循环在模拟中没有正常工作

2024-06-13 18:52:45 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我用python创建的一段代码(对它来说相当陌生)。你知道吗

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

我面临的问题是,从第1代到第2代的增长(根据印刷品产量)比它应该增长的要多得多。增长应该只去相邻的行,但在一个步骤,它变成了很多。你知道吗

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

请帮我找出这里的问题。你知道吗

代码(在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 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 . . 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 . . . . . . 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 . . . . . 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 . . . . 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 . . . . . . . . . . . . . . .

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

Tags: and代码inselfforif节点range
1条回答
网友
1楼 · 发布于 2024-06-13 18:52:45

在主循环中,使用 grid = undergrid,这是一个浅拷贝。从现在开始(即在接下来的迭代中)gridundergrid都是相同的python列表。 试试grid = deepcopy(undergrid)。你知道吗

相关问题 更多 >