{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "**Make sure you run this at the begining**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "np.set_printoptions(suppress=True)\n", "from scipy.optimize import linprog" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Feasible Example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\\begin{align}\n", "\\max f=4x_1+x_2 \\\\\n", "\\text{s.t.} -x_1 + 2x_2 &\\leq 4 \\\\\n", "2x_1 + 3x_2 &\\leq 12 \\\\\n", "x_1 - x_2 &\\leq 3 \\\\\n", "\\text{and } x_1, x_2 \\geq 0\n", "\\end{align}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c = [-4, -1, 0, 0, 0]\n", "A = [[-1, 2, 0, 0, 0], [2, 3, 0, 0, 0], [1, -1, 0, 0, 0]]\n", "b = [4, 12, 3]\n", "\n", "x0_bounds = (0, None)\n", "x1_bounds = (0, None)\n", "x2_bounds = (None, None)\n", "x3_bounds = (None, None)\n", "x4_bounds = (None, None)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "res = linprog(c, A_ub=A, b_ub=b, bounds=[x0_bounds, x1_bounds, x2_bounds, x3_bounds, x4_bounds])\n", "\n", "print(res)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Infeasible Example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\\begin{align}\n", "\\min f = x_1 - 2x_2 + 3x_3 \\\\\n", "s.t. -2x_1 + x_2 + 3x_3 & = 2 \\\\\n", "3x_1 + 3x_2 + 4x_3 & = 1 \\\\\n", "\\end{align}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c = [-1, 2, -3]\n", "A = [[-2, 1, 3], [3, 3, 4]]\n", "b = [2, 1]\n", "\n", "x0_bounds = (0, None)\n", "x1_bounds = (0, None)\n", "x2_bounds = (0, None)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "res = linprog(c, A_eq=A, b_eq=b, bounds=[x0_bounds, x1_bounds, x2_bounds,])\n", "\n", "print(res)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Assignment

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\\begin{align}\n", "\\min f = 3x_1 - x_2 \\\\\n", "s.t. 2x_1 + x_2 & \\geq 2 \\\\\n", "x_1 + 3x_2 & \\leq 3 \\\\\n", "x_2 & \\leq 4 \\\\\n", "\\text{and } x_1, x_2 \\geq 0\n", "\\end{align}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c = [-3, 1]\n", "A = [[-2, -1], [1, 3], [0, 1]]\n", "b = [-2, 3, 4]\n", "\n", "x0_bounds = (0, None)\n", "x1_bounds = (0, None)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "res = linprog(c, A_ub=A, b_ub=b, bounds=[x0_bounds, x1_bounds])\n", "\n", "print(res)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }