0

Here is a code that I created in python (fairly new to it).

I am trying to make a simulation for biofilms, but before I take it to implementing the actual algorithms (mathematical formulas for growth) and the graphics part i want my code to run in the desired manner. I am facing issues with the for loops in the generation part.

The problem that i am facing is that the growth from generation 1 to 2 ( as per the printed output) is increasing much more than it should. The growth should only go to adjacent rows but in one step it becomes a lot.

There is a matrix on which we call the generate function. Say there are 5 nodes at the start of the loop. Now when we run generate on the first node then lets say 1 node gets added. Now the loop should not run generate on this newly added node in this generation. But it does, which leads to exponential growth.

Please help me identify the problem over here.

The code (in python version 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 "----------------------------------------------"

One output was this: (if the output is not aligned please copy paste the above code and run it, it should work fine)

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

----------------------------------------------
DK5
  • 317
  • 3
  • 15
  • why do you have `self` in `def checksuround(self,i,j):`? Did you copy it from within a class? – Francesco Montesano Apr 09 '13 at 11:45
  • @user2246845 You're welcome any time, and welcome to StackOverflow :) – Paolo Moretti Apr 09 '13 at 11:46
  • I thought that self is a local attribute and separate instances of self are created in generate and checksuround. So self is a matrix that is being sent inside the function. I did so because that is how we do it in C,me having experience of using C. Are you suggesting that the problem could be arising because the same self is being refered to in both places ? – DK5 Apr 09 '13 at 11:48
  • It's just a variable name. In python usually `self` is used to identifying the class instance in class methods see e.g. [here](http://docs.python.org/2/tutorial/classes.html#random-remarks) or [here](http://stackoverflow.com/questions/475871/python-why-use-self-in-a-class). It's better if you rename your variable to `matrix` as in `def add(..)`. Give a look at `numpy` for array and matrices manipulations and operations (it can avoid you a number of `for` loops – Francesco Montesano Apr 09 '13 at 11:55
  • oh so i was using a predefined keyword ! okay thank you for pointing that out sit. – DK5 Apr 09 '13 at 14:17

1 Answers1

2

In your main loop, you use grid = undergrid, which is a shallow copy. From this point on (i.e. in the following iterations) both grid and undergrid are the same python list. Try grid = deepcopy(undergrid) instead.

Elmar Peise
  • 14,014
  • 3
  • 21
  • 40
  • Ahaa ! there the error lies. I searched and found a similar solution hence before the loop i made 'undergrid=deepcopy(grid)' but didnt notice that inside the loop 'grid = undergrid' . Thank you very much sir for pointing that out. It helps really well !! – DK5 Apr 09 '13 at 11:55