Skip to content

Repository files navigation

vscode-python-opt

Simple examples of running discrete optimization solvers in Python

Quick start

If needed, install uv. You can then run the examples with uv run manage.py.

Environment Setup

Dev Container (VS Code, VSCodium)

You can open this repo in a Visual Studio Code Dev Container with all the required packages automatically installed. You will need to install Visual Studio Code, and a containerization platform like Docker or Podman (see Dev Containers tutorial).

Alternatively, you can create a DevPod Workspace if you want to use other IDEs like VSCodium.

Docker / Docker Compose

You can open this repo in a container with command-line access using Docker Compose (included in Docker Desktop) or Podman Compose (setup from Podman Desktop), by running the following command in the repo's root directory:

docker compose run --rm devbox

Since the repo directory is mounted into the container as a volume, any changes to the files will be reflected immediately.

If you make any changes to the package requirements, you can rebuild the container with

docker compose build devbox

Virtual environment (conda)

If needed, install a conda environment manager (recommend a local user installation of either miniforge or micromamba; but mamba, conda, or the Anaconda Distribution will also work).

Then, you can build and activate a conda environment / mamba environment with all the necessary packages by running the following commands in the repo's root directory:

# Replace `conda` with `micromamba` or `mamba` as needed.
conda create -n vscode-python-opt -f environment.yml
conda activate vscode-python-opt

Virtual environment (python, py, uv)

If needed, install and setup a Python environment for your platform.

Then, you can build and activate a Python virtual environment with all the necessary packages by running the following commands in the repo's root directory:

# Windows PowerShell / cmd
# You may need to use `python` instead of `py` if you installed Python via the Microsoft Store app.
py -m venv .venv
.\.venv\Scripts\activate
pip install -r requirements.txt

# Linux / OSX
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

Alternatively, if you are using uv, you can run uv sync to update the virtual environment, and/or run the examples with uv run manage.py.

Running the examples

To run the examples, you can use the manage.py script.

To run a specific example, specify the name of the solver and the name of the example:

python manage.py gurobi simple

You can also call manage.py with no arguments to get a CLI showing the possible options:

# Show available solvers
python manage.py

# Show available examples for the solver Gurobi
python manage.py gurobi

Workarounds

Python-MIP (COIN-OR) is incompatible with OR-Tools - 2026-04-23

An error occurred while loading the CBC library: cannot load library ...
...
nameError: name 'cbclib' is not defined

For whatever reason, Python-MIP seems to be incompatible with OR-Tools, and sometimes fails to find the CBC binary if OR-Tools has also been imported. Two simple workarounds are

  1. Comment out all imports of OR-Tools when running COIN-OR examples.
  2. Revert to using an older version python-mip<1.16 (before the CBC binary was decoupled into a separate package).

OR-Tools is incompatible with HiGHS - 2026-07-11

ImportError: libortools.so.9: undefined symbol: _Z19setLocalOptionValueRK15HighsLogOptionsRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERS_RSt6vectorIP12OptionRecordSaISD_EES7_

OR-Tools currently has an incompatibility issue with HiGHS on certain platforms - see google/or-tools#5246 for the relevant discussion.

A simple workaround is to comment out all imports of OR-Tools or HiGHS depending on the example being run.

Development

Updating versions

When updating package versions, remember to update:

  1. pyproject.toml
  2. requirements.txt
  3. environment.yml
  4. uv.lock

When updating Python version, remember to update:

  1. pyproject.toml
  2. environment.yml
  3. .python-version
  4. Dockerfile
  5. devcontainer.json
  6. uv.lock

When updating the Docker image version (e.g. 3-VARIANT), remember to update:

  1. Dockerfile

About

Simple examples of running optimization solvers in Python

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages