GithubHelp home page GithubHelp logo

arbazcodes / travelling-salesman-problem-genetic-algiorthm-with-crossover-and-mutation Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 23 KB

Travelling Salesman Problem-Genetic Algiorthm with Crossover and Mutation

Python 100.00%
ai evolutionary-algorithms genetic genetic-algorithm travelling-salesman-problem tsp-genetic-algorithm tsp-solver crossover mutation tournament-selection

travelling-salesman-problem-genetic-algiorthm-with-crossover-and-mutation's Introduction

Travelling Salesman Problem-Genetic Algiorthm with Crossover and Mutation

This code implements a genetic algorithm to solve the travelling salesman problem for a given graph. The algorithm works as follows:

  • First, a population of random genomes is initialized, where each genome is a random path in the graph.

  • Then, the fitness of each genome is calculated as the total distance travelled on the path.

  • The genetic algorithm is then applied to the population for a number of generations.

  • In each generation, the fittest individuals are selected using tournament selection.

  • Then, crossover is applied to the selected individuals to create new offspring.

  • Finally, mutation is applied to the offspring with a given mutation rate.

  • The new offspring replace the weakest individuals in the population.

  • This process is repeated for a number of generations until a stopping criterion is reached (in this case, a maximum number of generations is defined).

  • The fittest individual in the final population is returned as the solution to the travelling salesman problem.

The code defines several functions to implement the genetic algorithm, including:

  • fitness(): a function to calculate the fitness of a given genome (i.e. the total distance travelled on the path).

  • select_population(): a function to select a random sample of individuals from a given population.

  • tournament_selection(): a function to select the fittest individuals from a given population using tournament selection.

  • ordered_crossover(): a function to apply ordered crossover to two parent genomes and generate new offspring.

  • mutation(): a function to apply mutation to a given genome.

  • total_population_score(): a function to calculate the total score of a given population (i.e. the sum of all the fitness scores of the individuals in the population).

  • generate_path(): a function to generate a random path in the graph.

  • initialize_population(): a function to initialize a random population of given size.

The code is tailored to solve the travelling salesman problem for a specific graph defined in the code. The graph has six cities labelled "PISKLM" and the distances between the cities are defined by a 6x6 adjacency matrix. However, it is easy to make your own changes by inititalizing a different graph and changing the relevant parameters. You can also change the genetic parameters to see how they affect the convergence to a solution.

travelling-salesman-problem-genetic-algiorthm-with-crossover-and-mutation's People

Contributors

arbazcodes avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.