2

this is a continuation of a previous stackoverflow question here

I want to do it in 2 dimensions. I tried the following:

for i in range(pos):
    steps=0   #the steps that the particle does until it falls in  a trap
    in_pos = sc.random.randint(0, len(grid), 2)
    initial_trap=False
    while initial_trap==False:
        #the step of the particle can be one of this
        step=sc.array(random.choice([[0, 1], [1, 0], [0, -1], [-1, 0]]))          
        # Check position for edges and fix if required

        if in_pos + step > sc.size(grid) - 1:
            in_pos = 0
        elif in_pos + step < 0:
            in_pos = sc.size(grid) - 1
        else:
            in_pos += step


        # Check if it's a trap in order to stop the loop

        if grid[in_pos] == 0:
            initial_trap = True

        # If it isn't a trap, continue
        steps+=1
    steps_count.append(steps)
return steps_count

I also tried :

in_pos_x =int(sc.random.randint(0, len(grid), 1)) #initial position of particle in x axis
in_pos_y =int(sc.random.randint(0, len(grid), 1)) #initial position of particle in y axis

if  in_pos_x + step > len(grid)- 1 and in_pos_y + step > len(grid)-1:
    in_pos_x = 0
    in_pos_y = 0
elif in_pos_x + step < 0 and in_pos_y + step < 0:
    in_pos_x = len(grid)- 1
    in_pos_y = len(grid)- 1
else:    in_pos_x += step
    in_pos_y += step

if grid[in_pos_x][in_pos_y] == 0:
    initial_trap = True

And last,i tried to work with lists, not arrays.

in_pos = (scipy.random.randint(0,len(grid),2)).tolist()
step=random.choice([[0, 1], [1, 0], [0, -1], [-1, 0]])

but with no success either. I am lost here!

---------------Error messages-----------------------------

If run the program it gives me:

'The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()' at the line "if (in_pos + step) > sc.size(grid) - 1:"

If i use if sc.any(in_pos + step) > sc.size(grid) - 1: or if sc.any(grid[in_pos]) == 0: it runs but i used a print(grid[in_pos]) and I figured that it doesn't change values at all! It doesn't get the value '0', so the loop never ends.

Community
  • 1
  • 1
George
  • 5,808
  • 15
  • 83
  • 160
  • It would be better if you tell *what* is not working. – Avaris Nov 09 '11 at 16:07
  • Nothing is working under the 'step=sc.array(random.choice([[0, 1], [1, 0], [0, -1], [-1, 0]]))' – George Nov 09 '11 at 16:09
  • 1
    You can still do better. "Not working"=? Throws exception? In that case share the exception. Doesn't do what you intended? In that case share what it does and what you intended. – Avaris Nov 09 '11 at 16:14
  • possible duplicate: http://stackoverflow.com/questions/7100995/testing-if-all-values-in-a-numpy-array-are-equal – Andrew Walker Sep 27 '12 at 14:16

1 Answers1

3

After the comments, I think I understand where you are going.

I guess you are using periodic boundaries, but the check you are doing as below is wrong:

# Check position for edges and fix if requireD

if in_pos + step > sc.size(grid) - 1:
    in_pos = 0
elif in_pos + step < 0:
    in_pos = sc.size(grid) - 1
else:
    in_pos += step

size returns the total number of elements in an array. You need to check individual lengths (in x and y directions) of array. Here, shape is your friend. And you need to correct the index that goes beyond the limits, not all of them. mod operator % can be of use. So the above code can be simply re-written as:

# Move by step
in_pos += step
# Correct according to periodic boundaries
in_pos = in_pos % grid.shape    # or simply in_pos %= grid.shape

Second problem is the indexing of grid. Suffice it to say, the way you are doing is not what you want (check the docs for details). For your simple case it can be re-written as:

if grid[in_pos[0], in_pos[1]] == 0:
    initial_trap = True
Avaris
  • 35,883
  • 7
  • 81
  • 72
  • :Ok,i think its right.I will test it tomorrow.I just don't understand how the in_pos% grid.shape works..It is equivalent with "if in_pos + step > len(grid) - 1...."that i have above?If i wanted to apply it to 1dimension it would be "in_pos = in_pos % len(grid)? or len(grid)-1?But i still don't understand the modulo. – George Nov 09 '11 at 21:12
  • @George: Say you have a 3x3 grid. You are at [1,2] (that's end of the second row) and you want to go right ([+0,+1]). What you will have is [1,3], but that's out of bounds. So you need to come back from the left side (i.e. periodic boundary) and be at [1,0]. grid.shape gives you [3,3]. % with an array will give you mod with each corresponding element. So [1,3] % [3,3] == [1%3,3%3] == [1,0]. In general %n gives you something in [0,n-1], so that makes it useful in periodic stuff. – Avaris Nov 09 '11 at 21:23
  • :Ok,thanks i understood that now.But ,it confuses me now abou the 1 dimension .The equivalent would be in_pos%=len(grid)-1 ? Thanks again! – George Nov 09 '11 at 21:45
  • @George: no need for -1, just in_pos%=len(grid) is enough. – Avaris Nov 09 '11 at 22:22