-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAgent_v2.py
205 lines (170 loc) · 6.77 KB
/
Agent_v2.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
import numpy as np
import matplotlib.pyplot as plt
"""
m:ft,feature
batchSize: a trajectory
batchLength: trajectories
"""
class Agent2(object):
def __init__(self, fileName, m, batchSize, trajecNum):
self.action_space = [-1, 0, 1]
self.m = m
self.batchSize = batchSize
self.trajecNum = trajecNum
#跳过第一行
f_matrix = np.loadtxt(open(fileName,'rb'),delimiter=',',skiprows=1)
self.dataBase = f_matrix[:,4]
#print(self.dataBase)
#f = open(fileName, 'r')
#self.dataBase = f.readline()
#self.dataBase = self.dataBase.split(',')
#self.dataBase.pop()
#print(len(self.dataBase))
self.diff = []
#按照价格序列
for i in range(len(self.dataBase)):
self.dataBase[i] = float(self.dataBase[i])
for i in range(1, len(self.dataBase)):
self.diff.append(self.dataBase[i] - self.dataBase[i-1])
#对数据进行归一化处理
self.statenew = []
mean = np.mean(self.diff)
variance = np.var(self.diff)
self.diff1 = (self.diff - mean)/variance
for i in range(0,len(self.diff1)-m+1):
self.statenew.append(self.diff1[i:i+m])
self.state = []
for i in range(0,len(self.diff)-m):
#state由前m个价差表示
self.state.append(self.diff[i:i+m])
#state 由h,l,o,c,volume,amt,chg,pct_chg,oi,MA....
befor = 2
for i in range(len(self.diff)-(m-befor)):
#newstate =np.hstack((f_matrix[i+(m-10),1:11],self.diff[i:i+m-10]))
#newstate = np.hstack((f_matrix[i+(m-befor),5],f_matrix[i+(m-befor),7],f_matrix[i+(m-befor)+1,9],self.diff[i:i+m-befor]))
newstate = np.hstack((f_matrix[i+(m-befor),4],f_matrix[i+(m-befor),5],self.diff[i:i+m-befor]))
#self.state.append(newstate)
#if i == 0 :
# print(newstate)
# print(np.shape(newstate))
def choose_action(self,state):
pass
# return np.random.randint(-1,2)
def get_state(self,i):
#index = np.random.randint(0, len(self.state)-self.batchSize+1)
index = i*100
state = self.state[index:index+self.batchSize]
return state
def get_reward(self,state,action):
#rewards=[float(0)]
rewards = []
#print(np.shape(state))
#print(np.shape(action))
#print(len(action))
state=np.reshape(state,[-1,self.m])
action=np.reshape(action,[-1])
action = action - 1
#print(np.shape(state))
#print(np.shape(action))
for i in range(len(action)):
if i == 0 :
reward = -1*abs(action[i])
#reward = 0
else:
reward=action[i-1]*state[i][-1]-1*abs(action[i]-action[i-1])
#reward=action[i-1]*state[i][-1]
rewards.append(reward)
return rewards
#def get_trajectory(self,i,test,train):
def get_trajectory(self,i):
#index = np.random.randint(0, len(self.state)-self.batchSize+1)
#state = self.state[index:index+self.batchSize]
#state=[]
#for item in self.state[index:index+self.batchSize]:
# state.append(item)
#for item in self.dataBase[index:index+self.batchSize]:
# state.append(item)
#index = i*100
index = i
state = self.state[index:index+self.batchSize]
print("state")
action0 = self.choose_action(state)
#print("action")
print(action0)
#将action转换为-1,0,1
action = action0 -1
print("action")
#重新定义reward
#状态s0,采取动作a0,状态转移为s1,产生reward:p1-p0,a0产生的代价是,0到a0.最后一个状态产生reward为0
#rewards=[]
#for i in range(self.batchSize-1):
# if i == 0:
# reward = action[i]*state[i+1][-1]-1*abs(action[i])
#reward = action[i]*state[i+1][-1]
# else:
# reward = action[i]*state[i+1][-1]-1*abs(action[i]-action[i-1])
#reward = action[i]*state[i+1][-1]
# rewards.append(reward)
#rewards.append(float(0))
#文章中的定义reward
#在状态0时刻,不产生reward,但是当产生1,或者-1的时候,会产生手续费
rewards = []
for i in range(self.batchSize):
if i==0:
#test的时候,两个bantch首位相接
#if test:
#print("start")
#print(self.start)
# rew = self.start * state[i][-1]- 1* abs(action[i]-self.start)
#if train:
#print("begin")
#print(self.begin)
# rew = self.begin * state[i][-1]- 1* abs(action[i]-self.begin)
rew = - 1* abs(action[i])
#rew=0
else:
rew = action[i-1] * state[i][-1] - 1* abs(action[i]-action[i-1])
#rew = action[i-1] * state[i][-1]
rewards.append(rew)
#print(rewards)
return {"reward":rewards,
"state": state,
"action": action0
}
def random_trajectory(self,i):
index = np.random.randint(0, len(self.state)-self.batchSize+1)
state = self.state[index:index+self.batchSize]
#action = np.random.randint(-1,2,size=100)
#将action转换为-1,0,1
action = self.choose_action(state)
action = action -1
#重新定义reward
#状态s0,采取动作a0,状态转移为s1,产生reward:p1-p0,a0产生的代价是,0到a0.最后一个状态产生reward为0
rewards=[]
for i in range(self.batchSize):
if i == 0:
#reward = action[i]*state[i+1][-1]-1*abs(action[i])
#reward = state[i+1][-1]
reward = - 1* abs(action[i])
else:
#reward = action[i]*state[i+1][-1]-1*abs(action[i]-action[i-1])
#reward = state[i+1][-1]
rew = action[i-1] * state[i][-1] - 1* abs(action[i]-action[i-1])
rewards.append(reward)
#rewards.append(float(0))
return {"reward":rewards,
"state": state,
"action": action
}
def get_trajectories(self):
#
index=10
trajectories = []
i=0
#while i < self.trajecNum and index<=len(self.state)-self.batchSize+1:
while i < self.trajecNum:
i += 1
trajectory = self.get_trajectory(i)
index +=1
trajectories.append(trajectory)
return trajectories