-
Notifications
You must be signed in to change notification settings - Fork 12
/
evaluate.go
315 lines (276 loc) · 7.29 KB
/
evaluate.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/**
* Filename: /Users/bao/code/allhic/allhic/evaluate.go
* Path: /Users/bao/code/allhic/allhic
* Created Date: Wednesday, January 3rd 2018, 9:40:36 pm
* Author: bao
*
* Copyright (c) 2018 Haibao Tang
*/
package allhic
import (
"fmt"
"math"
"math/rand"
"os"
"github.com/MaxHalford/eaopt"
)
// LIMIT determines the largest distance for two tigs to add to total score
const LIMIT = 10000000
// LimitLog is the Log of LIMIT
var LimitLog = math.Log(LIMIT)
// We will implement the Slice interface here, key ideas borrowed from:
// https://github.com/MaxHalford/eaopt-examples/blob/master/tsp_grid/main.go
// At method from Slice
func (r Tour) At(i int) interface{} {
return r.Tigs[i]
}
// Set method from Slice
func (r Tour) Set(i int, v interface{}) {
r.Tigs[i] = v.(Tig)
}
// Len method from Slice
func (r Tour) Len() int {
return len(r.Tigs)
}
// Swap method from Slice
func (r Tour) Swap(i, j int) {
r.Tigs[i], r.Tigs[j] = r.Tigs[j], r.Tigs[i]
}
// Slice method from Slice
func (r Tour) Slice(a, b int) eaopt.Slice {
return Tour{r.Tigs[a:b], r.M}
}
// Split method from Slice
func (r Tour) Split(k int) (eaopt.Slice, eaopt.Slice) {
return Tour{r.Tigs[:k], r.M}, Tour{r.Tigs[k:], r.M}
}
// Append method from Slice
func (r Tour) Append(q eaopt.Slice) eaopt.Slice {
return Tour{append(r.Tigs, q.(Tour).Tigs...), r.M}
}
// Replace method from Slice
func (r Tour) Replace(q eaopt.Slice) {
copy(r.Tigs, q.(Tour).Tigs)
}
// Copy method from Slice
func (r Tour) Copy() eaopt.Slice {
var clone Tour
clone.Tigs = make([]Tig, r.Len())
copy(clone.Tigs, r.Tigs)
clone.M = r.M
return clone
}
// EvaluateSumLog calculates a score for the current tour
func (r Tour) EvaluateSumLog() (float64, error) {
//func (r Tour) Evaluate() (float64, error) {
size := r.Len()
mid := make([]float64, size)
cumSum := 0.0
for i, t := range r.Tigs {
tsize := float64(t.Size)
mid[i] = cumSum + tsize/2
cumSum += tsize
}
score := 0.0
// Now add up all the pairwise scores
for i := 0; i < size; i++ {
a := r.Tigs[i].Idx
for j := i + 1; j < size; j++ {
b := r.Tigs[j].Idx
nlinks := r.M[a][b]
dist := mid[j] - mid[i]
// This serves two purposes:
// 1. Break earlier reduces the amount of calculation
// 2. Ignore distant links so that telomeric regions don't come
// to be adjacent (based on Ler0 data)
if dist > LIMIT {
break
}
// eaopt only looks at minimum =>
// everytime we have a small dist, we reduce the total score
// we are looking at the largest reductions from all links
score += float64(nlinks) * (math.Log(dist) - LimitLog)
}
}
return score, nil
}
// Evaluate calculates a score for the current tour
func (r Tour) Evaluate() (float64, error) {
//func (r Tour) EvaluateSumRecip() (float64, error) {
size := r.Len()
mid := make([]float64, size)
cumSum := 0.0
for i, t := range r.Tigs {
tsize := float64(t.Size)
mid[i] = cumSum + tsize/2
cumSum += tsize
}
score := 0.0
// Now add up all the pairwise scores
for i := 0; i < size; i++ {
a := r.Tigs[i].Idx
for j := i + 1; j < size; j++ {
b := r.Tigs[j].Idx
nlinks := r.M[a][b]
dist := mid[j] - mid[i]
if dist > LIMIT {
break
}
// We are looking for maximum
score -= float64(nlinks) / dist
}
}
return score, nil
}
// randomTwoInts is a faster version than randomInts above
func randomTwoInts(genome eaopt.Slice, rng *rand.Rand) (int, int) {
n := genome.Len()
p := rng.Intn(n)
q := rng.Intn(n)
if p > q {
p, q = q, p
}
return p, q
}
// MutInversion applies inversion operation on the genome
func MutInversion(genome eaopt.Slice, rng *rand.Rand) {
// log.Debugf("Before MutInversion: %v", genome)
// Choose two points on the genome
p, q := randomTwoInts(genome, rng)
if p == q {
return
}
// Swap within range
for i, j := p, q; i < j; i, j = i+1, j-1 {
genome.Swap(i, j)
}
// log.Debugf("After MutInversion: %v", genome)
}
// MutInsertion applies insertion operation on the genome
func MutInsertion(genome eaopt.Slice, rng *rand.Rand) {
// log.Debugf("Before MutInsertion: %v", genome)
// Choose two points on the genome
p, q := randomTwoInts(genome, rng)
if p == q {
return
}
if rng.Float64() < .5 {
cq := genome.At(q) // Pop q and insert to p position
// Move cq to the front and push everyone right
for i := q; i > p; i-- {
genome.Set(i, genome.At(i-1))
}
genome.Set(p, cq)
} else {
cp := genome.At(p)
// Move cq to the back and push everyone left
for i := p; i < q; i++ {
genome.Set(i, genome.At(i+1))
}
genome.Set(q, cp)
}
// log.Debugf("After MutInsertion: %v", genome)
}
// MutPermute permutes two genes at random n times
func MutPermute(genome eaopt.Slice, rng *rand.Rand) {
// Nothing to permute
if genome.Len() <= 1 {
return
}
// Choose two points on the genome
p, q := randomTwoInts(genome, rng)
genome.Swap(p, q)
}
// MutSplice splits a genome in 2 and glues the pieces back together in reverse
// order
func MutSplice(genome eaopt.Slice, rng *rand.Rand) {
var (
k = rng.Intn(genome.Len()-1) + 1
a, b = genome.Split(k)
)
genome.Replace(b.Append(a))
}
// Mutate a Tour by applying by inversion or insertion
func (r Tour) Mutate(rng *rand.Rand) {
rd := rng.Float64()
if rd < 0.2 {
MutPermute(r, rng)
} else if rd < .4 {
MutSplice(r, rng)
} else if rd < .7 {
MutInsertion(r, rng)
} else {
MutInversion(r, rng)
}
}
// Crossover a Tour with another Tour by using Partially Mixed Crossover (PMX).
func (r Tour) Crossover(_ eaopt.Genome, _ *rand.Rand) {
}
// Clone a Tour
func (r Tour) Clone() eaopt.Genome {
var clone Tour
clone.Tigs = make([]Tig, r.Len())
copy(clone.Tigs, r.Tigs)
clone.M = r.M
return clone
}
// Shuffle randomly shuffles an integer array using Knuth or Fisher-Yates
func (r Tour) Shuffle(rng *rand.Rand) {
N := r.Len()
for i := 0; i < N; i++ {
// choose index uniformly in [i, N-1]
j := i + rng.Intn(N-i)
r.Tigs[j], r.Tigs[i] = r.Tigs[i], r.Tigs[j]
}
}
// GARun set up the Genetic Algorithm and run it
func (r *CLM) GARun(fwtour *os.File, opt *Optimizer, phase int) Tour {
MakeTour := func(rng *rand.Rand) eaopt.Genome {
c := r.Tour.Clone()
return c
}
ga, err := eaopt.NewDefaultGAConfig().NewGA()
if err != nil {
panic(err)
}
ga.NPops = 1
ga.NGenerations = 1000000
ga.PopSize = uint(opt.NPop)
ga.Model = eaopt.ModGenerational{
Selector: eaopt.SelTournament{
NContestants: 3,
},
MutRate: opt.MutProb,
}
ga.RNG = opt.rng
ga.ParallelEval = true
best := new(float64)
updated := new(uint)
*best = -math.MaxFloat64 // Currently best score
*updated = 0 // Last updated generation
// Additional bookkeeping per generation
ga.Callback = func(ga *eaopt.GA) {
gen := ga.Generations
currentBest := -ga.HallOfFame[0].Fitness
if currentBest > *best {
*best = currentBest
*updated = gen
}
if gen%500 == 0 {
fmt.Printf("Current iteration GA%d-%d: max_score=%.5f\n",
phase, gen, currentBest)
currentBestTour := ga.HallOfFame[0].Genome.(Tour)
r.printTour(fwtour, currentBestTour, fmt.Sprintf("GA%d-%d-%.5f",
phase, gen, currentBest))
}
}
// Convergence criteria
ga.EarlyStop = func(ga *eaopt.GA) bool {
return ga.Generations-*updated > uint(opt.NGen)
}
log.Noticef("GA initialized (npop: %v, ngen: %v, mu: %.2f, rng: %d, break: %d)",
opt.NPop, opt.NGen, opt.MutProb, opt.Seed, LIMIT)
_ = ga.Minimize(MakeTour)
r.Tour = ga.HallOfFame[0].Genome.(Tour)
return r.Tour
}