An individual is one candidate solution in a genetic algorithm. Its genes encode traits as a vector or sequence, while its fitness measures solution quality. section 2.5.3 section 3.3.1
Common encodings include:
- A binary individual stores genes in a NumPy row, with each gene initialized to 0 or 1. section 2.5.3
- A traveling-salesman individual stores destination indexes as a permutation describing the route. section 3.3.1
- An evaluation function assigns fitness; for a route, fitness is the summed distance around the path back to its start. section 3.3.1
A route individual
For destination points indexed from 0 onward, [0, 3, 1, 2] represents a route. The path can be drawn by connecting those indexed points and evaluated by summing consecutive distances plus the return distance. section 3.3.1
Listing 2.14 EDL_2_5_Genetic_Algorithms.ipynb: Creating the population
population = 100 #1 genes = 100 #2 generations = 100 #3 pop = np.random.randint(0,2, size=(population,genes)) #4 print(pop) ======================== [[0 1 1 ... 1 0 0] #5 [1 1 0 ... 1 1 0] [1 0 0 ... 1 0 1] ... [1 0 1 ... 0 0 1] [1 0 0 ... 0 1 0] [1 0 0 ... 0 0 1]]
Inspect the NumPy construction that creates a population whose rows are binary individuals. section 2.5.3
Listing 3.16 EDL_3_3_TSP.ipynb: Filling the toolbox
toolbox = base.Toolbox()
toolbox.register("indices", random.sample,
range(destinations), destinations) #1
toolbox.register("individual", tools.initIterate,
creator.Individual, toolbox.indices) #2
toolbox.register("population", tools.initRepeat,
list, toolbox.individual) #3
Inspect the DEAP toolbox registrations that generate individuals and repeat them into a population. section 3.3.1


