GithubHelp home page GithubHelp logo

charon25 / genetictspsolver Goto Github PK

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

Genetic algorithm to find an approximate solution of the Travelling Salesman Problem.

License: MIT License

Python 100.00%
evolutionary-algorithms genetic-algorithm python travelling-salesman-problem

genetictspsolver's Introduction

Genetic Travelling Salesman Problem solver

This repo contains a genetic algorithm (GA) designed to solve the Travelling Salesman Problem (or TSP).

It works by repeatedly generating new paths from previous one in an attempt to improve them.

Install

To install the package :

git clone https://github.com/charon25/GeneticTSPSolver.git
cd GeneticTSPSolver
python setup.py install --user

This package requires numpy (>= 1.21.2) and tqdm (>= 4.62.3).

Usage

To use the solver, just import the package and instantiate the class :

from gentsp import TSPSolver

solver = TSPSolver(distances)

solver.pass_generations(n_generations=100)

The solver has only one mandatory argument : a square distances matrix which indicates the distance between every two pair of points. Everything else is either optional or deduced.

Optional keyword arguments

  • population_size (int, value > 0, default: 100): size of the population of the GA.
  • mutation_rate (float, 1.0 >= value >= 0, default: 0.05): probability of mutation of every individual
  • new_individuals (int, value >= 0, default: 0): number of new random individuals in each generation (introduces novelty).
  • elitism (int, value >= 0, default: population_size / 5): number of individuals to keep between every generation (only the best are kept).
  • selection (str, 'best' or 'weighted', default: 'weighted'): method to chose the individuals which will breed:
    • best will use the best one (sort by increasing distance)
    • weighted will pick randomly with a probability proportional to the inverse of the distance
  • breeder_count (int, value > 0, default: population_size / 2): number of individual who will breed.

Example :

solver = TSPSolver(distances, elitism=30, population_size=400, selection='best', breeder_count=30)

Statistics

The function pass_generations return a dictionary with the following statistics (each as a list of the value for each generation) :

  • best_fitness, average_fitness, worst_fitness
  • best_distance, average_distance, worst_distance
  • fitness_stdev: standard deviation of the fitnesses
  • best_individual, average_individual (the closest to the average fitness), worst_individual

Examples

With 25 random points (0 <= x < 100, 0 <= y < 100) and the following code :

solver = TSPSolver(distances, elitism=20)

plot

With 50 random points (0 <= x < 100, 0 <= y < 100) and the following code :

solver = TSPSolver(distances, population_size=300, elitism=20, new_individuals=10)

plot

genetictspsolver's People

Contributors

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