Exemplo n.º 1
0
func NewExperiment(ctx neat.Context, cfg neat.ExperimentSettings, t int) (exp *neat.Experiment, err error) {

	// Create the experiment
	exp = &neat.Experiment{ExperimentSettings: cfg}
	exp.SetContext(ctx)

	// Restore the saved setting and, if available, state
	if *ConfigName == "" {
		*ConfigName = os.Args[0] // Use the executable's name
	}
	rst := &archiver.File{
		FileSettings: ConfigSettings{path: *ConfigPath, name: *ConfigName},
	}
	if err = rst.Restore(ctx); err != nil {
		return
	}

	// Update helpers with trial number
	if t > NoTrials {
		hs := []interface{}{
			ctx.Archiver(),
			ctx.Comparer(),
			ctx.Crosser(),
			ctx.Decoder(),
			ctx.Evaluator(),
			ctx.Generator(),
			ctx.Mutator(),
			ctx.Searcher(),
			ctx.Speciater(),
			ctx.Visualizer(),
		}
		for _, h := range hs {
			if th, ok := h.(neat.Trialable); ok {
				th.SetTrial(t)
			}
		}
	}

	// Load ids and innovations
	if ph, ok := ctx.(neat.Populatable); ok {
		ph.SetPopulation(exp.Population())
	}
	return
}
Exemplo n.º 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
}