2021-11-22 20:33:22 -08:00
|
|
|
package grow
|
|
|
|
|
|
|
|
|
|
import "math/rand"
|
|
|
|
|
|
|
|
|
|
import "github.com/firestuff/subcoding/gen"
|
|
|
|
|
import "github.com/firestuff/subcoding/vm"
|
|
|
|
|
|
2021-11-22 21:05:05 -08:00
|
|
|
const instructionsPerFunctionMean = 15
|
|
|
|
|
const instrucitonsPerFunctionStdDev = 10
|
|
|
|
|
|
2021-11-22 20:33:22 -08:00
|
|
|
func Mutate(prog *vm.Program) {
|
2021-11-22 21:34:55 -08:00
|
|
|
target := int(rand.NormFloat64()*instrucitonsPerFunctionStdDev+instructionsPerFunctionMean)
|
2021-11-22 20:33:22 -08:00
|
|
|
|
|
|
|
|
if len(prog.Functions[0].Instructions) < target {
|
|
|
|
|
addInstruction(prog, prog.Functions[0])
|
|
|
|
|
} else {
|
|
|
|
|
removeInstruction(prog, prog.Functions[0])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func addInstruction(prog *vm.Program, fnc *vm.Function) {
|
|
|
|
|
if len(fnc.Instructions) == 0 {
|
|
|
|
|
fnc.Instructions = append(fnc.Instructions, gen.RandInstruction(prog))
|
|
|
|
|
} else {
|
|
|
|
|
i := rand.Intn(len(fnc.Instructions))
|
|
|
|
|
fnc.Instructions = append(fnc.Instructions[:i+1], fnc.Instructions[i:]...)
|
|
|
|
|
fnc.Instructions[i] = gen.RandInstruction(prog)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func removeInstruction(prog *vm.Program, fnc *vm.Function) {
|
|
|
|
|
if len(fnc.Instructions) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i := rand.Intn(len(fnc.Instructions))
|
|
|
|
|
fnc.Instructions = append(fnc.Instructions[:i], fnc.Instructions[i+1:]...)
|
|
|
|
|
}
|