Advertisement
AmCholadawan

levywalklike_function

Mar 13th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | None | 0 0
  1. import random
  2. from datetime import datetime
  3. import numpy as np
  4.  
  5. random.seed(datetime.now())
  6.  
  7. def direction(probability, last_direction):
  8.  
  9.     if last_direction == [0, 0]:
  10.         """pick an initial random direction with uniform distribution"""
  11.         direction_xy = polarToCartesian(random.randint(0, 7))
  12.     else:
  13.         last_direction = cartesianToPolar(last_direction)
  14.         low_prob = (1 - probability/100) / 7
  15.         directions_p = [low_prob for p in range(7)]
  16.         directions_p[last_direction] = probability/100
  17.         directions_p /= np.sum(directions_p)
  18.         direction_xy = np.random.choice(range(7), p=directions_p)
  19.         direction_xy = polarToCartesian(direction_xy)
  20.  
  21.     return(direction_xy)
  22.  
  23. def polarToCartesian(direction):
  24.     # 0 1 2
  25.     # 7 b 3
  26.     # 6 5 4
  27.     return {
  28.         0: [-1, -1],
  29.         1: [-1,  0],
  30.         2: [-1, +1],
  31.         3: [ 0, +1],
  32.         4: [+1, +1],
  33.         5: [+1,  0],
  34.         6: [+1, -1],
  35.         7: [ 0, -1]
  36.     } [direction]
  37.  
  38. def cartesianToPolar(direction):
  39.     if direction[0] < 0:
  40.         if direction[1] == -1:
  41.             return 0
  42.         elif direction[1] == 0:
  43.             return 1
  44.         else:
  45.             return 2
  46.     elif direction[0] == 0:
  47.         if direction[1] == -1:
  48.             return 7
  49.         else:
  50.             return 3
  51.     else:
  52.         if direction[1] == -1:
  53.             return 6
  54.         elif direction[1] == 0:
  55.             return 5
  56.         else:
  57.             return 4
  58.  
  59. ### TEST
  60. #for t in range(100):
  61. #    print(direction(50, [1, 0]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement