Educational genetic algorithm library for knapsack optimization problems.
Project description
Genetic Algorithms for Knapsack
Educational genetic algorithm toolkit with a FastAPI service for running knapsack optimization experiments.
This project provides a core GA library (ga_core) and a FastAPI service for running and tracking experiments on knapsack benchmarks.
Project Structure
This repository contains:
src/ga_core/– Core genetic algorithm library (publishable to PyPI)src/api/– FastAPI web service for running GA experimentsdane AG 2/– Benchmark knapsack problem instancestests/– Test suite
Installation
Library Only (for use in your code)
pip install ga-knapsack-toolkit
Full Development Setup
git clone https://github.com/maleckainez/GeneticAlgorithms.git
cd GeneticAlgorithms
python -m venv .venv
# Linux:
source .venv/bin/activate
# Windows:
.venv\Scripts\activate
# MacOS:
. .venv/bin/activate
pip install -e ".[dev,api]"
Install options:
- Base:
pip install .– Core library only .[api]– Include FastAPI dependencies.[dev]– Include testing & linting tools.[dev,api]- Install all dependencies and tools
Library Usage (ga_core)
Quick Start
from pathlib import Path
from src.ga_core import ExperimentConfig, InputConfig, ExperimentStorage
from src.ga_core.engine import EvolutionEngine
from src.ga_core.io import load_yaml_config, load_experiment_data
# Load configuration
config_path = Path("config.yaml")
input_config = load_yaml_config(config_path)
# Load knapsack data
data_path = Path("dane AG 2/low-dimensional/f1_l-d_kp_10_269")
items_data = load_experiment_data(data_path)
# Create experiment config
config = ExperimentConfig(
input=input_config,
job_id="experiment-001",
root_path=Path.cwd()
)
# Setup storage
from src.ga_core.storage import SimpleStorageLayout
layout = SimpleStorageLayout(root=config.root_path)
storage = ExperimentStorage(layout=layout, job_id=config.job_id)
# Run evolution
engine = EvolutionEngine(config=config, storage=storage, items_data=items_data)
engine.run()
Configuration (config.yaml)
data:
data_filename: "f1_l-d_kp_10_269"
max_weight: 269
population:
size: 100
generations: 500
stream_batch_size: 50
commit_mode: "swap" # or "copy"
selection:
type: "roulette" # roulette, tournament, linear_rank
selection_pressure: null # for rank selection: 1.0-2.0
tournament_size: null # for tournament: int
genetic_operators:
crossover_type: "two_point" # one_point, two_point
crossover_probability: 0.75
mutation_probability: 0.01
penalty_multiplier: 1.0
strict_weight_constraints: false
experiment:
seed: 42
identifier: "exp-001"
log_level: "INFO"
Web API
Running the API
# Development mode
fastapi dev src/api/main.py --reload --reload-dir src/
# Production mode
fastapi run src/api/main.py
API will be available at:
API Docs at:
API Endpoints
Health Check
GET /health
Submit GA Job
POST /backend/run/ga
Content-Type: application/json
{
"data": {"data_filename": "f1_l-d_kp_10_269", "max_weight": 269},
"population": {"size": 100, "generations": 500, ...},
...
}
Response: {"job_id": "job-uuid", "status": "pending"}
Check Job Status
GET /backend/status/{job_id}
Response: {"job_id": "uuid", "status": "running"}
List Jobs by Status
GET /backend/jobs/finished
Response: {"status": "finished", "jobs": ["uuid1", "uuid2", ...]}
🧪 Development
Running Tests
pytest
Code Quality
# Format code
black .
# Lint
ruff check .
# Type check
mypy .
Test Coverage
coverage run -m pytest
coverage report
📊 Dataset: dane AG 2
The repository includes benchmark knapsack problem instances:
dane AG 2/
├── large_scale/ # Large instances (knapPI_*)
├── large_scale-optimum/ # Known optimal values
├── low-dimensional/ # Smaller instances (f*)
└── low-dimensional-optimum/ # Known optimal values
File format: Each line contains value weight
Author: Inez Małecka – maleckainez@gmail.com
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ga_knapsack_toolkit-3.0.0.tar.gz.
File metadata
- Download URL: ga_knapsack_toolkit-3.0.0.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
213c682d890564659fcca606a144fad2425f25bde6ecf177d500a971990dd963
|
|
| MD5 |
dd68e6f800ed7ead76d5a490fc7ee48e
|
|
| BLAKE2b-256 |
69bba60e03263e3c2c697217513941f2db025da8a07bf5446938e86ef26b53e2
|
Provenance
The following attestation bundles were made for ga_knapsack_toolkit-3.0.0.tar.gz:
Publisher:
publish.yml on maleckainez/GeneticAlgorithms
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ga_knapsack_toolkit-3.0.0.tar.gz -
Subject digest:
213c682d890564659fcca606a144fad2425f25bde6ecf177d500a971990dd963 - Sigstore transparency entry: 923453947
- Sigstore integration time:
-
Permalink:
maleckainez/GeneticAlgorithms@6eac84b193a9279a54a708a1c075e84a2ff7e872 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/maleckainez
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6eac84b193a9279a54a708a1c075e84a2ff7e872 -
Trigger Event:
workflow_run
-
Statement type:
File details
Details for the file ga_knapsack_toolkit-3.0.0-py3-none-any.whl.
File metadata
- Download URL: ga_knapsack_toolkit-3.0.0-py3-none-any.whl
- Upload date:
- Size: 3.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
747d1ff61f7e599018b97284a75421054122459a037c7414ded8789ed256409c
|
|
| MD5 |
460841d9bd5c938566dac56ee5f25685
|
|
| BLAKE2b-256 |
59c5e628bcb3c7037638b7987e0ab670040481dbfbc8200b9c833fcb117fbef5
|
Provenance
The following attestation bundles were made for ga_knapsack_toolkit-3.0.0-py3-none-any.whl:
Publisher:
publish.yml on maleckainez/GeneticAlgorithms
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ga_knapsack_toolkit-3.0.0-py3-none-any.whl -
Subject digest:
747d1ff61f7e599018b97284a75421054122459a037c7414ded8789ed256409c - Sigstore transparency entry: 923453952
- Sigstore integration time:
-
Permalink:
maleckainez/GeneticAlgorithms@6eac84b193a9279a54a708a1c075e84a2ff7e872 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/maleckainez
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6eac84b193a9279a54a708a1c075e84a2ff7e872 -
Trigger Event:
workflow_run
-
Statement type: