{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ee4f8a22",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}$$"
      ],
      "text/plain": [
       "ry"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}$$"
      ],
      "text/plain": [
       "ry"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\begin{array}{l}\n",
       "\\begin{array}{lcrcrcl}\n",
       " \\min \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} 2 x_{1} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} 2 x_{2} \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} \\\\\n",
       " \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} 3 x_{1} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} 2 x_{2} \\mspace{-6mu}&\\mspace{-6mu} \\geq \\mspace{-6mu}&\\mspace{-6mu} 20 \\\\\n",
       " \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} 2 x_{1} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} x_{2} \\mspace{-6mu}&\\mspace{-6mu} \\geq \\mspace{-6mu}&\\mspace{-6mu} 10 \\\\\n",
       "\\end{array} \\\\\n",
       "x_{1}, x_{2} \\geq 0\n",
       "\\end{array}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\begin{array}{l}\n",
       "\\begin{array}{lcrcrcl}\n",
       " \\min \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} 2 x_{1} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} 2 x_{2} \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} \\\\\n",
       " \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} 3 x_{1} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} 2 x_{2} \\mspace{-6mu}&\\mspace{-6mu} \\geq \\mspace{-6mu}&\\mspace{-6mu} 20 \\\\\n",
       " \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} 2 x_{1} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} x_{2} \\mspace{-6mu}&\\mspace{-6mu} \\geq \\mspace{-6mu}&\\mspace{-6mu} 10 \\\\\n",
       "\\end{array} \\\\\n",
       "x_{1}, x_{2} \\geq 0\n",
       "\\end{array}$$"
      ],
      "text/plain": [
       "LP problem (use 'view(...)' or '%display typeset' for details)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}-\\infty</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}-\\infty$$"
      ],
      "text/plain": [
       "-Infinity"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\mathrm{None}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\mathrm{None}$$"
      ],
      "text/plain": [
       "None"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}$$"
      ],
      "text/plain": [
       "ry"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}{}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{}$$"
      ],
      "text/plain": [
       "salto"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}$$"
      ],
      "text/plain": [
       "ry"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{red}{t}}_{1} & {\\color{red}{t}}_{2} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{red}{t}}_{1} & -3 & 2 & -1 & 0 & 1 & 0 & 20 \\\\\n",
       "{\\color{red}{t}}_{2} & -2 & 1 & 0 & -1 & 0 & 1 & 10 \\\\\n",
       "\\hline\n",
       " {W_{ind}} & 5 & -3 & 1 & 1 & 0 & 0 & 30\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{red}{t}}_{1} & {\\color{red}{t}}_{2} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{red}{t}}_{1} & -3 & 2 & -1 & 0 & 1 & 0 & 20 \\\\\n",
       "{\\color{red}{t}}_{2} & -2 & 1 & 0 & -1 & 0 & 1 & 10 \\\\\n",
       "\\hline\n",
       " {W_{ind}} & 5 & -3 & 1 & 1 & 0 & 0 & 30\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[  B| x1  x2  s1  s2  t1  t2|rhs]\n",
       "[---+-----------------------+---]\n",
       "[ t1| -3   2  -1   0   1   0| 20]\n",
       "[ t2| -2   1   0  -1   0   1| 10]\n",
       "[---+-----------------------+---]\n",
       "[  W|  5  -3   1   1   0   0| 30]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "tipo ='min'     #   Problema de tipo max o min\n",
    "\n",
    "n=2 #  número de variables x's\n",
    "m=2 #  número de restricciones (distintas de las de signo)\n",
    "\n",
    "c=matrix(QQ,1,n,[2,-2] )  # vector de costos\n",
    "\n",
    "A=matrix(QQ,m,n,[   # coeficientes de las variables x's\n",
    "[-3,2],\n",
    "[-2,1]\n",
    "])\n",
    "\n",
    "b=matrix(QQ,m,1,[20,10])  # vector de la derecha\n",
    "\n",
    "\n",
    "h=matrix(ZZ,1,m,[-1,-1])  # variables de holgura: \n",
    "                            # Pon 1 si va sumando, \n",
    "                            # -1 si va restando y \n",
    "                            # 0 si no hay variable de holgura.\n",
    "                            \n",
    "t=matrix(ZZ,1,m,[1,1])   # variables artificiales: \n",
    "                             # pon 1 si utilizas variable artificial en la fila i-ésima\n",
    "                             #     0 si no utilizas variable artificial en la fila i-ésima\n",
    "\n",
    "colId=(5,6) # posición de la matriz identidad\n",
    "\n",
    "\n",
    "    \n",
    "    \n",
    "####################################################################\n",
    "############  HASTA AQUÍ LOS DATOS  ################################\n",
    "####################################################################\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "#load('https://sage.unex.es/501708/simplex2023Auto.sage')\n",
    "\n",
    "load('https://sage.unex.es/501708/simplex2023CompletamenteManual.sage')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "0bf0816e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{red}{t}}_{1} & {\\color{red}{t}}_{2} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{red}{t}}_{1} & 1 & 0 & -1 & 2 & 1 & -2 & 0 \\\\\n",
       "{x}_{2} & -2 & 1 & 0 & -1 & 0 & 1 & 10 \\\\\n",
       "\\hline\n",
       " {W_{ind}} & -1 & 0 & 1 & -2 & 0 & 3 & 0\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{red}{t}}_{1} & {\\color{red}{t}}_{2} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{red}{t}}_{1} & 1 & 0 & -1 & 2 & 1 & -2 & 0 \\\\\n",
       "{x}_{2} & -2 & 1 & 0 & -1 & 0 & 1 & 10 \\\\\n",
       "\\hline\n",
       " {W_{ind}} & -1 & 0 & 1 & -2 & 0 & 3 & 0\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[  B| x1  x2  s1  s2  t1  t2|rhs]\n",
       "[---+-----------------------+---]\n",
       "[ t1|  1   0  -1   2   1  -2|  0]\n",
       "[ x2| -2   1   0  -1   0   1| 10]\n",
       "[---+-----------------------+---]\n",
       "[  W| -1   0   1  -2   0   3|  0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cambio(2,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "a7f475f7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{red}{t}}_{1} & {\\color{red}{t}}_{2} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{2} & \\frac{1}{2} & 0 & -\\frac{1}{2} & 1 & \\frac{1}{2} & -1 & 0 \\\\\n",
       "{x}_{2} & -\\frac{3}{2} & 1 & -\\frac{1}{2} & 0 & \\frac{1}{2} & 0 & 10 \\\\\n",
       "\\hline\n",
       " {W_{ind}} & 0 & 0 & 0 & 0 & 1 & 1 & 0\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{red}{t}}_{1} & {\\color{red}{t}}_{2} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{2} & \\frac{1}{2} & 0 & -\\frac{1}{2} & 1 & \\frac{1}{2} & -1 & 0 \\\\\n",
       "{x}_{2} & -\\frac{3}{2} & 1 & -\\frac{1}{2} & 0 & \\frac{1}{2} & 0 & 10 \\\\\n",
       "\\hline\n",
       " {W_{ind}} & 0 & 0 & 0 & 0 & 1 & 1 & 0\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[   B|  x1   x2   s1   s2   t1   t2| rhs]\n",
       "[----+-----------------------------+----]\n",
       "[  s2| 1/2    0 -1/2    1  1/2   -1|   0]\n",
       "[  x2|-3/2    1 -1/2    0  1/2    0|  10]\n",
       "[----+-----------------------------+----]\n",
       "[   W|   0    0    0    0    1    1|   0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cambio(1,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "333fc2cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{2} & \\frac{1}{2} & 0 & -\\frac{1}{2} & 1 & 0 \\\\\n",
       "{x}_{2} & -\\frac{3}{2} & 1 & -\\frac{1}{2} & 0 & 10 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & -1 & 0 & -1 & 0 & -20\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{green}{s}}_{2} & \\frac{1}{2} & 0 & -\\frac{1}{2} & 1 & 0 \\\\\n",
       "{x}_{2} & -\\frac{3}{2} & 1 & -\\frac{1}{2} & 0 & 10 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & -1 & 0 & -1 & 0 & -20\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[   B|  x1   x2   s1   s2| rhs]\n",
       "[----+-------------------+----]\n",
       "[  s2| 1/2    0 -1/2    1|   0]\n",
       "[  x2|-3/2    1 -1/2    0|  10]\n",
       "[----+-------------------+----]\n",
       "[   Z|  -1    0   -1    0| -20]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "faseII(0, (0,0))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a219f233",
   "metadata": {},
   "source": [
    "# El problema es no acotado"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "fa8577a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}$$"
      ],
      "text/plain": [
       "ry"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}$$"
      ],
      "text/plain": [
       "ry"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\begin{array}{l}\n",
       "\\begin{array}{lcrcrcrcl}\n",
       " \\min \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} x_{1} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} 2 x_{2} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} x_{3} \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} \\\\\n",
       " \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} 2 x_{2} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} x_{3} \\mspace{-6mu}&\\mspace{-6mu} = \\mspace{-6mu}&\\mspace{-6mu} 9 \\\\\n",
       " \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} x_{1} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} x_{2} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} x_{3} \\mspace{-6mu}&\\mspace{-6mu} = \\mspace{-6mu}&\\mspace{-6mu} 4 \\\\\n",
       "\\end{array} \\\\\n",
       "x_{1}, x_{2}, x_{3} \\geq 0\n",
       "\\end{array}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\begin{array}{l}\n",
       "\\begin{array}{lcrcrcrcl}\n",
       " \\min \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} x_{1} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} 2 x_{2} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} x_{3} \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} \\\\\n",
       " \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} 2 x_{2} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} x_{3} \\mspace{-6mu}&\\mspace{-6mu} = \\mspace{-6mu}&\\mspace{-6mu} 9 \\\\\n",
       " \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} x_{1} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} x_{2} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} x_{3} \\mspace{-6mu}&\\mspace{-6mu} = \\mspace{-6mu}&\\mspace{-6mu} 4 \\\\\n",
       "\\end{array} \\\\\n",
       "x_{1}, x_{2}, x_{3} \\geq 0\n",
       "\\end{array}$$"
      ],
      "text/plain": [
       "LP problem (use 'view(...)' or '%display typeset' for details)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\frac{19}{2}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\frac{19}{2}$$"
      ],
      "text/plain": [
       "19/2"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\frac{1}{2},\\,\\frac{9}{2},\\,0\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\frac{1}{2},\\,\\frac{9}{2},\\,0\\right)$$"
      ],
      "text/plain": [
       "(1/2, 9/2, 0)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}$$"
      ],
      "text/plain": [
       "ry"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}{}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{}$$"
      ],
      "text/plain": [
       "salto"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}$$"
      ],
      "text/plain": [
       "ry"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {\\color{red}{t}}_{1} & {\\color{red}{t}}_{2} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{red}{t}}_{1} & 0 & 2 & 1 & 1 & 0 & 9 \\\\\n",
       "{\\color{red}{t}}_{2} & -1 & 1 & 1 & 0 & 1 & 4 \\\\\n",
       "\\hline\n",
       " {W_{ind}} & 1 & -3 & -2 & 0 & 0 & 13\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {\\color{red}{t}}_{1} & {\\color{red}{t}}_{2} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{red}{t}}_{1} & 0 & 2 & 1 & 1 & 0 & 9 \\\\\n",
       "{\\color{red}{t}}_{2} & -1 & 1 & 1 & 0 & 1 & 4 \\\\\n",
       "\\hline\n",
       " {W_{ind}} & 1 & -3 & -2 & 0 & 0 & 13\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[  B| x1  x2  x3  t1  t2|rhs]\n",
       "[---+-------------------+---]\n",
       "[ t1|  0   2   1   1   0|  9]\n",
       "[ t2| -1   1   1   0   1|  4]\n",
       "[---+-------------------+---]\n",
       "[  W|  1  -3  -2   0   0| 13]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "tipo ='min'     #   Problema de tipo max o min\n",
    "\n",
    "n=3 #  número de variables x's\n",
    "m=2 #  número de restricciones (distintas de las de signo)\n",
    "\n",
    "c=matrix(QQ,1,n,[1,2,1] )  # vector de costos\n",
    "\n",
    "A=matrix(QQ,m,n,[   # coeficientes de las variables x's\n",
    "[0,2,1],\n",
    "[-1,1,1]\n",
    "])\n",
    "\n",
    "b=matrix(QQ,m,1,[9,4])  # vector de la derecha\n",
    "\n",
    "\n",
    "h=matrix(ZZ,1,m,[0,0])  # variables de holgura: \n",
    "                            # Pon 1 si va sumando, \n",
    "                            # -1 si va restando y \n",
    "                            # 0 si no hay variable de holgura.\n",
    "                            \n",
    "t=matrix(ZZ,1,m,[1,1])   # variables artificiales: \n",
    "                             # pon 1 si utilizas variable artificial en la fila i-ésima\n",
    "                             #     0 si no utilizas variable artificial en la fila i-ésima\n",
    "\n",
    "colId=(4,5) # posición de la matriz identidad\n",
    "\n",
    "\n",
    "    \n",
    "    \n",
    "####################################################################\n",
    "############  HASTA AQUÍ LOS DATOS  ################################\n",
    "####################################################################\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "#load('https://sage.unex.es/501708/simplex2023Auto.sage')\n",
    "\n",
    "load('https://sage.unex.es/501708/simplex2023CompletamenteManual.sage')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "cdbc63c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {\\color{red}{t}}_{1} & {\\color{red}{t}}_{2} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{red}{t}}_{1} & 2 & 0 & -1 & 1 & -2 & 1 \\\\\n",
       "{x}_{2} & -1 & 1 & 1 & 0 & 1 & 4 \\\\\n",
       "\\hline\n",
       " {W_{ind}} & -2 & 0 & 1 & 0 & 3 & 1\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {\\color{red}{t}}_{1} & {\\color{red}{t}}_{2} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{red}{t}}_{1} & 2 & 0 & -1 & 1 & -2 & 1 \\\\\n",
       "{x}_{2} & -1 & 1 & 1 & 0 & 1 & 4 \\\\\n",
       "\\hline\n",
       " {W_{ind}} & -2 & 0 & 1 & 0 & 3 & 1\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[  B| x1  x2  x3  t1  t2|rhs]\n",
       "[---+-------------------+---]\n",
       "[ t1|  2   0  -1   1  -2|  1]\n",
       "[ x2| -1   1   1   0   1|  4]\n",
       "[---+-------------------+---]\n",
       "[  W| -2   0   1   0   3|  1]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cambio(2,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "fed38d95",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {\\color{red}{t}}_{1} & {\\color{red}{t}}_{2} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{1} & 1 & 0 & -\\frac{1}{2} & \\frac{1}{2} & -1 & \\frac{1}{2} \\\\\n",
       "{x}_{2} & 0 & 1 & \\frac{1}{2} & \\frac{1}{2} & 0 & \\frac{9}{2} \\\\\n",
       "\\hline\n",
       " {W_{ind}} & 0 & 0 & 0 & 1 & 1 & 0\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {\\color{red}{t}}_{1} & {\\color{red}{t}}_{2} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{1} & 1 & 0 & -\\frac{1}{2} & \\frac{1}{2} & -1 & \\frac{1}{2} \\\\\n",
       "{x}_{2} & 0 & 1 & \\frac{1}{2} & \\frac{1}{2} & 0 & \\frac{9}{2} \\\\\n",
       "\\hline\n",
       " {W_{ind}} & 0 & 0 & 0 & 1 & 1 & 0\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[   B|  x1   x2   x3   t1   t2| rhs]\n",
       "[----+------------------------+----]\n",
       "[  x1|   1    0 -1/2  1/2   -1| 1/2]\n",
       "[  x2|   0    1  1/2  1/2    0| 9/2]\n",
       "[----+------------------------+----]\n",
       "[   W|   0    0    0    1    1|   0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cambio(1,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "f17c922e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html><script type=\"math/tex; mode=display\">\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{1} & 1 & 0 & -\\frac{1}{2} & \\frac{1}{2} \\\\\n",
       "{x}_{2} & 0 & 1 & \\frac{1}{2} & \\frac{9}{2} \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & 0 & \\frac{1}{2} & \\frac{19}{2}\n",
       "\\end{array}\\right)</script></html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{1} & 1 & 0 & -\\frac{1}{2} & \\frac{1}{2} \\\\\n",
       "{x}_{2} & 0 & 1 & \\frac{1}{2} & \\frac{9}{2} \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & 0 & \\frac{1}{2} & \\frac{19}{2}\n",
       "\\end{array}\\right)$$"
      ],
      "text/plain": [
       "[   B|  x1   x2   x3| rhs]\n",
       "[----+--------------+----]\n",
       "[  x1|   1    0 -1/2| 1/2]\n",
       "[  x2|   0    1  1/2| 9/2]\n",
       "[----+--------------+----]\n",
       "[   Z|   0    0  1/2|19/2]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "faseII(0,(0,0))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fcdd769d",
   "metadata": {},
   "source": [
    "# Cuadro óptimo"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "SageMath 9.5",
   "language": "sage",
   "name": "sagemath"
  },
  "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.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
