forked from habeanf/yap
-
Notifications
You must be signed in to change notification settings - Fork 20
/
framework.go
329 lines (295 loc) · 8.46 KB
/
framework.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
package search
import (
"fmt"
"log"
"sync"
"yap/alg/transition"
"yap/util"
)
const (
MAX_TRANSITIONS = 800
)
var AllOut bool = true
type Agenda interface {
AddCandidates([]Candidate, Candidate, int) (Candidate, int)
Contains(Candidate) bool
Len() int
Clear()
}
type Problem interface{}
type Candidate interface {
Copy() Candidate
Equal(Candidate) bool
Score() float64
Len() int
Terminal() bool
}
type Aligned interface {
Alignment() int
}
type Candidates interface {
Get(int) Candidate
Len() int
}
type Interface interface {
StartItem(p Problem) []Candidate
Clear(Agenda) Agenda
Insert(cs chan Candidate, a Agenda) []Candidate //Agenda
Expand(c Candidate, p Problem, candidateNum int) chan Candidate
Top(a Agenda) Candidate
Best(a Agenda) Candidate
GoalTest(p Problem, c Candidate, rounds int) bool
TopB(a Agenda, B int) ([]Candidate, bool)
Concurrent() bool
SetEarlyUpdate(int)
Name() string
Aligned() bool
}
type IdleFunc func(c Candidate, candidateNum int) Candidate
type Idle interface {
Idle(c Candidate, candidateNum int) Candidate
}
func Search(b Interface, problem Problem, B int) Candidate {
candidate, _ := search(b, problem, B, 1, false, nil)
return candidate
}
func SearchEarlyUpdate(b Interface, problem Problem, B int, goldSequence Candidates) (Candidate, Candidate) {
return search(b, problem, B, 1, true, goldSequence)
}
func search(b Interface, problem Problem, B, topK int, earlyUpdate bool, goldSequence Candidates) (Candidate, Candidate) {
var (
goldValue Candidate
best Candidate
agenda Agenda
// for early update
i int
goldIndex int
goldExists bool
bestBeamCandidate Candidate
resultsReady chan chan int
// for alignment
minAgendaAlignment int
minCandidateAlignment int
allTerminal bool
idleCandidates bool = false
idleFunc IdleFunc
idleGoldTransitions int
)
tempAgendas := make([][]Candidate, 0, B)
if idleCandidates {
idlingInterface, idles := b.(Idle)
if idles {
idleFunc = idlingInterface.Idle
} else {
panic("Can't idle when beam does not have idling function")
}
}
// candidates <- {STARTITEM(problem)}
candidates := b.StartItem(problem)
bestBeamCandidate = candidates[0]
// verify alignment support
if _, aligned := bestBeamCandidate.(Aligned); b.Aligned() {
if !aligned {
panic("Beam is aligned but candidate does not support alignment")
} else {
minAgendaAlignment = bestBeamCandidate.(Aligned).Alignment()
}
}
// agenda <- CLEAR(agenda)
agenda = b.Clear(agenda)
if earlyUpdate {
goldValue = goldSequence.Get(0)
goldIndex = 0
}
// loop do
for {
// log.Println()
// log.Println()
// log.Println("At gold sequence", i)
// early update
if earlyUpdate {
goldExists, bestBeamCandidate = false, nil
if AllOut {
// log.Println("Gold:", goldValue.(*ScoredConfiguration).C.GetSequence())
log.Println("Gold:", goldValue)
}
}
best = nil
tempAgendas = tempAgendas[0:0]
resultsReady = make(chan chan int, B)
var wg sync.WaitGroup
if len(candidates) > cap(tempAgendas) {
panic(fmt.Sprintf("Should not have more candidates than the capacity of the tempAgenda: (%d,%d)\n", len(candidates), cap(tempAgendas)))
}
// for each candidate in candidates
go func() {
if b.Aligned() {
minCandidateAlignment = candidates[0].(Aligned).Alignment()
if earlyUpdate && candidates[0].Equal(goldValue) {
// log.Println("Candidate 1 Gold true")
goldExists = true
} else {
// log.Println("Candidate 1 Gold false")
}
for _, candidate := range candidates[1:] {
if candAlign := candidate.(Aligned).Alignment(); candAlign < minCandidateAlignment {
minCandidateAlignment = candAlign
}
if earlyUpdate && candidate.Equal(goldValue) {
// log.Println("Candidate", i+2, "Gold true")
goldExists = true
} else {
// log.Println("Candidate", i+2, "Gold false")
}
}
minAgendaAlignment = -1
}
for i, candidate := range candidates {
tempAgendas = append(tempAgendas, nil)
readyChan := make(chan int, 1)
resultsReady <- readyChan
if b.Aligned() && candidate.(Aligned).Alignment() > minCandidateAlignment {
if AllOut {
log.Println("\tIdling candidate", i+1, "due to misalignment", candidate.(Aligned).Alignment(), minCandidateAlignment)
// log.Println("Idle candidate", candidate.(*ScoredConfiguration).C.GetSequence())
log.Println("\tCandidate", candidate)
}
if idleCandidates {
tempAgendas[i] = []Candidate{idleFunc(candidate, i)}
} else {
tempAgendas[i] = []Candidate{candidate}
}
if !b.Concurrent() {
best, minAgendaAlignment = agenda.AddCandidates(tempAgendas[i], best, minAgendaAlignment)
}
readyChan <- i
close(readyChan)
continue
}
wg.Add(1)
go func(ag Agenda, cand Candidate, j int, doneChan chan int) {
defer wg.Done()
// agenda <- INSERT(EXPAND(candidate,problem),agenda)
// tempAgendas[i] = b.Insert(b.Expand(candidate, problem, i), agenda)
tempAgendas[j] = b.Insert(b.Expand(cand, problem, j), ag)
doneChan <- j
close(doneChan)
// readyChan <- i
// close(readyChan)
if !b.Concurrent() {
best, minAgendaAlignment = agenda.AddCandidates(tempAgendas[j], best, minAgendaAlignment)
}
}(agenda, candidate, i, readyChan)
if !b.Concurrent() {
wg.Wait()
// best = agenda.AddCandidates(tempAgendas[i], best)
}
if earlyUpdate {
if bestBeamCandidate == nil || candidate.Score() > bestBeamCandidate.Score() {
// bestScore = candidate.Score()
bestBeamCandidate = candidate
// log.Println("Candidate is best")
} else {
// log.Println("Candidate is not best")
}
if candidate.Equal(goldValue) {
goldExists = true
// log.Println("Candidate is gold")
}
}
// *** <POSSIBLY REDUNDANT>
// if !b.Concurrent() {
// wg.Wait()
// }
// *** </POSSIBLY REDUNDANT>
}
close(resultsReady)
}()
// wg.Wait()
for readyChan := range resultsReady {
if b.Concurrent() {
for tempAgendaId := range readyChan {
best, minAgendaAlignment = agenda.AddCandidates(tempAgendas[tempAgendaId], best, minAgendaAlignment)
}
} else {
for _ = range readyChan {
}
}
}
// for _, tempCandidates := range tempAgendas {
// agenda.AddCandidates(tempCandidates)
// }
i++
// early update
if earlyUpdate {
if !goldExists || goldIndex+1 >= (goldSequence.Len()+idleGoldTransitions) {
if AllOut {
log.Println("EARLY UPDATE")
}
if bestBeamCandidate == nil {
panic("Best Beam Candidate is nil")
}
b.SetEarlyUpdate(util.Min(goldIndex, bestBeamCandidate.Len()-1))
best = bestBeamCandidate
break
} else {
if b.Aligned() {
if AllOut {
log.Println(" Early Update continues, testing alignment")
log.Println("\tMin Alignment:", minAgendaAlignment)
log.Println("\tNext Gold Alignment:", goldSequence.Get(goldIndex).(Aligned).Alignment())
}
if goldSequence.Get(goldIndex).(Aligned).Alignment() == minAgendaAlignment {
goldIndex++
nextValue := goldSequence.Get(goldIndex)
nextValue.(*ScoredConfiguration).C.SetPrevious(goldValue.(*ScoredConfiguration).C)
goldValue = nextValue
} else {
if AllOut {
log.Println("\tNot aligned, leaving gold as is (idling)")
}
if idleCandidates {
nextValue := idleFunc(goldValue, 0)
nextValue.(*ScoredConfiguration).C.SetPrevious(goldValue.(*ScoredConfiguration).C)
nextValue.(*ScoredConfiguration).C.SetLastTransition(transition.IDLE)
goldValue = nextValue
idleGoldTransitions++
}
}
} else {
goldIndex++
goldValue = goldSequence.Get(goldIndex)
if goldValue == nil {
panic("Got nil gold value")
}
}
}
// best <- TOP(AGENDA)
best = b.Top(agenda)
}
// candidates <- TOP-B(agenda, B)
candidates, allTerminal = b.TopB(agenda, B)
// if GOALTEST(problem,best)
if ((allTerminal || earlyUpdate) && b.GoalTest(problem, best, i)) || i > MAX_TRANSITIONS {
if AllOut {
log.Println("Next Round", i-1)
if earlyUpdate {
log.Println("Returning:", goldValue.(*ScoredConfiguration).C.GetSequence())
}
}
// return best
break
}
// agenda <- CLEAR(agenda)
agenda = b.Clear(agenda)
if AllOut {
log.Println("Next Round", i-1)
}
}
if !earlyUpdate {
best = b.Best(agenda)
}
best = best.Copy()
agenda = b.Clear(agenda)
return best, goldValue
}