|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# SPOT Kriging in 100 Dimensions: Nyström Approximation vs. Exact (Ackley 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 100-dimensional Ackley function.\n", |
| 10 | + "\n", |
| 11 | + "We use a maximum of 500 function evaluations." |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "code", |
| 16 | + "execution_count": 1, |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [ |
| 19 | + { |
| 20 | + "name": "stderr", |
| 21 | + "output_type": "stream", |
| 22 | + "text": [ |
| 23 | + "Seed set to 123\n" |
| 24 | + ] |
| 25 | + } |
| 26 | + ], |
| 27 | + "source": [ |
| 28 | + "import warnings\n", |
| 29 | + "warnings.filterwarnings(\"ignore\")\n", |
| 30 | + "import numpy as np\n", |
| 31 | + "from spotpython.fun.objectivefunctions import Analytical\n", |
| 32 | + "from spotpython.spot import Spot\n", |
| 33 | + "from spotpython.utils.init import fun_control_init, design_control_init, surrogate_control_init" |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "markdown", |
| 38 | + "metadata": {}, |
| 39 | + "source": [ |
| 40 | + "## Define the 100D Ackley Function" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "code", |
| 45 | + "execution_count": 2, |
| 46 | + "metadata": {}, |
| 47 | + "outputs": [], |
| 48 | + "source": [ |
| 49 | + "# do not change, these are given by the organizers\n", |
| 50 | + "dim = 100\n", |
| 51 | + "lower = np.full(dim, -32.768)\n", |
| 52 | + "upper = np.full(dim, 32.768)\n", |
| 53 | + "fun = Analytical().fun_ackley\n", |
| 54 | + "fun_evals = 500\n", |
| 55 | + "max_time = 600" |
| 56 | + ] |
| 57 | + }, |
| 58 | + { |
| 59 | + "cell_type": "markdown", |
| 60 | + "metadata": {}, |
| 61 | + "source": [ |
| 62 | + "## Set up SPOT Controls" |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "markdown", |
| 67 | + "metadata": {}, |
| 68 | + "source": [ |
| 69 | + "## Regression and y as Infill Criterion" |
| 70 | + ] |
| 71 | + }, |
| 72 | + { |
| 73 | + "cell_type": "code", |
| 74 | + "execution_count": 3, |
| 75 | + "metadata": {}, |
| 76 | + "outputs": [], |
| 77 | + "source": [ |
| 78 | + "init_size = 10\n", |
| 79 | + "use_nystrom = False\n", |
| 80 | + "method = \"regression\"\n", |
| 81 | + "infill_criterion = \"y\"\n", |
| 82 | + "tolerance_x = 1e-9\n", |
| 83 | + "seed = 321\n", |
| 84 | + "max_surrogate_points = fun_evals\n", |
| 85 | + "min_Lambda = -4\n", |
| 86 | + "max_Lambda = 3\n", |
| 87 | + "min_theta = -3\n", |
| 88 | + "max_theta = 2\n", |
| 89 | + "isotropic = False\n", |
| 90 | + "kernel = \"matern\"\n", |
| 91 | + "kernel_params = {\"nu\": 1.5}\n", |
| 92 | + "selection_method = \"distance\"\n", |
| 93 | + "min_success_rate = 0.2\n", |
| 94 | + "max_time = 60" |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "code", |
| 99 | + "execution_count": 4, |
| 100 | + "metadata": {}, |
| 101 | + "outputs": [ |
| 102 | + { |
| 103 | + "name": "stderr", |
| 104 | + "output_type": "stream", |
| 105 | + "text": [ |
| 106 | + "Seed set to 321\n" |
| 107 | + ] |
| 108 | + }, |
| 109 | + { |
| 110 | + "name": "stdout", |
| 111 | + "output_type": "stream", |
| 112 | + "text": [ |
| 113 | + "Moving TENSORBOARD_PATH: runs/ to TENSORBOARD_PATH_OLD: runs_OLD/runs_2025_11_04_22_21_30_0\n", |
| 114 | + "Created spot_tensorboard_path: runs/spot_logs/000_p040025_2025-11-04_22-21-30 for SummaryWriter()\n" |
| 115 | + ] |
| 116 | + } |
| 117 | + ], |
| 118 | + "source": [ |
| 119 | + "fun_control = fun_control_init(\n", |
| 120 | + " lower=lower,\n", |
| 121 | + " upper=upper,\n", |
| 122 | + " fun_evals=fun_evals,\n", |
| 123 | + " seed=seed,\n", |
| 124 | + " show_progress=True,\n", |
| 125 | + " infill_criterion=infill_criterion,\n", |
| 126 | + " tolerance_x=tolerance_x,\n", |
| 127 | + " TENSORBOARD_CLEAN=True,\n", |
| 128 | + " tensorboard_log=True,\n", |
| 129 | + " kernel=kernel,\n", |
| 130 | + " kernel_params=kernel_params,\n", |
| 131 | + " selection_method=selection_method,\n", |
| 132 | + " min_success_rate=min_success_rate,\n", |
| 133 | + " max_time=max_time\n", |
| 134 | + ")\n", |
| 135 | + "design_control = design_control_init(init_size=init_size)\n", |
| 136 | + "surrogate_control_exact = surrogate_control_init(use_nystrom=use_nystrom, 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)" |
| 137 | + ] |
| 138 | + }, |
| 139 | + { |
| 140 | + "cell_type": "markdown", |
| 141 | + "metadata": {}, |
| 142 | + "source": [ |
| 143 | + "# Sklearn Gaussian Process Regressor as Surrogate" |
| 144 | + ] |
| 145 | + }, |
| 146 | + { |
| 147 | + "cell_type": "code", |
| 148 | + "execution_count": null, |
| 149 | + "metadata": {}, |
| 150 | + "outputs": [ |
| 151 | + { |
| 152 | + "name": "stdout", |
| 153 | + "output_type": "stream", |
| 154 | + "text": [ |
| 155 | + "spotpython tuning: 20.99221801489919 [----------] 2.20%. Success rate: 0.00% \n" |
| 156 | + ] |
| 157 | + } |
| 158 | + ], |
| 159 | + "source": [ |
| 160 | + "# Needed for the sklearn surrogates:\n", |
| 161 | + "from sklearn.gaussian_process import GaussianProcessRegressor\n", |
| 162 | + "from sklearn.gaussian_process.kernels import RBF\n", |
| 163 | + "from sklearn.tree import DecisionTreeRegressor\n", |
| 164 | + "from sklearn.ensemble import RandomForestRegressor\n", |
| 165 | + "from sklearn import linear_model\n", |
| 166 | + "from sklearn import tree\n", |
| 167 | + "import pandas as pd\n", |
| 168 | + "from xgboost import XGBRegressor\n", |
| 169 | + "\n", |
| 170 | + "S_Tree = DecisionTreeRegressor(random_state=0)\n", |
| 171 | + "S_LM = linear_model.LinearRegression()\n", |
| 172 | + "S_Ridge = linear_model.Ridge()\n", |
| 173 | + "S_RF = RandomForestRegressor(max_depth=100, random_state=0)\n", |
| 174 | + "kernel = 1 * RBF(length_scale=1.0, length_scale_bounds=(1e-2, 1e2))\n", |
| 175 | + "S_GP = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=100)\n", |
| 176 | + "\n", |
| 177 | + "\n", |
| 178 | + "spot_exact_sk = Spot(\n", |
| 179 | + " fun=fun,\n", |
| 180 | + " fun_control=fun_control,\n", |
| 181 | + " design_control=design_control,\n", |
| 182 | + " surrogate_control=surrogate_control_exact,\n", |
| 183 | + " surrogate=S_GP,\n", |
| 184 | + ")\n", |
| 185 | + "spot_exact_sk.run()" |
| 186 | + ] |
| 187 | + }, |
| 188 | + { |
| 189 | + "cell_type": "code", |
| 190 | + "execution_count": null, |
| 191 | + "metadata": {}, |
| 192 | + "outputs": [], |
| 193 | + "source": [ |
| 194 | + "spot_exact_sk.plot_progress(log_y=True, title=\"Exact sklearn Kriging Progress with y\")" |
| 195 | + ] |
| 196 | + }, |
| 197 | + { |
| 198 | + "cell_type": "code", |
| 199 | + "execution_count": null, |
| 200 | + "metadata": {}, |
| 201 | + "outputs": [], |
| 202 | + "source": [ |
| 203 | + "print(f\"[100D] Exact Kriging y: min y = {spot_exact_sk.min_y:.4f} at x = {spot_exact_sk.min_X}\")\n" |
| 204 | + ] |
| 205 | + } |
| 206 | + ], |
| 207 | + "metadata": { |
| 208 | + "kernelspec": { |
| 209 | + "display_name": "spot313", |
| 210 | + "language": "python", |
| 211 | + "name": "python3" |
| 212 | + }, |
| 213 | + "language_info": { |
| 214 | + "codemirror_mode": { |
| 215 | + "name": "ipython", |
| 216 | + "version": 3 |
| 217 | + }, |
| 218 | + "file_extension": ".py", |
| 219 | + "mimetype": "text/x-python", |
| 220 | + "name": "python", |
| 221 | + "nbconvert_exporter": "python", |
| 222 | + "pygments_lexer": "ipython3", |
| 223 | + "version": "3.13.7" |
| 224 | + } |
| 225 | + }, |
| 226 | + "nbformat": 4, |
| 227 | + "nbformat_minor": 2 |
| 228 | +} |
0 commit comments