Skip to content

Commit

Permalink
Fix extra notebooks from chapter 3 + LP relaxation notation convention
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrozocca committed Oct 19, 2023
1 parent 926d37e commit 3dd2cec
Show file tree
Hide file tree
Showing 8 changed files with 606 additions and 827 deletions.
2 changes: 1 addition & 1 deletion notebooks/02/bim-rawmaterialplanning.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@
"\n",
"where $(\\Omega_p)_{p \\in P}$ is the vector with the desired end inventories.\n",
"\n",
"Here is the Pyomo implementation of this LP."
"Here is the Pyomo implementation of this linear problem."
]
},
{
Expand Down
6 changes: 3 additions & 3 deletions notebooks/03/bim-production-revisited.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
" \n",
"else:\n",
" from pyomo.environ import SolverFactory\n",
" SOLVER = SolverFactory('appsi_highs')\n",
" SOLVER = SolverFactory('cbc')\n",
"\n",
"assert SOLVER.available(), f\"Solver {SOLVER} is not available.\""
]
Expand Down Expand Up @@ -574,7 +574,7 @@
" unitary_products,\n",
" unitary_holding_costs,\n",
"):\n",
" m = pyo.ConcreteModel(\"Product acquisition and inventory with sophisticated prices\")\n",
" m = pyo.ConcreteModel(\"BIM product acquisition and inventory problem\")\n",
"\n",
" periods = demand.columns\n",
" products = demand.index\n",
Expand Down Expand Up @@ -2829,7 +2829,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.2"
"version": "3.10.10"
}
},
"nbformat": 4,
Expand Down
38 changes: 23 additions & 15 deletions notebooks/03/cryptarithms.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@
"\n",
"There are several possible approaches to modeling this puzzle in Pyomo. \n",
"\n",
"[One approach](https://stackoverflow.com/questions/67456379/pyomo-model-constraint-programming-for-sendmore-money-task) would be to using a matrix of binary variables $x_{a,d}$ indexed by letter $a$ and digit $d$ such that $x_{a,d} = 1$ designates the corresponding assignment. The problem constraints can then be implemented by summing the binary variables along the two axes. The arithmetic constraint becomes a more challenging.\n",
"[One approach](https://stackoverflow.com/questions/67456379/pyomo-model-constraint-programming-for-sendmore-money-task) would be to using a matrix of binary variables $x_{a,d}$ indexed by letter $a$ and digit $d$ such that $x_{a,d} = 1$ designates the corresponding assignment. The problem constraints can then be implemented by summing the binary variables along the two axes. The arithmetic constraint becomes a more challenging.\n",
"\n",
"[Another approach](https://www.gecode.org/doc/6.0.1/MPG.pdf) is to use Pyomo integer variables indexed by letters, then setup an linear expression to represent the puzzle. If we use the notation $n_a$ to represent the digit assigned to letter $a$, the algebraic constraint becomes\n",
"[Another approach](https://www.gecode.org/doc/6.0.1/MPG.pdf) is to use Pyomo integer variables indexed by letters, then set up a linear expression to represent the puzzle. If we use the notation $n_a$ to represent the digit assigned to letter $a$, the algebraic constraint becomes\n",
"\n",
"$$\n",
"\\begin{align*}\n",
Expand All @@ -97,7 +97,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 11,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
Expand All @@ -121,6 +121,17 @@
"name": "stdout",
"output_type": "stream",
"text": [
"The solution of the puzzle is:\n",
"S = 9\n",
"E = 5\n",
"N = 6\n",
"D = 7\n",
"M = 1\n",
"O = 0\n",
"R = 8\n",
"Y = 2\n",
"\n",
"\n",
" 9 5 6 7\n",
" + 1 0 8 5\n",
" ----------\n",
Expand All @@ -132,7 +143,7 @@
"import pyomo.environ as pyo\n",
"import pyomo.gdp as gdp\n",
"\n",
"m = pyo.ConcreteModel()\n",
"m = pyo.ConcreteModel(\"Cryptarithms Problem\")\n",
"\n",
"m.LETTERS = pyo.Set(initialize=[\"S\", \"E\", \"N\", \"D\", \"M\", \"O\", \"R\", \"Y\"])\n",
"m.PAIRS = pyo.Set(initialize=m.LETTERS * m.LETTERS, filter=lambda m, a, b: a < b)\n",
Expand Down Expand Up @@ -170,21 +181,25 @@
" return [m.n[a] >= m.n[b] + 1, m.n[b] >= m.n[a] + 1]\n",
"\n",
"\n",
"# assign a \"dummy\" objective to avoid solver errors\n",
"# assign a \"dummy\" constant objective to avoid solver errors\n",
"@m.Objective()\n",
"def dummy_objective(m):\n",
" return m.n[\"M\"]\n",
" return 0\n",
"\n",
"\n",
"pyo.TransformationFactory(\"gdp.bigm\").apply_to(m)\n",
"SOLVER.solve(m)\n",
"\n",
"print(\"The solution of the puzzle is:\")\n",
"for l in m.LETTERS:\n",
" print(f\"{l} = {int(m.n[l]())}\")\n",
"\n",
"\n",
"def letters2num(s):\n",
" return \" \".join(map(lambda s: f\"{int(m.n[s]())}\", list(s)))\n",
"\n",
"\n",
"print(\" \", letters2num(\"SEND\"))\n",
"print(\"\\n\\n \", letters2num(\"SEND\"))\n",
"print(\" + \", letters2num(\"MORE\"))\n",
"print(\" ----------\")\n",
"print(\"= \", letters2num(\"MONEY\"))"
Expand All @@ -202,13 +217,6 @@
"\n",
"2. There are [many more examples](http://cryptarithms.awardspace.us/puzzles.html) of cryptarithm puzzles. Refactor this code and create a function that can be used to solve generic puzzles of this type."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -233,7 +241,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.16"
"version": "3.10.10"
}
},
"nbformat": 4,
Expand Down
171 changes: 73 additions & 98 deletions notebooks/03/maintenance-planning.ipynb

Large diffs are not rendered by default.

Loading

0 comments on commit 3dd2cec

Please sign in to comment.