GA solves TSP problem

Genetic algorithm solves TSP problem

Pseudo code for the core

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

Use a better variant

For an individual A to be mutated, separatelySliding variationFlip mutationirgibnnm variation, The one with the best three variants1

Sliding variation

Randomly generate two subscripts a, b (a<b); move s[a] behind s[b]

Demo animation:

Flip mutation

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:

irgibnnm variation

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:
ir

Operation and results

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

The complete code of genetic algorithm

github complete code

reference


  1. Improving TSP Solutions Using GA with a New Hybrid Mutation Based on Knowledge and Randomness

  2. Github simulated annealing solves tsp problem

Intelligent Recommendation

Genetic Algorithm Solving TSP Problem - GA Algorithm Realization

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?...

[Optimization of operations] GA genetic algorithm solve TSP problem (Java implementation)

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:...

【Combination Optimization】Traveling Salesman Problem(TSP)-Genetic Algorithm[GA]

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...

TSP based on GA

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

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...

More Recommendation

Genetic algorithm solves traveling salesman problem (TSP)

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...

Genetic algorithm solves TSP problem (Pyhton code)

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...

Simulated annealing algorithm solves TSP problem

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...

Simulated annealing solves TSP problem python implementation

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:...

[Combination Optimization] Traveler Problem Traveling Salesman Problem (TSP) - Genetic Algorithm [GA]

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...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top