-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchromosome_binary.go
56 lines (47 loc) · 1.35 KB
/
chromosome_binary.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
package genetic_algorithm
import (
"bytes"
"fmt"
)
type BinaryGenes []bool
func (g BinaryGenes) Len() int { return len(g) }
func (g BinaryGenes) Swap(i, j int) { g[i], g[j] = g[j], g[i] }
func (g BinaryGenes) Get(i int) interface{} { return g[i] }
func (g BinaryGenes) Set(i int, val interface{}) { g[i] = val.(bool) }
func (g BinaryGenes) Copy(genes GenesInterface, from1, from2, to2 int) int {
bgenes, ok := genes.(BinaryGenes)
if !ok {
panic("Unexpected genes. Expected BinaryGenes")
}
return copy(g[from1:], bgenes[from2:to2])
}
type BinaryChromosome struct {
*ChromosomeBase
genes BinaryGenes
}
func NewBinaryChromosome(genes BinaryGenes) *BinaryChromosome {
chrom := new(BinaryChromosome)
chrom.ChromosomeBase = NewChromosomeBase()
chrom.genes = genes
return chrom
}
func NewEmptyBinaryChromosome(genesLen int) ChromosomeInterface {
return NewBinaryChromosome(make(BinaryGenes, genesLen))
}
func (chrom *BinaryChromosome) Genes() GenesInterface {
return chrom.genes
}
func (chrom *BinaryChromosome) BinaryGenes() BinaryGenes {
return chrom.genes
}
func (chrom *BinaryChromosome) String() string {
var buffer bytes.Buffer
for _, b := range chrom.genes {
if b {
buffer.WriteString("1")
} else {
buffer.WriteString("0")
}
}
return fmt.Sprintf("BC genes:[%v], cost: %f", buffer.String(), chrom.costVal)
}