{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c4e1a7d2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-30T19:26:45.462529Z",
     "iopub.status.busy": "2026-11-30T19:26:45.462232Z",
     "iopub.status.idle": "2026-11-30T19:26:45.469568Z",
     "shell.execute_reply": "2026-11-30T19:26:45.468542Z"
    },
    "papermill": {
     "duration": 0.011952,
     "end_time": "2026-11-30T19:26:45.470595+00:00",
     "exception": false,
     "start_time": "2026-11-30T19:26:45.458643+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "active-ipynb",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "try:\n",
    "    from openmdao.utils.notebook_utils import notebook_mode  # noqa: F401\n",
    "except ImportError:\n",
    "    !python -m pip install openmdao[notebooks]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2ceaf1d",
   "metadata": {
    "papermill": {
     "duration": 0.002109,
     "end_time": "2026-11-30T19:26:45.475072+00:00",
     "exception": false,
     "start_time": "2026-11-30T19:26:45.472963+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "# HessianAutoscaler\n",
    "\n",
    "`HessianAutoscaler` scales the design variables using approximate eigenvalues and eigenvectors of the objective Hessian. It estimates this information once, at the initial design point, before optimization begins. For a quadratic objective, the Hessian is constant; for a nonlinear objective, its curvature can change during optimization.\n",
    "\n",
    "The examples demonstrate how to apply the autoscaler and briefly show its potential. The first example compares `SLSQP` with and without Hessian-based scaling on a quadratic problem with linear equality constraints. A second example shows how design-variable bounds are handled. \n",
    "\n",
    "`HessianAutoscaler` can be used with gradient-based optimizers. However, as shown in the second example, design-variable bounds are converted into linear inequality constraints, so the optimizer must support these constraints. For example, a bound-constrained problem solved using `L-BFGS-B` would require switching to an optimizer that supports general constraints, such as `SLSQP` or `trust-constr` in `ScipyOptimizeDriver`, or `IPOPT` or `SNOPT` in `pyOptSparseDriver`, when using `HessianAutoscaler`.\n",
    "\n",
    "Because the Hessian-based transformation mixes the design variables, every constraint depends on every optimizer variable. Drivers therefore treat the total jacobian as dense when using `HessianAutoscaler`, rather than using the sparsity of the model."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80348d3c",
   "metadata": {
    "papermill": {
     "duration": 0.002064,
     "end_time": "2026-11-30T19:26:45.479221+00:00",
     "exception": false,
     "start_time": "2026-11-30T19:26:45.477157+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## Quadratic problem with equality constraints\n",
    "\n",
    "We solve\n",
    "\n",
    "$$\n",
    "\\min_x \\quad f(x) = \\tfrac{1}{2} x^T H x,\n",
    "\\qquad \\text{subject to } Ax = b.\n",
    "$$\n",
    "\n",
    "The Hessian is constructed as $H = Q\\Lambda Q^T$, where $Q$ is orthogonal and $\\Lambda_{ii} = i^{-d}$ for $i=1,\\ldots,n$. Here, $n=256$, $d=1$, and $b=0$. The unique solution is therefore $x=0$, with $f(x)=0$.\n",
    "\n",
    "The component below evaluates the objective, the constraint residuals $c(x)=Ax-b$, and their derivatives with respect to the design vector. The matrices and right-hand side are fixed problem data. We use a fixed random seed and the same initial point for both optimization runs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bd89def4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-30T19:26:45.484393Z",
     "iopub.status.busy": "2026-11-30T19:26:45.484160Z",
     "iopub.status.idle": "2026-11-30T19:26:47.961546Z",
     "shell.execute_reply": "2026-11-30T19:26:47.960644Z"
    },
    "papermill": {
     "duration": 2.480968,
     "end_time": "2026-11-30T19:26:47.962252+00:00",
     "exception": false,
     "start_time": "2026-11-30T19:26:45.481284+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import openmdao.api as om\n",
    "import numpy as np\n",
    "\n",
    "np.random.seed(42)\n",
    "\n",
    "n_vars = 256        # number of design variables\n",
    "n_cons = n_vars//2  # number of constraints\n",
    "decay_rate = 1.0    # eigenvalue decay rate\n",
    "x0 = np.random.randn(n_vars,1)\n",
    "\n",
    "# Objective function parameters\n",
    "Q, _ = np.linalg.qr(np.random.normal(loc=0, scale=1, size=(n_vars, n_vars)))\n",
    "Lambda = 1.0 / np.arange(1, n_vars + 1, dtype=float)**decay_rate\n",
    "\n",
    "# Constraints parameters\n",
    "A = np.random.normal(loc=0, scale=1, size=(n_cons, n_vars))\n",
    "b = np.zeros(n_cons)\n",
    "\n",
    "# the objective Hessian matrix \n",
    "H = Q @ np.diag(Lambda) @ np.transpose(Q)\n",
    "\n",
    "class QuadraticProblem(om.ExplicitComponent):\n",
    "    \"\"\"Evaluate a quadratic objective and linear constraint residuals.\"\"\"\n",
    "    \n",
    "    def initialize(self):\n",
    "        self.options.declare('n_vars', types=int)\n",
    "        self.options.declare('n_cons', types=int)\n",
    "    def setup(self):\n",
    "        n_vars = self.options['n_vars']                    \n",
    "        n_cons = self.options['n_cons']\n",
    "        self.add_input('x', shape=(n_vars,))\n",
    "        self.add_input('H', shape=(n_vars,n_vars), desc=\"objective Hessian matrix\")\n",
    "        self.add_input('A', shape=(n_cons,n_vars), desc=\"constraint Jacobian matrix\")\n",
    "        self.add_input('b', shape=(n_cons,), desc=\"constraint vector\")\n",
    "        self.add_output('c', shape=(n_cons,), desc=\"constraint residuals\")\n",
    "        self.add_output('fx')\n",
    "        \n",
    "    def setup_partials(self):\n",
    "        self.declare_partials('fx', 'x')\n",
    "        self.declare_partials('c', 'x')\n",
    "    \n",
    "    def compute(self, inputs, outputs): \n",
    "        x = inputs['x']\n",
    "        H = inputs['H']\n",
    "        A = inputs['A']\n",
    "        b = inputs['b']\n",
    "        outputs['fx'] = 0.5 * x.T @ H @ x\n",
    "        outputs['c'] = A @ x - b\n",
    "        \n",
    "    def compute_partials(self, inputs, partials):\n",
    "        partials['fx', 'x'] = (inputs['H'] @ inputs['x']).reshape(1, -1)  \n",
    "        partials['c', 'x'] = inputs['A']\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1c7fa82",
   "metadata": {
    "papermill": {
     "duration": 0.003845,
     "end_time": "2026-11-30T19:26:47.969504+00:00",
     "exception": false,
     "start_time": "2026-11-30T19:26:47.965659+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## The baseline run script with no scaling\n",
    "The following can be used to run the optimization without any scaling, using the SLSQP optimizer:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a75571fc",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-30T19:26:47.977523Z",
     "iopub.status.busy": "2026-11-30T19:26:47.977039Z",
     "iopub.status.idle": "2026-11-30T19:27:05.389184Z",
     "shell.execute_reply": "2026-11-30T19:27:05.388708Z"
    },
    "papermill": {
     "duration": 17.415816,
     "end_time": "2026-11-30T19:27:05.390261+00:00",
     "exception": false,
     "start_time": "2026-11-30T19:26:47.974445+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully    (Exit mode 0)\n",
      "            Current function value: 1.2432760707706028e-07\n",
      "            Iterations: 82\n",
      "            Function evaluations: 83\n",
      "            Gradient evaluations: 82\n",
      "Optimization Complete\n",
      "-----------------------------------\n",
      "f(x) =  [1.24327607e-07]\n",
      "||x|| =  0.005691933971449958\n",
      "||Ax - b|| =  2.0437264873802465e-17\n"
     ]
    }
   ],
   "source": [
    "prob = om.Problem(reports=False)    \n",
    "prob.model.add_subsystem('quad_obj', QuadraticProblem(n_vars=n_vars, n_cons=n_cons), promotes=['*'])\n",
    "\n",
    "prob.driver = om.ScipyOptimizeDriver(optimizer='SLSQP', tol=1e-8, disp=True)\n",
    "prob.driver.options['maxiter'] = 300\n",
    "prob.model.add_design_var('x')\n",
    "prob.model.add_objective('fx')\n",
    "prob.model.add_constraint('c', equals=0.0)\n",
    "\n",
    "prob.setup()\n",
    "\n",
    "prob.set_val('x', x0.copy())\n",
    "prob.set_val('A', A)\n",
    "prob.set_val('b', b)\n",
    "prob.set_val('H', H)\n",
    "\n",
    "prob.run_driver()\n",
    "\n",
    "print('f(x) = ', prob.get_val('fx'))\n",
    "print('||x|| = ', np.linalg.norm(prob.get_val('x')))\n",
    "print('||Ax - b|| = ', np.linalg.norm(prob.get_val('c')))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b61b07c2",
   "metadata": {
    "papermill": {
     "duration": 0.005677,
     "end_time": "2026-11-30T19:27:05.400023+00:00",
     "exception": false,
     "start_time": "2026-11-30T19:27:05.394346+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "```\n",
    "Optimization terminated successfully    (Exit mode 0)\n",
    "            Current function value: 1.2489326838270796e-07\n",
    "            Iterations: 84\n",
    "            Function evaluations: 85\n",
    "            Gradient evaluations: 84\n",
    "Optimization Complete\n",
    "-----------------------------------\n",
    "f(x) =  [1.24893268e-07]\n",
    "||x|| =  0.005544603082868548\n",
    "||Ax - b|| =  2.4088952727249008e-17\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a934087",
   "metadata": {
    "papermill": {
     "duration": 0.005367,
     "end_time": "2026-11-30T19:27:05.410547+00:00",
     "exception": false,
     "start_time": "2026-11-30T19:27:05.405180+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## Applying the HessianAutoscaler\n",
    "Import the `HessianAutoscaler` class from `openmdao.api`, then assign an instance to `prob.driver.autoscaler` before calling `setup()`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6fb3d6d4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-30T19:27:05.422299Z",
     "iopub.status.busy": "2026-11-30T19:27:05.421933Z",
     "iopub.status.idle": "2026-11-30T19:27:05.425280Z",
     "shell.execute_reply": "2026-11-30T19:27:05.424867Z"
    },
    "papermill": {
     "duration": 0.011155,
     "end_time": "2026-11-30T19:27:05.426385+00:00",
     "exception": false,
     "start_time": "2026-11-30T19:27:05.415230+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "from openmdao.api import HessianAutoscaler"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26b7f8f1",
   "metadata": {
    "papermill": {
     "duration": 0.001771,
     "end_time": "2026-11-30T19:27:05.430028+00:00",
     "exception": false,
     "start_time": "2026-11-30T19:27:05.428257+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "When instantiating the `HessianAutoscaler`, all arguments are optional. The constructor accepts the following arguments:\n",
    "\n",
    "| Argument | Meaning | Default |\n",
    "|---|---|---|\n",
    "| `num_modes` | Number of retained/used Hessian eigenpairs. | `min(16, max(1, n_vars - 2))`, computed during setup |\n",
    "| `num_krylov` | Number of Krylov iterations for the local Arnoldi implementation; number of Arnoldi vectors (`ncv`) passed to SciPy when that backend is selected. Must be at least `num_modes`. | `2 * num_modes` |\n",
    "| `grad_fd_step` | Finite difference step for approximating Hessian-vector products from objective gradients. | `1e-6` |\n",
    "| `use_scipy_eigs` | Select SciPy's `eigs` instead of the local Arnoldi implementation. | `False` |\n",
    "| `start_vec` | Starting vector for eigenpair estimation, with the same size as the number of design variables. | Pseudorandom vector from a fixed seed when `None` |\n",
    "\n",
    "The number of retained modes cannot exceed the number of design variables. SciPy's `eigs` backend additionally requires `num_modes < n_vars - 1` for the matrix-free operator used here. This example uses 16 of 256 modes, the local implementation and 32 Krylov iterations.\n",
    "\n",
    "Objectives and constraints are scaled using their `ref`, `ref0`, `scaler`, and `adder`, as they are without the `HessianAutoscaler`, so a negative `scaler` or `ref` on the objective still maximizes it. Design variable `ref`, `ref0`, `scaler`, and `adder` are replaced by the Hessian-based scaling."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f7968fd1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-30T19:27:05.434700Z",
     "iopub.status.busy": "2026-11-30T19:27:05.434518Z",
     "iopub.status.idle": "2026-11-30T19:27:10.517156Z",
     "shell.execute_reply": "2026-11-30T19:27:10.516054Z"
    },
    "papermill": {
     "duration": 5.08597,
     "end_time": "2026-11-30T19:27:10.517808+00:00",
     "exception": false,
     "start_time": "2026-11-30T19:27:05.431838+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully    (Exit mode 0)\n",
      "            Current function value: 9.800308532097707e-09\n",
      "            Iterations: 25\n",
      "            Function evaluations: 25\n",
      "            Gradient evaluations: 25\n",
      "Optimization Complete\n",
      "-----------------------------------\n",
      "f(x) =  [9.80030853e-09]\n",
      "||x|| =  0.00153324505464539\n",
      "||Ax - b|| =  1.1535716550773889e-17\n"
     ]
    }
   ],
   "source": [
    "prob = om.Problem(reports=False)\n",
    "prob.model.add_subsystem('quad_obj', QuadraticProblem(n_vars=n_vars, n_cons=n_cons), promotes=['*'])\n",
    "\n",
    "prob.driver = om.ScipyOptimizeDriver(optimizer='SLSQP', tol=1e-8, disp=True)\n",
    "\n",
    "prob.model.add_design_var('x')\n",
    "prob.model.add_objective('fx')\n",
    "prob.model.add_constraint('c', equals=0.0)\n",
    "\n",
    "prob.driver.autoscaler = HessianAutoscaler(\n",
    "    num_modes=16,\n",
    "    num_krylov=32,\n",
    "    grad_fd_step=1e-6, \n",
    "    use_scipy_eigs=False,\n",
    "    start_vec=np.random.rand(n_vars)\n",
    ")\n",
    "\n",
    "prob.setup()\n",
    "\n",
    "prob.set_val('x', x0.copy())\n",
    "prob.set_val('A', A)\n",
    "prob.set_val('b', b)\n",
    "prob.set_val('H', H)\n",
    "\n",
    "prob.run_driver()\n",
    "\n",
    "print('f(x) = ', prob.get_val('fx'))\n",
    "print('||x|| = ', np.linalg.norm(prob.get_val('x')))\n",
    "print('||Ax - b|| = ', np.linalg.norm(prob.get_val('c')))\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66e102a2",
   "metadata": {
    "papermill": {
     "duration": 0.001876,
     "end_time": "2026-11-30T19:27:10.521912+00:00",
     "exception": false,
     "start_time": "2026-11-30T19:27:10.520036+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "```\n",
    "Optimization terminated successfully    (Exit mode 0)\n",
    "            Current function value: 9.80030859069809e-09\n",
    "            Iterations: 25\n",
    "            Function evaluations: 25\n",
    "            Gradient evaluations: 25\n",
    "Optimization Complete\n",
    "-----------------------------------\n",
    "f(x) =  [9.80030859e-09]\n",
    "||x|| =  0.0015332450594031447\n",
    "||Ax - b|| =  1.2387914277951112e-17\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c92822e6",
   "metadata": {
    "papermill": {
     "duration": 0.001808,
     "end_time": "2026-11-30T19:27:10.525593+00:00",
     "exception": false,
     "start_time": "2026-11-30T19:27:10.523785+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "Compare the optimizer summaries above to assess the impact of scaling with the `HessianAutoscaler` on this example. To see more benefits, we can make the problem more poorly-scaled by increasing `decay_rate` to 2.0 or 3.0; we can also double or quadruple `n_vars` to 512 or 1024. \n",
    "\n",
    "We can also see more benefit by increasing `num_modes`. However, this would also increase the cost of estimating the eigenpairs. Eigenpair estimation requires additional gradient evaluations before optimization, so the optimizer's iteration count alone does not measure the total computational cost. The missing piece is the gradient evaluations performed during the Arnoldi iteration to compute these eigenpairs, which are determined by the chosen `num_krylov`. Note that `num_krylov` >= `num_modes`. The number of gradient evaluations during the Arnoldi iteration can be extracted as:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "eb89d8dc",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-30T19:27:10.533652Z",
     "iopub.status.busy": "2026-11-30T19:27:10.533423Z",
     "iopub.status.idle": "2026-11-30T19:27:10.536632Z",
     "shell.execute_reply": "2026-11-30T19:27:10.535975Z"
    },
    "papermill": {
     "duration": 0.009822,
     "end_time": "2026-11-30T19:27:10.537353+00:00",
     "exception": false,
     "start_time": "2026-11-30T19:27:10.527531+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Arnoldi gradient evaluations =  34\n"
     ]
    }
   ],
   "source": [
    "print('Arnoldi gradient evaluations = ', prob.driver.autoscaler.arnoldi_grad_evals)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "739f30aa",
   "metadata": {
    "papermill": {
     "duration": 0.008491,
     "end_time": "2026-11-30T19:27:10.551523+00:00",
     "exception": false,
     "start_time": "2026-11-30T19:27:10.543032+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "```Arnoldi gradient evaluations =  34```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b14156ae",
   "metadata": {
    "papermill": {
     "duration": 0.006061,
     "end_time": "2026-11-30T19:27:10.562514+00:00",
     "exception": false,
     "start_time": "2026-11-30T19:27:10.556453+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "Both baseline and scaled runs should approach the same optimum. However, at a fixed stopping tolerance, scaling can reduce the iteration count and final numerical error; it does not change the mathematical solution or guarantee improved performance for every problem.\n",
    "\n",
    "For problems with objectives that are nonlinear beyond quadratic, the Hessian is not constant; the curvature can change during optimization. Therefore, the autoscaler constructed based on the initial design might not be well-suited after several iterations. A user of the `HessianAutoscaler` could still benefit by restarting the optimization with the new design variables."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eaf03599",
   "metadata": {
    "papermill": {
     "duration": 0.005074,
     "end_time": "2026-11-30T19:27:10.572526+00:00",
     "exception": false,
     "start_time": "2026-11-30T19:27:10.567452+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## Handling design-variable bounds\n",
    "A matrix transformation can mix design variables, so bounds on model variables generally cannot be represented as independent bounds on optimizer variables. For $x=My$, the bounds $x_l\\leq x\\leq x_u$ become linear inequalities $x_l\\leq My\\leq x_u$.\n",
    "\n",
    "`HessianAutoscaler` performs this conversion automatically. During setup, each bounded design variable gets an equivalent linear constraint named `<design var name>_bounds` in the driver, using the design variable's units and scaling, and the direct bounds are removed from the driver. The model itself is not modified, so `add_design_var` calls do not need to change and switching to a different autoscaler restores the ordinary bounds. The optimizer must support linear inequality constraints; otherwise an error is raised.\n",
    "\n",
    "`transform_desvar_bounds(prob)` is still available if you want the conversion to be part of the model, for example to inspect the constraints with `prob.model.get_constraints()`. Call it after `prob.setup()` and before `run_model()`, `final_setup()`, or `run_driver()`.\n",
    "\n",
    "The example below keeps the bounds on the design variable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a2cfc23f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-30T19:27:10.583648Z",
     "iopub.status.busy": "2026-11-30T19:27:10.583411Z",
     "iopub.status.idle": "2026-11-30T19:27:20.347332Z",
     "shell.execute_reply": "2026-11-30T19:27:20.346292Z"
    },
    "papermill": {
     "duration": 9.77046,
     "end_time": "2026-11-30T19:27:20.347913+00:00",
     "exception": false,
     "start_time": "2026-11-30T19:27:10.577453+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully    (Exit mode 0)\n",
      "            Current function value: 9.925396209195554e-09\n",
      "            Iterations: 25\n",
      "            Function evaluations: 25\n",
      "            Gradient evaluations: 25\n",
      "Optimization Complete\n",
      "-----------------------------------\n",
      "f(x) =  [9.92539621e-09]\n",
      "||x|| =  0.001540381604926829\n",
      "||Ax - b|| =  1.2081925939305361e-17\n"
     ]
    }
   ],
   "source": [
    "prob = om.Problem(reports=False) \n",
    "prob.model.add_subsystem('quad_obj', QuadraticProblem(n_vars=n_vars, n_cons=n_cons), promotes=['*'])\n",
    "\n",
    "prob.driver = om.ScipyOptimizeDriver(optimizer='SLSQP', tol=1e-8, disp=True)\n",
    "prob.model.add_design_var('x', lower=-1.0, upper=2.0)\n",
    "prob.model.add_objective('fx')\n",
    "prob.model.add_constraint('c', equals=0.0)\n",
    "\n",
    "prob.driver.autoscaler = HessianAutoscaler(\n",
    "    num_modes=16,\n",
    "    num_krylov=32,\n",
    "    grad_fd_step=1e-6, \n",
    "    use_scipy_eigs=False,\n",
    "    start_vec=np.random.rand(n_vars),\n",
    ")\n",
    "\n",
    "prob.setup()\n",
    "\n",
    "prob.set_val('x', x0.copy())\n",
    "prob.set_val('A', A)\n",
    "prob.set_val('b', b)\n",
    "prob.set_val('H', H)\n",
    "\n",
    "prob.run_driver()\n",
    "\n",
    "print('f(x) = ', prob.get_val('fx'))\n",
    "print('||x|| = ', np.linalg.norm(prob.get_val('x')))\n",
    "print('||Ax - b|| = ', np.linalg.norm(prob.get_val('c')))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48bf08bd",
   "metadata": {
    "papermill": {
     "duration": 0.001978,
     "end_time": "2026-11-30T19:27:20.352264+00:00",
     "exception": false,
     "start_time": "2026-11-30T19:27:20.350286+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "To learn more about the `HessianAutoscaler`, please see the [published paper](https://www.researchgate.net/profile/Habeeb-Idris/publication/384220671_Impact_of_Winding_Material_on_Optimal_Electric_Motor_Designs/links/66f6fc7bb753fa724d4dbaef/Impact-of-Winding-Material-on-Optimal-Electric-Motor-Designs.pdf)."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.13.18"
  },
  "papermill": {
   "default_parameters": {},
   "duration": 36.199632,
   "end_time": "2026-11-30T19:27:21.071473+00:00",
   "environment_variables": {},
   "exception": null,
   "input_path": "/home/runner/work/OpenMDAO/OpenMDAO/openmdao/docs/openmdao_book/features/building_blocks/drivers/hessian_autoscaler.ipynb",
   "output_path": "/home/runner/work/OpenMDAO/OpenMDAO/openmdao/docs/_executed_book/features/building_blocks/drivers/hessian_autoscaler.ipynb",
   "parameters": {},
   "start_time": "2026-11-30T19:26:44.871841+00:00",
   "version": "2.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}