-
Notifications
You must be signed in to change notification settings - Fork 1
/
stop_criterion_default.go
106 lines (89 loc) · 2.71 KB
/
stop_criterion_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
package genetic_algorithm
import (
log "github.com/cihub/seelog"
)
type StopCriterionDefault struct {
maxGenerations int
maxGenerationsCrit bool
minCost float64
minCostCrit bool
maxGensWoImprv int
maxGensWoImprvCrit bool
minMinCostsVar float64
minMinCostsVarCrit bool
}
func NewStopCriterionDefault() *StopCriterionDefault {
criterion := new(StopCriterionDefault)
return criterion
}
// Stop when specified number of generations is reached
func (criterion *StopCriterionDefault) Max_Generations(value int) *StopCriterionDefault {
if value < 0 {
panic("Value can't be negative")
}
criterion.maxGenerationsCrit = true
criterion.maxGenerations = value
return criterion
}
// Stop when min cost less than or equals value
func (criterion *StopCriterionDefault) Min_Cost(value float64) *StopCriterionDefault {
criterion.minCostCrit = true
criterion.minCost = value
return criterion
}
// Stop when min cost wasn't changed for value generations
func (criterion *StopCriterionDefault) Max_GenerationsWithoutImprovements(value int) *StopCriterionDefault {
criterion.maxGensWoImprvCrit = true
criterion.maxGensWoImprv = value
return criterion
}
// Stop when variance of min costs less than or equals value
func (criterion *StopCriterionDefault) Min_MinCostsVar(value float64) *StopCriterionDefault {
criterion.minMinCostsVarCrit = true
criterion.minMinCostsVar = value
return criterion
}
func (criterion *StopCriterionDefault) Setup(opts StatisticsOptionsInterface) {
options, ok := opts.(*StatisticsDefaultOptions)
if !ok {
panic("Method expects StatisticsDefault")
}
if criterion.maxGensWoImprvCrit {
options.TrackGenerationsWithoutImprovements()
}
if criterion.minMinCostsVarCrit {
options.TrackMinCostsVar()
}
}
func (criterion *StopCriterionDefault) ShouldStop(statistics StatisticsDataInterface) bool {
stats, ok := statistics.(StatisticsDataDefault)
if !ok {
panic("Method expects StatisticsDefault")
}
if criterion.maxGenerationsCrit && stats.Generations() >= criterion.maxGenerations {
log.Info("Stop by max generations")
return true
}
if criterion.minCostCrit {
log.Debugf("MinCost %v", stats.MinCost())
if stats.MinCost() <= criterion.minCost {
log.Info("Stop by min cost")
return true
}
}
if criterion.maxGensWoImprvCrit {
log.Debugf("GenerationsWithoutImprovements %v", stats.GenerationsWithoutImprovements())
if stats.GenerationsWithoutImprovements() >= criterion.maxGensWoImprv {
log.Info("Stop by max min cost's age")
return true
}
}
if criterion.minMinCostsVarCrit {
log.Debugf("MinCostsVar %v", stats.MinCostsVar())
if stats.MinCostsVar() >= criterion.minMinCostsVar {
log.Info("Stop by max min costs var")
return true
}
}
return false
}