From a9bf4e62abbcab8ab4785428e075a5f4eae78ddd Mon Sep 17 00:00:00 2001 From: Haibao Tang Date: Mon, 2 Jul 2018 11:28:44 -0700 Subject: [PATCH] [evaluate] Use rng in randomTwoInts --- evaluate.go | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/evaluate.go b/evaluate.go index 72f4e76..e1cf967 100644 --- a/evaluate.go +++ b/evaluate.go @@ -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 } @@ -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 } @@ -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) }