示例#1
0
func TestSubtract(t *testing.T) {
	x := real.Random(8, 1)
	y := real.Random(8, 1)
	z := x.Copy()
	z.Subtract(y)
	for i := range z {
		if z[i] != x[i]-y[i] {
			t.Fail()
		}
	}
}
示例#2
0
func TestUniformX(t *testing.T) {
	mom := real.Random(8, 1)
	dad := real.Random(8, 1)
	child := make([]float64, 8)
	real.UniformX(child, mom, dad)
	for i := range child {
		if child[i] != mom[i] && child[i] != dad[i] {
			t.Fail()
		}
	}
}
示例#3
0
func TestAdd(t *testing.T) {
	x := real.Random(8, 1)
	y := real.Random(8, 1)
	z := x.Copy()
	z.Add(y)
	for i := range z {
		if z[i] != x[i]+y[i] {
			t.Fail()
		}
	}
}
示例#4
0
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.
	init := make([]evo.Genome, 40)
	for i := range init {
		init[i] = &ackley{
			gene:  real.Random(dim, 30),
			steps: real.Random(dim, 1),
		}
	}
	pop := gen.New(init)
	pop.Start()

	// Tear-down:
	// Upon returning, we cleanup our resources and print the solution.
	defer func() {
		pop.Close()
		selector.Close()
		fmt.Println("\nSolution:", evo.Max(pop))
	}()

	// Run:
	// We continuously poll the population for statistics and terminate when we
	// have a solution or after 200,000 evaluations.
	for {
		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())

		// We've converged once the deviation is within the precision
		if stats.SD() < precision {
			return
		}

		// Force stop after 200,000 fitness evaluations
		if n > 200000 {
			return
		}
	}
}
示例#5
0
func TestAdapt(t *testing.T) {
	x := real.Random(8, 1)
	y := x.Copy()
	y.Adapt()
	for i := range x {
		if x[i] == y[i] {
			t.Fail()
		}
	}
}
示例#6
0
func TestScale(t *testing.T) {
	x := real.Random(8, 1)
	y := x.Copy()
	y.Scale(3)
	for i := range y {
		if y[i] != x[i]*3 {
			t.Fail()
		}
	}
}
示例#7
0
func TestRandom(t *testing.T) {
	x := real.Random(8, 1)
	if len(x) != 8 {
		t.Fail()
		return
	}
	for i := range x {
		if x[i] < 0 || 1 < x[i] {
			t.Fail()
		}
	}
}
示例#8
0
func TestCopy(t *testing.T) {
	x := real.Random(8, 1)
	y := x.Copy()
	for i := range x {
		if x[i] != y[i] {
			t.Fail()
		}
	}
	x[0] = 0
	if x[0] == y[0] {
		t.Fail()
	}
}
示例#9
0
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)
}