PZR0033 commited on
Commit
a4539de
·
1 Parent(s): f3ec673

optimizer added

Browse files
Files changed (1) hide show
  1. rl_agent/utils.py +34 -0
rl_agent/utils.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import torch
3
+
4
+ class myOptimizer():
5
+
6
+ def __init__(self, lr, mu, mu_square, adaptation_rate, transaction_cost):
7
+ self.lr = lr
8
+ self.mu = mu
9
+ self.mu_square = mu_square
10
+ self.adaptation_rate = adaptation_rate
11
+ self.last_gradient = 0.0
12
+ self.transaction_cost = transaction_cost
13
+
14
+ def step(self, grad_n, reward, last_observation):
15
+
16
+ numerator = self.mu_square - (self.mu * reward)
17
+ denominator = torch.sqrt((self.mu_square - (self.mu ** 2)) ** 3)
18
+
19
+ gradient = numerator / denominator
20
+
21
+ self.mu = self.mu + self.adaptation_rate * (reward - self.mu)
22
+ self.mu_square = self.mu_square + self.adaptation_rate * ((reward ** 2) - self.mu_square)
23
+
24
+ current_grad = (-1.0 * self.transaction_cost * grad_n)
25
+ previous_grad = (last_observation + self.transaction_cost) * self.last_gradient
26
+
27
+ gradient = gradient * (current_grad + previous_grad)
28
+
29
+ self.last_gradient = grad_n
30
+
31
+ return self.lr * gradient
32
+
33
+
34
+