I have a two-dimensional grid of one and zeros. A cluster is defined as an off-diagonal set of neighbors. For example, if we look at the grid:
[[0 0 0 0 0] [1 1 1 1 1] [1 0 0 0 1] [0 1 0 0 1] [1 1 1 1 0]]
One cluster will be a set of coordinates (I actually use lists for this, but that doesn't matter):
c1=[[1, 0], [1, 1], [1, 2], [1, 3], [1, 4], [2, 1], [2, 4], [3, 4]]
Another cluster in this grid is specified:
c2=[[3,1], [4, 0], [4, 1], [4, 2], [4, 3]]
Now I have created a method that for a given initial coordinate (if this value is 1) returns the cluster to which this point belongs (for example, if I select the coordinate [1,1], it will return c1).
For testing, I will select a point (1, 1)
and a small grid. This is the result when the result is good:
Number of recursions: 10 Length of cluster: 10 [[1 1 1 0 1] [1 1 0 1 1] [0 1 0 0 1] [1 1 1 0 0] [0 1 0 1 1]] [[1 1 1 0 0] [1 1 0 0 0] [0 1 0 0 0] [1 1 1 0 0] [0 1 0 0 0]]
I was trying to understand how fast my algorithm is when the cluster size increases. If I run the program and then repeat it and do it many times, it always gives a good result. If I use a loop, it starts to give incorrect results. Here is one possible test case scenario:
Number of recursions: 10 Length of cluster: 10 [[1 1 1 0 1] [1 1 0 1 1] [0 1 0 0 1] [1 1 1 0 0] [0 1 0 1 1]] [[1 1 1 0 0] [1 1 0 0 0] [0 1 0 0 0] [1 1 1 0 0] [0 1 0 0 0]] Number of recursions: 8 Length of cluster: 8 [[0 1 1 1 0] [1 1 1 0 0] [1 0 0 0 0] [1 1 1 0 1] [1 1 0 0 0]] [[0 0 0 0 0] - the first one is always good, this one already has an error [1 1 0 0 0] [1 0 0 0 0] [1 1 1 0 0] [1 1 0 0 0]] Number of recursions: 1 Length of cluster: 1 [[1 1 1 1 1] [0 1 0 1 0] [0 1 0 0 0] [0 1 0 0 0] [0 1 1 0 1]] [[0 0 0 0 0] - till end [0 1 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0]] Number of recursions: 1 Length of cluster: 1 [[1 1 1 1 1] [0 1 1 0 0] [1 0 1 1 1] [1 1 0 1 0] [0 1 1 1 0]] [[0 0 0 0 0] [0 1 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0]] ... till end
I will give the code for the loop (this is not a problem giving you all the code, but it is too big, and the error is probably related to what I am doing inside the loop):
import numpy as np from time import time def test(N, p, testTime, length): assert N>0 x=1 y=1 a=PercolationGrid(N)
I assume that I am doing something wrong with freeing up memory or something (unless it is some trivial error in a loop that I cannot see)? I am using python 2.7.3 on 64 bit Linux.
EDIT: I know that people here should not consider whole codes, but specific problems, but I canβt find what is happening, the only assumption is that maybe I have some static variables, but it seems to me that this not this way. So, if someone has good will and energy, you can look at the code, and maybe you will see something. I started using classes not earlier, so be prepared for a lot of bad things.
import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt import time class ProbabilityGrid(object): """ This class gives 2D quadratic array (a grid) which is filled with float values from 0-1, which in many cases represent probabilities """ def __init__(self, size=2, dataType='float16'): """initialization of a grid with 0. values""" assert size>1 assert dataType=='float64' or dataType=='float32' or dataType=='float16' self.n=size self.dataType=dataType self.grid=np.zeros((size, size), dtype=dataType) def getGrid(self): """returns a 2D probability array""" return self.grid def getSize(self): """returns a size of a 2D array""" return self.size def fillRandom(self): """fills the grid with uniformly random values from 0 to 1""" n=self.n self.grid=np.random.rand(n, n) def fixedProbabilities(self, p): """fills the grid with fixed value from 0 to 1""" assert p<1.0 self.grid=p*np.ones((self.n, self.n)) class PercolationGrid(object): """ percolation quadratic grid filled with 1 and 0, int8 which represent a state. Percolation grid is closly connected to probabilies grid. ProbabilityGrid gives the starting probabilities will the [i,j] spot be filled or not. All functions change the PercolationGrid.grid when ProbabilityGrid.grid changes, so in a way their values are connected """ def __init__(self, size=2, dataType='int8'): """ initialization of PercolationGrid, sets uniformly 0 and 1 to grid """ assert size>1 assert dataType=='int64' or dataType=='int32' or dataType=='int8' self.n=size self.dataType=dataType self.grid=np.zeros((size, size), dtype=dataType) self.pGrid=ProbabilityGrid(self.n) self.pGrid.fillRandom() self.useProbabilityGrid()