-
Notifications
You must be signed in to change notification settings - Fork 1
/
moreinforce_dst.py
123 lines (101 loc) · 3.61 KB
/
moreinforce_dst.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import gym
from datetime import datetime
import uuid
class Actor(nn.Module):
def __init__(self, nS):
super(Actor, self).__init__()
self.out = nn.Sequential(
nn.Linear(nS,50),
nn.Tanh(),
nn.Linear(50,4)
)
# self.out = nn.Linear(7,2)
def forward(self, x):
x = self.out(x)
x = F.log_softmax(x, dim=-1)
return x
def utility(values):
debt = 45; deadline = 10; penalty = -10
ut = F.softplus(values[:,0]-debt)
# everything lower than deadline yields 0, otherwise, additional steps are squared
uf = -(values[:,1].abs()-deadline).clamp(0)**2
uf[uf.nonzero()] += penalty
return (ut+uf).view(-1,1)
def all_returns(env, gamma):
returns = torch.empty(0,2)
for k, v in env.unwrapped._treasures().items():
steps = sum(k)
r = torch.tensor([[v*gamma**steps, sum([-1*gamma**i for i in range(steps)])]])
returns = torch.cat((returns, r), dim=0)
return returns
def make_weighted_sum(env, gamma, weights, normalize=False):
min_u, max_u = 0., 1.
if normalize:
returns = all_returns(env, gamma)
values = torch.sum(returns*weights, dim=-1)
min_u, max_u = values.min(), values.max()
def utility(values):
v = torch.sum(values*weights, dim=-1, keepdim=True)
v = (v-min_u)/(max_u-min_u)
return v
return utility
class NormalizedEnv(gym.RewardWrapper):
def __init__(self, env, weights, gamma):
super(NormalizedEnv, self).__init__(env)
returns = all_returns(DeepSeaTreasureEnv(), gamma)
values = torch.sum(returns*weights, dim=-1)
self.min_u = values.min().item()
self.max_u = values.max().item()
def reward(self, rew):
breakpoint()
return (rew-self.min_u)/(self.max_u-self.min_u)
if __name__ == '__main__':
from agents.moreinforce import MOReinforce
from policies.policy import Categorical
from memory.memory import EpisodeMemory
from gym.wrappers import TimeLimit
from wrappers.one_hot import OneHotEnv
from wrappers.weighted_sum import WeightedSum
from wrappers.terminal import TerminalEnv
from envs.dst import DeepSeaTreasureEnv
from log.plotter import Plotter
import argparse
parser = argparse.ArgumentParser(description='')
parser.add_argument('--lr', default=1e-3, type=float)
parser.add_argument('--gamma', default=0.95, type=float)
parser.add_argument('--w', default=0., type=float)
parser.add_argument('--timesteps', default=50000, type=int)
args = parser.parse_args()
print(args)
gamma = args.gamma
w = args.w
normalize = False
env = DeepSeaTreasureEnv()
env = TimeLimit(env, 100)
env = OneHotEnv(env, env.nS)
# env = TerminalEnv(env , utility)
# env = WeightedSum(env, np.array([w, 1.-w]))
if normalize:
env = NormalizedEnv(env, torch.tensor([[w, 1.-w]]), gamma)
actor = Actor(env.nS)
utility_function = make_weighted_sum(env, gamma, torch.tensor([[w, 1.-w]]))
# utility_function = utility
logdir = f'runs/deep_sea_treasure/moreinforce/gamma_{gamma}/w_{w}/lr_{args.lr}/'
logdir += datetime.now().strftime('%Y-%m-%d_%H-%M-%S_') + str(uuid.uuid4())[:4] + '/'
agent = MOReinforce(
env,
Categorical(),
EpisodeMemory(),
actor,
utility=utility_function,
gamma=gamma,
lr=args.lr,
logdir=logdir,
)
agent.train(timesteps=args.timesteps) #, eval_freq=0.1)
Plotter(logdir)
returns = all_returns(env, gamma)