-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmutator_invert_and_swap.go
43 lines (36 loc) · 1.31 KB
/
mutator_invert_and_swap.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
package genetic_algorithm
import (
"math/rand"
)
// InvertMutator + SwapMutator
// Mutator selects some part of the chromosome inverts it then swap on gene from it with one gene outside of the interval
type InvertSwapMutator struct {
*MutatorIntervalBase
inverter *InvertMutator
}
// Probability is applied to each chromosome
func NewInvertSwapMutator(probability float64, chromosomeConstructor EmptyChromosomeConstructor) *InvertSwapMutator {
mutator := new(InvertSwapMutator)
mutator.MutatorIntervalBase = NewMutatorIntervalBase(mutator, probability, chromosomeConstructor)
mutator.inverter = NewInvertMutator(probability, chromosomeConstructor)
return mutator
}
func (mutator *InvertSwapMutator) MutateGenes(genes GenesInterface, from, to int) {
mutator.inverter.MutateGenes(genes, from, to)
mutator.swap(genes, from, to)
}
func (mutator *InvertSwapMutator) swap(genes GenesInterface, from, to int) {
ind1 := mutator.chooseFirstInd(genes.Len(), from , to)
ind2 := mutator.chooseSecondInd(genes.Len(), from , to)
genes.Swap(ind1, ind2)
}
func (mutator *InvertSwapMutator) chooseFirstInd(genesLen, from, to int) int {
return rand.Intn(to - from) + from
}
func (mutator *InvertSwapMutator) chooseSecondInd(genesLen, from, to int) int {
ind2 := rand.Intn(genesLen - from)
if ind2 >= from {
ind2 += from
}
return ind2
}