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
163 changes: 96 additions & 67 deletions .github/copilot-instructions.md
Original file line number Diff line number Diff line change
@@ -1,46 +1,48 @@
# Useful Optimizer - GitHub Copilot Instructions

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.
Useful Optimizer is a Python optimization library containing 58 optimization algorithms for numeric problems. The project uses **uv** for dependency management, virtual environments, and packaging while providing a comprehensive collection of metaheuristic, gradient-based, and nature-inspired optimization techniques.

**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.**
**Always reference these instructions first and fall back to search or shell commands only when you encounter unexpected information that does not match the info here.** All commands are written as **single-line Fish-shell commands** so they can be copied directly into the terminal.

## Working Effectively

### Initial Setup and Installation
- Install Poetry package manager:
```bash
pip3 install poetry
export PATH="$HOME/.local/bin:$PATH"
- Install uv (recommended via installer script):
```fish
curl -LsSf https://astral.sh/uv/install.sh | sh
```
**Alternative if PATH export doesn't work:** Use `~/.local/bin/poetry` instead of `poetry`
- Install project dependencies:
```bash
poetry install
- Ensure `$HOME/.local/bin` is on your Fish PATH (replace with your actual install dir if different):
```fish
set -Ux PATH $HOME/.local/bin $PATH
```
- Install project dependencies and create the managed virtual environment:
```fish
uv sync
```
**NEVER CANCEL: Takes 2-3 minutes to complete. Set timeout to 5+ minutes.**

### Core Development Commands
- Run any Python command in the project environment:
```bash
poetry run python [your_command]
```fish
uv run python [your_command]
```
- Test basic functionality:
```bash
poetry run python -c "import opt; print('Import successful')"
```fish
uv run python -c "import opt; print('Import successful')"
```
- Run linting (finds code quality issues):
```bash
poetry run ruff check opt/
```fish
uv run ruff check opt/
```
**Takes 5-10 seconds. Common to find existing issues in codebase.**
- Auto-format code:
```bash
poetry run ruff format opt/
```fish
uv run ruff format opt/
```
**Takes less than 1 second.**
- Build the package:
```bash
poetry build
```fish
uv build
```
**Takes less than 1 second. Creates wheel and sdist in dist/ directory.**

Expand All @@ -49,56 +51,26 @@ Useful Optimizer is a Python optimization library containing 58 optimization alg
**ALWAYS test your changes with these scenarios after making modifications:**

### Scenario 1: Basic Import and Functionality Test
```bash
poetry run python -c "
from opt.benchmark.functions import shifted_ackley, rosenbrock, sphere
from opt.particle_swarm import ParticleSwarm
print('Testing basic import and optimizer creation...')
pso = ParticleSwarm(func=shifted_ackley, lower_bound=-2.768, upper_bound=2.768, dim=2, max_iter=50)
best_solution, best_fitness = pso.search()
print(f'PSO completed successfully. Fitness: {best_fitness:.6f}')
print('Basic functionality test PASSED')
"
```fish
uv run python -c "from opt.benchmark.functions import shifted_ackley, rosenbrock, sphere; from opt.particle_swarm import ParticleSwarm; print('Testing basic import and optimizer creation...'); pso = ParticleSwarm(func=shifted_ackley, lower_bound=-2.768, upper_bound=2.768, dim=2, max_iter=50); best_solution, best_fitness = pso.search(); print(f'PSO completed successfully. Fitness: {best_fitness:.6f}'); print('Basic functionality test PASSED')"
```
**Expected: Completes in < 1 second, prints fitness value around 0.001-1.0**

### Scenario 2: Multiple Optimizer Test
```bash
poetry run python -c "
from opt.benchmark.functions import shifted_ackley, rosenbrock
from opt.harmony_search import HarmonySearch
from opt.ant_colony import AntColony
print('Testing multiple optimizers...')

# Test HarmonySearch
hs = HarmonySearch(func=rosenbrock, lower_bound=-5, upper_bound=5, dim=2, max_iter=50)
_, fitness1 = hs.search()

# Test AntColony
ac = AntColony(func=shifted_ackley, lower_bound=-2.768, upper_bound=2.768, dim=2, max_iter=50)
_, fitness2 = ac.search()

print(f'HS fitness: {fitness1:.6f}, ACO fitness: {fitness2:.6f}')
print('Multiple optimizer test PASSED')
"
```fish
uv run python -c "from opt.benchmark.functions import shifted_ackley, rosenbrock; from opt.harmony_search import HarmonySearch; from opt.ant_colony import AntColony; print('Testing multiple optimizers...'); hs = HarmonySearch(func=rosenbrock, lower_bound=-5, upper_bound=5, dim=2, max_iter=50); _, fitness1 = hs.search(); ac = AntColony(func=shifted_ackley, lower_bound=-2.768, upper_bound=2.768, dim=2, max_iter=50); _, fitness2 = ac.search(); print(f'HS fitness: {fitness1:.6f}, ACO fitness: {fitness2:.6f}'); print('Multiple optimizer test PASSED')"
```
**Expected: Completes in < 1 second, prints two fitness values**

### Scenario 3: Direct Script Execution Test
```bash
poetry run python opt/harmony_search.py
```fish
uv run python opt/harmony_search.py
```
**Expected: Prints "Best solution found:" and "Best fitness found:" with numerical values**

### Scenario 4: Advanced Import Test
```bash
poetry run python -c "
from opt.abstract_optimizer import AbstractOptimizer
from opt.benchmark.functions import shifted_ackley, sphere, rosenbrock, ackley
from opt.sgd_momentum import SGDMomentum
from opt.adamw import AdamW
print('Advanced imports successful - gradient-based and base classes work')
"
```fish
uv run python -c "from opt.abstract_optimizer import AbstractOptimizer; from opt.benchmark.functions import shifted_ackley, sphere, rosenbrock, ackley; from opt.sgd_momentum import SGDMomentum; from opt.adamw import AdamW; print('Advanced imports successful - gradient-based and base classes work')"
```

## Project Structure
Expand Down Expand Up @@ -142,9 +114,15 @@ print('Advanced imports successful - gradient-based and base classes work')

### Code Quality and Linting
- **ALWAYS run linting before committing:**
```bash
poetry run ruff check opt/
poetry run ruff format opt/
```fish
uv run ruff check opt/ && uv run ruff format opt/
```
- Or run them separately:
```fish
uv run ruff check opt/
```
```fish
uv run ruff format opt/
```
- The project uses extensive ruff rules - expect to find existing linting issues
- Ruff configuration is in `pyproject.toml` with Google docstring convention
Expand All @@ -158,16 +136,67 @@ print('Advanced imports successful - gradient-based and base classes work')
- Use benchmark functions from `opt.benchmark.functions` for testing

### Testing Changes
- No formal test suite exists - use the validation scenarios above
- Run the test suite:
```fish
uv run pytest opt/test/ -v
```
- Run specific test files:
```fish
uv run pytest opt/test/test_benchmarks.py -v
```
- Test with multiple benchmark functions: `shifted_ackley`, `rosenbrock`, `sphere`
- Test with different parameter combinations
- Ensure optimizers complete within reasonable time (< 1 second for small max_iter)

### Common Issues to Avoid
- Don't modify `poetry.lock` manually - use `poetry add/remove` commands
- Don't modify `uv.lock` manually - use `uv add`, `uv remove`, or `uv sync` to change dependencies
- Ruff linting will fail on many existing files - focus on new/modified code
- Some algorithms use legacy `np.random.rand` calls - documented in README
- Always use `poetry run` prefix for Python commands to use project environment
- Always use `uv run` (or activate `.venv`) so commands execute inside the project environment

### CRITICAL: Fish Shell and Terminal Rules
**All commands MUST be single-line.** Multiline commands crash the terminal in Fish shell.

**Git commit messages:** Always use single-line format with conventional commits:
```fish
git commit -m "feat: add new optimizer algorithm"
```

**Conventional commit types:**
- `feat:` - New feature
- `fix:` - Bug fix
- `docs:` - Documentation changes
- `test:` - Adding or updating tests
- `refactor:` - Code refactoring
- `chore:` - Maintenance tasks

**NEVER use multiline commit messages like this (WILL CRASH):**
```fish
# BAD - DO NOT USE
git commit -m "Title

Multiline body"
```

**For commit messages with title and body**, use multiple `-m` flags on one line:
```fish
git commit -m "Title" -m "Body paragraph 1" -m "Body paragraph 2"
```

**For longer commit messages**, use the editor:
```fish
git commit
```

**Chaining commands:** Use `&&` to chain multiple commands:
```fish
git add . && git commit -m "message" && git push
```

**Python -c commands:** Keep on single line with semicolons:
```fish
uv run python -c "import x; print(x.value); do_something()"
```

## Project Information
- **Version:** 0.1.2
Expand All @@ -178,16 +207,16 @@ print('Advanced imports successful - gradient-based and base classes work')
- **License:** MIT

## Build and Distribution
- Build package: `poetry build` (< 1 second)
- Build package: `uv build` (< 1 second)
- Built artifacts appear in `dist/` directory
- CI/CD publishes to PyPI on tag pushes via GitHub Actions
- Test PyPI publishing is also configured

## Performance Expectations
- **Poetry install:** 2-3 minutes (NEVER CANCEL)
- **uv sync:** 2-3 minutes (NEVER CANCEL)
- **Ruff linting:** 5-10 seconds
- **Ruff formatting:** < 1 second
- **Package build:** < 1 second
- **Package build (uv build):** < 1 second
- **Small optimization runs:** < 1 second (max_iter=50-100)
- **Import operations:** Nearly instantaneous

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/python-publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ jobs:
name: python-package-distributions
path: dist/
- name: Sign the dists with Sigstore
uses: sigstore/gh-action-sigstore-python@v3.1.0
uses: sigstore/gh-action-sigstore-python@v3.2.0
with:
inputs: >-
./dist/*.tar.gz
Expand Down
Loading
Loading