-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptimizer_base.go
171 lines (145 loc) · 4.59 KB
/
optimizer_base.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
package genetic_algorithm
import (
log "github.com/cihub/seelog"
"sort"
)
type OptimizerBase struct {
OptimizerBaseVirtualMInterface
initializer InitializerInterface
weeder WeederInterface
selector SelectorInterface
crossover CrossoverInterface
mutator MutatorInterface
costFunction CostFunction
statisticsConstructor StatisticsConstructor
statisticsOptions StatisticsOptionsInterface
stopCriterion StopCriterionInterface
popSize int
chromSize int
population Chromosomes
statistics StatisticsInterface
}
// MutatorBase's virtual methods
type OptimizerBaseVirtualMInterface interface {
optimizeInner()
check()
}
func NewOptimizerBase(virtual OptimizerBaseVirtualMInterface) *OptimizerBase {
optimizer := new(OptimizerBase)
optimizer.OptimizerBaseVirtualMInterface = virtual
optimizer.statisticsConstructor = NewStatisticsDefault
optimizer.statisticsOptions = NewStatisticsDefaultOptions()
return optimizer
}
func (optimizer *OptimizerBase) Initializer(initializer InitializerInterface) *OptimizerBase {
optimizer.initializer = initializer
return optimizer
}
func (optimizer *OptimizerBase) Selector(selector SelectorInterface) *OptimizerBase {
optimizer.selector = selector
return optimizer
}
func (optimizer *OptimizerBase) Crossover(crossover CrossoverInterface) *OptimizerBase {
optimizer.crossover = crossover
return optimizer
}
func (optimizer *OptimizerBase) Mutator(mutator MutatorInterface) *OptimizerBase {
optimizer.mutator = mutator
return optimizer
}
func (optimizer *OptimizerBase) CostFunction(cost CostFunction) *OptimizerBase {
optimizer.costFunction = cost
return optimizer
}
func (optimizer *OptimizerBase) StopCriterion(stopCriterion StopCriterionInterface) *OptimizerBase {
optimizer.stopCriterion = stopCriterion
return optimizer
}
func (optimizer *OptimizerBase) StatisticsConstructor(constr StatisticsConstructor) *OptimizerBase {
optimizer.statisticsConstructor = constr
return optimizer
}
func (optimizer *OptimizerBase) StatisticsOptions(statisticsOptions StatisticsOptionsInterface) *OptimizerBase {
optimizer.statisticsOptions = statisticsOptions
return optimizer
}
func (optimizer *OptimizerBase) PopSize(popSize int) *OptimizerBase {
optimizer.popSize = popSize
return optimizer
}
func (optimizer *OptimizerBase) ChromSize(chromSize int) *OptimizerBase {
optimizer.chromSize = chromSize
return optimizer
}
func (optimizer *OptimizerBase) check() {
if optimizer.initializer == nil {
panic("Initializer must be set")
}
if optimizer.selector == nil {
panic("Selector must be set")
}
if optimizer.crossover == nil {
panic("Crossover must be set")
}
if optimizer.mutator == nil {
panic("Mutator must be set")
}
if optimizer.costFunction == nil {
panic("CostFunction must be set")
}
if optimizer.stopCriterion == nil {
panic("StopCriterion must be set")
}
if optimizer.statisticsConstructor == nil {
panic("StatisticsConstructor must be set")
}
if optimizer.statisticsOptions == nil {
panic("StatisticsOptions must be set")
}
if optimizer.popSize <= 0 {
panic("PopSize must be positive value")
}
if optimizer.chromSize <= 0 {
panic("ChromSize must be positive value")
}
optimizer.OptimizerBaseVirtualMInterface.check()
}
func (optimizer *OptimizerBase) Optimize() (ChromosomeInterface, StatisticsDataInterface) {
optimizer.check()
optimizer.stopCriterion.Setup(optimizer.statisticsOptions)
optimizer.statistics = optimizer.statisticsConstructor(optimizer.statisticsOptions)
optimizer.statistics.Start()
optimizer.initPopulation()
iter := 0
for {
log.Infof("GENERATION %d", iter)
optimizer.sort()
optimizer.statistics.OnGeneration(optimizer.population)
if optimizer.stopCriterion.ShouldStop(optimizer.statistics.Data()) {
break
}
optimizer.OptimizerBaseVirtualMInterface.optimizeInner()
iter++
}
optimizer.statistics.End()
return optimizer.population[0], optimizer.statistics.Data()
}
func (optimizer *OptimizerBase) initPopulation() {
optimizer.statistics.Start("init")
defer optimizer.statistics.End()
optimizer.population = optimizer.initializer.Init(optimizer.popSize, optimizer.chromSize)
if len(optimizer.population) == 0 {
panic("Init population is empty")
}
}
func (optimizer *OptimizerBase) sort() {
optimizer.statistics.Start("cost")
defer optimizer.statistics.End()
optimizer.population.SetCost(optimizer.costFunction)
sort.Sort(optimizer.population)
log.Infof("Best: %v", optimizer.population[0])
log.Debugf("Population:\n%v\n", optimizer.population)
}
func (optimizer *OptimizerBase) SetupStatisticsOptions() StatisticsOptionsInterface {
return optimizer.statisticsOptions
}