Giter Club home page Giter Club logo

geneticalgorithmtsp's Introduction

GeneticAlgorithmTSP

Genetic algorithm code for solving Travelling Salesman Problem

Programming Language : Python

Number of cities : 11

General flow of solving a problem using Genetic Algorithm

            {
              initialize population;
              evaluate population;
              while TerminationCriteriaNotSatisfied
                  {
                    select parents for reproduction;
                    perform recombination and mutation;
                    evaluate population;
                  }
            }

run the following lines in terminal before proceeding :

pip install -r requirements.txt
conda install basemap
or run bash setup.sh (if running a linux based system)

pass this data frame to the genetic algorithm function as dist_mat

data = pd.read_csv("data/cities_and_distances.csv")
data.reset_index(inplace=True)
data1 = data.iloc[:,2:]
data1.index = data1.columns.values
data1

Initialize population: The initial population is a set of random routes generated using numpy.

Evaluate population: The evaluation is a process of finding how good the solutions is. This is

Mutation:

Crossover: Implemented PMX by goldberg - https://www.hindawi.com/journals/cin/2017/7430125/

OverallRun:

      ga_obj = GeneticAlgoLibrary.OverallGaRun(noverall=1,
                                     number_of_cities=11,
                                     initial_pop_size=1000,
                                     nelite=10,
                                     percentage_to_crossover=20,
                                     percentage_to_mutate=20,
                                     dist_mat=data1)

If you want to run the genetic algorithm with multiple runs

  for i in [10,100,1000]:
      ga_obj = GeneticAlgoLibrary.OverallGaRun(noverall=i,
                                           number_of_cities=11,
                                           initial_pop_size=10000,
                                           nelite=10,
                                           percentage_to_crossover=20,
                                           percentage_to_mutate=20,
                                           dist_mat=data1)
      ga_obj.run_overall_ga()

The solution obtained from running Genetic algorithm

Starting:

Final solution plotted with folium

Note:

1. The final solution was obtained after multiple runs of the Genetic Algorithm with different inital population sizes and overall runs.

_2. The map needs access to city_lat_lon data, in the code to the file that is availabe in the data folder, if you are running the code from a different folder, the path should be changed accordingly.

_3. basemap is now deprecated, will update the plots with new code when I find a package which can generate these kind of plots, if you have any suggestions, please forward them to Chaithanya Kumar

Update: Added plot support with folium and branca, updated the requirements file and lat lon file with required format data.

Update 20-04-2021: Fitness value calculation is now using ray multiprocessing to speed up the process.

geneticalgorithmtsp's People

Contributors

sck22 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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