Skip to content

3d Visualizations of optimization problems #581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b8c33dd
Refactored slice_plot to support different projections (3D and contour)
shammeer-s Apr 12, 2025
8dcb9e2
Refactored codebase to decouple data processing logic from visualizat…
shammeer-s Apr 13, 2025
8e2bfe0
Enhance sliceplot with comprehensive docstrings and minor refactoring
shammeer-s Apr 13, 2025
8b4cc8c
Minor bug fixes
shammeer-s Apr 13, 2025
9316ea8
Merge branch 'optimagic-dev:main' into visualization
shammeer-s Apr 23, 2025
7782546
Slice plot 3D implementation in a sandbox version
shammeer-s Apr 24, 2025
7108c7d
Merge remote-tracking branch 'origin/visualization' into visualization
shammeer-s Apr 24, 2025
1fdde7f
Slice plot 3D implementation in a sandbox version
shammeer-s Apr 27, 2025
8087689
Slice plot 3D implementation in a sandbox version
shammeer-s Apr 27, 2025
834a779
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 27, 2025
1164bd4
Add notebook for visualizing 3D slice plots and update sandbox imports
shammeer-s Apr 28, 2025
e995aa3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 28, 2025
300a460
Enhance 3D slice plot visualization notebook and refactor plotting fu…
shammeer-s Apr 28, 2025
5ba721c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 28, 2025
1c8243f
Documentation strings are updated for all functions in slice_plot_3d.…
shammeer-s Apr 28, 2025
14d5a32
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 28, 2025
5cdfb56
Merge branch 'main' into visualization
timmens Apr 29, 2025
2a53a89
Minor changes according to previous slice_plot.py file logic
shammeer-s May 4, 2025
382cd15
Minor fixes with code login on evaluating kwargs
shammeer-s May 4, 2025
b3caa04
Minor fixes with code login on evaluating kwargs
shammeer-s May 6, 2025
091eb80
Merge remote-tracking branch 'origin/visualization' into visualization
shammeer-s May 6, 2025
a9405d3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 6, 2025
5c08b18
Merge branch 'main' into visualization
timmens May 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
290 changes: 290 additions & 0 deletions docs/source/how_to/how_to_slice_plot_3d.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "0",
"metadata": {},
"source": [
"# Visualizing Objective Functions with `slice_plot_3d`\n",
"\n",
"In optimization, understanding the shape of your objective function is a key step toward choosing the right algorithm.\n",
"\n",
"This notebook introduces the `slice_plot_3d` tool, which provides flexible ways to visualize:\n",
"- Single-parameter sensitivity through **slice plots**,\n",
"- Pairwise interactions through **contour** or **surface plots**,\n",
"- Full parameter relationships through **subplot grids**.\n",
"\n",
"We will progress from basic to advanced usage, learning how to create clean and insightful plots easily.\n"
]
},
{
"cell_type": "markdown",
"id": "1",
"metadata": {},
"source": [
"## 1D Slice Plot\n",
"\n",
"We start with a **1D slice plot**.\n",
"This plots the function along each parameter individually to the function value,\n",
"while fixing others at their current values. This provides a clean view of how sensitive the function is to each parameter separately. We use the **Sphere function**, which sums the squares of each input.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"import optimagic as om"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3",
"metadata": {},
"outputs": [],
"source": [
"# Define the Sphere function\n",
"def sphere(params):\n",
" x = np.array(list(params.values()))\n",
" return np.sum(x**2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4",
"metadata": {},
"outputs": [],
"source": [
"params = {\"alpha\": 0, \"beta\": 0, \"gamma\": 0, \"delta\": 0}\n",
"bounds = om.Bounds(\n",
" lower={name: -5 for name in params},\n",
" upper={name: i + 2 for i, name in enumerate(params)},\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5",
"metadata": {
"jupyter": {
"is_executing": true
}
},
"outputs": [],
"source": [
"fig = om.sandbox.slice_plot_3d(\n",
" func=sphere,\n",
" params=params,\n",
" bounds=bounds,\n",
")\n",
"fig.show()"
]
},
{
"cell_type": "markdown",
"id": "6",
"metadata": {},
"source": [
"## Slice Plot with Selected Parameters\n",
"\n",
"In many situations, we are interested in exploring only specific parameters.\n",
"Using the `selector` argument, we can restrict the slice plots to\n",
"chosen parameters — here, we select `\"alpha\"` and `\"beta\"`.\n",
"\n",
"This focuses our visualization on dimensions of interest."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7",
"metadata": {},
"outputs": [],
"source": [
"fig = om.sandbox.slice_plot_3d(\n",
" func=sphere,\n",
" params=params,\n",
" bounds=bounds,\n",
" selector=lambda p: [p[\"alpha\"], p[\"beta\"]],\n",
" projection=\"slice\",\n",
")\n",
"fig.show(renderer=\"png\")"
]
},
{
"cell_type": "markdown",
"id": "8",
"metadata": {},
"source": [
"## 3D Surface Plot for Two Parameters\n",
"\n",
"To better understand interaction between parameters,\n",
"we can switch to a **3D surface plot**.\n",
"\n",
"Surface plots reveal valleys, ridges, and general landscape shapes clearly.\n",
"Here, we vary `\"alpha\"` and `\"beta\"` simultaneously and plot the resulting surface."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9",
"metadata": {},
"outputs": [],
"source": [
"fig = om.sandbox.slice_plot_3d(\n",
" func=sphere,\n",
" params=params,\n",
" bounds=bounds,\n",
" selector=lambda p: [p[\"alpha\"], p[\"beta\"]],\n",
" projection=\"surface\",\n",
" n_gridpoints=30,\n",
")\n",
"fig.show(renderer=\"png\")"
]
},
{
"cell_type": "markdown",
"id": "10",
"metadata": {},
"source": [
"## 2D Contour Plot for Two Parameters\n",
"\n",
"Contour plots offer a 2D view with iso-function-value curves.\n",
"\n",
"They are especially useful for:\n",
"- Finding basins or valleys.\n",
"- Visualizing optimization paths.\n",
"- Detecting steep or flat regions easily.\n",
"\n",
"Again, we use `\"alpha\"` and `\"beta\"` to generate the plot."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "11",
"metadata": {},
"outputs": [],
"source": [
"fig = om.sandbox.slice_plot_3d(\n",
" func=sphere,\n",
" params=params,\n",
" bounds=bounds,\n",
" selector=lambda p: [p[\"alpha\"], p[\"beta\"]],\n",
" projection=\"contour\",\n",
" n_gridpoints=30,\n",
")\n",
"fig.show(renderer=\"png\")"
]
},
{
"cell_type": "markdown",
"id": "12",
"metadata": {},
"source": [
"## Grid View for Multiple Parameters\n",
"\n",
"When selecting **more than two parameters**, `slice_plot_3d`\n",
"automatically builds a grid:\n",
"\n",
"- **Diagonal** elements: 1D slice plots for each parameter.\n",
"- **Off-diagonal** elements: 3D surface (or contour) plots for parameter pairs.\n",
"\n",
"This creates a rich overview showing both individual and pairwise effects."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "13",
"metadata": {},
"outputs": [],
"source": [
"fig = om.sandbox.slice_plot_3d(\n",
" func=sphere,\n",
" params=params,\n",
" bounds=bounds,\n",
" projection=\"surface\",\n",
" n_gridpoints=20,\n",
")\n",
"fig.show(renderer=\"png\")"
]
},
{
"cell_type": "markdown",
"id": "14",
"metadata": {},
"source": [
"## Full Customization of the Visualization\n",
"\n",
"`s‍lice_plot_3d` allows fine control over plot styling:\n",
"\n",
"- `layout_kwargs` adjusts figure size, titles, background themes.\n",
"- `plot_kwargs` controls color maps, marker options, and plot styles.\n",
"- `make_subplot_kwargs` configures grid spacing, axis sharing, and more.\n",
"\n",
"Here, we demonstrate a fully customized plot combining all these features."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "15",
"metadata": {},
"outputs": [],
"source": [
"fig = om.sandbox.slice_plot_3d(\n",
" func=sphere,\n",
" params=params,\n",
" bounds=bounds,\n",
" selector=lambda p: [p[\"alpha\"], p[\"beta\"], p[\"gamma\"]],\n",
" projection=\"surface\",\n",
" n_gridpoints=40,\n",
" layout_kwargs={\n",
" \"width\": 800,\n",
" \"height\": 800,\n",
" \"title\": {\"text\": \"Customized Sphere Function Visualization\"},\n",
" \"template\": \"plotly_dark\",\n",
" },\n",
" make_subplot_kwargs={\n",
" \"horizontal_spacing\": 0.1,\n",
" \"vertical_spacing\": 0.1,\n",
" },\n",
" plot_kwargs={\n",
" \"surface_plot\": {\"colorscale\": \"Viridis\", \"opacity\": 0.7},\n",
" },\n",
")\n",
"fig.show(renderer=\"png\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.17"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
3 changes: 2 additions & 1 deletion src/optimagic/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from optimagic import constraints, mark, timing, utilities
from optimagic import constraints, mark, sandbox, timing, utilities
from optimagic.algorithms import algos
from optimagic.benchmarking.benchmark_reports import (
convergence_report,
Expand Down Expand Up @@ -103,4 +103,5 @@
"__version__",
"algos",
"timing",
"sandbox",
]
3 changes: 3 additions & 0 deletions src/optimagic/sandbox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from optimagic.visualization.slice_plot_3d import slice_plot_3d

__all__ = ["slice_plot_3d"]
Loading
Loading