feat(ga, api): Implement advanced GA strategy and refactor API for v0.4.0 (#16)
All checks were successful
Publish Python Package to PyPI / deploy (push) Successful in 18s

This commit introduces a major enhancement to the genetic algorithm's convergence logic and refactors key parts of the API for better clarity and usability.

- **feat(ga):** Re-implements the GA solver (CPU & CUDA) to use a more robust strategy based on Elitism, Crossover, and Mutation. This replaces the previous, less efficient model and is designed to significantly improve accuracy and convergence speed.

- **feat(api):** Updates `GA_Options` to expose the new GA strategy parameters:
    - Renames `mutation_percentage` to `mutation_strength` for clarity.
    - Adds `elite_ratio`, `crossover_ratio`, and `mutation_ratio`.
    - Includes a `__post_init__` validator to ensure ratios are valid.

- **refactor(api):** Moves `quadratic_solve` from a standalone function to a method of the `Function` class (`f1.quadratic_solve()`). This provides a cleaner, more object-oriented API.

- **docs:** Updates the README, `GA_Options` doc page, and `quadratic_solve` doc page to reflect all API changes, new parameters, and updated usage examples.

- **chore:** Bumps version to 0.4.0.

Reviewed-on: #16
Co-authored-by: Jonathan Rampersad <rampersad.jonathan@gmail.com>
Co-committed-by: Jonathan Rampersad <rampersad.jonathan@gmail.com>
This commit was merged in pull request #16.
This commit is contained in:
2025-10-27 14:20:56 +00:00
committed by Jonathan Rampersad
parent 0536003dce
commit c3b3513e79
4 changed files with 237 additions and 107 deletions

View File

@@ -43,12 +43,12 @@ pip install polysolve[cuda12]
Here is a simple example of how to define a quadratic function, find its properties, and solve for its roots.
```python
from polysolve import Function, GA_Options, quadratic_solve
from polysolve import Function, GA_Options
# 1. Define the function f(x) = 2x^2 - 3x - 5
# Coefficients can be integers or floats.
f1 = Function(largest_exponent=2)
f1.set_constants([2, -3, -5])
f1.set_coeffs([2, -3, -5])
print(f"Function f1: {f1}")
# > Function f1: 2x^2 - 3x - 5
@@ -70,7 +70,7 @@ print(f"2nd Derivative of f1: {ddf1}")
# 5. Find roots analytically using the quadratic formula
# This is exact and fast for degree-2 polynomials.
roots_analytic = quadratic_solve(f1)
roots_analytic = f1.quadratic_solve()
print(f"Analytic roots: {sorted(roots_analytic)}")
# > Analytic roots: [-1.0, 2.5]
@@ -89,6 +89,32 @@ print(f"Approximate roots from GA: {roots_ga[:2]}")
---
## Tuning the Genetic Algorithm
The `GA_Options` class gives you fine-grained control over the genetic algorithm's performance, letting you trade speed for accuracy.
The default options are balanced, but for very complex polynomials, you may want a more exhaustive search.
```python
from polysolve import GA_Options
# Create a config for a much deeper, more accurate search
# (slower, but better for high-degree, complex functions)
ga_accurate = GA_Options(
num_of_generations=50, # Run for more generations
data_size=500000, # Use a larger population
elite_ratio=0.1, # Keep the top 10%
mutation_ratio=0.5 # Mutate 50%
)
# Pass the custom options to the solver
roots = f1.get_real_roots(ga_accurate)
```
For a full breakdown of all parameters, including crossover_ratio, mutation_strength, and more, please see [the full GA_Options API Documentation](https://polysolve.jono-rams.work/docs/ga-options-api).
---
## Development & Testing Environment
This project is automatically tested against a specific set of dependencies to ensure stability. Our Continuous Integration (CI) pipeline runs on an environment using **CUDA 12.5** on **Ubuntu 24.04**.