Giter Site home page Giter Site logo

genetic_algorithm's Introduction

This is an implementation of a genetic algorithm in Python. The genetic algorithm is a search algorithm that is inspired by the process of natural selection. It is used to solve optimization problems by iteratively improving a population of candidate solutions.

Fitness Function

The fitness function is used to evaluate the quality of a candidate solution. It takes an array of integers and floats as input, and can be defined with bounds for each input. In our implementation, we have used a simple fitness function that calculates the sum of the inputs.

Genetic Algorithm

The genetic algorithm works by iteratively improving a population of candidate solutions. Each candidate solution is represented by a chromosome, which is a sequence of bits. The genetic algorithm consists of the following steps:

  1. Initialize the population: Create an initial population of candidate solutions by randomly generating chromosomes.
  2. Evaluate the fitness of each candidate solution: Use the fitness function to evaluate the quality of each candidate solution.
  3. Select parents for mating: Use tournament selection to select two parents for mating.
  4. Create offspring through crossover: Use two-point crossover to create two offspring from the parents.
  5. Mutate the offspring: Introduce random mutations to the offspring.
  6. Add the offspring to the new population: Add the offspring to the new population of candidate solutions.
  7. Replace the old population with the new population: Replace the old population with the new population of candidate solutions.
  8. Repeat steps 2-7 for a fixed number of generations.

Implementation Details

In our implementation, we have used the following parameters:

POPULATION_SIZE: The number of candidate solutions in each generation.

  1. MIN_CHROMOSOME_LENGTH: The minimum length of a chromosome.
  2. MAX_CHROMOSOME_LENGTH: The maximum length of a chromosome.
  3. TOURNAMENT_SIZE: The number of candidate solutions to consider in each tournament selection.
  4. MUTATION_RATE: The probability of introducing a random mutation to a bit in a chromosome.
  5. MUTATION_RATE_INCREASE: The amount by which to increase the mutation rate after each generation.
  6. NUM_GENERATIONS: The number of generations to run the genetic algorithm for. We have also defined the following functions:

fitness: The fitness function that evaluates the quality of a candidate solution. tournament_selection: The function that performs tournament selection to select parents for mating. two_point_crossover: The function that performs two-point crossover to create offspring from parents. mutation: The function that introduces random mutations to offspring.

Usage

To run the genetic algorithm, simply run the following command in the terminal:

python GA.py

This will run the genetic algorithm with the default parameters. If you want to change the parameters, you can modify the variables at the top of the genetic_algorithm.py file.

Conclusion

The genetic algorithm is a powerful optimization algorithm that can be used to solve a wide range of problems. In this implementation, we have shown how to use the genetic algorithm to solve an optimization problem with bounds on the inputs. This implementation can be easily extended to solve other optimization problems by defining a suitable fitness function.

genetic_algorithm's People

Contributors

shamonia avatar

Stargazers

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