-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsac_maze3d_train.py
68 lines (54 loc) · 2.12 KB
/
sac_maze3d_train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import time
from datetime import timedelta
from experiment import Experiment
from maze3D.Maze3DEnv import Maze3D
from maze3D.assets import *
from rl_models.sac_agent import Agent
from rl_models.sac_discrete_agent import DiscreteSACAgent
from rl_models.utils import get_config, get_plot_and_chkpt_dir, get_sac_agent
from maze3D.utils import save_logs_and_plot
import sys
"""
The code of this work is based on the following github repos:
https://github.com/kengz/SLM-Lab
https://github.com/EveLIn3/Discrete_SAC_LunarLander/blob/master/sac_discrete.py
"""
def main(argv):
# get configuration
config = get_config(argv[0])
# creating environment
maze = Maze3D(config_file=argv[0])
chkpt_dir, load_checkpoint_name = [None, None]
if config["game"]["save"]:
# create the checkpoint and plot directories for this experiment
chkpt_dir, plot_dir, load_checkpoint_name = get_plot_and_chkpt_dir(config)
# create the SAC agent
sac = get_sac_agent(config, maze, chkpt_dir)
# create the experiment
experiment = Experiment(maze, sac, config=config)
start_experiment = time.time()
# set the goal
goal = config["game"]["goal"]
# training loop
loop = config['Experiment']['loop']
if loop == 1:
# Experiment 1
experiment.loop_1(goal)
else:
# Experiment 2
experiment.loop_2(goal)
end_experiment = time.time()
experiment_duration = timedelta(seconds=end_experiment - start_experiment - experiment.duration_pause_total)
print('Total Experiment time: {}'.format(experiment_duration))
if config["game"]["save"]:
# save training logs to a pickle file
experiment.df.to_pickle(plot_dir + '/training_logs.pkl')
if not config['game']['test_model']:
total_games = experiment.max_episodes if loop == 1 else experiment.game
# save rest of the experiment logs and plot them
save_logs_and_plot(experiment, chkpt_dir, plot_dir, total_games)
experiment.save_info(chkpt_dir, experiment_duration, total_games, goal)
pg.quit()
if __name__ == '__main__':
main(sys.argv[1:])
exit(0)