Skip to content

Commit

Permalink
Fix preambles, imports, and positioning of the solve() command
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrozocca committed Nov 30, 2023
1 parent 9926d67 commit c076df9
Show file tree
Hide file tree
Showing 32 changed files with 524 additions and 689 deletions.
12 changes: 3 additions & 9 deletions notebooks/01/01-production-planning.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,8 @@
"```\n",
"```{index} single: Pyomo; decorators\n",
"```\n",
"```{index} single: solver; cbc\n",
"```\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```{index} single: solver; HiGHS\n",
"```\n",
"# 1.1 A first production planning problem"
]
},
Expand Down Expand Up @@ -172,7 +166,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.2"
"version": "3.10.10"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
Expand Down
38 changes: 19 additions & 19 deletions notebooks/01/02-production-planning-basic.ipynb

Large diffs are not rendered by default.

10 changes: 4 additions & 6 deletions notebooks/01/03-production-planning-advanced.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
"```\n",
"```{index} single: Pyomo; decorators\n",
"```\n",
"```{index} single: solver; cbc\n",
"```{index} single: Pyomo; parameters\n",
"```\n",
"```{index} single: solver; HiGHS\n",
"```\n",
"\n",
"# 1.3 A data-driven Pyomo Model\n",
Expand Down Expand Up @@ -63,7 +65,7 @@
" \n",
"import pyomo.environ as pyo\n",
"SOLVER = pyo.SolverFactory(solver)\n",
" \n",
"\n",
"assert SOLVER.available(), f\"Solver {solver} is not available.\""
]
},
Expand Down Expand Up @@ -543,8 +545,6 @@
" labor B : -Inf : 2*y[U] + y[V] - x[labor B] : 0.0 : True\n",
"\n",
"12 Declarations: PRODUCTS RESOURCES demand available cp cr a_index a x y profit materials_used\n",
"Running HiGHS 1.5.3 [date: 2023-05-16, git hash: 594fa5a9d]\n",
"Copyright (c) 2023 HiGHS under MIT licence terms\n",
"Profit = 2600.0\n",
"\n",
"Production Report\n",
Expand Down Expand Up @@ -594,8 +594,6 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Running HiGHS 1.5.3 [date: 2023-05-16, git hash: 594fa5a9d]\n",
"Copyright (c) 2023 HiGHS under MIT licence terms\n",
"Profit = 2600.0\n",
"\n",
"Production Report\n",
Expand Down
30 changes: 18 additions & 12 deletions notebooks/02/01-bim.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@
"source": [
"```{index} dual problem\n",
"```\n",
"```{index} single: solver; cbc\n",
"```\n",
"```{index} single: solver; highs\n",
"```{index} single: solver; HiGHS\n",
"```\n",
"```{index} single: application; production planning\n",
"```\n",
"```{index} single: Pyomo; sets \n",
"```\n",
"```{index} single: Pyomo; decorators \n",
"```\n",
"```{index} single: numpy\n",
"```\n",
"```{index} single: Pyomo; ConstraintList \n",
"```\n",
"\n",
"# 2.1 BIM production"
"# 2.1 BIM production planning using linear optimization"
]
},
{
Expand Down Expand Up @@ -251,9 +257,7 @@
}
],
"source": [
"import pyomo.environ as pyo\n",
"\n",
"model = pyo.ConcreteModel(\"BIM\")\n",
"model = pyo.ConcreteModel(\"BIM production planning\")\n",
"\n",
"# Decision variables and their domains\n",
"model.x1 = pyo.Var(domain=pyo.NonNegativeReals)\n",
Expand Down Expand Up @@ -297,7 +301,7 @@
}
],
"source": [
"model = pyo.ConcreteModel(\"BIM with decorators\")\n",
"model = pyo.ConcreteModel(\"BIM BIM production planning with decorators\")\n",
"\n",
"# Decision variables and their domains\n",
"model.x1 = pyo.Var(domain=pyo.NonNegativeReals)\n",
Expand Down Expand Up @@ -332,7 +336,7 @@
"\n",
"\n",
"# Solve and print solution\n",
"SOLVER.solve(model, tee=False)\n",
"SOLVER.solve(model)\n",
"print(f\"x = ({model.x1.value:.1f}, {model.x2.value:.1f})\")\n",
"print(f\"optimal value = {pyo.value(model.profit):.1f}\")"
]
Expand Down Expand Up @@ -464,7 +468,7 @@
"source": [
"import numpy as np\n",
"\n",
"model = pyo.ConcreteModel(\"BIM in matrix form\")\n",
"model = pyo.ConcreteModel(\"BIM production planning in matrix form\")\n",
"\n",
"# Define the number of variables and constraints\n",
"n_vars = 2\n",
Expand Down Expand Up @@ -519,7 +523,9 @@
}
],
"source": [
"model = pyo.ConcreteModel(\"BIM in matrix form using decorators and index sets\")\n",
"model = pyo.ConcreteModel(\n",
" \"BIM production planning in matrix form using decorators and index sets\"\n",
")\n",
"\n",
"# Define the number of variables and constraints and two corresponding index sets\n",
"n_vars = 2\n",
Expand Down Expand Up @@ -572,7 +578,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.2"
"version": "3.10.10"
}
},
"nbformat": 4,
Expand Down
22 changes: 12 additions & 10 deletions notebooks/02/02-lad-regression.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
"tags": []
},
"source": [
"```{index} single: application; regression\n",
"```\n",
"```{index} single: solver; HiGHS\n",
"```\n",
"```{index} single: Pyomo; RangeSet\n",
"```\n",
"```{index} single: Pyomo; decorators\n",
"```\n",
"```{index} single: sklearn\n",
"```\n",
"```{index} single: application; regression\n",
"```\n",
"\n",
Expand Down Expand Up @@ -76,8 +86,9 @@
},
"outputs": [],
"source": [
"from sklearn.datasets import make_regression\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"from sklearn.datasets import make_regression\n",
"\n",
"n_features = 1\n",
"n_samples = 1000\n",
Expand Down Expand Up @@ -121,10 +132,6 @@
}
],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"plt.rcParams[\"font.size\"] = 14\n",
"\n",
"if n_features == 1:\n",
" fig, ax = plt.subplots()\n",
" ax.scatter(X, y, alpha=0.7, color=plt.get_cmap(\"coolwarm\")(0))\n",
Expand Down Expand Up @@ -208,9 +215,6 @@
}
],
"source": [
"import pyomo.environ as pyo\n",
"\n",
"\n",
"def lad_regression(X, y):\n",
" model = pyo.ConcreteModel(\"LAD Regression\")\n",
"\n",
Expand Down Expand Up @@ -296,8 +300,6 @@
"source": [
"y_fit = np.array([sum(x[j] * model.m[j]() for j in model.J) + model.b() for x in X])\n",
"\n",
"plt.rcParams[\"font.size\"] = 14\n",
"\n",
"if n_features == 1:\n",
" fig, ax = plt.subplots()\n",
" ax.scatter(X, y, alpha=0.7, color=plt.get_cmap(\"coolwarm\")(0), label=\"data\")\n",
Expand Down
6 changes: 3 additions & 3 deletions notebooks/02/03-mad-portfolio-optimization.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
"```\n",
"```{index} single: application; investment\n",
"```\n",
"```{index} single: solver; cbc\n",
"```\n",
"```{index} single: solver; highs\n",
"```\n",
"```{index} pandas dataframe\n",
"```{index} single: pandas\n",
"```\n",
"```{index} web scraping\n",
"```\n",
"```{index} yfinance\n",
"```\n",
"\n",
"# 2.3 Mean Absolute Deviation (MAD) portfolio optimization\n",
"\n",
Expand Down
12 changes: 6 additions & 6 deletions notebooks/02/04-bim-dual.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
"source": [
"```{index} dual problem\n",
"```\n",
"```{index} single: solver; cbc\n",
"```{index} single: solver; HiGHS\n",
"```\n",
"```{index} single: application; production planning\n",
"```\n",
"```{index} single: Pyomo; Suffix\n",
"```\n",
"\n",
"# 2.4 Dual of the BIM production problem"
]
Expand Down Expand Up @@ -50,7 +52,7 @@
" \n",
"import pyomo.environ as pyo\n",
"SOLVER = pyo.SolverFactory(solver)\n",
" \n",
"\n",
"assert SOLVER.available(), f\"Solver {solver} is not available.\""
]
},
Expand Down Expand Up @@ -139,9 +141,7 @@
}
],
"source": [
"import pyomo.environ as pyo\n",
"\n",
"model = pyo.ConcreteModel(\"BIM dual\")\n",
"model = pyo.ConcreteModel(\"BIM production planning dual problem\")\n",
"\n",
"# Decision variables and their domain\n",
"model.y1 = pyo.Var(domain=pyo.NonNegativeReals)\n",
Expand Down Expand Up @@ -209,7 +209,7 @@
}
],
"source": [
"model = pyo.ConcreteModel(\"BIM with decorators\")\n",
"model = pyo.ConcreteModel(\"BIM production planning with decorators\")\n",
"\n",
"# Decision variables and their domains\n",
"model.x1 = pyo.Var(domain=pyo.NonNegativeReals)\n",
Expand Down
16 changes: 7 additions & 9 deletions notebooks/02/05-bim-maxmin.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"```{index} single: solver; cbc\n",
"```{index} single: solver; HiGHS\n",
"```\n",
"```{index} single: solver; highs\n",
"```{index} single: Pyomo; ConstraintList\n",
"```\n",
"```{index} minmax objective\n",
"```\n",
"\n",
"# 2.5 BIM production for worst case"
Expand Down Expand Up @@ -43,7 +45,7 @@
" \n",
"import pyomo.environ as pyo\n",
"SOLVER = pyo.SolverFactory(solver)\n",
" \n",
"\n",
"assert SOLVER.available(), f\"Solver {solver} is not available.\""
]
},
Expand All @@ -53,7 +55,7 @@
"source": [
"## Minmax objective function\n",
"\n",
"Another class of seemingly complicated objective functions that can be easily rewritten as an LP are those stated as maxima over several linear functions. Given a finite set of indices $K$ and a collection of vectors $\\{c_k\\}_{k \\in K}$, the minimax problem given by\n",
"Another class of seemingly complicated objective functions that can be easily rewritten as an LO problem are those stated as maxima over several linear functions. Given a finite set of indices $K$ and a collection of vectors $\\{c_k\\}_{k \\in K}$, the minimax problem given by\n",
"\n",
"$$\n",
"\\begin{align}\n",
Expand Down Expand Up @@ -99,11 +101,8 @@
}
],
"source": [
"import pyomo.environ as pyo\n",
"\n",
"\n",
"def BIM_maxmin(costs):\n",
" m = pyo.ConcreteModel(\"BIM with maxmin objective\")\n",
" m = pyo.ConcreteModel(\"BIM production planning with maxmin objective\")\n",
"\n",
" m.x1 = pyo.Var(domain=pyo.NonNegativeReals)\n",
" m.x2 = pyo.Var(domain=pyo.NonNegativeReals)\n",
Expand All @@ -124,7 +123,6 @@
"\n",
"\n",
"m = BIM_maxmin([[12, 9], [11, 10], [8, 11]])\n",
"\n",
"SOLVER.solve(m)\n",
"\n",
"print(f\"x = ({pyo.value(m.x1):.1f}, {pyo.value(m.x2):.1f})\")\n",
Expand Down
8 changes: 5 additions & 3 deletions notebooks/02/06-bim-fractional.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"```{index} single: solver; cbc\n",
"```{index} single: solver; HiGHS\n",
"```\n",
"```{index} single: solver; highs\n",
"```{index} single: Pyomo; ConstraintList\n",
"```\n",
"```{index} single: Pyomo; Expression\n",
"```\n",
"\n",
"# 2.6 BIM production variants"
Expand Down Expand Up @@ -43,7 +45,7 @@
" \n",
"import pyomo.environ as pyo\n",
"SOLVER = pyo.SolverFactory(solver)\n",
" \n",
"\n",
"assert SOLVER.available(), f\"Solver {solver} is not available.\""
]
},
Expand Down
Loading

0 comments on commit c076df9

Please sign in to comment.