293 lines
10 KiB
Plaintext
293 lines
10 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"ISYE 6669 Homework 5\n",
|
|
"\n",
|
|
"By: Mark Pearl \n",
|
|
"\n",
|
|
"February 20, 2021"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"output_type": "error",
|
|
"ename": "ModuleNotFoundError",
|
|
"evalue": "No module named 'cvxpy'",
|
|
"traceback": [
|
|
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
|
"\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
|
|
"\u001b[1;32m<ipython-input-1-205be1a4cf27>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mcvxpy\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mcp\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;31m# inputs\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m c = np.array([[3, 7, 11, 8], \n\u001b[0;32m 5\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m6\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
|
|
"\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'cvxpy'"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import cvxpy as cp\n",
|
|
"import numpy as np\n",
|
|
"# inputs\n",
|
|
"c = np.array([[3, 7, 11, 8], \n",
|
|
" [0, 4, 4, 6], \n",
|
|
" [0, 4, 10, 9], \n",
|
|
" [0, 0, 6, 5]])\n",
|
|
"# declaring variables\n",
|
|
"x = cp.Variable((4,4))\n",
|
|
"# defining objective\n",
|
|
"objective = cp.Minimize(cp.sum(cp.multiply(c, x)))\n",
|
|
"\n",
|
|
"# defining constraints\n",
|
|
"constraints = []\n",
|
|
"for ii in range(4): \n",
|
|
" constraints.append((cp.sum(x[ii,:])==1.0)) \n",
|
|
" constraints.append((cp.sum(x[:,ii])==1.0)) \n",
|
|
" for jj in range(4): \n",
|
|
" constraints.append(x[ii,jj]>=0)\n",
|
|
"\n",
|
|
"myprob = cp.Problem(objective, constraints)\n",
|
|
"myprob.solve()\n",
|
|
"# printing outputs\n",
|
|
"print(\"\\nThe optimal value is\", round(myprob.value, 2))\n",
|
|
"print(\"The values of x are \", np.round(x.value))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 132,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"77.0\n",
|
|
"optimal\n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"c:\\users\\mjpearl\\desktop\\omsa\\isye-6669-oan\\env_isye6669\\lib\\site-packages\\ipykernel_launcher.py:4: PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal with linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code to use regular ndarray.\n",
|
|
" after removing the cwd from sys.path.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"import cvxpy as cp\n",
|
|
"\n",
|
|
"c = np.matrix([[3, 0, 0, 0], [7, 4, 4, 0], [11, 4, 10, 6], [8, 6, 9, 5]])\n",
|
|
"x = cp.Variable(c.shape, boolean=True)\n",
|
|
"constraints = [x>=0,x==1]\n",
|
|
"objective = cp.Minimize(cp.sum(cp.sum(cp.multiply(c,x))))\n",
|
|
"prob = cp.Problem(objective,constraints)\n",
|
|
"\n",
|
|
"prob.solve()\n",
|
|
"print(prob.value)\n",
|
|
"print(prob.status)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"The optimal value is 1269.36\n",
|
|
"The quantum of electricity produced at 1,3,5 are [20.0, 69.36, 130.64]\n",
|
|
"The flows in different lines are [-10.0, -20.0, 49.36, -70.64, 60.0, -30.0]\n",
|
|
"The electricity price at node 2 is 6.97\n",
|
|
"The electricity price at node 4 is 5.58\n",
|
|
"The electricity price at node 6 is 8.0\n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"c:\\users\\mjpearl\\desktop\\omsa\\isye-6669-oan\\env_isye6669\\lib\\site-packages\\cvxpy\\expressions\\expression.py:550: UserWarning: \n",
|
|
"This use of ``*`` has resulted in matrix multiplication.\n",
|
|
"Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.\n",
|
|
" Use ``*`` for matrix-scalar and vector-scalar multiplication.\n",
|
|
" Use ``@`` for matrix-matrix and matrix-vector multiplication.\n",
|
|
" Use ``multiply`` for elementwise multiplication.\n",
|
|
"\n",
|
|
" warnings.warn(__STAR_MATMUL_WARNING__, UserWarning)\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import cvxpy as cvx\n",
|
|
"import numpy as np\n",
|
|
"# inputs provides\n",
|
|
"p_min = np.array([20, 20, 10])\n",
|
|
"p_max = np.array([70, 150, 150])\n",
|
|
"f_max = np.array([100, 120, 50, 90, 60, 50])\n",
|
|
"B = [11.6, 5.9, 13.7, 9.8, 5.6, 10.5]\n",
|
|
"c = np.array([10, 6, 5])\n",
|
|
"d = [10, 120, 90]\n",
|
|
"# declaring variables\n",
|
|
"p = cvx.Variable(3)\n",
|
|
"f = cvx.Variable(6)\n",
|
|
"o = cvx.Variable(6)\n",
|
|
"# defining objective\n",
|
|
"objective = cvx.Minimize(p * c)\n",
|
|
"# defining constraints\n",
|
|
"constraints = [f[0] - f[-1] == p[0], \n",
|
|
" f[2] - f[1] == p[1], \n",
|
|
" f[4] - f[3] == p[2], \n",
|
|
" f[1] - f[0] == -d[0], \n",
|
|
" f[3] - f[2] == -d[1], \n",
|
|
" f[5] - f[4] == -d[2], \n",
|
|
" f[0] == B[0] * (o[0] - o[1]), \n",
|
|
" f[1] == B[1] * (o[1] - o[2]), \n",
|
|
" f[2] == B[2] * (o[2] - o[3]), \n",
|
|
" f[3] == B[3] * (o[3] - o[4]), \n",
|
|
" f[4] == B[4] * (o[4] - o[5]), \n",
|
|
" f[5] == B[5] * (o[5] - o[0]), \n",
|
|
" -f_max <= f, \n",
|
|
" f <= f_max, \n",
|
|
" p_min <= p, \n",
|
|
" p <= p_max,\n",
|
|
" ]\n",
|
|
"myprob = cvx.Problem(objective, constraints)\n",
|
|
"myprob.solve()# printing outputs\n",
|
|
"print(\"\\nThe optimal value is\", round(myprob.value, 2))\n",
|
|
"print(\"The quantum of electricity produced at 1,3,5 are\", [round(x, 2) for x in p.value])\n",
|
|
"print(\"The flows in different lines are\", [round(x, 2) for x in f.value])\n",
|
|
"print(\"The electricity price at node 2 is\", round(constraints[3].dual_value, 2))\n",
|
|
"print(\"The electricity price at node 4 is\", round(constraints[4].dual_value, 2))\n",
|
|
"print(\"The electricity price at node 6 is\", round(constraints[5].dual_value, 2))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 125,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Optimal Value: 1249.9999997631494\n",
|
|
"With the following variables: 19.999999928688347 50.00000011972041 149.99999995158868 59.85392656833289 63.21238250844543 71.5103636788361 71.43431176537509 59.7820689114058 54.238870488904865 -38.95808890530549 -48.958088905305054 1.041911214415802 -118.95808878558378 31.041911166005285 -58.95808883399427\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import cvxpy as cp\n",
|
|
"import numpy as np\n",
|
|
"\n",
|
|
"p1=cp.Variable()\n",
|
|
"p2=cp.Variable()\n",
|
|
"p3=cp.Variable()\n",
|
|
"theta1=cp.Variable()\n",
|
|
"theta2=cp.Variable()\n",
|
|
"theta3=cp.Variable()\n",
|
|
"theta4=cp.Variable()\n",
|
|
"theta5=cp.Variable()\n",
|
|
"theta6=cp.Variable()\n",
|
|
"flow1=cp.Variable()\n",
|
|
"flow2=cp.Variable()\n",
|
|
"flow3=cp.Variable()\n",
|
|
"flow4=cp.Variable()\n",
|
|
"flow5=cp.Variable()\n",
|
|
"flow6=cp.Variable()\n",
|
|
"\n",
|
|
"constraints= [p1>=20,p1<=70,\n",
|
|
" p2>=20,p2<=150,\n",
|
|
" p3>=10,p3<=150,\n",
|
|
" flow1<=100,flow2<=120,\n",
|
|
" flow3<=50,flow4<=90,\n",
|
|
" flow5<=60,flow6<=50,\n",
|
|
" flow1-flow6==p1,\n",
|
|
" flow3-flow2==p2,\n",
|
|
" flow5-flow4==p3,\n",
|
|
" flow1-flow2==10,\n",
|
|
" flow3-flow4==120,\n",
|
|
" flow5-flow6==90,\n",
|
|
" flow1==11.6*theta1-11.6*theta2,\n",
|
|
" flow2==5.9*theta2-5.9*theta3,\n",
|
|
" flow3==13.7*theta3-13.7*theta4, \n",
|
|
" flow4==9.8*theta4-13.7*theta5, \n",
|
|
" flow5==5.6*theta5-5.6*theta6,\n",
|
|
" flow6==10.5*theta6-10.5*theta1]\n",
|
|
"\n",
|
|
"obj = cp.Minimize(10*p1+6*p2+5*p3)\n",
|
|
"\n",
|
|
"prob = cp.Problem(obj,constraints)\n",
|
|
"prob.solve()\n",
|
|
"\n",
|
|
"print(\"Optimal Value:\",prob.value)\n",
|
|
"print(\"With the following variables:\",\n",
|
|
" p1.value,\n",
|
|
"p2.value,\n",
|
|
"p3.value,\n",
|
|
"theta1.value,\n",
|
|
"theta2.value,\n",
|
|
"theta3.value,\n",
|
|
"theta4.value,\n",
|
|
"theta5.value,\n",
|
|
"theta6.value,\n",
|
|
"flow1.value,\n",
|
|
"flow2.value,\n",
|
|
"flow3.value,\n",
|
|
"flow4.value,\n",
|
|
"flow5.value,\n",
|
|
"flow6.value)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 129,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"-5.9999999735722565\n",
|
|
"-5.999999975933699\n",
|
|
"-5.999999973803812\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(constraints[15].dual_value)\n",
|
|
"print(constraints[16].dual_value)\n",
|
|
"print(constraints[17].dual_value)"
|
|
]
|
|
}
|
|
],
|
|
"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.9-final"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
} |