From 7f30ac7503f22e1f398807ae8b719e1289c6f761 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Schnabel?= Date: Tue, 21 Oct 2025 13:20:54 +0200 Subject: [PATCH 1/5] Extend workflow for CUDA13 --- .github/workflows/main.yml | 124 +++++++++++++++++++++++++++---------- 1 file changed, 91 insertions(+), 33 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 32bd03e..f277ba0 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -4,7 +4,7 @@ on: push: # branches: [main] tags: - - '*' # Run also when a tag is pushed + - '*' # Run only when a new tag is pushed pull_request: jobs: @@ -21,14 +21,22 @@ jobs: run: | apt-get update && apt-get install -y patchelf curl unzip zip gcc - - name: Set up python virtual environment with NVIDIA dependencies + # Get CUDA runtimes via pip + - name: Set up python virtual environments with NVIDIA dependencies for CUDA 12 and CUDA 13 (respectively) run: | mkdir -p venvs python -m venv venvs/cu12 bash -c "source venvs/cu12/bin/activate && \ pip install --upgrade pip -qq && \ - pip install --extra-index-url=https://pypi.anaconda.org/rapidsai-wheels-nightly/simple/ cuopt-cu12==25.10.0 nvidia-cuda-runtime-cu12==12.8.* nvidia-nvjitlink-cu12 -qq" + pip install --extra-index-url=https://pypi.nvidia.com cuopt-cu12==25.10.* cuda-toolkit[cudart,nvjitlink]==12.9.* -qq && + deactivate" + python -m venv venvs/cu13 + bash -c "source venvs/cu13/bin/activate && \ + pip install --upgrade pip -qq && \ + pip install --extra-index-url=https://pypi.nvidia.com cuopt-cu13==25.10.* cuda-toolkit[cudart,nvjitlink]==13.0.* -qq && + deactivate" + # Get GAMS - name: Download and extract latest GAMS distribution run: | curl https://d37drm4t2jghv5.cloudfront.net/distributions/latest/linux/linux_x64_64_sfx.exe --output linux_x64_64_sfx.exe @@ -36,66 +44,116 @@ jobs: mv gams*_linux_x64_64_sfx gamsdist rm linux_x64_64_sfx.exe - - name: Compile gmscuopt + # Build link + - name: Compile GAMS/cuOpt-link binary "gmscuopt.out" for CUDA 12 and then for CUDA 13 run: | export GAMSCAPI="gamsdist/apifiles/C/api" export CUOPT="venvs/cu12/lib/python3.12/site-packages/libcuopt" export JITLINK="venvs/cu12/lib/python3.12/site-packages/nvidia/nvjitlink/lib" export CUOPT_VERSION="`cat "$CUOPT/VERSION"`" export CUOPT_HASH="`cat "$CUOPT/GIT_COMMIT"`" - gcc -Wall gmscuopt.c -o gmscuopt.out \ + gcc -Wall gmscuopt.c -o gmscuopt-cu12.out \ -DCUOPT_VERSION=\"$CUOPT_VERSION\" -DCUOPT_HASH=\"$CUOPT_HASH\" \ -I $GAMSCAPI $GAMSCAPI/gmomcc.c $GAMSCAPI/optcc.c $GAMSCAPI/gevmcc.c \ -I $CUOPT/include $JITLINK/libnvJitLink.so.12 -L $CUOPT/lib64 -lcuopt - patchelf --set-rpath \$ORIGIN gmscuopt.out + patchelf --set-rpath \$ORIGIN gmscuopt-cu12.out + export CUOPT="venvs/cu13/lib/python3.12/site-packages/libcuopt" + export JITLINK="venvs/cu13/lib/python3.12/site-packages/nvidia/nvjitlink/lib" + export CUOPT_VERSION="`cat "$CUOPT/VERSION"`" + export CUOPT_HASH="`cat "$CUOPT/GIT_COMMIT"`" + gcc -Wall gmscuopt.c -o gmscuopt-cu13.out \ + -DCUOPT_VERSION=\"$CUOPT_VERSION\" -DCUOPT_HASH=\"$CUOPT_HASH\" \ + -I $GAMSCAPI $GAMSCAPI/gmomcc.c $GAMSCAPI/optcc.c $GAMSCAPI/gevmcc.c \ + -I $CUOPT/include $JITLINK/libnvJitLink.so.13 -L $CUOPT/lib64 -lcuopt + patchelf --set-rpath \$ORIGIN gmscuopt-cu13.out + # Collect dependencies for link and runtime convenience archive - name: Prepare release artifact and runtime bundle run: | - mkdir release - cp gmscuopt.out release/ - cp assets/* release/ - cp venvs/cu12/lib/python3.12/site-packages/libcuopt/lib64/libcuopt.so release/ - cp venvs/cu12/lib/python3.12/site-packages/libcuopt/lib64/libmps_parser.so release/ - cp venvs/cu12/lib/python3.12/site-packages/libcuopt_cu12.libs/libgomp-*.so.1.0.0 release/ - cp venvs/cu12/lib/python3.12/site-packages/libcuopt_cu12.libs/libtbb-*.so.2 release/ - cp venvs/cu12/lib/python3.12/site-packages/libcuopt_cu12.libs/libtbbmalloc-*.so.2 release/ - cp venvs/cu12/lib/python3.12/site-packages/rapids_logger/lib64/librapids_logger.so release/ - cp venvs/cu12/lib/python3.12/site-packages/librmm/lib64/librmm.so release/ - mkdir runtime - cp venvs/cu12/lib/python3.12/site-packages/nvidia/cu12/lib/libcudss.so.0 runtime/ - cp venvs/cu12/lib/python3.12/site-packages/nvidia/cusolver/lib/libcusolver.so.11 runtime/ - cp venvs/cu12/lib/python3.12/site-packages/nvidia/cublas/lib/libcublas.so.12 runtime/ - cp venvs/cu12/lib/python3.12/site-packages/nvidia/cublas/lib/libcublasLt.so.12 runtime/ - cp venvs/cu12/lib/python3.12/site-packages/nvidia/nvjitlink/lib/libnvJitLink.so.12 runtime/ - cp venvs/cu12/lib/python3.12/site-packages/nvidia/curand/lib/libcurand.so.10 runtime/ - cp venvs/cu12/lib/python3.12/site-packages/nvidia/cusparse/lib/libcusparse.so.12 runtime/ + mkdir release-cu12 + cp gmscuopt-cu12.out release-cu12/gmscuopt.out + cp assets/* release-cu12/ + cp venvs/cu12/lib/python3.12/site-packages/libcuopt/lib64/libcuopt.so release-cu12/ + cp venvs/cu12/lib/python3.12/site-packages/libcuopt/lib64/libmps_parser.so release-cu12/ + cp venvs/cu12/lib/python3.12/site-packages/libcuopt_cu12.libs/libgomp-*.so.1.0.0 release-cu12/ + cp venvs/cu12/lib/python3.12/site-packages/libcuopt_cu12.libs/libtbb-*.so.2 release-cu12/ + cp venvs/cu12/lib/python3.12/site-packages/libcuopt_cu12.libs/libtbbmalloc-*.so.2 release-cu12/ + cp venvs/cu12/lib/python3.12/site-packages/rapids_logger/lib64/librapids_logger.so release-cu12/ + cp venvs/cu12/lib/python3.12/site-packages/librmm/lib64/librmm.so release-cu12/ + mkdir runtime-cu12 + cp venvs/cu12/lib/python3.12/site-packages/nvidia/cu12/lib/libcudss.so.0 runtime-cu12/ + cp venvs/cu12/lib/python3.12/site-packages/nvidia/cusolver/lib/libcusolver.so.11 runtime-cu12/ + cp venvs/cu12/lib/python3.12/site-packages/nvidia/cublas/lib/libcublas.so.12 runtime-cu12/ + cp venvs/cu12/lib/python3.12/site-packages/nvidia/cublas/lib/libcublasLt.so.12 runtime-cu12/ + cp venvs/cu12/lib/python3.12/site-packages/nvidia/nvjitlink/lib/libnvJitLink.so.12 runtime-cu12/ + cp venvs/cu12/lib/python3.12/site-packages/nvidia/curand/lib/libcurand.so.10 runtime-cu12/ + cp venvs/cu12/lib/python3.12/site-packages/nvidia/cusparse/lib/libcusparse.so.12 runtime-cu12/ + mkdir release-cu13 + cp gmscuopt-cu13.out release-cu13/gmscuopt.out + cp assets/* release-cu13/ + cp venvs/cu13/lib/python3.12/site-packages/libcuopt/lib64/libcuopt.so release-cu13/ + cp venvs/cu13/lib/python3.12/site-packages/libcuopt/lib64/libmps_parser.so release-cu13/ + cp venvs/cu13/lib/python3.12/site-packages/libcuopt_cu13.libs/libgomp-*.so.1.0.0 release-cu13/ + cp venvs/cu13/lib/python3.12/site-packages/libcuopt_cu13.libs/libtbb-*.so.2 release-cu13/ + cp venvs/cu13/lib/python3.12/site-packages/libcuopt_cu13.libs/libtbbmalloc-*.so.2 release-cu13/ + cp venvs/cu13/lib/python3.12/site-packages/rapids_logger/lib64/librapids_logger.so release-cu13/ + cp venvs/cu13/lib/python3.12/site-packages/librmm/lib64/librmm.so release-cu13/ + mkdir runtime-cu13 + cp venvs/cu13/lib/python3.12/site-packages/nvidia/cu13/lib/libcudss.so.0 runtime-cu13/ + cp venvs/cu13/lib/python3.12/site-packages/nvidia/cusolver/lib/libcusolver.so.12 runtime-cu13/ + cp venvs/cu13/lib/python3.12/site-packages/nvidia/cublas/lib/libcublas.so.13 runtime-cu13/ + cp venvs/cu13/lib/python3.12/site-packages/nvidia/cublas/lib/libcublasLt.so.13 runtime-cu13/ + cp venvs/cu13/lib/python3.12/site-packages/nvidia/nvjitlink/lib/libnvJitLink.so.13 runtime-cu13/ + cp venvs/cu13/lib/python3.12/site-packages/nvidia/curand/lib/libcurand.so.10 runtime-cu13/ + cp venvs/cu13/lib/python3.12/site-packages/nvidia/cusparse/lib/libcusparse.so.12 runtime-cu13/ - - name: Upload link artifact to GitHub Actions (always) + # Upload artifacts + - name: Upload CUDA 12 link artifact to GitHub Actions (always) uses: actions/upload-artifact@v4 with: - name: cuopt-link - path: release/ + name: cuopt-link-cu12 + path: release-cu12/ - - name: Upload runtime artifact to GitHub Actions (always) + - name: Upload CUDA 12 runtime artifact to GitHub Actions (always) uses: actions/upload-artifact@v4 with: name: cu12-runtime - path: runtime/ + path: runtime-cu12/ + + - name: Upload CUDA 13 link artifact to GitHub Actions (always) + uses: actions/upload-artifact@v4 + with: + name: cuopt-link-cu13 + path: release-cu13/ + + - name: Upload CUDA 13 runtime artifact to GitHub Actions (always) + uses: actions/upload-artifact@v4 + with: + name: cu13-runtime + path: runtime-cu13/ + # Bundle into archives - name: Create zip archive (only on tag push) if: startsWith(github.ref, 'refs/tags/') run: | - cd release - zip -r ../cuopt-link-release.zip . - cd ../runtime + cd release-cu12 + zip -r ../cuopt-link-release-cu12.zip . + cd ../runtime-cu12 zip -r ../cu12-runtime.zip . + cd ../release-cu13 + zip -r ../cuopt-link-release-cu13.zip . + cd ../runtime-cu13 + zip -r ../cu13-runtime.zip . + # Create new release with archives - name: Create GitHub Release (only on tag push) if: startsWith(github.ref, 'refs/tags/') uses: softprops/action-gh-release@v2 with: files: | - cuopt-link-release.zip + cuopt-link-release-cu12.zip cu12-runtime.zip + cuopt-link-release-cu13.zip + cu13-runtime.zip env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From a808a60f3544424439e0e85da5dc574e1f3eb418 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Schnabel?= Date: Tue, 21 Oct 2025 13:28:40 +0200 Subject: [PATCH 2/5] Fix path to libnvJitLink.so.13 for CUDA 13 --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f277ba0..88f778e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -58,7 +58,7 @@ jobs: -I $CUOPT/include $JITLINK/libnvJitLink.so.12 -L $CUOPT/lib64 -lcuopt patchelf --set-rpath \$ORIGIN gmscuopt-cu12.out export CUOPT="venvs/cu13/lib/python3.12/site-packages/libcuopt" - export JITLINK="venvs/cu13/lib/python3.12/site-packages/nvidia/nvjitlink/lib" + export JITLINK="venvs/cu13/lib/python3.12/site-packages/nvidia/cu13/lib" export CUOPT_VERSION="`cat "$CUOPT/VERSION"`" export CUOPT_HASH="`cat "$CUOPT/GIT_COMMIT"`" gcc -Wall gmscuopt.c -o gmscuopt-cu13.out \ From c5420dfb2eafacc7120fdfbbfe568aa385375e2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Schnabel?= Date: Tue, 21 Oct 2025 13:32:20 +0200 Subject: [PATCH 3/5] Fix path to nvjitlink for CUDA13 in copy statement --- .github/workflows/main.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 88f778e..c6a2314 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -100,11 +100,11 @@ jobs: cp venvs/cu13/lib/python3.12/site-packages/librmm/lib64/librmm.so release-cu13/ mkdir runtime-cu13 cp venvs/cu13/lib/python3.12/site-packages/nvidia/cu13/lib/libcudss.so.0 runtime-cu13/ - cp venvs/cu13/lib/python3.12/site-packages/nvidia/cusolver/lib/libcusolver.so.12 runtime-cu13/ + cp venvs/cu13/lib/python3.12/site-packages/nvidia/cu13/lib/libnvJitLink.so.13 runtime-cu13/ cp venvs/cu13/lib/python3.12/site-packages/nvidia/cublas/lib/libcublas.so.13 runtime-cu13/ cp venvs/cu13/lib/python3.12/site-packages/nvidia/cublas/lib/libcublasLt.so.13 runtime-cu13/ - cp venvs/cu13/lib/python3.12/site-packages/nvidia/nvjitlink/lib/libnvJitLink.so.13 runtime-cu13/ cp venvs/cu13/lib/python3.12/site-packages/nvidia/curand/lib/libcurand.so.10 runtime-cu13/ + cp venvs/cu13/lib/python3.12/site-packages/nvidia/cusolver/lib/libcusolver.so.12 runtime-cu13/ cp venvs/cu13/lib/python3.12/site-packages/nvidia/cusparse/lib/libcusparse.so.12 runtime-cu13/ # Upload artifacts From 0c01b23a87d7abdf7d284e86e4ee2e0cc9d8930e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Schnabel?= Date: Tue, 21 Oct 2025 13:39:48 +0200 Subject: [PATCH 4/5] More paths to adapt --- .github/workflows/main.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c6a2314..e6c4965 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -101,11 +101,11 @@ jobs: mkdir runtime-cu13 cp venvs/cu13/lib/python3.12/site-packages/nvidia/cu13/lib/libcudss.so.0 runtime-cu13/ cp venvs/cu13/lib/python3.12/site-packages/nvidia/cu13/lib/libnvJitLink.so.13 runtime-cu13/ - cp venvs/cu13/lib/python3.12/site-packages/nvidia/cublas/lib/libcublas.so.13 runtime-cu13/ - cp venvs/cu13/lib/python3.12/site-packages/nvidia/cublas/lib/libcublasLt.so.13 runtime-cu13/ - cp venvs/cu13/lib/python3.12/site-packages/nvidia/curand/lib/libcurand.so.10 runtime-cu13/ - cp venvs/cu13/lib/python3.12/site-packages/nvidia/cusolver/lib/libcusolver.so.12 runtime-cu13/ - cp venvs/cu13/lib/python3.12/site-packages/nvidia/cusparse/lib/libcusparse.so.12 runtime-cu13/ + cp venvs/cu13/lib/python3.12/site-packages/nvidia/cu13/lib/libcublas.so.13 runtime-cu13/ + cp venvs/cu13/lib/python3.12/site-packages/nvidia/cu13/lib/libcublasLt.so.13 runtime-cu13/ + cp venvs/cu13/lib/python3.12/site-packages/nvidia/cu13/lib/libcurand.so.10 runtime-cu13/ + cp venvs/cu13/lib/python3.12/site-packages/nvidia/cu13/lib/libcusolver.so.12 runtime-cu13/ + cp venvs/cu13/lib/python3.12/site-packages/nvidia/cu13/lib/libcusparse.so.12 runtime-cu13/ # Upload artifacts - name: Upload CUDA 12 link artifact to GitHub Actions (always) From a8008cfc94db7fdc95ccaad03a32e280efb48824 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Schnabel?= Date: Tue, 21 Oct 2025 14:06:39 +0200 Subject: [PATCH 5/5] Update notebooks and readme --- README.md | 22 +- examples/trnsport_cuopt.ipynb | 142 +-- examples/trnsport_cuopt_cu13.ipynb | 1871 ++++++++++++++++++++++++++++ 3 files changed, 1949 insertions(+), 86 deletions(-) create mode 100644 examples/trnsport_cuopt_cu13.ipynb diff --git a/README.md b/README.md index 9363618..6f25eca 100644 --- a/README.md +++ b/README.md @@ -8,29 +8,20 @@ You can get more details and tips by reading the blog post ["GPU-Accelerated Opt ## Requirements -- **Operating System:** Linux, Windows through WSL2 +- **Operating System:** Linux, Windows 11 through WSL2 - **GAMS:** Version 49 or newer. - **GAMSPy:** Version 1.12.1 or newer - **NVIDIA GPU:** Volta architecture or better -- **CUDA Runtime Libraries:** 12.0+ +- **CUDA Runtime Libraries:** 12 and 13 ## Getting started / installation - Make sure [CUDA runtime](https://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64) is installed -- Download and unpack `cuopt-link-release.zip` from the [releases page](https://github.com/GAMS-dev/cuoptlink-builder/releases): - - Unpack the contents of `cuopt-link-release.zip` into your GAMS system directory. For GAMSPy, you can find out your system directory by running `gamspy show base`. So for example you can run `unzip -o cuopt-link-release.zip -d $(gamspy show base)`. +- Download and unpack `cuopt-link-release-cu12.zip` or `cuopt-link-release-cu13.zip` (for CUDA 12 and 13 respectively) from the [releases page](https://github.com/GAMS-dev/cuoptlink-builder/releases): + - Unpack the contents of `cuopt-link-release-cu*.zip` into your GAMS system directory. For GAMSPy, you can find out your system directory by running `gamspy show base`. So for example you can run `unzip -o cuopt-link-release-cu*.zip -d $(gamspy show base)`. - **Caution:** This will overwrite any existing `gamsconfig.yaml` file in that directory. The contained `gamsconfig.yaml` contains a `solverConfig` section to make cuOpt available to GAMS. -More specifically, the files from the CUDA runtime needed are -``` -libcublas.so.12 -libcublasLt.so.12 -libcudss.so.0 -libcurand.so.10 -libcusolver.so.11 -libnvJitLink.so.12 -``` -and can be installed e.g. via `pip install --extra-index-url=https://pypi.nvidia.com cuopt-cu12==25.5.* nvidia-cuda-runtime-cu12==12.8.* nvidia-nvjitlink-cu12` into a Python environment or downloaded as archive `cu12-runtime.zip` from the [releases page](https://github.com/GAMS-dev/cuoptlink-builder/releases). +The neccessary files from the CUDA 12 or 13 runtime can also be downloaded as convenient archive `cu12-runtime.zip` or `cu13-runtime.zip` from the [releases page](https://github.com/GAMS-dev/cuoptlink-builder/releases). ## Test the setup @@ -42,4 +33,5 @@ gams trnsport lp cuopt ## Examples -- [examples/trnsport_cuopt.ipynb](examples/trnsport_cuopt.ipynb) +- [examples/trnsport_cuopt.ipynb](examples/trnsport_cuopt.ipynb) for CUDA 12 +- [examples/trnsport_cuopt.ipynb](examples/trnsport_cuopt_cu13.ipynb) for CUDA 13 diff --git a/examples/trnsport_cuopt.ipynb b/examples/trnsport_cuopt.ipynb index e96dad5..3418cb6 100644 --- a/examples/trnsport_cuopt.ipynb +++ b/examples/trnsport_cuopt.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 59, + "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:15.883029Z", @@ -18,10 +18,10 @@ { "data": { "text/plain": [ - "CompletedProcess(args='unzip -q -o cuopt-link-release.zip -d /home/andre/cuoptlink-builder/venv/lib/python3.12/site-packages/gamspy_base', returncode=0)" + "CompletedProcess(args='unzip -q -o cuopt-link-release-cu12.zip -d /home/andre/cuoptlink-builder/venv/lib/python3.12/site-packages/gamspy_base', returncode=0)" ] }, - "execution_count": 59, + "execution_count": 29, "metadata": {}, "output_type": "execute_result" } @@ -32,10 +32,10 @@ "import sys\n", "!pip install -q gamspy\n", "gams_base_path = subprocess.check_output([sys.executable, '-m', 'gamspy', 'show', 'base']).decode('utf-8').strip()\n", - "!wget -nc -nv --show-progress -q \"https://github.com/GAMS-dev/cuoptlink-builder/releases/download/v0.0.3/cu12-runtime.zip\"\n", - "!wget -nc -nv --show-progress -q \"https://github.com/GAMS-dev/cuoptlink-builder/releases/download/v0.0.3/cuopt-link-release.zip\"\n", + "!wget -nc -nv --show-progress -q \"https://github.com/GAMS-dev/cuoptlink-builder/releases/download/v0.0.4/cu12-runtime.zip\"\n", + "!wget -nc -nv --show-progress -q \"https://github.com/GAMS-dev/cuoptlink-builder/releases/download/v0.0.4/cuopt-link-release-cu12.zip\"\n", "subprocess.run(f\"unzip -q -o cu12-runtime.zip -d {gams_base_path}\", shell=True, check=True)\n", - "subprocess.run(f\"unzip -q -o cuopt-link-release.zip -d {gams_base_path}\", shell=True, check=True)" + "subprocess.run(f\"unzip -q -o cuopt-link-release-cu12.zip -d {gams_base_path}\", shell=True, check=True)" ] }, { @@ -91,7 +91,7 @@ }, { "cell_type": "code", - "execution_count": 60, + "execution_count": 30, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:40.915536Z", @@ -149,7 +149,7 @@ "san-diego 600" ] }, - "execution_count": 60, + "execution_count": 30, "metadata": {}, "output_type": "execute_result" } @@ -166,7 +166,7 @@ }, { "cell_type": "code", - "execution_count": 61, + "execution_count": 31, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:41.084284Z", @@ -229,7 +229,7 @@ "topeka 275" ] }, - "execution_count": 61, + "execution_count": 31, "metadata": {}, "output_type": "execute_result" } @@ -244,7 +244,7 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": 32, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:41.087770Z", @@ -326,7 +326,7 @@ " topeka 1.4" ] }, - "execution_count": 62, + "execution_count": 32, "metadata": {}, "output_type": "execute_result" } @@ -349,7 +349,7 @@ }, { "cell_type": "code", - "execution_count": 63, + "execution_count": 33, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:41.092111Z", @@ -374,7 +374,7 @@ }, { "cell_type": "code", - "execution_count": 64, + "execution_count": 34, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:41.093948Z", @@ -399,7 +399,7 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": 35, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:41.164784Z", @@ -436,7 +436,7 @@ }, { "cell_type": "code", - "execution_count": 66, + "execution_count": 36, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:41.173454Z", @@ -460,7 +460,7 @@ }, { "cell_type": "code", - "execution_count": 67, + "execution_count": 37, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:41.180411Z", @@ -490,7 +490,7 @@ }, { "cell_type": "code", - "execution_count": 68, + "execution_count": 38, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:41.191227Z", @@ -546,7 +546,7 @@ "1 san-diego " ] }, - "execution_count": 68, + "execution_count": 38, "metadata": {}, "output_type": "execute_result" } @@ -557,7 +557,7 @@ }, { "cell_type": "code", - "execution_count": 69, + "execution_count": 39, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:41.194209Z", @@ -619,7 +619,7 @@ "2 topeka " ] }, - "execution_count": 69, + "execution_count": 39, "metadata": {}, "output_type": "execute_result" } @@ -641,7 +641,7 @@ }, { "cell_type": "code", - "execution_count": 70, + "execution_count": 40, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:41.196997Z", @@ -697,7 +697,7 @@ "1 san-diego 600.0" ] }, - "execution_count": 70, + "execution_count": 40, "metadata": {}, "output_type": "execute_result" } @@ -715,7 +715,7 @@ }, { "cell_type": "code", - "execution_count": 71, + "execution_count": 41, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:41.202335Z", @@ -777,7 +777,7 @@ "2 topeka 275.0" ] }, - "execution_count": 71, + "execution_count": 41, "metadata": {}, "output_type": "execute_result" } @@ -795,7 +795,7 @@ }, { "cell_type": "code", - "execution_count": 72, + "execution_count": 42, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:41.207532Z", @@ -834,7 +834,7 @@ }, { "cell_type": "code", - "execution_count": 73, + "execution_count": 43, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:41.210541Z", @@ -916,7 +916,7 @@ " topeka 0.126" ] }, - "execution_count": 73, + "execution_count": 43, "metadata": {}, "output_type": "execute_result" } @@ -928,7 +928,7 @@ }, { "cell_type": "code", - "execution_count": 74, + "execution_count": 44, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:41.213745Z", @@ -1015,7 +1015,7 @@ "5 san-diego topeka 0.126" ] }, - "execution_count": 74, + "execution_count": 44, "metadata": {}, "output_type": "execute_result" } @@ -1036,7 +1036,7 @@ }, { "cell_type": "code", - "execution_count": 75, + "execution_count": 45, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:41.219662Z", @@ -1123,7 +1123,7 @@ "5 san-diego topeka 1.4" ] }, - "execution_count": 75, + "execution_count": 45, "metadata": {}, "output_type": "execute_result" } @@ -1141,7 +1141,7 @@ }, { "cell_type": "code", - "execution_count": 76, + "execution_count": 46, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:41.225778Z", @@ -1228,7 +1228,7 @@ "5 san-diego topeka 0.126" ] }, - "execution_count": 76, + "execution_count": 46, "metadata": {}, "output_type": "execute_result" } @@ -1256,7 +1256,7 @@ }, { "cell_type": "code", - "execution_count": 77, + "execution_count": 47, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:41.230884Z", @@ -1299,7 +1299,7 @@ }, { "cell_type": "code", - "execution_count": 78, + "execution_count": 48, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:41.233875Z", @@ -1336,7 +1336,7 @@ }, { "cell_type": "code", - "execution_count": 79, + "execution_count": 49, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:41.237863Z", @@ -1359,7 +1359,7 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": 50, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:41.241111Z", @@ -1390,7 +1390,7 @@ }, { "cell_type": "code", - "execution_count": 81, + "execution_count": 51, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:41.244117Z", @@ -1424,7 +1424,7 @@ }, { "cell_type": "code", - "execution_count": 82, + "execution_count": 52, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:41.246093Z", @@ -1455,7 +1455,7 @@ }, { "cell_type": "code", - "execution_count": 83, + "execution_count": 53, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:41.248970Z", @@ -1502,7 +1502,7 @@ }, { "cell_type": "code", - "execution_count": 84, + "execution_count": 54, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:41.251912Z", @@ -1516,35 +1516,35 @@ "name": "stdout", "output_type": "stream", "text": [ - "--- Job _tk377MxKRH2zBrLeT2QeNQ.gms Start 10/21/25 12:30:48 51.2.1 5fb43f63 LEX-LEG x86 64bit/Linux\n", + "--- Job _Hr3hm7kNTWO3RGVW68CCVg.gms Start 10/21/25 14:04:24 51.2.1 5fb43f63 LEX-LEG x86 64bit/Linux\n", "--- Applying:\n", " /home/andre/cuoptlink-builder/venv/lib/python3.12/site-packages/gamspy_base/gmsprmun.txt\n", " /home/andre/cuoptlink-builder/venv/lib/python3.12/site-packages/gamspy_base/gamsconfig.yaml\n", "--- GAMS Parameters defined\n", " LP cuopt\n", - " Input /tmp/tmpv2h7owhp/_tk377MxKRH2zBrLeT2QeNQ.gms\n", - " Output /tmp/tmpv2h7owhp/_tk377MxKRH2zBrLeT2QeNQ.lst\n", - " ScrDir /tmp/tmpv2h7owhp/tmp0ho_oj3e/\n", + " Input /tmp/tmphm0vba6u/_Hr3hm7kNTWO3RGVW68CCVg.gms\n", + " Output /tmp/tmphm0vba6u/_Hr3hm7kNTWO3RGVW68CCVg.lst\n", + " ScrDir /tmp/tmphm0vba6u/tmpezhqcvh4/\n", " SysDir /home/andre/cuoptlink-builder/venv/lib/python3.12/site-packages/gamspy_base/\n", " LogOption 3\n", - " Trace /tmp/tmpv2h7owhp/_tk377MxKRH2zBrLeT2QeNQ.txt\n", + " Trace /tmp/tmphm0vba6u/_Hr3hm7kNTWO3RGVW68CCVg.txt\n", " License /home/andre/cuoptlink-builder/venv/lib/python3.12/site-packages/gamspy_base/gamslice.txt\n", " OptFile 1\n", - " OptDir /tmp/tmpv2h7owhp/\n", + " OptDir /tmp/tmphm0vba6u/\n", " LimRow 0\n", " LimCol 0\n", " TraceOpt 3\n", - " GDX /tmp/tmpv2h7owhp/_tk377MxKRH2zBrLeT2QeNQout.gdx\n", + " GDX /tmp/tmphm0vba6u/_Hr3hm7kNTWO3RGVW68CCVgout.gdx\n", " SolPrint 0\n", " SolveLink 2\n", " PreviousWork 1\n", " gdxSymbols newOrChanged\n", "System information: 10 physical cores and 16 Gb physical memory detected\n", "--- Starting compilation\n", - "--- _tk377MxKRH2zBrLeT2QeNQ.gms(71) 4 Mb\n", - "--- Starting execution: elapsed 0:00:00.001\n", + "--- _Hr3hm7kNTWO3RGVW68CCVg.gms(71) 4 Mb\n", + "--- Starting execution: elapsed 0:00:00.000\n", "--- Generating LP model transport\n", - "--- _tk377MxKRH2zBrLeT2QeNQ.gms(142) 4 Mb\n", + "--- _Hr3hm7kNTWO3RGVW68CCVg.gms(142) 4 Mb\n", "--- 6 rows 7 columns 19 non-zeroes\n", "--- Range statistics (absolute non-zero finite values)\n", "--- RHS [min, max] : [ 2.750E+02, 6.000E+02] - Zero values observed as well\n", @@ -1552,15 +1552,15 @@ "--- Matrix [min, max] : [ 1.260E-01, 1.000E+00]\n", "--- Executing CUOPT (Solvelink=2): elapsed 0:00:00.001\n", "GAMS/cuOpt link was built against cuOpt version: 25.10.00, git hash: 99e549ce0d4c67f1383187d719f7fd5a7fed33de\n", - "Reading parameter(s) from \"/tmp/tmpv2h7owhp/cuopt.opt\"\n", + "Reading parameter(s) from \"/tmp/tmphm0vba6u/cuopt.opt\"\n", ">> method 1\n", ">> crossover 0\n", - "Finished reading from \"/tmp/tmpv2h7owhp/cuopt.opt\"\n", + "Finished reading from \"/tmp/tmphm0vba6u/cuopt.opt\"\n", "Setting parameter method to 1\n", "Setting parameter crossover to false\n", - "Setting parameter log_file to /tmp/tmpv2h7owhp/tmp0ho_oj3e/cuopt.dat\n", + "Setting parameter log_file to /tmp/tmphm0vba6u/tmpezhqcvh4/cuopt.dat\n", "cuOpt version: 25.10.0, git hash: 99e549c, host arch: x86_64, device archs: 70-real,75-real,80-real,86-real,90a-real,100f-real,120a-real,120\n", - "CPU: 13th Gen Intel(R) Core(TM) i7-13700H, threads (physical/logical): 10/20, RAM: 8.16 GiB\n", + "CPU: 13th Gen Intel(R) Core(TM) i7-13700H, threads (physical/logical): 10/20, RAM: 7.25 GiB\n", "CUDA 12.9, device: NVIDIA RTX A1000 6GB Laptop GPU (ID 0), VRAM: 6.00 GiB\n", "CUDA device UUID: 542affffffe55e-08ffffffe0-0257-fffff\n", "\n", @@ -1574,8 +1574,8 @@ "Third-party presolve is disabled, skipping\n", "Objective offset -0.000000 scaling_factor 1.000000\n", " Iter Primal Obj. Dual Obj. Gap Primal Res. Dual Res. Time\n", - " 0 +0.00000000e+00 +0.00000000e+00 0.00e+00 5.21e+02 4.39e-01 0.067s\n", - " 420 +1.53697337e+02 +1.53675357e+02 2.20e-02 0.00e+00 9.60e-06 0.111s\n", + " 0 +0.00000000e+00 +0.00000000e+00 0.00e+00 5.21e+02 4.39e-01 0.086s\n", + " 420 +1.53697337e+02 +1.53675357e+02 2.20e-02 0.00e+00 9.60e-06 0.127s\n", "LP Solver status: Optimal\n", "Primal objective: +1.53697337e+02\n", "Dual objective: +1.53675357e+02\n", @@ -1583,14 +1583,14 @@ "Primal infeasibility (abs/rel): +0.00e+00 / +0.00e+00\n", "Dual infeasibility (abs/rel): +9.60e-06 / +6.67e-06\n", "PDLP finished\n", - "Status: Optimal Objective: 1.53697337e+02 Iterations: 420 Time: 0.073s, Total time 0.113s\n", + "Status: Optimal Objective: 1.53697337e+02 Iterations: 420 Time: 0.075s, Total time 0.130s\n", "--- Reading solution for model transport\n", - "--- Executing after solve: elapsed 0:00:00.532\n", - "--- _tk377MxKRH2zBrLeT2QeNQ.gms(143) 4 Mb\n", - "--- _tk377MxKRH2zBrLeT2QeNQ.gms(201) 4 Mb\n", - "--- GDX File /tmp/tmpv2h7owhp/_tk377MxKRH2zBrLeT2QeNQout.gdx\n", + "--- Executing after solve: elapsed 0:00:00.528\n", + "--- _Hr3hm7kNTWO3RGVW68CCVg.gms(143) 4 Mb\n", + "--- _Hr3hm7kNTWO3RGVW68CCVg.gms(201) 4 Mb\n", + "--- GDX File /tmp/tmphm0vba6u/_Hr3hm7kNTWO3RGVW68CCVgout.gdx\n", "*** Status: Normal completion\n", - "--- Job _tk377MxKRH2zBrLeT2QeNQ.gms Stop 10/21/25 12:30:49 elapsed 0:00:00.532\n" + "--- Job _Hr3hm7kNTWO3RGVW68CCVg.gms Stop 10/21/25 14:04:25 elapsed 0:00:00.528\n" ] }, { @@ -1634,7 +1634,7 @@ " 7\n", " LP\n", " CUOPT\n", - " 0.113\n", + " 0.13\n", " \n", " \n", "\n", @@ -1645,10 +1645,10 @@ "0 Normal OptimalGlobal 153.697337 6 \n", "\n", " Num of Variables Model Type Solver Solver Time \n", - "0 7 LP CUOPT 0.113 " + "0 7 LP CUOPT 0.13 " ] }, - "execution_count": 84, + "execution_count": 54, "metadata": {}, "output_type": "execute_result" } @@ -1672,7 +1672,7 @@ }, { "cell_type": "code", - "execution_count": 85, + "execution_count": 55, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:41.454555Z", @@ -1786,7 +1786,7 @@ " topeka 275.002378 0.0 0.0 inf 1.0" ] }, - "execution_count": 85, + "execution_count": 55, "metadata": {}, "output_type": "execute_result" } @@ -1812,7 +1812,7 @@ }, { "cell_type": "code", - "execution_count": 86, + "execution_count": 56, "metadata": { "execution": { "iopub.execute_input": "2025-07-17T10:21:41.459304Z", @@ -1828,7 +1828,7 @@ "153.6973372476197" ] }, - "execution_count": 86, + "execution_count": 56, "metadata": {}, "output_type": "execute_result" } diff --git a/examples/trnsport_cuopt_cu13.ipynb b/examples/trnsport_cuopt_cu13.ipynb new file mode 100644 index 0000000..36b57b9 --- /dev/null +++ b/examples/trnsport_cuopt_cu13.ipynb @@ -0,0 +1,1871 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:15.883029Z", + "iopub.status.busy": "2025-07-17T10:21:15.882854Z", + "iopub.status.idle": "2025-07-17T10:21:40.914508Z", + "shell.execute_reply": "2025-07-17T10:21:40.914244Z" + }, + "vscode": { + "languageId": "shellscript" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "CompletedProcess(args='unzip -q -o cuopt-link-release-cu13.zip -d /home/andre/cuoptlink-builder/venv/lib/python3.12/site-packages/gamspy_base', returncode=0)" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# remove -q to debug issues with pip installs\n", + "import subprocess\n", + "import sys\n", + "!pip install -q gamspy\n", + "gams_base_path = subprocess.check_output([sys.executable, '-m', 'gamspy', 'show', 'base']).decode('utf-8').strip()\n", + "!wget -nc -nv --show-progress -q \"https://github.com/GAMS-dev/cuoptlink-builder/releases/download/v0.0.4/cu13-runtime.zip\"\n", + "!wget -nc -nv --show-progress -q \"https://github.com/GAMS-dev/cuoptlink-builder/releases/download/v0.0.4/cuopt-link-release-cu13.zip\"\n", + "subprocess.run(f\"unzip -q -o cu13-runtime.zip -d {gams_base_path}\", shell=True, check=True)\n", + "subprocess.run(f\"unzip -q -o cuopt-link-release-cu13.zip -d {gams_base_path}\", shell=True, check=True)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# GAMSPy model example\n", + "\n", + "## Introduction\n", + "\n", + "This quick start guide is designed to provide you with a concise overview of GAMSPy and its key features. By the end of this guide, you'll have a solid understanding of how to create basic mathematical models using GAMSPy. For more advanced features, we recommend exploring our comprehensive [user guide](https://gamspy.readthedocs.io/en/latest/user/index.html) and the extensive [model library](https://gamspy.readthedocs.io/en/latest/user/model_library.html).\n", + "\n", + "While not mandatory, having a basic understanding of Python programming and familiarity with the [Pandas library](https://pandas.pydata.org/). will be helpful in following this tutorial.\n", + "\n", + "## A Transportation Problem\n", + "\n", + "In this guide, we'll delve into an example of the transportation problem. This classic scenario involves managing supplies from various plants to meet demands at multiple markets for a single commodity. Additionally, we have the unit costs associated with shipping the commodity from plants to markets. The fundamental economic question here is: \n", + "\n", + "> How can we optimize the shipment quantities between each plant and market to minimize the total transport cost?\n", + "\n", + "The problem's algebraic representation typically takes the following format:\n", + "\n", + "Indices (Sets):\n", + "\n", + "- $i$ = plants\n", + "- $j$ = markets\n", + "\n", + "Given Data (Parameters):\n", + "\n", + "- $a_i$ = supply of commodity at plant $i$ (in cases)\n", + "- $b_j$ = demand for commodity at market $j$ (in cases)\n", + "- $c_{ij}$ = cost per unit of shipment between plant $i$ and market $j$\n", + "\n", + "Decision Variables:\n", + "\n", + "- $x_{ij}$ = amount of commodity to ship from plant $i$ to market $j$ where $x_{ij} \\ge 0$ for all $i,j$\n", + "\n", + "Constraints:\n", + "\n", + "- Observe supply limit at plant $i$: $\\sum_j x_{ij} \\le a_i \\: \\forall i$\n", + "- Satisfy demand at market $j$: $\\sum_i x_{ij} \\ge b_j \\: \\forall j$\n", + "- Objective Function: Minimize $\\sum_i \\sum_j c_{ij} \\cdot x_{ij}$\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Data\n", + "\n", + "Before we dive into the optimization process, let's handle our data using the [Pandas library](https://pandas.pydata.org/). We'll begin by organizing the necessary information, which we will subsequently feed into our optimization model." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:40.915536Z", + "iopub.status.busy": "2025-07-17T10:21:40.915457Z", + "iopub.status.idle": "2025-07-17T10:21:41.083438Z", + "shell.execute_reply": "2025-07-17T10:21:41.083230Z" + } + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
capacity
city
seattle350
san-diego600
\n", + "
" + ], + "text/plain": [ + " capacity\n", + "city \n", + "seattle 350\n", + "san-diego 600" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import pandas as pd\n", + "\n", + "capacities = pd.DataFrame(\n", + " [[\"seattle\", 350], [\"san-diego\", 600]], columns=[\"city\", \"capacity\"]\n", + ").set_index(\"city\")\n", + "\n", + "capacities" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:41.084284Z", + "iopub.status.busy": "2025-07-17T10:21:41.084168Z", + "iopub.status.idle": "2025-07-17T10:21:41.086987Z", + "shell.execute_reply": "2025-07-17T10:21:41.086797Z" + } + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
demand
city
new-york325
chicago300
topeka275
\n", + "
" + ], + "text/plain": [ + " demand\n", + "city \n", + "new-york 325\n", + "chicago 300\n", + "topeka 275" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "demands = pd.DataFrame(\n", + " [[\"new-york\", 325], [\"chicago\", 300], [\"topeka\", 275]], columns=[\"city\", \"demand\"]\n", + ").set_index(\"city\")\n", + "\n", + "demands" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:41.087770Z", + "iopub.status.busy": "2025-07-17T10:21:41.087633Z", + "iopub.status.idle": "2025-07-17T10:21:41.091517Z", + "shell.execute_reply": "2025-07-17T10:21:41.091358Z" + } + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
distance
fromto
seattlenew-york2.5
chicago1.7
topeka1.8
san-diegonew-york2.5
chicago1.8
topeka1.4
\n", + "
" + ], + "text/plain": [ + " distance\n", + "from to \n", + "seattle new-york 2.5\n", + " chicago 1.7\n", + " topeka 1.8\n", + "san-diego new-york 2.5\n", + " chicago 1.8\n", + " topeka 1.4" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "distances = pd.DataFrame(\n", + " [\n", + " [\"seattle\", \"new-york\", 2.5],\n", + " [\"seattle\", \"chicago\", 1.7],\n", + " [\"seattle\", \"topeka\", 1.8],\n", + " [\"san-diego\", \"new-york\", 2.5],\n", + " [\"san-diego\", \"chicago\", 1.8],\n", + " [\"san-diego\", \"topeka\", 1.4],\n", + " ],\n", + " columns=[\"from\", \"to\", \"distance\"],\n", + ").set_index([\"from\", \"to\"])\n", + "\n", + "distances" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:41.092111Z", + "iopub.status.busy": "2025-07-17T10:21:41.092042Z", + "iopub.status.idle": "2025-07-17T10:21:41.093337Z", + "shell.execute_reply": "2025-07-17T10:21:41.093174Z" + } + }, + "outputs": [], + "source": [ + "freight_cost = 90" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Symbol Declaration\n", + "\n", + "In line with our systematic breakdown of the transportation problem into sets, parameters, variables, and constraints, we will adopt a similar approach to define the problem as a GAMSPy `Model`. To do so, it is essential to import the `gamspy` library initially." + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:41.093948Z", + "iopub.status.busy": "2025-07-17T10:21:41.093881Z", + "iopub.status.idle": "2025-07-17T10:21:41.163655Z", + "shell.execute_reply": "2025-07-17T10:21:41.163440Z" + } + }, + "outputs": [], + "source": [ + "from gamspy import Container, Set, Parameter, Variable, Equation, Model, Sum, Sense" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Container\n", + "\n", + "Before we proceed further, let's create a `Container` to encapsulate all the relevant information for our GAMSPy ``Model``. This ``Container`` acts as a centralized hub, gathering essential data, sets, parameters, variables, and constraints, providing a clear structure for our optimization problem." + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:41.164784Z", + "iopub.status.busy": "2025-07-17T10:21:41.164605Z", + "iopub.status.idle": "2025-07-17T10:21:41.172679Z", + "shell.execute_reply": "2025-07-17T10:21:41.172515Z" + } + }, + "outputs": [], + "source": [ + "m = Container()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Sets\n", + "\n", + "Sets serve as the fundamental building blocks of a GAMSPy ``Model``, directly corresponding to the indices in the algebraic representations of models. In our transportation problem context, we have defined the following indices:\n", + "\n", + "- $i$ = plants\n", + "- $j$ = markets\n", + "\n", + "For detailed guidance on using sets, please refer to the [set section](https://gamspy.readthedocs.io/en/latest/user/basics/set.html) of our user guide.\n", + "\n", + "There a two ways to declare sets:\n", + "\n", + "1. Separate declaration and data assignment\n", + "2. Combine declaration and data assignment\n", + "\n", + "#### Separate declaration and data assignment\n" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:41.173454Z", + "iopub.status.busy": "2025-07-17T10:21:41.173302Z", + "iopub.status.idle": "2025-07-17T10:21:41.179619Z", + "shell.execute_reply": "2025-07-17T10:21:41.179429Z" + } + }, + "outputs": [], + "source": [ + "i = Set(container=m, name=\"i\", description=\"plants\")\n", + "i.setRecords(capacities.index)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Combine declaration and data assignment\n" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:41.180411Z", + "iopub.status.busy": "2025-07-17T10:21:41.180278Z", + "iopub.status.idle": "2025-07-17T10:21:41.190480Z", + "shell.execute_reply": "2025-07-17T10:21:41.190323Z" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "j = Set(container=m, name=\"j\", description=\"markets\", records=demands.index)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The effect of using the above `Set` statements is that we declared two sets, namely $i$ and $j$. Additionally, we provided descriptions to elaborate on their meaning, enhancing the readability of our ``Model``. Lastly, we assigned members to the sets, establishing a clear connection between the abstract sets and their real-world counterparts.\n", + "\n", + "$i$ = {Seattle, San Diego}\n", + "\n", + "$j$ = {New York, Chicago, Topeka}\n", + "\n", + "To verify the content of a set, you can use `.records`." + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:41.191227Z", + "iopub.status.busy": "2025-07-17T10:21:41.191108Z", + "iopub.status.idle": "2025-07-17T10:21:41.193520Z", + "shell.execute_reply": "2025-07-17T10:21:41.193371Z" + } + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
unielement_text
0seattle
1san-diego
\n", + "
" + ], + "text/plain": [ + " uni element_text\n", + "0 seattle \n", + "1 san-diego " + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "i.records" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:41.194209Z", + "iopub.status.busy": "2025-07-17T10:21:41.194141Z", + "iopub.status.idle": "2025-07-17T10:21:41.196323Z", + "shell.execute_reply": "2025-07-17T10:21:41.196172Z" + } + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
unielement_text
0new-york
1chicago
2topeka
\n", + "
" + ], + "text/plain": [ + " uni element_text\n", + "0 new-york \n", + "1 chicago \n", + "2 topeka " + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "j.records" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Parameters\n", + "\n", + "Declaring parameters involves using `Parameter`. Each parameter is assigned a name and a description. Note that parameter $a_i$ is indexed by $i$. To accommodate these indices, we include the `domain` attribute, pointing to the corresponding set.\n", + "\n", + "It is worth mentioning that, similar to sets, you have the flexibility to either combine or separate the declaration and data assignment steps. For convenience, we will proceed by combining the declaration and data assignment.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:41.196997Z", + "iopub.status.busy": "2025-07-17T10:21:41.196933Z", + "iopub.status.idle": "2025-07-17T10:21:41.201694Z", + "shell.execute_reply": "2025-07-17T10:21:41.201542Z" + } + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
cityvalue
0seattle350.0
1san-diego600.0
\n", + "
" + ], + "text/plain": [ + " city value\n", + "0 seattle 350.0\n", + "1 san-diego 600.0" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = Parameter(\n", + " container=m, \n", + " name=\"a\",\n", + " domain=i,\n", + " description=\"supply of commodity at plant i (in cases)\",\n", + " records=capacities.reset_index(),\n", + ")\n", + "a.records" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:41.202335Z", + "iopub.status.busy": "2025-07-17T10:21:41.202270Z", + "iopub.status.idle": "2025-07-17T10:21:41.206850Z", + "shell.execute_reply": "2025-07-17T10:21:41.206694Z" + } + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
cityvalue
0new-york325.0
1chicago300.0
2topeka275.0
\n", + "
" + ], + "text/plain": [ + " city value\n", + "0 new-york 325.0\n", + "1 chicago 300.0\n", + "2 topeka 275.0" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b = Parameter(\n", + " container=m,\n", + " name=\"b\",\n", + " domain=j,\n", + " description=\"demand for commodity at market j (in cases)\",\n", + " records=demands.reset_index(),\n", + ")\n", + "b.records" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:41.207532Z", + "iopub.status.busy": "2025-07-17T10:21:41.207468Z", + "iopub.status.idle": "2025-07-17T10:21:41.209778Z", + "shell.execute_reply": "2025-07-17T10:21:41.209610Z" + } + }, + "outputs": [], + "source": [ + "c = Parameter(\n", + " container=m,\n", + " name=\"c\",\n", + " domain=[i, j],\n", + " description=\"cost per unit of shipment between plant i and market j\",\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The cost per unit of shipment between plant $i$ and market $j$ is derived from the distance between $i$ and $j$ and can be calculated as follows:\n", + "\n", + "$c_{ij} = \\frac{90 \\cdot d_{ij}}{1000}$,\n", + "\n", + "where $d_{ij}$ denotes the distance between $i$ and $j$.\n", + "\n", + "We have two options to calculate $c_{ij}$ and assign the data to the GAMSPy parameter:\n", + "\n", + "1. Python assignment - calculation in Python, e.g., using Pandas and `.setRecords()`\n", + "2. GAMSPy assignment - calculation in GAMSPy\n", + "\n", + "#### Python Assignment" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:41.210541Z", + "iopub.status.busy": "2025-07-17T10:21:41.210390Z", + "iopub.status.idle": "2025-07-17T10:21:41.213168Z", + "shell.execute_reply": "2025-07-17T10:21:41.213001Z" + } + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
distance
fromto
seattlenew-york0.225
chicago0.153
topeka0.162
san-diegonew-york0.225
chicago0.162
topeka0.126
\n", + "
" + ], + "text/plain": [ + " distance\n", + "from to \n", + "seattle new-york 0.225\n", + " chicago 0.153\n", + " topeka 0.162\n", + "san-diego new-york 0.225\n", + " chicago 0.162\n", + " topeka 0.126" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cost = freight_cost * distances / 1000\n", + "cost" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:41.213745Z", + "iopub.status.busy": "2025-07-17T10:21:41.213681Z", + "iopub.status.idle": "2025-07-17T10:21:41.218945Z", + "shell.execute_reply": "2025-07-17T10:21:41.218784Z" + } + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
fromtovalue
0seattlenew-york0.225
1seattlechicago0.153
2seattletopeka0.162
3san-diegonew-york0.225
4san-diegochicago0.162
5san-diegotopeka0.126
\n", + "
" + ], + "text/plain": [ + " from to value\n", + "0 seattle new-york 0.225\n", + "1 seattle chicago 0.153\n", + "2 seattle topeka 0.162\n", + "3 san-diego new-york 0.225\n", + "4 san-diego chicago 0.162\n", + "5 san-diego topeka 0.126" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c.setRecords(cost.reset_index())\n", + "c.records" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### GAMSPy Assignment\n", + "\n", + "For the direct assignment we need to declare a new ``Parameter`` denoting the distances between $i$ and $j$.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:41.219662Z", + "iopub.status.busy": "2025-07-17T10:21:41.219599Z", + "iopub.status.idle": "2025-07-17T10:21:41.225144Z", + "shell.execute_reply": "2025-07-17T10:21:41.224999Z" + } + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
fromtovalue
0seattlenew-york2.5
1seattlechicago1.7
2seattletopeka1.8
3san-diegonew-york2.5
4san-diegochicago1.8
5san-diegotopeka1.4
\n", + "
" + ], + "text/plain": [ + " from to value\n", + "0 seattle new-york 2.5\n", + "1 seattle chicago 1.7\n", + "2 seattle topeka 1.8\n", + "3 san-diego new-york 2.5\n", + "4 san-diego chicago 1.8\n", + "5 san-diego topeka 1.4" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d = Parameter(\n", + " container=m,\n", + " name=\"d\",\n", + " domain=[i, j],\n", + " description=\"distance between plant i and market j\",\n", + " records=distances.reset_index(),\n", + ")\n", + "d.records" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:41.225778Z", + "iopub.status.busy": "2025-07-17T10:21:41.225715Z", + "iopub.status.idle": "2025-07-17T10:21:41.230168Z", + "shell.execute_reply": "2025-07-17T10:21:41.229995Z" + } + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ijvalue
0seattlenew-york0.225
1seattlechicago0.153
2seattletopeka0.162
3san-diegonew-york0.225
4san-diegochicago0.162
5san-diegotopeka0.126
\n", + "
" + ], + "text/plain": [ + " i j value\n", + "0 seattle new-york 0.225\n", + "1 seattle chicago 0.153\n", + "2 seattle topeka 0.162\n", + "3 san-diego new-york 0.225\n", + "4 san-diego chicago 0.162\n", + "5 san-diego topeka 0.126" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c[i, j] = freight_cost * d[i, j] / 1000\n", + "c.records" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Further information on the usage of parameters can be found in our [parameter section](https://gamspy.readthedocs.io/en/latest/user/basics/parameter.html) of the user guide." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Variables\n", + "\n", + "GAMSPy variables are declared using `Variable`. Each ``Variable`` is assigned a name, a domain if necessary, a type, and, optionally, a description." + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:41.230884Z", + "iopub.status.busy": "2025-07-17T10:21:41.230819Z", + "iopub.status.idle": "2025-07-17T10:21:41.233170Z", + "shell.execute_reply": "2025-07-17T10:21:41.233016Z" + } + }, + "outputs": [], + "source": [ + "x = Variable(\n", + " container=m,\n", + " name=\"x\",\n", + " domain=[i, j],\n", + " type=\"Positive\",\n", + " description=\"amount of commodity to ship from plant i to market j\",\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This statement results in the declaration of a shipment variable for each (i,j) pair.\n", + "\n", + "More information on variables can be found in the [variable section](https://gamspy.readthedocs.io/en/latest/user/basics/variable.html) of our user guide." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Equations\n", + "A GAMSPy ``Equation`` must be declared and defined in two separate statements. The format of the declaration is the same as for other GAMSPy symbols. First comes the keyword, `Equation` in this case, followed by the name, domain and text. The transportation problem has two constraints:\n", + "\n", + "Supply: observe supply limit at plant $i$: $\\sum_j x_{ij} \\le a_i \\: \\forall i$\n", + "\n", + "Demand: satisfy demand at market $j$: $\\sum_i x_{ij} \\ge b_j \\: \\forall j$" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:41.233875Z", + "iopub.status.busy": "2025-07-17T10:21:41.233798Z", + "iopub.status.idle": "2025-07-17T10:21:41.237181Z", + "shell.execute_reply": "2025-07-17T10:21:41.237005Z" + } + }, + "outputs": [], + "source": [ + "supply = Equation(\n", + " container=m, name=\"supply\", domain=i, description=\"observe supply limit at plant i\"\n", + ")\n", + "demand = Equation(\n", + " container=m, name=\"demand\", domain=j, description=\"satisfy demand at market j\"\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The components of an ``Equation`` definition are:\n", + "1. The Python variable of the ``Equation`` being defined\n", + "2. The domain (optional)\n", + "3. Domain restricting conditions (optional)\n", + "4. A `=` sign\n", + "5. Left hand side expression\n", + "6. Relational operator (`==`, `<=`, `>=`)\n", + "7. The right hand side expression.\n", + "\n", + "The ``Equation`` definition for the supply constraint of the transportation problem is implemented as follows:" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:41.237863Z", + "iopub.status.busy": "2025-07-17T10:21:41.237785Z", + "iopub.status.idle": "2025-07-17T10:21:41.240437Z", + "shell.execute_reply": "2025-07-17T10:21:41.240285Z" + } + }, + "outputs": [], + "source": [ + "supply[i] = Sum(j, x[i, j]) <= a[i]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Using the same logic as above, we can define the demand equation as follows:" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:41.241111Z", + "iopub.status.busy": "2025-07-17T10:21:41.241033Z", + "iopub.status.idle": "2025-07-17T10:21:41.243421Z", + "shell.execute_reply": "2025-07-17T10:21:41.243270Z" + } + }, + "outputs": [], + "source": [ + "demand[j] = Sum(i, x[i, j]) >= b[j]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "More information on equations is given in the [equation section](https://gamspy.readthedocs.io/en/latest/user/basics/equation.html) of our user guide." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Objective\n", + "The objective function of a GAMSPy ``Model`` does not require a separate ``Equation`` declaration. You can assign the objective expression to a Python variable or use it directly in the ``Model()`` statement of the [next section](#model).\n" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:41.244117Z", + "iopub.status.busy": "2025-07-17T10:21:41.244038Z", + "iopub.status.idle": "2025-07-17T10:21:41.245432Z", + "shell.execute_reply": "2025-07-17T10:21:41.245247Z" + } + }, + "outputs": [], + "source": [ + "obj = Sum((i, j), c[i, j] * x[i, j])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Model\n", + "\n", + "A GAMSPy `Model()` consolidates constraints, an objective function, a sense (minimize, maximize, and feasibility), and a problem type. It also possesses a name and is associated with a ``Container``.\n", + "\n", + "To define our transportation problem as a GAMSPy ``Model``, we assign it to a Python variable, link it to our ``Container`` (populated with symbols and data), name it \"transport\", specify the equations, set the problem type as linear program (LP), specify the sense of the objective function (``Sense.MIN``), and point to the objective expression.\n", + "\n", + "GAMSPy allows two alternatives to assign equations to a `Model`:\n", + "1. Using a list of equations,\n", + "2. Retrieving _all_ equations by calling `m.getEquations()`.\n", + "\n", + "### Using a List of Equations\n", + "Using a list of equations is especially useful if you want to define multiple GAMSPy ``Model``s with a subset of the equations in your ``Container``. For the transportation problem this can be done as follows:" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:41.246093Z", + "iopub.status.busy": "2025-07-17T10:21:41.246032Z", + "iopub.status.idle": "2025-07-17T10:21:41.248358Z", + "shell.execute_reply": "2025-07-17T10:21:41.248217Z" + } + }, + "outputs": [], + "source": [ + "transport = Model(\n", + " m,\n", + " name=\"transport\",\n", + " equations=[supply, demand],\n", + " problem=\"LP\",\n", + " sense=Sense.MIN,\n", + " objective=obj,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Retrieving all Equations\n", + "Using `m.getEquations()` is especially convenient if you want to include all equations of your ``Container`` to be associated with your model. For the transportation problem this can be done as follows:" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:41.248970Z", + "iopub.status.busy": "2025-07-17T10:21:41.248907Z", + "iopub.status.idle": "2025-07-17T10:21:41.251180Z", + "shell.execute_reply": "2025-07-17T10:21:41.251016Z" + } + }, + "outputs": [], + "source": [ + "transport_2 = Model(\n", + " m,\n", + " name=\"transport2\",\n", + " equations=m.getEquations(),\n", + " problem=\"LP\",\n", + " sense=Sense.MIN,\n", + " objective=obj,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "More information on the usage of a GAMSPy `Model` can be found in the [model section](https://gamspy.readthedocs.io/en/latest/user/basics/model.html) of our user guide." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Solve\n", + "\n", + "Upon defining the GAMSPy ``Model``, it's ready for being solved. The ``solve()`` statement triggers the generation of the specific model instance, creates suitable data structures for the solver, and invokes the solver. To view solver output in the console, the ``sys`` library can be used, passing the ``output=sys.stdout`` attribute to ``transport.solve()``.\n", + "\n", + "**Note**: The following cell contains the two most important pieces to get a GAMSPy model being solved by NVIDIA cuOpt:\n", + "1. Disable solver validation via `gp.set_options({\"SOLVER_VALIDATION\": 0})` as the solver is manually \"plugged into\" GAMSPy\n", + "2. Choose cuOpt as solver with `transport.solve(solver=\"cuopt\", ...)`\n", + "\n", + "Furthermore it sets two cuOpt solver options:\n", + "1. The [LP solution method](https://docs.nvidia.com/cuopt/user-guide/latest/lp-milp-settings.html#method) to PDLP\n", + "2. The [crossover flag](https://docs.nvidia.com/cuopt/user-guide/latest/lp-milp-settings.html#crossover) to false" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:41.251912Z", + "iopub.status.busy": "2025-07-17T10:21:41.251772Z", + "iopub.status.idle": "2025-07-17T10:21:41.453729Z", + "shell.execute_reply": "2025-07-17T10:21:41.453525Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "--- Job _ptz8VUt7QVGccTqAteqKKA.gms Start 10/21/25 13:57:25 51.2.1 5fb43f63 LEX-LEG x86 64bit/Linux\n", + "--- Applying:\n", + " /home/andre/cuoptlink-builder/venv/lib/python3.12/site-packages/gamspy_base/gmsprmun.txt\n", + " /home/andre/cuoptlink-builder/venv/lib/python3.12/site-packages/gamspy_base/gamsconfig.yaml\n", + "--- GAMS Parameters defined\n", + " LP cuopt\n", + " Input /tmp/tmpjahsyl1m/_ptz8VUt7QVGccTqAteqKKA.gms\n", + " Output /tmp/tmpjahsyl1m/_ptz8VUt7QVGccTqAteqKKA.lst\n", + " ScrDir /tmp/tmpjahsyl1m/tmpvzjj8f4d/\n", + " SysDir /home/andre/cuoptlink-builder/venv/lib/python3.12/site-packages/gamspy_base/\n", + " LogOption 3\n", + " Trace /tmp/tmpjahsyl1m/_ptz8VUt7QVGccTqAteqKKA.txt\n", + " License /home/andre/cuoptlink-builder/venv/lib/python3.12/site-packages/gamspy_base/gamslice.txt\n", + " OptFile 1\n", + " OptDir /tmp/tmpjahsyl1m/\n", + " LimRow 0\n", + " LimCol 0\n", + " TraceOpt 3\n", + " GDX /tmp/tmpjahsyl1m/_ptz8VUt7QVGccTqAteqKKAout.gdx\n", + " SolPrint 0\n", + " SolveLink 2\n", + " PreviousWork 1\n", + " gdxSymbols newOrChanged\n", + "System information: 10 physical cores and 16 Gb physical memory detected\n", + "--- Starting compilation\n", + "--- _ptz8VUt7QVGccTqAteqKKA.gms(71) 4 Mb\n", + "--- Starting execution: elapsed 0:00:00.001\n", + "--- Generating LP model transport\n", + "--- _ptz8VUt7QVGccTqAteqKKA.gms(142) 4 Mb\n", + "--- 6 rows 7 columns 19 non-zeroes\n", + "--- Range statistics (absolute non-zero finite values)\n", + "--- RHS [min, max] : [ 2.750E+02, 6.000E+02] - Zero values observed as well\n", + "--- Bound [min, max] : [ NA, NA] - Zero values observed as well\n", + "--- Matrix [min, max] : [ 1.260E-01, 1.000E+00]\n", + "--- Executing CUOPT (Solvelink=2): elapsed 0:00:00.001\n", + "GAMS/cuOpt link was built against cuOpt version: 25.10.00, git hash: 99e549ce0d4c67f1383187d719f7fd5a7fed33de\n", + "Reading parameter(s) from \"/tmp/tmpjahsyl1m/cuopt.opt\"\n", + ">> method 1\n", + ">> crossover 0\n", + "Finished reading from \"/tmp/tmpjahsyl1m/cuopt.opt\"\n", + "Setting parameter method to 1\n", + "Setting parameter crossover to false\n", + "Setting parameter log_file to /tmp/tmpjahsyl1m/tmpvzjj8f4d/cuopt.dat\n", + "cuOpt version: 25.10.0, git hash: 99e549c, host arch: x86_64, device archs: 75-real,80-real,86-real,90a-real,100f-real,120a-real,120\n", + "CPU: 13th Gen Intel(R) Core(TM) i7-13700H, threads (physical/logical): 10/20, RAM: 8.96 GiB\n", + "CUDA 13.0, device: NVIDIA RTX A1000 6GB Laptop GPU (ID 0), VRAM: 6.00 GiB\n", + "CUDA device UUID: 542affffffe55e-08ffffffe0-0257-fffff\n", + "\n", + "Solving a problem with 5 constraints, 6 variables (0 integers), and 12 nonzeros\n", + "Problem scaling:\n", + "Objective coefficents range: [1e-01, 2e-01]\n", + "Constraint matrix coefficients range: [1e+00, 1e+00]\n", + "Constraint rhs / bounds range: [0e+00, 6e+02]\n", + "Variable bounds range: [0e+00, 0e+00]\n", + "\n", + "Third-party presolve is disabled, skipping\n", + "Objective offset -0.000000 scaling_factor 1.000000\n", + " Iter Primal Obj. Dual Obj. Gap Primal Res. Dual Res. Time\n", + " 0 +0.00000000e+00 +0.00000000e+00 0.00e+00 5.21e+02 4.39e-01 0.095s\n", + " 420 +1.53697337e+02 +1.53675357e+02 2.20e-02 0.00e+00 9.60e-06 0.148s\n", + "LP Solver status: Optimal\n", + "Primal objective: +1.53697337e+02\n", + "Dual objective: +1.53675357e+02\n", + "Duality gap (abs/rel): +2.20e-02 / +7.13e-05\n", + "Primal infeasibility (abs/rel): +0.00e+00 / +0.00e+00\n", + "Dual infeasibility (abs/rel): +9.60e-06 / +6.67e-06\n", + "PDLP finished\n", + "Status: Optimal Objective: 1.53697337e+02 Iterations: 420 Time: 0.092s, Total time 0.150s\n", + "--- Reading solution for model transport\n", + "--- Executing after solve: elapsed 0:00:00.462\n", + "--- _ptz8VUt7QVGccTqAteqKKA.gms(143) 4 Mb\n", + "--- _ptz8VUt7QVGccTqAteqKKA.gms(201) 4 Mb\n", + "--- GDX File /tmp/tmpjahsyl1m/_ptz8VUt7QVGccTqAteqKKAout.gdx\n", + "*** Status: Normal completion\n", + "--- Job _ptz8VUt7QVGccTqAteqKKA.gms Stop 10/21/25 13:57:25 elapsed 0:00:00.463\n" + ] + }, + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Solver StatusModel StatusObjectiveNum of EquationsNum of VariablesModel TypeSolverSolver Time
0NormalOptimalGlobal153.69733767LPCUOPT0.15
\n", + "
" + ], + "text/plain": [ + " Solver Status Model Status Objective Num of Equations \\\n", + "0 Normal OptimalGlobal 153.697337 6 \n", + "\n", + " Num of Variables Model Type Solver Solver Time \n", + "0 7 LP CUOPT 0.15 " + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import sys\n", + "import gamspy as gp\n", + "\n", + "gp.set_options({\"SOLVER_VALIDATION\": 0})\n", + "transport.solve(solver=\"cuopt\", solver_options=dict(method=1, crossover=0), output=sys.stdout)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Retrieving Results\n", + "### Variable Values\n", + "The values of the variables in the solution can be retrieved using using ``.records``. The level specifies the shipment quantities $x_{ij}$. Other variable attributes are the marginal values, lower and upper bounds, and the variable's scaling factor." + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:41.454555Z", + "iopub.status.busy": "2025-07-17T10:21:41.454455Z", + "iopub.status.idle": "2025-07-17T10:21:41.458669Z", + "shell.execute_reply": "2025-07-17T10:21:41.458521Z" + } + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
levelmarginallowerupperscale
ij
seattlenew-york49.7364450.00.0inf1.0
chicago300.0955590.00.0inf1.0
topeka0.0000000.00.0inf1.0
san-diegonew-york275.2965200.00.0inf1.0
chicago0.0000000.00.0inf1.0
topeka275.0023780.00.0inf1.0
\n", + "
" + ], + "text/plain": [ + " level marginal lower upper scale\n", + "i j \n", + "seattle new-york 49.736445 0.0 0.0 inf 1.0\n", + " chicago 300.095559 0.0 0.0 inf 1.0\n", + " topeka 0.000000 0.0 0.0 inf 1.0\n", + "san-diego new-york 275.296520 0.0 0.0 inf 1.0\n", + " chicago 0.000000 0.0 0.0 inf 1.0\n", + " topeka 275.002378 0.0 0.0 inf 1.0" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x.records.set_index([\"i\", \"j\"])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Objective Value\n", + "The optimal objective function value can be accessed by `.objective_value`." + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": { + "execution": { + "iopub.execute_input": "2025-07-17T10:21:41.459304Z", + "iopub.status.busy": "2025-07-17T10:21:41.459237Z", + "iopub.status.idle": "2025-07-17T10:21:41.460707Z", + "shell.execute_reply": "2025-07-17T10:21:41.460556Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "153.6973372476197" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "transport.objective_value" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. SPDX-License-Identifier: MIT Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n", + "\n", + "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "venv", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.3" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}