-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrossover_multi_point.go
122 lines (99 loc) · 3.24 KB
/
crossover_multi_point.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
package genetic_algorithm
import (
log "github.com/cihub/seelog"
"math/rand"
"sort"
)
type MultiPointCrossover struct {
crossPointsCount int
chromConstr EmptyChromosomeConstructor
canProduceCopiesOfParents bool
}
func NewMultiPointCrossover(chromConstr EmptyChromosomeConstructor, crossPointsCount int) *MultiPointCrossover {
if crossPointsCount <= 0 {
panic("crossPointsCount must be positive")
}
crossover := new(MultiPointCrossover)
crossover.chromConstr = chromConstr
crossover.crossPointsCount = crossPointsCount
return crossover
}
func NewOnePointCrossover(chromConstr EmptyChromosomeConstructor) *MultiPointCrossover {
return NewMultiPointCrossover(chromConstr, 1)
}
func NewTwoPointCrossover(chromConstr EmptyChromosomeConstructor) *MultiPointCrossover {
return NewMultiPointCrossover(chromConstr, 2)
}
func (crossover *MultiPointCrossover) ParentsCount() int {
return 2
}
func (crossover *MultiPointCrossover) CanProduceCopiesOfParents(val bool) *MultiPointCrossover {
crossover.canProduceCopiesOfParents = val
return crossover
}
func (crossover *MultiPointCrossover) Crossover(parents Chromosomes) Chromosomes {
if len(parents) != crossover.ParentsCount() {
panic("Incorrect parents count")
}
p1 := parents[0]
p2 := parents[1]
genesLen := p1.Genes().Len()
if genesLen != p2.Genes().Len() {
panic("Crossover do not support different chromosome size")
}
crossover.checkGenesLen(genesLen)
crossPointsList := crossover.chooseCrossPoints(genesLen)
sort.Sort(sort.IntSlice(crossPointsList))
log.Tracef("Cross on %v\n", crossPointsList)
c1, c2 := crossover.crossover(p1, p2, crossPointsList)
return Chromosomes{c1, c2}
}
func (crossover *MultiPointCrossover) checkGenesLen(genesLen int) {
possibleCrossPoints := genesLen + 1
if !crossover.canProduceCopiesOfParents && crossover.crossPointsCount <= 2 {
if crossover.crossPointsCount == 1 {
possibleCrossPoints -= 2
} else {
possibleCrossPoints--
}
}
if possibleCrossPoints < crossover.crossPointsCount {
panic("Chromosome too short")
}
}
func (crossover *MultiPointCrossover) chooseCrossPoints(genesLen int) []int {
if crossover.crossPointsCount == 1 {
if crossover.canProduceCopiesOfParents {
return []int{rand.Intn(genesLen + 1)}
} else {
return []int{rand.Intn(genesLen-1) + 1}
}
} else if crossover.crossPointsCount == 2 {
p1, p2 := chooseTwoPointCrossSection(genesLen, crossover.canProduceCopiesOfParents)
return []int{p1, p2}
}
return chooseDifferentRandomNumbers(crossover.crossPointsCount, genesLen+1)
}
func (crossover *MultiPointCrossover) crossover(p1, p2 ChromosomeInterface, crossPoints []int) (c1, c2 ChromosomeInterface) {
p1genes := p1.Genes()
p2genes := p2.Genes()
genesLen := p1.Genes().Len()
c1 = crossover.chromConstr(genesLen)
c1genes := c1.Genes()
c2 = crossover.chromConstr(genesLen)
c2genes := c2.Genes()
crossPoint := 0
for i := 0; i < len(crossPoints); i++ {
crossPoint = crossPoints[i]
start := 0
if i > 0 {
start = crossPoints[i-1]
}
c1genes.Copy(p1genes, start, start, crossPoint)
c2genes.Copy(p2genes, start, start, crossPoint)
c1genes, c2genes = c2genes, c1genes
}
c1genes.Copy(p1genes, crossPoint, crossPoint, genesLen)
c2genes.Copy(p2genes, crossPoint, crossPoint, genesLen)
return
}