Commit
·
ec3a39e
1
Parent(s):
35908ef
Update multi_agent_2D.py
Browse files- multi_agent_2D.py +9 -8
multi_agent_2D.py
CHANGED
|
@@ -4,12 +4,12 @@ import numpy as np
|
|
| 4 |
import pandas as pd
|
| 5 |
from matplotlib.lines import Line2D
|
| 6 |
|
|
|
|
| 7 |
def single_random_walk(iters, agent_number, ax, step_size = 1, random_seed = None):
|
| 8 |
-
|
| 9 |
-
if random_seed:
|
| 10 |
random.seed(random_seed)
|
| 11 |
|
| 12 |
-
iters = int(iters)
|
| 13 |
directions = ['east', 'north', 'west', 'south']
|
| 14 |
start_point = [0, 0]
|
| 15 |
|
|
@@ -32,7 +32,7 @@ def single_random_walk(iters, agent_number, ax, step_size = 1, random_seed = Non
|
|
| 32 |
|
| 33 |
coordinate_list = [start_point]
|
| 34 |
|
| 35 |
-
for _ in range(iters):
|
| 36 |
new_step = step_determination()
|
| 37 |
new_coordinate = step_addition(coordinate_list[-1], new_step)
|
| 38 |
coordinate_list.append(new_coordinate)
|
|
@@ -41,19 +41,20 @@ def single_random_walk(iters, agent_number, ax, step_size = 1, random_seed = Non
|
|
| 41 |
y = [i[1] for i in coordinate_list]
|
| 42 |
df = pd.DataFrame({'x':x,'y':y})
|
| 43 |
|
| 44 |
-
|
| 45 |
-
#Add the axis from the argument to the figure
|
| 46 |
base_marker_size = 10
|
| 47 |
markersize = base_marker_size / np.sqrt(iters)
|
| 48 |
|
|
|
|
| 49 |
plot = ax.plot(x, y, marker='o', markersize=markersize, linestyle='None', alpha=0.5, label = 'Agent {i}'.format(i=agent_number+1))
|
| 50 |
-
color = plot[0].get_color()
|
| 51 |
ax.plot(x[-1], y[-1], marker='o', markersize=5, color = 'black')
|
| 52 |
ax.text(x[-1], y[-1], 'End {i}'.format(i=agent_number+1), color = 'black', alpha=1.0)
|
| 53 |
|
| 54 |
return ax, df, color
|
| 55 |
|
| 56 |
|
|
|
|
| 57 |
def multi_agent_walk(agent_count, iters, step_size = 1, random_seed = None):
|
| 58 |
assert agent_count >= 1, "Number of agents must be >= than 1"
|
| 59 |
agent_count = int(agent_count)
|
|
@@ -69,7 +70,7 @@ def multi_agent_walk(agent_count, iters, step_size = 1, random_seed = None):
|
|
| 69 |
|
| 70 |
random_seed = int(random_seed)
|
| 71 |
assert type(random_seed) == int, "Random seed must be an integer"
|
| 72 |
-
#
|
| 73 |
random.seed(random_seed)
|
| 74 |
random_numbers = [random.randint(0,100000) for _ in range(agent_count)]
|
| 75 |
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
from matplotlib.lines import Line2D
|
| 6 |
|
| 7 |
+
#single_random_walk takes a single particle and plots its trajectory on the axis passed to it
|
| 8 |
def single_random_walk(iters, agent_number, ax, step_size = 1, random_seed = None):
|
| 9 |
+
if random_seed:
|
|
|
|
| 10 |
random.seed(random_seed)
|
| 11 |
|
| 12 |
+
iters = int(iters) #Because for some reason, the input from Gradio input components is in float
|
| 13 |
directions = ['east', 'north', 'west', 'south']
|
| 14 |
start_point = [0, 0]
|
| 15 |
|
|
|
|
| 32 |
|
| 33 |
coordinate_list = [start_point]
|
| 34 |
|
| 35 |
+
for _ in range(iters): #Key part that decides the trajectory of the agent
|
| 36 |
new_step = step_determination()
|
| 37 |
new_coordinate = step_addition(coordinate_list[-1], new_step)
|
| 38 |
coordinate_list.append(new_coordinate)
|
|
|
|
| 41 |
y = [i[1] for i in coordinate_list]
|
| 42 |
df = pd.DataFrame({'x':x,'y':y})
|
| 43 |
|
| 44 |
+
#This to determine the markersize. This is is only a makeshift solution.
|
|
|
|
| 45 |
base_marker_size = 10
|
| 46 |
markersize = base_marker_size / np.sqrt(iters)
|
| 47 |
|
| 48 |
+
#Plot on the axis passed, do not make a new figure.
|
| 49 |
plot = ax.plot(x, y, marker='o', markersize=markersize, linestyle='None', alpha=0.5, label = 'Agent {i}'.format(i=agent_number+1))
|
| 50 |
+
color = plot[0].get_color() #Get the color so that we can add label with proper colors later
|
| 51 |
ax.plot(x[-1], y[-1], marker='o', markersize=5, color = 'black')
|
| 52 |
ax.text(x[-1], y[-1], 'End {i}'.format(i=agent_number+1), color = 'black', alpha=1.0)
|
| 53 |
|
| 54 |
return ax, df, color
|
| 55 |
|
| 56 |
|
| 57 |
+
#multi_agent_walk iteratively calls the single_random_walk function to plot different trajectories on the same axis.
|
| 58 |
def multi_agent_walk(agent_count, iters, step_size = 1, random_seed = None):
|
| 59 |
assert agent_count >= 1, "Number of agents must be >= than 1"
|
| 60 |
agent_count = int(agent_count)
|
|
|
|
| 70 |
|
| 71 |
random_seed = int(random_seed)
|
| 72 |
assert type(random_seed) == int, "Random seed must be an integer"
|
| 73 |
+
#Generates a list of random seeds for each agent
|
| 74 |
random.seed(random_seed)
|
| 75 |
random_numbers = [random.randint(0,100000) for _ in range(agent_count)]
|
| 76 |
|