DebasishDhal99 commited on
Commit
a0fdaee
·
1 Parent(s): d9a3422

Create single_agent_2D.py

Browse files
Files changed (1) hide show
  1. single_agent_2D.py +77 -0
single_agent_2D.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import matplotlib.pyplot as plt
2
+ import random
3
+ import numpy as np
4
+ import pandas as pd
5
+
6
+ def generate_random_walk(iters, step_size = 1, random_seed = None):
7
+ # random.seed(random_seed)
8
+ iters = int(iters)
9
+ directions = ['east', 'north', 'west', 'south']
10
+ start_point = [0, 0]
11
+
12
+ if random_seed is None:
13
+ random_seed = random.randint(1, 100000)
14
+ else:
15
+ random_seed = random_seed
16
+
17
+ random.seed(random_seed)
18
+
19
+ def distance_from_start(final_coord, start_coord, round_to=2):
20
+ return round(np.sqrt((final_coord[0] - start_coord[0])**2 + (final_coord[1] - start_coord[1])**2), round_to)
21
+
22
+ def step_addition(old_coord, step):
23
+ return [sum(x) for x in zip(old_coord, step)]
24
+
25
+ def step_determination():
26
+ direction = random.choice(directions)
27
+ if direction == 'east':
28
+ return [1*step_size, 0]
29
+ elif direction == 'west':
30
+ return [-1*step_size, 0]
31
+ elif direction == 'north':
32
+ return [0, 1*step_size]
33
+ elif direction == 'south':
34
+ return [0, -1*step_size]
35
+
36
+ coordinate_list = [start_point]
37
+
38
+ for i in range(iters):
39
+ new_step = step_determination()
40
+ new_coordinate = step_addition(coordinate_list[-1], new_step)
41
+ coordinate_list.append(new_coordinate)
42
+
43
+ x = [i[0] for i in coordinate_list]
44
+ y = [i[1] for i in coordinate_list]
45
+ df = pd.DataFrame({'x':x,'y':y})
46
+ csv_file = "2d_random_walk_coordinates.csv"
47
+ df.to_csv(csv_file, index=False)
48
+
49
+ fig, ax = plt.subplots(1)
50
+
51
+ base_marker_size = 10
52
+ markersize = base_marker_size / np.sqrt(iters)
53
+
54
+ ax.plot(x, y, marker='o', markersize=markersize, linestyle='None')
55
+
56
+ ax.plot(x[0], y[0], marker='o', markersize=5, color="red")
57
+ ax.plot(x[-1], y[-1], marker='o', markersize=5, color="orange")
58
+
59
+ ax.text(start_point[0], start_point[1], 'Start', color='red')
60
+ ax.text(x[-1], y[-1], 'End', color='orange')
61
+
62
+ x_max_index = x.index(max(x))
63
+ x_min_index = x.index(min(x))
64
+ y_max_index = y.index(max(y))
65
+ y_min_index = y.index(min(y))
66
+
67
+ info_text = 'Start point=' + str(start_point) + '\n' +'End point=' + str([x[-1],y[-1]]) + '\n' +'Displacement =' + str(distance_from_start([x[-1], y[-1]], start_point)) + '\n' +'Max x = ' + str(max(x)) + '\n' + 'Min x = ' + str(min(x)) + '\n' + 'Max y = ' + str(max(y)) + '\n' + 'Min y = ' + str(min(y))
68
+ ax.legend([info_text], loc='best', handlelength=0, handletextpad=0, fancybox=True, fontsize=8)
69
+
70
+ plt.title( '2D Random Walk\nsteps=' + str(iters)+', step size='+ str(step_size)+ ', seed = '+str((random_seed)) )
71
+ plt.grid()
72
+
73
+ fig.canvas.draw()
74
+ image_array = np.array(fig.canvas.renderer.buffer_rgba())
75
+
76
+
77
+ return image_array, csv_file