-
Notifications
You must be signed in to change notification settings - Fork 96
/
distance.go
61 lines (56 loc) · 2.03 KB
/
distance.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
package gago
// A Metric returns the distance between two genomes.
type Metric func(a, b Individual) float64
// A DistanceMemoizer computes and stores Metric calculations.
type DistanceMemoizer struct {
Metric Metric
Distances map[string]map[string]float64
nCalculations int // Total number of calls to Metric for testing purposes
}
// makeDistanceMemoizer initializes a DistanceMemoizer.
func makeDistanceMemoizer(metric Metric) DistanceMemoizer {
return DistanceMemoizer{
Metric: metric,
Distances: make(map[string]map[string]float64),
}
}
// GetDistance returns the distance between two Individuals based on the
// DistanceMemoizer's Metric field. If the two individuals share the same ID
// then GetDistance returns 0. DistanceMemoizer stores the calculated distances
// so that if GetDistance is called twice with the two same Individuals then
// the second call will return the stored distance instead of recomputing it.
func (dm *DistanceMemoizer) GetDistance(a, b Individual) float64 {
// Check if the two individuals are the same before proceding
if a.ID == b.ID {
return 0
}
// Create maps if the genomes have never been encountered
if _, ok := dm.Distances[a.ID]; !ok {
dm.Distances[a.ID] = make(map[string]float64)
} else {
// Check if the distance between the two genomes has been calculated
if dist, ok := dm.Distances[a.ID][b.ID]; ok {
return dist
}
}
if _, ok := dm.Distances[b.ID]; !ok {
dm.Distances[b.ID] = make(map[string]float64)
}
// Calculate the distance between the genomes and memoize it
var dist = dm.Metric(a, b)
dm.Distances[a.ID][b.ID] = dist
dm.Distances[b.ID][a.ID] = dist
dm.nCalculations++
return dist
}
// Return the average distance between a Individual and a slice of Individuals.
func calcAvgDistances(indis Individuals, dm DistanceMemoizer) map[string]float64 {
var avgDistances = make(map[string]float64)
for _, a := range indis {
for _, b := range indis {
avgDistances[a.ID] += dm.GetDistance(a, b)
}
avgDistances[a.ID] /= float64(len(indis) - 1)
}
return avgDistances
}