Exemple #1
0
// Generates the initial population
func generateFirst(ctx neat.Context, cfg ClassicSettings) (next neat.Population, err error) {
	// Create the first generation
	next = neat.Population{
		Generation: 0,
		Species:    make([]neat.Species, 1, 10),
		Genomes:    make([]neat.Genome, cfg.PopulationSize()),
	}

	// Create the genomes
	wg := new(sync.WaitGroup)
	for i := 0; i < len(next.Genomes); i++ {
		wg.Add(1)
		go func(i int) {
			genome := createSeed(ctx, cfg)
			genome.ID = ctx.NextID()
			genome.SpeciesIdx = 0
			next.Genomes[i] = genome
			wg.Done()
		}(i)
	}
	wg.Wait()

	// Create the initial species
	next.Species[0] = neat.Species{Example: next.Genomes[0]}

	return
}
Exemple #2
0
func createOffspring(ctx neat.Context, cfg ClassicSettings, cross bool, rng *rand.Rand, pool map[int]Improvements, cnts map[int]int, next *neat.Population) (err error) {
	var child neat.Genome
	for idx, cnt := range cnts {
		l := pool[idx]
		for i := 0; i < cnt; i++ {
			p1, p2 := pickParents(cfg, cross, rng, l, pool)
			if p1.ID == p2.ID {
				child = neat.CopyGenome(p1)
			} else {
				child, err = ctx.Crosser().Cross(p1, p2)
				if err != nil {
					return
				}
			}
			child.ID = ctx.NextID()
			child.Birth = next.Generation
			err = ctx.Mutator().Mutate(&child)
			next.Genomes = append(next.Genomes, child)
		}
	}
	return
}