Skip to content

Issue #8 – Optimization Infrastructure & Genetic Algorithms (GA) #8

Description

@D4R102004

Description: Implement the Genetic Algorithm (GA) in optimization/genetic.py. This module introduces the "Heavy Weaponry" of the library: Numpy. By representing our population as a 2D matrix, we can evaluate fitness, perform crossovers, and apply mutations to hundreds of individuals at once using vectorized operations.

Objectives:

Establish the pattern for Soft Imports of Numpy so the library remains lightweight for users who only need core search features.

Implement a Vectorized Fitness Evaluation system.

Implement the three pillars of evolution: Tournament Selection, Crossover (Recombination), and Mutation.

Support both binary strings and real-valued (floating point) genotypes.

Tasks:

[ ] Implement Soft Numpy Imports:

    In optimization/__init__.py, use try/except ImportError to handle environments without Numpy, providing clear warnings.

[ ] Develop optimization/genetic.py:

    evolve(fitness_func, pop_size, genome_len, generations): The main loop.

    Selection: Implement "Tournament Selection"—randomly pick k individuals and select the best.

    Crossover: Implement "Single-Point Crossover" using Numpy slicing for speed.

    Mutation: Use np.random.rand() to flip bits or add Gaussian noise to genes.

[ ] Vectorization Logic:

    Ensure the fitness_func can receive the entire population matrix and return a 1D array of scores in one call.

[ ] Unit Testing:

    Create tests/test_optimization.py.

    Use GA to solve a simple optimization problem, such as maximizing the number of "1s" in a bitstring (OneMax problem).

Acceptance Criteria:

[ ] The algorithm can evolve a population of 1,000 individuals for 100 generations in under a few seconds.

[ ] The crossover and mutation operations are implemented using Numpy broadcasting (no explicit Python for loops where possible).

[ ] The code includes a NumpyStateMixin in core.py (as per the spec) to handle hashing of Numpy-based states.

Dependencies:

Issue #1 (Project Initialization)

Issue #2 (Core Architecture)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions