|
| 1 | +from robot_nav.models.TD3.TD3 import TD3 |
| 2 | +from robot_nav.models.DDPG.DDPG import DDPG |
| 3 | +from robot_nav.models.SAC.SAC import SAC |
| 4 | +from robot_nav.models.HCM.hardcoded_model import HCM |
| 5 | +from robot_nav.models.PPO.PPO import PPO |
| 6 | +from robot_nav.models.CNNTD3.CNNTD3 import CNNTD3 |
| 7 | +import statistics |
| 8 | +import numpy as np |
| 9 | +import tqdm |
| 10 | +import matplotlib.pyplot as plt |
| 11 | + |
| 12 | +import torch |
| 13 | +from sim import SIM_ENV |
| 14 | + |
| 15 | + |
| 16 | +def main(args=None): |
| 17 | + """Main testing function""" |
| 18 | + action_dim = 2 # number of actions produced by the model |
| 19 | + max_action = 1 # maximum absolute value of output actions |
| 20 | + state_dim = 25 # number of input values in the neural network (vector length of state input) |
| 21 | + device = torch.device( |
| 22 | + "cuda" if torch.cuda.is_available() else "cpu" |
| 23 | + ) # using cuda if it is available, cpu otherwise |
| 24 | + epoch = 0 # epoch number |
| 25 | + max_steps = 300 # maximum number of steps in single episode |
| 26 | + test_scenarios = 1000 |
| 27 | + |
| 28 | + model = DDPG( |
| 29 | + state_dim=state_dim, |
| 30 | + action_dim=action_dim, |
| 31 | + max_action=max_action, |
| 32 | + device=device, |
| 33 | + load_model=True, |
| 34 | + model_name="DDPGexp5", |
| 35 | + ) # instantiate a model |
| 36 | + |
| 37 | + sim = SIM_ENV( |
| 38 | + world_file="eval_world.yaml", disable_plotting=True |
| 39 | + ) # instantiate environment |
| 40 | + |
| 41 | + print("..............................................") |
| 42 | + print(f"Testing {test_scenarios} scenarios") |
| 43 | + total_reward = [] |
| 44 | + reward_per_ep = [] |
| 45 | + lin_actions = [] |
| 46 | + ang_actions = [] |
| 47 | + total_steps = 0 |
| 48 | + col = 0 |
| 49 | + goals = 0 |
| 50 | + inter_rew = [] |
| 51 | + steps_to_goal = [] |
| 52 | + for _ in tqdm.tqdm(range(test_scenarios)): |
| 53 | + count = 0 |
| 54 | + ep_reward = 0 |
| 55 | + latest_scan, distance, cos, sin, collision, goal, a, reward = sim.reset( |
| 56 | + robot_state=None, |
| 57 | + robot_goal=None, |
| 58 | + random_obstacles=True, |
| 59 | + random_obstacle_ids=[i + 1 for i in range(6)], |
| 60 | + ) |
| 61 | + done = False |
| 62 | + while not done and count < max_steps: |
| 63 | + state, terminal = model.prepare_state( |
| 64 | + latest_scan, distance, cos, sin, collision, goal, a |
| 65 | + ) |
| 66 | + action = model.get_action(np.array(state), False) |
| 67 | + a_in = [(action[0] + 1) / 4, action[1]] |
| 68 | + lin_actions.append(a_in[0]) |
| 69 | + ang_actions.append(a_in[1]) |
| 70 | + latest_scan, distance, cos, sin, collision, goal, a, reward = sim.step( |
| 71 | + lin_velocity=a_in[0], ang_velocity=a_in[1] |
| 72 | + ) |
| 73 | + ep_reward += reward |
| 74 | + total_reward.append(reward) |
| 75 | + total_steps += 1 |
| 76 | + count += 1 |
| 77 | + if collision: |
| 78 | + col += 1 |
| 79 | + if goal: |
| 80 | + goals += 1 |
| 81 | + steps_to_goal.append(count) |
| 82 | + done = collision or goal |
| 83 | + if done: |
| 84 | + reward_per_ep.append(ep_reward) |
| 85 | + if not done: |
| 86 | + inter_rew.append(reward) |
| 87 | + |
| 88 | + total_reward = np.array(total_reward) |
| 89 | + reward_per_ep = np.array(reward_per_ep) |
| 90 | + inter_rew = np.array(inter_rew) |
| 91 | + steps_to_goal = np.array(steps_to_goal) |
| 92 | + lin_actions = np.array(lin_actions) |
| 93 | + ang_actions = np.array(ang_actions) |
| 94 | + avg_step_reward = statistics.mean(total_reward) |
| 95 | + avg_step_reward_std = statistics.stdev(total_reward) |
| 96 | + avg_ep_reward = statistics.mean(reward_per_ep) |
| 97 | + avg_ep_reward_std = statistics.stdev(reward_per_ep) |
| 98 | + avg_col = col / test_scenarios |
| 99 | + avg_goal = goals / test_scenarios |
| 100 | + avg_inter_step_rew = statistics.mean(inter_rew) |
| 101 | + avg_inter_step_rew_std = statistics.stdev(inter_rew) |
| 102 | + avg_steps_to_goal = statistics.mean(steps_to_goal) |
| 103 | + avg_steps_to_goal_std = statistics.stdev(steps_to_goal) |
| 104 | + mean_lin_action = statistics.mean(lin_actions) |
| 105 | + lin_actions_std = statistics.stdev(lin_actions) |
| 106 | + mean_ang_action = statistics.mean(ang_actions) |
| 107 | + ang_actions_std = statistics.stdev(ang_actions) |
| 108 | + print(f"avg_step_reward {avg_step_reward}") |
| 109 | + print(f"avg_step_reward_std: {avg_step_reward_std}") |
| 110 | + print(f"avg_ep_reward: {avg_ep_reward}") |
| 111 | + print(f"avg_ep_reward_std: {avg_ep_reward_std}") |
| 112 | + print(f"avg_col: {avg_col}") |
| 113 | + print(f"avg_goal: {avg_goal}") |
| 114 | + print(f"avg_inter_step_rew: {avg_inter_step_rew}") |
| 115 | + print(f"avg_inter_step_rew_std: {avg_inter_step_rew_std}") |
| 116 | + print(f"avg_steps_to_goal: {avg_steps_to_goal}") |
| 117 | + print(f"avg_steps_to_goal_std: {avg_steps_to_goal_std}") |
| 118 | + print(f"mean_lin_action: {mean_lin_action}") |
| 119 | + print(f"lin_actions_std: {lin_actions_std}") |
| 120 | + print(f"mean_ang_action: {mean_ang_action}") |
| 121 | + print(f"ang_actions_std: {ang_actions_std}") |
| 122 | + print("..............................................") |
| 123 | + model.writer.add_scalar("test/avg_step_reward", avg_step_reward, epoch) |
| 124 | + model.writer.add_scalar("test/avg_step_reward_std", avg_step_reward_std, epoch) |
| 125 | + model.writer.add_scalar("test/avg_ep_reward", avg_ep_reward, epoch) |
| 126 | + model.writer.add_scalar("test/avg_ep_reward_std", avg_ep_reward_std, epoch) |
| 127 | + model.writer.add_scalar("test/avg_col", avg_col, epoch) |
| 128 | + model.writer.add_scalar("test/avg_goal", avg_goal, epoch) |
| 129 | + model.writer.add_scalar("test/avg_inter_step_rew", avg_inter_step_rew, epoch) |
| 130 | + model.writer.add_scalar( |
| 131 | + "test/avg_inter_step_rew_std", avg_inter_step_rew_std, epoch |
| 132 | + ) |
| 133 | + model.writer.add_scalar("test/avg_steps_to_goal", avg_steps_to_goal, epoch) |
| 134 | + model.writer.add_scalar("test/avg_steps_to_goal_std", avg_steps_to_goal_std, epoch) |
| 135 | + model.writer.add_scalar("test/mean_lin_action", mean_lin_action, epoch) |
| 136 | + model.writer.add_scalar("test/lin_actions_std", lin_actions_std, epoch) |
| 137 | + model.writer.add_scalar("test/mean_ang_action", mean_ang_action, epoch) |
| 138 | + model.writer.add_scalar("test/ang_actions_std", ang_actions_std, epoch) |
| 139 | + bins = 100 |
| 140 | + model.writer.add_histogram("test/lin_actions", lin_actions, epoch, max_bins=bins) |
| 141 | + model.writer.add_histogram("test/ang_actions", ang_actions, epoch, max_bins=bins) |
| 142 | + |
| 143 | + counts, bin_edges = np.histogram(lin_actions, bins=bins) |
| 144 | + fig, ax = plt.subplots() |
| 145 | + ax.bar( |
| 146 | + bin_edges[:-1], counts, width=np.diff(bin_edges), align="edge", log=True |
| 147 | + ) # Log scale on y-axis |
| 148 | + ax.set_xlabel("Value") |
| 149 | + ax.set_ylabel("Frequency (Log Scale)") |
| 150 | + ax.set_title("Histogram with Log Scale") |
| 151 | + model.writer.add_figure("test/lin_actions_hist", fig) |
| 152 | + |
| 153 | + counts, bin_edges = np.histogram(ang_actions, bins=bins) |
| 154 | + fig, ax = plt.subplots() |
| 155 | + ax.bar( |
| 156 | + bin_edges[:-1], counts, width=np.diff(bin_edges), align="edge", log=True |
| 157 | + ) # Log scale on y-axis |
| 158 | + ax.set_xlabel("Value") |
| 159 | + ax.set_ylabel("Frequency (Log Scale)") |
| 160 | + ax.set_title("Histogram with Log Scale") |
| 161 | + model.writer.add_figure("test/ang_actions_hist", fig) |
| 162 | + |
| 163 | + |
| 164 | +if __name__ == "__main__": |
| 165 | + main() |
0 commit comments