@@ -100,6 +100,162 @@ md"Tests will automatically fetch the optimal values from your solved model."
100100# ╔═╡ 248b398a-0cf5-4c2b-8752-7b9cc4e765d6
101101question_box (md " Did we get partial products?" )
102102
103+ # ╔═╡ 245eb671-84e1-447b-8045-e9eb04966d80
104+ md """
105+ ### 1.2 Other Modeling Tricks
106+ #### Epigraph reformulation for absolute-value expressions
107+ A convex **epigraph** turns a non-linear term into a linear one by treating it
108+ as the upper envelope of an auxiliary variable.
109+
110+ *Goal:* model $u \;\g e\; |x|$ (or set $u=|x|$ in an objective).
111+
112+ **Epigraph form**
113+
114+ ```math
115+ \b egin{aligned}
116+ u &\g e \p hantom{-}x,\\ [-2pt]
117+ u &\g e -x.
118+ \e nd{aligned}
119+ ```
120+
121+ * Both constraints are linear, so $u$ is the **least upper bound** on $|x|$.
122+ * If the objective minimises $u$ (or if $u$ appears on the left-hand side of
123+ other $\l e$ constraints) the optimum forces $u=|x|$ automatically.
124+ * No binaries are required – this is purely continuous and exploits the convex
125+ shape of $|x|$.
126+
127+ ---
128+
129+ """
130+
131+ # ╔═╡ 6a823649-04fa-4322-a028-2fb29dffb08b
132+ md """
133+ **Example** – Weighted-Median Warehouse Location on a Highway
134+
135+ A logistics company must decide where to build a single cross-dock hub along a straight 50-km stretch of highway.
136+ Eight supermarkets lie on the same axis at known kilometre posts $d_i$ and generate a daily demand (truck-loads) $w_i$.
137+
138+ | Supermarket $i$ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
139+ |------------------------------|---|---|---|---|---|---|---|---|
140+ | Location $d_i\, [\t ext{km}]$| 3 | 8 | 15 | 20 | 25 | 29 | 35 | 40 |
141+ | Daily demand $w_i$ | 2 | 1 | 3 | 4 | 2 | 3 | 5 | 1 |
142+
143+ Let $x \i n [0,50]$ be the kilometre post chosen for the hub.
144+ Travel occurs on the highway, so distance is **one-dimensional**.
145+ Devise an optimization problem that minimizes the work done by the truck (distance times weight).
146+
147+ """
148+
149+ # ╔═╡ c369ab46-b416-4c12-83fe-65040a0c47c8
150+ begin
151+ # === Your LP model goes below ===
152+ # Replace the contents of this cell with your own model.
153+ model_lp2 = Model (HiGHS. Optimizer)
154+
155+ # Required variable names (used for testing)
156+ @variable (model_lp2, x_d)
157+
158+ # --- YOUR CODE HERE ---
159+
160+ # optimize!(model_lp2) # uncomment to optimize
161+
162+ # Let's look at our model
163+ println (model_lp2)
164+ end
165+
166+ # ╔═╡ b13f9775-68c2-4646-9b67-c69ee23a4ea0
167+ md """
168+ #### Cutting-plane (outer-approximation) method for nonlinear functions
169+ When a constraint contains a smooth convex function $g(x)\l e 0$, one can
170+ iteratively approximate it by linear cuts that **support** the graph of $g$.
171+
172+ **Idea**
173+
174+ 1. **Relax** the nonlinear constraint and solve the LP/MILP master problem.
175+ 2. **Check** the candidate solution $\b ar x$:
176+ * If $g(\b ar x)\l e0$ it is feasible (and the current best).
177+ * Otherwise, generate a tangent plane that cuts off $\b ar x$ but remains
178+ valid for all feasible points (by convexity).
179+
180+ The gradient $\n abla g(\b ar x)$ gives the supporting hyperplane
181+
182+ ```math
183+ g(\b ar x) + \n abla g(\b ar x)^{\!\t op}(x-\b ar x)\;\l e\; 0 .
184+ ```
185+
186+ 3. **Add** this cut to the master problem and repeat.
187+
188+ Because each cut is a *global* under-estimator of $g$, the feasible region is
189+ shrunk safely from the outside—hence *outer approximation*. For a convex NLP
190+ the procedure converges finitely to the exact optimum; for non-convex problems
191+ it provides a lower bound similar to branch-and-cut.
192+
193+ *Common uses*
194+
195+ * **Piecewise-linearisation** of $\s qrt{\c dot},\;\l og(\c dot),\;\e xp(\c dot)$ or
196+ power losses in energy networks.
197+ * **Benders-like** decomposition where cuts approximate a difficult sub-problem
198+ (e.g. chance-constraints, stochastic recourse).
199+
200+ ---
201+
202+ Combined with the earlier tricks, epigraphs give **exact linear models** for
203+ many convex terms, while cutting planes let you tackle the truly nonlinear bits
204+ progressively, keeping the master problem linear and solver-friendly.
205+ """
206+
207+ # ╔═╡ ea3ea95a-58cb-4d0d-a167-aa68b8bc2645
208+ md """
209+ **Example** -- Cutting-plane Exercise – Approximating a Circular Constraint with Linear Cuts
210+
211+ A small drone must carry a **payload** of mass $m\, [\m athrm{kg}]$ and an
212+ **on-board battery** of capacity $c\, [\m athrm{Ah}]$.
213+ Because of fuselage geometry the two design variables must lie
214+ *inside* a circular envelope in the $(m,c)$-plane:
215+
216+ ```math
217+ m^{2} + c^{2} \; \l e \; 100 .
218+ ```
219+
220+ The engineering goal is to minimise the total cost
221+
222+ ```math
223+ \m in\; 200\, m + 80\, c ,
224+ ```
225+
226+ subject to the circle above and simple bounds
227+
228+ ```math
229+ 0 \;\l e\; m \;\l e\; 12, \q quad
230+ 0 \;\l e\; c \;\l e\; 12 .
231+ ```
232+
233+ Since the solver available to you handles only linear (M)ILPs, you will
234+ replace the quadratic constraint by a sequence of cutting planes
235+ (outer approximation).
236+
237+ **Cutting-plane algorithm outline**
238+
239+ 1. **Master LP**
240+ * Start with the bounding box only.
241+ * Solve for a tentative point $(\b ar m,\b ar c)$.
242+
243+ 2. **Feasibility check**
244+ * If $\b ar m^{2}+\b ar c^{2} \l e 100$ the point is feasible $\R ightarrow$ **done**.
245+ * Otherwise generate a *supporting hyperplane* for the circle at $(\b ar m,\b ar c)$:
246+
247+ ```math
248+ 2\,\b ar m\, (m - \b ar m) \; +\; 2\,\b ar c\, (c - \b ar c) \;\l e\; 100 \; -\; \b ar m^{2} \; -\; \b ar c^{2}.
249+ ```
250+
251+ (Derived from the gradient of $g(m,c)=m^{2}+c^{2}-100$.)
252+
253+ 3. **Add Cut**
254+ * Add cut & repeat.
255+
256+ 2
257+ """
258+
103259# ╔═╡ 808c505d-e10d-42e3-9fb1-9c6f384b2c3c
104260md """
105261---
@@ -237,6 +393,56 @@ begin
237393 end
238394end
239395
396+ # ╔═╡ fa5785a1-7274-4524-9e54-895d46e83861
397+ md """
398+ ### 2.3 Other Modeling Tricks
399+
400+ Below are some classic “tricks of the trade’’ for turning non-linear or logical
401+ requirements into **mixed-integer linear programming (MILP)** form.
402+ Throughout, $x\i n\m athbb R^n$ are continuous decision variables and
403+ $z\i n\{ 0,1\} $ are binary (0–1) variables.
404+
405+ ---
406+
407+ #### Big-$M$ linearisation of conditional constraints
408+ Suppose a constraint should apply **only if** a binary variable is 1:
409+
410+ ```math
411+ z = 1 \;\L ongrightarrow\; a^\t op x \l e b.
412+ ```
413+
414+ Introduce a sufficiently large constant $M>0$ and write
415+
416+ ```math
417+ a^\t op x \;\l e\; b + M\, (1-z).
418+ ```
419+
420+ * If $z=1$ the right-hand side is $b$, so the original constraint is enforced.
421+ * If $z=0$ the bound is relaxed by $M$ and becomes non-binding.
422+
423+ > **Caveat:** pick $M$ as tight as possible. Over-large $M$ values hurt the LP
424+ > relaxation and may cause numerical instability.
425+
426+ ---
427+
428+ #### Indicator constraints (a safer alternative)
429+ Modern solvers allow *indicator* constraints that internally handle the
430+ implication without an explicit $M$:
431+
432+ ```math
433+ z = 1 \;\L ongrightarrow\; a^\t op x \l e b.
434+ ```
435+
436+ In JuMP:
437+
438+ ```julia
439+ @constraint(model, z --> a' * x <= b)
440+ ```
441+
442+ ---
443+
444+ """
445+
240446# ╔═╡ 5e3444d0-8333-4f51-9146-d3d9625fe2e9
241447md """
242448---
@@ -422,6 +628,26 @@ begin
422628 end
423629end
424630
631+ # ╔═╡ 20aef3e9-47b5-4f60-9726-7db77f7c3e47
632+ begin
633+ # student answer
634+ ansd = missing
635+ try
636+ ansd = safeval (model_lp2, :x_d )
637+ catch # objective_value will throw if model_lp not ready
638+ ansd = missing
639+ end
640+
641+ # Decide which badge to show
642+ if ismissing (ansd) # nothing yet
643+ still_missing ()
644+ elseif x == 25.0
645+ correct ()
646+ else
647+ keep_working ()
648+ end
649+ end
650+
425651# ╔═╡ 254b9a87-17f9-4fea-8b28-0e3873b58fe2
426652begin
427653 ground_truth_3 = (x = - 7.946795 , y = 60.00 , obj = - 78554.7682 )
490716# ╟─1d3edbdd-7747-4651-b650-c6b9bf87b460
491717# ╟─6fb672d0-5a18-4ccc-b7b3-184839c2401b
492718# ╟─248b398a-0cf5-4c2b-8752-7b9cc4e765d6
719+ # ╟─245eb671-84e1-447b-8045-e9eb04966d80
720+ # ╟─6a823649-04fa-4322-a028-2fb29dffb08b
721+ # ╠═c369ab46-b416-4c12-83fe-65040a0c47c8
722+ # ╟─20aef3e9-47b5-4f60-9726-7db77f7c3e47
723+ # ╟─b13f9775-68c2-4646-9b67-c69ee23a4ea0
724+ # ╠═ea3ea95a-58cb-4d0d-a167-aa68b8bc2645
493725# ╟─808c505d-e10d-42e3-9fb1-9c6f384b2c3c
494726# ╠═39617561-bbbf-4ef6-91e2-358dfe76581c
495727# ╟─01367096-3971-4e79-ace2-83600672fbde
498730# ╟─3997d993-0a31-435e-86cd-50242746c305
499731# ╠═3f56ec63-1fa6-403c-8d2a-1990382b97ae
500732# ╟─0e8ed625-df85-4bd2-8b16-b475a72df566
733+ # ╟─fa5785a1-7274-4524-9e54-895d46e83861
501734# ╟─5e3444d0-8333-4f51-9146-d3d9625fe2e9
502735# ╠═0e190de3-da60-41e9-9da5-5a0c7fefd1d7
503736# ╟─cac18d70-b354-48c7-9f37-31ee0c585675
0 commit comments