Beispiel #1
0
func main() {
	flag.Parse()
	rand.Seed(time.Now().UTC().UnixNano())
	// Make the dummy population to train against
	dummies = evogo.NewPopulation(1000, 20, 20, CreateGene)

	var strongest *evogo.Individual
	if *train_com {
		pop := evogo.NewPopulation(100, 20, 20, CreateGene)
		evogo.Train(pop, 2198, Fitness, MutateGene)
		strongest = pop.Individuals()[0] // Get strongest individual
	} else {
		strongest = dummies.Individuals()[0]
	}

	for {
		// Start games
		var a, b string
		var pa, pb player
		fmt.Print("Player 1 is human or computer (h, c, q to quit): ")
		fmt.Scanf("%v", &a)
		if a == "q" {
			return
		}
		fmt.Print("Player 2 is human or computer (h, c, q to quit): ")
		fmt.Scanf("%v", &b)
		if b == "q" {
			return
		}

		if a == "h" {
			pa = human{}
		} else {
			pa = NewComputer(strongest, true)
		}
		if b == "h" {
			pb = human{}
		} else {
			pb = NewComputer(strongest, true)
		}

		winner := NewGame(pa, pb).play()
		fmt.Println("The winner is player", winner, "\n\n")
	}
}
Beispiel #2
0
func main() {
	rand.Seed(time.Now().UTC().UnixNano())
	// Generate a new populaation with
	// 100 Individuals
	// 13 Genes per individual
	// And our custom CreateGene function used to make the genes
	pop := evogo.NewPopulation(1000, 13, 13, CreateGene)
	pop.SetShowIndividual(ShowGenes) // Give it our show function

	// Call the train function and provide it with
	// The population to train
	// The target fitness at which to stop
	// fitness function to evaluate the fitness of a particular individual
	start := time.Now()
	evogo.Train(pop, 0, 1000, 40, fitness, MutateGene)
	elapsed := time.Since(start)
	fmt.Printf("Took %s\n", elapsed)
}