Skip to content

Commit

Permalink
[evaluate] Use rng in randomTwoInts
Browse files Browse the repository at this point in the history
  • Loading branch information
tanghaibao committed Jul 2, 2018
1 parent a0cefdf commit a9bf4e6
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions evaluate.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ func randomInts(k, min, max int, rng *rand.Rand) (ints []int) {
}

// randomTwoInts is a faster version than randomInts above
func randomTwoInts(genome gago.Slice) (int, int) {
func randomTwoInts(genome gago.Slice, rng *rand.Rand) (int, int) {
n := genome.Len()
p := rand.Intn(n)
q := rand.Intn(n)
p := rng.Intn(n)
q := rng.Intn(n)
if p > q {
p, q = q, p
}
Expand All @@ -142,7 +142,7 @@ func randomTwoInts(genome gago.Slice) (int, int) {
func MutInversion(genome gago.Slice, rng *rand.Rand) {
// log.Debugf("Before MutInversion: %v", genome)
// Choose two points on the genome
p, q := randomTwoInts(genome)
p, q := randomTwoInts(genome, rng)
if p == q {
return
}
Expand All @@ -157,16 +157,27 @@ func MutInversion(genome gago.Slice, rng *rand.Rand) {
func MutInsertion(genome gago.Slice, rng *rand.Rand) {
// log.Debugf("Before MutInsertion: %v", genome)
// Choose two points on the genome
p, q := randomTwoInts(genome)
p, q := randomTwoInts(genome, rng)
if p == q {
return
}
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))

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)
}
genome.Set(p, cq)

// log.Debugf("After MutInsertion: %v", genome)
}

Expand Down

0 comments on commit a9bf4e6

Please sign in to comment.