{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "bbdb5d29", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "from scipy.optimize import minimize" ] }, { "cell_type": "markdown", "id": "102c0edd", "metadata": {}, "source": [ "Example:\n", "\n", "Objective Function:\n", "$$\n", "\\min x^2_1 + x_1 \\times x_2\n", "$$\n", "s.t. Constraints:\n", "\n", "$$\n", "\\begin{aligned}\n", "x^3_1 + x_1 \\times x_2 & = 100 \\\\\n", "x^2_1 + x_2 \\leq 50 \\\\\n", "-100 \\leq x_1, x_2 & \\le 100\n", "\\end{aligned}\n", "$$" ] }, { "cell_type": "code", "execution_count": 2, "id": "f3a4de91", "metadata": {}, "outputs": [], "source": [ "# define the objective function and constraints\n", "def objective_fcn(x):\n", " # parameters:\n", " x1 = x[0]\n", " x2 = x[1]\n", " return x1**2 + x1 * x2\n", "\n", "def equality_constraint(x):\n", " x1 = x[0]\n", " x2 = x[1]\n", " return x1**3 + x1 * x2 - 100 # Remember that we need to put everything from the RHS to the LHS so its = 0\n", "\n", "def inequality_constraint(x):\n", " x1 = x[0]\n", " x2 = x[1]\n", " return x1**2 + x2 - 50\n", "\n", "# Note: could also make all of these lambda functions\n", "\n", "bounds_x1 = (-100, 100)\n", "bounds_x2 = (-100, 100)\n", "# we need to make it into a list\n", "bounds = [bounds_x1, bounds_x2]\n", "\n", "# we need to define the equality types \n", "constraint1 = {'type': 'eq', 'fun': equality_constraint}\n", "constraint2 = {'type': 'ineq', 'fun': inequality_constraint}\n", "\n", "constraints = [constraint1, constraint2]\n", "\n", "# finally, give an initial value for x0. As long as it's reasonable, any pairs of numbers in between the\n", "# bounds should be ok\n", "x0 = [1,1]\n", "\n", "# now we apply the minimization function. \n", "# scipy.optimize.minimize(fun, x0, method, bounds, constraints)\n", "\n", "result = minimize(objective_fcn, x0, method='SLSQP', bounds=bounds, constraints=constraints)" ] }, { "cell_type": "code", "execution_count": 3, "id": "3c945da9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ " fun: 96.00000027660178\n", " jac: array([50.00000095, 2. ])\n", " message: 'Optimization terminated successfully'\n", " nfev: 32\n", " nit: 10\n", " njev: 10\n", " status: 0\n", " success: True\n", " x: array([ 1.99999997, 46.00000084])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result" ] }, { "cell_type": "markdown", "id": "8b637a70", "metadata": {}, "source": [ "The result returns:\n", "* fun = values of objective function\n", "* jac = jacobian\n", "* message = description of the cause of the termination\n", "* nfev, njev = No. of evaluations of the objective functions and of its Jacobian\n", "* nit = NO. of iterations\n", "* status, success = Termination and exit status\n", "* x = Solution of the optimization" ] }, { "cell_type": "code", "execution_count": null, "id": "5b710a15", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "optimus", "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.8.12" } }, "nbformat": 4, "nbformat_minor": 5 }