Skip to content

Commit

Permalink
feat: genetic algorithm methods
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandreps1123 committed Apr 26, 2024
1 parent 1bc0b60 commit 93200dd
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
54 changes: 54 additions & 0 deletions fuzz/genetic_operators/crossover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package genetic_operators

// metodos a serem testados podem ter mais de um parametro de entrada
// uma seed representa uma lista de paramentros de entrada
func Crossover(seed1, seed2 []string) ([]string, []string) {
var crossoverSeed1 []string
var crossoverSeed2 []string

if len(seed1) == len(seed2) {
for i := range seed1 {
smallest := smallestSize(seed1[i], seed2[i])
// analisar a necessidade desta verificacao
if smallest == 0 {
crossoverSeed1 = seed1
crossoverSeed2 = seed2
// funcionando como esperado
} else if smallest == 1 {
var tempSeed1 string
var tempSeed2 string
for j := 0; j < smallest; j++ {
tempSeed1 = seed1[i][j:j+1] + seed2[i][j:j+1]
tempSeed2 = seed2[i][j:j+1] + seed1[i][j:j+1]
}
crossoverSeed1 = append(crossoverSeed1, tempSeed1)
crossoverSeed2 = append(crossoverSeed2, tempSeed2)
} else {
var tempSeed1 string
var tempSeed2 string
for j := 0; j < smallest; j++ {

if j%2 == 0 {
tempSeed1 += seed2[i][j : j+1]
tempSeed2 += seed1[i][j : j+1]
} else {
tempSeed1 += seed1[i][j : j+1]
tempSeed2 += seed2[i][j : j+1]
}
}
crossoverSeed1 = append(crossoverSeed1, tempSeed1)
crossoverSeed2 = append(crossoverSeed2, tempSeed2)
}
}
}

return crossoverSeed1, crossoverSeed2
}

func smallestSize(seed1, seed2 string) int {
if len(seed1) >= len(seed2) {
return len(seed2)
}

return len(seed1)
}
14 changes: 14 additions & 0 deletions fuzz/genetic_operators/mutation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package genetic_operators

import "github.com/dogefuzz/dogefuzz/pkg/common"

const MUTATION_CHANCE = 0.1

func Mutation(mutationFunction []func()) {
rnd := common.RandomFloat64()

if rnd < MUTATION_CHANCE {
mutationFunction := common.RandomChoice(mutationFunction)
mutationFunction()
}
}
40 changes: 40 additions & 0 deletions fuzz/genetic_operators/selection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package genetic_operators

import (
"math"

"github.com/dogefuzz/dogefuzz/pkg/common"
)

// consts to define chances to select some seeds
const FIRST_LIMIT = 0.5
const SECOND_LIMIT = 0.8

// consts to define what range interval will used to prioritize some seeds
const FIRST_RANGE = 0.4
const SECOND_RANGE = 0.7

func Selection(seedsList [][]string) []string {
rnd := common.RandomFloat64()
lengthList := len(seedsList)
var seedsSlice [][]string

if rnd < FIRST_LIMIT {
seedsSlice = seedsList[0:maxIndex(lengthList, FIRST_RANGE)]
} else if rnd < SECOND_LIMIT {
seedsSlice = seedsList[minIndex(lengthList, FIRST_RANGE):maxIndex(lengthList, SECOND_RANGE)]
} else {
// [minIndex:len(seedsList)]
seedsSlice = seedsList[minIndex(lengthList, SECOND_RANGE):]
}

return common.RandomChoice(seedsSlice)
}

func maxIndex(length int, factor float64) int {
return int(math.Ceil(float64(length) * factor))
}

func minIndex(length int, factor float64) int {
return int(math.Floor(float64(length) * factor))
}

0 comments on commit 93200dd

Please sign in to comment.