-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathgenome_ordered_int.go
147 lines (131 loc) · 3.35 KB
/
genome_ordered_int.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
/*
Copyright 2009 Thomas Jager <[email protected]> All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
Ordered list genome for problems where the order of Genes matter, tSP for example.
*/
package ga
import (
//"container/vector"
//"container/list"
"fmt"
"math/rand"
"sort"
)
type GAOrderedIntGenome struct {
Gene []int
score float64
hasscore bool
sfunc func(ga *GAOrderedIntGenome) float64
}
func NewOrderedIntGenome(i []int, sfunc func(ga *GAOrderedIntGenome) float64) *GAOrderedIntGenome {
g := new(GAOrderedIntGenome)
g.Gene = i
g.sfunc = sfunc
return g
}
//Helper for Partially mapped crossover
func (a *GAOrderedIntGenome) pmxmap(v, p1, p2 int) (int, bool) {
for i, c := range a.Gene {
if c == v && (i < p1 || i > p2) {
return i, true
}
}
return 0, false
}
// Partially mapped crossover.
func (a *GAOrderedIntGenome) Crossover(bi GAGenome, p1, p2 int) (GAGenome, GAGenome) {
ca := a.Copy().(*GAOrderedIntGenome)
b := bi.(*GAOrderedIntGenome)
cb := b.Copy().(*GAOrderedIntGenome)
copy(ca.Gene[p1:p2+1], b.Gene[p1:p2+1])
copy(cb.Gene[p1:p2+1], a.Gene[p1:p2+1])
//Proto child needs fixing
//amap := new(vector.IntVector)
//bmap := new(vector.IntVector)
amap := make([]int, 0)
bmap := make([]int, 0)
for i := p1; i <= p2; i++ {
ma, found := ca.pmxmap(ca.Gene[i], p1, p2)
if found {
//amap.Push(ma)
amap = append(amap, ma)
//if bmap.Len() > 0 {
if len(bmap) > 0 {
//i1 := amap.Pop()
//i2 := bmap.Pop()
var i1, i2 int
i1, amap = amap[len(amap)-1], amap[:len(amap)-1]
i2, bmap = bmap[len(bmap)-1], bmap[:len(bmap)-1]
ca.Gene[i1], cb.Gene[i2] = cb.Gene[i2], ca.Gene[i1]
}
}
mb, found := cb.pmxmap(cb.Gene[i], p1, p2)
if found {
//bmap.Push(mb)
bmap = append(bmap, mb)
//if amap.Len() > 0 {
if len(amap) > 0 {
//i1 := amap.Pop()
//i2 := bmap.Pop()
var i1, i2 int
i1, amap = amap[len(amap)-1], amap[:len(amap)-1]
i2, bmap = bmap[len(bmap)-1], bmap[:len(bmap)-1]
ca.Gene[i1], cb.Gene[i2] = cb.Gene[i2], ca.Gene[i1]
}
}
}
ca.Reset()
cb.Reset()
return ca, cb
}
func (a *GAOrderedIntGenome) Splice(bi GAGenome, from, to, length int) {
b := bi.(*GAOrderedIntGenome)
copy(a.Gene[to:length+to], b.Gene[from:length+from])
a.Reset()
}
func (g *GAOrderedIntGenome) Valid() bool {
t := g.Copy().(*GAOrderedIntGenome)
sort.Ints(t.Gene)
last := -9
for _, c := range t.Gene {
if last > -1 && c == last {
fmt.Printf("%d - %d", c, last)
return false
}
last = c
}
return true
}
func (g *GAOrderedIntGenome) Switch(x, y int) {
g.Gene[x], g.Gene[y] = g.Gene[y], g.Gene[x]
g.Reset()
}
func (g *GAOrderedIntGenome) Randomize() {
l := len(g.Gene)
for i := 0; i < l; i++ {
x := rand.Intn(l)
y := rand.Intn(l)
g.Gene[x], g.Gene[y] = g.Gene[y], g.Gene[x]
}
g.Reset()
}
func (g *GAOrderedIntGenome) Copy() GAGenome {
n := new(GAOrderedIntGenome)
n.Gene = make([]int, len(g.Gene))
copy(n.Gene, g.Gene)
n.sfunc = g.sfunc
n.score = g.score
n.hasscore = g.hasscore
return n
}
func (g *GAOrderedIntGenome) Len() int { return len(g.Gene) }
func (g *GAOrderedIntGenome) Score() float64 {
if !g.hasscore {
g.score = g.sfunc(g)
g.hasscore = true
}
return g.score
}
func (g *GAOrderedIntGenome) Reset() { g.hasscore = false }
func (g *GAOrderedIntGenome) String() string { return fmt.Sprintf("%v", g.Gene) }