Skip to main content

Genetic algorithm implementation based on binary representation

Project description

GAbits

gabits is a Python package that implements a genetic algorithm based on binary representation, along with utility functions for data manipulation and transformation. The code is based on the original implementation by Roberto T. Raittz and adapted to Python by Diogo de J. S. Machado.

Features

Genetic Algorithm

  • gago: Genetic algorithm for optimization, based on binary representation.

Utility Functions

  • mat2vec: Converts a matrix to a 1D array.
  • vec2mat: Creates a matrix from a 1D array.
  • bytes2bits: Converts a list of numbers or a single number to a bit array.
  • bits2bytes: Converts a bit array to bytes.

Installation

To install the package, use pip:

pip install gabits

Usage

Genetic Algorithm

Here's a basic example of using the gago function for optimization:

from gabits import gago, bits2bytes

# Options for the genetic algorithm
gaoptions = {
    "PopulationSize": 200,    # Population size
    "Generations": 50,        # Number of generations
    "InitialPopulation": [],  # Initial population (empty in this case)
    "MutationFcn": 0.15,      # Mutation rate
    "EliteCount": 2,          # Number of elite individuals to keep
}

# Fitness function for Genetic Algorithm
def fit_func(bits):
    """
    Fitness function for Genetic Algorithm.

    Objective
    --------
    Set integer values so that the following equation holds true:

    .. math:: x_1 + x_2 = |x_3 - x_4|

    Input
    -----
    64 bits (8 bytes). Each 16 bits (2 bytes) form an integer value.

    Output
    ------
    Absolute error from the expected result.
    """
    # Convert bit string to 16-bit integers
    X = bits2bytes(bits, 'int16').astype(int)
    # Calculate error based on integers
    error = abs((X[0] + X[1]) - abs(X[2] - X[3]))
    return error

# Run the genetic algorithm with the fitness function, 64 bits per individual
# and the defined options
result = gago(fit_func, 64, gaoptions)

# Extract bits from the best individual found by the genetic algorithm
bits = result[0]

# Convert the extracted bits into 16-bit integers
X = bits2bytes(bits, 'int16').astype(int)

# Print the result (the integer values found)
print('Result =', list(X))

# Calculate and print the error for the best individual found
print('Error =', fit_func(bits))

# Print the sum of the first two integer values found
print('x1 + x2 =', X[0] + X[1])

# Print the absolute value of the difference between the last two integer
# values found
print('|x3 - x4| =', abs(X[2] - X[3]))

Utility Functions

Here are examples demonstrating the utility functions:

mat2vec

import numpy as np
from gabits import mat2vec

mat = np.array([[1, 2, 3], [4, 5, 6]])
vec = mat2vec(mat)
print("Matrix converted to vector:", vec)

vec2mat

from gabits import vec2mat

vec = [1, 2, 3, 4, 5]
larg = 3
mat = vec2mat(vec, larg)
print("Vector converted to matrix:")
print(mat)

bytes2bits

from gabits import bytes2bits

num = 5
bits = bytes2bits(num)
print("Number converted to bits:", bits)

bits2bytes

from gabits import bits2bytes

bits = [1, 0, 1, 0, 0, 0, 0, 0]
bytes_ = bits2bytes(bits)
print("Bits converted to bytes:", bytes_)

Authors

  • Original Implementation: Roberto T. Raittz
  • Python Adaptation: Diogo de J. S. Machado

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gabits-1.0.0.1.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gabits-1.0.0.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file gabits-1.0.0.1.tar.gz.

File metadata

  • Download URL: gabits-1.0.0.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for gabits-1.0.0.1.tar.gz
Algorithm Hash digest
SHA256 8132c8fe23b6c147dba409ad2b719e775f5fcb9006a33b28ebe22ce744284b14
MD5 ad7611554ea4140a3b24e2334b694188
BLAKE2b-256 68fe1ca4f134e2fe0034cdbe78258e1ec0b5623856e0c28bf2b2de6df61c4ade

See more details on using hashes here.

File details

Details for the file gabits-1.0.0.1-py3-none-any.whl.

File metadata

  • Download URL: gabits-1.0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for gabits-1.0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f171a5bdadc20dd40f8b39ad25aff18f0b07dd6cac5b752c2f4fdc70c09905f8
MD5 c63b7ccfc794e5aca0d510ca8b1ef2ef
BLAKE2b-256 1568330dcf9d0985d9133d721c864fedfb4390061a15adcc03a4bd670f95f03f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page