Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions colonyx/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
colonyx: Swarm Intelligence Optimization Library

A Python library for solving optimization problems using swarm intelligence algorithms
like Ant Colony Optimization (ACO), Particle Swarm Optimization (PSO), and
Artificial Bee Colony (ABC) with a scikit-learn compatible interface.
"""

__version__ = "0.1.0"
__author__ = "Minh, Le Duc"
__email__ = "minh.leduc.0210@gmail.com"

# Import the main interface
from .auto import AutoColony

# Import individual algorithms (will be implemented)
# from .algorithms import AntColonyOptimizer, ParticleSwarmOptimizer, ArtificialBeeColonyOptimizer

# Import utilities (will be implemented)
# from .utils import check_optimization_problem, check_bounds

# Import datasets (will be implemented)
# from .datasets import load_tsp_data, benchmark_functions

__all__ = [
"AutoColony",
# "AntColonyOptimizer",
# "ParticleSwarmOptimizer",
# "ArtificialBeeColonyOptimizer",
# "check_optimization_problem",
# "check_bounds",
# "load_tsp_data",
# "benchmark_functions",
]
216 changes: 216 additions & 0 deletions colonyx/auto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
"""
AutoColony: Main interface for swarm intelligence optimization algorithms
"""

from typing import Optional, Union, Dict, Any, Callable
import numpy as np
from sklearn.base import BaseEstimator


class AutoColony(BaseEstimator):
"""
Unified interface for swarm intelligence optimization algorithms.

Similar to HuggingFace's AutoModel, this class provides a single interface
for multiple optimization algorithms selected via the 'mode' parameter.

Parameters
----------
mode : str, default='auto'
Algorithm selection mode:
- 'auto': Automatically select algorithm based on problem type
- 'aco': Ant Colony Optimization
- 'pso': Particle Swarm Optimization
- 'abc': Artificial Bee Colony

n_iterations : int, default=100
Number of iterations to run

random_state : int, default=None
Random seed for reproducibility

**kwargs : dict
Algorithm-specific parameters
"""

def __init__(
self,
mode: str = 'auto',
n_iterations: int = 100,
random_state: Optional[int] = None,
**kwargs
):
self.mode = mode
self.n_iterations = n_iterations
self.random_state = random_state

# Algorithm-specific parameters
self.kwargs = kwargs

# Internal state
self._fitted = False
self._best_solution = None
self._best_score = None
self._algorithm = None

# Validate mode
valid_modes = ['auto', 'aco', 'pso', 'abc']
if mode not in valid_modes:
raise ValueError(f"Invalid mode '{mode}'. Must be one of {valid_modes}")

def _detect_problem_type(self, X, y=None):
"""Auto-detect problem type for algorithm selection"""
if y is not None:
# Supervised learning problem - use PSO
return 'pso'
elif hasattr(X, 'shape') and len(X.shape) == 2:
# Distance matrix (TSP-like) - use ACO
if X.shape[0] == X.shape[1]:
return 'aco'
else:
return 'pso'
else:
# Default to PSO for continuous problems
return 'pso'

def _create_algorithm(self, algorithm_mode: str):
"""Create the appropriate algorithm instance"""

if algorithm_mode == 'aco':
# TODO: Import and create ACO instance
# from .algorithms import AntColonyOptimizer
# return AntColonyOptimizer(**self._filter_params('aco'))
raise NotImplementedError("ACO algorithm not yet implemented")

elif algorithm_mode == 'pso':
# TODO: Import and create PSO instance
# from .algorithms import ParticleSwarmOptimizer
# return ParticleSwarmOptimizer(**self._filter_params('pso'))
raise NotImplementedError("PSO algorithm not yet implemented")

elif algorithm_mode == 'abc':
# TODO: Import and create ABC instance
# from .algorithms import ArtificialBeeColonyOptimizer
# return ArtificialBeeColonyOptimizer(**self._filter_params('abc'))
raise NotImplementedError("ABC algorithm not yet implemented")

else:
raise ValueError(f"Unknown algorithm mode: {algorithm_mode}")

def _filter_params(self, algorithm_mode: str) -> Dict[str, Any]:
"""Filter parameters relevant to the specific algorithm"""
base_params = {
'n_iterations': self.n_iterations,
'random_state': self.random_state,
}

if algorithm_mode == 'aco':
aco_params = {
'n_ants': self.kwargs.get('n_ants', 50),
'alpha': self.kwargs.get('alpha', 1.0),
'beta': self.kwargs.get('beta', 2.0),
'rho': self.kwargs.get('rho', 0.5),
'q': self.kwargs.get('q', 1.0),
}
return {**base_params, **aco_params}

elif algorithm_mode == 'pso':
pso_params = {
'n_particles': self.kwargs.get('n_particles', 30),
'w': self.kwargs.get('w', 0.9),
'c1': self.kwargs.get('c1', 2.0),
'c2': self.kwargs.get('c2', 2.0),
}
return {**base_params, **pso_params}

elif algorithm_mode == 'abc':
abc_params = {
'n_bees': self.kwargs.get('n_bees', 50),
'limit': self.kwargs.get('limit', 10),
}
return {**base_params, **abc_params}

return base_params

def fit(self, X, y=None):
"""
Fit the optimizer to the problem

Parameters
----------
X : array-like or callable
Problem data (distance matrix, objective function, etc.)
y : array-like, optional
Target values for supervised problems

Returns
-------
self : AutoColony
Returns self for method chaining
"""
# Determine algorithm mode
if self.mode == 'auto':
algorithm_mode = self._detect_problem_type(X, y)
else:
algorithm_mode = self.mode

# Create algorithm instance
self._algorithm = self._create_algorithm(algorithm_mode)

# Fit the algorithm
# TODO: Implement actual fitting logic
self._fitted = True

return self

def predict(self):
"""
Get the best solution found

Returns
-------
solution : array-like
Best solution found by the algorithm
"""
if not self._fitted:
raise ValueError("Must call fit() before predict()")

# TODO: Return actual best solution
return self._best_solution

def score(self, X=None, y=None):
"""
Get the best score/fitness value

Returns
-------
score : float
Best score found by the algorithm
"""
if not self._fitted:
raise ValueError("Must call fit() before score()")

# TODO: Return actual best score
return self._best_score

def get_params(self, deep=True):
"""Get parameters for this estimator"""
params = {
'mode': self.mode,
'n_iterations': self.n_iterations,
'random_state': self.random_state,
}
params.update(self.kwargs)
return params

def set_params(self, **params):
"""Set parameters for this estimator"""
valid_params = self.get_params()

for key, value in params.items():
if key in ['mode', 'n_iterations', 'random_state']:
setattr(self, key, value)
else:
self.kwargs[key] = value

return self
39 changes: 39 additions & 0 deletions docs/NOTE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

## Algorithms

| Order | Algorithm | Type | Use Case | Notes |
| ----- | --------- | ---------- | ---------------------------- | --------------------- |
| 1 | ACO | Discrete | TSP, routing, scheduling | Good starter |
Expand All @@ -6,3 +9,39 @@
| 4 | Firefly | Continuous | Multimodal optimization | Easy to add after PSO |
| 5 | Glowworm | Continuous | Multi-solution discovery | Experimental |
| 6 | Bacterial | Mixed | Bio-inspired optimization | Complex |


## Directory

```

colonyx/
├── src/ # Rust core implementation
│ ├── lib.rs # Main library entry point
│ ├── algorithms/ # Algorithm implementations
│ │ ├── mod.rs # Algorithms module
│ │ ├── base.rs # Base traits and types
│ │ ├── aco.rs # Ant Colony Optimization
│ │ ├── pso.rs # Particle Swarm Optimization
│ │ └── abc.rs # Artificial Bee Colony
│ ├── core/ # Core optimization structures
│ │ ├── mod.rs
│ │ ├── problem.rs # Problem definitions
│ │ └── solution.rs # Solution representations
│ ├── utils/ # Utilities
│ │ ├── mod.rs
│ │ └── math.rs # Math utilities
│ └── bindings.rs # Python bindings
├── colonyx/ # Python package
│ ├── __init__.py # Main exports
│ ├── auto.py # AutoColony class
│ ├── base.py # Base classes
│ ├── utils.py # Python utilities
│ └── datasets.py # Benchmark datasets
├── examples/ # Usage examples
├── tests/ # Tests
└── docs/ # Documentation

```
6 changes: 0 additions & 6 deletions hello.py

This file was deleted.

7 changes: 5 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ build-backend = "maturin"
[project]
name = "colonyx"
requires-python = ">=3.8"
description = "A Pythonic toolkit for Ant Colony, Particle Swarm, and Bee Colony optimization — written in Rust for high performance, designed for real-world use."
readme = "README.md"
classifiers = [
"Programming Language :: Rust",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]
dynamic = ["version"]


[tool.maturin]
features = ["pyo3/extension-module"]
python-source = "python"
compatibility = "py38, py39, py310, py311, py312"
python-source = "colonyx"
Loading
Loading