{ "cells": [ { "cell_type": "markdown", "id": "33231800", "metadata": {}, "source": [ "\n", "*This notebook contains material from [CBE60499](https://ndcbe.github.io/CBE60499);\n", "content is available [on Github](git@github.com:ndcbe/CBE60499.git).*\n" ] }, { "cell_type": "markdown", "id": "0e0e436a", "metadata": {}, "source": [ "\n", "< [4.4 Constraint Qualifications](https://ndcbe.github.io/CBE60499/04.04-Constraint-Qualifications.html) | [Contents](toc.html) | [Tag Index](tag_index.html) | [4.6 NLP Diagnostics with Degeneracy Hunter](https://ndcbe.github.io/CBE60499/04.06-NLP-Diagnostics.html) >

\"Open

\"Download\"" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 1, "link": "[4.5 Second Order Optimality Conditions](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5-Second-Order-Optimality-Conditions)", "section": "4.5 Second Order Optimality Conditions" } }, "source": [ "# 4.5 Second Order Optimality Conditions" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "nbpages": { "level": 1, "link": "[4.5 Second Order Optimality Conditions](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5-Second-Order-Optimality-Conditions)", "section": "4.5 Second Order Optimality Conditions" } }, "outputs": [], "source": [ "# This code cell installs packages on Colab\n", "\n", "import sys\n", "if \"google.colab\" in sys.modules:\n", " !wget \"https://raw.githubusercontent.com/ndcbe/CBE60499/main/notebooks/helper.py\"\n", " import helper\n", " helper.install_idaes()\n", " helper.install_ipopt()\n", " helper.download_figures(['cone1.png','cone2.png'])" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[4.5.1 Helpful Cones](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.1-Helpful-Cones)", "section": "4.5.1 Helpful Cones" } }, "source": [ "## 4.5.1 Helpful Cones" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[4.5.1 Helpful Cones](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.1-Helpful-Cones)", "section": "4.5.1 Helpful Cones" } }, "source": [ "![picture](./figures/cone1.png)" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[4.5.1 Helpful Cones](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.1-Helpful-Cones)", "section": "4.5.1 Helpful Cones" } }, "source": [ "![picture](./figures/cone2.png)" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[4.5.2 Second Order Necessary Conditions](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.2-Second-Order-Necessary-Conditions)", "section": "4.5.2 Second Order Necessary Conditions" } }, "source": [ "## 4.5.2 Second Order Necessary Conditions" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[4.5.2 Second Order Necessary Conditions](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.2-Second-Order-Necessary-Conditions)", "section": "4.5.2 Second Order Necessary Conditions" } }, "source": [ "![picture](./figures/thm-4-17.png)" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[4.5.3 Second Order Sufficient Conditions](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.3-Second-Order-Sufficient-Conditions)", "section": "4.5.3 Second Order Sufficient Conditions" } }, "source": [ "## 4.5.3 Second Order Sufficient Conditions" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[4.5.3 Second Order Sufficient Conditions](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.3-Second-Order-Sufficient-Conditions)", "section": "4.5.3 Second Order Sufficient Conditions" } }, "source": [ "![picture](./figures/thm-4-18.png)" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[4.5.4 Reduced Hessian](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.4-Reduced-Hessian)", "section": "4.5.4 Reduced Hessian" } }, "source": [ "## 4.5.4 Reduced Hessian" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[4.5.4 Reduced Hessian](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.4-Reduced-Hessian)", "section": "4.5.4 Reduced Hessian" } }, "source": [ "![picture](./figures/def-4-19.png)" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[4.5.4 Reduced Hessian](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.4-Reduced-Hessian)", "section": "4.5.4 Reduced Hessian" } }, "source": [ "![picture](./figures/projected-reduced-Hessian1.png)" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[4.5.4 Reduced Hessian](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.4-Reduced-Hessian)", "section": "4.5.4 Reduced Hessian" } }, "source": [ "![picture](./figures/projected-reduced-Hessian2.png)" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[4.5.5 Example](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.5-Example)", "section": "4.5.5 Example" } }, "source": [ "## 4.5.5 Example" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[4.5.5 Example](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.5-Example)", "section": "4.5.5 Example" } }, "source": [ "![picture](./figures/ex-4-20.png)" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[4.5.5 Example](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.5-Example)", "section": "4.5.5 Example" } }, "source": [ "See errata here: http://numero.cheme.cmu.edu/content/errata2.pdf\n", "\n", "![picture](./figures/errata_83.png)" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 3, "link": "[4.5.5.1 Calculation with numpy](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.5.1-Calculation-with-numpy)", "section": "4.5.5.1 Calculation with numpy" } }, "source": [ "### 4.5.5.1 Calculation with numpy" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "nbpages": { "level": 3, "link": "[4.5.5.1 Calculation with numpy](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.5.1-Calculation-with-numpy)", "section": "4.5.5.1 Calculation with numpy" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[2.3093 0.4315]\n", " [0.4315 3.2021]]\n" ] } ], "source": [ "import numpy as np\n", "\n", "# Define the Hessian of the Lagrange function\n", "hessian_L = np.array([[2.3093, 0.4315], [0.4315, 3.2021]])\n", "print(hessian_L)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "nbpages": { "level": 3, "link": "[4.5.5.1 Calculation with numpy](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.5.1-Calculation-with-numpy)", "section": "4.5.5.1 Calculation with numpy" } }, "outputs": [], "source": [ "# Define the gradiant of the constraints\n", "A = np.reshape(np.array([-2.2245, -1.7981]),(2,1))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "nbpages": { "level": 3, "link": "[4.5.5.1 Calculation with numpy](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.5.1-Calculation-with-numpy)", "section": "4.5.5.1 Calculation with numpy" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[-2.2245]\n", " [-1.7981]]\n" ] } ], "source": [ "print(A)" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 3, "link": "[4.5.5.1 Calculation with numpy](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.5.1-Calculation-with-numpy)", "section": "4.5.5.1 Calculation with numpy" } }, "source": [ "Calculate the complete QR factorization.\n", "\n", "$$\n", "A = Q \\times R\n", "$$" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "nbpages": { "level": 3, "link": "[4.5.5.1 Calculation with numpy](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.5.1-Calculation-with-numpy)", "section": "4.5.5.1 Calculation with numpy" } }, "outputs": [], "source": [ "# Calculate the COMPLETE QR factorization\n", "Q, R = np.linalg.qr(A, mode='complete')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "nbpages": { "level": 3, "link": "[4.5.5.1 Calculation with numpy](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.5.1-Calculation-with-numpy)", "section": "4.5.5.1 Calculation with numpy" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[-0.77770385 -0.62863083]\n", " [-0.62863083 0.77770385]]\n" ] } ], "source": [ "print(Q)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "nbpages": { "level": 3, "link": "[4.5.5.1 Calculation with numpy](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.5.1-Calculation-with-numpy)", "section": "4.5.5.1 Calculation with numpy" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[2.86034331]\n", " [0. ]]\n" ] } ], "source": [ "print(R)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "nbpages": { "level": 3, "link": "[4.5.5.1 Calculation with numpy](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.5.1-Calculation-with-numpy)", "section": "4.5.5.1 Calculation with numpy" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[-2.2245]\n", " [-1.7981]]\n" ] } ], "source": [ "print(Q @ R)" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 3, "link": "[4.5.5.1 Calculation with numpy](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.5.1-Calculation-with-numpy)", "section": "4.5.5.1 Calculation with numpy" } }, "source": [ "The second column of $Q$ is the null space of $A$. How do we know it is the null space? It corresponds to the second element of $R$ which is 0.\n", "\n", "Finally, we can calculate the reduced Hessian:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "nbpages": { "level": 3, "link": "[4.5.5.1 Calculation with numpy](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.5.1-Calculation-with-numpy)", "section": "4.5.5.1 Calculation with numpy" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Reduced Hessian w.r.t. null space of constraints:\n" ] }, { "data": { "text/plain": [ "2.4273753426093774" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Calculate the reduced Hessian\n", "print(\"Reduced Hessian w.r.t. null space of constraints:\")\n", "Q[:,1].T @ hessian_L @ Q[:,1]" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 3, "link": "[4.5.5.2 Calculate with Pyomo](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.5.2-Calculate-with-Pyomo)", "section": "4.5.5.2 Calculate with Pyomo" } }, "source": [ "### 4.5.5.2 Calculate with Pyomo" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 4, "link": "[4.5.5.2.1 Define and solve the model](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.5.2.1-Define-and-solve-the-model)", "section": "4.5.5.2.1 Define and solve the model" } }, "source": [ "#### 4.5.5.2.1 Define and solve the model" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "nbpages": { "level": 4, "link": "[4.5.5.2.1 Define and solve the model](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.5.2.1-Define-and-solve-the-model)", "section": "4.5.5.2.1 Define and solve the model" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ipopt 3.13.2: \n", "\n", "******************************************************************************\n", "This program contains Ipopt, a library for large-scale nonlinear optimization.\n", " Ipopt is released as open source code under the Eclipse Public License (EPL).\n", " For more information visit http://projects.coin-or.org/Ipopt\n", "******************************************************************************\n", "\n", "This is Ipopt version 3.13.2, running with linear solver ma27.\n", "\n", "Number of nonzeros in equality constraint Jacobian...: 0\n", "Number of nonzeros in inequality constraint Jacobian.: 2\n", "Number of nonzeros in Lagrangian Hessian.............: 3\n", "\n", "Total number of variables............................: 2\n", " variables with only lower bounds: 0\n", " variables with lower and upper bounds: 0\n", " variables with only upper bounds: 0\n", "Total number of equality constraints.................: 0\n", "Total number of inequality constraints...............: 1\n", " inequality constraints with only lower bounds: 0\n", " inequality constraints with lower and upper bounds: 0\n", " inequality constraints with only upper bounds: 1\n", "\n", "iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls\n", " 0 1.5000000e+00 3.00e+00 2.67e+00 -1.0 0.00e+00 - 0.00e+00 0.00e+00 0\n", " 1 -3.3886674e-01 2.05e+00 5.62e+00 -1.0 1.78e+00 - 1.00e+00 2.75e-01f 1\n", " 2 -1.1897000e-01 0.00e+00 5.69e-01 -1.0 8.27e-01 - 1.00e+00 1.00e+00f 1\n", " 3 -4.7085742e-01 0.00e+00 6.15e-02 -1.7 1.54e-01 - 1.00e+00 1.00e+00h 1\n", " 4 -4.9093265e-01 0.00e+00 5.84e-04 -2.5 2.04e-02 - 1.00e+00 1.00e+00h 1\n", " 5 -4.9428887e-01 0.00e+00 4.51e-06 -3.8 5.92e-03 - 1.00e+00 1.00e+00h 1\n", " 6 -4.9445149e-01 0.00e+00 9.87e-09 -5.7 2.84e-04 - 1.00e+00 1.00e+00h 1\n", " 7 -4.9445337e-01 0.00e+00 1.29e-12 -8.6 3.29e-06 - 1.00e+00 1.00e+00h 1\n", "\n", "Number of Iterations....: 7\n", "\n", " (scaled) (unscaled)\n", "Objective...............: -4.9445336916038940e-01 -4.9445336916038940e-01\n", "Dual infeasibility......: 1.2920775560587572e-12 1.2920775560587572e-12\n", "Constraint violation....: 0.0000000000000000e+00 0.0000000000000000e+00\n", "Complementarity.........: 2.5098574967664475e-09 2.5098574967664475e-09\n", "Overall NLP error.......: 2.5098574967664475e-09 2.5098574967664475e-09\n", "\n", "\n", "Number of objective function evaluations = 8\n", "Number of objective gradient evaluations = 8\n", "Number of equality constraint evaluations = 0\n", "Number of inequality constraint evaluations = 8\n", "Number of equality constraint Jacobian evaluations = 0\n", "Number of inequality constraint Jacobian evaluations = 8\n", "Number of Lagrangian Hessian evaluations = 7\n", "Total CPU secs in IPOPT (w/o function evaluations) = 0.002\n", "Total CPU secs in NLP function evaluations = 0.000\n", "\n", "EXIT: Optimal Solution Found.\n" ] }, { "data": { "text/plain": [ "{'Problem': [{'Lower bound': -inf, 'Upper bound': inf, 'Number of objectives': 1, 'Number of constraints': 1, 'Number of variables': 2, 'Sense': 'unknown'}], 'Solver': [{'Status': 'ok', 'Message': 'Ipopt 3.13.2\\\\x3a Optimal Solution Found', 'Termination condition': 'optimal', 'Id': 0, 'Error rc': 0, 'Time': 0.04768204689025879}], 'Solution': [OrderedDict([('number of solutions', 0), ('number of solutions displayed', 0)])]}" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pyomo.environ as pyo\n", "\n", "m = pyo.ConcreteModel()\n", "\n", "# Define variables\n", "m.x1 = pyo.Var(initialize=1)\n", "m.x2 = pyo.Var(initialize=1)\n", "\n", "# Define constraints\n", "# Note: changing this to an equality constraint\n", "# changes the reduced_hessian\n", "m.con1 = pyo.Constraint(expr=4 - m.x1*m.x2 <= 0 )\n", "\n", "# Define objective\n", "m.obj = pyo.Objective(expr=m.x1**2 - 4*m.x1 + 1.5*m.x2**2 - 7*m.x2 + m.x1*m.x2 + 9 - pyo.log(m.x1) - pyo.log(m.x2))\n", "\n", "# Obtain dual solutions from first solve and send to warm start\n", "m.dual = pyo.Suffix(direction=pyo.Suffix.IMPORT)\n", "\n", "# Specify Ipopt as the solver and solve\n", "opt = pyo.SolverFactory('ipopt')\n", "opt.solve(m, tee=True)" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 4, "link": "[4.5.5.2.2 Extract the dual variable for the constraint](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.5.2.2-Extract-the-dual-variable-for-the-constraint)", "section": "4.5.5.2.2 Extract the dual variable for the constraint" } }, "source": [ "#### 4.5.5.2.2 Extract the dual variable for the constraint" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "nbpages": { "level": 4, "link": "[4.5.5.2.2 Extract the dual variable for the constraint](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.5.2.2-Extract-the-dual-variable-for-the-constraint)", "section": "4.5.5.2.2 Extract the dual variable for the constraint" } }, "outputs": [ { "data": { "text/plain": [ "-0.5684977067847848" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m.dual[m.con1]" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 4, "link": "[4.5.5.2.2 Extract the dual variable for the constraint](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.5.2.2-Extract-the-dual-variable-for-the-constraint)", "section": "4.5.5.2.2 Extract the dual variable for the constraint" } }, "source": [ "This is negative because of a sign convention in Pyomo. Notice it matches the book (expect the sign)." ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 4, "link": "[4.5.5.2.3 Extract the reduced Hessian](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.5.2.3-Extract-the-reduced-Hessian)", "section": "4.5.5.2.3 Extract the reduced Hessian" } }, "source": [ "#### 4.5.5.2.3 Extract the reduced Hessian" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "nbpages": { "level": 4, "link": "[4.5.5.2.3 Extract the reduced Hessian](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.5.2.3-Extract-the-reduced-Hessian)", "section": "4.5.5.2.3 Extract the reduced Hessian" } }, "outputs": [], "source": [ "# Warning: this Pyomo feature is experimental\n", "\n", "from pyomo.contrib.interior_point.inverse_reduced_hessian import inv_reduced_hessian_barrier\n", "# https://github.com/Pyomo/pyomo/blob/main/pyomo/contrib/interior_point/inverse_reduced_hessian.py" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 4, "link": "[4.5.5.2.3 Extract the reduced Hessian](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.5.2.3-Extract-the-reduced-Hessian)", "section": "4.5.5.2.3 Extract the reduced Hessian" } }, "source": [ "Compute the reduced Hessian with respect to $x_1$" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "nbpages": { "level": 4, "link": "[4.5.5.2.3 Extract the reduced Hessian](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.5.2.3-Extract-the-reduced-Hessian)", "section": "4.5.5.2.3 Extract the reduced Hessian" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ipopt 3.13.2: bound_relax_factor=0\n", "honor_original_bounds=no\n", "\n", "\n", "******************************************************************************\n", "This program contains Ipopt, a library for large-scale nonlinear optimization.\n", " Ipopt is released as open source code under the Eclipse Public License (EPL).\n", " For more information visit http://projects.coin-or.org/Ipopt\n", "******************************************************************************\n", "\n", "This is Ipopt version 3.13.2, running with linear solver ma27.\n", "\n", "Number of nonzeros in equality constraint Jacobian...: 0\n", "Number of nonzeros in inequality constraint Jacobian.: 2\n", "Number of nonzeros in Lagrangian Hessian.............: 3\n", "\n", "Total number of variables............................: 2\n", " variables with only lower bounds: 0\n", " variables with lower and upper bounds: 0\n", " variables with only upper bounds: 0\n", "Total number of equality constraints.................: 0\n", "Total number of inequality constraints...............: 1\n", " inequality constraints with only lower bounds: 0\n", " inequality constraints with lower and upper bounds: 0\n", " inequality constraints with only upper bounds: 1\n", "\n", "iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls\n", " 0 -4.9445337e-01 3.56e-08 3.85e-01 -1.0 0.00e+00 - 0.00e+00 0.00e+00 0\n", " 1 -4.7328569e-01 0.00e+00 3.92e-04 -1.7 1.17e-02 - 1.00e+00 1.00e+00h 1\n", " 2 -4.9381301e-01 0.00e+00 1.46e-04 -3.8 3.57e-02 - 1.00e+00 1.00e+00f 1\n", " 3 -4.9445098e-01 0.00e+00 1.74e-07 -5.7 1.06e-03 - 1.00e+00 1.00e+00h 1\n", " 4 -4.9445335e-01 0.00e+00 2.15e-12 -8.6 4.10e-06 - 1.00e+00 1.00e+00h 1\n", "\n", "Number of Iterations....: 4\n", "\n", " (scaled) (unscaled)\n", "Objective...............: -4.9445334641782601e-01 -4.9445334641782601e-01\n", "Dual infeasibility......: 2.1520563109334034e-12 2.1520563109334034e-12\n", "Constraint violation....: 0.0000000000000000e+00 0.0000000000000000e+00\n", "Complementarity.........: 2.5123319576325299e-09 2.5123319576325299e-09\n", "Overall NLP error.......: 2.5123319576325299e-09 2.5123319576325299e-09\n", "\n", "\n", "Number of objective function evaluations = 5\n", "Number of objective gradient evaluations = 5\n", "Number of equality constraint evaluations = 0\n", "Number of inequality constraint evaluations = 5\n", "Number of equality constraint Jacobian evaluations = 0\n", "Number of inequality constraint Jacobian evaluations = 5\n", "Number of Lagrangian Hessian evaluations = 4\n", "Total CPU secs in IPOPT (w/o function evaluations) = 0.001\n", "Total CPU secs in NLP function evaluations = 0.000\n", "\n", "EXIT: Optimal Solution Found.\n", "\n", "Reduced Hessian w.r.t. x1:\n", "[[1.99699045]]\n" ] } ], "source": [ "solve_result, inv_red_hes = inv_reduced_hessian_barrier(\n", " m,\n", " independent_variables= [m.x1], # Warning: these variables cannot be at their bounds\n", " tee=True)\n", "\n", "print(\"\\nReduced Hessian w.r.t. x1:\")\n", "print(np.linalg.inv(inv_red_hes))" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 4, "link": "[4.5.5.2.3 Extract the reduced Hessian](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.5.2.3-Extract-the-reduced-Hessian)", "section": "4.5.5.2.3 Extract the reduced Hessian" } }, "source": [ "Compute the reduced Hessian with respect to $x_2$" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "nbpages": { "level": 4, "link": "[4.5.5.2.3 Extract the reduced Hessian](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.5.2.3-Extract-the-reduced-Hessian)", "section": "4.5.5.2.3 Extract the reduced Hessian" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ipopt 3.13.2: bound_relax_factor=0\n", "honor_original_bounds=no\n", "\n", "\n", "******************************************************************************\n", "This program contains Ipopt, a library for large-scale nonlinear optimization.\n", " Ipopt is released as open source code under the Eclipse Public License (EPL).\n", " For more information visit http://projects.coin-or.org/Ipopt\n", "******************************************************************************\n", "\n", "This is Ipopt version 3.13.2, running with linear solver ma27.\n", "\n", "Number of nonzeros in equality constraint Jacobian...: 0\n", "Number of nonzeros in inequality constraint Jacobian.: 2\n", "Number of nonzeros in Lagrangian Hessian.............: 3\n", "\n", "Total number of variables............................: 2\n", " variables with only lower bounds: 0\n", " variables with lower and upper bounds: 0\n", " variables with only upper bounds: 0\n", "Total number of equality constraints.................: 0\n", "Total number of inequality constraints...............: 1\n", " inequality constraints with only lower bounds: 0\n", " inequality constraints with lower and upper bounds: 0\n", " inequality constraints with only upper bounds: 1\n", "\n", "iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls\n", " 0 -4.9445335e-01 0.00e+00 3.85e-01 -1.0 0.00e+00 - 0.00e+00 0.00e+00 0\n", " 1 -4.7328569e-01 0.00e+00 3.92e-04 -1.7 1.17e-02 - 1.00e+00 1.00e+00h 1\n", " 2 -4.9381301e-01 0.00e+00 1.46e-04 -3.8 3.57e-02 - 1.00e+00 1.00e+00f 1\n", " 3 -4.9445098e-01 0.00e+00 1.74e-07 -5.7 1.06e-03 - 1.00e+00 1.00e+00h 1\n", " 4 -4.9445335e-01 0.00e+00 2.15e-12 -8.6 4.10e-06 - 1.00e+00 1.00e+00h 1\n", "\n", "Number of Iterations....: 4\n", "\n", " (scaled) (unscaled)\n", "Objective...............: -4.9445334641782779e-01 -4.9445334641782779e-01\n", "Dual infeasibility......: 2.1527224447481785e-12 2.1527224447481785e-12\n", "Constraint violation....: 0.0000000000000000e+00 0.0000000000000000e+00\n", "Complementarity.........: 2.5123319576325299e-09 2.5123319576325299e-09\n", "Overall NLP error.......: 2.5123319576325299e-09 2.5123319576325299e-09\n", "\n", "\n", "Number of objective function evaluations = 5\n", "Number of objective gradient evaluations = 5\n", "Number of equality constraint evaluations = 0\n", "Number of inequality constraint evaluations = 5\n", "Number of equality constraint Jacobian evaluations = 0\n", "Number of inequality constraint Jacobian evaluations = 5\n", "Number of Lagrangian Hessian evaluations = 4\n", "Total CPU secs in IPOPT (w/o function evaluations) = 0.001\n", "Total CPU secs in NLP function evaluations = 0.000\n", "\n", "EXIT: Optimal Solution Found.\n", "\n", "Reduced Hessian w.r.t. x2:\n", "[[2.76904325]]\n" ] } ], "source": [ "solve_result, inv_red_hes = inv_reduced_hessian_barrier(\n", " m,\n", " independent_variables= [m.x2], # Warning: these variables cannot be at their bounds\n", " tee=True)\n", "\n", "print(\"\\nReduced Hessian w.r.t. x2:\")\n", "print(np.linalg.inv(inv_red_hes))" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 4, "link": "[4.5.5.2.3 Extract the reduced Hessian](https://ndcbe.github.io/CBE60499/04.05-Second-Order.html#4.5.5.2.3-Extract-the-reduced-Hessian)", "section": "4.5.5.2.3 Extract the reduced Hessian" } }, "source": [ "**Take away message**: The reduced Hessian relative to all three bases (null space, $[1, 0]$, and $[0, 1]$) is positive definite." ] }, { "cell_type": "markdown", "id": "cdf9bc8b", "metadata": {}, "source": [ "\n", "< [4.4 Constraint Qualifications](https://ndcbe.github.io/CBE60499/04.04-Constraint-Qualifications.html) | [Contents](toc.html) | [Tag Index](tag_index.html) | [4.6 NLP Diagnostics with Degeneracy Hunter](https://ndcbe.github.io/CBE60499/04.06-NLP-Diagnostics.html) >

\"Open

\"Download\"" ] } ], "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.7.7" } }, "nbformat": 4, "nbformat_minor": 2 }