This repository has been archived by the owner on Apr 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 698a4d0
Showing
7 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Binary file | ||
turtles |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |