113 lines
2.6 KiB
Plaintext
113 lines
2.6 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 51,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"output_type": "stream",
|
|
"name": "stdout",
|
|
"text": [
|
|
"\nThe optimal value is 4.043281386825957e-10\nThe values of a and b are 57.94946776796661 28.97473388417445\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import cvxpy as cp\n",
|
|
"import numpy as np\n",
|
|
"\n",
|
|
"# declaring variables\n",
|
|
"z = cp.Variable()\n",
|
|
"a = cp.Variable()\n",
|
|
"b = cp.Variable()\n",
|
|
"\n",
|
|
"# defining objective\n",
|
|
"objective1 = cp.Minimize(z)\n",
|
|
"\n",
|
|
"# defining constraints\n",
|
|
"constraints1 = [\n",
|
|
"a-(2*b) <= z,\n",
|
|
"(2*b)-a <= z,\n",
|
|
"a+b >= 50,\n",
|
|
"a >= 30,\n",
|
|
"a <= 100,\n",
|
|
"b <= 100,\n",
|
|
"b >= 0\n",
|
|
"]\n",
|
|
"\n",
|
|
"problem1 = cp.Problem(objective1, constraints1)\n",
|
|
"problem1.solve()\n",
|
|
"# printing outputs\n",
|
|
"print(\"\\nThe optimal value is\", problem1.value)\n",
|
|
"print(\"The values of a and b are \", a.value, b.value)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 53,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"output_type": "stream",
|
|
"name": "stdout",
|
|
"text": [
|
|
"\nThe optimal value is 3200.000001852015\nThe values of a and b are -6.345394058853363e-08 500.00000006345397\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import cvxpy as cp\n",
|
|
"import numpy as np\n",
|
|
"\n",
|
|
"# declaring variables\n",
|
|
"c_1 = cp.Variable()\n",
|
|
"c_2 = cp.Variable()\n",
|
|
"x = cp.Variable()\n",
|
|
"\n",
|
|
"# defining objective\n",
|
|
"objective2 = cp.Minimize(c_1+c_2)\n",
|
|
"\n",
|
|
"# defining constraints\n",
|
|
"constraints2 = [\n",
|
|
"x >= 0,\n",
|
|
"x <= 500,\n",
|
|
"10*x <= c_1, \n",
|
|
"1200 <= c_2,\n",
|
|
"1200 + 5*(400-x) <= c_2,\n",
|
|
"]\n",
|
|
"\n",
|
|
"problem2 = cp.Problem(objective2, constraints2)\n",
|
|
"problem2.solve()\n",
|
|
"# printing outputs\n",
|
|
"print(\"\\nThe optimal value is\", problem2.value)\n",
|
|
"print(\"The values of a and b are \", x.value, 500-x.value)"
|
|
]
|
|
}
|
|
],
|
|
"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
|
|
} |