-
Notifications
You must be signed in to change notification settings - Fork 1
/
agents.py
249 lines (199 loc) · 7.02 KB
/
agents.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import numpy as np
import gym
from stable_baselines3 import A2C, PPO
import global_var
"""RLAgents"""
class Agent:
"""Agent基类。"""
def __init__(self):
self.name = None
def act(self, state: np.ndarray) -> np.ndarray:
pass
def learn(self, timesteps: int):
pass
def save(self, path: str):
pass
def load(self, path: str):
pass
def train_mode(self):
pass
def eval_mode(self):
pass
class DumbAgent(Agent):
"""每天随机采取行动的Agent。测试和基础对比用。"""
def __init__(self, env: gym.Env):
super().__init__()
self.name = 'Dumb'
self.env = env
def learn(self, timesteps: int):
pass # DumbAgent doesn't learn
def act(self, state: np.ndarray) -> np.ndarray:
action = self.env.action_space.sample() # sample an action from action space
# print('DumbAgent:', 'random action:', action)
return action
def train_mode(self):
pass
def eval_mode(self):
pass
class HoldAgent(Agent):
"""仅在第一天将余额平均分配买入各只股票,之后不再采取买卖动作的Agent。主要基线。"""
def __init__(self, env: gym.Env):
super().__init__()
self.name = 'Hold'
self.env = env
self.is_first_day = True
def learn(self, timesteps: int):
pass # HoldAgent doesn't learn
def act(self, state: np.ndarray) -> np.ndarray:
action = np.zeros(self.env.action_space.shape[0])
if self.is_first_day: # only perform at the first day
stock_dim = self.env.stock_dim
# 由于action被传入环境后会被乘以EVAL_MAX_PERCENTAGE_PER_TRADE参数放大,而HoldAgent应该永远保证在第一个交易日
# 将所有资金平均分配到各股票中,因此要做如下调整:
action_adjusted = 1 / stock_dim / global_var.EVAL_MAX_PERCENTAGE_PER_TRADE
action = np.array([action_adjusted for _ in range(stock_dim)])
# single_stock_budget = global_var.INITIAL_BALANCE * global_var.EVAL_MAX_PERCENTAGE_PER_TRADE * action_adjusted
# print('HoldAgent:', f'day 0, {stock_dim} stocks,'
# f' single stock budget {single_stock_budget}')
self.is_first_day = False
return action
def train_mode(self):
pass
def eval_mode(self):
pass
class A2CAgent(Agent):
"""采用A2C算法的Agent。"""
def __init__(self, env: gym.Env):
super().__init__()
self.name = 'A2C'
self.model = A2C('MlpPolicy', env, verbose=0)
self.determ_policy = False
def learn(self, timesteps: int):
self.model.learn(total_timesteps=timesteps)
def act(self, state: np.ndarray) -> np.ndarray:
action, _ = self.model.predict(state, deterministic=self.determ_policy)
# print('A2CAgent:', 'action:', action)
return action
def save(self, path: str):
self.model.save(path)
def load(self, path: str):
self.model = A2C.load(path)
def train_mode(self):
self.determ_policy = False
def eval_mode(self):
self.determ_policy = True
class PPOAgent(Agent):
"""采用PPO算法的Agent。"""
def __init__(self, env: gym.Env):
super().__init__()
self.name = 'PPO'
self.model = PPO('MlpPolicy', env, verbose=0)
self.determ_policy = False
def learn(self, timesteps: int):
self.model.learn(total_timesteps=timesteps)
def act(self, state: np.ndarray) -> np.ndarray:
action, _ = self.model.predict(state, deterministic=self.determ_policy)
# print('PPOAgent:', 'action:', action)
return action
def save(self, path: str):
self.model.save(path)
def load(self, path: str):
self.model = PPO.load(path)
def train_mode(self):
self.determ_policy = False
def eval_mode(self):
self.determ_policy = True
# class TD3Agent(Agent):
# """采用TD3算法的Agent。"""
#
# def __init__(self, env: gym.Env):
# n_actions = env.action_space.shape[-1]
# action_noise = NormalActionNoise(mean=np.zeros(n_actions), sigma=0.1 * np.ones(n_actions))
# self.model = TD3('MlpPolicy', env, verbose=1,
# action_noise=action_noise, learning_starts=1000, learning_rate=0.0002)
#
# def learn(self, timesteps: int):
# self.model.learn(total_timesteps=timesteps)
#
# def act(self, state: np.ndarray) -> np.ndarray:
# action, _ = self.model.predict(state)
# print('TD3Agent:', action.tolist())
# return action
#
# def save(self, path: str):
# self.model.save(path)
#
# def load(self, path: str):
# self.model = TD3.load(path)
#
#
# class SACAgent(Agent):
# """采用SAC算法的Agent。"""
#
# def __init__(self, env: gym.Env):
# self.model = SAC('MlpPolicy', env, verbose=1,
# gamma=0.99, learning_rate=0.0001, learning_starts=100, batch_size=512)
#
# def learn(self, timesteps: int):
# self.model.learn(total_timesteps=timesteps)
#
# def act(self, state: np.ndarray) -> np.ndarray:
# action, _ = self.model.predict(state)
# print('SACAgent:', action.tolist())
# return action
#
# def save(self, path: str):
# self.model.save(path)
#
# def load(self, path: str):
# self.model = SAC.load(path)
#
#
# class NNAgent(Agent):
# """DL的神经网络Agent,作为RL算法的对比。"""
#
# def __init__(self, env: gym.Env):
# self.env = env
# self.model = NeuralNetwork(env.observation_space.shape[0], env.action_space.shape[0])
# self.loss_fn = nn.CrossEntropyLoss()
# self.optimizer = torch.optim.SGD(self.model.parameters(), lr=1e-3)
#
# def learn(self, timesteps: int = 10000):
# self.model.train()
# epochs = timesteps
# for e in range(1, epochs+1):
# state = self.env.reset()
# while True:
# action = self.model.act(state)
# next_state, reward, done, _ = self.env.step(action)
# state = next_state
# if done:
# break
#
#
# def act(self, state: np.ndarray) -> np.ndarray:
# self.model.eval()
# return self.model(state)
#
#
# class NeuralNetwork(nn.Module):
# def __init__(self, input_dim, output_dim):
# super(NeuralNetwork, self).__init__()
# half = input_dim // 2
# self.net = nn.Sequential(
# nn.Linear(input_dim, half),
# nn.ReLU(),
# nn.Linear(half, half),
# nn.ReLU(),
# nn.Linear(half, output_dim)
# )
def agent_factory(agent_name: str, env) -> Agent:
if agent_name == 'Dumb':
return DumbAgent(env)
elif agent_name == 'Hold':
return HoldAgent(env)
elif agent_name == 'PPO':
return PPOAgent(env)
elif agent_name == 'A2C':
return A2CAgent(env)
raise ValueError('所需Agent未定义')