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.2.0.tar.gz (43.5 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.2.0-cp314-cp314-macosx_11_0_arm64.whl (293.0 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

File details

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

File metadata

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

File hashes

Hashes for dsmga2-0.2.0.tar.gz
Algorithm Hash digest
SHA256 7fd3873fd5700e946f359eaa4407ca1ea8c2acf95d8f0d04386d3a8c589af97d
MD5 1ff95b6d79c1e0a1f379fe20186c4552
BLAKE2b-256 554800ffce425fe7e0e13871337b71c79a6bfb0ef83c34616f732c0b3455e7c8

See more details on using hashes here.

File details

Details for the file dsmga2-0.2.0-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for dsmga2-0.2.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5503c6a8cc5ed7de9a189595f92511364c57b53e2ba5c61e660fec4c249ffe21
MD5 246b7faeab512bcab6475e8def6ffa6f
BLAKE2b-256 f024b86c860cf77b361c0f8bb50d624968b08c99137e5eb2bec57550485f6a4d

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