Skip to main content

Dependency Structure Matrix Genetic Algorithm II - A fast genetic algorithm with linkage learning

Project description

DSMGA2 Python Bindings

Python bindings for DSMGA2 (Dependency Structure Matrix Genetic Algorithm II), a fast genetic algorithm with automatic linkage learning.

Installation

pip install dsmga2

Or build from source:

pip install maturin
maturin develop --release

Quick Start

from dsmga2 import Dsmga2, OneMax

# Create optimizer for 100-bit OneMax problem
optimizer = Dsmga2(problem_size=100, fitness_function=OneMax())
optimizer.population_size = 200
optimizer.max_generations = 1000
optimizer.seed = 42

# Run optimization
result = optimizer.run()
print(f"Best fitness: {result.best_fitness}")
print(f"Generations: {result.generation}")
print(f"Evaluations: {result.num_evaluations}")

Built-in Fitness Functions

  • OneMax(): Counts the number of 1s in the chromosome
  • MkTrap(k=5): m-k Trap function with deceptive blocks

Custom Fitness Functions

You can implement custom fitness functions in Python:

class CustomFitness:
    def evaluate(self, genes):
        """
        Evaluate fitness of a chromosome.

        Args:
            genes: List of booleans representing the chromosome

        Returns:
            float: Fitness value (higher is better)
        """
        # Your fitness evaluation logic here
        return sum(genes)

    def optimum(self, length):
        """Return the optimal fitness value for this problem size."""
        return float(length)

# Use custom fitness
optimizer = Dsmga2(problem_size=100, fitness_function=CustomFitness())
result = optimizer.run()

API Reference

Dsmga2

Main optimizer class.

Constructor:

Dsmga2(problem_size: int, fitness_function: FitnessFunction)

Attributes:

  • population_size: int - Population size (default: problem_size)
  • max_generations: int - Maximum generations (default: -1, unlimited)
  • seed: int - Random seed (default: 42)

Methods:

  • run() -> OptimizationResult - Run optimization until convergence or max generations
  • step() -> bool - Run one generation, returns True if not converged
  • best_fitness() -> float - Get current best fitness
  • generation() -> int - Get current generation number

OptimizationResult

Result object returned by run().

Attributes:

  • best_fitness: float - Best fitness found
  • generation: int - Number of generations run
  • num_evaluations: int - Total fitness evaluations
  • mean_fitness: float - Mean population fitness
  • converged: bool - Whether algorithm converged

License

MIT OR Apache-2.0

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

dsmga2-0.1.0.tar.gz (43.2 kB view details)

Uploaded Source

Built Distribution

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

dsmga2-0.1.0-cp312-cp312-macosx_11_0_arm64.whl (297.4 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

File details

Details for the file dsmga2-0.1.0.tar.gz.

File metadata

  • Download URL: dsmga2-0.1.0.tar.gz
  • Upload date:
  • Size: 43.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.10.2

File hashes

Hashes for dsmga2-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4e8ad2080b1b99a33273b5e6f8868c6c5c27082482dc55a174da22d2a33a8ae6
MD5 1f8c33dcfe68602fc7df5f373d56a85c
BLAKE2b-256 850477ea2831044fa77e1b0cc00f82427fda0df3687ee9cde2a11c6561f2b530

See more details on using hashes here.

File details

Details for the file dsmga2-0.1.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for dsmga2-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f0ff499247a6f1ed6dd018b07c61581c78b5f6a8d17572a414d3230337d81d68
MD5 4e9c8bd60657b796eecf1515a6de3322
BLAKE2b-256 c5636f94f48cee6a086fbcfe996cd675b0353c42a881abc1b45563f9bd14a419

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