-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtask3.py
More file actions
71 lines (57 loc) · 2.24 KB
/
Copy pathtask3.py
File metadata and controls
71 lines (57 loc) · 2.24 KB
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
69
70
71
# TASK 3
"""
Task 3 Train two agents with your algorithm of choice, on the source and target domains respectively. Then, test each model and
report its average return over 50 test episodes. In particular, report results for the following “training→test” configurations:
source→source,
source→target (lower bound),
target→target (upper bound).
Test with different hyperparameters and report the best results found together with the parameters used.
"""
from utils import load_model
from datetime import datetime
import gym
from env.custom_hopper import *
from stable_baselines3 import PPO
from stable_baselines3.common.monitor import Monitor
import os
from utils import train, test, test_plot
# Compatibilità Enf
import matplotlib as mpl
mpl.use("GTK3Agg")
start_time = datetime.now()
if not os.path.exists("target_model.mdl"):
print("Training target")
env = gym.make('CustomHopper-target-v0')
model, env = train(env, total_timesteps=100_00)
model.save("./target_model.mdl")
print(f"program ran for: {datetime.now() - start_time} ")
env = gym.make('CustomHopper-target-v0')
model = load_model('ppo', env, 'target_model.mdl')
rew, lens = test(model, Monitor(env, "./tmp/gym/target_model/"))
test_plot(rew, lens, title="target")
# Testing
# load source model from the previous task
model_src = PPO.load("source_model.mdl")
# policy_src = model_src.policy
model_trg = PPO.load("target_model.mdl")
# policy_trg = model_trg.policy
source = gym.make('CustomHopper-source-v0')
target = gym.make('CustomHopper-target-v0')
# # source -> source # We don't really need this
# monitor_src= Monitor(source)
# rew1,lens1 = test(policy_src, monitor_src, False)
# source.close()
# print("test source -> source")
# # source.render()
# test_plot(rew1,lens1, title="source -> source")
# source -> targe
monitor_trg = Monitor(target)
rew2, lens2 = test(model_src, monitor_trg, render=False)
print("test source -> target")
test_plot(rew2, lens2, title="source -> target", save_filename="source_target")
# target -> target
monitor_trg2 = Monitor(target)
rew3, lens3 = test(model_trg, monitor_trg2, render=False)
target.close()
print("test target -> target")
test_plot(rew3, lens3, title="target -> target", save_filename="target_target")