|
| 1 | +### A Pluto.jl notebook ### |
| 2 | +# v0.20.13 |
| 3 | + |
| 4 | +using Markdown |
| 5 | +using InteractiveUtils |
| 6 | + |
| 7 | +# ╔═╡ 881eed45-e7f0-4785-bde8-530e378d7050 |
| 8 | +begin |
| 9 | +using Pkg; Pkg.activate("..") |
| 10 | +end |
| 11 | + |
| 12 | +# ╔═╡ 9f5675a3-07df-4fb1-b683-4c5fd2a85002 |
| 13 | +begin |
| 14 | + using PlutoUI |
| 15 | + using Random |
| 16 | + using LinearAlgebra |
| 17 | + using HypertextLiteral |
| 18 | + using PlutoTeachingTools |
| 19 | + using ShortCodes, MarkdownLiteral |
| 20 | + using Random |
| 21 | + using Plots |
| 22 | + Random.seed!(8803) |
| 23 | +end |
| 24 | + |
| 25 | +# ╔═╡ 9ce52307-bc22-4f66-a4af-a4e4ac382212 |
| 26 | +begin |
| 27 | + using JuMP |
| 28 | + using HiGHS # Solver for LPs and MILPs |
| 29 | + using Ipopt # Solver for NLPs |
| 30 | + using Test # For quick validation checks |
| 31 | + # const MOI = JuMP.MathOptInterface |
| 32 | +end |
| 33 | + |
| 34 | +# ╔═╡ 0df8b65a-0527-4545-bf11-00e9912bced0 |
| 35 | +md""" |
| 36 | +# Background – Modeling Optimization Problems in JuMP 🏗️ |
| 37 | +
|
| 38 | +This short Pluto notebook walks you through three small optimisation models of increasing |
| 39 | +difficulty: |
| 40 | +
|
| 41 | +1. **Linear program (LP)** |
| 42 | +2. **Mixed‑integer linear program (MILP)** |
| 43 | +3. **Non‑linear program (NLP)** – a taste of what shows up constantly in **optimal‑control** and simulaing **non‑linear systems**. |
| 44 | +
|
| 45 | +For every task you will: |
| 46 | +
|
| 47 | +* Write down the mathematical formulation. |
| 48 | +* Translate it into a JuMP model. |
| 49 | +* Solve it. |
| 50 | +* Run the provided `@testset` to make sure your implementation is correct. |
| 51 | + When the tests are green ✅ you can be confident that your model is producing the expected answer. |
| 52 | +""" |
| 53 | + |
| 54 | +# ╔═╡ 6f67ca7c-1391-4cb9-b692-cd818e037587 |
| 55 | +md""" |
| 56 | +--- |
| 57 | +
|
| 58 | +## 1. Linear program – Production planning |
| 59 | +
|
| 60 | +A workshop makes **widgets** \(w\) and **gadgets** \(g\). |
| 61 | +
|
| 62 | +| | Machine‑hours | Labour‑hours | Profit (\$) | |
| 63 | +|------------|---------------|--------------|--------------| |
| 64 | +| Widget (\(w\)) | 2 | 3 | 3 | |
| 65 | +| Gadget (\(g\)) | 4 | 2 | 5 | |
| 66 | +
|
| 67 | +Resources available this week: **100 machine‑hours** and **90 labour‑hours**. |
| 68 | +
|
| 69 | +### 1.1 Your tasks |
| 70 | +1. Write the mathematical model *(maximization)*. |
| 71 | +2. Fill in the JuMP code in the next cell. |
| 72 | +3. Run the tests. |
| 73 | +""" |
| 74 | + |
| 75 | +# ╔═╡ 49042d6c-cf78-46d3-bfee-a8fd7ddf3aa0 |
| 76 | +begin |
| 77 | +# === Your LP model goes below === |
| 78 | +# Replace the contents of this cell with your own model. |
| 79 | +model_lp = Model(HiGHS.Optimizer) |
| 80 | + |
| 81 | +# Suggested variable names |
| 82 | +# @variable(model_lp, w >= 0) |
| 83 | +# @variable(model_lp, g >= 0) |
| 84 | + |
| 85 | +# --- YOUR CODE HERE --- |
| 86 | + |
| 87 | +# optimize!(model_lp) |
| 88 | +end |
| 89 | + |
| 90 | +# ╔═╡ 6fb672d0-5a18-4ccc-b7b3-184839c2401b |
| 91 | +begin |
| 92 | +# === Quick check === |
| 93 | +@testset "LP check" begin |
| 94 | + @test termination_status(model_lp) == MOI.OPTIMAL |
| 95 | + @test isapprox(objective_value(model_lp), 135.0; atol = 1e-3) |
| 96 | +end |
| 97 | +end |
| 98 | + |
| 99 | +# ╔═╡ 808c505d-e10d-42e3-9fb1-9c6f384b2c3c |
| 100 | +md""" |
| 101 | +--- |
| 102 | +
|
| 103 | +## 2. MILP – 0‑1 Knapsack |
| 104 | +
|
| 105 | +You have a backpack that can carry at most **10 kg**. |
| 106 | +There are three items: |
| 107 | +
|
| 108 | +| Item | Value | Weight | |
| 109 | +|------|-------|--------| |
| 110 | +| 1 | 10 | 4 | |
| 111 | +| 2 | 7 | 3 | |
| 112 | +| 3 | 5 | 2 | |
| 113 | +
|
| 114 | +### 2.1 Your tasks |
| 115 | +1. Write the mathematical model with **binary** decision variables \(x_i \in \{0,1\}\). |
| 116 | +2. Complete the JuMP model and solve it. |
| 117 | +3. Pass the tests. |
| 118 | +""" |
| 119 | + |
| 120 | +# ╔═╡ 39617561-bbbf-4ef6-91e2-358dfe76581c |
| 121 | +begin |
| 122 | +# === Your MILP model goes below === |
| 123 | +# Replace the contents of this cell with your own model. |
| 124 | +model_milp = Model(HiGHS.Optimizer) |
| 125 | + |
| 126 | +# Example: |
| 127 | +# @variable(model_milp, x[1:3], Bin) |
| 128 | + |
| 129 | +# --- YOUR CODE HERE --- |
| 130 | + |
| 131 | +# optimize!(model_milp) |
| 132 | +end |
| 133 | + |
| 134 | +# ╔═╡ 01367096-3971-4e79-ace2-83600672fbde |
| 135 | +begin |
| 136 | +# === Quick check === |
| 137 | +@testset "MILP check" begin |
| 138 | + @test termination_status(model_milp) == MOI.OPTIMAL |
| 139 | + @test isapprox(objective_value(model_milp), 22.0; atol = 1e-3) |
| 140 | +end |
| 141 | +end |
| 142 | + |
| 143 | +# ╔═╡ 5e3444d0-8333-4f51-9146-d3d9625fe2e9 |
| 144 | +md""" |
| 145 | +--- |
| 146 | +
|
| 147 | +## 3. Non‑linear program – Rosenbrock valley |
| 148 | +
|
| 149 | +Non‑linear models dominate **optimal control** because discretising the differential equations that |
| 150 | +describe a physical system almost always yields a **non‑linear program (NLP)**. |
| 151 | +
|
| 152 | +A classic (and benign) test problem is the **Rosenbrock** function |
| 153 | +
|
| 154 | +\[ |
| 155 | +\min_{x,\,y\in\mathbb R} \; f(x,y)= (1-x)^{2} + 100\,(y - x^{2})^{2}. |
| 156 | +\] |
| 157 | +
|
| 158 | +It has a single global optimum at \((x^{\star},y^{\star}) = (1,1)\) with \(f^{\star}=0\). |
| 159 | +
|
| 160 | +### 3.1 Your tasks |
| 161 | +1. Build and solve the model with **Ipopt**. |
| 162 | +2. Inspect the solution and objective. |
| 163 | +3. Check your work below. |
| 164 | +""" |
| 165 | + |
| 166 | +# ╔═╡ 00728de8-3c36-48c7-8520-4c9f408a7c5f |
| 167 | +begin |
| 168 | +# === Your NLP model goes below === |
| 169 | +# Replace the contents of this cell with your own model. |
| 170 | +model_nlp = Model(Ipopt.Optimizer) |
| 171 | + |
| 172 | +# --- YOUR CODE HERE --- |
| 173 | + |
| 174 | +# optimize!(model_nlp) |
| 175 | +end |
| 176 | + |
| 177 | +# ╔═╡ 254b9a87-17f9-4fea-8b28-0e3873b58fe2 |
| 178 | +begin |
| 179 | +# === Quick check === |
| 180 | +@testset "NLP check" begin |
| 181 | + @test termination_status(model_nlp) == MOI.LOCALLY_SOLVED || termination_status(model_nlp) == MOI.OPTIMAL |
| 182 | + @test isapprox(objective_value(model_nlp), 0.0; atol = 1e-6) |
| 183 | +end |
| 184 | +end |
| 185 | + |
| 186 | +# ╔═╡ 147fe732-fe65-4226-af43-956b33a75bff |
| 187 | +md""" |
| 188 | +--- |
| 189 | +
|
| 190 | +## Why non‑linear models matter in optimal control 🚀 |
| 191 | +
|
| 192 | +When you discretise a continuous‑time optimal‑control problem (for example with **direct collocation**) |
| 193 | +you obtain an optimisation problem whose variables are the states, controls, and possibly parameters |
| 194 | +at many discrete time points: |
| 195 | +
|
| 196 | +```math |
| 197 | +\begin{aligned} |
| 198 | +&\min_{x_{k},u_{k}} && \sum_{k=0}^{N-1} \; \ell(x_{k},u_{k}) \\ |
| 199 | +&\text{s.t.} && x_{k+1} = x_{k} + h\,f(x_{k},u_{k}), \qquad k=0,\dots,N-1, \\ |
| 200 | +& && g(x_{k},u_{k}) \le 0, \\ |
| 201 | +& && x_{0}=x_{\text{init}}, \; x_{N}=x_{\text{goal}}. |
| 202 | +\end{aligned} |
| 203 | +``` |
| 204 | +
|
| 205 | +Even when \(f\) and \(g\) are **polynomial** the resulting constraints are *non‑linear* in the decision variables. |
| 206 | +Hence your optimisation solver must tackle *general NLPs*. |
| 207 | +Getting comfortable with modelling and debugging small nonlinear examples like Rosenbrock will pay off |
| 208 | +when you step up to thousands of variables in real control problems! |
| 209 | +""" |
| 210 | + |
| 211 | +# ╔═╡ Cell order: |
| 212 | +# ╟─881eed45-e7f0-4785-bde8-530e378d7050 |
| 213 | +# ╟─9f5675a3-07df-4fb1-b683-4c5fd2a85002 |
| 214 | +# ╟─0df8b65a-0527-4545-bf11-00e9912bced0 |
| 215 | +# ╠═9ce52307-bc22-4f66-a4af-a4e4ac382212 |
| 216 | +# ╟─6f67ca7c-1391-4cb9-b692-cd818e037587 |
| 217 | +# ╠═49042d6c-cf78-46d3-bfee-a8fd7ddf3aa0 |
| 218 | +# ╠═6fb672d0-5a18-4ccc-b7b3-184839c2401b |
| 219 | +# ╠═808c505d-e10d-42e3-9fb1-9c6f384b2c3c |
| 220 | +# ╠═39617561-bbbf-4ef6-91e2-358dfe76581c |
| 221 | +# ╠═01367096-3971-4e79-ace2-83600672fbde |
| 222 | +# ╠═5e3444d0-8333-4f51-9146-d3d9625fe2e9 |
| 223 | +# ╠═00728de8-3c36-48c7-8520-4c9f408a7c5f |
| 224 | +# ╠═254b9a87-17f9-4fea-8b28-0e3873b58fe2 |
| 225 | +# ╟─147fe732-fe65-4226-af43-956b33a75bff |
0 commit comments