Giter VIP home page Giter VIP logo

optivolutionpy's Introduction

OptivolutionPy

PyPI - Python Version PyPI version PyPI - Format PyPI downloads PyPI - License

Homepage | PyPI

A flexible genetic algorithm library written purely in Python3.

Check out this repo for an implementation in Java.

Installation

For python3, simply run:

$ pip3 install OptivolutionPy

Or clone this repository and run python3 setup.py install from within the project directory. e.g.:

$ git clone https://github.com/Mhmd-Hisham/OptivolutionPy.git
$ cd OptivolutionPy
$ python3 setup.py install

Advanced Example

Smart Ants using OptivolutionPy & Processing3. check this repo for more details.

SmartAntsDemo

Simple example

Solving the one-dimensional knapsack problem:

#!/usr/bin/env python3

import random

from optivolution.population import Population
from optivolution.chromosome import Chromosome

class OneDimensinalKnapsack(Chromosome):
    """ Inidividual knapsack object. """
    maximum_weight = 15
    knapsack_data = [{'name': 'box1', 'value': 4, 'weight': 12},
                     {'name': 'box2', 'value': 2, 'weight': 1},
                     {'name': 'box3', 'value': 10, 'weight': 4},
                     {'name': 'box4', 'value': 1, 'weight': 1},
                     {'name': 'box5', 'value': 2, 'weight': 2}]
    
    def __init__(self, genes_length=len(knapsack_data), genes=[]):
        super().__init__(genes_length, genes)
    
    @Chromosome.fitness_property
    def fitness(self):        
        """ Defining the fitness function. """
        # Use the knapsack value as the fitness value
        total_value = 0
        total_weight = 0
        
        for i in range(self.genes_length):
            if (self.genes[i] == True):
                total_value += self.knapsack_data[i]['value']
                total_weight += self.knapsack_data[i]['weight']
        
        if total_weight > self.maximum_weight:
            total_value = 0
        
        return total_value
    
    def random_gene(self):
        """ Defining the random gene. """
        return random.choice((0, 1))

class KnapscakPopulation(Population):
    tournament_sample_percentage = 10

    def random_individual(self):
        """ Defining the random individual in the population. """
        return OneDimensinalKnapsack()

def main():
    population = KnapscakPopulation(population_size=20)    
    population.run(20)
    
    print(f"Generation {population.generation_number}")
    best = population.get_best_individual()

    # The optimal answer for this test case is
    # (15, [0, 1, 1, 1, 1])
    print((best.fitness, best.genes))

if __name__ == "__main__":
    main()

Output:

(15, [0, 1, 1, 1, 1])

P.S. check the examples folder for more examples.

Meta

Mohamed Hisham โ€“ G-Mail | GitHub | LinkedIn

This project is licensed under the GNU GPLv3 License - check LICENSE for more details.

optivolutionpy's People

Contributors

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