-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitstrings.go
38 lines (32 loc) · 1.02 KB
/
bitstrings.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
package ga
import (
"errors"
"fmt"
"math/rand"
"strconv"
)
type Bitstring []string
type GenerateCandidateFunction func(int, *rand.Rand) (Bitstring, error)
// GenerateBitString returns an encoded string as set by calls SetGenerateBitString. Defaults to binary strings
var DefaultGenerateCandidate GenerateCandidateFunction = func(length int, random *rand.Rand) (Bitstring, error) {
if length <= 0 {
return nil, errors.New("strings cannot be zero-length")
}
var sequence Bitstring
for i := 0; i < length; i++ {
sequence = append(sequence, strconv.Itoa(random.Int()%2))
}
return sequence, nil
}
// SetGenerateBitString sets the function that generates the Bitstring candidatePool
func (genA *GeneticAlgorithm) SetGenerateCandidate(f GenerateCandidateFunction) {
genA.GenerateCandidate = f
}
// SetGenerateBitString sets the function that generates the Bitstring candidatePool
func (b Bitstring) String() string {
output := ""
for _, val := range b {
output += fmt.Sprintf("%v", val) + " "
}
return "[" + output + "]"
}