-
Notifications
You must be signed in to change notification settings - Fork 1
/
statistics_default.go
348 lines (284 loc) · 8.63 KB
/
statistics_default.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
336
337
338
339
340
341
342
343
344
345
346
347
348
package genetic_algorithm
import (
"bytes"
"fmt"
log "github.com/cihub/seelog"
"math"
"time"
)
type StatisticsDataDefault interface {
Generations() int
Duration() time.Duration
Durations() *HierarchicalDuration
MinCost() float64
MinCosts() []float64
GenerationsWithoutImprovements() int
MinCostsVar() float64
MeanCost() float64
MeanCosts() []float64
WorstCost() float64
WorstCosts() []float64
}
// Default realization of StatisticsInterface
type StatisticsDefault struct {
started bool
durationTracker *durationTracker
durationTrackerStack [][]string
hierarchy *HierarchicalDuration
generations int
minCost float64
gensWoImprv int
prevDifferentMinCost float64
minCosts []float64
minCostsVar float64
meanCost float64
meanCosts []float64
worstCost float64
worstCosts []float64
options *StatisticsDefaultOptions
}
func NewStatisticsDefault(options StatisticsOptionsInterface) StatisticsInterface {
opts, ok := options.(*StatisticsDefaultOptions)
if !ok {
panic("Expects instance of StatisticsOptionsInterface")
}
statistics := new(StatisticsDefault)
statistics.generations = -1
statistics.options = opts
return statistics
}
func (statistics *StatisticsDefault) Start(keys ...string) {
if !statistics.options.trackDurations {
return
}
if statistics.durationTracker == nil {
statistics.durationTracker = newDurationTracker()
statistics.durationTrackerStack = make([][]string, 0)
}
statistics.started = true
statistics.hierarchy = nil
if len(keys) != 0 {
statistics.durationTrackerStack = append(statistics.durationTrackerStack, keys)
}
tracker := statistics.durationTracker
for _, key := range keys {
tracker = tracker.child(key)
}
tracker.start()
}
func (statistics *StatisticsDefault) End() {
if !statistics.options.trackDurations {
return
}
if !statistics.started {
return
}
stackLen := len(statistics.durationTrackerStack)
if stackLen == 0 {
statistics.started = false
statistics.durationTracker.end()
return
}
keys := statistics.durationTrackerStack[stackLen-1]
statistics.durationTrackerStack = statistics.durationTrackerStack[:stackLen-1]
tracker := statistics.durationTracker
for _, key := range keys {
tracker = tracker.child(key)
}
tracker.end()
}
// Expects sorted population
func (statistics *StatisticsDefault) OnGeneration(population Chromosomes) {
statistics.generations++
if len(population) == 0 {
panic("Population is empty")
}
statistics.minCost = population[0].Cost()
log.Tracef("MinCost %v", statistics.minCost)
if statistics.options.trackMinCosts {
statistics.minCosts = append(statistics.minCosts, population[0].Cost())
log.Tracef("MinCosts %v", statistics.minCosts)
}
if statistics.options.trackGensWoImprv {
if statistics.generations == 0 || statistics.prevDifferentMinCost != statistics.minCost {
statistics.prevDifferentMinCost = statistics.minCost
statistics.gensWoImprv = 0
} else {
statistics.gensWoImprv++
}
log.Tracef("MinCostAge %v", statistics.gensWoImprv)
}
if statistics.options.trackMinCostsVar {
if statistics.generations == 0 || statistics.generations == statistics.gensWoImprv {
statistics.minCostsVar = math.NaN()
} else {
statistics.minCostsVar = pvarianceFloat64(statistics.minCosts)
}
log.Tracef("MinCostsVar %v", statistics.minCostsVar)
}
if statistics.options.trackMeanCost {
statistics.meanCost = population.MeanCost()
log.Tracef("MeanCost %v", statistics.meanCost)
}
if statistics.options.trackMeanCosts {
var mean float64
if statistics.options.trackMeanCost {
mean = statistics.meanCost
} else {
mean = population.MeanCost()
}
statistics.meanCosts = append(statistics.meanCosts, mean)
log.Tracef("MeanCosts %v", statistics.meanCosts)
}
if statistics.options.trackWorstCost {
statistics.worstCost = population[len(population)-1].Cost()
log.Tracef("WorstCost %v", statistics.worstCost)
}
if statistics.options.trackWorstCosts {
statistics.worstCosts = append(statistics.worstCosts, population[len(population)-1].Cost())
log.Tracef("WorstCosts %v", statistics.worstCosts)
}
}
// Number of generations
func (statistics *StatisticsDefault) Generations() int {
return statistics.generations
}
// Returns duration of optimization.
// If optimization are in progress then returns time elapsed from start.
func (statistics *StatisticsDefault) Duration() time.Duration {
if !statistics.options.trackDurations {
return time.Duration(0)
}
if !statistics.started {
return statistics.durationTracker.elapsed[0]
}
return time.Since(statistics.durationTracker.startTime)
}
// Returns entire tracked hierarchy of duration
func (statistics *StatisticsDefault) Durations() *HierarchicalDuration {
if statistics.hierarchy == nil {
statistics.hierarchy = statistics.durationTracker.toHierarchy()
}
return statistics.hierarchy
}
// Min cost for last iteration
func (statistics *StatisticsDefault) MinCost() float64 {
return statistics.minCost
}
// Min cost for each iteration
// Len would be `Generations() + 1` because of initial value
func (statistics *StatisticsDefault) MinCosts() []float64 {
return statistics.minCosts
}
// Number of generations during which the min cost remains unchanged
func (statistics *StatisticsDefault) GenerationsWithoutImprovements() int {
return statistics.gensWoImprv
}
// Variance of min costs
// Variance equals NaN until two different values of MinCost are obtained
func (statistics *StatisticsDefault) MinCostsVar() float64 {
return statistics.minCostsVar
}
// Mean cost of last iteration
func (statistics *StatisticsDefault) MeanCost() float64 {
return statistics.meanCost
}
// Mean cost of each iteration
// Len would be `Iterations() + 1` because of initial value
func (statistics *StatisticsDefault) MeanCosts() []float64 {
return statistics.meanCosts
}
// Worst cost for last iteration
func (statistics *StatisticsDefault) WorstCost() float64 {
return statistics.worstCost
}
// Worst cost for each iteration
// Len would be `Iterations() + 1` because of initial value
func (statistics *StatisticsDefault) WorstCosts() []float64 {
return statistics.worstCosts
}
func (statistics *StatisticsDefault) Data() StatisticsDataInterface {
return statistics
}
type durationTracker struct {
startTime time.Time
elapsed []time.Duration
children map[string]*durationTracker
}
func newDurationTracker() *durationTracker {
tracker := new(durationTracker)
return tracker
}
func (tracker *durationTracker) start() {
tracker.startTime = time.Now()
}
func (tracker *durationTracker) end() {
if tracker.elapsed == nil {
tracker.elapsed = make([]time.Duration, 0, 1)
}
tracker.elapsed = append(tracker.elapsed, time.Since(tracker.startTime))
}
func (tracker *durationTracker) child(name string) *durationTracker {
if tracker.children == nil {
tracker.children = make(map[string]*durationTracker, 1)
}
child, ok := tracker.children[name]
if !ok {
child = newDurationTracker()
tracker.children[name] = child
}
return child
}
func (tracker *durationTracker) toHierarchy() *HierarchicalDuration {
return tracker.toNamedHierarchy("total")
}
func (tracker *durationTracker) toNamedHierarchy(name string) *HierarchicalDuration {
hierarchy := newHierarchicalDuration(name)
for _, elapsed := range tracker.elapsed {
hierarchy.add(elapsed)
}
for childName, child := range tracker.children {
hierarchy.addChild(child.toNamedHierarchy(childName))
}
return hierarchy
}
type HierarchicalDuration struct {
Name string
Duration time.Duration
Calls int
Children map[string]*HierarchicalDuration
}
func newHierarchicalDuration(name string) *HierarchicalDuration {
hierarchy := new(HierarchicalDuration)
hierarchy.Name = name
return hierarchy
}
func (hierarchy *HierarchicalDuration) add(duration time.Duration) {
hierarchy.Calls++
hierarchy.Duration += duration
}
func (hierarchy *HierarchicalDuration) addChild(child *HierarchicalDuration) {
if hierarchy.Children == nil {
hierarchy.Children = make(map[string]*HierarchicalDuration, 1)
}
hierarchy.Children[child.Name] = child
}
func (hierarchy *HierarchicalDuration) String() string {
var buffer bytes.Buffer
hierarchy.string(0, &buffer)
return buffer.String()
}
func (hierarchy *HierarchicalDuration) string(indent int, buffer *bytes.Buffer) {
for i := 0; i < indent; i++ {
buffer.WriteString("\t")
}
buffer.WriteString(fmt.Sprintf("%s: %v", hierarchy.Name, hierarchy.Duration))
if hierarchy.Calls != 0 && hierarchy.Calls != 1 {
buffer.WriteString(fmt.Sprintf(" [%d x %v]",
hierarchy.Calls, hierarchy.Duration/time.Duration(hierarchy.Calls)))
}
buffer.WriteString("\n")
for _, child := range hierarchy.Children {
child.string(indent+1, buffer)
}
}