-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
145 lines (114 loc) · 3.31 KB
/
main.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"log"
"math/rand"
"os"
"strconv"
"sync"
"time"
)
var (
nodes []point
)
func main() {
rand.Seed(time.Now().UTC().UnixNano())
if len(os.Args) != 4 {
log.Fatalf("usage: %s <tsp file> <island file> <output file>\n", os.Args[0])
}
nodes = parseFile(os.Args[1])
islands := islandsFromFile(os.Args[2])
csvHelper := newCSVHelper(os.Args[3])
defer csvHelper.close()
iterations := 20
generations := 100
// between 1 and numIslands
migRates := []float64{1, 2, 3, 4}
//migRates := []float64{1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6}
for m, migRate := range migRates {
avgBestFit := 0.0
for i := 0; i < iterations; i++ {
log.Printf("running iteration %v/%v (migRate=%v %v/%v)\n", m*iterations+i+1, len(migRates)*iterations, migRate, i+1, iterations)
t := time.Now()
bestFitPerGen, _ := run(generations, islands, migRate, false)
avgBestFit += bestFitPerGen[len(bestFitPerGen)-1]
log.Printf("done after %v\n", time.Since(t))
}
avgBestFit /= float64(iterations)
csvHelper.write([]string{
strconv.FormatFloat(migRate, 'E', -1, 64),
strconv.FormatFloat(avgBestFit, 'E', -1, 64),
})
}
}
func run(generations int, islands []population, migRate float64, dynamic bool) (bestFitPerGen []float64, divPerIslPerGen [][]float64) {
numIslands := len(islands)
islandsCopy := make([]population, numIslands)
for i, popul := range islands {
islandsCopy[i] = popul.copy()
}
islands = islandsCopy
bestFitPerGen = make([]float64, generations+1)
divPerIslPerGen = make([][]float64, numIslands)
for i := 0; i < numIslands; i++ {
divPerIslPerGen[i] = make([]float64, generations+1)
}
migrants := make([][]*individual, numIslands)
for i := 0; i < numIslands; i++ {
migrants[i] = make([]*individual, 0, len(islands[i]))
}
var wgMain sync.WaitGroup
wgIslands := make([]sync.WaitGroup, numIslands)
// launch islands
for i := 0; i < numIslands; i++ {
go func(i int) {
clock := time.Now()
if i == 0 {
log.Printf("%5.1f%% done\n", 0.0)
}
for g := 0; g < generations; g++ {
divPerIslPerGen[i][g] = islands[i].diversity()
// new generation
islands[i] = islands[i].newGeneration()
// immigration
for j, immigrant := range migrants[i] {
_, islands[i] = islands[i].removeRandom()
islands[i] = islands[i].insert(immigrant)
migrants[i][j] = nil
}
migrants[i] = migrants[i][:0]
if i == 0 && time.Since(clock) > 10*time.Second {
clock = time.Now()
log.Printf("%5.1f%% done\n", 100*float64(g+1)/float64(generations))
}
wgMain.Done()
wgIslands[i].Add(1)
wgIslands[i].Wait()
}
divPerIslPerGen[i][generations] = islands[i].diversity()
}(i)
}
// synchronize islands
for g := 0; g < generations; g++ {
bestFitPerGen[g] = islandsBestFitness(islands)
wgMain.Add(numIslands)
wgMain.Wait()
// migrations
for i := 0; i < numIslands; i++ {
immigrations := int(migRate)
if dynamic {
concentration := 1.0 - islands[i].diversity()
immigrations = int(migRate * concentration)
}
sources := randomSlice(immigrations, numIslands, i)
for _, j := range sources {
k := islands[j].selection()
migrants[i] = append(migrants[i], islands[j][k].copy())
}
}
for i := 0; i < numIslands; i++ {
wgIslands[i].Done()
}
}
bestFitPerGen[generations] = islandsBestFitness(islands)
return
}