Skip to content

Commit

Permalink
experiments repeated
Browse files Browse the repository at this point in the history
  • Loading branch information
JongYun-Kim committed Jul 7, 2023
1 parent d746670 commit 9d26c6a
Showing 1 changed file with 115 additions and 57 deletions.
172 changes: 115 additions & 57 deletions env/env_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np
import matplotlib.pyplot as plt
# import time
import time
# from gym.spaces import Box, Dict
from env.envs import LazyAgentsCentralized # Import your custom environment

Expand All @@ -27,7 +27,7 @@ def plot_trajectories(agent_positions_history, current_states, fig, ax):
ax.grid(True)

plt.draw()
plt.pause(0.001)
plt.pause(0.0001)


def plot_agents(agents_state, fig, ax):
Expand Down Expand Up @@ -64,7 +64,7 @@ def plot_agents(agents_state, fig, ax):
ax.grid(True)

plt.draw()
plt.pause(0.001)
plt.pause(0.0001)


def plot_std_history(std_vel_history, std_pos_history, fig, ax):
Expand All @@ -79,75 +79,133 @@ def plot_std_history(std_vel_history, std_pos_history, fig, ax):
ax.grid(True)

plt.draw()
plt.pause(0.001)
plt.pause(0.0001)


if __name__ == '__main__':
print("Running env_test.py")

num_experiment = 30
cases = [10, 15, 20, 25, 30]
mum_cases = len(cases)

num_experiment = 10

result_average_converged_time = np.zeros((mum_cases, num_experiment))

for case in range(mum_cases):

for exp in range(num_experiment):

num_agent = cases[case]
action = np.ones(num_agent, dtype=np.float32)
config = {"num_agent_max": num_agent,
"num_agent_min": num_agent,
#
# Optional parameters
"speed": 15, # Speed in m/s. Default is 15
"predefined_distance": 60, # Predefined distance in meters. Default is 60
# "communication_decay_rate": 1 / 3, # Communication decay rate. Default is 1/3
# "cost_weight": 1, # Cost weight. Default is 1
# "inter_agent_strength": 5, # Inter agent strength. Default is 5
# "bonding_strength": 1, # Bonding strength. Default is 1
# "k1": 1, # K1 coefficient. Default is 1
# "k2": 3, # K2 coefficient. Default is 3
"max_turn_rate": 8 / 15, # Maximum turn rate in rad/s. Default is 8/15
"initial_position_bound": 250, # Initial position bound in meters. Default is 250
"dt": 0.1, # Delta time in seconds. Default is 0.1
# "network_topology": "fully_connected", # Network topology. Default is "fully_connected"
# Params to tune
"std_pos_converged": 45, # Standard position when converged. Default is R/2
"std_vel_converged": 0.1, # Standard velocity when converged. Default is 0.1
"std_pos_rate_converged": 0.1, # Standard position rate when converged. Default is 0.1
"std_vel_rate_converged": 0.2, # Standard velocity rate when converged. Default is 0.01
"max_time_step": 1000 # Maximum time steps. Default is 1000,
}

env = LazyAgentsCentralized(config)
obs = env.reset()
agent_states = obs['agent_embeddings']
reward_sum = 0

for exp in range(num_experiment):
# std_vel_history = []
# std_pos_history = []
# agent_positions_history = np.zeros((num_agent, env.max_time_step, 2), dtype=np.float32)

num_agent = 20
action = np.ones(num_agent, dtype=np.float32)
config = {"num_agent_max": num_agent,
"num_agent_min": num_agent,
#
"std_pos_converged": 42, # Standard position when converged. Default is R/2
"std_vel_converged": 0.1, # Standard velocity when converged. Default is 0.1
"std_pos_rate_converged": 0.1, # Standard position rate when converged. Default is 0.1
"std_vel_rate_converged": 0.2, # Standard velocity rate when converged. Default is 0.01
"max_time_step": 1000 # Maximum time steps. Default is 1000,
}
# fig1, ax1 = plt.subplots()
# fig2, ax2 = plt.subplots()
# fig3, ax3 = plt.subplots()

env = LazyAgentsCentralized(config)
obs = env.reset()
agent_states = obs['agent_embeddings']
done = False
while not done:
obs, reward, done, info = env.step(action)

# std_vel_history = []
# std_pos_history = []
agent_positions_history = np.zeros((num_agent, env.max_time_step, 2), dtype=np.float32)
# Get the reward
# reward_sum += reward

fig1, ax1 = plt.subplots()
fig2, ax2 = plt.subplots()
fig3, ax3 = plt.subplots()
# extract agent_states from the observations
# agent_states = obs['agent_embeddings']

done = False
while not done:
obs, reward, done, info = env.step(action)
# update agent_positions_history with current positions
# agent_positions_history[:, env.time_step-1] = agent_states[:, :2]

# extract agent_states from the observations
agent_states = obs['agent_embeddings']
# # plot trajectories and std dev history
# std_pos_history = env.std_pos_hist[:env.time_step]
# std_vel_history = env.std_vel_hist[:env.time_step]
# plot_agents(agent_states, fig1, ax1)
# if env.time_step % 50 == 0:
# plot_std_history(std_vel_history, std_pos_history, fig2, ax2)
# plot_trajectories(agent_positions_history[:, :env.time_step], agent_states, fig3, ax3)

# update agent_positions_history with current positions
agent_positions_history[:, env.time_step-1] = agent_states[:, :2]
# if env.time_step % 100 == 0:
# print(f"Time step: {env.time_step}")
# time.sleep(0.01)

# # plot trajectories and std dev history
# plot trajectories and std dev history
# std_pos_history = env.std_pos_hist[:env.time_step]
# std_vel_history = env.std_vel_hist[:env.time_step]
# plot_agents(agent_states, fig1, ax1)
# if env.time_step % 50 == 0:
# plot_std_history(std_vel_history, std_pos_history, fig2, ax2)
# plot_trajectories(agent_positions_history[:, :env.time_step], agent_states, fig3, ax3)

# if env.time_step % 100 == 0:
# print(f"Time step: {env.time_step}")
# time.sleep(0.01)

# plot trajectories and std dev history
std_pos_history = env.std_pos_hist[:env.time_step]
std_vel_history = env.std_vel_hist[:env.time_step]
plot_agents(agent_states, fig1, ax1)
plot_std_history(std_vel_history, std_pos_history, fig2, ax2)
plot_trajectories(agent_positions_history[:, :env.time_step], agent_states, fig3, ax3)

print("Done!")

if env.time_step == env.max_time_step:
print("Max time step reached!")

plt.close(fig1)
plt.close(fig2)
plt.close(fig3)
# plot_std_history(std_vel_history, std_pos_history, fig2, ax2)
# plot_trajectories(agent_positions_history[:, :env.time_step], agent_states, fig3, ax3)

# std = np.std(agent_states[:, :2], axis=0).sum()
# print(f"Final std_mine: {std}")
# print(f"Accumulated reward: {reward_sum}")

print(f"Experiment {exp} of case {case} finished!")
print("Done!")

if env.time_step == env.max_time_step:
print("Max time step reached!")

# plt.close(fig1)
# plt.close(fig2)
# plt.close(fig3)

result_average_converged_time[case, exp] = env.time_step


# # plot the average converged time
# fig4, ax4 = plt.subplots()
# ax4.set_title('Average Converged Time')
# ax4.set_xlabel('Number of Agents')
# ax4.set_ylabel('Average Converged Time')
# ax4.plot(cases, np.mean(result_average_converged_time, axis=1), label='Average Converged Time')
# ax4.legend()
# ax4.grid(True)
# plt.draw()
# plt.pause(0.001)

# plot the average converged time with bar chart
plt.ion() # Turn on interactive mode
fig4, ax4 = plt.subplots()
ax4.set_title('Average Converged Time')
ax4.set_xlabel('Number of Agents')
ax4.set_ylabel('Average Converged Time')
# Use bar function instead of plot for a bar chart
ax4.bar(cases, np.mean(result_average_converged_time, axis=1), label='Average Converged Time')
ax4.legend()
ax4.grid(True)
plt.draw()
plt.pause(0.001)

print("Done!")

0 comments on commit 9d26c6a

Please sign in to comment.