### Thread: Python Particle Stimulation. Any ideas?

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
4
Rep Power
0

#### Python Particle Stimulation. Any ideas?

I was wondering, and I've been pondering over this for quite a while now, how is this to be written? I am a beginner, so pardon my stupidity (if you will).
Thanks, I really need help.

Imagine a "particle" located on the centre square of a two‑dimensional grid of dimensions 11 by 75. The particle can only move one square at a time, either up, down, left, or right. Associated with each possible motion are the following probabilities:

Up 1/12

Down 1/12

Left 1/24

Right 19/24

Write a program that simulates the motion of the particle within the grid, until it reaches an "exit" at the square in row 6, column 75. The particle cannot "penetrate" any of the four boundaries. Have the program repeat the simulation 10 times and calculate the average total distance travelled by the particle until it exits.
2. Code:
```'''
Imagine a "particle" located on the centre square of a two-dimensional
grid of dimensions 11 by 75.  The particle can only move one square at
a time, either up, down, left, or right. Associated with each possible
motion are the following probabilities:

Up 1/12
Down 1/12
Left 1/24
Right 19/24

Write a program  that simulates the motion of  the particle within the
grid,  until it  reaches an  "exit"  at the  square in  row 6,  column
75. The particle  cannot "penetrate" any of the  four boundaries. Have
the program repeat  the simulation 10 times and  calculate the average
total distance travelled by the particle until it exits.
'''

import pprint
import random

# define the rectangle boundary and probability table
ROWS = 11
COLS = 75
EXIT_ROW = 6-1                            # index origin 0
EXIT_COL = 75-1                           # index origin 0
MOVEMENT = (                              # a list of 24 2-tuples
(( 0, 1),)*2 +  # Up
(( 0,-1),)*2 +  # Down
((-1, 0),)*1 +  # Left
(( 1, 0),)*19   # Right
)
#print('MOVEMENT')       # uncomment to display the MOVEMENT list
#pprint.pprint(MOVEMENT) # uncomment to display the MOVEMENT list

# assign the start position at the center
R = ROWS//2  # integer division
C = COLS//2

# prepare the distance traveled
STEPS = 0
BRUISES = 0

while (R != EXIT_ROW) or (C != EXIT_COL):# repeat until finished
MOVE = MOVEMENT[random.randrange(len(MOVEMENT))] # choose a step and maybe take it
(COL_STEP,ROW_STEP) = MOVE            # tuple assignment
PROPOSED_C = C + COL_STEP
if not (0 <= PROPOSED_C < COLS):      # out-of-bounds
BRUISES += 1
continue
PROPOSED_R = R + ROW_STEP
if not (0 <= PROPOSED_R < ROWS):
BRUISES += 1
continue
C = PROPOSED_C                        # update position and distance
R = PROPOSED_R
STEPS += 1

print('Exit after crashing into the wall %d times and taking %d steps'%(BRUISES,STEPS))```
Then you'd need to package up your version of the simulation into a function that also returns the distance traveled, call that function the right number of times, and accumulate necessary statistics. You have some choice about how many setup parameters to provide.

You could specify the probability table as moving to the right with probability 1, specify the start position rather than center, specify the exit, and blah blah blah... easily test that your code works. You could assert:
Code:
```#assertive test
assert 3 == simulation(ROWS = 1, COLS = 4, EXIT_ROW = 0, EXIT_COL = 3, MOVEMENT = ((1,0),), START_ROW = 0, START_COL = 0)```
if you had a function defined as follows:

Code:
```def simulation(
ROWS = 11,
COLS = 75,
EXIT_ROW = 6-1,
EXIT_COL = 75-1,
MOVEMENT = ((1,0),),
START_ROW = 11//2,
START_COL = 75//2):

'''drunkard's walk on tilted checkerboard'''
# fill in the function```
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
4
Rep Power
0

#### Thanks :-) And one thing...

Originally Posted by b49P23TIvg
Code:
```'''
Imagine a "particle" located on the centre square of a two-dimensional
grid of dimensions 11 by 75.  The particle can only move one square at
a time, either up, down, left, or right. Associated with each possible
motion are the following probabilities:

Up 1/12
Down 1/12
Left 1/24
Right 19/24

Write a program  that simulates the motion of  the particle within the
grid,  until it  reaches an  "exit"  at the  square in  row 6,  column
75. The particle  cannot "penetrate" any of the  four boundaries. Have
the program repeat  the simulation 10 times and  calculate the average
total distance travelled by the particle until it exits.
'''

import pprint
import random

# define the rectangle boundary and probability table
ROWS = 11
COLS = 75
EXIT_ROW = 6-1                            # index origin 0
EXIT_COL = 75-1                           # index origin 0
MOVEMENT = (                              # a list of 24 2-tuples
(( 0, 1),)*2 +  # Up
(( 0,-1),)*2 +  # Down
((-1, 0),)*1 +  # Left
(( 1, 0),)*19   # Right
)
#print('MOVEMENT')       # uncomment to display the MOVEMENT list
#pprint.pprint(MOVEMENT) # uncomment to display the MOVEMENT list

# assign the start position at the center
R = ROWS//2  # integer division
C = COLS//2

# prepare the distance traveled
STEPS = 0
BRUISES = 0

while (R != EXIT_ROW) or (C != EXIT_COL):# repeat until finished
MOVE = MOVEMENT[random.randrange(len(MOVEMENT))] # choose a step and maybe take it
(COL_STEP,ROW_STEP) = MOVE            # tuple assignment
PROPOSED_C = C + COL_STEP
if not (0 <= PROPOSED_C < COLS):      # out-of-bounds
BRUISES += 1
continue
PROPOSED_R = R + ROW_STEP
if not (0 <= PROPOSED_R < ROWS):
BRUISES += 1
continue
C = PROPOSED_C                        # update position and distance
R = PROPOSED_R
STEPS += 1

print('Exit after crashing into the wall %d times and taking %d steps'%(BRUISES,STEPS))```
Then you'd need to package up your version of the simulation into a function that also returns the distance traveled, call that function the right number of times, and accumulate necessary statistics. You have some choice about how many setup parameters to provide.

You could specify the probability table as moving to the right with probability 1, specify the start position rather than center, specify the exit, and blah blah blah... easily test that your code works. You could assert:
Code:
```#assertive test
assert 3 == simulation(ROWS = 1, COLS = 4, EXIT_ROW = 0, EXIT_COL = 3, MOVEMENT = ((1,0),), START_ROW = 0, START_COL = 0)```
if you had a function defined as follows:

Code:
```def simulation(
ROWS = 11,
COLS = 75,
EXIT_ROW = 6-1,
EXIT_COL = 75-1,
MOVEMENT = ((1,0),),
START_ROW = 11//2,
START_COL = 75//2):

'''drunkard's walk on tilted checkerboard'''
# fill in the function```

Thanks for the help! I really appreciate it! I was wondering though, is there any way this code could be simplified? And I don't understand whether or not if it runs 10 times and it calculates the average.
Could you elaborate on that a bit more for me, if you can please? Thanks :-)
4. I did not completely finish your project. Did you actually run any of it? What do you consider simpler? The program can be different. I thought I had spelled out the algorithm carefully and thoughtfully.

Here's the program in executable Iverson notation
Code:
```NB. dw (<:75 11,:75 6),<.75 11%2[load'j.ijs'
NB. (+/%#)dw"2[10#,:(<:75 11,:75 6),<.75 11%2[load'j.ijs' NB. 10 runs averaged
dw=: 3 : 0
'SHAPE END START'=. y
POSITION=. START
N=. 0
p=. (+/\2r24 2r24 1r24)&(I.?)
while. POSITION -.@:-: END do.
POSITION=. SHAPE<.0>.POSITION+(p 0){0 _1,0 1,_1,:1
N=. >:N
end.
)```