{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Fixed parameters" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this notebook, we will show how to use fixed parameters. Therefore,\n", "we employ our Rosenbrock example. We define two problems, where for the\n", "first problem all parameters are optimized, and for the second we fix some\n", "of them to specified values." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# install if not done yet\n", "# %pip install pypesto --quiet" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Define the two problems" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import scipy as sp\n", "\n", "import pypesto\n", "import pypesto.optimize as optimize\n", "import pypesto.visualize as visualize\n", "\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "objective = pypesto.Objective(\n", " fun=sp.optimize.rosen,\n", " grad=sp.optimize.rosen_der,\n", " hess=sp.optimize.rosen_hess,\n", ")\n", "\n", "dim_full = 5\n", "lb = -2 * np.ones((dim_full, 1))\n", "ub = 2 * np.ones((dim_full, 1))\n", "\n", "problem1 = pypesto.Problem(objective=objective, lb=lb, ub=ub)\n", "\n", "x_fixed_indices = [1, 3]\n", "x_fixed_vals = [1, 1]\n", "problem2 = pypesto.Problem(\n", " objective=objective,\n", " lb=lb,\n", " ub=ub,\n", " x_fixed_indices=x_fixed_indices,\n", " x_fixed_vals=x_fixed_vals,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Optimize" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "optimizer = optimize.ScipyOptimizer()\n", "n_starts = 10\n", "\n", "result1 = optimize.minimize(\n", " problem=problem1, optimizer=optimizer, n_starts=n_starts, filename=None\n", ")\n", "result2 = optimize.minimize(\n", " problem=problem2, optimizer=optimizer, n_starts=n_starts, filename=None\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Visualize" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots()\n", "visualize.waterfall(result1, ax)\n", "visualize.waterfall(result2, ax)\n", "visualize.parameters(result1)\n", "visualize.parameters(result2)\n", "visualize.parameters(result2, parameter_indices=\"all\");" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "result1.optimize_result.as_dataframe([\"fval\", \"x\", \"grad\"])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "result2.optimize_result.as_dataframe([\"fval\", \"x\", \"grad\"])" ] } ], "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.12.3" }, "vscode": { "interpreter": { "hash": "44a9cdcbdccbf05a880e90d2e6fe72470baab4d1b82472d890be0596ed887a6b" } } }, "nbformat": 4, "nbformat_minor": 4 }