Example #1
0
func main() {
	rand.Seed(time.Now().UTC().UnixNano())

	param := ga.GAParameter{
		Initializer: new(ga.GARandomInitializer),
		Selector:    ga.NewGATournamentSelector(0.2, 5),
		Breeder:     new(ga.GA2PointBreeder),
		Mutator:     ga.NewGAGaussianMutator(0.4, 0),
		PMutate:     0.5,
		PBreed:      0.2}

	gao := ga.NewGA(param)

	genome := ga.NewFloatGenome(make([]float64, 20), rosenbrock, 1, -1)

	gao.Init(1000, genome) //Total population

	gao.OptimizeUntil(func(best ga.GAGenome) bool {
		return best.Score() < 1e-3
	})

	best := gao.Best().(*ga.GAFloatGenome)
	fmt.Printf("%s = %f\n", best, best.Score())
	fmt.Printf("Calls to score = %d\n", scores)
}
Example #2
0
func main() {
	rand.Seed(time.Now().UTC().UnixNano())

	m := ga.NewMultiMutator()
	msh := new(ga.GAShiftMutator)
	msw := new(ga.GASwitchMutator)
	m.Add(msh)
	m.Add(msw)

	param := ga.GAParameter{
		Initializer: new(ga.GARandomInitializer),
		Selector:    ga.NewGATournamentSelector(0.7, 5),
		Breeder:     new(ga.GA2PointBreeder),
		Mutator:     m,
		PMutate:     0.1,
		PBreed:      0.7}

	gao := ga.NewGA(param)

	genome := ga.NewOrderedIntGenome([]int{10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, score)

	gao.Init(100, genome) //Total population
	gao.OptimizeUntil(func(best ga.GAGenome) bool {
		return best.Score() <= 680
	})
	gao.PrintTop(10)

	fmt.Printf("Calls to score = %d\n", scores)
	fmt.Printf("%s\n", m.Stats())
}
Example #3
0
func main() {
	rand.Seed(time.Now().UTC().UnixNano())

	m := ga.NewMultiMutator()
	msh := new(ga.GAShiftMutator)
	msw := new(ga.GASwitchMutator)
	m.Add(msh)
	m.Add(msw)

	param := ga.GAParameter{
		Initializer: new(ga.GARandomInitializer),
		Selector:    ga.NewGATournamentSelector(0.7, 5),
		Breeder:     new(ga.GA2PointBreeder),
		Mutator:     m,
		PMutate:     0.2,
		PBreed:      0.2}

	gao := ga.NewGA(param)

	genome := ga.NewFixedBitstringGenome(make([]bool, len(theset)), score)

	gao.Init(50, genome) //Total population

	for {
		gao.Optimize(1) // Run genetic algorithm for 20 generations.
		best := gao.Best().(*ga.GAFixedBitstringGenome)
		sum := 0
		if best.Score() == 0 {
			for n, value := range best.Gene {
				if value {
					fmt.Printf("%d,", theset[n])
					sum += theset[n]
				}
			}
			fmt.Printf(" = %d / %f\n", sum, best.Score())
			break
		}
	}
	fmt.Printf("Calls to score = %d\n", scores)
}