Skip to main content

Optimization tutorial: learn by example

Project description

Python Optimization Cookbook

Python License NumPy SciPy pymoo Pyomo Optuna pytest

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.


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


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

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyoptiwize-0.2.0-py3-none-any.whl (9.6 MB view details)

Uploaded Python 3

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

Hashes for pyoptiwize-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ba8ed8bfa936a16c46df82080ee33e4f328681156afacb5ab6bc2746699c4d97
MD5 7b8167b3591b26b7ee576878d9e74723
BLAKE2b-256 d79b201d5cf8691d7bad708ec736a7eb7a1b1c2d59b893cfa0fcc651aa97786f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page