Created
November 9, 2020 02:12
-
-
Save t-flora/0237f69d4a657afb764a9c6acc3072b4 to your computer and use it in GitHub Desktop.
CS164 - Linear Programming I
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Linear Programming I" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"status: optimal\n", | |
"Optimal value: 24.000\n", | |
"Optimal vars: 6.000, 2.000\n" | |
] | |
} | |
], | |
"source": [ | |
"import cvxpy as cp\n", | |
"\n", | |
"x = cp.Variable()\n", | |
"y = cp.Variable()\n", | |
"\n", | |
"constraints = [y - x*2/3 - 2 <= 0,\n", | |
" y + x*2/3 - 6 <= 0,\n", | |
" x >= 0,\n", | |
" y - x*2/3 + 2 >= 0]\n", | |
"\n", | |
"obj = cp.Maximize(5*x - 3*y)\n", | |
"\n", | |
"# Form and solve problem.\n", | |
"prob = cp.Problem(obj, constraints)\n", | |
"prob.solve()\n", | |
"\n", | |
"print(\"status:\", prob.status)\n", | |
"print(f\"Optimal value: {prob.value:.3f}\")\n", | |
"print(f\"Optimal vars: {x.value:.3f}, {y.value:.3f}\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"status: optimal\n", | |
"Optimal value: 8000.000\n", | |
"Optimal vars: 80.000, 160.000\n" | |
] | |
} | |
], | |
"source": [ | |
"x = cp.Variable()\n", | |
"y = cp.Variable()\n", | |
"\n", | |
"constraints = [y + x <= 240,\n", | |
" y + x*2 <= 320,\n", | |
" x >= 0,\n", | |
" y >= 0]\n", | |
"\n", | |
"obj = cp.Maximize(40*x + 30*y)\n", | |
"\n", | |
"# Form and solve problem.\n", | |
"prob = cp.Problem(obj, constraints)\n", | |
"prob.solve()\n", | |
"\n", | |
"print(\"status:\", prob.status)\n", | |
"print(f\"Optimal value: {prob.value:.3f}\")\n", | |
"print(f\"Optimal vars: {x.value:.3f}, {y.value:.3f}\")" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"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.8" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment