-
Notifications
You must be signed in to change notification settings - Fork 0
/
forecast.go
133 lines (107 loc) · 2.59 KB
/
forecast.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
package main
import "sort"
type ImpressionForecastor struct {
pages map[int64]PageMeta
featureToPages map[Feature]*PagePostingList
}
func (i *ImpressionForecastor) forecast(ad Ad) int {
impressions := 0
searcher := &Searcher{
pages: i.pages,
feature2Pages: i.featureToPages,
currentPage: 0,
ad: ad,
}
searcher.initCursors()
pageID := searcher.nextCandidate()
for pageID < lastPID {
p := i.pages[pageID]
score := i.score(ad, p)
if score > p.minScore {
impressions += p.impressions
}
pageID = searcher.nextCandidate()
}
return impressions
}
func (i *ImpressionForecastor) score(ad Ad, page PageMeta) float32 {
pageWeight := make(map[Feature]float32, len(page.features))
for i, f := range page.features {
pageWeight[f] = page.weights[i]
}
sim := float32(0)
for _, f := range ad.Features() {
sim += pageWeight[f] * ad.Weight(f)
}
return sim * ad.Bid()
}
type Searcher struct {
pages map[int64]PageMeta
feature2Pages map[Feature]*PagePostingList
currentPage int64
ad Ad
features []*FeatureCursor
}
func (s *Searcher) initCursors() {
s.currentPage = 0
ad := s.ad
features := ad.Features()
cursors := make([]*FeatureCursor, len(features))
for i, f := range features {
cursors[i] = &FeatureCursor{
feature: f,
posting: s.feature2Pages[f],
current: 0,
}
}
s.features = cursors
}
func (s *Searcher) nextCandidate() int64 {
for {
s.sortFeatures(s.features)
pivot := s.findPivotFeature(s.features)
if pivot == -1 {
return lastPID
}
pivotPID := s.features[pivot].CurrentPage().PID
if pivotPID == lastPID {
return lastPID
}
if pivotPID == s.currentPage {
f := s.pickFeature(s.features[0 : pivot+1])
s.features[f].Beyond(pivotPID + 1)
} else {
if s.features[0].CurrentPage().PID == pivotPID {
s.currentPage = pivotPID
return s.currentPage
} else {
f := s.pickFeature(s.features[0 : pivot+1])
s.features[f].Beyond(pivotPID)
}
}
}
}
func (s *Searcher) sortFeatures(features []*FeatureCursor) {
sort.Slice(features, func(i, j int) bool {
return features[i].CurrentPage().PID < features[j].CurrentPage().PID
})
}
func (s *Searcher) findPivotFeature(cursors []*FeatureCursor) int {
n := len(cursors)
ub := float32(0)
ad := s.ad
minScoreP := s.pages[s.currentPage].minScore
threshold := minScoreP / ad.Bid()
for i := 0; i < n; i++ {
f := cursors[i].feature
maxWeight := s.feature2Pages[f].MaxWeight
ub += ad.Weight(f) * maxWeight
if ub >= threshold {
return i
}
}
return -1
}
func (s *Searcher) pickFeature(cursors []*FeatureCursor) int {
return 0
}