Optimization tutorial: learn by example
Project description
Python Optimization Cookbook
A collection of hands-on optimization examples covering single-objective, multi-objective, constrained, and benchmark problems โ implemented in Python with solvers including SciPy, pymoo, Pyomo, SCIP, IPOPT, GLPK, Optuna, and Z3.
๐ Repository Structure
pyoptiwize/ # repo root (cloned from optimization_tutorial)
โโโ pyproject.toml
โโโ conftest.py
โโโ LICENSE
โโโ pyoptiwize/ # package directory
โ โโโ config/ # Environment setup & dependency notes
โ โโโ examples/
โ โโโ shekel/py/ # Global optimization โ Shekel function (SHGO)
โ โโโ eggholder/py/ # Global optimization โ Eggholder function (SHGO, Dual Annealing)
โ โโโ constraint_dora/py/ # Constrained minimization (SLSQP)
โ โโโ cattlefeed/py/ # Nonlinear constrained NLP โ HS73 cattle feed problem
โ โโโ bnh/py/ # Multi-objective โ BNH problem (NSGA-II, Z3, PySMT)
โ โโโ c3dtlz4/py/ # Multi-objective benchmark โ C3-DTLZ4 (Optuna GP Sampler)
โ โโโ pyomo/py/ # Solver comparison โ NSGA-II, SCIP, IPOPT, GLPK
๐งช Examples
1. ๐ต Shekel Function โ Global Optimization
Path: pyoptiwize/examples/shekel/py/
Classic 4-dimensional multimodal benchmark with 10 local minima. Solved with SciPy's SHGO algorithm using Sobol sampling.
- Type: Single-objective, unconstrained, global
- Known minimum: f(x*) โ โ10.5363 at x* โ [4, 4, 4, 4]
- Solver: SHGO (Simplicial Homology Global Optimization)
- ๐ Reference
2. ๐ก Eggholder Function โ Global Optimization
Path: pyoptiwize/examples/eggholder/py/
Highly non-convex 2D benchmark with many local minima. Compares SHGO and Dual Annealing; includes 3D visualization and dataset generation.
- Type: Single-objective, unconstrained, global
- Known minimum: f(x*) โ โ959.64 at x* โ (512, 404.23)
- Solvers: SHGO, Dual Annealing
- ๐ Reference
3. ๐ข Constraint Dora โ Constrained Minimization
Path: pyoptiwize/examples/constraint_dora/py/
Textbook constrained minimization: minimize (xโโ2)ยฒ + (xโโ1)ยฒ subject to xโยฒ + xโยฒ โค 1. Solved with SLSQP; result verified analytically.
- Type: Single-objective, nonlinear constraint
- Solver: SLSQP
- ๐ WolframAlpha solution
4. ๐ Cattle Feed Problem (HS73) โ Nonlinear Programming
Path: pyoptiwize/examples/cattlefeed/py/
HockโSchittkowski test problem #73. Minimizes a linear feed cost subject to nutritional linear, nonlinear, and equality constraints over 4 variables.
- Type: NLP, mixed linear/nonlinear constraints
- Known minimum: f(x*) โ 29.894
- Recommended solvers: IPOPT, SQP, interior point methods
- ๐ Reference
5. ๐ด BNH Problem โ Multi-Objective Optimization
Path: pyoptiwize/examples/bnh/py/
Binh and Korn constrained bi-objective problem. Pareto front approximated with NSGA-II; Pareto-stable solutions certified using Z3 SMT solver and PySMT (gradient-stable and minimax-stable flavors).
- Type: Multi-objective, constrained (2 objectives, 2 constraints)
- Solvers: NSGA-II (pymoo), Z3, PySMT
- ๐ pymoo BNH
6. ๐ฃ C3-DTLZ4 โ Constrained Multi-Objective Benchmark
Path: pyoptiwize/examples/c3dtlz4/py/
Scalable constrained benchmark combining DTLZ4 objectives (ฮฑ=100 density bias) with C3-type constraints. Solved with Optuna's Gaussian Process Sampler.
- Type: Multi-objective, constrained, scalable (m objectives, n variables)
- Solver: Optuna GP Sampler
- ๐ Deb et al. DTLZ
7. ๐ท Pymoo & Pyomo โ Solver Comparison Suite
Path: pyoptiwize/examples/pyomo/py/
Comprehensive multi-solver comparison on the BNH problem and mixed-variable problems. Includes Pareto front approximation from CSV data using linear interpolation and decision trees, and direct solver comparisons.
- Type: Multi-objective & single-objective, constrained
- Solvers: NSGA-II, NSGA-III, SCIP, IPOPT, GLPK
- Highlights:
- BNH Pareto front: analytical vs. linear interpolation vs. decision tree
- Mixed-variable Pareto fronts (NSGA-II, NSGA-III)
- Single-objective: SCIP vs. IPOPT comparison
- Multi-objective: SCIP vs. IPOPT vs. NSGA-II comparison
- Linear programming via GLPK/Pyomo
โก Installation (Ubuntu 24.04)
1. System packages
sudo apt-get update && sudo apt-get install -y \
g++ gcc git glpk-utils jq \
libgfortran5 libgmp-dev libgomp1 libmpfr6 \
locales tzdata \
python3 python-is-python3 python3-dev python3-pip \
python3-pytest-mock python3-setuptools python3-tk python3-venv \
python3-z3 z3 \
tcsh vim wget \
x11-apps x11-xserver-utils xvfb
2. Python 3.11 and 3.13 (via deadsnakes PPA)
Ubuntu 24.04 ships Python 3.12 by default. Install 3.11 and 3.13 from the deadsnakes PPA:
sudo apt-get install -y gnupg curl ca-certificates
curl -fsSL "https://keyserver.ubuntu.com/pks/lookup?op=get&search=0xF23C5A6CF475977595C89F51BA6932366A755776" \
| gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/deadsnakes.gpg > /dev/null
echo "deb https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu noble main" \
| sudo tee /etc/apt/sources.list.d/deadsnakes.list
sudo apt-get update && sudo apt-get install -y \
python3.11 python3.11-venv python3.11-dev python3.11-tk \
python3.13 python3.13-venv python3.13-dev python3.13-tk
3. Miniconda
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash ./Miniconda3-latest-Linux-x86_64.sh -b
Accept the Conda terms of service:
~/miniconda3/bin/conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/main
~/miniconda3/bin/conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/r
4. SCIP and IPOPT
~/miniconda3/bin/conda install -y -c conda-forge ipopt scip
sudo ln -sf ~/miniconda3/bin/scip /usr/local/bin/scip
sudo ln -sf ~/miniconda3/bin/ipopt /usr/local/bin/ipopt
5. MathSAT (for PySMT / Z3 stable-gradient examples)
โ ๏ธ Licensing limitations Please read the MathSAT 5 license terms before using MathSAT. MathSAT 5 is available for research and evaluation purposes only. It cannot be used in a commercial environment, particularly as part of a commercial product, without written permission. MathSAT 5 is provided as-is, without any warranty.
Download MathSAT 5.6.15 and build the Python bindings for each interpreter:
wget https://mathsat.fbk.eu/release/mathsat-5.6.15-linux-x86_64.tar.gz
# Python 3.12 (system default)
tar -xf mathsat-5.6.15-linux-x86_64.tar.gz
cd mathsat-5.6.15-linux-x86_64/python
python setup.py build
sudo mkdir -p /usr/local/lib/python3.12/dist-packages
sudo mv mathsat.py /usr/local/lib/python3.12/dist-packages/
sudo mv build/lib.linux-x86_64-cpython-312/_mathsat.cpython-312-x86_64-linux-gnu.so \
/usr/local/lib/python3.12/dist-packages/_mathsat.so
cd ../..
# Python 3.13
tar -xf mathsat-5.6.15-linux-x86_64.tar.gz
cd mathsat-5.6.15-linux-x86_64/python
python3.13 setup.py build
sudo mkdir -p /usr/local/lib/python3.13/dist-packages
sudo mv mathsat.py /usr/local/lib/python3.13/dist-packages/
sudo mv build/lib.linux-x86_64-cpython-313/_mathsat.cpython-313-x86_64-linux-gnu.so \
/usr/local/lib/python3.13/dist-packages/_mathsat.so
cd ../..
# Python 3.11
tar -xf mathsat-5.6.15-linux-x86_64.tar.gz
cd mathsat-5.6.15-linux-x86_64/python
python3.11 setup.py build
sudo mkdir -p /usr/local/lib/python3.11/dist-packages
sudo mv mathsat.py /usr/local/lib/python3.11/dist-packages/
sudo mv build/lib.linux-x86_64-cpython-311/_mathsat.cpython-311-x86_64-linux-gnu.so \
/usr/local/lib/python3.11/dist-packages/_mathsat.so
cd ../..
rm -rf mathsat-5.6.15-linux-x86_64 mathsat-5.6.15-linux-x86_64.tar.gz
6. Locale
sudo locale-gen en_US.UTF-8
Add to your ~/.bashrc (or ~/.profile):
export LANG=en_US.UTF-8
export LANGUAGE=en_US:en
export LC_ALL=en_US.UTF-8
7. Clone the repository
git clone https://github.com/mdmitry1/optimization_tutorial.git pyoptiwize
cd pyoptiwize
8. Install Python dependencies
Install the package and its dependencies from pyproject.toml:
# Standard install
pip install .
# With test dependencies
pip install ".[test]"
python3.11 dependecies
python3.12 dependecies
python3.13 dependecies
9. Run an example
cd pyoptiwize/examples/eggholder/py
python optimization_ex.py
10. Run tests
Some tests (Z3 and Keras-based) must run in forked subprocesses to isolate solver state. The conftest.py at the repo root automatically marks any test whose path contains z3 or keras with the forked marker.
Run in two passes from each example's py/ directory:
# Pass 1 โ forked tests (z3, keras)
pytest -v --forked -m forked
# Pass 2 โ all other tests
pytest -v -s -m 'not forked'
๐ ๏ธ Environment
| Component | Version |
|---|---|
| OS | Ubuntu 24.04 LTS (Noble) |
| Python | 3.11, 3.12, 3.13 |
| IPOPT | 3.14.19 |
| SCIP | 10.0.0 |
| GLPK | 5.0 |
| Z3 | 4.8.12 |
| CUDA | 12.8 |
๐ References
- Shekel function
- Eggholder function
- Hock & Schittkowski test problems
- BNH problem โ pymoo
- NSGA-II
- NSGA-III
- C3-DTLZ4 source code
- SCIP solver
- IPOPT
- GLPK
- pymoo
- Pyomo
- Optuna GP Sampler
Copyright and license
ยฉ 2025 Dmitry Messerman. Licensed under GNU General Public License v3.0.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyoptiwize-0.2.0-py3-none-any.whl.
File metadata
- Download URL: pyoptiwize-0.2.0-py3-none-any.whl
- Upload date:
- Size: 9.6 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba8ed8bfa936a16c46df82080ee33e4f328681156afacb5ab6bc2746699c4d97
|
|
| MD5 |
7b8167b3591b26b7ee576878d9e74723
|
|
| BLAKE2b-256 |
d79b201d5cf8691d7bad708ec736a7eb7a1b1c2d59b893cfa0fcc651aa97786f
|