{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Bayesian Optimization with ENTMOOT\n", "\n", "This notebook describes how to perform Bayesian optimization with a simple test function." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from entmoot import Enting, ProblemConfig, PyomoOptimizer\n", "from entmoot.benchmarks import build_reals_only_problem, eval_reals_only_testfunc\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# define problem\n", "problem_config = ProblemConfig(rnd_seed=73)\n", "build_reals_only_problem(problem_config)\n", "# sample data\n", "N_init = 10\n", "train_x = problem_config.get_rnd_sample_list(num_samples=N_init)\n", "train_y = eval_reals_only_testfunc(train_x)\n", "\n", "# create enting object\n", "# this defines the surrogate model\n", "params = {\"unc_params\": {\"dist_metric\": \"l1\", \"acq_sense\": \"exploration\"}}\n", "enting = Enting(problem_config, params=params)\n", "\n", "# create optimizer object\n", "params_pyomo = {\"solver_name\": \"gurobi\", \"verbose\": False}\n", "opt_pyo = PyomoOptimizer(problem_config, params=params_pyomo)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We now run the BO loop `N_iterations` times. At each iteration, we fit the surrogate to the data, then use the optimizer to propose a new experiment." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "N_iterations = 40\n", "for itr in range(N_iterations):\n", " enting.fit(train_x, train_y)\n", " result = opt_pyo.solve(enting)\n", "\n", " next_x = np.reshape(result.opt_point, (1, -1))\n", " next_y = eval_reals_only_testfunc(next_x)\n", "\n", " # update the training data\n", " train_x = np.concatenate([train_x, next_x], axis=0)\n", " train_y = np.concatenate([train_y, next_y], axis=0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "iterations = np.arange(N_init, N_init + N_iterations + 1)\n", "plt.step(iterations, np.minimum.accumulate(train_y)[N_init-1:])" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.11" } }, "nbformat": 4, "nbformat_minor": 4 }