Skip to content

Commit

Permalink
[allhic] Implements gago.Genome interface
Browse files Browse the repository at this point in the history
  • Loading branch information
tanghaibao committed Jan 4, 2018
1 parent e60eb10 commit e493349
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
39 changes: 38 additions & 1 deletion allhic/evaluate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package allhic

import (
"fmt"
"math/rand"

"github.com/MaxHalford/gago"
)
Expand Down Expand Up @@ -96,12 +97,48 @@ func (r Tour) Evaluate() (score float64) {
return
}

// Mutate a Tour by applying by permutation mutation and/or splice mutation
// TODO: CHANGE TO GENOME_MUTATION (INVERSION + TRANSPOSITION)
func (r Tour) Mutate(rng *rand.Rand) {
if rng.Float64() < 0.35 {
gago.MutPermute(r, 3, rng)
}
if rng.Float64() < 0.45 {
gago.MutSplice(r, rng)
}
}

// Crossover a Tour with another Tour by using Partially Mixed Crossover (PMX).
func (r Tour) Crossover(q gago.Genome, rng *rand.Rand) {
gago.CrossPMX(r, q.(Tour), rng)
}

// Clone a Tour.
func (r Tour) Clone() gago.Genome {
var clone Tour
copy(clone.Tigs, r.Tigs)
return clone
}

// Shuffle randomly shuffles an integer array using Knuth or Fisher-Yates
func (r Tour) Shuffle() {
N := r.Len()
for i := 0; i < N; i++ {
// choose index uniformly in [i, N-1]
j := i + rand.Intn(N-i)
r.Tigs[j], r.Tigs[i] = r.Tigs[i], r.Tigs[j]
}
}

// MakeTour creates a slice of tigs and shuffles them
// func MakeTour(rng *rand.Rand) gago.Genome {
// var tour Tour
// }
// MakeVector returns a random vector by generating 5 values uniformally
// distributed between -10 and 10.
// func MakeVector(rng *rand.Rand) gago.Genome {
// return Vector(gago.InitUnifFloat64(2, -20, 20, rng))
// }

// GASetup set up the Genetic algorithm
// func GASetup() {
// var ga = gago.Generational(MakeVector)
Expand Down
13 changes: 1 addition & 12 deletions allhic/optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,13 @@ package allhic

import (
"fmt"
"math/rand"
)

// Optimizer runs the order-and-orientation procedure, given a clmfile
type Optimizer struct {
Clmfile string
}

// Shuffle randomly shuffles an integer array using Knuth or Fisher-Yates
func Shuffle(tour Tour) {
N := len(tour.Tigs)
for i := 0; i < N; i++ {
// choose index uniformly in [i, N-1]
r := i + rand.Intn(N-i)
tour.Tigs[r], tour.Tigs[i] = tour.Tigs[i], tour.Tigs[r]
}
}

// Run kicks off the Optimizer
func (r *Optimizer) Run() {
clm := InitCLMFile(r.Clmfile)
Expand All @@ -44,7 +33,7 @@ func (r *Optimizer) Run() {
// }
// fmt.Println(test)

Shuffle(tour)
tour.Shuffle()
// fmt.Println(tour)
tourScore = tour.Evaluate()
fmt.Println(tourScore)
Expand Down

0 comments on commit e493349

Please sign in to comment.