Skip to content

Commit

Permalink
Change folder name from result files to logs, add speed comparison fo…
Browse files Browse the repository at this point in the history
…r intersection calculation
  • Loading branch information
Erikx3 committed May 12, 2022
1 parent 9e3a7c1 commit d6058a1
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,4 @@ fabric.properties
gym_dockauv/notes.py
/gym_dockauv/tests/test_notes.py
result_files/
logs/
2 changes: 1 addition & 1 deletion gym_dockauv/config/env_default_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# ---------- SIMULATION --------------
"t_step_size": 0.10, # Length of each simulation timestep [s]
"interval_datastorage": 20, # Interval of episodes on which extended data is saved through data class
"save_path_folder": os.path.join(os.getcwd(), "result_files"), # Folder name where all result files will be stored
"save_path_folder": os.path.join(os.getcwd(), "logs"), # Folder name where all result files will be stored

# ---------- GOAL ----------
"goal_location": np.array([0, 0, 0]),
Expand Down
12 changes: 6 additions & 6 deletions gym_dockauv/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def train(total_timesteps: int) -> None:
model = PPO('MlpPolicy', env, verbose=0)
model.learn(total_timesteps=total_timesteps) # Train the agent
# Save the agent
model.save("PPO_docking")
model.save("logs/PPO_docking")
env.save()


Expand All @@ -29,7 +29,7 @@ def predict():
# NOTE: if you have loading issue, you can pass `print_system_info=True`
# to compare the system on which the model was trained vs the current one
# model = DQN.load("dqn_lunar", env=env, print_system_info=True)
model = PPO.load("PPO_docking", env=env)
model = PPO.load("logs/PPO_docking", env=env)

# Evaluate the agent
# NOTE: If you use wrappers with your environment that modify rewards,
Expand All @@ -46,21 +46,21 @@ def predict():
env.render()


def post_analysis_directory():
directory = "/home/erikx3/PycharmProjects/gym_dockauv/result_files"
def post_analysis_directory(directory: str = "/home/erikx3/PycharmProjects/gym_dockauv/logs"):

for file in os.listdir(directory):
filename = os.fsdecode(file)
# Capture full data pkl file
full_path = os.path.join(directory, filename)
if filename.endswith("FULL.pkl"):
if filename.endswith("FULL__DATA__STORAGE.pkl"):
full_stor = FullDataStorage()
full_stor.load(full_path)
full_stor.plot_rewards()
# Episode Data Storage:
elif filename.endswith(".pkl"):
epi_stor = EpisodeDataStorage()
epi_stor.load(full_path)
epi_stor.plot_epsiode_states_and_u()
epi_stor.plot_rewards()
plt.show()
#epi_stor.plot_episode_animation(t_per_step=None, title="Test Post Flight Visualization")
#epi_stor.plot_episode_animation(t_per_step=None, title="Test Post Flight Visualization")
8 changes: 4 additions & 4 deletions gym_dockauv/utils/datastorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def set_up_episode_storage(self, env, path_folder: str, title: str = "") -> None
r"""
Set up the storage to save and update incoming data with passing a reference of the env
file_save_name: will be formatted path_folder\YYYY-MM-DDTHH-MM-SS__{title}__FULL.pkl
file_save_name: will be formatted path_folder\YYYY-MM-DDTHH-MM-SS__{title}__FULL_DATA_STORAGE.pkl
:param env: The gym environment
:param path_folder: Path to folder
Expand All @@ -49,7 +49,7 @@ def set_up_episode_storage(self, env, path_folder: str, title: str = "") -> None
utc_str = datetime.datetime.utcnow().strftime('%Y_%m_%dT%H_%M_%S')
if len(path_folder) > 0:
os.makedirs(path_folder, exist_ok=True) # Create folder if not exists yet
self.file_save_name = os.path.join(path_folder, f"{utc_str}__{title}__FULL.pkl")
self.file_save_name = os.path.join(path_folder, f"{utc_str}__{title}__FULL_DATA_STORAGE.pkl")

cum_rewards_arr = ArrayList(env.cum_reward_arr)
rewards_arr = ArrayList(env.last_reward_arr)
Expand Down Expand Up @@ -213,7 +213,7 @@ def set_up_episode_storage(self, path_folder: str, vehicle: AUVSim, step_size: f
Set up the storage to save and update incoming data, including passing a reference to the vehicle and
environment
file_save_name: will be formatted path_folder\YYYY-MM-DDTHH-MM-SS__episode{episode}__{title}.pkl
file_save_name: will be formatted path_folder\YYYY-MM-DDTHH-MM-SS__{title}__EPISODE_{episode}_DATA_STORAGE.pkl
:param path_folder: Path to folder
Expand All @@ -232,7 +232,7 @@ def set_up_episode_storage(self, path_folder: str, vehicle: AUVSim, step_size: f
utc_str = datetime.datetime.utcnow().strftime('%Y_%m_%dT%H_%M_%S')
if len(path_folder) > 0:
os.makedirs(path_folder, exist_ok=True) # Create folder if not exists yet
self.file_save_name = os.path.join(path_folder, f"{utc_str}__episode{episode}__{title}.pkl")
self.file_save_name = os.path.join(path_folder, f"{utc_str}__{title}__EPISODE_{episode}_DATA_STORAGE.pkl")
self.vehicle = vehicle # Vehicle instance (not a copy, automatically a reference which is updated in reference)
if shapes is None:
shapes = []
Expand Down
53 changes: 52 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import matplotlib.pyplot as plt
import numpy as np
import gym
import gym_dockauv
Expand All @@ -11,7 +12,7 @@
import gym_dockauv.train as train

if __name__ == "__main__":
train.train(total_timesteps=20000)
train.train(total_timesteps=10000)
train.predict()
train.post_analysis_directory()

Expand Down Expand Up @@ -53,3 +54,53 @@
# env.reset()
# done = False


# import gym_dockauv.objects.shape as shape
# import time
#
#
# def test_not_vec(l1, ld, cap1, cap2, cap_rad=0.1):
# for r1, rd in zip(l1, ld):
# dist = shape.intersec_dist_line_capsule(l1=r1, ld=rd, cap1=cap1, cap2=cap2, cap_rad=cap_rad)
#
#
# def test_vectorized(l1, ld, cap1, cap2, cap_rad=0.1):
# dist = shape.intersec_dist_line_capsule_vectorized(l1=l1, ld=ld, cap1=cap1, cap2=cap2, cap_rad=cap_rad)


# if __name__ == '__main__':
# # Init
# t_not_vec = []
# t_vec = []
# jj = 100 # Number of function executions in a run
# # # Loop over different amount of rays
# for i in range(1, 100):
# # Create arrays
# l1 = np.random.random((i, 3))
# ld = np.random.random((i, 3))
# cap1 = np.random.random(3)
# cap2 = np.random.random(3)
#
# # Find the sum over xx calls
# loop = [[], []] # Index 0, not vec, Index1, vec
# for j in range(0, jj):
# t = time.process_time()
# test_not_vec(l1, ld, cap1, cap2)
# elapsed_time = time.process_time() - t
# loop[0].append(elapsed_time)
#
# t = time.process_time()
# test_vectorized(l1, ld, cap1, cap2)
# elapsed_time = time.process_time() - t
# loop[1].append(elapsed_time)
#
# t_not_vec.append(sum(loop[0]))
# t_vec.append(sum(loop[1]))
#
# plt.figure()
# plt.plot(t_not_vec, label="Not vectorized")
# plt.plot(t_vec, label="Vectorized")
# plt.legend()
# plt.xlabel("Number of rays")
# plt.ylabel(f"t [s] over {jj} runs")
# plt.show()

0 comments on commit d6058a1

Please sign in to comment.