PyNopt is an object-oriented Python library for numerical optimization.
Originally conceived as an evolution of a project for the Numerical Optimization for Large-Scale Problems course at Politecnico di Torino, it has been engineered from the ground up to showcase production-ready software engineering practices combined with rigorous numerical optimization techniques.
- Zero-Order Solvers: Nelder-Mead (Derivative-free optimization)
- First-Order Solvers: Gradient Descent and Projected Gradient Descent.
- Second-Order Solvers: Modified Newton Method.
- Line Search Strategies: Backtracking (Armijo condition), for general-purpose optimization, and Feasible Direction and Projection Arc methods for constrained optimization.
- Constrained Optimization: Built-in support for box constraint, Euclidean ball constraint and active-set handling.
- Computational Tracking: Automatic tracking of function, gradient, and Hessian evaluations (
n_feval,n_geval,n_heval) to facilitate empirical complexity analysis.
- Clean Architecture: Strongly typed (
Mypy/typing), utilizing Python Generics (TypeVar) for state management. - Design Patterns: Heavy use of Strategy (Step Strategies, Convergence Checkers) and Template Method (Objective Functions).
- Fail-Fast API: Input validation blocks mathematical impossibilities before runtime.
- CI/CD Pipeline: Fully automated test suite powered by
pytestand GitHub Actions, testing matrix across multiple Python versions.
Clone the repository and install it in editable mode using the standard pyproject.toml workflow:
git clone https://github.com/samuuuu0/PyNopt.git
cd PyNopt
pip install -e .PyNopt's API is designed to be intuitive and modular. Here's how to solve the Rosenbrock function using the Modified Newton solver with Backtracking line search:
import numpy as np
from pynopt import ModifiedNewtonSolver, Rosenbrock
# 1. Initialize the benchmark problem
problem = Rosenbrock()
x0 = np.array([-1.2, 1.0]) # Difficult starting point
# 2. Configure the solver with a Strategy Pattern
solver = ModifiedNewtonSolver(strategy="backtracking", max_iter=100, tol_stat=1e-6)
# 3. Solve the problem and print the optimization result
result = solver.solve(problem, x0)
print(result)Output
Optimization Result:
----------------------------------------
Status: Success (Stationarity criterion met)
Iterations: 21
Optimal f(x): 8.004303e-19
Optimal x: [1., 1.]
----------------------------------------
Function evals: 50
Gradient evals: 22
Hessian evals: 21
Time elapsed: 0.0026 s
----------------------------------------
Plot
The Modified Newton Solver (with Backtracking Line Search) converging to the minimum on the notoriously difficult, non-convex Rosenbrock function.
The notebooks/ directory contains analyses and visual benchmarks of the algorithms.
01_projected_gradient_analysis.ipynb: Analysis of constrained optimization boundaries.02_nelder_mead_analysis.ipynb: Behavior of zero-order methods.
The library includes a test suite ensuring both mathematical accuracy and architectural integrity.
pip install pytest pytest-cov
pytest test/ --cov=pynoptDeveloped by Samuele
