Skip to content

Commit 78dab9b

Browse files
Anselmoopre-commit-ci[bot]Copilot
authored
refactor: reorganize optimizers into subfolders and migrate to uv (#30)
* refactor: reorganize optimizers into categorical subfolders * refactor: reorganize optimizers into categorical subfolders - Move 54 optimizer files into 7 categorical subfolders: - gradient_based/ (11): AdaDelta, AdaGrad, Adam, etc. - swarm_intelligence/ (12): PSO, ACO, Bat, Bee, etc. - evolutionary/ (6): GA, DE, CMA-ES, etc. - classical/ (9): BFGS, Nelder-Mead, etc. - metaheuristic/ (12): Harmony Search, etc. - constrained/ (2): Augmented Lagrangian, SLP - probabilistic/ (2): TPE, LDA - Add __init__.py with exports to each subfolder - Update root opt/__init__.py for backward compatibility - All existing imports still work (from opt import X)" * ci: update workflow to use uv for building * test: add comprehensive test suite for all optimizers * ci: add test and lint workflow * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix: update ruff config for Python 3.10+ and fix pre-commit * docs: update README for new folder structure and uv migration * Initial plan * fix: address review comments - fix BeeAlgorithm typo and add tests for BatAlgorithm and BeeAlgorithm Co-authored-by: Anselmoo <13209783+Anselmoo@users.noreply.github.com> --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 9e94a2a commit 78dab9b

File tree

71 files changed

+1532
-301
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+1532
-301
lines changed

.github/workflows/python-publish.yaml

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,14 @@ jobs:
1414

1515
steps:
1616
- uses: actions/checkout@v4
17-
- name: Set up Python
18-
uses: actions/setup-python@v5
17+
- name: Install uv
18+
uses: astral-sh/setup-uv@v4
1919
with:
20-
python-version: "3.x"
21-
- name: Install pypa/build
22-
run: >-
23-
python3 -m
24-
pip install
25-
build
26-
--user
20+
version: "latest"
21+
- name: Set up Python
22+
run: uv python install 3.12
2723
- name: Build a binary wheel and a source tarball
28-
run: python3 -m build
24+
run: uv build
2925
- name: Store the distribution packages
3026
uses: actions/upload-artifact@v4
3127
with:

.github/workflows/tests.yaml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
test:
13+
name: Test Python ${{ matrix.python-version }}
14+
runs-on: ubuntu-latest
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
python-version: ["3.10", "3.11", "3.12"]
19+
20+
steps:
21+
- uses: actions/checkout@v4
22+
23+
- name: Install uv
24+
uses: astral-sh/setup-uv@v4
25+
with:
26+
version: "latest"
27+
28+
- name: Set up Python ${{ matrix.python-version }}
29+
run: uv python install ${{ matrix.python-version }}
30+
31+
- name: Install dependencies
32+
run: uv sync
33+
34+
- name: Run tests
35+
run: uv run pytest opt/test/ -v --tb=short
36+
37+
lint:
38+
name: Lint
39+
runs-on: ubuntu-latest
40+
41+
steps:
42+
- uses: actions/checkout@v4
43+
44+
- name: Install uv
45+
uses: astral-sh/setup-uv@v4
46+
with:
47+
version: "latest"
48+
49+
- name: Set up Python
50+
run: uv python install 3.12
51+
52+
- name: Install dependencies
53+
run: uv sync
54+
55+
- name: Run ruff check
56+
run: uv run ruff check opt/
57+
58+
- name: Run ruff format check
59+
run: uv run ruff format --check opt/

.pre-commit-config.yaml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22
# See https://pre-commit.com/hooks.html for more hooks
33
repos:
44
- repo: https://github.com/pre-commit/pre-commit-hooks
5-
rev: "v4.5.0"
5+
rev: "v6.0.0"
66
hooks:
77
- id: trailing-whitespace
88
- id: end-of-file-fixer
99
- id: check-yaml
1010
- id: check-added-large-files
1111
- repo: https://github.com/astral-sh/ruff-pre-commit
12-
rev: "v0.3.4"
12+
rev: "v0.14.10"
1313
hooks:
1414
- id: ruff
15-
language_version: python3.12
16-
types_or: [ python, pyi, jupyter ]
15+
types_or: [python, pyi, jupyter]
1716
- id: ruff-format
18-
language_version: python3.12

README.md

Lines changed: 111 additions & 82 deletions
Large diffs are not rendered by default.

opt/__init__.py

Lines changed: 150 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,155 @@
1-
"""Useful optimizers, a set of optimization algorithms."""
1+
"""Useful optimizers, a set of optimization algorithms.
2+
3+
This package provides 54 optimization algorithms organized into categories:
4+
- gradient_based: Gradient-based optimizers (AdaDelta, AdaGrad, Adam, etc.)
5+
- swarm_intelligence: Nature-inspired swarm algorithms (PSO, ACO, etc.)
6+
- evolutionary: Evolutionary algorithms (GA, DE, CMA-ES, etc.)
7+
- classical: Classical optimization methods (BFGS, Nelder-Mead, etc.)
8+
- metaheuristic: Metaheuristic algorithms (Harmony Search, etc.)
9+
- constrained: Constrained optimization methods
10+
- probabilistic: Probabilistic optimization methods
11+
12+
All optimizers are re-exported at the package level for backward compatibility.
13+
"""
214

315
from __future__ import annotations
416

17+
# Base class
18+
from opt.abstract_optimizer import AbstractOptimizer
19+
20+
# Classical algorithms
21+
from opt.classical import BFGS
22+
from opt.classical import LBFGS
23+
from opt.classical import ConjugateGradient
24+
from opt.classical import HillClimbing
25+
from opt.classical import NelderMead
26+
from opt.classical import Powell
27+
from opt.classical import SimulatedAnnealing
28+
from opt.classical import TabuSearch
29+
from opt.classical import TrustRegion
30+
31+
# Constrained optimization
32+
from opt.constrained import AugmentedLagrangian
33+
from opt.constrained import SuccessiveLinearProgramming
34+
35+
# Evolutionary algorithms
36+
from opt.evolutionary import CMAESAlgorithm
37+
from opt.evolutionary import CulturalAlgorithm
38+
from opt.evolutionary import DifferentialEvolution
39+
from opt.evolutionary import EstimationOfDistributionAlgorithm
40+
from opt.evolutionary import GeneticAlgorithm
41+
from opt.evolutionary import ImperialistCompetitiveAlgorithm
42+
from opt.gradient_based import SGD
43+
44+
# Gradient-based algorithms
45+
from opt.gradient_based import ADAGrad
46+
from opt.gradient_based import ADAMOptimization
47+
from opt.gradient_based import AMSGrad
48+
from opt.gradient_based import AdaDelta
49+
from opt.gradient_based import AdaMax
50+
from opt.gradient_based import AdamW
51+
from opt.gradient_based import Nadam
52+
from opt.gradient_based import NesterovAcceleratedGradient
53+
from opt.gradient_based import RMSprop
54+
from opt.gradient_based import SGDMomentum
55+
56+
# Metaheuristic algorithms
57+
from opt.metaheuristic import CollidingBodiesOptimization
58+
from opt.metaheuristic import CrossEntropyMethod
59+
from opt.metaheuristic import EagleStrategy
60+
from opt.metaheuristic import HarmonySearch
61+
from opt.metaheuristic import ParticleFilter
62+
from opt.metaheuristic import ShuffledFrogLeapingAlgorithm
63+
from opt.metaheuristic import SineCosineAlgorithm
64+
from opt.metaheuristic import StochasticDiffusionSearch
65+
from opt.metaheuristic import StochasticFractalSearch
66+
from opt.metaheuristic import VariableDepthSearch
67+
from opt.metaheuristic import VariableNeighborhoodSearch
68+
from opt.metaheuristic import VeryLargeScaleNeighborhood
69+
70+
# Probabilistic algorithms
71+
from opt.probabilistic import LDAnalysis
72+
from opt.probabilistic import ParzenTreeEstimator
73+
74+
# Swarm intelligence algorithms
75+
from opt.swarm_intelligence import AntColony
76+
from opt.swarm_intelligence import ArtificialFishSwarm
77+
from opt.swarm_intelligence import BatAlgorithm
78+
from opt.swarm_intelligence import BeeAlgorithm
79+
from opt.swarm_intelligence import CatSwarmOptimization
80+
from opt.swarm_intelligence import CuckooSearch
81+
from opt.swarm_intelligence import FireflyAlgorithm
82+
from opt.swarm_intelligence import GlowwormSwarmOptimization
83+
from opt.swarm_intelligence import GreyWolfOptimizer
84+
from opt.swarm_intelligence import ParticleSwarm
85+
from opt.swarm_intelligence import SquirrelSearchAlgorithm
86+
from opt.swarm_intelligence import WhaleOptimizationAlgorithm
87+
588

689
__version__ = "0.1.2"
90+
91+
__all__: list[str] = [
92+
# Classical
93+
"BFGS",
94+
"LBFGS",
95+
"SGD",
96+
"ADAGrad",
97+
"ADAMOptimization",
98+
"AMSGrad",
99+
# Base class
100+
"AbstractOptimizer",
101+
# Gradient-based
102+
"AdaDelta",
103+
"AdaMax",
104+
"AdamW",
105+
# Swarm intelligence
106+
"AntColony",
107+
"ArtificialFishSwarm",
108+
# Constrained
109+
"AugmentedLagrangian",
110+
"BatAlgorithm",
111+
"BeeAlgorithm",
112+
# Evolutionary
113+
"CMAESAlgorithm",
114+
"CatSwarmOptimization",
115+
# Metaheuristic
116+
"CollidingBodiesOptimization",
117+
"ConjugateGradient",
118+
"CrossEntropyMethod",
119+
"CuckooSearch",
120+
"CulturalAlgorithm",
121+
"DifferentialEvolution",
122+
"EagleStrategy",
123+
"EstimationOfDistributionAlgorithm",
124+
"FireflyAlgorithm",
125+
"GeneticAlgorithm",
126+
"GlowwormSwarmOptimization",
127+
"GreyWolfOptimizer",
128+
"HarmonySearch",
129+
"HillClimbing",
130+
"ImperialistCompetitiveAlgorithm",
131+
# Probabilistic
132+
"LDAnalysis",
133+
"Nadam",
134+
"NelderMead",
135+
"NesterovAcceleratedGradient",
136+
"ParticleFilter",
137+
"ParticleSwarm",
138+
"ParzenTreeEstimator",
139+
"Powell",
140+
"RMSprop",
141+
"SGDMomentum",
142+
"ShuffledFrogLeapingAlgorithm",
143+
"SimulatedAnnealing",
144+
"SineCosineAlgorithm",
145+
"SquirrelSearchAlgorithm",
146+
"StochasticDiffusionSearch",
147+
"StochasticFractalSearch",
148+
"SuccessiveLinearProgramming",
149+
"TabuSearch",
150+
"TrustRegion",
151+
"VariableDepthSearch",
152+
"VariableNeighborhoodSearch",
153+
"VeryLargeScaleNeighborhood",
154+
"WhaleOptimizationAlgorithm",
155+
]

opt/classical/__init__.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Classical optimization algorithms.
2+
3+
This module contains traditional mathematical optimization methods including derivative-based
4+
and derivative-free approaches. Includes: BFGS, Conjugate Gradient, Hill Climbing, L-BFGS,
5+
Nelder-Mead, Powell, Simulated Annealing, Tabu Search, and Trust Region methods.
6+
"""
7+
8+
from __future__ import annotations
9+
10+
from opt.classical.bfgs import BFGS
11+
from opt.classical.conjugate_gradient import ConjugateGradient
12+
from opt.classical.hill_climbing import HillClimbing
13+
from opt.classical.lbfgs import LBFGS
14+
from opt.classical.nelder_mead import NelderMead
15+
from opt.classical.powell import Powell
16+
from opt.classical.simulated_annealing import SimulatedAnnealing
17+
from opt.classical.tabu_search import TabuSearch
18+
from opt.classical.trust_region import TrustRegion
19+
20+
21+
__all__: list[str] = [
22+
"BFGS",
23+
"LBFGS",
24+
"ConjugateGradient",
25+
"HillClimbing",
26+
"NelderMead",
27+
"Powell",
28+
"SimulatedAnnealing",
29+
"TabuSearch",
30+
"TrustRegion",
31+
]
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)