-
Notifications
You must be signed in to change notification settings - Fork 40
/
genome.go
54 lines (47 loc) · 1.18 KB
/
genome.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
/*
Copyright 2009 Thomas Jager <[email protected]> All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
go-galib genome
*/
package ga
// Genome interface, Not final.
type GAGenome interface {
//Randomize.Genens
Randomize()
//Copy a genome;
Copy() GAGenome
//Calculate score
Score() float64
//Reset cached score
Reset()
//Crossover for this genome
Crossover(bi GAGenome, p1, p2 int) (ca GAGenome, cb GAGenome)
//Switch for the genome
Switch(x, y int)
//Splice two genomes;
Splice(bi GAGenome, from, to, length int)
//Check if genome is valid
Valid() bool
String() string
Len() int
}
type GAGenomes []GAGenome
func (g GAGenomes) Len() int { return len(g) }
func (g GAGenomes) Less(i, j int) bool { return g[i].Score() < g[j].Score() }
func (g GAGenomes) Swap(i, j int) { g[i], g[j] = g[j], g[i] }
func AppendGenomes(slice, data GAGenomes) GAGenomes {
l := len(slice)
if l+len(data) > cap(slice) {
newSlice := make(GAGenomes, (l+len(data))*2)
for i, c := range slice {
newSlice[i] = c
}
slice = newSlice
}
slice = slice[0 : l+len(data)]
for i, c := range data {
slice[l+i] = c
}
return slice
}