Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Created on Sat Apr 21 19:42:16 2018
- @author: alinaulezko
- """
- #ignore this
- #side = int(trueRand/(side_length-1))
- #displacement = trueRand%(side_length-1)
- #Import Modules
- import numpy as np
- import numpy.random as nprnd
- import matplotlib.pyplot as plt
- #Define variables. The side length of a square matrix is variable.
- #The center will be located at the index as specified by the formula.
- #A matrix of dimensions side length suqared is created, where all numbers are (for now) zeros.
- #At the center position, a 1 is placed. 1 indicates STATIONARY DOT.
- #In the beggining, there is only one stationary dot (center)
- #StatCounter counts how many stationary points there are.
- #logically, in the beggining there is only one.
- #reqStat is the maximum amount of stationary points that we want (which is what was specified in the instructions you provided as 1000)
- side_length = 1000
- center = int((side_length - 1) / 2)
- matrix = np.zeros((side_length,side_length))
- matrix[center,center] = 1
- statCounter = 1
- reqStat = 5
- #This is to fill the boundaries of the matrix with numbers that go from 2 (because 1 means stationary and 0 means unocuppied)
- #to increasing numbers, like a snake that surrounds the matrix in a clockwise fashion. Please run this part and click "matrix" for clarification
- for i in range(side_length):
- matrix[i, side_length - 1] = i + side_length + 1
- matrix[side_length - 1, -i] = i + (2 * side_length - 1)
- matrix[-i, 0] = i + (3 * side_length - 2)
- matrix[0, i] = i + 2
- #Defines function that will spawn (i.e. generate) a dot (NewDot) form the boundary. This is why the random number "rand" is in the range 2 to boundlength -3
- #Once the random number is generated, a new object "NewDot" is created, which is placed at the random number in the matrix boundary as generated in the previous line
- def spawn_dot():
- bound_length = 4 * side_length - 3
- rand = nprnd.randint(2,bound_length+1)
- newDot = [np.where(matrix==rand)[0][0],np.where(matrix==rand)[1][0]]
- return newDot
- #This function checks whether the new dot is/is not next to a stationary dot.
- # The for loop has i's that can be -1, 0, or 1.
- #If the x parameter of NewDot is in the first row and it the i = 1 (i.e. the third run of the for loop),
- #then keep running the for loop and don't do anything (because moving to the right in this case is illogical)
- #The second for loop has j's that can be -1, 0, and 0
- #If the y parameter of NewDot is in the first column and and j = 1, don't do anything because you can't move upwards.
- #The print statement is here because we were checking that these loops work
- #The next statement says that if the neighbouring dot is stationary (aka equals to 1), then make this dot stationary again
- #once you make it stationary, add one to the statCounter (stationary dot counter). We made the StatCounter a global variable because it is used in another function below.
- def stat_check():
- for i in range(-1,2):
- if (newDot[0] == (side_length-1) and i == 1):
- continue
- for j in range (-1,2):
- if (newDot[1] == (side_length-1) and j == 1):
- continue
- print(j, newDot[1], i, newDot[0])
- if (matrix[newDot[0]+i,newDot[1]+j] == 1):
- #make newDot stationary:
- matrix[newDot[0],newDot[1]] = 1
- global statCounter
- statCounter += 1
- return True
- return False
- #This is the function that does the random walk of the dot.
- #While the statcheck returns true, generate a random x and y displacement that is either -1 or 1
- #The if statements below check for corners and for boundaries, and it reflects them.
- #for example, if the new dot is in the upper left corner, reflect x if x=-1 and reflect y if y=-1, and so on
- #once we're done with checking for boundaries and corners, make the new dot move according to x and y
- def rw():
- while (not(stat_check())):
- x = 2*(nprnd.random() > 0.5) - 1
- y = 2*(nprnd.random() > 0.5) - 1
- if (newDot == [0,0]):
- if (x == -1):
- x = -x
- if (y == -1):
- y = -y
- elif (newDot == [side_length-1, 0]):
- if (x == 1):
- x = -x
- if (y == -1):
- y = -y
- elif (newDot == [side_length-1, side_length-1]):
- if (x == 1):
- x = -x
- if (y == 1):
- y = -y
- elif (newDot == [0, side_length-1]):
- if (x == -1):
- x = -x
- if (y == 1):
- y = -y
- elif (int(matrix[newDot[0],newDot[1]] / (side_length - 1)) == 0):
- if (y == -1):
- y = -y
- #the above is a problem. false bounds.
- elif (int(matrix[newDot[0],newDot[1]] / (side_length - 1)) == 1):
- if (x == 1):
- x = -x
- elif (int(matrix[newDot[0],newDot[1]] / (side_length - 1)) == 2):
- if (y == 1):
- y = -y
- elif (int(matrix[newDot[0],newDot[1]] / (side_length - 1)) == 3):
- if (x == -1):
- x = -x
- movement = [x,y]
- newDot[0] = newDot[0] + movement[0]
- newDot[1] = newDot[1] + movement[1]
- #Keep spawning dots and ding random walk while the number of stationary dots is lower than the required number of Stat dots
- while (statCounter < reqStat):
- newDot = spawn_dot()
- rw()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement