Skip to content
This repository has been archived by the owner on Apr 22, 2021. It is now read-only.

Commit

Permalink
all: add basic GA operations
Browse files Browse the repository at this point in the history
  • Loading branch information
yoonsue committed Nov 12, 2018
0 parents commit 698a4d0
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Binary file
turtles
25 changes: 25 additions & 0 deletions breeding/breeding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package breeding

import "math/rand"

func createChild(individual1 string, individual2 string) string {
child := ""
for i := 0; i < len(individual1); i++ {
if int(100*rand.Int()) < 50 {
child += string(individual1[i])
} else {
child += string(individual2[i])
}
}
return child
}

func createChildren(breeders []string, numberOfChild int) []string {
var nextPopulation []string
for i := 0; i < (len(breeders) / 2); i++ {
for j := 0; j < numberOfChild; j++ {
nextPopulation = append(nextPopulation, createChild(breeders[i], breeders[len(breeders)-1-i]))
}
}
return nextPopulation
}
24 changes: 24 additions & 0 deletions fitness/fitness.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package fitness

import (
"fmt"
)

type Fitness struct {
fit float32
}

func GetFitness(password string, testWord string) float32 {
if len(testWord) != len(password) {
fmt.Printf("length difference between test word and password\n")
return -1
} else {
score := 0
for i := range password {
if password[i] == testWord[i] {
score += 1
}
}
return float32(score * 100 / len(password))
}
}
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "fmt"

func main() {
fmt.Printf("Turtles Problem Sloving with GA\n")
}
25 changes: 25 additions & 0 deletions mutation/mutation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package mutation

import (
"math/rand"
"strconv"
)

func mutateWord(word string) string {
indexModification := int(rand.Int() * len(word))
if indexModification == 0 {
word = strconv.Itoa(97+int(26*rand.Int())) + word[1:]
} else {
word = word[:indexModification] + strconv.Itoa(97+int(26*rand.Int())) + word[indexModification+1:]
}
return word
}

func mutatePopulation(population []string, chanceOfMutation int) []string {
for i := 0; i < len(population); i++ {
if rand.Int()*100 < chanceOfMutation {
population[i] = mutateWord(population[i])
}
}
return population
}
23 changes: 23 additions & 0 deletions population/population.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package population

import (
"math/rand"
"strconv"
)

func generateAWord(length int) string {
result := ""
for i := 0; i < length; i++ {
letter := strconv.Itoa(97 + int(26*rand.Intn(100)))
result += letter
}
return result
}

func generateFirstPopulation(sizePopulation int, password string) []string {
var population []string
for i := 0; i < sizePopulation; i++ {
population = append(population, generateAWord(len(password)))
}
return population
}
30 changes: 30 additions & 0 deletions selection/selection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package selection

import (
"math/rand"

"github.com/yoonsue/turtles/fitness"
)

func computePerfPopulation(population []string, password string) {
var populationPerf []float32
for i, individual := range population {
populationPerf[i] = fitness.GetFitness(password, individual)
}
}

func selectFromPopulation(populationSorted [][]string, bestSample []string, luckyFew []string) []string {
var nextGeneration []string
for i, _ := range bestSample {
nextGeneration = append(nextGeneration, populationSorted[i][0])
}
for _, _ = range luckyFew {
nextGeneration = append(nextGeneration, populationSorted[rand.Intn(len(populationSorted))][0])
}
// TO BE IMPLEMENTED
// Population shuffle reversely
rand.Shuffle(len(nextGeneration), func(i, j int) {
nextGeneration[i], nextGeneration[j] = nextGeneration[j], nextGeneration[i]
})
return nextGeneration
}

0 comments on commit 698a4d0

Please sign in to comment.