diff --git a/docs/ext/pymoo.ipynb b/docs/ext/pymoo.ipynb new file mode 100644 index 0000000..9c5d049 --- /dev/null +++ b/docs/ext/pymoo.ipynb @@ -0,0 +1,101 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "0", + "metadata": {}, + "source": [ + "# Pymoo External Connector\n", + "ParetoBench's benchmark problems can be used in [pymoo](https://pymoo.org/) through the wrapper class `PymooProblemWrapper`. On this page, we demonstrate running one of pymoo's optimization algorithms against a ParetoBench problem." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1", + "metadata": {}, + "outputs": [], + "source": [ + "from pymoo.algorithms.moo.nsga2 import NSGA2\n", + "from pymoo.optimize import minimize\n", + "\n", + "import paretobench as pb\n", + "from paretobench.ext.pymoo import PymooProblemWrapper" + ] + }, + { + "cell_type": "markdown", + "id": "2", + "metadata": {}, + "source": [ + "### Using ParetoBench Problems in Pymoo\n", + "`PymooProblemWrapper` subclasses pymoo's `Problem` class and evaluates the ParetoBench problem using vectorization. Data such as number of objectives, variables and their bounds, etc are passed through from the ParetoBench problem." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3", + "metadata": {}, + "outputs": [], + "source": [ + "# Wrap one of ParetoBench's problems for use in pymoo\n", + "prob = PymooProblemWrapper.from_line_fmt(\"ZDT3\")\n", + "prob" + ] + }, + { + "cell_type": "markdown", + "id": "4", + "metadata": {}, + "source": [ + "The wrapper may be passed directly to pymoo's algorithms. We will run a short optimization with pymoo's NSGA-II implementation." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5", + "metadata": {}, + "outputs": [], + "source": [ + "# Run a short optimization\n", + "res = minimize(prob, NSGA2(pop_size=32), (\"n_gen\", 64), seed=1, verbose=False)\n", + "print(f\"Number of nondominated solutions: {len(res.X)}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6", + "metadata": {}, + "outputs": [], + "source": [ + "# Load the result into a ParetoBench Population and plot it against the true Pareto front\n", + "pop = pb.Population(x=res.X, f=res.F)\n", + "pop.plot_obj_scatter(problem=\"ZDT3\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "paretobench", + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/mkdocs.yml b/mkdocs.yml index ac24cd2..b170723 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -36,3 +36,4 @@ nav: - IGD Analysis: analysis/inverted_generational_distance_analysis.ipynb - External Connectors: - Xopt: ext/xopt.ipynb + - Pymoo: ext/pymoo.ipynb diff --git a/pyproject.toml b/pyproject.toml index c5a235f..92d9442 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -38,6 +38,7 @@ docs = [ "mkdocs", "mkdocs-jupyter", "mkdocs-material", + "pymoo", "xopt" ]