Skip to content

Commit aa68a81

Browse files
author
blaine booher
committed
updated rand to math/rand, replaced vector with slices (of fixed lengths)
1 parent 85a9ef1 commit aa68a81

14 files changed

+69
-48
lines changed

breeder.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ go-galib beeders
99
package ga
1010

1111
import (
12-
"rand"
12+
"math/rand"
1313
)
1414

1515
type GABreeder interface {

example/floating.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ subset sum solver
88
package main
99

1010
import (
11-
"math"
11+
"../_obj/ga"
1212
"fmt"
13-
"rand"
13+
"math"
14+
"math/rand"
1415
"time"
15-
"../_obj/ga"
1616
)
1717

1818
var scores int
@@ -41,7 +41,6 @@ func rosenbrock(g *ga.GAFloatGenome) float64 {
4141
return sum
4242
}
4343

44-
4544
func main() {
4645
rand.Seed(time.Nanoseconds())
4746

example/floating_parallel.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ subset sum solver
88
package main
99

1010
import (
11-
"math"
11+
"../_obj/ga"
1212
"fmt"
13-
"rand"
13+
"math"
14+
"math/rand"
1415
"time"
15-
"../_obj/ga"
1616
)
1717

1818
var scores int
@@ -41,7 +41,6 @@ func rosenbrock(g *ga.GAFloatGenome) float64 {
4141
return sum
4242
}
4343

44-
4544
func main() {
4645
rand.Seed(time.Nanoseconds())
4746

example/ordered_int.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ Example of uing the ordered int genome and mutators
88
package main
99

1010
import (
11+
"../_obj/ga"
1112
"fmt"
12-
"rand"
13+
"math/rand"
1314
"time"
14-
"../_obj/ga"
1515
)
1616

1717
var scores int
18+
1819
// Boring fitness/score function.
1920
func score(g *ga.GAOrderedIntGenome) float64 {
2021
var total int

example/subset_sum.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ subset sum solver
88
package main
99

1010
import (
11+
"../_obj/ga"
1112
"fmt"
12-
"rand"
13+
"math/rand"
1314
"time"
14-
"../_obj/ga"
1515
)
1616

1717
var theset = [200]int{332, 401, -178, 60, -436, -135, -275, 192, -223, -150, -67, 401, -140, -71, 152, 478, 210, -485, -465, -492, -355, -474, -420, 213, -63, 33, 366, -94, -469, 429, -307, -291, 176, 465, 180, 28, 408, -245, -318, -66, -158, -202, -191, 47, -71, -320, 142, 305, 429, -449, -58, -115, 153, -47, 95, 215, 82, 452, 390, 331, -419, -68, -416, 331, -35, -102, 270, -72, -81, 133, 159, -417, 455, -99, -137, -477, -99, 312, -409, -401, -468, -453, -165, 163, 415, -85, 304, 307, -38, -439, 162, 310, 13, 320, 362, 336, -461, 435, 378, 194, -430, -322, 307, -159, -325, -290, -339, 485, -464, 315, -205, 385, 98, 439, -82, 374, -288, -407, -225, -463, 302, -442, 237, -427, -40, -156, -117, -53, -386, -133, -5, -287, -403, -487, -134, -273, 481, -405, 459, 108, 454, -106, 76, 116, -390, 90, -52, -120, -213, -62, -481, -417, 115, -33, 484, -8, 243, 439, -491, -299, -289, 191, 394, -161, 109, -468, -289, 355, 293, -54, -499, -374, -99, -142, 238, 115, -46, -182, 398, -32, 186, -91, -479, -108, -399, -231, -212, -233, 178, 82, -126, 304, -140, -364, 14, -307, 280, -334, 2, -444}
1818

1919
var scores int
20+
2021
// Fitness function for subset sum
2122
func score(g *ga.GAFixedBitstringGenome) float64 {
2223
scores++

ga.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ package ga
1010

1111
import (
1212
"fmt"
13+
"math/rand"
1314
"sort"
14-
"rand"
1515
)
1616

1717
type GAParameter struct {

genome_fixed_bitstring.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ package ga
1010

1111
import (
1212
"fmt"
13-
"rand"
13+
"math/rand"
1414
)
1515

1616
type GAFixedBitstringGenome struct {
@@ -88,7 +88,6 @@ func (g *GAFixedBitstringGenome) Score() float64 {
8888

8989
func (g *GAFixedBitstringGenome) Reset() { g.hasscore = false }
9090

91-
9291
func (g *GAFixedBitstringGenome) String() string {
9392
return fmt.Sprintf("%v", g.Gene)
9493
}

genome_float64.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ package ga
1111

1212
import (
1313
"fmt"
14-
"rand"
14+
"math/rand"
1515
)
1616

1717
type GAFloatGenome struct {
@@ -92,5 +92,4 @@ func (g *GAFloatGenome) Score() float64 {
9292

9393
func (g *GAFloatGenome) Reset() { g.hasscore = false }
9494

95-
9695
func (g *GAFloatGenome) String() string { return fmt.Sprintf("%v", g.Gene) }

genome_ordered_int.go

+25-12
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ Ordered list genome for problems where the order of Genes matter, tSP for exampl
99
package ga
1010

1111
import (
12+
//"container/vector"
13+
//"container/list"
1214
"fmt"
13-
"container/vector"
15+
"math/rand"
1416
"sort"
15-
"rand"
1617
)
1718

1819
type GAOrderedIntGenome struct {
@@ -47,24 +48,36 @@ func (a *GAOrderedIntGenome) Crossover(bi GAGenome, p1, p2 int) (GAGenome, GAGen
4748
copy(ca.Gene[p1:p2+1], b.Gene[p1:p2+1])
4849
copy(cb.Gene[p1:p2+1], a.Gene[p1:p2+1])
4950
//Proto child needs fixing
50-
amap := new(vector.IntVector)
51-
bmap := new(vector.IntVector)
51+
//amap := new(vector.IntVector)
52+
//bmap := new(vector.IntVector)
53+
amap := make([]int, 1000)
54+
bmap := make([]int, 1000)
5255
for i := p1; i <= p2; i++ {
5356
ma, found := ca.pmxmap(ca.Gene[i], p1, p2)
5457
if found {
55-
amap.Push(ma)
56-
if bmap.Len() > 0 {
57-
i1 := amap.Pop()
58-
i2 := bmap.Pop()
58+
//amap.Push(ma)
59+
amap = append(amap, ma)
60+
//if bmap.Len() > 0 {
61+
if len(bmap) > 0 {
62+
//i1 := amap.Pop()
63+
//i2 := bmap.Pop()
64+
var i1, i2 int
65+
i1, amap = amap[len(amap)-1], amap[:len(amap)-1]
66+
i2, bmap = bmap[len(bmap)-1], bmap[:len(bmap)-1]
5967
ca.Gene[i1], cb.Gene[i2] = cb.Gene[i2], ca.Gene[i1]
6068
}
6169
}
6270
mb, found := cb.pmxmap(cb.Gene[i], p1, p2)
6371
if found {
64-
bmap.Push(mb)
65-
if amap.Len() > 0 {
66-
i1 := amap.Pop()
67-
i2 := bmap.Pop()
72+
//bmap.Push(mb)
73+
bmap = append(bmap, mb)
74+
//if amap.Len() > 0 {
75+
if len(amap) > 0 {
76+
//i1 := amap.Pop()
77+
//i2 := bmap.Pop()
78+
var i1, i2 int
79+
i1, amap = amap[len(amap)-1], amap[:len(amap)-1]
80+
i2, bmap = bmap[len(bmap)-1], bmap[:len(bmap)-1]
6881
ca.Gene[i1], cb.Gene[i2] = cb.Gene[i2], ca.Gene[i1]
6982
}
7083
}

mutator_gaussian.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Shifts the whole genome random length to the right.
99
package ga
1010

1111
import (
12-
"rand"
12+
"math/rand"
1313
)
1414

1515
type GAGaussianMutator struct {

mutator_multi.go

+24-13
Original file line numberDiff line numberDiff line change
@@ -9,47 +9,58 @@ Combines several mutators into one, each mutation has equal chance of occuring.
99
package ga
1010

1111
import (
12-
"rand"
13-
"container/vector"
12+
//"container/vector"
1413
"fmt"
14+
"math/rand"
1515
)
1616

1717
type GAMultiMutator struct {
18-
v *vector.Vector
18+
//v *vector.Vector
19+
v []GAMutator
1920
stats []int
2021
}
2122

2223
func NewMultiMutator() *GAMultiMutator {
2324
m := new(GAMultiMutator)
24-
m.v = new(vector.Vector)
25+
//m.v = new(vector.Vector)
26+
m.v = make([]GAMutator, 100)
2527
m.stats = make([]int, 100)
2628
return m
2729
}
2830

2931
func (m GAMultiMutator) Mutate(a GAGenome) GAGenome {
30-
if m.v.Len() == 0 {
32+
//if m.v.Len() == 0 {
33+
if len(m.v) == 0 {
3134
panic("No mutators added!")
3235
}
33-
r := float64(1.0 / float64(m.v.Len()))
34-
for i := 0; i < m.v.Len()-1; i++ {
36+
//r := float64(1.0 / float64(m.v.Len()))
37+
r := float64(1.0 / float64(len(m.v)))
38+
//for i := 0; i < m.v.Len()-1; i++ {
39+
for i := 0; i < (len(m.v) - 1); i++ {
3540
if rand.Float64() < r {
36-
sm := m.v.At(i).(GAMutator)
41+
//sm := m.v.At(i).(GAMutator)
42+
sm := m.v[i].(GAMutator)
3743
m.stats[i]++
3844
return sm.Mutate(a)
3945
}
4046
}
41-
sm := m.v.At(m.v.Len() - 1).(GAMutator)
42-
m.stats[m.v.Len()-1]++
47+
//sm := m.v.At(m.v.Len() - 1).(GAMutator)
48+
sm := m.v[len(m.v)-1].(GAMutator)
49+
//m.stats[m.v.Len()-1]++
50+
m.stats[len(m.v)-1]++
4351
return sm.Mutate(a)
4452
}
4553

4654
//Add mutator
47-
func (m *GAMultiMutator) Add(a GAMutator) { m.v.Push(a) }
55+
//func (m *GAMultiMutator) Add(a GAMutator) { m.v.Push(a) }
56+
func (m *GAMultiMutator) Add(a GAMutator) { m.v = append(m.v, a) }
4857
func (m GAMultiMutator) String() string { return "GAMultiMutator" }
4958
func (m *GAMultiMutator) Stats() string {
5059
o := "Used "
51-
for i := 0; i < m.v.Len(); i++ {
52-
sm := m.v.At(i).(GAMutator)
60+
//for i := 0; i < m.v.Len(); i++ {
61+
for i := 0; i < len(m.v); i++ {
62+
//sm := m.v.At(i).(GAMutator)
63+
sm := m.v[i].(GAMutator)
5364
o = fmt.Sprintf("%s%s %d times, ", o, sm, m.stats[i])
5465
}
5566
return o

mutator_shift.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Shifts the whole genome random length to the right.
99
package ga
1010

1111
import (
12-
"rand"
12+
"math/rand"
1313
)
1414

1515
type GAShiftMutator struct{}

mutator_switch.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ the copy and returns the new mutated copy.
1010
package ga
1111

1212
import (
13-
"rand"
13+
"math/rand"
1414
)
1515

1616
type GASwitchMutator struct{}

selector.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ license that can be found in the LICENSE file.
66
go-galib selectors
77
*/
88

9-
109
package ga
1110

1211
import (
1312
"math"
14-
"rand"
13+
"math/rand"
1514
"sort"
1615
)
1716

0 commit comments

Comments
 (0)