Files
tunmnlu/task_2/others-answer/omsa-main/ISYE-6669-OAN/hw10/hw10.ipynb
louiscklaw 9035c1312b update,
2025-02-01 02:09:32 +08:00

166 lines
5.2 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"The optimal value is 9.79\n",
"The values for variable x are: [[1.5 ]\n",
" [4.28571429]\n",
" [4. ]]\n"
]
}
],
"source": [
"import cvxpy as cp\n",
"import numpy as np\n",
"# inputs\n",
"A = np.array([[10, 0, 0], \n",
" [0, 7, 0], \n",
" [0, 0, 5]])\n",
"\n",
"c = np.ones((3,1))\n",
"b = np.array([15, 30, 20]).reshape((3,1))\n",
"# declaring variables\n",
"x = cp.Variable((3,1))\n",
"\n",
"# defining objective\n",
"objective = cp.Minimize(cp.sum(x))\n",
"\n",
"# defining constraints\n",
"constraints = [A@x==b,\n",
" x>=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 for variable x are:\", myprob.variables()[0].value)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[15],\n",
" [30],\n",
" [20]])"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def calculate_reduced_costs(c, B):\n",
" #Get inverse of basis matrix\n",
" B_inv = np.linalg.inv(B)\n",
" #Get reduced costs for simplex method step\n",
" w = B_inv.dot(c)\n",
" #Return index of minimum value\n",
" return np.argmin(w)"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"The optimal value is 11.0\n",
"The values for variable x are: [[11.]\n",
" [ 0.]\n",
" [ 0.]]\n"
]
}
],
"source": [
"#Knapsack problem\n",
"a= cp.Variable((3,1), integer=True)\n",
"w = np.array([7, 11, 16]).reshape((3,1))\n",
"\n",
"knapsack_objective = cp.Maximize(cp.sum(a))\n",
"# defining constraints\n",
"knap_sack_constraints = [cp.sum(cp.multiply(w,a))<=80,\n",
" a>=0]\n",
"knapsack_prob = cp.Problem(knapsack_objective, knap_sack_constraints)\n",
"knapsack_prob.solve()\n",
"\n",
"# printing outputs\n",
"print(\"\\nThe optimal value is\", round(knapsack_prob.value, 2))\n",
"print(\"The values for variable x are:\", knapsack_prob.variables()[0].value)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"For I,j,k in range(0, int(W/min(w)):\n",
"If each I j and k times their weight <= b(each element associated with which entry I j and k weights are multiplied by):\n",
"Append to an empty list [i, j, k]"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "cannot unpack non-iterable int object",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-52-63e51660b59f>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mW\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m80\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mj\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mk\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mW\u001b[0m\u001b[1;33m/\u001b[0m\u001b[0mmin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mw\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\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[0m\u001b[0;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mj\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mk\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mTypeError\u001b[0m: cannot unpack non-iterable int object"
]
}
],
"source": [
"W=80\n",
"for i,j,k in range(0, int(W/min(w))):\n",
" print(i,j,k)"
]
}
],
"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"
}
},
"nbformat": 4,
"nbformat_minor": 2
}