diff --git a/package/samplers/whale_optimization/README.md b/package/samplers/whale_optimization/README.md index c9ed91e6..82f06d7b 100644 --- a/package/samplers/whale_optimization/README.md +++ b/package/samplers/whale_optimization/README.md @@ -13,6 +13,8 @@ license: MIT License ## Example +[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/optuna/optunahub-registry/blob/main/package/samplers/whale_optimization/example.ipynb) + ```python from __future__ import annotations @@ -32,12 +34,7 @@ if __name__ == "__main__": y = trial.suggest_float("y", -10, 10) return x**2 + y**2 - sampler = WhaleOptimizationSampler( - { - "x": optuna.distributions.FloatDistribution(-10, 10), - "y": optuna.distributions.FloatDistribution(-10, 10), - } - ) + sampler = WhaleOptimizationSampler() study = optuna.create_study(sampler=sampler) study.optimize(objective, n_trials=100) optuna.visualization.matplotlib.plot_optimization_history(study) diff --git a/package/samplers/whale_optimization/example.ipynb b/package/samplers/whale_optimization/example.ipynb new file mode 100644 index 00000000..63d0451d --- /dev/null +++ b/package/samplers/whale_optimization/example.ipynb @@ -0,0 +1,188 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "source": [ + "# Example usage of `WhaleOptimizationSampler` 🐳\n", + "\n", + "This notebook demonstrates a simple usage of [`WhaleOptimizationSampler`](https://hub.optuna.org/samplers/whale_optimization/) in [OptunaHub](https://hub.optuna.org/). \n", + "- `WhaleOptimizationSampler` is an implementation of the [Whale Optimization Algorithm (WOA)](https://www.sciencedirect.com/science/article/abs/pii/S0965997816300163). \n", + "- WOA is a nature-inspired meta-heuristic optimization algorithm that mimics the bubble-net feeding behavior of humpback whales.\n", + "\n", + "## Step 1: Imports\n", + "\n", + "This example requires the following two modules:\n", + "- `optuna`: A hyperparameter optimization framework\n", + "- `oputnahub`: A module for loading additional components of Optuna" + ], + "metadata": { + "id": "u3QqXvzOFAeE" + } + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "id": "pws57WUlE1te" + }, + "outputs": [], + "source": [ + "!pip install --quiet --progress-bar off optuna optunahub" + ] + }, + { + "cell_type": "code", + "source": [ + "import optuna\n", + "import optunahub" + ], + "metadata": { + "id": "Wx2Ov0CyF9lB" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## Step 2: Define objective function\n", + "\n", + "As a demonstration, we use a simple quadratic function as an objective function." + ], + "metadata": { + "id": "LdYS50siFywV" + } + }, + { + "cell_type": "code", + "source": [ + "def objective(trial: optuna.trial.Trial) -> float:\n", + " x = trial.suggest_float(\"x\", -10, 10)\n", + " y = trial.suggest_float(\"y\", -10, 10)\n", + " return x**2 + y**2" + ], + "metadata": { + "id": "lNInSywME95H" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## Step 3: Load `WhaleOptimizationSampler`\n", + "\n", + "With `optunahub.load_module`, you can use modules in [Optunanub](https://hub.optuna.org/) in your code. \n", + "In this case, a module defined in [samplers/whale_optimization](https://github.com/optuna/optunahub-registry/tree/main/package/samplers/whale_optimization) is loaded, and you can instantiate `WhaleOptimizationSampler` from the module." + ], + "metadata": { + "id": "yIEfLon-GlvV" + } + }, + { + "cell_type": "code", + "source": [ + "mod = optunahub.load_module(\"samplers/whale_optimization\")\n", + "sampler = mod.WhaleOptimizationSampler()" + ], + "metadata": { + "id": "0nH6__u5F5uQ" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## Step 4: Run optimization\n", + "\n", + "You can optimize the objective function with `WhaleOptimizationSampler` as usual." + ], + "metadata": { + "id": "Vx2XljcNG0o_" + } + }, + { + "cell_type": "code", + "source": [ + "study = optuna.create_study(sampler=sampler)\n", + "study.optimize(objective, n_trials=100)" + ], + "metadata": { + "id": "zWEKYXYaG3Sg" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "optuna.visualization.plot_optimization_history(study)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 542 + }, + "id": "NuhMdbUqG5U2", + "outputId": "c0d4fbd4-6b27-4dc3-a5f5-c05f5a857d4c" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "display_data", + "data": { + "text/html": [ + "\n", + "\n", + "\n", + "
\n", + "
\n", + "\n", + "" + ] + }, + "metadata": {} + } + ] + } + ] +} \ No newline at end of file diff --git a/recipes/002_registration.py b/recipes/002_registration.py index f69ecb47..eb3f1ec0 100644 --- a/recipes/002_registration.py +++ b/recipes/002_registration.py @@ -18,7 +18,7 @@ | ├── __init__.py | ├── README.md | ├── LICENSE -| ├── (example.py) +| ├── (example.py, example.ipynb) | ├── (requirements.txt) | └── (images) | ├── (figure1.png) @@ -36,7 +36,7 @@ - `__init__.py`: An initialization file. This file must implement your algorithm or import its implementation from another file, e.g., `YOUR_ALGORITHM_NAME.py`. - `README.md`: A description of your algorithm. This file is used to create an `web page of OptunaHub `_. Let me explain the format of the `README.md` file later. - `LICENSE`: A license file. This file must contain the license of your algorithm. It should be the MIT license in the alpha version of OptunaHub. -- `example.py`: This is optional. This file should contain a simple example of how to use your algorithm (Example: `example.py for Simulated Annealing Sampler `_). +- `example.py`, `example.ipynb`: This is optional. This file should contain a simple example of how to use your algorithm (Example: `example.py for Simulated Annealing Sampler `_). You can provide examples in both formats. - `requirements.txt`: This is optional. A file that contains the additional dependencies of your algorithm. If there are no additional dependencies other than Optuna and OptunaHub, you do not need to create this file. - `images`: This is optional. A directory that contains images. Only relative references to images in this directory are allowed in README.md, e.g., ``![Numrical Results](images/numerical_results.png)``, and absolute paths to images are not allowed. The first image that appears in README.md will be used as the thumbnail.