-
Notifications
You must be signed in to change notification settings - Fork 12
/
pca_analyzer.go
335 lines (291 loc) · 7.54 KB
/
pca_analyzer.go
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package main
import (
"fmt"
"log"
"math"
"math/rand"
"sync"
)
// *****************************************************************************
// ******************* PCA Based seed test case analysis ***********************
type pcaAnalyzer struct {
mtx sync.RWMutex
brMap map[int]struct{}
brList []int
pl *pcaLearner
// Internal seed copy
seedsMtx *sync.Mutex
seedPts *seedList
acceptedSeedHist []int
// Communication with seed manager: PCA features
featMtx sync.Mutex
feats map[uint64]*pcaFeatures
// Reset/cull seeds channel.
pcaCullCh chan []uint64
frkSrvNb int
}
func newPCAAnalyzer(seedPts seedList, pcaCullCh chan []uint64, frkSrvNb int) (
pcaA *pcaAnalyzer) {
pcaA = new(pcaAnalyzer)
pcaA.brMap = make(map[int]struct{})
pcaA.seedsMtx = new(sync.Mutex)
pcaA.seedPts = new(seedList)
*pcaA.seedPts = seedPts
pcaA.feats = make(map[uint64]*pcaFeatures)
for _, seedPt := range seedPts {
feat := new(pcaFeatures)
seedPt.pcaFeat = feat
pcaA.feats[seedPt.hash] = feat
}
pcaA.pcaCullCh = pcaCullCh
pcaA.frkSrvNb = frkSrvNb
return pcaA
}
func (pcaA *pcaAnalyzer) isFit(seed *seedT, orgHash uint64) bool {
var ok, newBranch, pcaAccepted bool
var traceBits []byte = seed.getTrace()
// Check if it has new branch.
pcaA.mtx.RLock()
for i, tr := range traceBits {
if tr > 0 {
if _, ok = pcaA.brMap[i]; !ok {
newBranch = true
break
}
}
}
pcaA.mtx.RUnlock()
// If no new branch, see if the PCA Live Tester "finds" it new.
if !newBranch {
if pcaA.pl != nil {
// pcaLiveAnalyzer handles its own concurrency; so we can return
// directly after call no matter the result (accepted or not).
pcaAccepted = pcaA.pl.newObs(traceBits, orgHash)
// !!! For test !!!!
// If want to make the PCA-Learner a "static" observer".
//pcaAccepted = false
}
if !pcaAccepted && !newBranch {
return false
}
}
newBranch = false
pcaA.mtx.Lock()
// Unfortunately, if we want everyone to be able to read before we get
// the write lock, we have to do this scoring twice.
for i, tr := range traceBits {
if tr > 0 {
if _, ok = pcaA.brMap[i]; !ok {
newBranch = true
pcaA.brMap[i] = struct{}{}
pcaA.brList = append(pcaA.brList, i)
}
}
}
pcaA.mtx.Unlock()
// Accepted because new branch and not because because PCA, so need to tell
// the PCA-Learner this input will be added to the seed pool.
if !pcaAccepted && pcaA.pl != nil {
if newBranch {
pcaA.pl.addNewSeed(traceBits)
}
}
if newBranch && pcaA.pl == nil {
// Just need to keep track of seeds until PCA initialization.
pcaA.seedsMtx.Lock()
*pcaA.seedPts = append(*pcaA.seedPts, seed)
pcaA.seedsMtx.Unlock()
}
res := newBranch || pcaAccepted
if res {
pcaA.newPCAFeatures(seed)
}
return res
}
// pcaAnalyzer complies with the distCalcGetter interface.
func (pcaA *pcaAnalyzer) getDistCalc() distCalculator {
if len(pcaA.brList) > 0 {
return distCalculator(pcaA.brList)
}
// Not initilized yet
return makeDefaultDistCalculator().getDistCalc()
}
// *****************************************************************************
// ************************** Interface Compliance *****************************
func (pcaA *pcaAnalyzer) roundEnd() {
pcaA.updateFeats()
pcaA.mayStartPCA()
}
func (pcaA *pcaAnalyzer) epilogue(progName string) {
if pcaA.pl != nil {
pcaA.pl.epilogue(progName)
}
}
func (pcaA *pcaAnalyzer) String() (str string) {
pcaA.mtx.RLock()
edgeN := float64(len(pcaA.brList))
str = fmt.Sprintf("#edges: %.3v (%.02f%%)", edgeN, 100*edgeN/mapSize)
//
if pcaA.pl != nil {
str += fmt.Sprintf(" - %s", pcaA.pl.String())
}
pcaA.mtx.RUnlock()
//
return str
}
// *****************************************************************************
// ************************* PCA Learner Initiatlization ***********************
func (pcaA *pcaAnalyzer) mayStartPCA() {
if pcaA.pl != nil {
return
}
seedAcptRate := pcaA.updateSeedNHistory()
if !noPCAstart && seedAcptRate < .1 && len(*pcaA.seedPts) > pcaSubDim {
ok, pl := newPCALearner(*pcaA.seedPts, pcaA.pcaCullCh, pcaA.frkSrvNb)
if ok {
pcaA.pl = pl
*pcaA.seedPts = nil // Don't need to keep track of seeds anymore.
}
}
dbgPr("Len of receiver seed list: %d.\n", len(*pcaA.seedPts))
}
func (pcaA *pcaAnalyzer) updateSeedNHistory() (rate float64) {
const histLenMax = 500
n := len(*pcaA.seedPts)
for i := 0; i < pcaA.frkSrvNb; i++ {
pcaA.acceptedSeedHist = append(pcaA.acceptedSeedHist, n)
}
histLen := len(pcaA.acceptedSeedHist)
if histLen > histLenMax { // After init, always shorten the slice.
pcaA.acceptedSeedHist = pcaA.acceptedSeedHist[histLen-histLenMax:]
histLen = histLenMax
//
// Randomly reset the slice so it doesn't get too long. All truncunted
// part is kept in memory otherwise.
if rand.Intn(1000) == 0 {
tmp := make([]int, len(pcaA.acceptedSeedHist))
copy(tmp, pcaA.acceptedSeedHist)
pcaA.acceptedSeedHist = tmp
}
}
rate = 1
if histLen < histLenMax {
return rate
}
rate = float64(pcaA.acceptedSeedHist[histLen-1] - pcaA.acceptedSeedHist[0])
rate /= float64(histLen)
if n > 0 {
rate /= float64(n) / histLenMax
}
return rate
}
// *****************************************************************************
// ******************** Communication for seed selection ***********************
// Features of a seed from the PCA that are relevant for seed selection.
type pcaFeatures struct {
set bool
minD float64
complement bool
extra float64
d2 float64
}
func (pcaA *pcaAnalyzer) newPCAFeatures(seedPt *seedT) {
if selType != pcaWmoSel {
return
}
feat := new(pcaFeatures)
seedPt.pcaFeat = feat
pcaA.featMtx.Lock()
pcaA.feats[seedPt.hash] = feat
pcaA.featMtx.Unlock()
}
func (pcaA *pcaAnalyzer) cull(toRem []uint64) { pcaA.cullFeats(toRem) }
func (pcaA *pcaAnalyzer) cullFeats(toRem []uint64) {
if selType != pcaWmoSel {
return
}
pcaA.featMtx.Lock()
for _, hash := range toRem {
delete(pcaA.feats, hash)
}
pcaA.featMtx.Unlock()
}
func (pcaA *pcaAnalyzer) updateFeats() {
if selType != pcaWmoSel {
return
}
pl := pcaA.pl
if pl == nil || !pl.converged {
return
}
safeSqrt := func(a float64) float64 {
if a < almostZero {
return 0
}
return math.Sqrt(a)
}
pcaA.featMtx.Lock()
pl.basisMtx.Lock()
for _, pheno := range pl.phenos {
hash := pheno.hash
feat, ok := pcaA.feats[hash]
if !ok {
log.Printf("Seed in phenotypes but not in feature map (hash=0x%x).\n", hash)
feat = new(pcaFeatures)
pcaA.feats[hash] = feat
}
if pheno.extra < 0 {
pheno.extra = pheno.sqNorm - doSqSum(pheno.proj)
if pheno.extra < 0 {
pheno.extra = 0
}
}
//
*feat = pcaFeatures{
set: true,
minD: pheno.minD,
complement: false,
extra: safeSqrt(pheno.extra),
d2: safeSqrt(calcD2(pheno.proj, pl.vars)),
}
}
//
for _, pheno := range pl.covComp {
hash := pheno.hash
feat, ok := pcaA.feats[hash]
if !ok {
log.Printf("Seed in complement but not in feature map (hash=0x%x).\n", hash)
feat = new(pcaFeatures)
pcaA.feats[hash] = feat
}
if pheno.extra < 0 {
pheno.extra = pheno.sqNorm - doSqSum(pheno.proj)
if pheno.extra < 0 {
pheno.extra = 0
}
}
//
*feat = pcaFeatures{
set: true,
minD: 0,
complement: true,
extra: safeSqrt(pheno.extra),
d2: safeSqrt(calcD2(pheno.proj, pl.vars)),
}
}
pl.basisMtx.Unlock()
pcaA.featMtx.Unlock()
}
func calcD2(proj []float64, vars []float64) (d2 float64) {
if len(proj) > len(vars) {
proj = proj[:len(vars)]
}
if len(proj) <= d2Q {
return d2
}
//
for i, p := range proj {
d2 += p * p / vars[i]
}
return d2
}