Example #1
0
// Define a fitness function to evaluate an individual
// In our case we are just using distance between unicode runes
// The higher the fitness is the cloesr to our goal the individual is
func fitness(i *evogo.Individual, others []*evogo.Individual) int {
	var f int = 0 // Highest score it can get is 0
	for i, g := range i.Genes() {
		gene := g.(Gene) // Need to typecast it from the eve.Gene interface to our custom Gene type
		difference := int(goal[i]) - int(gene.value)
		if difference > 0 { // -abs
			difference = -difference
		}
		f += difference // Subtract off how far it is from the target
	}
	return f
}
Example #2
0
// Function to display an individual (optional),
// if provided each generation will show the highest fitness individual
func ShowGenes(i *evogo.Individual) {
	for _, g := range i.Genes() {
		gene := g.(Gene) // Typecast from evogo.Gene interface to our Gene struct
		fmt.Printf("%c", rune(gene.value))
	}
}
Example #3
0
func ShowGenes(i *evogo.Individual) {
	for _, g := range i.Genes() {
		gene := g.(Gene)
		fmt.Printf("%d ", gene.choice)
	}
}