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, clone this repository and use pip
for installation:
pip install gabits
or
git clone https://github.com/diogomachado-bioinfo/gabits.git
cd gabits
pip install .
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file gabits-1.0.0.0.tar.gz
.
File metadata
- Download URL: gabits-1.0.0.0.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6dd1ec1f35c0a7a28a66a932e0252c96bca1182fed8b9a3728e4fbfcd257edbb |
|
MD5 | d72a887dd35aa34ff361f04bac3d7954 |
|
BLAKE2b-256 | 5ecf463ae62b7db723af92f376f08d596a5c7e1660f1a405ec2c478f13f414da |
File details
Details for the file gabits-1.0.0.0-py3-none-any.whl
.
File metadata
- Download URL: gabits-1.0.0.0-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c6b107db1088df73c2eb4665156ceed3c88d56ff731c3a4868040bb2be794fdc |
|
MD5 | a88c11d0408744cf4d69a2ab1aa9ac53 |
|
BLAKE2b-256 | cfc39e33068730b950c03551227e153cfe556fbc40d8c502f726e2be4d782682 |