|
| 1 | +# Useful Optimizer - GitHub Copilot Instructions |
| 2 | + |
| 3 | +Useful Optimizer is a Python optimization library containing 58 optimization algorithms for numeric problems. The library uses Poetry for dependency management and provides a comprehensive collection of metaheuristic, gradient-based, and nature-inspired optimization techniques. |
| 4 | + |
| 5 | +**Always reference these instructions first and fallback to search or bash commands only when you encounter unexpected information that does not match the info here.** |
| 6 | + |
| 7 | +## Working Effectively |
| 8 | + |
| 9 | +### Initial Setup and Installation |
| 10 | +- Install Poetry package manager: |
| 11 | + ```bash |
| 12 | + pip3 install poetry |
| 13 | + export PATH="$HOME/.local/bin:$PATH" |
| 14 | + ``` |
| 15 | + **Alternative if PATH export doesn't work:** Use `~/.local/bin/poetry` instead of `poetry` |
| 16 | +- Install project dependencies: |
| 17 | + ```bash |
| 18 | + poetry install |
| 19 | + ``` |
| 20 | + **NEVER CANCEL: Takes 2-3 minutes to complete. Set timeout to 5+ minutes.** |
| 21 | + |
| 22 | +### Core Development Commands |
| 23 | +- Run any Python command in the project environment: |
| 24 | + ```bash |
| 25 | + poetry run python [your_command] |
| 26 | + ``` |
| 27 | +- Test basic functionality: |
| 28 | + ```bash |
| 29 | + poetry run python -c "import opt; print('Import successful')" |
| 30 | + ``` |
| 31 | +- Run linting (finds code quality issues): |
| 32 | + ```bash |
| 33 | + poetry run ruff check opt/ |
| 34 | + ``` |
| 35 | + **Takes 5-10 seconds. Common to find existing issues in codebase.** |
| 36 | +- Auto-format code: |
| 37 | + ```bash |
| 38 | + poetry run ruff format opt/ |
| 39 | + ``` |
| 40 | + **Takes less than 1 second.** |
| 41 | +- Build the package: |
| 42 | + ```bash |
| 43 | + poetry build |
| 44 | + ``` |
| 45 | + **Takes less than 1 second. Creates wheel and sdist in dist/ directory.** |
| 46 | + |
| 47 | +## Validation Scenarios |
| 48 | + |
| 49 | +**ALWAYS test your changes with these scenarios after making modifications:** |
| 50 | + |
| 51 | +### Scenario 1: Basic Import and Functionality Test |
| 52 | +```bash |
| 53 | +poetry run python -c " |
| 54 | +from opt.benchmark.functions import shifted_ackley, rosenbrock, sphere |
| 55 | +from opt.particle_swarm import ParticleSwarm |
| 56 | +print('Testing basic import and optimizer creation...') |
| 57 | +pso = ParticleSwarm(func=shifted_ackley, lower_bound=-2.768, upper_bound=2.768, dim=2, max_iter=50) |
| 58 | +best_solution, best_fitness = pso.search() |
| 59 | +print(f'PSO completed successfully. Fitness: {best_fitness:.6f}') |
| 60 | +print('Basic functionality test PASSED') |
| 61 | +" |
| 62 | +``` |
| 63 | +**Expected: Completes in < 1 second, prints fitness value around 0.001-1.0** |
| 64 | + |
| 65 | +### Scenario 2: Multiple Optimizer Test |
| 66 | +```bash |
| 67 | +poetry run python -c " |
| 68 | +from opt.benchmark.functions import shifted_ackley, rosenbrock |
| 69 | +from opt.harmony_search import HarmonySearch |
| 70 | +from opt.ant_colony import AntColony |
| 71 | +print('Testing multiple optimizers...') |
| 72 | +
|
| 73 | +# Test HarmonySearch |
| 74 | +hs = HarmonySearch(func=rosenbrock, lower_bound=-5, upper_bound=5, dim=2, max_iter=50) |
| 75 | +_, fitness1 = hs.search() |
| 76 | +
|
| 77 | +# Test AntColony |
| 78 | +ac = AntColony(func=shifted_ackley, lower_bound=-2.768, upper_bound=2.768, dim=2, max_iter=50) |
| 79 | +_, fitness2 = ac.search() |
| 80 | +
|
| 81 | +print(f'HS fitness: {fitness1:.6f}, ACO fitness: {fitness2:.6f}') |
| 82 | +print('Multiple optimizer test PASSED') |
| 83 | +" |
| 84 | +``` |
| 85 | +**Expected: Completes in < 1 second, prints two fitness values** |
| 86 | + |
| 87 | +### Scenario 3: Direct Script Execution Test |
| 88 | +```bash |
| 89 | +poetry run python opt/harmony_search.py |
| 90 | +``` |
| 91 | +**Expected: Prints "Best solution found:" and "Best fitness found:" with numerical values** |
| 92 | + |
| 93 | +### Scenario 4: Advanced Import Test |
| 94 | +```bash |
| 95 | +poetry run python -c " |
| 96 | +from opt.abstract_optimizer import AbstractOptimizer |
| 97 | +from opt.benchmark.functions import shifted_ackley, sphere, rosenbrock, ackley |
| 98 | +from opt.sgd_momentum import SGDMomentum |
| 99 | +from opt.adamw import AdamW |
| 100 | +print('Advanced imports successful - gradient-based and base classes work') |
| 101 | +" |
| 102 | +``` |
| 103 | + |
| 104 | +## Project Structure |
| 105 | + |
| 106 | +### Key Directories and Files |
| 107 | +- `opt/` - Main optimization algorithms (58 Python files) |
| 108 | +- `opt/abstract_optimizer.py` - Base class that all optimizers inherit from |
| 109 | +- `opt/benchmark/functions.py` - Benchmark functions (shifted_ackley, sphere, rosenbrock, etc.) |
| 110 | +- `pyproject.toml` - Project configuration, dependencies, and ruff linting rules |
| 111 | +- `.pre-commit-config.yaml` - Pre-commit hooks configuration |
| 112 | +- `.github/workflows/python-publish.yaml` - CI/CD for PyPI publishing |
| 113 | + |
| 114 | +### Common Optimization Algorithms Available |
| 115 | +**Nature-Inspired:** |
| 116 | +- `ParticleSwarm` - Particle Swarm Optimization |
| 117 | +- `HarmonySearch` - Music-inspired metaheuristic |
| 118 | +- `AntColony` - Ant Colony Optimization |
| 119 | +- `BatAlgorithm` - Bat-inspired algorithm |
| 120 | +- `FireflyAlgorithm` - Firefly Algorithm |
| 121 | + |
| 122 | +**Gradient-Based:** |
| 123 | +- `SGDMomentum` - Stochastic Gradient Descent with momentum |
| 124 | +- `AdamW` - Adam with weight decay |
| 125 | +- `RMSprop` - RMSprop optimizer |
| 126 | +- `BFGS` - Quasi-Newton method |
| 127 | + |
| 128 | +**Classical:** |
| 129 | +- `SimulatedAnnealing` - Simulated Annealing |
| 130 | +- `NelderMead` - Nelder-Mead simplex method |
| 131 | +- `HillClimbing` - Hill Climbing |
| 132 | + |
| 133 | +### Benchmark Functions Available |
| 134 | +- `shifted_ackley` - Non-centered Ackley function (commonly used in examples) |
| 135 | +- `sphere` - Simple sphere function |
| 136 | +- `rosenbrock` - Rosenbrock function |
| 137 | +- `ackley` - Standard Ackley function |
| 138 | +- `rastrigin` - Rastrigin function |
| 139 | +- And 15+ more test functions |
| 140 | + |
| 141 | +## Development Guidelines |
| 142 | + |
| 143 | +### Code Quality and Linting |
| 144 | +- **ALWAYS run linting before committing:** |
| 145 | + ```bash |
| 146 | + poetry run ruff check opt/ |
| 147 | + poetry run ruff format opt/ |
| 148 | + ``` |
| 149 | +- The project uses extensive ruff rules - expect to find existing linting issues |
| 150 | +- Ruff configuration is in `pyproject.toml` with Google docstring convention |
| 151 | +- Pre-commit hooks are configured but run `ruff` manually to be sure |
| 152 | + |
| 153 | +### Creating New Optimizers |
| 154 | +- Inherit from `AbstractOptimizer` class in `opt/abstract_optimizer.py` |
| 155 | +- Implement the required `search()` method returning `tuple[np.ndarray, float]` |
| 156 | +- Follow existing pattern: see `opt/particle_swarm.py` or `opt/harmony_search.py` as examples |
| 157 | +- Include a `if __name__ == "__main__":` block for direct testing |
| 158 | +- Use benchmark functions from `opt.benchmark.functions` for testing |
| 159 | + |
| 160 | +### Testing Changes |
| 161 | +- No formal test suite exists - use the validation scenarios above |
| 162 | +- Test with multiple benchmark functions: `shifted_ackley`, `rosenbrock`, `sphere` |
| 163 | +- Test with different parameter combinations |
| 164 | +- Ensure optimizers complete within reasonable time (< 1 second for small max_iter) |
| 165 | + |
| 166 | +### Common Issues to Avoid |
| 167 | +- Don't modify `poetry.lock` manually - use `poetry add/remove` commands |
| 168 | +- Ruff linting will fail on many existing files - focus on new/modified code |
| 169 | +- Some algorithms use legacy `np.random.rand` calls - documented in README |
| 170 | +- Always use `poetry run` prefix for Python commands to use project environment |
| 171 | + |
| 172 | +## Project Information |
| 173 | +- **Version:** 0.1.2 |
| 174 | +- **Python Requirements:** 3.10-3.12 (currently running 3.12.3) |
| 175 | +- **Main Dependencies:** numpy, scipy, scikit-learn |
| 176 | +- **Dev Dependencies:** ruff (linting and formatting) |
| 177 | +- **Package Name:** useful-optimizer |
| 178 | +- **License:** MIT |
| 179 | + |
| 180 | +## Build and Distribution |
| 181 | +- Build package: `poetry build` (< 1 second) |
| 182 | +- Built artifacts appear in `dist/` directory |
| 183 | +- CI/CD publishes to PyPI on tag pushes via GitHub Actions |
| 184 | +- Test PyPI publishing is also configured |
| 185 | + |
| 186 | +## Performance Expectations |
| 187 | +- **Poetry install:** 2-3 minutes (NEVER CANCEL) |
| 188 | +- **Ruff linting:** 5-10 seconds |
| 189 | +- **Ruff formatting:** < 1 second |
| 190 | +- **Package build:** < 1 second |
| 191 | +- **Small optimization runs:** < 1 second (max_iter=50-100) |
| 192 | +- **Import operations:** Nearly instantaneous |
| 193 | + |
| 194 | +Always verify your changes work by running the validation scenarios above before committing. |
0 commit comments