#
#   Executes target seeking random walk inside a
#   confining boundary(rectangle). Conducts walks, 
#   reports statistics and plots histogram of the 
#   number of steps required to reach target.
#   
#   Imports common functions from randwalk2d_mod.py
#  
#  Following parameters can be changed:
#    1- Size and location of boundary
#    2- Size and location of target
#    3- Starting location for random walk
#    4- Function for random step(rand_angle or rand_grid)  
#    5- Number of walks to create statistics (input)
#
import sys, random
import numpy as np
import randwalk2d_mod1 as rw2d 
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle  # Used to draw rectangle
random.seed(None)        # Seed generator, None => system clock

#
#   Enter number of walks and sets maximum number of steps for walk
#    Note: upper limit needed walk does not go on indefinitely 
num_walks = int(input('Enter number of walks to do  :  '))
max_steps = 50000
#  
#  Set confining box, target , starting point for walks, random step function
#  
boundary      = rw2d.setup_box((0.0, 0.0), 40, 40)
target        = rw2d.setup_box((10.0, 10.0), 3, 3)    
start_loc     = (0.0, 0.0)
#
#  Perform loop for each walk and plots histogram of num odf steps to target 
# 
plt.clf    #  Clear the graph figure
walk_list = []
for num in range(num_walks):      
    xwalk1,ywalk1,steps_taken,target_reached = rw2d.random_walk_target(max_steps, \
            start_loc, boundary, target )
    if target_reached :
        walk_list.append(steps_taken)
#  Compute statistics for walks
walk_list = np.array(walk_list)
ave_steps = int(np.mean(walk_list))
std_dev_steps = int(np.std(walk_list))

#  Create histogram of walk steps. Set plot title , legend, and grid 
plt.hist(walk_list, bins=50, color='red')      
plt.title(' 2D walks with target (steps: mean= '+str(ave_steps)+', std_dev ='  \
             +str(std_dev_steps)+  ')')
plt.xlabel(' Number of steps')
plt.ylabel(' Frequency')
plt.grid(True)  
plt.show()

