230 lines
4.9 KiB
Plaintext
230 lines
4.9 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 75,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"from scipy import linalg\n",
|
|
"from cvxpy import *\n",
|
|
"A = np.matrix([[1, 1, 1, 0, 0],\n",
|
|
"[-2, 1, 0, 1, 0],\n",
|
|
"[2, 1, 0, 0, 1]])\n",
|
|
"b = np.array([4, 2, 6])\n",
|
|
"c = np.array([-1, -2, 0, 0, 0])\n",
|
|
"# put in the indices of the basic variables, ranging from 1, 2,..., 5\n",
|
|
"def ComputeBasicVar(Ind1, Ind2, Ind3):\n",
|
|
" Ind1 -= 1\n",
|
|
" Ind2 -= 1\n",
|
|
" Ind3 -= 1\n",
|
|
" A_B = A[:, (Ind1, Ind2, Ind3)]\n",
|
|
" c_B = c[(Ind1, Ind2, Ind3),]\n",
|
|
" print(\"A_B = \")\n",
|
|
" print(A_B)\n",
|
|
" print(\"c_B = \")\n",
|
|
" print(c_B)\n",
|
|
" print(\"x_B = \")\n",
|
|
" print(linalg.inv(A_B).dot(b))\n",
|
|
" #print(\"x_B = [x_{0}, x_{1}, x_{2}] = \".format(Ind1 + 1, Ind2 + 1, Ind3 + 1))\n",
|
|
" #print(x_B)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 76,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"output_type": "stream",
|
|
"name": "stdout",
|
|
"text": [
|
|
"A_B = \n[[1 0 0]\n [0 1 0]\n [0 0 1]]\nc_B = \n[0 0 0]\nx_B = \n[4. 2. 6.]\nNone\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(ComputeBasicVar(3,4,5 ))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 83,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"output_type": "stream",
|
|
"name": "stdout",
|
|
"text": [
|
|
"A_B = \n[[ 1 1 0]\n [-2 0 0]\n [ 2 0 1]]\nc_B = \n[-1 0 0]\nx_B = \n[-1. 5. 8.]\nNone\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(ComputeBasicVar(1,3,5)) "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 78,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"output_type": "stream",
|
|
"name": "stdout",
|
|
"text": [
|
|
"A_B = \n[[ 1 0 0]\n [-2 1 0]\n [ 2 0 1]]\nc_B = \n[-1 0 0]\nx_B = \n[ 4. 10. -2.]\nNone\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(ComputeBasicVar(1,4,5))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 79,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"output_type": "stream",
|
|
"name": "stdout",
|
|
"text": [
|
|
"A_B = \n[[ 1 1 0]\n [-2 0 1]\n [ 2 0 0]]\nc_B = \n[-1 0 0]\nx_B = \n[3. 1. 8.]\nNone\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(ComputeBasicVar(1,3,4))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 80,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"output_type": "stream",
|
|
"name": "stdout",
|
|
"text": [
|
|
"A_B = \n[[1 1 0]\n [1 0 1]\n [1 0 0]]\nc_B = \n[-2 0 0]\nx_B = \n[ 6. -2. -4.]\nNone\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(ComputeBasicVar(2,3,4))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 84,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"output_type": "stream",
|
|
"name": "stdout",
|
|
"text": [
|
|
"A_B = \n[[1 0 0]\n [1 1 0]\n [1 0 1]]\nc_B = \n[-2 0 0]\nx_B = \n[ 4. -2. 2.]\nNone\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(ComputeBasicVar(2,4,5))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 85,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"output_type": "stream",
|
|
"name": "stdout",
|
|
"text": [
|
|
"A_B = \n[[1 1 0]\n [1 0 0]\n [1 0 1]]\nc_B = \n[-2 0 0]\nx_B = \n[2. 2. 4.]\nNone\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(ComputeBasicVar(2,3,5))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 86,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"output_type": "stream",
|
|
"name": "stdout",
|
|
"text": [
|
|
"A_B = \n[[ 1 1 0]\n [-2 1 0]\n [ 2 1 1]]\nc_B = \n[-1 -2 0]\nx_B = \n[0.66666667 3.33333333 1.33333333]\nNone\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(ComputeBasicVar(1,2,5))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 88,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"output_type": "stream",
|
|
"name": "stdout",
|
|
"text": [
|
|
"A_B = \n[[ 1 1 0]\n [-2 1 1]\n [ 2 1 0]]\nc_B = \n[-1 -2 0]\nx_B = \n[2. 2. 4.]\nNone\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(ComputeBasicVar(1,2,4))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 89,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"output_type": "stream",
|
|
"name": "stdout",
|
|
"text": [
|
|
"A_B = \n[[ 1 1 1]\n [-2 1 0]\n [ 2 1 0]]\nc_B = \n[-1 -2 0]\nx_B = \n[ 1. 4. -1.]\nNone\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(ComputeBasicVar(1,2,3))"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"name": "python3",
|
|
"display_name": "Python 3.7.9 64-bit",
|
|
"metadata": {
|
|
"interpreter": {
|
|
"hash": "e9752fd82d88768c444c24c0be54a259b62c17c2d80b41ed9b492f84bd073631"
|
|
}
|
|
}
|
|
},
|
|
"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.7.9-final"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
} |