forked from MaxHalford/eaopt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
40 lines (36 loc) · 870 Bytes
/
util.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
package gago
import "math/rand"
// Find where an element is in a slice.
func getIndex(element interface{}, array []interface{}) int {
for i, v := range array {
if v == element {
return i
}
}
// Element not in array
return -1
}
// Generate random weights that sum up to 1?
func generateWeights(size int) []float64 {
var weights = make([]float64, size)
// Sum of the weights
var total float64
// Assign a weight to each individual
for i := range weights {
weights[i] = rand.Float64()
total += weights[i]
}
// Normalize the weights
for i := range weights {
weights[i] /= total
}
return weights
}
// Shuffle a slice of strings.
func shuffleStrings(strings []string, generator *rand.Rand) []string {
var shuffled = make([]string, len(strings))
for i, v := range generator.Perm(len(strings)) {
shuffled[v] = strings[i]
}
return shuffled
}