{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "cfd262a3-b18d-417a-a930-7ba5e8f48c57",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html>\\(\\displaystyle {===================================}\\)</html>"
      ],
      "text/latex": [
       "$\\displaystyle {===================================}$"
      ],
      "text/plain": [
       "ry"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html>\\(\\displaystyle {===================================}\\)</html>"
      ],
      "text/latex": [
       "$\\displaystyle {===================================}$"
      ],
      "text/plain": [
       "ry"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html>\\(\\displaystyle \\begin{array}{l}\n",
       "\\begin{array}{lcrcrcrcrcl}\n",
       " \\max \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} 4 x_{1} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} 2 x_{2} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} 3 x_{3} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} x_{4} \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} \\\\\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} 3 x_{3} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} 5 x_{4} \\mspace{-6mu}&\\mspace{-6mu} = \\mspace{-6mu}&\\mspace{-6mu} 1 \\\\\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}  \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} 2 x_{4} \\mspace{-6mu}&\\mspace{-6mu} = \\mspace{-6mu}&\\mspace{-6mu} 3 \\\\\n",
       "\\end{array} \\\\\n",
       "x_{1}, x_{2}, x_{3}, x_{4} \\geq 0\n",
       "\\end{array}\\)</html>"
      ],
      "text/latex": [
       "$\\displaystyle \\begin{array}{l}\n",
       "\\begin{array}{lcrcrcrcrcl}\n",
       " \\max \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} 4 x_{1} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} 2 x_{2} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} 3 x_{3} \\mspace{-6mu}&\\mspace{-6mu} - \\mspace{-6mu}&\\mspace{-6mu} x_{4} \\mspace{-6mu}&\\mspace{-6mu}  \\mspace{-6mu}&\\mspace{-6mu} \\\\\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} 3 x_{3} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} 5 x_{4} \\mspace{-6mu}&\\mspace{-6mu} = \\mspace{-6mu}&\\mspace{-6mu} 1 \\\\\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}  \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} 2 x_{4} \\mspace{-6mu}&\\mspace{-6mu} = \\mspace{-6mu}&\\mspace{-6mu} 3 \\\\\n",
       "\\end{array} \\\\\n",
       "x_{1}, x_{2}, x_{3}, x_{4} \\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>\\(\\displaystyle 10\\)</html>"
      ],
      "text/latex": [
       "$\\displaystyle 10$"
      ],
      "text/plain": [
       "10"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html>\\(\\displaystyle \\left(2,\\,1,\\,0,\\,0\\right)\\)</html>"
      ],
      "text/latex": [
       "$\\displaystyle \\left(2,\\,1,\\,0,\\,0\\right)$"
      ],
      "text/plain": [
       "(2, 1, 0, 0)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html>\\(\\displaystyle {===================================}\\)</html>"
      ],
      "text/latex": [
       "$\\displaystyle {===================================}$"
      ],
      "text/plain": [
       "ry"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html>\\(\\displaystyle {}\\)</html>"
      ],
      "text/latex": [
       "$\\displaystyle {}$"
      ],
      "text/plain": [
       "salto"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html>\\(\\displaystyle {===================================}\\)</html>"
      ],
      "text/latex": [
       "$\\displaystyle {===================================}$"
      ],
      "text/plain": [
       "ry"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html>\\(\\displaystyle \\left(\\begin{array}{r|rrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{red}{t}}_{1} & {\\color{red}{t}}_{2} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{red}{t}}_{1} & 1 & -1 & 3 & 5 & 1 & 0 & 1 \\\\\n",
       "{\\color{red}{t}}_{2} & 1 & 1 & 0 & 2 & 0 & 1 & 3 \\\\\n",
       "\\hline\n",
       " {W_{ind}} & -2 & 0 & -3 & -7 & 0 & 0 & 4\n",
       "\\end{array}\\right)\\)</html>"
      ],
      "text/latex": [
       "$\\displaystyle \\left(\\begin{array}{r|rrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{red}{t}}_{1} & {\\color{red}{t}}_{2} & {b} \\\\\n",
       "\\hline\n",
       " {\\color{red}{t}}_{1} & 1 & -1 & 3 & 5 & 1 & 0 & 1 \\\\\n",
       "{\\color{red}{t}}_{2} & 1 & 1 & 0 & 2 & 0 & 1 & 3 \\\\\n",
       "\\hline\n",
       " {W_{ind}} & -2 & 0 & -3 & -7 & 0 & 0 & 4\n",
       "\\end{array}\\right)$"
      ],
      "text/plain": [
       "[  B| x1  x2  x3  x4  t1  t2|rhs]\n",
       "[---+-----------------------+---]\n",
       "[ t1|  1  -1   3   5   1   0|  1]\n",
       "[ t2|  1   1   0   2   0   1|  3]\n",
       "[---+-----------------------+---]\n",
       "[  W| -2   0  -3  -7   0   0|  4]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "tipo='max'  # max o min\n",
    "\n",
    "n=4 #  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+5,2,3,-1] )  # vector de costos\n",
    "\n",
    "A=matrix(QQ,m,n,[   # coeficientes de las variables x's\n",
    "[1,-1, 3, 5],\n",
    "[1,1,0,2]\n",
    "])\n",
    "\n",
    "b=matrix(QQ,m,1,[1,3])  # vector de la derecha\n",
    "\n",
    "\n",
    "######################################################\n",
    "############### VARIABLES DE HOLGURA  ################\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",
    "t=matrix(ZZ,1,m,[1,1])\n",
    "\n",
    "colId=(5,6)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "####################################################################\n",
    "############  HASTA AQUÍ LOS DATOS  ################################\n",
    "####################################################################\n",
    "\n",
    "\n",
    "\n",
    "load('https://sage.unex.es/501708/simplex2023CompletamenteManual.sage')\n",
    "\n",
    "#load('https://sage.unex.es/501708/simplex2023Auto.sage')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "515f33ba-9c94-48e6-a82b-b48d081f0d06",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html>\\(\\displaystyle \\left(\\begin{array}{r|rrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{red}{t}}_{1} & {\\color{red}{t}}_{2} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{1} & 1 & -1 & 3 & 5 & 1 & 0 & 1 \\\\\n",
       "{\\color{red}{t}}_{2} & 0 & 2 & -3 & -3 & -1 & 1 & 2 \\\\\n",
       "\\hline\n",
       " {W_{ind}} & 0 & -2 & 3 & 3 & 2 & 0 & 2\n",
       "\\end{array}\\right)\\)</html>"
      ],
      "text/latex": [
       "$\\displaystyle \\left(\\begin{array}{r|rrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{red}{t}}_{1} & {\\color{red}{t}}_{2} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{1} & 1 & -1 & 3 & 5 & 1 & 0 & 1 \\\\\n",
       "{\\color{red}{t}}_{2} & 0 & 2 & -3 & -3 & -1 & 1 & 2 \\\\\n",
       "\\hline\n",
       " {W_{ind}} & 0 & -2 & 3 & 3 & 2 & 0 & 2\n",
       "\\end{array}\\right)$"
      ],
      "text/plain": [
       "[  B| x1  x2  x3  x4  t1  t2|rhs]\n",
       "[---+-----------------------+---]\n",
       "[ x1|  1  -1   3   5   1   0|  1]\n",
       "[ t2|  0   2  -3  -3  -1   1|  2]\n",
       "[---+-----------------------+---]\n",
       "[  W|  0  -2   3   3   2   0|  2]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cambio(1,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f94413bc-0b33-4464-b55a-aca7c857fa5a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html>\\(\\displaystyle \\left(\\begin{array}{r|rrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{red}{t}}_{1} & {\\color{red}{t}}_{2} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{1} & 1 & 0 & \\frac{3}{2} & \\frac{7}{2} & \\frac{1}{2} & \\frac{1}{2} & 2 \\\\\n",
       "{x}_{2} & 0 & 1 & -\\frac{3}{2} & -\\frac{3}{2} & -\\frac{1}{2} & \\frac{1}{2} & 1 \\\\\n",
       "\\hline\n",
       " {W_{ind}} & 0 & 0 & 0 & 0 & 1 & 1 & 0\n",
       "\\end{array}\\right)\\)</html>"
      ],
      "text/latex": [
       "$\\displaystyle \\left(\\begin{array}{r|rrrrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {\\color{red}{t}}_{1} & {\\color{red}{t}}_{2} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{1} & 1 & 0 & \\frac{3}{2} & \\frac{7}{2} & \\frac{1}{2} & \\frac{1}{2} & 2 \\\\\n",
       "{x}_{2} & 0 & 1 & -\\frac{3}{2} & -\\frac{3}{2} & -\\frac{1}{2} & \\frac{1}{2} & 1 \\\\\n",
       "\\hline\n",
       " {W_{ind}} & 0 & 0 & 0 & 0 & 1 & 1 & 0\n",
       "\\end{array}\\right)$"
      ],
      "text/plain": [
       "[   B|  x1   x2   x3   x4   t1   t2| rhs]\n",
       "[----+-----------------------------+----]\n",
       "[  x1|   1    0  3/2  7/2  1/2  1/2|   2]\n",
       "[  x2|   0    1 -3/2 -3/2 -1/2  1/2|   1]\n",
       "[----+-----------------------------+----]\n",
       "[   W|   0    0    0    0    1    1|   0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cambio(2,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "94b113a4-8bb0-4ead-bca3-d77c979d50f7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html>\\(\\displaystyle \\left(\\begin{array}{r|rrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{1} & 1 & 0 & \\frac{3}{2} & \\frac{7}{2} & 2 \\\\\n",
       "{x}_{2} & 0 & 1 & -\\frac{3}{2} & -\\frac{3}{2} & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & 0 & 0 & -12 & 10\n",
       "\\end{array}\\right)\\)</html>"
      ],
      "text/latex": [
       "$\\displaystyle \\left(\\begin{array}{r|rrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{1} & 1 & 0 & \\frac{3}{2} & \\frac{7}{2} & 2 \\\\\n",
       "{x}_{2} & 0 & 1 & -\\frac{3}{2} & -\\frac{3}{2} & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & 0 & 0 & -12 & 10\n",
       "\\end{array}\\right)$"
      ],
      "text/plain": [
       "[   B|  x1   x2   x3   x4| rhs]\n",
       "[----+-------------------+----]\n",
       "[  x1|   1    0  3/2  7/2|   2]\n",
       "[  x2|   0    1 -3/2 -3/2|   1]\n",
       "[----+-------------------+----]\n",
       "[   Z|   0    0    0  -12|  10]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "faseII(0,(0,0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "f54d8e94-6a9f-4aab-87d1-07e3931760f0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html>\\(\\displaystyle \\left(\\begin{array}{r|rrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{3} & \\frac{2}{3} & 0 & 1 & \\frac{7}{3} & \\frac{4}{3} \\\\\n",
       "{x}_{2} & 1 & 1 & 0 & 2 & 3 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & 0 & 0 & -12 & 10\n",
       "\\end{array}\\right)\\)</html>"
      ],
      "text/latex": [
       "$\\displaystyle \\left(\\begin{array}{r|rrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{3} & \\frac{2}{3} & 0 & 1 & \\frac{7}{3} & \\frac{4}{3} \\\\\n",
       "{x}_{2} & 1 & 1 & 0 & 2 & 3 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & 0 & 0 & -12 & 10\n",
       "\\end{array}\\right)$"
      ],
      "text/plain": [
       "[  B| x1  x2  x3  x4|rhs]\n",
       "[---+---------------+---]\n",
       "[ x3|2/3   0   1 7/3|4/3]\n",
       "[ x2|  1   1   0   2|  3]\n",
       "[---+---------------+---]\n",
       "[  Z|  0   0   0 -12| 10]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cambio(1,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "0f671709-0f0d-4f75-b140-72f4de7f7cf5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html>\\(\\displaystyle \\left(\\begin{array}{r|rrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{1} & 1 & 0 & \\frac{3}{2} & \\frac{7}{2} & 2 \\\\\n",
       "{x}_{2} & 0 & 1 & -\\frac{3}{2} & -\\frac{3}{2} & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & 0 & 0 & -12 & 10\n",
       "\\end{array}\\right)\\)</html>"
      ],
      "text/latex": [
       "$\\displaystyle \\left(\\begin{array}{r|rrrr|r}\n",
       "{Basic} & {x}_{1} & {x}_{2} & {x}_{3} & {x}_{4} & {b} \\\\\n",
       "\\hline\n",
       " {x}_{1} & 1 & 0 & \\frac{3}{2} & \\frac{7}{2} & 2 \\\\\n",
       "{x}_{2} & 0 & 1 & -\\frac{3}{2} & -\\frac{3}{2} & 1 \\\\\n",
       "\\hline\n",
       " {Z_{ind}} & 0 & 0 & 0 & -12 & 10\n",
       "\\end{array}\\right)$"
      ],
      "text/plain": [
       "[   B|  x1   x2   x3   x4| rhs]\n",
       "[----+-------------------+----]\n",
       "[  x1|   1    0  3/2  7/2|   2]\n",
       "[  x2|   0    1 -3/2 -3/2|   1]\n",
       "[----+-------------------+----]\n",
       "[   Z|   0    0    0  -12|  10]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cambio(1,1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "SageMath",
   "language": "python",
   "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.13.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
