depth-first algorithm in python does not work

498 views Asked by At

I have some project which I decide to do in Python. In brief: I have list of lists. Each of them also have lists, sometimes one-element, sometimes more. It looks like this:

rules=[
[[1],[2],[3,4,5],[4],[5],[7]]
[[1],[8],[3,7,8],[3],[45],[12]]
[[31],[12],[43,24,57],[47],[2],[43]]
]

The point is to compare values from numpy array to values from this rules (elements of rules table). We are comparing some [x][y] point to first element (e.g. 1 in first element), then, if it is true, value [x-1][j] from array with second from list and so on. Five first comparisons must be true to change value of [x][y] point. I've wrote sth like this (main function is SimulateLoop, order are switched because simulate2 function was written after second one):

def simulate2(self, i, j, w, rule):
        data = Data(rule)
        if w.world[i][j] in data.c:
            if w.world[i-1][j] in data.n:
                if w.world[i][j+1] in data.e:
                    if w.world[i+1][j] in data.s:
                        if w.world[i][j-1] in data.w:
                            w.world[i][j] = data.cc[0]
                        else: return
                    else: return
                else: return
            else: return
        else: return




def SimulateLoop(self,w):
    for z in range(w.steps):
            for i in range(2,w.x-1):
                for j in range(2,w.y-1):
                    for rule in w.rules:
                        self.simulate2(i,j,w,rule)

Data class:

class Data:
    def __init__(self, rule):
        self.c = rule[0]
        self.n = rule[1]
        self.e = rule[2]
        self.s = rule[3]
        self.w = rule[4]
        self.cc = rule[5]

NumPy array is a object from World class. Rules is list as described above, parsed by function obtained from another program (GPL License).

To be honest it seems to work fine, but it does not. I was trying other possibilities, without luck. It is working, interpreter doesn't return any errors, but somehow values in array changing wrong. Rules are good because it was provided by program from which I've obtained parser for it (GPL license).

Maybe it will be helpful - it is Perrier's Loop, modified Langton's loop (artificial life).

Will be very thankful for any help! )

1

There are 1 answers

1
dmitry_romanov On

I am not familiar with Perrier's Loop, but if you code something like famous "game life" you would have done simple mistake: store the next generation in the same array thus corrupting it.

Normally you store the next generation in temporary array and do copy/swap after the sweep, like in this sketch:

def do_step_in_game_life(world):
    next_gen = zeros(world.shape)    # <<< Tmp array here
    Nx, Ny = world.shape
    for i in range(1, Nx-1):
        for j in range(1, Ny-1):
            neighbours = sum(world[i-1:i+2, j-1:j+2]) - world[i,j]
            if neighbours < 3:
                next_gen[i,j] = 0
            elif ...
    world[:,:] = next_gen[:,:]       # <<< Saving computed next generation