{ "cells": [ { "cell_type": "markdown", "metadata": { "nbpages": { "level": 0, "link": "[](https://jckantor.github.io/CBE30338/06.05-Linear-Programming-in-Pyomo.html)", "section": "" } }, "source": [ "\n", "*This notebook contains material from [CBE30338](https://jckantor.github.io/CBE30338);\n", "content is available [on Github](https://github.com/jckantor/CBE30338.git).*\n" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 0, "link": "[](https://jckantor.github.io/CBE30338/06.05-Linear-Programming-in-Pyomo.html)", "section": "" } }, "source": [ "\n", "< [6.4 Linear Production Model in Pyomo](https://jckantor.github.io/CBE30338/06.04-Linear-Production-Model-in-Pyomo.html) | [Contents](toc.html) | [Tag Index](tag_index.html) | [6.6 Linear Blending Problem](https://jckantor.github.io/CBE30338/06.06-Linear-Blending-Problem.html) >
"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 1,
"link": "[6.5 Linear Programming in Pyomo](https://jckantor.github.io/CBE30338/06.05-Linear-Programming-in-Pyomo.html#6.5-Linear-Programming-in-Pyomo)",
"section": "6.5 Linear Programming in Pyomo"
}
},
"source": [
"# 6.5 Linear Programming in Pyomo\n",
"\n",
"Pyomo is an example of a recent generation of modeling languages that are fully integrated with an underlying scripting language. \n",
"\n",
"The purpose of this tutorial is to introduce the basics of constructing Pyomo models. In this first example we consider the production of two products, 'A' and 'B', subject to constraints. The objective is to maximize profit\n",
"\n",
"\\begin{align}\n",
"\\max_{x,y} \\mbox{profit} = 40 x + 30 y\n",
"\\end{align}\n",
"subject to\n",
"\\begin{align}\n",
"x & \\leq 40 \\\\\n",
"x + 2y & \\leq 80 \\\\\n",
"x + y & \\leq 100\n",
"\\end{align}\n",
"\n",
"where $x$ refers the production of 'A', and $y$ to the production of 'B'."
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[6.5.1 Starting the Model](https://jckantor.github.io/CBE30338/06.05-Linear-Programming-in-Pyomo.html#6.5.1-Starting-the-Model)",
"section": "6.5.1 Starting the Model"
}
},
"source": [
"## 6.5.1 Starting the Model\n",
"\n",
"At the highest level, Pyomo employs two objects -- a model and a solver -- to generate a resulting solution. "
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {
"nbpages": {
"level": 2,
"link": "[6.5.1 Starting the Model](https://jckantor.github.io/CBE30338/06.05-Linear-Programming-in-Pyomo.html#6.5.1-Starting-the-Model)",
"section": "6.5.1 Starting the Model"
}
},
"outputs": [],
"source": [
"from pyomo.environ import *\n",
"\n",
"model = ConcreteModel()\n",
"\n",
"solver = SolverFactory('glpk')"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"nbpages": {
"level": 2,
"link": "[6.5.1 Starting the Model](https://jckantor.github.io/CBE30338/06.05-Linear-Programming-in-Pyomo.html#6.5.1-Starting-the-Model)",
"section": "6.5.1 Starting the Model"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Product A 0.0\n",
"Product B 80.0\n"
]
}
],
"source": [
"from pyomo.environ import *\n",
"\n",
"# create a model\n",
"model = ConcreteModel()\n",
"\n",
"# index for x\n",
"idx = ['Product A', 'Product B']\n",
"\n",
"# create decision variables\n",
"model.x = Var(idx, domain=NonNegativeIntegers)\n",
"\n",
"# create objective\n",
"model.maxprofit = Objective(expr = 40*model.x['Product A'] + 30*model.x['Product B'], sense=maximize)\n",
"\n",
"# create constraints\n",
"model.constraints = ConstraintList()\n",
"model.constraints.add(model.x['Product A'] <= 40)\n",
"model.constraints.add(model.x['Product A'] + model.x['Product B'] <= 80.5)\n",
"model.constraints.add(model.x['Product A'] + model.x['Product B'] <= 100)\n",
"\n",
"solver = SolverFactory('glpk')\n",
"\n",
"results = solver.solve(model)\n",
"\n",
"for i in idx:\n",
" print(i, model.x[i]())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 2,
"link": "[6.5.1 Starting the Model](https://jckantor.github.io/CBE30338/06.05-Linear-Programming-in-Pyomo.html#6.5.1-Starting-the-Model)",
"section": "6.5.1 Starting the Model"
}
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"nbpages": {
"level": 2,
"link": "[6.5.1 Starting the Model](https://jckantor.github.io/CBE30338/06.05-Linear-Programming-in-Pyomo.html#6.5.1-Starting-the-Model)",
"section": "6.5.1 Starting the Model"
}
},
"outputs": [
{
"data": {
"text/plain": [
"{'dime': 10, 'half-dollar': 50, 'nickel': 5, 'penny': 1, 'quarter': 25}"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = {'penny':1, 'nickel':5, 'dime':10, 'quarter':25, 'half-dollar':50}\n",
"data"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"nbpages": {
"level": 2,
"link": "[6.5.1 Starting the Model](https://jckantor.github.io/CBE30338/06.05-Linear-Programming-in-Pyomo.html#6.5.1-Starting-the-Model)",
"section": "6.5.1 Starting the Model"
}
},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['penny', 'nickel', 'dime', 'quarter'])"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.keys()"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"nbpages": {
"level": 2,
"link": "[6.5.1 Starting the Model](https://jckantor.github.io/CBE30338/06.05-Linear-Programming-in-Pyomo.html#6.5.1-Starting-the-Model)",
"section": "6.5.1 Starting the Model"
}
},
"outputs": [
{
"data": {
"text/plain": [
"dict_values([1, 5, 10, 25])"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.values()"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"nbpages": {
"level": 2,
"link": "[6.5.1 Starting the Model](https://jckantor.github.io/CBE30338/06.05-Linear-Programming-in-Pyomo.html#6.5.1-Starting-the-Model)",
"section": "6.5.1 Starting the Model"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 Set Declarations\n",
" x_index : Dim=0, Dimen=1, Size=5, Domain=None, Ordered=False, Bounds=None\n",
" ['dime', 'half-dollar', 'nickel', 'penny', 'quarter']\n",
"\n",
"1 Var Declarations\n",
" x : Size=5, Index=x_index\n",
" Key : Lower : Value : Upper : Fixed : Stale : Domain\n",
" dime : 0 : 0.0 : None : False : False : NonNegativeIntegers\n",
" half-dollar : 0 : 1.0 : None : False : False : NonNegativeIntegers\n",
" nickel : 0 : 1.0 : None : False : False : NonNegativeIntegers\n",
" penny : 0 : 3.0 : None : False : False : NonNegativeIntegers\n",
" quarter : 0 : 1.0 : None : False : False : NonNegativeIntegers\n",
"\n",
"1 Objective Declarations\n",
" ncoins : Size=1, Index=None, Active=True\n",
" Key : Active : Sense : Expression\n",
" None : True : minimize : x[penny] + x[nickel] + x[dime] + x[quarter] + x[half-dollar]\n",
"\n",
"1 Constraint Declarations\n",
" cons : Size=1, Index=None, Active=True\n",
" Key : Lower : Body : Upper : Active\n",
" None : 83.0 : x[penny] + 5*x[nickel] + 10*x[dime] + 25*x[quarter] + 50*x[half-dollar] : 83.0 : True\n",
"\n",
"4 Declarations: x_index x ncoins cons\n"
]
}
],
"source": [
"model = ConcreteModel()\n",
"\n",
"model.x = Var(data.keys(), domain=NonNegativeIntegers)\n",
"\n",
"model.ncoins = Objective(expr = sum(model.x[c] for c in data.keys()), sense=minimize)\n",
"\n",
"model.cons = Constraint(expr = 83 == sum(data[c]*model.x[c] for c in data.keys()))\n",
"\n",
"solver = SolverFactory('glpk')\n",
"solver.solve(model)\n",
"model.pprint()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 2,
"link": "[6.5.1 Starting the Model](https://jckantor.github.io/CBE30338/06.05-Linear-Programming-in-Pyomo.html#6.5.1-Starting-the-Model)",
"section": "6.5.1 Starting the Model"
}
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[6.5.1 Starting the Model](https://jckantor.github.io/CBE30338/06.05-Linear-Programming-in-Pyomo.html#6.5.1-Starting-the-Model)",
"section": "6.5.1 Starting the Model"
}
},
"source": [
"\n",
"< [6.4 Linear Production Model in Pyomo](https://jckantor.github.io/CBE30338/06.04-Linear-Production-Model-in-Pyomo.html) | [Contents](toc.html) | [Tag Index](tag_index.html) | [6.6 Linear Blending Problem](https://jckantor.github.io/CBE30338/06.06-Linear-Blending-Problem.html) >
"
]
}
],
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}