{"cells": [{"cell_type": "code", "execution_count": 1, "metadata": {"school_cell_uuid": "6c558053b6784cf891a4760d64a8e531"}, "outputs": [{"data": {"text/plain": [" con: array([], dtype=float64)\n", " fun: -1400.0\n", " message: 'Optimization terminated successfully.'\n", " nit: 3\n", " slack: array([ 200., 0., 0., 8100.])\n", " status: 0\n", " success: True\n", " x: array([300., 100.])"]}, "execution_count": 1, "metadata": {}, "output_type": "execute_result"}], "source": ["import scipy.optimize\n", "\n", "A = np.array([[-1, 0], [0, -1], [1, 2], [4, 5]])\n", "b = np.array([-100, -100, 500, 9800])\n", "c = np.array([-3, -5])\n", "\n", "result = sp.optimize.linprog(c, A, b)\n", "result"]}, {"cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\uc0c1\ud0dc: optimal\n", "\ucd5c\uc801\uac12: 299.99999999999983 100.00000000000001\n"]}], "source": ["import cvxpy as cp\n", "\n", "# \ubcc0\uc218\uc758 \uc815\uc758\n", "a = cp.Variable() # A\uc758 \uc0dd\uc0b0\ub7c9\n", "b = cp.Variable() # B\uc758 \uc0dd\uc0b0\ub7c9\n", "\n", "# \uc870\uac74\uc758 \uc815\uc758\n", "constraints = [\n", " a >= 100, # A\ub97c 100\uac1c \uc774\uc0c1 \uc0dd\uc0b0\ud574\uc57c \ud55c\ub2e4.\n", " b >= 100, # B\ub97c 100\uac1c \uc774\uc0c1 \uc0dd\uc0b0\ud574\uc57c \ud55c\ub2e4. \n", " a + 2 * b <= 500, # 500\uc2dc\uac04 \ub0b4\uc5d0 \uc0dd\uc0b0\ud574\uc57c \ud55c\ub2e4.\n", " 4 * a + 5 * b <= 9800, # \ubd80\ud488\uc774 9800\uac1c \ubc16\uc5d0 \uc5c6\ub2e4.\n", "]\n", "\n", "# \ubb38\uc81c\uc758 \uc815\uc758\n", "obj = cp.Maximize(3 * a + 5 * b)\n", "prob = cp.Problem(obj, constraints)\n", "\n", "# \uacc4\uc0b0\n", "prob.solve() \n", "\n", "# \uacb0\uacfc\n", "print(\"\uc0c1\ud0dc:\", prob.status)\n", "print(\"\ucd5c\uc801\uac12:\", a.value, b.value)"]}, {"cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [{"data": {"text/plain": ["array([[0.5],\n", " [0.5]])"]}, "execution_count": 3, "metadata": {}, "output_type": "execute_result"}], "source": ["from cvxopt import matrix, solvers\n", "\n", "Q = matrix(np.diag([2.0, 2.0]))\n", "c = matrix(np.array([0.0, 0.0]))\n", "A = matrix(np.array([[1.0, 1.0]]))\n", "b = matrix(np.array([[1.0]]))\n", "\n", "sol = solvers.qp(Q, c, A=A, b=b)\n", "np.array(sol['x'])"]}], "metadata": {"celltoolbar": "Edit 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.0"}, "pycharm": {"stem_cell": {"cell_type": "raw", "metadata": {"collapsed": false}, "source": []}}}, "nbformat": 4, "nbformat_minor": 2}