{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Tutorial 2: ready-to-use benchmarks and visualization" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "nbsphinx": "hidden" }, "outputs": [], "source": [ "# Set Plotly to render plots as interactive in notebooks (this cell will be hidden: I set \"nbsphinx\": \"hidden\" in the cell metadata)\n", "import plotly.io as pio\n", "pio.renderers.default = \"notebook\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the previous tutorial, you have learnt how to define your own custom benchmark function using the BenchmarkFunction class. In this tutorial, you will learn how to use the ready-to-use benchmark functions provided by the `beeoptimal.benchmarks` module. These pre-defined functions are designed to help you quickly test optimization algorithms without having to implement the functions yourself. Additionally, you will get introduced to some visualization tools that allow a better understanding of the optimization landscapes.\n", "\n", "At the moment, the available ready-to-use benchmark functions are the following:\n", "\n", "- Sphere\n", "- Rosenbrock\n", "- Ackley\n", "- Rastrigin\n", "- Weierstrass\n", "- Griewank\n", "- Schwefel\n", "- Sumsquares\n", "- Eggholder\n", "\n", "Except from the Eggholder, which is inherently a 2D function, all the other functions are available in 2,10 and 30 dimensions." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For this tutorial, we will focus on the 2D Griewank function, as its landscape offers interesting features that become more apparent depending on the zoom level we apply." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from beeoptimal.benchmarks import Griewank2d\n", "from beeoptimal.plotting import contourplot,surfaceplot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can easily evaluate the function in the same way as we did in the previous tutorial." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Benchmark:\n", " Griewank-2d\n", "\n", "Default Bounds:\n", " [[-600 600]\n", " [-600 600]]\n", "\n", "Optimal Solution:\n", " [0. 0.]\n", "\n", "Optimal Value:\n", " 0.0\n" ] } ], "source": [ "print(f\"\\nBenchmark:\\n {Griewank2d.name}\")\n", "print(f\"\\nDefault Bounds:\\n {Griewank2d.bounds}\")\n", "print(f\"\\nOptimal Solution:\\n {Griewank2d.optimal_solution}\")\n", "print(f\"\\nOptimal Value:\\n {Griewank2d.optimal_value}\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Griewank-2d function evaluated at x=[-572.05440973 -548.83516035] --> f(x)=158.02230710936988\n" ] } ], "source": [ "x = np.random.uniform(low=Griewank2d.bounds[:,0], high=Griewank2d.bounds[:,1], size=len(Griewank2d.bounds))\n", "f_x = Griewank2d.evaluate(x)\n", "print(f\"{Griewank2d.name} function evaluated at x={x} --> f(x)={f_x}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As mentioned above, `BeeOptimal` provides two visualization tools that facilitate the exploration of the benchmark functions:\n", "\n", "- `contourplot`\n", "- `surfaceplot`\n", "\n", ".. warning::\n", " These visualization tools are designed specifically for 2D functions, as we cannot visualize more than three dimensions (unless you have exceeded the glasses of brandy...😵💫🥃)\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The most simple way to use them is the following:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "