|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# SPOT Kriging in 2 Dimensions: Nyström Approximation vs. Exact (Rosenbrock Function)\n", |
| 8 | + "\n", |
| 9 | + "This notebook demonstrates how to use the `Spot` class from `spotpython` with and without the Nyström approximation for Kriging surrogates on the 2-dimensional Rosenbrock function.\n", |
| 10 | + "\n", |
| 11 | + "We use a maximum of 100 function evaluations." |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "code", |
| 16 | + "execution_count": null, |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [], |
| 19 | + "source": [ |
| 20 | + "import numpy as np\n", |
| 21 | + "from spotpython.fun.objectivefunctions import Analytical\n", |
| 22 | + "from spotpython.spot import Spot\n", |
| 23 | + "from spotpython.utils.init import fun_control_init, design_control_init, surrogate_control_init" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "markdown", |
| 28 | + "metadata": {}, |
| 29 | + "source": [ |
| 30 | + "## Define the Rosenbrock Function" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "code", |
| 35 | + "execution_count": null, |
| 36 | + "metadata": {}, |
| 37 | + "outputs": [], |
| 38 | + "source": [ |
| 39 | + "# do not change, these are given by the organizers\n", |
| 40 | + "dim = 6\n", |
| 41 | + "lower = np.full(dim, -2)\n", |
| 42 | + "upper = np.full(dim, 2)\n", |
| 43 | + "fun = Analytical().fun_rosenbrock\n", |
| 44 | + "fun_evals = 100" |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "markdown", |
| 49 | + "metadata": {}, |
| 50 | + "source": [ |
| 51 | + "## Set up SPOT Controls" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "markdown", |
| 56 | + "metadata": {}, |
| 57 | + "source": [ |
| 58 | + "## Aquisition Random and Regression and y as Infill Criterion" |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "code", |
| 63 | + "execution_count": null, |
| 64 | + "metadata": {}, |
| 65 | + "outputs": [], |
| 66 | + "source": [ |
| 67 | + "init_size = 12\n", |
| 68 | + "use_nystrom = False\n", |
| 69 | + "method = \"regression\"\n", |
| 70 | + "infill_criterion = \"ei\"\n", |
| 71 | + "tolerance_x = 1e-6 # minimum distance in x for accepting new points\n", |
| 72 | + "seed = 321\n", |
| 73 | + "max_surrogate_points = 24\n", |
| 74 | + "min_Lambda = -6\n", |
| 75 | + "max_Lambda = 1\n", |
| 76 | + "min_theta = -3\n", |
| 77 | + "max_theta = 2\n", |
| 78 | + "isotropic = False\n", |
| 79 | + "optim_p = True\n", |
| 80 | + "min_success_rate = 0.10 # percent\n", |
| 81 | + "acquisition_failure_strategy = \"random\"" |
| 82 | + ] |
| 83 | + }, |
| 84 | + { |
| 85 | + "cell_type": "code", |
| 86 | + "execution_count": null, |
| 87 | + "metadata": {}, |
| 88 | + "outputs": [], |
| 89 | + "source": [ |
| 90 | + "fun_control = fun_control_init(\n", |
| 91 | + " lower=lower,\n", |
| 92 | + " upper=upper,\n", |
| 93 | + " fun_evals=fun_evals,\n", |
| 94 | + " max_time=60,\n", |
| 95 | + " seed=seed,\n", |
| 96 | + " show_progress=True,\n", |
| 97 | + " infill_criterion=infill_criterion,\n", |
| 98 | + " min_success_rate=min_success_rate,\n", |
| 99 | + " tolerance_x=tolerance_x,\n", |
| 100 | + " TENSORBOARD_CLEAN=True,\n", |
| 101 | + " tensorboard_log=True,\n", |
| 102 | + " acquisition_failure_strategy=acquisition_failure_strategy,\n", |
| 103 | + ")\n", |
| 104 | + "design_control = design_control_init(init_size=init_size)\n", |
| 105 | + "surrogate_control_exact = surrogate_control_init(use_nystrom=use_nystrom, \n", |
| 106 | + " optim_p=optim_p,\n", |
| 107 | + " method=method, max_surrogate_points=max_surrogate_points, min_Lambda=min_Lambda, max_Lambda=max_Lambda, min_theta=min_theta, max_theta=max_theta, isotropic=isotropic)\n", |
| 108 | + "spot_exact_random = Spot(\n", |
| 109 | + " fun=fun,\n", |
| 110 | + " fun_control=fun_control,\n", |
| 111 | + " design_control=design_control,\n", |
| 112 | + " surrogate_control=surrogate_control_exact\n", |
| 113 | + " )\n", |
| 114 | + "spot_exact_random.run()\n", |
| 115 | + "spot_exact_random.plot_progress(log_y=True, title=\"spotpython: Random Kriging\")" |
| 116 | + ] |
| 117 | + }, |
| 118 | + { |
| 119 | + "cell_type": "markdown", |
| 120 | + "metadata": {}, |
| 121 | + "source": [ |
| 122 | + "## Aquisition MM and Regression and y as Infill Criterion" |
| 123 | + ] |
| 124 | + }, |
| 125 | + { |
| 126 | + "cell_type": "code", |
| 127 | + "execution_count": null, |
| 128 | + "metadata": {}, |
| 129 | + "outputs": [], |
| 130 | + "source": [ |
| 131 | + "acquisition_failure_strategy = \"mm\"" |
| 132 | + ] |
| 133 | + }, |
| 134 | + { |
| 135 | + "cell_type": "code", |
| 136 | + "execution_count": null, |
| 137 | + "metadata": {}, |
| 138 | + "outputs": [], |
| 139 | + "source": [ |
| 140 | + "fun_control = fun_control_init(\n", |
| 141 | + " lower=lower,\n", |
| 142 | + " upper=upper,\n", |
| 143 | + " fun_evals=fun_evals,\n", |
| 144 | + " max_time=60,\n", |
| 145 | + " seed=seed,\n", |
| 146 | + " show_progress=True,\n", |
| 147 | + " infill_criterion=infill_criterion,\n", |
| 148 | + " min_success_rate=min_success_rate,\n", |
| 149 | + " tolerance_x=tolerance_x,\n", |
| 150 | + " TENSORBOARD_CLEAN=True,\n", |
| 151 | + " tensorboard_log=True,\n", |
| 152 | + " acquisition_failure_strategy=acquisition_failure_strategy,\n", |
| 153 | + ")\n", |
| 154 | + "design_control = design_control_init(init_size=init_size)\n", |
| 155 | + "surrogate_control_exact = surrogate_control_init(use_nystrom=use_nystrom, optim_p=optim_p, method=method, max_surrogate_points=max_surrogate_points, min_Lambda=min_Lambda, max_Lambda=max_Lambda, min_theta=min_theta, max_theta=max_theta, isotropic=isotropic)\n", |
| 156 | + "spot_exact_mm = Spot(\n", |
| 157 | + " fun=fun,\n", |
| 158 | + " fun_control=fun_control,\n", |
| 159 | + " design_control=design_control,\n", |
| 160 | + " surrogate_control=surrogate_control_exact\n", |
| 161 | + ")\n", |
| 162 | + "spot_exact_mm.run()\n", |
| 163 | + "spot_exact_mm.plot_progress(log_y=True, title=\"spotpython: Morris-Mitchell Kriging.\")" |
| 164 | + ] |
| 165 | + }, |
| 166 | + { |
| 167 | + "cell_type": "markdown", |
| 168 | + "metadata": {}, |
| 169 | + "source": [ |
| 170 | + "## Compare Minimum Found" |
| 171 | + ] |
| 172 | + }, |
| 173 | + { |
| 174 | + "cell_type": "code", |
| 175 | + "execution_count": null, |
| 176 | + "metadata": {}, |
| 177 | + "outputs": [], |
| 178 | + "source": [ |
| 179 | + "print(f\"[2D] spotpython Kriging Random:\\n min y = {spot_exact_random.min_y:.4f} at x = {spot_exact_random.min_X}\")\n", |
| 180 | + "print(f\"[2D] spotpython Kriging Morris-Mitchell:\\n min y = {spot_exact_mm.min_y:.4f} at x = {spot_exact_mm.min_X}\")" |
| 181 | + ] |
| 182 | + }, |
| 183 | + { |
| 184 | + "cell_type": "code", |
| 185 | + "execution_count": null, |
| 186 | + "metadata": {}, |
| 187 | + "outputs": [], |
| 188 | + "source": [ |
| 189 | + "spot_exact_random.plot_important_hyperparameter_contour(max_imp=3)\n" |
| 190 | + ] |
| 191 | + }, |
| 192 | + { |
| 193 | + "cell_type": "code", |
| 194 | + "execution_count": null, |
| 195 | + "metadata": {}, |
| 196 | + "outputs": [], |
| 197 | + "source": [ |
| 198 | + "spot_exact_mm.plot_important_hyperparameter_contour(max_imp=3)" |
| 199 | + ] |
| 200 | + } |
| 201 | + ], |
| 202 | + "metadata": { |
| 203 | + "kernelspec": { |
| 204 | + "display_name": "spot313", |
| 205 | + "language": "python", |
| 206 | + "name": "python3" |
| 207 | + }, |
| 208 | + "language_info": { |
| 209 | + "codemirror_mode": { |
| 210 | + "name": "ipython", |
| 211 | + "version": 3 |
| 212 | + }, |
| 213 | + "file_extension": ".py", |
| 214 | + "mimetype": "text/x-python", |
| 215 | + "name": "python", |
| 216 | + "nbconvert_exporter": "python", |
| 217 | + "pygments_lexer": "ipython3", |
| 218 | + "version": "3.13.7" |
| 219 | + } |
| 220 | + }, |
| 221 | + "nbformat": 4, |
| 222 | + "nbformat_minor": 2 |
| 223 | +} |
0 commit comments