-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_exploration.py
82 lines (60 loc) · 2.21 KB
/
run_exploration.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
69
70
71
72
73
74
75
76
77
78
79
from ast import parse
import numpy as np
import random
from argparse import ArgumentParser
from tqdm import tqdm
from envs import RiverSwim
from agents import SARSA
from runners import run_experiment
from utils import *
def main():
parser = ArgumentParser()
parser.add_argument('--env', type=str, default='riverswim')
parser.add_argument('--seed', type=int, default=100)
parser.add_argument('--save', action='store_true', default=False)
parser.add_argument('--n_runs', type=int, default=10)
parser.add_argument('--total_steps', type=int, default=5000)
parser.add_argument('--use_fr', action='store_true', default=False)
parser.add_argument('--use_sr', action='store_true', default=False)
parser.add_argument('--gamma_sfr', type=float, default=0.99)
parser.add_argument('--step_size', type=float, default=0.25)
parser.add_argument('--epsilon', type=float, default=0.05)
parser.add_argument('--beta', type=float, default=1.0)
parser.add_argument('--eta', type=float, default=0.05)
args = parser.parse_args()
params = vars(args)
env_dict = {
'riverswim': RiverSwim
}
rewards = []
for i_exp in tqdm(range(1, params['n_runs']+1)):
#flush_print(f"training {i_exp}/{params['n_runs']}")
np.random.seed(params['seed'] + i_exp)
random.seed(params['seed'] + i_exp)
# create env
env = env_dict[params['env']]()
# create agent
agent = SARSA(
env.size,
2,
env.get_obs(),
use_sr=params['use_sr'],
use_fr=params['use_fr'],
step_size=params['step_size'],
epsilon=params['epsilon'],
eta=params['eta'],
gamma_sfr=params['gamma_sfr'],
beta=params['beta'],
norm=L1
)
# run
results = run_experiment(
env, agent, params['total_steps'], display_steps=None
)
rewards.append(np.cumsum(results['reward_hist']))
rewards = np.stack(rewards)
if params['save']:
np.savetxt("rewards.csv", rewards, delimiter=',')
print (f"\nmean total reward: {np.mean(rewards[:, -1])}")
if __name__ == '__main__':
main()