{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Nelder-Mead Generator adapted from SciPy\n", "\n", "Most of the algorithms in scipy.optimize are self-contained functions that operate on the user-provided `func`. Xopt has adapted the Nelder-Mead directly from scipy.optimize to be in a generator form. This allows for the manual stepping through the algorithm.\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2023-10-30T17:44:49.812021200Z", "start_time": "2023-10-30T17:44:49.674945300Z" }, "execution": { "iopub.execute_input": "2025-01-07T16:05:23.459014Z", "iopub.status.busy": "2025-01-07T16:05:23.458828Z", "iopub.status.idle": "2025-01-07T16:05:25.350036Z", "shell.execute_reply": "2025-01-07T16:05:25.349511Z" } }, "outputs": [], "source": [ "from xopt.generators.scipy.neldermead import NelderMeadGenerator\n", "from xopt import Evaluator, VOCS\n", "from xopt.resources.test_functions.rosenbrock import rosenbrock\n", "\n", "import pandas as pd\n", "\n", "from xopt import Xopt\n", "import numpy as np\n", "\n", "from scipy.optimize import fmin\n", "\n", "# from xopt import output_notebook\n", "# output_notebook()\n", "\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Nelder-Mead optimization of the Rosenbrock function with Xopt" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2023-10-30T17:44:49.892121Z", "start_time": "2023-10-30T17:44:49.680946600Z" }, "execution": { "iopub.execute_input": "2025-01-07T16:05:25.352217Z", "iopub.status.busy": "2025-01-07T16:05:25.351768Z", "iopub.status.idle": "2025-01-07T16:05:25.615427Z", "shell.execute_reply": "2025-01-07T16:05:25.614908Z" } }, "outputs": [], "source": [ "YAML = \"\"\"\n", "generator:\n", " name: neldermead\n", " initial_point: {x0: -1, x1: -1}\n", " adaptive: true\n", " xatol: 0.0001\n", " fatol: 0.0001 \n", "evaluator:\n", " function: xopt.resources.test_functions.rosenbrock.evaluate_rosenbrock\n", "vocs:\n", " variables:\n", " x0: [-5, 5]\n", " x1: [-5, 5]\n", " objectives: {y: MINIMIZE}\n", "\"\"\"\n", "X = Xopt.from_yaml(YAML)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2023-10-30T17:44:49.974002700Z", "start_time": "2023-10-30T17:44:49.689493800Z" }, "execution": { "iopub.execute_input": "2025-01-07T16:05:25.617231Z", "iopub.status.busy": "2025-01-07T16:05:25.616991Z", "iopub.status.idle": "2025-01-07T16:05:25.619436Z", "shell.execute_reply": "2025-01-07T16:05:25.619061Z" } }, "outputs": [], "source": [ "XMIN = [1, 1] # True minimum" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2023-10-30T17:44:50.195902300Z", "start_time": "2023-10-30T17:44:49.692491400Z" }, "execution": { "iopub.execute_input": "2025-01-07T16:05:25.620820Z", "iopub.status.busy": "2025-01-07T16:05:25.620666Z", "iopub.status.idle": "2025-01-07T16:05:25.887756Z", "shell.execute_reply": "2025-01-07T16:05:25.887273Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " | x0 | \n", "x1 | \n", "y | \n", "xopt_runtime | \n", "xopt_error | \n", "
---|---|---|---|---|---|
0 | \n", "-1.000000 | \n", "-1.000000 | \n", "4.040000e+02 | \n", "0.000008 | \n", "False | \n", "
1 | \n", "-1.050000 | \n", "-1.000000 | \n", "4.462531e+02 | \n", "0.000007 | \n", "False | \n", "
2 | \n", "-1.000000 | \n", "-1.050000 | \n", "4.242500e+02 | \n", "0.000005 | \n", "False | \n", "
3 | \n", "-0.950000 | \n", "-1.050000 | \n", "3.850281e+02 | \n", "0.000005 | \n", "False | \n", "
4 | \n", "-0.900000 | \n", "-1.075000 | \n", "3.589325e+02 | \n", "0.000006 | \n", "False | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
120 | \n", "0.999935 | \n", "0.999867 | \n", "5.114951e-09 | \n", "0.000005 | \n", "False | \n", "
121 | \n", "0.999877 | \n", "0.999764 | \n", "2.587916e-08 | \n", "0.000005 | \n", "False | \n", "
122 | \n", "0.999999 | \n", "0.999995 | \n", "5.309344e-10 | \n", "0.000005 | \n", "False | \n", "
123 | \n", "1.000045 | \n", "1.000097 | \n", "7.751675e-09 | \n", "0.000005 | \n", "False | \n", "
124 | \n", "0.999963 | \n", "0.999925 | \n", "1.412126e-09 | \n", "0.000005 | \n", "False | \n", "
125 rows × 5 columns
\n", "