{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/lib/python3/dist-packages/sage/misc/remote_file.py:46: DeprecationWarning: ssl.SSLContext() without protocol argument is deprecated.\n",
" content = urlopen(req, timeout=1, context=SSLContext())\n",
"/usr/lib/python3/dist-packages/sage/misc/remote_file.py:46: DeprecationWarning: ssl.PROTOCOL_TLS is deprecated\n",
" content = urlopen(req, timeout=1, context=SSLContext())\n"
]
},
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}$$"
],
"text/plain": [
"ry"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}$$"
],
"text/plain": [
"ry"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\begin{array}{l}\n",
"\\begin{array}{lcrcrcl}\n",
" \\max \\mspace{-6mu}&\\mspace{-6mu} \\mspace{-6mu}&\\mspace{-6mu} 1500 x_{1} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} 1600 x_{2} \\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} \\leq \\mspace{-6mu}&\\mspace{-6mu} 170 \\\\\n",
" \\mspace{-6mu}&\\mspace{-6mu} \\mspace{-6mu}&\\mspace{-6mu} 50 x_{1} \\mspace{-6mu}&\\mspace{-6mu} \\mspace{-6mu}&\\mspace{-6mu} \\mspace{-6mu}&\\mspace{-6mu} \\leq \\mspace{-6mu}&\\mspace{-6mu} 7000 \\\\\n",
" \\mspace{-6mu}&\\mspace{-6mu} \\mspace{-6mu}&\\mspace{-6mu} \\mspace{-6mu}&\\mspace{-6mu} \\mspace{-6mu}&\\mspace{-6mu} 80 x_{2} \\mspace{-6mu}&\\mspace{-6mu} \\leq \\mspace{-6mu}&\\mspace{-6mu} 10000 \\\\\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": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}267500$$"
],
"text/plain": [
"267500"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(45,\\,125\\right)$$"
],
"text/plain": [
"(45, 125)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}$$"
],
"text/plain": [
"ry"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}$$"
],
"text/plain": [
"ry"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{}$$"
],
"text/plain": [
"salto"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{\\texttt{El dual es:}}$$"
],
"text/plain": [
"dual"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}$$"
],
"text/plain": [
"ry"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/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} 170 y_{1} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} 7000 y_{2} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} 10000 y_{3} \\mspace{-6mu}&\\mspace{-6mu} \\mspace{-6mu}&\\mspace{-6mu} \\\\\n",
" \\mspace{-6mu}&\\mspace{-6mu} \\mspace{-6mu}&\\mspace{-6mu} y_{1} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} 50 y_{2} \\mspace{-6mu}&\\mspace{-6mu} \\mspace{-6mu}&\\mspace{-6mu} \\mspace{-6mu}&\\mspace{-6mu} \\geq \\mspace{-6mu}&\\mspace{-6mu} 1500 \\\\\n",
" \\mspace{-6mu}&\\mspace{-6mu} \\mspace{-6mu}&\\mspace{-6mu} y_{1} \\mspace{-6mu}&\\mspace{-6mu} \\mspace{-6mu}&\\mspace{-6mu} \\mspace{-6mu}&\\mspace{-6mu} + \\mspace{-6mu}&\\mspace{-6mu} 80 y_{3} \\mspace{-6mu}&\\mspace{-6mu} \\geq \\mspace{-6mu}&\\mspace{-6mu} 1600 \\\\\n",
"\\end{array} \\\\\n",
"y_{1}, y_{2}, y_{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": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}$$"
],
"text/plain": [
"ry"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}267500$$"
],
"text/plain": [
"267500"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(1500,\\,0,\\,\\frac{5}{4}\\right)$$"
],
"text/plain": [
"(1500, 0, 5/4)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{}$$"
],
"text/plain": [
"salto"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}{===================================}$$"
],
"text/plain": [
"ry"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrr|r}\n",
"{Basic} & {x}_{1} & {x}_{2} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
"\\hline\n",
" {\\color{green}{s}}_{1} & 1 & 1 & 1 & 0 & 0 & 170 \\\\\n",
"{\\color{green}{s}}_{2} & 50 & 0 & 0 & 1 & 0 & 7000 \\\\\n",
"{\\color{green}{s}}_{3} & 0 & 80 & 0 & 0 & 1 & 10000 \\\\\n",
"\\hline\n",
" {Z_{ind}} & 1500 & 1600 & 0 & 0 & 0 & 0\n",
"\\end{array}\\right)$$"
],
"text/plain": [
"[ B| x1 x2 s1 s2 s3| rhs]\n",
"[-----+-----------------------------+-----]\n",
"[ s1| 1 1 1 0 0| 170]\n",
"[ s2| 50 0 0 1 0| 7000]\n",
"[ s3| 0 80 0 0 1|10000]\n",
"[-----+-----------------------------+-----]\n",
"[ Z| 1500 1600 0 0 0| 0]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"cambio( 3 , 2 )\n"
]
},
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrr|r}\n",
"{Basic} & {x}_{1} & {x}_{2} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
"\\hline\n",
" {\\color{green}{s}}_{1} & 1 & 0 & 1 & 0 & -\\frac{1}{80} & 45 \\\\\n",
"{\\color{green}{s}}_{2} & 50 & 0 & 0 & 1 & 0 & 7000 \\\\\n",
"{x}_{2} & 0 & 1 & 0 & 0 & \\frac{1}{80} & 125 \\\\\n",
"\\hline\n",
" {Z_{ind}} & 1500 & 0 & 0 & 0 & -20 & 200000\n",
"\\end{array}\\right)$$"
],
"text/plain": [
"[ B| x1 x2 s1 s2 s3| rhs]\n",
"[------+----------------------------------+------]\n",
"[ s1| 1 0 1 0 -1/80| 45]\n",
"[ s2| 50 0 0 1 0| 7000]\n",
"[ x2| 0 1 0 0 1/80| 125]\n",
"[------+----------------------------------+------]\n",
"[ Z| 1500 0 0 0 -20|200000]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"cambio( 1 , 1 )\n"
]
},
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrr|r}\n",
"{Basic} & {x}_{1} & {x}_{2} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} \\\\\n",
"\\hline\n",
" {x}_{1} & 1 & 0 & 1 & 0 & -\\frac{1}{80} & 45 \\\\\n",
"{\\color{green}{s}}_{2} & 0 & 0 & -50 & 1 & \\frac{5}{8} & 4750 \\\\\n",
"{x}_{2} & 0 & 1 & 0 & 0 & \\frac{1}{80} & 125 \\\\\n",
"\\hline\n",
" {Z_{ind}} & 0 & 0 & -1500 & 0 & -\\frac{5}{4} & 267500\n",
"\\end{array}\\right)$$"
],
"text/plain": [
"[ B| x1 x2 s1 s2 s3| rhs]\n",
"[------+----------------------------------+------]\n",
"[ x1| 1 0 1 0 -1/80| 45]\n",
"[ s2| 0 0 -50 1 5/8| 4750]\n",
"[ x2| 0 1 0 0 1/80| 125]\n",
"[------+----------------------------------+------]\n",
"[ Z| 0 0 -1500 0 -5/4|267500]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"El problema es óptimo\n"
]
}
],
"source": [
"tipo='max' # max o min\n",
"\n",
"n=2 # número de variables x's\n",
"m=3 # número de restricciones (distintas de las de signo)\n",
"\n",
"c=matrix(QQ,1,n,[30*50,20*80] ) # vector de costos\n",
"\n",
"A=matrix(QQ,m,n,[ # coeficientes de las variables x's\n",
"[1,1],\n",
"[50,0],\n",
"[0,80]\n",
"])\n",
"\n",
"b=matrix(QQ,m,1,[170,7000,10000]) # vector de la derecha\n",
"\n",
"\n",
"######################################################\n",
"############### VARIABLES DE HOLGURA ################\n",
"\n",
"\n",
"h=matrix(ZZ,1,m,[1,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",
"\n",
"\n",
"\n",
"####################################################################\n",
"############ HASTA AQUÍ LOS DATOS ################################\n",
"####################################################################\n",
"\n",
"load('https://sage.unex.es/501708/simplex2023Auto.sage')\n",
"#load('https://sage.unex.es/501708/SAGE/simplex2023AutoREVISED.sage')\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrr|rr}\n",
"{Basic} & {x}_{1} & {x}_{2} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} & {b_1} \\\\\n",
"\\hline\n",
" {x}_{1} & 1 & 0 & 1 & 0 & -\\frac{1}{80} & 45 & 1 \\\\\n",
"{\\color{green}{s}}_{2} & 0 & 0 & -50 & 1 & \\frac{5}{8} & 4750 & -50 \\\\\n",
"{x}_{2} & 0 & 1 & 0 & 0 & \\frac{1}{80} & 125 & 0 \\\\\n",
"\\hline\n",
" {Z_{ind}} & 0 & 0 & -1500 & 0 & -\\frac{5}{4} & 267500 & 1500\n",
"\\end{array}\\right)$$"
],
"text/plain": [
"[ B| x1 x2 s1 s2 s3| rhs rhs1]\n",
"[------+----------------------------------+-------------]\n",
"[ x1| 1 0 1 0 -1/80| 45 1]\n",
"[ s2| 0 0 -50 1 5/8| 4750 -50]\n",
"[ x2| 0 1 0 0 1/80| 125 0]\n",
"[------+----------------------------------+-------------]\n",
"[ Z| 0 0 -1500 0 -5/4|267500 1500]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"b1p=matrix(QQ,m,1,[1,0,0]); param1b(b1p)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/latex": [
"$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{r|rrrrr|rr}\n",
"{Basic} & {x}_{1} & {x}_{2} & {\\color{green}{s}}_{1} & {\\color{green}{s}}_{2} & {\\color{green}{s}}_{3} & {b} & {b_1} \\\\\n",
"\\hline\n",
" {x}_{1} & 1 & 0 & 0 & \\frac{1}{50} & 0 & 140 & 0 \\\\\n",
"{\\color{green}{s}}_{1} & 0 & 0 & 1 & -\\frac{1}{50} & -\\frac{1}{80} & -95 & 1 \\\\\n",
"{x}_{2} & 0 & 1 & 0 & 0 & \\frac{1}{80} & 125 & 0 \\\\\n",
"\\hline\n",
" {Z_{ind}} & 0 & 0 & 0 & -30 & -20 & 410000 & 0\n",
"\\end{array}\\right)$$"
],
"text/plain": [
"[ B| x1 x2 s1 s2 s3| rhs rhs1]\n",
"[------+----------------------------------+-------------]\n",
"[ x1| 1 0 0 1/50 0| 140 0]\n",
"[ s1| 0 0 1 -1/50 -1/80| -95 1]\n",
"[ x2| 0 1 0 0 1/80| 125 0]\n",
"[------+----------------------------------+-------------]\n",
"[ Z| 0 0 0 -30 -20|410000 0]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"cambio(2,3)"
]
}
],
"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": 4
}