|
| 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.att import CNNTD3 |
| 7 | + |
| 8 | +import torch |
| 9 | +import numpy as np |
| 10 | +from sim2 import SIM_ENV |
| 11 | +from utils import get_buffer |
| 12 | + |
| 13 | +def outside_of_bounds(poses): |
| 14 | + outside = False |
| 15 | + for pose in poses: |
| 16 | + norm_x = pose[0] - 6 |
| 17 | + norm_y = pose[1] - 6 |
| 18 | + if abs(norm_x) > 10.5 or abs(norm_y) > 10.5: |
| 19 | + outside = True |
| 20 | + break |
| 21 | + return outside |
| 22 | + |
| 23 | +def main(args=None): |
| 24 | + """Main training function""" |
| 25 | + action_dim = 2 # number of actions produced by the model |
| 26 | + max_action = 1 # maximum absolute value of output actions |
| 27 | + state_dim = 9 # number of input values in the neural network (vector length of state input) |
| 28 | + device = torch.device( |
| 29 | + "cuda" if torch.cuda.is_available() else "cpu" |
| 30 | + ) # using cuda if it is available, cpu otherwise |
| 31 | + nr_eval_episodes = 10 # how many episodes to use to run evaluation |
| 32 | + max_epochs = 100 # max number of epochs |
| 33 | + epoch = 0 # starting epoch number |
| 34 | + episodes_per_epoch = 70 # how many episodes to run in single epoch |
| 35 | + episode = 0 # starting episode number |
| 36 | + train_every_n = 10 # train and update network parameters every n episodes |
| 37 | + training_iterations = 80 # how many batches to use for single training cycle |
| 38 | + batch_size = 16 # batch size for each training iteration |
| 39 | + max_steps = 300 # maximum number of steps in single episode |
| 40 | + steps = 0 # starting step number |
| 41 | + load_saved_buffer = False # whether to load experiences from assets/data.yml |
| 42 | + pretrain = False # whether to use the loaded experiences to pre-train the model (load_saved_buffer must be True) |
| 43 | + pretraining_iterations = ( |
| 44 | + 10 # number of training iterations to run during pre-training |
| 45 | + ) |
| 46 | + save_every = 5 # save the model every n training cycles |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + sim = SIM_ENV(world_file="multi_robot_world2.yaml",disable_plotting=False) # instantiate environment |
| 51 | + |
| 52 | + model = CNNTD3( |
| 53 | + state_dim=state_dim, |
| 54 | + action_dim=action_dim, |
| 55 | + max_action=max_action, |
| 56 | + num_robots=sim.num_robots, |
| 57 | + device=device, |
| 58 | + save_every=save_every, |
| 59 | + load_model=True, |
| 60 | + model_name="CNNTD3", |
| 61 | + ) # instantiate a model |
| 62 | + |
| 63 | + replay_buffer = get_buffer( |
| 64 | + model, |
| 65 | + sim, |
| 66 | + load_saved_buffer, |
| 67 | + pretrain, |
| 68 | + pretraining_iterations, |
| 69 | + training_iterations, |
| 70 | + batch_size, |
| 71 | + ) |
| 72 | + con = torch.tensor([[0. for _ in range(sim.num_robots-1)] for _ in range(sim.num_robots) ]) |
| 73 | + |
| 74 | + poses, distance, cos, sin, collision, goal, a, reward, positions, goal_positions = sim.step([[0, 0] for _ in range(sim.num_robots)], con) # get the initial step state |
| 75 | + |
| 76 | + while epoch < max_epochs: # train until max_epochs is reached |
| 77 | + state, terminal = model.prepare_state( |
| 78 | + poses, distance, cos, sin, collision, goal, a, positions, goal_positions |
| 79 | + ) # get state a state representation from returned data from the environment |
| 80 | + |
| 81 | + action, connection, combined_weights = model.get_action(np.array(state), True) # get an action from the model |
| 82 | + |
| 83 | + a_in = [[(a[0] + 1) / 4, a[1]] for a in action] # clip linear velocity to [0, 0.5] m/s range |
| 84 | + |
| 85 | + poses, distance, cos, sin, collision, goal, a, reward, positions, goal_positions = sim.step(a_in, connection, combined_weights) # get data from the environment |
| 86 | + next_state, terminal = model.prepare_state( |
| 87 | + poses, distance, cos, sin, collision, goal, a, positions, goal_positions |
| 88 | + ) # get a next state representation |
| 89 | + replay_buffer.add( |
| 90 | + state, action, reward, terminal, next_state |
| 91 | + ) # add experience to the replay buffer |
| 92 | + outside = outside_of_bounds(poses) |
| 93 | + if ( |
| 94 | + any(terminal) or steps == max_steps or outside |
| 95 | + ): # reset environment of terminal stat ereached, or max_steps were taken |
| 96 | + poses, distance, cos, sin, collision, goal, a, reward, positions, goal_positions = sim.reset() |
| 97 | + episode += 1 |
| 98 | + if episode % train_every_n == 0: |
| 99 | + model.train( |
| 100 | + replay_buffer=replay_buffer, |
| 101 | + iterations=training_iterations, |
| 102 | + batch_size=batch_size, |
| 103 | + ) # train the model and update its parameters |
| 104 | + |
| 105 | + steps = 0 |
| 106 | + else: |
| 107 | + steps += 1 |
| 108 | + |
| 109 | + if ( |
| 110 | + episode + 1 |
| 111 | + ) % episodes_per_epoch == 0: # if epoch is concluded, run evaluation |
| 112 | + episode = 0 |
| 113 | + epoch += 1 |
| 114 | + # evaluate(model, epoch, sim, eval_episodes=nr_eval_episodes) |
| 115 | + |
| 116 | + |
| 117 | +def evaluate(model, epoch, sim, eval_episodes=10): |
| 118 | + print("..............................................") |
| 119 | + print(f"Epoch {epoch}. Evaluating scenarios") |
| 120 | + avg_reward = 0.0 |
| 121 | + col = 0 |
| 122 | + goals = 0 |
| 123 | + for _ in range(eval_episodes): |
| 124 | + count = 0 |
| 125 | + poses, distance, cos, sin, collision, goal, a, reward, positions, goal_positions = sim.reset() |
| 126 | + done = False |
| 127 | + while not done and count < 501: |
| 128 | + state, terminal = model.prepare_state( |
| 129 | + poses, distance, cos, sin, collision, goal, a, positions, goal_positions |
| 130 | + ) |
| 131 | + action, connection, combined_weights = model.get_action(np.array(state), False) |
| 132 | + a_in = [[(a[0] + 1) / 4, a[1]] for a in action] |
| 133 | + poses, distance, cos, sin, collision, goal, a, reward, positions, goal_positions = sim.step(a_in, connection, combined_weights) |
| 134 | + avg_reward += sum(reward)/len(reward) |
| 135 | + count += 1 |
| 136 | + if collision: |
| 137 | + col += 1 |
| 138 | + if goal: |
| 139 | + goals += 1 |
| 140 | + done = collision or goal |
| 141 | + avg_reward /= eval_episodes |
| 142 | + avg_col = col / eval_episodes |
| 143 | + avg_goal = goals / eval_episodes |
| 144 | + print(f"Average Reward: {avg_reward}") |
| 145 | + print(f"Average Collision rate: {avg_col}") |
| 146 | + print(f"Average Goal rate: {avg_goal}") |
| 147 | + print("..............................................") |
| 148 | + model.writer.add_scalar("eval/avg_reward", avg_reward, epoch) |
| 149 | + model.writer.add_scalar("eval/avg_col", avg_col, epoch) |
| 150 | + model.writer.add_scalar("eval/avg_goal", avg_goal, epoch) |
| 151 | + |
| 152 | + |
| 153 | +if __name__ == "__main__": |
| 154 | + main() |
0 commit comments