0

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?

1
  • This is actually two different questions that each already have an answer on site. Commented Nov 6, 2019 at 10:29

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.