-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_vector.py
276 lines (239 loc) · 9.58 KB
/
generate_vector.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import numpy as np
class Mean_vector:
# m维空间,H:[0/H,1/H.....H/H]
def __init__(self, H=10, m=3):
self.H = H
self.m = m
self.stepsize = 1 / H
print("\nGenerating vector")
def perm(self, sequence):
# !!! 序列全排列,且无重复
l = sequence
if (len(l) <= 1):
return [l]
r = []
for i in range(len(l)):
if i != 0 and sequence[i - 1] == sequence[i]:
continue
else:
s = l[:i] + l[i + 1:]
p = self.perm(s)
for x in p:
r.append(l[i:i + 1] + x)
return r
def get_mean_vectors(self):
#print("\nget mean vector")
#生成权均匀向量
H = self.H
m = self.m
sequence = []
for ii in range(H):
sequence.append(0)
for jj in range(m - 1):
sequence.append(1)
ws = []
pe_seq = self.perm(sequence)
for sq in pe_seq:
s = -1
weight = []
for i in range(len(sq)):
if sq[i] == 1:
w = i - s
w = (w - 1) / H
s = i
weight.append(w)
nw = H + m - 1 - s
nw = (nw - 1) / H
weight.append(nw)
if weight not in ws:
ws.append(weight)
return ws
def get_sorted_vectors(self):
#print("\nget sorted vector")
#按0的个数排序
mv = self.get_mean_vectors()
oIndex = [0 for _ in range(self.m+1)]
ws = [0 for _ in range(len(mv))]
for i in range(len(mv)):
count0 = mv[i].count(0) #0的个数
oIndex[self.m-count0] += 1 #下标对应目标的数量,子目标的数量为m-count0的个数
for i in range(self.m):
oIndex[i+1] += oIndex[i]
oi = list(oIndex)
for i in range(len(mv)):
count0 = mv[i].count(0) # 0的个数
ws[oIndex[self.m-count0-1]] = mv[i]
oIndex[self.m-count0-1] += 1
return ws, oi
def get_similar_vectors(self):
#print("\nget similar vector")
#按0的个数排序
m_v_s, oi = self.get_sorted_vectors()
mvidex = np.zeros(len(m_v_s))
sim1 = list()
sim2 = list()#如果目标的个数为2,用两个表记录w的相似关系,开始的w为0,1 和1,0
sim3 = list()#如果目标的个数为3,开始的w为0,0,1 和0,1,0 以及1,0,0
if self.m == 2:
sim1.append(m_v_s[0])
sim2.append(m_v_s[1])
mvidex[0] = 1 #mvidex记录当前的w是否被加入相似关系的表中
mvidex[1] = 1
simp1 = 0 #记录当前w的位置
simp2 = 0
sim1n = 1
while np.count_nonzero(mvidex) < len(m_v_s):
cv = np.array(sim1[simp1])
tMin1 = 999999
tMinIdex1 = 0
for si1 in range(oi[1],oi[2]):
if mvidex[si1] == 0:
t = np.sqrt(np.sum(np.square(cv - np.array(m_v_s[si1]))))
if t < tMin1:
tMin1 = t
tMinIdex1 = si1
sim1.append(m_v_s[tMinIdex1])
mvidex[tMinIdex1] = 1
simp1 += 1
if np.count_nonzero(mvidex) == len(m_v_s):
break
else:
sim1n += 1
cv = np.array(sim2[simp2])
tMin2 = 999999
tMinIdex2 = 0
for si2 in range(oi[1], oi[2]):
if mvidex[si2] == 0:
t = np.sqrt(np.sum(np.square(cv - np.array(m_v_s[si2]))))
if t < tMin2:
tMin2 = t
tMinIdex2 = si2
sim2.append(m_v_s[tMinIdex2])
mvidex[tMinIdex2] = 1
simp2 += 1
mvsidex = np.zeros(len(m_v_s))#mvidex重新定义,用于记录不同相似W的开始位置
mvsidex[0] = 1 #当mvsidex==1,使用随机方式初始化参数,在训练时
mvsidex[sim1n+1] =1 #两个目标,所以有两个位置需要被记录,用于随机化
return sim1 + sim2 + sim3, mvsidex
else: #目标的个数 == 3, 暂不考虑更多维
sim1.append(m_v_s[0])
sim2.append(m_v_s[1])
sim3.append(m_v_s[2])
mvidex[0] = 1 # mvidex记录当前的w是否被加入相似关系的表中
mvidex[1] = 1
mvidex[2] = 1
simp1 = 0
simp2 = 0
simp3 = 0
sim1n = 1
sim2n = 1
work_2zero = 1
while np.count_nonzero(mvidex) < len(m_v_s):
if work_2zero == 1:
cv = np.array(sim1[simp1])
tMin1 = 999999
tMinIdex1 = 0
for si1 in range(oi[1],oi[2]):
if mvidex[si1] == 0:
t = np.sqrt(np.sum(np.square(cv - np.array(m_v_s[si1]))))
if t < tMin1:
tMin1 = t
tMinIdex1 = si1
if tMin1 != 999999:
sim1.append(m_v_s[tMinIdex1])
mvidex[tMinIdex1] = 1
simp1 += 1
sim1n += 1
cv = np.array(sim2[simp2])
tMin2 = 999999
tMinIdex2 = 0
for si2 in range(oi[1], oi[2]):
if mvidex[si2] == 0:
t = np.sqrt(np.sum(np.square(cv - np.array(m_v_s[si2]))))
if t < tMin2:
tMin2 = t
tMinIdex2 = si2
if tMin2 != 999999:
sim2.append(m_v_s[tMinIdex2])
mvidex[tMinIdex2] = 1
simp2 += 1
sim2n += 1
cv = np.array(sim3[simp3])
tMin3 = 999999
tMinIdex3 = 0
for si3 in range(oi[1], oi[2]):
if mvidex[si3] == 0:
t = np.sqrt(np.sum(np.square(cv - np.array(m_v_s[si3]))))
if t < tMin3:
tMin3 = t
tMinIdex3 = si3
if tMin3 != 999999:
sim3.append(m_v_s[tMinIdex3])
mvidex[tMinIdex3] = 1
simp3 += 1
if np.count_nonzero(mvidex) == oi[2]:
work_2zero = 0
else:
cv = np.array(sim1[simp1])
tMin1 = 999999
tMinIdex1 = 0
for si1 in range(oi[2], oi[3]):
if mvidex[si1] == 0:
t = np.sqrt(np.sum(np.square(cv - np.array(m_v_s[si1]))))
if t < tMin1:
tMin1 = t
tMinIdex1 = si1
if tMin1 != 999999:
sim1.append(m_v_s[tMinIdex1])
mvidex[tMinIdex1] = 1
simp1 += 1
sim1n += 1
cv = np.array(sim2[simp2])
tMin2 = 999999
tMinIdex2 = 0
for si2 in range(oi[2], oi[3]):
if mvidex[si2] == 0:
t = np.sqrt(np.sum(np.square(cv - np.array(m_v_s[si2]))))
if t < tMin2:
tMin2 = t
tMinIdex2 = si2
if tMin2 != 999999:
sim2.append(m_v_s[tMinIdex2])
mvidex[tMinIdex2] = 1
simp2 += 1
sim2n += 1
cv = np.array(sim3[simp3])
tMin3 = 999999
tMinIdex3 = 0
for si3 in range(oi[2], oi[3]):
if mvidex[si3] == 0:
t = np.sqrt(np.sum(np.square(cv - np.array(m_v_s[si3]))))
if t < tMin3:
tMin3 = t
tMinIdex3 = si3
if tMin3 != 999999:
sim3.append(m_v_s[tMinIdex3])
mvidex[tMinIdex3] = 1
simp3 += 1
mvsidex = np.zeros(len(m_v_s)) # mvidex重新定义,用于记录不同相似W的开始位置
mvsidex[0] = 1 # 当mvsidex==1,使用随机方式初始化参数,在训练时
mvsidex[sim1n] = 1
mvsidex[sim1n +sim2n ] = 1
return sim1 + sim2 + sim3, mvsidex
def save_mv_to_file(self, mv, name='out.csv'):
#保存为csv
f = np.array(mv, dtype=np.float32)
#f = np.round(f, 2)
np.savetxt(fname=name, X=f)
def test(self):
#测试
m_v = self.get_mean_vectors()
print("mv: ", m_v)
m_v_s, oi = self.get_sorted_vectors()
print("\nmvs: ", m_v_s,"\nmv_sort_index: ",oi)
a, b = self.get_similar_vectors()
print("\nmv_sim: ", a, "\nmv_sim_index: ", b)
#self.save_mv_to_file(a, 'test.csv')
#print(oi)
if __name__ == "__main__":
mv = Mean_vector(20, 3)
mv.test()