-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathMT2022513.py
1 lines (1 loc) · 1.96 KB
/
MT2022513.py
1
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyPgQvEZvXRRJVHJ02bm9y0T"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"code","execution_count":1,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"0P2S3mOffegX","executionInfo":{"status":"ok","timestamp":1666690556314,"user_tz":-330,"elapsed":703,"user":{"displayName":"Raj Kachhadiya","userId":"09117100948785957797"}},"outputId":"8dcf438a-ce72-448d-f5f4-ebc1f6f18c58"},"outputs":[{"output_type":"stream","name":"stdout","text":["\n","The optimal value is 105.86446788567747\n","A solution x is\n","[-1.70652998 0.23218369 -2.12070071 -2.57210791 1.21339644 0.05753306\n"," -4.08860369 3.6157608 -0.12584929 -3.17653077]\n","A dual solution corresponding to the inequality constraints is\n","[ 0. 0. 0. 0. 0. 9.55277892\n"," 0. 0. 0. 45.38702192 0. 0.\n"," 0. 31.21047075 8.65338335]\n"]}],"source":["import cvxpy as cp\n","import numpy as np\n","\n","# Generate a random non-trivial quadratic program.\n","m = 15\n","n = 10\n","p = 5\n","np.random.seed(1)\n","P = np.random.randn(n, n)\n","P = P.T @ P\n","q = np.random.randn(n)\n","G = np.random.randn(m, n)\n","h = G @ np.random.randn(n)\n","A = np.random.randn(p, n)\n","b = np.random.randn(p)\n","\n","# Define and solve the CVXPY problem.\n","x = cp.Variable(n)\n","prob = cp.Problem(cp.Minimize((1/2)*cp.quad_form(x, P) - q.T @ x),\n"," [G @ x <= h,\n"," A @ x == b])\n","prob.solve()\n","\n","# Print result.\n","print(\"\\nThe optimal value is\", prob.value)\n","print(\"A solution x is\")\n","print(x.value)\n","print(\"A dual solution corresponding to the inequality constraints is\")\n","print(prob.constraints[0].dual_value)"]},{"cell_type":"code","source":[],"metadata":{"id":"6lfTy5fHf11V"},"execution_count":null,"outputs":[]}]}