Skip to main content

OptivolutionPy

A flexible genetic algorithm library written purly in Python3.

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])

Meta

Mohamed Hisham – G-Mail | GitHub | LinkedIn

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

Metadata

Release files for OptivolutionPy 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for OptivolutionPy 1.0.1
File Size Uploaded
OptivolutionPy-1.0.1.tar.gz 4.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for OptivolutionPy 1.0.1
File Interpreter ABI Platform
OptivolutionPy-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 22.0 kB

Release files / OptivolutionPy-1.0.1.tar.gz

Download URL OptivolutionPy-1.0.1.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c24cbd7c38a665d55b4fb8b4094814be0e3947c09fe4a70b3dd6af2876c4ff2a
BLAKE2b-256 checksum
How to use checksums
98a29d8364cd3d442f1f715440f3c522697005994bba8dc4eabe2e2861ceef67
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.34.0 CPython/3.6.8

Release files / OptivolutionPy-1.0.1-py3-none-any.whl

Download URL OptivolutionPy-1.0.1-py3-none-any.whl
Size 17.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
64f7da38edfd55b1f313552c7f2ce3c65789c6186b4ec500dba90bde51bbb72d
BLAKE2b-256 checksum
How to use checksums
fe253b3c4f59bd1933b67af829eb72b153d58e60a290b6e1c68c0c0eadabc423
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.34.0 CPython/3.6.8

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page