-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevop.go
363 lines (302 loc) · 6.65 KB
/
evop.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package evop
import "fmt"
type (
gene struct {
key string
weight int
}
// operator should return a genome and information if passed genome was modified.
operator func([]*gene) ([]*gene, bool)
// constraint is kind of a filter which let us eliminate incorrecty encoded trees.
constraint func([]*gene) bool
)
// isEmpty returns true if gene has no information.
// A gene is empty if:
// 1) is nil,
// 2) key is an empty string (no information) -
// it represents a "miss" node in a tree (e.g. we couldn't find a key with certain information).
// "Miss" nodes are also empty genes (leaves), but with weight > 0.
func (g *gene) isEmpty() bool {
return nil == g || "" == g.key
}
func (g *gene) equal(gg *gene) bool {
if g == nil && gg == nil {
return true
}
if g == nil || gg == nil {
return false
}
return *g == *gg
}
// String returns a gene in "printing friendly" format
func (g *gene) String() string {
if nil == g {
return "(nil)"
}
if "" == g.key {
return fmt.Sprintf("(%d)", g.weight)
}
return fmt.Sprintf("(%s/%d)", g.key, g.weight)
}
// clone returns a copy of given genome.
func clone(genome []*gene) []*gene {
if genome == nil {
return nil
}
g := make([]*gene, len(genome))
copy(g, genome)
return g
}
// feasible tests genome against all constraints.
// If the genome passes the constraints, it is feasible -
// the function returns true, otherwise false.
func feasible(genome []*gene, consts ...constraint) bool {
for _, c := range consts {
if !c(genome) {
return false
}
}
return true
}
// eval evaluates a given genome with fitness function: ∑wi(hi + 1), i in [0, n)
func eval(genome []*gene) int {
var fn func(int) int
// fitness function
fn = func(h int) int {
if len(genome) == 0 || genome[0] == nil {
return 0
}
if genome[0].isEmpty() {
// miss node - no information, but it has a weight.
return h * genome[0].weight
}
s := h * genome[0].weight
genome = genome[1:]
s += fn(h + 1)
genome = genome[1:]
s += fn(h + 1)
return s
}
// start from the root (height: 0 + 1)
return fn(1)
}
// (a, b, [c, d, e], f, g) -> (a, b, [e, d, c], f, g)
func inversion(genome []*gene) ([]*gene, bool) {
n := len(genome)
k1, k2 := randIntn(n), randIntn(n)
if k1 == k2 {
return genome, false
}
if k1 > k2 {
k1, k2 = k2, k1
}
return inversionAt(genome, k1, k2), true
}
func inversionAt(genome []*gene, k1, k2 int) []*gene {
for k1 < k2 {
genome[k1], genome[k2] = genome[k2], genome[k1]
k1++
k2--
}
return genome
}
// (a, [b], c, d, e, [f], g) -> (a, [f], c, d, e, [b], g)
func swap(genome []*gene) ([]*gene, bool) {
n := len(genome)
k1, k2 := randNormIntn(n), randNormIntn(n)
if k1 == k2 {
return genome, false
}
return swapAt(genome, k1, k2), true
}
func swapAt(genome []*gene, k1, k2 int) []*gene {
genome[k1], genome[k2] = genome[k2], genome[k1]
return genome
}
// ([a, b, c, d] [e, f], g) -> ([e, f] [a, b, c, d], g)
func crossover(genome []*gene) ([]*gene, bool) {
n := len(genome)
k := randIntn(n - 3)
return crossoverAt(genome, k), true
}
func crossoverAt(genome []*gene, k int) []*gene {
n := len(genome)
head, tail := clone(genome[0:k+1]), clone(genome[k+1:n-2])
copy(genome[0:len(tail)], tail)
copy(genome[len(tail):], head)
return genome
}
func splayLeft(genome []*gene) ([]*gene, bool) {
n := len(genome)
cnt0, cnt1 := 0, 0
for i := 1; i < n; i++ {
if genome[i].isEmpty() {
cnt0++
} else {
cnt1++
}
if cnt0 > cnt1 {
k := i + 1
if k < n && !genome[k].isEmpty() {
return splayLeftAt(genome, k), true
}
}
}
return genome, false
}
func splayLeftAt(genome []*gene, k int) []*gene {
g := genome[k]
copy(genome[1:k+1], genome[0:k])
genome[0] = g
return genome
}
func splayLeftSubTree(genome []*gene) ([]*gene, bool) {
n := len(genome)
return splayLeftSubTreeAt(genome, randIntn(n))
}
func splayLeftSubTreeAt(genome []*gene, root int) ([]*gene, bool) {
n := len(genome)
k1 := subTreeIndex(genome, root)
cnt0, cnt1 := 0, 0
for i := k1; i < n; i++ {
if genome[i].isEmpty() {
cnt0++
} else {
cnt1++
}
if cnt0 > cnt1 {
k2 := i
if k2 < n {
_, b := splayLeft(genome[k1 : k2+1])
return genome, b
}
}
}
return genome, false
}
func splayRight(genome []*gene) ([]*gene, bool) {
n := len(genome)
if n > 1 && genome[1] != nil {
k1 := 1
cnt0, cnt1 := 0, 0
for i := 2; i < n; i++ {
if genome[i].isEmpty() {
cnt0++
} else {
cnt1++
}
if cnt0 > cnt1 {
k2 := i
return splayRightAt(genome, k1, k2), true
}
}
}
return genome, false
}
func splayRightAt(genome []*gene, k1, k2 int) []*gene {
g := genome[0]
copy(genome[0:], genome[k1:k2+1])
genome[k2] = g
return genome
}
func splayRightSubTree(genome []*gene) ([]*gene, bool) {
n := len(genome)
if n > 1 && genome[1] != nil {
return splayRightSubTreeAt(genome, randIntn(n))
}
return genome, false
}
func splayRightSubTreeAt(genome []*gene, root int) ([]*gene, bool) {
n := len(genome)
k1 := subTreeIndex(genome, root)
cnt0, cnt1 := 0, 0
for i := k1; i < n; i++ {
if genome[i].isEmpty() {
cnt0++
} else {
cnt1++
}
if cnt0 > cnt1 {
k2 := i
if k2 < n {
_, b := splayRight(genome[k1 : k2+1])
return genome, b
}
}
}
return genome, false
}
func subTreeIndex(genome []*gene, root int) int {
n := 0
for k := root; k > 0; k-- {
if genome[k].isEmpty() {
n++
continue
}
n--
if n < 2 {
return k
}
}
return 0
}
// isBinTree is a constraint which checks if given genome is a correctly encoded binary tree
func isBinTree(genome []*gene) bool {
n := len(genome) - 1
cnt0, cnt1 := 0, 0
for i := 0; i < n; i++ {
if genome[i].isEmpty() {
cnt0++
} else {
cnt1++
}
if cnt0 > cnt1 {
return false
}
}
return genome[n].isEmpty()
}
// isBST is a constraint which checks if given genome is a correctly encoded binary search tree.
// The function assumes that genome is correctly encoded binary tree,
// and only checks BST constraints (left < root < right).
func isBST(genome []*gene) bool {
var (
keys []string
inorder func() error
)
inorder = func() error {
if len(genome) == 0 || genome[0].isEmpty() {
return nil
}
root := genome[0].key
genome = genome[1:]
if err := inorder(); err != nil {
return err
}
keys = append(keys, root)
if n := len(keys); n > 1 {
if keys[n-2] > keys[n-1] {
return fmt.Errorf("isBST: %s > %s", keys[n-2], keys[n-1])
}
keys = keys[1:]
}
genome = genome[1:]
if err := inorder(); err != nil {
return err
}
return nil
}
err := inorder()
return err == nil
}
func equal(g1 []*gene, g2 []*gene) bool {
if len(g1) != len(g2) {
return false
}
for i, g := range g1 {
if !g.equal(g2[i]) {
return false
}
}
return true
}