Beispiel #1
0
func BenchmarkMutate(b *testing.B) {
	headSize := 7
	maxArity := 2
	tailSize := headSize*(maxArity-1) + 1
	numTerminals := 5
	numConstants := 5
	funcs := []gene.FuncWeight{
		{"+", 1},
		{"-", 5},
		{"*", 5},
	}
	g1 := gene.RandomNew(headSize, tailSize, numTerminals, numConstants, funcs)
	g2 := gene.RandomNew(headSize, tailSize, numTerminals, numConstants, funcs)
	g3 := gene.RandomNew(headSize, tailSize, numTerminals, numConstants, funcs)
	g4 := gene.RandomNew(headSize, tailSize, numTerminals, numConstants, funcs)
	g := New([]*gene.Gene{g1, g2, g3, g4}, "+")
	for i := 0; i < b.N; i++ {
		g.Mutate(1)
	}
}
Beispiel #2
0
func TestMutate(t *testing.T) {
	headSize := 7
	maxArity := 2
	tailSize := headSize*(maxArity-1) + 1
	numTerminals := 5
	funcs := []gene.FuncWeight{
		{"Not", 1},
		{"And", 5},
		{"Or", 5},
	}
	mux := New([]*gene.Gene{
		gene.RandomNew(headSize, tailSize, numTerminals, 0, funcs),
		gene.RandomNew(headSize, tailSize, numTerminals, 0, funcs),
		gene.RandomNew(headSize, tailSize, numTerminals, 0, funcs),
		gene.RandomNew(headSize, tailSize, numTerminals, 0, funcs),
	},
		"And")
	gn := mux.Dup()
	mux.Mutate(1)
	if err := checkEqual(gn, mux); err == nil {
		t.Errorf("TestMutate failed: gn == mux\n")
	}
}
Beispiel #3
0
// New creates a new random generation of the model.
// fs is a slice of function weights.
// fm is the map of available functions to use for creating the generation of the model.
// numGenomes is the number of genomes to use to populate this generation of the model.
// headSize is the number of head symbols to use in a genome.
// numGenesPerGenome is the number of genes to use per genome.
// numTerminals is the number of terminals (inputs) to use within each gene.
// numConstants is the number of constants (inputs) to use within each gene.
// linkFunc is the linking function used to combine the genes within a genome.
// sf is the scoring (or fitness) function.
func New(fs []gene.FuncWeight, fm functions.FuncMap, numGenomes, headSize, numGenesPerGenome, numTerminals, numConstants int, linkFunc string, sf genome.ScoringFunc) *Generation {
	r := &Generation{
		Genomes:     make([]*genome.Genome, numGenomes, numGenomes),
		Funcs:       fs,
		ScoringFunc: sf,
	}
	n := maxArity(fs, fm)
	tailSize := headSize*(n-1) + 1
	for i := range r.Genomes {
		genes := make([]*gene.Gene, numGenesPerGenome, numGenesPerGenome)
		for j := range genes {
			genes[j] = gene.RandomNew(headSize, tailSize, numTerminals, numConstants, fs)
		}
		r.Genomes[i] = genome.New(genes, linkFunc)
	}
	return r
}