{
"metadata": {
"name": "",
"signature": "sha256:cbcf518d8f253bda46bf0fb8f33e3bd26de23bbdd06f00f11f7c62278d8128d5"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"%pylab inline\n",
"from gpkit import GP, VectorVariable, Variable\n",
"import gpkit.interactive\n",
"gpkit.interactive.init_printing()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Populating the interactive namespace from numpy and matplotlib\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Optimizing Maintenance Schedules with GPkit"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"L = Variable(\"L\", 0.1, \"years\", \"system lifetime\")\n",
"\n",
"Rmin = 0.997 # minimum system reliability\n",
"mlogRmin = Variable(\"\\\\mathrm{log}(\\\\tfrac{1}{R_{min}})\",\n",
" log(1/Rmin), \"-\", \"log of inverse of system reliability\")\n",
"\n",
"n = 3 # number of parts\n",
"\n",
"c = VectorVariable(n, \"c\", [1, 2, 2], \"-\", \"cost to replace each part, in USD\")\n",
"\n",
"q = gpkit.VectorVariable(n, \"q\", \"1/years\", \"log of inverse of part reliabilities\")\n",
"L10 = VectorVariable(n, \"L_{10}\", [1, 3, 1], \"years\", \"L10 lifetimes\")\n",
"k = array([1.5, 1.5, 2]) # part shape factors; 1.5 is what's used by ISO-281\n",
"lam = 4.4828*L10 # Weibull scale factors\n",
"\n",
"x = gpkit.VectorVariable(n, \"x\", \"1/years\", \"maintenance rate\")"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"gp = gpkit.GP(L*(c*x).sum(),\n",
" [q == x*(lam*x)**-k,\n",
" q.sum() <= mlogRmin/L,\n",
" L*x >= 1])\n",
"gp"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$\\begin{array}[ll]\n",
"\\text{}\n",
"\\text{minimize}\n",
" & {c}_{0} {x}_{0} L + {x}_{1} {c}_{1} L + {x}_{2} {c}_{2} L\\mathrm{\\left[ - \\right]} \\\\\n",
"\\text{subject to}\n",
" & {q}_{0} = \\frac{0.1054}{{L_{10}}_{0}^{1.5} {x}_{0}^{0.5}} \\\\\n",
" & {q}_{1} = \\frac{0.1054}{{x}_{1}^{0.5} {L_{10}}_{1}^{1.5}} \\\\\n",
" & {q}_{2} = \\frac{0.04976}{{x}_{2} {L_{10}}_{2}^{2}} \\\\\n",
" & \\frac{\\mathrm{log}(\\tfrac{1}{R_{min}})}{L} \\geq {q}_{0} + {q}_{1} + {q}_{2} \\\\\n",
" & 1 \\leq {x}_{0} L \\\\\n",
" & 1 \\leq {x}_{1} L \\\\\n",
" & 1 \\leq {x}_{2} L \\\\\n",
"\\text{substituting}\n",
" & L = 0.1 \\\\\n",
" & \\mathrm{log}(\\tfrac{1}{R_{min}}) = 0.003005 \\\\\n",
" & {L_{10}}_{0} = 1 \\\\\n",
" & {L_{10}}_{1} = 3 \\\\\n",
" & {L_{10}}_{2} = 1 \\\\\n",
" & {c}_{0} = 1 \\\\\n",
" & {c}_{1} = 2 \\\\\n",
" & {c}_{2} = 2 \\\\\n",
"\\end{array}$$"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 3,
"text": [
" gpkit.GP( # minimize \n",
" c_0*x_0*L + x_1*c_1*L + x_2*c_2*L, units='-', \n",
" [ # subject to \n",
" q_0 == 0.11*L_{10}_0**-1.5*x_0**-0.5, \n",
" q_1 == 0.11*x_1**-0.5*L_{10}_1**-1.5, \n",
" q_2 == 0.05*x_2**-1*L_{10}_2**-2, \n",
" \\mathrm{log}(\\tfrac{1}{R_{min}})*L**-1 >= q_0 + q_1 + q_2, \n",
" 1 <= x_0*L, \n",
" 1 <= x_1*L, \n",
" 1 <= x_2*L, \n",
" ], \n",
" substitutions={ \\mathrm{log}(\\tfrac{1}{R_{min}}): 0.0030045090202987\n",
" c_0: 1, \n",
" c_1: 2, \n",
" c_2: 2, \n",
" L_{10}_0: 1, \n",
" L_{10}_1: 3, \n",
" L_{10}_2: 1, \n",
" L: 0.1 }, \n",
" solver=\"cvxopt\") \n",
"\n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
"222,\n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" "
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"sol = gp.solve()\n",
"print \n",
"print \"The optimal schedule, over the lifetime of %.2g years\" % sol(L)\n",
"print \" is to buy each part [ %s ] times\" % \" \".join([\"%.2g\" % xi for xi in sol(L*x)])\n",
"print \" at a total cost of $%.3g\" % sol[\"cost\"]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Using solver 'cvxopt'\n",
"Solving for 6 variables.\n",
"Solving took 0.0813 seconds."
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
"The optimal schedule, over the lifetime of 0.1 years\n",
" is to buy each part [ 3.2 1 1 ] times\n",
" at a total cost of $7.19\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"sol[\"local_model\"].flatten()[0]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"latex": [
"$$0.3675\\frac{{c}_{0}^{0.44} {c}_{1}^{0.28} {c}_{2}^{0.28} L^{2.3}}{\\mathrm{log}(\\tfrac{1}{R_{min}})^{1.4} {L_{10}}_{0}^{1.3} {L_{10}}_{1}^{0.46} {L_{10}}_{2}^{0.47}}$$"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 5,
"text": [
"0.37*\\mathrm{log}(\\tfrac{1}{R_{min}})**-1.4*c_0**0.44*c_1**0.28*c_2**0.28*L_{1\n",
"0}_0**-1.3*L_{10}_1**-0.46*L_{10}_2**-0.47*L**2.3"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython import utils\n",
"from IPython.core.display import HTML\n",
"import os\n",
"def css_styling():\n",
" \"\"\"Load default custom.css file from ipython profile\"\"\"\n",
" base = utils.path.get_ipython_dir()\n",
" styles = (\"\" % \n",
" open(os.path.join(base,'profile_default/static/custom/custom.css'),'r').read())\n",
" return HTML(styles)\n",
"css_styling()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
""
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 6,
"text": [
""
]
}
],
"prompt_number": 6
}
],
"metadata": {}
}
]
}