コード例 #1
0
ファイル: ackley_test.go プロジェクト: cbarrick/evo
func TestAckley(t *testing.T) {
	fmt.Printf("Minimize the Ackley function with n=%d\n", dim)

	// Setup:
	// We initialize a set of 40 random solutions,
	// then add them to a generational population.
	seed := make([]evo.Genome, 40)
	for i := range seed {
		seed[i] = &ackley{
			gene:  real.Random(dim, 30),
			steps: real.Random(dim, 1),
		}
	}
	var pop gen.Population
	pop.Evolve(seed, Evolve)

	// Continuously print statistics while the optimization runs.
	pop.Poll(0, func() bool {
		count.Lock()
		n := count.n
		count.Unlock()
		stats := pop.Stats()

		// "\x1b[2K" is the escape code to clear the line
		// The fitness of minimization problems is negative
		fmt.Printf("\x1b[2K\rCount: %7d | Max: %8.3g | Mean: %8.3g | Min: %8.3g | RSD: %9.2e",
			n,
			-stats.Min(),
			-stats.Mean(),
			-stats.Max(),
			-stats.RSD())

		return false
	})

	// Terminate after 200,000 fitness evaluations.
	pop.Poll(0, func() bool {
		count.Lock()
		n := count.n
		count.Unlock()
		return n > 200000
	})

	// Terminate if the standard deviation is low.
	pop.Poll(0, func() bool {
		stats := pop.Stats()
		return stats.SD() < precision
	})

	pop.Wait()
	selector.Close()
	best := seed[0]
	bestFit := seed[0].Fitness()
	for i := range seed {
		fit := seed[i].Fitness()
		if fit > bestFit {
			best = seed[i]
			bestFit = fit
		}
	}
	fmt.Println("\nSolution:", best)
}