population_cur = init_population()
# Calculate the fitness value of the current population
fitness = get_fitness(population_cur)
while does not meet the termination condition:
# Keep part of the elite parent
population_next = select_sorted_population(fitness, population_cur, elite_size)
# Hybrid
for i in range(population_size):
# Here you can add the hybridization probability yourself
p1, p2 = selection(fitness, 2) # Use roulette selection operator to randomly select two as parents
child1, child2 = crossover(population_cur[p1], population_cur[p2])
# Mutate children
if random.random() < p_mutation:
child1 = mutations.select_best_mutaion(child1, distmat)
if random.random() < p_mutation:
child2 = mutations.select_best_mutaion(child2, distmat)
population_next.append(child1)
population_next.append(child2)
# At this time, there are a total population size of five quarters of individuals, from which the next generation population is selected
population_next = select_sorted_population(get_fitness(population_next), population_next, population_size)
# Replacement
population_cur = population_next
For an individual A to be mutated, separatelySliding variation,Flip mutation,irgibnnm variation, The one with the best three variants1
Randomly generate two subscripts a, b (a<b); move s[a] behind s[b]
Demo animation:

Generate two subscripts a, b at random; reverse the sequence between s[a] and s[b]
Flip mutation is also called 2 transform method, and demonstrates the animation:

First perform a flip mutation on the sequence; randomly select a city and exchange it to the nearest city to the map distance, the neighborhood in the literature is ±5
The experimental results of this mutation method do not seem to be as good as the results shown in the literature, may I have implemented it wrong? Demo animation:

Just run the gsp_ga.py file
The data set uses 78 points2, About 10s running on my computer, the optimal distance in the impression is about 5400m

Genetic algorithm solving TSP issues 1 algorithm principle The principle of algorithm refers to this website, this article focuses on implementation How to explain the genetic algorithm? What example?...
Articles directory Code Genome gene class GeneticalGorithm_TSP genetic algorithm class operation result Code Genome gene class GeneticalGorithm_TSP genetic algorithm class operation result Supplement:...
From a classification perspective, genetic algorithms are a type of heuristic algorithms. At the same time, two references can be provided for various precise solutions: (1) Initial solution; (2) Esti...
source: Implementing genetic algorithm on matlab to solve TSP traveler problem-CSDN blog https://blog.csdn.net/cordova/article/details/64912680?locationNum=1&fps=1 The TSP problem refers to traver...
Particle swarm optimization solves TSP problem 1. Introduction to particle swarm optimization Particle swarm optimization (PSO)Proposed by Kennedy and Eberhart in 1995, it is a type of evolutionary al...
Question: The traveling salesman wants to go to 20 cities (in this question, the location of each city is randomized) to sell goods. Which path will he take the shortest? Steps to solve the problem: 1...
The principle of genetic algorithm refers to Wikipedia:https://zh.wikipedia.org/wiki/%E9%81%97%E4%BC%A0%E7%AE%97%E6%B3%95 Flow chart of genetic algorithm: The idea and process of genetic algorithms ar...
1. Problem description Travelling Salesman Problem (Travelling Salesman Problem, TSP in abbreviation, also known as the salesman problem): Set n cities and a distance matrix D=[dij], where dij represe...
As a result, it feels that it has not reached the global optimum. It may have reached the local optimum. There is still room for optimization. data set:...
What is the variable in the genetic algorithm? according tocreateGenerate functions available, the variables in the genetic algorithm areAll-aloud. In other words, the genetic algorithm has beenFeasib...