Skip to content

Commit d508138

Browse files
committed
add comment to summary writer and add a test env
1 parent 54f2b26 commit d508138

File tree

10 files changed

+197
-21
lines changed

10 files changed

+197
-21
lines changed

robot_nav/models/BPG/BCNNPG.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def __init__(
131131
self.action_dim = action_dim
132132
self.max_action = max_action
133133
self.state_dim = state_dim
134-
self.writer = SummaryWriter()
134+
self.writer = SummaryWriter(comment=model_name)
135135
self.iter_count = 0
136136
if load_model:
137137
self.load(filename=model_name, directory=load_directory)

robot_nav/models/BPG/BCNNTD3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def __init__(
147147
self.action_dim = action_dim
148148
self.max_action = max_action
149149
self.state_dim = state_dim
150-
self.writer = SummaryWriter()
150+
self.writer = SummaryWriter(comment=model_name)
151151
self.iter_count = 0
152152
if load_model:
153153
self.load(filename=model_name, directory=load_directory)

robot_nav/models/BPG/BTD3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def __init__(
101101
self.action_dim = action_dim
102102
self.max_action = max_action
103103
self.state_dim = state_dim
104-
self.writer = SummaryWriter()
104+
self.writer = SummaryWriter(comment=model_name)
105105
self.iter_count = 0
106106
if load_model:
107107
self.load(filename=model_name, directory=load_directory)

robot_nav/models/CNNTD3/CNNTD3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def __init__(
145145
self.action_dim = action_dim
146146
self.max_action = max_action
147147
self.state_dim = state_dim
148-
self.writer = SummaryWriter()
148+
self.writer = SummaryWriter(comment=model_name)
149149
self.iter_count = 0
150150
if load_model:
151151
self.load(filename=model_name, directory=load_directory)

robot_nav/models/PPO/PPO.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def __init__(
157157
self.load(filename=model_name, directory=load_directory)
158158

159159
self.MseLoss = nn.MSELoss()
160-
self.writer = SummaryWriter()
160+
self.writer = SummaryWriter(comment=model_name)
161161

162162
def set_action_std(self, new_action_std):
163163
self.action_std = new_action_std

robot_nav/models/RCPG/RCPG.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def __init__(
189189
self.action_dim = action_dim
190190
self.max_action = max_action
191191
self.state_dim = state_dim
192-
self.writer = SummaryWriter()
192+
self.writer = SummaryWriter(comment=model_name)
193193
self.iter_count = 0
194194
self.model_name = model_name + rnn
195195
if load_model:

robot_nav/models/SAC/SAC.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def __init__(
112112
self.actor.train(True)
113113
self.critic.train(True)
114114
self.step = 0
115-
self.writer = SummaryWriter()
115+
self.writer = SummaryWriter(comment=model_name)
116116

117117
def save(self, filename, directory):
118118
Path(directory).mkdir(parents=True, exist_ok=True)

robot_nav/sim.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77

88

99
class SIM_ENV:
10-
def __init__(self, world_file="robot_world.yaml"):
11-
self.env = irsim.make(world_file)
10+
def __init__(self, world_file="robot_world.yaml", disable_plotting=False):
11+
self.env = irsim.make(world_file, disable_all_plot=disable_plotting)
1212
robot_info = self.env.get_robot_info(0)
1313
self.robot_goal = robot_info.goal
1414

1515
def step(self, lin_velocity=0.0, ang_velocity=0.1):
1616
self.env.step(action_id=0, action=np.array([[lin_velocity], [ang_velocity]]))
17-
self.env.render()
17+
self.env.render(interval=0.01)
1818

1919
scan = self.env.get_lidar_scan()
2020
latest_scan = scan["ranges"]
@@ -34,7 +34,13 @@ def step(self, lin_velocity=0.0, ang_velocity=0.1):
3434

3535
return latest_scan, distance, cos, sin, collision, goal, action, reward
3636

37-
def reset(self, robot_state=None, robot_goal=None, random_obstacles=True):
37+
def reset(
38+
self,
39+
robot_state=None,
40+
robot_goal=None,
41+
random_obstacles=True,
42+
random_obstacle_ids=None,
43+
):
3844
if robot_state is None:
3945
robot_state = [[random.uniform(1, 9)], [random.uniform(1, 9)], [0], [0]]
4046

@@ -44,10 +50,12 @@ def reset(self, robot_state=None, robot_goal=None, random_obstacles=True):
4450
)
4551

4652
if random_obstacles:
53+
if random_obstacle_ids is None:
54+
random_obstacle_ids = [i + 1 for i in range(7)]
4755
self.env.random_obstacle_position(
4856
range_low=[0, 0, -3.14],
4957
range_high=[10, 10, 3.14],
50-
ids=[i + 1 for i in range(7)],
58+
ids=random_obstacle_ids,
5159
non_overlapping=True,
5260
)
5361

robot_nav/test_random.py

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

robot_nav/train.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
from models.TD3.TD3 import TD3
2-
from models.DDPG.DDPG import DDPG
1+
from robot_nav.models.TD3.TD3 import TD3
2+
from robot_nav.models.DDPG.DDPG import DDPG
33
from robot_nav.models.BPG.BTD3 import BTD3
44
from robot_nav.models.BPG.BPG import BPG
5-
from models.SAC.SAC import SAC
6-
from models.HCM.hardcoded_model import HCM
7-
from models.PPO.PPO import PPO
5+
from robot_nav.models.BPG.BCNNPG import BCNNPG
6+
from robot_nav.models.SAC.SAC import SAC
7+
from robot_nav.models.HCM.hardcoded_model import HCM
8+
from robot_nav.models.PPO.PPO import PPO
89
from robot_nav.models.CNNTD3.CNNTD3 import CNNTD3
910

1011
import torch
@@ -38,18 +39,18 @@ def main(args=None):
3839
)
3940
save_every = 5 # save the model every n training cycles
4041

41-
model = BPG(
42+
model = BCNNPG(
4243
state_dim=state_dim,
4344
action_dim=action_dim,
4445
max_action=max_action,
4546
device=device,
4647
save_every=save_every,
4748
load_model=False,
48-
model_name="BPGw4exp1",
49-
bound_weight=4,
49+
model_name="BCNNPGw025exp1",
50+
bound_weight=0.25,
5051
) # instantiate a model
5152

52-
sim = SIM_ENV() # instantiate environment
53+
sim = SIM_ENV(disable_plotting=True) # instantiate environment
5354
replay_buffer = get_buffer(
5455
model,
5556
sim,

0 commit comments

Comments
 (0)