I am a beginner in Python and DEAP and I am trying to understand the evaluatiobn function for the TSP from: https://github.com/DEAP/deap/blob/master/examples/ga/tsp.py
def evalTSP(individual):
distance = distance_map[individual[-1]][individual[0]]
for gene1, gene2 in zip(individual[0:-1], individual[1:]):
distance += distance_map[gene1][gene2]
return distance,
On distance = distance_map[individual[-1]][individual[0]]
The [individual[-1]][individual[0]]
means the difference between the previous and the current individual positions?
On for gene1, gene2 in zip(individual[0:-1], individual[1:])
Which means the values 0:1
and 1:
of individual?
Each individual
is represented as an array
. individual[-1]
and individual[0]
refer respectively to the last and the first gene of the individual (i.e element in the array). So distance_map[individual[-1]][individual[0]]
is the distance between these two genes as stored in the DistanceMatrix.
individual[0:-1]
returns an array with all the genes of individual
except for the last one. individual[1:]
returns an array with all the genes of individual
except for the first one.