├── .gitignore ├── LICENSE ├── Project.toml ├── README.md ├── assignment.ipynb ├── bisection-method.ipynb ├── blocks.png ├── color-blocks.ipynb ├── digit-finding-problem.ipynb ├── duality.ipynb ├── entropy-dice.ipynb ├── flux-xor-nn.ipynb ├── game-stone-paper-scissors.ipynb ├── german-tank-problem.ipynb ├── goal programming.ipynb ├── gradient-descent.ipynb ├── hooke-jeeves.ipynb ├── julia-object-model.ipynb ├── k-nearest-neighbour.ipynb ├── kuhn-tucker-for-linear-problem.ipynb ├── kuhn-tucker1.ipynb ├── lad.ipynb ├── linear-equation-system.ipynb ├── logistic-regression.ipynb ├── maximum-flow.ipynb ├── maximum-flow.png ├── metaheuristics.ipynb ├── multi-dimensional-scaling.ipynb ├── newton-method-examples ├── generator.jl ├── newton1.pdf ├── newton1.tex ├── newton2.pdf ├── newton2.tex ├── newton3.pdf ├── newton3.tex ├── newton4.pdf └── newton4.tex ├── newton-multiple.ipynb ├── nlsolve-example.ipynb ├── nonlinear-least-squares.ipynb ├── notes └── or │ ├── assignment │ ├── assignment.pdf │ └── assignment.tex │ ├── minimum-spanning-tree │ └── mst-example.pdf │ ├── shortestpath.pdf │ ├── simplex │ ├── simplex.pdf │ └── simplex.tex │ ├── transportation │ ├── transportation-problems.pdf │ └── transportation-problems.tex │ └── yoneylem-rassal-sayi-tablosu.pdf ├── nsga2.ipynb ├── optimal system design.ipynb ├── p-median.ipynb ├── pluto └── sensitivity.jl ├── project-selection.ipynb ├── quantileregression.ipynb ├── sensitivity.ipynb ├── set-covering.ipynb ├── setcoveringmap.png ├── shadow_price.ipynb ├── shortestpath-solution.png ├── shortestpath.ipynb ├── shortestpath.png ├── simplex-iterations.ipynb ├── standard-normal-table.ipynb ├── symbolicregression.ipynb ├── topsis.ipynb ├── transportation-short.ipynb ├── transportation.ipynb └── traveling-salesman.ipynb /.gitignore: -------------------------------------------------------------------------------- 1 | # Files generated by invoking Julia with --code-coverage 2 | *.jl.cov 3 | *.jl.*.cov 4 | 5 | # Files generated by invoking Julia with --track-allocation 6 | *.jl.mem 7 | 8 | # System-specific files and directories generated by the BinaryProvider and BinDeps packages 9 | # They contain absolute paths specific to the host computer, and so should not be committed 10 | deps/deps.jl 11 | deps/build.log 12 | deps/downloads/ 13 | deps/usr/ 14 | deps/src/ 15 | 16 | # Build artifacts for creating documentation generated by the Documenter package 17 | docs/build/ 18 | docs/site/ 19 | 20 | # File generated by Pkg, the package manager, based on a corresponding Project.toml 21 | # It records a fixed state of all packages used by the project. As such, it should not be 22 | # committed for packages, but should be committed for applications that require a static 23 | # environment. 24 | Manifest.toml 25 | 26 | .* 27 | 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Mehmet Hakan Satman 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Project.toml: -------------------------------------------------------------------------------- 1 | [deps] 2 | HiGHS = "87dc4568-4c63-4d18-b0c0-bb2238e4078b" 3 | IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a" 4 | JuMP = "4076af6c-e467-56ae-b986-b466b2749572" 5 | OperationsResearchModels = "8042aa49-e599-49ec-a8f7-b5b80cc82a88" 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # notebooks 2 | 3 | My Jupyter notebooks for some code snippets. 4 | 5 | ## assignment.ipynb 6 | 7 | Julia example for an assignment problem 8 | 9 | ## bisection-method.ipynb 10 | 11 | Bisection method 12 | 13 | ## lad.ipynb 14 | 15 | Least absolute deviations regression estimator (Linear Programming) 16 | 17 | ## newton-multiple.ipynb 18 | 19 | Newton's method for minimizing multivariate functions 20 | 21 | 22 | ## transportation.ipynb 23 | 24 | An example of transportation problem in Julia 25 | 26 | ## shortestpath.ipynb 27 | 28 | An example of linear programming solution for a shortest path problem 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /assignment.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 89, 6 | "id": "5318e66e-be19-440b-804d-611e02e9e3b3", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "using JuMP" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 90, 16 | "id": "9797e424-66e7-4950-a1c9-f0f8962e037c", 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "using GLPK" 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "63034f6a-811a-4634-b101-d94db99b8273", 26 | "metadata": {}, 27 | "source": [ 28 | "# Assignment problem" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "id": "d1d5af64-6ac3-47fe-b86d-a0ec3500694c", 34 | "metadata": {}, 35 | "source": [ 36 | "| | Task 1 | Task 2 | Task 3 |\n", 37 | "| :---: | :-------: | :-----: | :----: |\n", 38 | "| Per1 | 1 | 5 | 4 |\n", 39 | "| Per2 | 3 | 9 | 7 |\n", 40 | "| Per3 | 3 | 6 | 2 |" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "id": "12be7930-a4fa-49a4-9f3b-36a72e010a81", 46 | "metadata": {}, 47 | "source": [ 48 | "$$\n", 49 | "\\begin{aligned}\n", 50 | "\\min z & = x_{11} + 5x_{12} + 4x_{13} + 3x_{21} + 9x_{22} + 7x_{23} + 3x_{31} + 6x_{32} + 2x_{33} \\\\\n", 51 | "\\text{Subjecto to:} \\\\\n", 52 | "& \\sum_{i=1}^{3} x_{i1} + x_{i2} + x_{i3} = 1 \\\\\n", 53 | "& \\sum_{j=1}^{3} x_{1j} + x_{2j} + x_{3j} = 1 \\\\\n", 54 | "& x_{ij} \\in \\{0,1\\} \\\\\n", 55 | "& i = 1,2,3 \\\\\n", 56 | "& j = 1,2,3\n", 57 | "\\end{aligned}\n", 58 | "$$" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 91, 64 | "id": "e6ac2cf3-e5eb-4032-81f4-c410b7d93891", 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "data": { 69 | "text/plain": [ 70 | "A JuMP Model\n", 71 | "Feasibility problem with:\n", 72 | "Variables: 0\n", 73 | "Model mode: AUTOMATIC\n", 74 | "CachingOptimizer state: EMPTY_OPTIMIZER\n", 75 | "Solver name: GLPK" 76 | ] 77 | }, 78 | "execution_count": 91, 79 | "metadata": {}, 80 | "output_type": "execute_result" 81 | } 82 | ], 83 | "source": [ 84 | "m = Model(GLPK.Optimizer)" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": 92, 90 | "id": "09972f9b-cbb4-4038-9749-5f0bf3839b10", 91 | "metadata": {}, 92 | "outputs": [ 93 | { 94 | "data": { 95 | "text/plain": [ 96 | "3×3 Matrix{VariableRef}:\n", 97 | " x[1,1] x[1,2] x[1,3]\n", 98 | " x[2,1] x[2,2] x[2,3]\n", 99 | " x[3,1] x[3,2] x[3,3]" 100 | ] 101 | }, 102 | "execution_count": 92, 103 | "metadata": {}, 104 | "output_type": "execute_result" 105 | } 106 | ], 107 | "source": [ 108 | "@variable(m, x[1:3, 1:3], Bin)" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 93, 114 | "id": "f334d3cf-c204-402d-af55-d9893ae195c2", 115 | "metadata": {}, 116 | "outputs": [], 117 | "source": [ 118 | "c = [1, 3, 3, 5, 9, 6, 4, 7, 2];" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 94, 124 | "id": "625ea2c6-9a8f-4f4d-8634-e36aba53ca7b", 125 | "metadata": {}, 126 | "outputs": [ 127 | { 128 | "data": { 129 | "text/latex": [ 130 | "$$ x_{1,1} + 3 x_{2,1} + 3 x_{3,1} + 5 x_{1,2} + 9 x_{2,2} + 6 x_{3,2} + 4 x_{1,3} + 7 x_{2,3} + 2 x_{3,3} $$" 131 | ], 132 | "text/plain": [ 133 | "x[1,1] + 3 x[2,1] + 3 x[3,1] + 5 x[1,2] + 9 x[2,2] + 6 x[3,2] + 4 x[1,3] + 7 x[2,3] + 2 x[3,3]" 134 | ] 135 | }, 136 | "execution_count": 94, 137 | "metadata": {}, 138 | "output_type": "execute_result" 139 | } 140 | ], 141 | "source": [ 142 | "@objective(m, Min, sum(c .* x[:]))" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 95, 148 | "id": "702b5457-3bc5-40d5-b135-1858110c5ead", 149 | "metadata": {}, 150 | "outputs": [], 151 | "source": [ 152 | "for i in 1:3\n", 153 | " @constraint(m, sum(x[i,:]) == 1)\n", 154 | "end" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 96, 160 | "id": "3799e366-efcb-4659-982b-397340157b22", 161 | "metadata": {}, 162 | "outputs": [], 163 | "source": [ 164 | "for i in 1:3\n", 165 | " @constraint(m, sum(x[:,i]) == 1)\n", 166 | "end" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": 97, 172 | "id": "a773f18f-7239-4a4d-af0d-654b3ac782a1", 173 | "metadata": {}, 174 | "outputs": [ 175 | { 176 | "name": "stdout", 177 | "output_type": "stream", 178 | "text": [ 179 | "Min x[1,1] + 3 x[2,1] + 3 x[3,1] + 5 x[1,2] + 9 x[2,2] + 6 x[3,2] + 4 x[1,3] + 7 x[2,3] + 2 x[3,3]\n", 180 | "Subject to\n", 181 | " x[1,1] + x[1,2] + x[1,3] = 1.0\n", 182 | " x[2,1] + x[2,2] + x[2,3] = 1.0\n", 183 | " x[3,1] + x[3,2] + x[3,3] = 1.0\n", 184 | " x[1,1] + x[2,1] + x[3,1] = 1.0\n", 185 | " x[1,2] + x[2,2] + x[3,2] = 1.0\n", 186 | " x[1,3] + x[2,3] + x[3,3] = 1.0\n", 187 | " x[1,1] binary\n", 188 | " x[2,1] binary\n", 189 | " x[3,1] binary\n", 190 | " x[1,2] binary\n", 191 | " x[2,2] binary\n", 192 | " x[3,2] binary\n", 193 | " x[1,3] binary\n", 194 | " x[2,3] binary\n", 195 | " x[3,3] binary\n", 196 | "\n" 197 | ] 198 | } 199 | ], 200 | "source": [ 201 | "println(m)" 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": 98, 207 | "id": "85bf6ed4-d579-4cc4-9795-7ee649462e93", 208 | "metadata": {}, 209 | "outputs": [], 210 | "source": [ 211 | "optimize!(m)" 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "execution_count": 99, 217 | "id": "880d6c60-4ca3-413e-8e45-48fcfc250472", 218 | "metadata": {}, 219 | "outputs": [ 220 | { 221 | "data": { 222 | "text/plain": [ 223 | "3×3 Matrix{Float64}:\n", 224 | " 0.0 1.0 0.0\n", 225 | " 1.0 0.0 0.0\n", 226 | " 0.0 0.0 1.0" 227 | ] 228 | }, 229 | "execution_count": 99, 230 | "metadata": {}, 231 | "output_type": "execute_result" 232 | } 233 | ], 234 | "source": [ 235 | "value.(x)" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": 100, 241 | "id": "ed3b02f2-ccee-40b2-9d58-b855e930407f", 242 | "metadata": {}, 243 | "outputs": [ 244 | { 245 | "data": { 246 | "text/plain": [ 247 | "10" 248 | ] 249 | }, 250 | "execution_count": 100, 251 | "metadata": {}, 252 | "output_type": "execute_result" 253 | } 254 | ], 255 | "source": [ 256 | "totalcost = 5 + 3 + 2" 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": 101, 262 | "id": "d4e55f94-9265-4e42-a82e-20751ebc7a90", 263 | "metadata": {}, 264 | "outputs": [ 265 | { 266 | "data": { 267 | "text/plain": [ 268 | "* Solver : GLPK\n", 269 | "\n", 270 | "* Status\n", 271 | " Termination status : OPTIMAL\n", 272 | " Primal status : FEASIBLE_POINT\n", 273 | " Dual status : NO_SOLUTION\n", 274 | " Message from the solver:\n", 275 | " \"Solution is optimal\"\n", 276 | "\n", 277 | "* Candidate solution\n", 278 | " Objective value : 10.0\n", 279 | " Objective bound : 10.0\n", 280 | "\n", 281 | "* Work counters\n", 282 | " Solve time (sec) : 0.00015\n" 283 | ] 284 | }, 285 | "execution_count": 101, 286 | "metadata": {}, 287 | "output_type": "execute_result" 288 | } 289 | ], 290 | "source": [ 291 | "JuMP.solution_summary(m)" 292 | ] 293 | }, 294 | { 295 | "cell_type": "code", 296 | "execution_count": null, 297 | "id": "66776132-468a-4cf2-a090-ea2b90151a54", 298 | "metadata": {}, 299 | "outputs": [], 300 | "source": [] 301 | } 302 | ], 303 | "metadata": { 304 | "kernelspec": { 305 | "display_name": "Julia 1.7.1", 306 | "language": "julia", 307 | "name": "julia-1.7" 308 | }, 309 | "language_info": { 310 | "file_extension": ".jl", 311 | "mimetype": "application/julia", 312 | "name": "julia", 313 | "version": "1.7.1" 314 | } 315 | }, 316 | "nbformat": 4, 317 | "nbformat_minor": 5 318 | } 319 | -------------------------------------------------------------------------------- /bisection-method.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "4f44ad1d-fb09-43c7-8ee5-0bbf5a5e71c5", 6 | "metadata": {}, 7 | "source": [ 8 | "# Bisection Method" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "f90c5f35-b7b9-4c37-9f4f-793fcc4a68c5", 14 | "metadata": {}, 15 | "source": [ 16 | "- $f(x)$: The function (concave downwards in bounds)\n", 17 | "- $x'$: Current solution\n", 18 | "- $x^*$: Optimum value\n", 19 | "- $\\underline{x}$: Lower bound for $x$.\n", 20 | "- $\\bar{x}$: Upper bound for $x$.\n", 21 | "- $\\varepsilon$: Tolerance" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "id": "3fc82273-718e-4a89-8397-b0f65ac84702", 27 | "metadata": {}, 28 | "source": [ 29 | "### The algorithm" 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "id": "14c04b88-9962-4939-a292-5a39394b0ac9", 35 | "metadata": {}, 36 | "source": [ 37 | "- Step 0:\n", 38 | "- Set $\\varepsilon$, e.g., $\\varepsilon = 0.001$.\n", 39 | "- Set initial values for $\\underline{x}$ and $\\bar{x}$.\n", 40 | "- Set initial solution as" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "id": "aa6e5ddd-abe9-4a88-ab3d-5057aa488358", 46 | "metadata": {}, 47 | "source": [ 48 | "$$\n", 49 | "x' = \\frac{\\underline{x} + \\bar{x}}{2}\n", 50 | "$$" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "id": "91f3e575-30dd-40a0-8451-9709bd291e99", 56 | "metadata": {}, 57 | "source": [ 58 | "- Step n:\n", 59 | "- set $x' = \\frac{\\underline{x} + \\bar{x}}{2}$\n", 60 | "- if $\\bar{x} - x' \\le 2 \\varepsilon$ then stop\n", 61 | "- Evaluate d = f'(x')\n", 62 | "- if $f'(x) \\ge 0$ then $\\underline{x} = x'$\n", 63 | "- otherwise $\\bar{x} = x'$\n", 64 | "- iterate Step n" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 1, 70 | "id": "e5d42587-3356-420f-b91d-bf02fbfc1454", 71 | "metadata": {}, 72 | "outputs": [ 73 | { 74 | "data": { 75 | "text/plain": [ 76 | "f (generic function with 1 method)" 77 | ] 78 | }, 79 | "execution_count": 1, 80 | "metadata": {}, 81 | "output_type": "execute_result" 82 | } 83 | ], 84 | "source": [ 85 | "f(x) = -4x^2" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 4, 91 | "id": "ca96f175-a9b6-43d1-9fe4-9b05f7ff179e", 92 | "metadata": {}, 93 | "outputs": [ 94 | { 95 | "data": { 96 | "text/plain": [ 97 | "fderiv (generic function with 1 method)" 98 | ] 99 | }, 100 | "execution_count": 4, 101 | "metadata": {}, 102 | "output_type": "execute_result" 103 | } 104 | ], 105 | "source": [ 106 | "fderiv(x) = -8x" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 13, 112 | "id": "ea5e798d-8586-4639-b3d0-f4b9bd521725", 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "data": { 117 | "text/plain": [ 118 | "-6" 119 | ] 120 | }, 121 | "execution_count": 13, 122 | "metadata": {}, 123 | "output_type": "execute_result" 124 | } 125 | ], 126 | "source": [ 127 | "xlower = -6" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": 14, 133 | "id": "8b0932c4-ccb0-4738-8c41-a055fa5024ab", 134 | "metadata": {}, 135 | "outputs": [ 136 | { 137 | "data": { 138 | "text/plain": [ 139 | "5" 140 | ] 141 | }, 142 | "execution_count": 14, 143 | "metadata": {}, 144 | "output_type": "execute_result" 145 | } 146 | ], 147 | "source": [ 148 | "xupper = 5" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 22, 154 | "id": "01a030f6-0018-47f9-a7f0-034978ac76cd", 155 | "metadata": {}, 156 | "outputs": [ 157 | { 158 | "data": { 159 | "text/plain": [ 160 | "1.0e-5" 161 | ] 162 | }, 163 | "execution_count": 22, 164 | "metadata": {}, 165 | "output_type": "execute_result" 166 | } 167 | ], 168 | "source": [ 169 | "epsilon = 0.00001" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 23, 175 | "id": "d8484cf3-227e-492a-8365-b7b872a21918", 176 | "metadata": {}, 177 | "outputs": [ 178 | { 179 | "name": "stderr", 180 | "output_type": "stream", 181 | "text": [ 182 | "┌ Info: Results: \n", 183 | "│ xupper = 1.52587890625e-5\n", 184 | "│ xlower = -5.7220458984375e-6\n", 185 | "│ x = 4.76837158203125e-6\n", 186 | "│ i = 5\n", 187 | "└ @ Main In[23]:5\n" 188 | ] 189 | } 190 | ], 191 | "source": [ 192 | "maxiter = 1000\n", 193 | "for i in 1:maxiter\n", 194 | " x = (xupper + xlower) / 2.0\n", 195 | " if (xupper - x) <= 2 * epsilon\n", 196 | " @info \"Results: \" xupper xlower x i\n", 197 | " break\n", 198 | " end\n", 199 | " fd = fderiv(x)\n", 200 | " if fd >= 0\n", 201 | " xlower = x\n", 202 | " else\n", 203 | " xupper = x\n", 204 | " end\n", 205 | "end" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": null, 211 | "id": "1ee28b8d-6047-4339-ab0d-a5f819fd6b9f", 212 | "metadata": {}, 213 | "outputs": [], 214 | "source": [] 215 | } 216 | ], 217 | "metadata": { 218 | "kernelspec": { 219 | "display_name": "Julia 1.7.1", 220 | "language": "julia", 221 | "name": "julia-1.7" 222 | }, 223 | "language_info": { 224 | "file_extension": ".jl", 225 | "mimetype": "application/julia", 226 | "name": "julia", 227 | "version": "1.7.1" 228 | } 229 | }, 230 | "nbformat": 4, 231 | "nbformat_minor": 5 232 | } 233 | -------------------------------------------------------------------------------- /blocks.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jbytecode/notebooks/0bb925e5599c6bcf130255d586b9c1d7f0e2595b/blocks.png -------------------------------------------------------------------------------- /color-blocks.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "12240dd9-c190-4ce9-bb76-b628e08e4a2d", 6 | "metadata": {}, 7 | "source": [ 8 | "" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 21, 14 | "id": "9c2fdd3b-7097-412d-b5a7-75533d217f6e", 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "using JuMP, GLPK" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 25, 24 | "id": "ba5fd7a4-f9a2-4165-8332-d28ca2d604ce", 25 | "metadata": {}, 26 | "outputs": [ 27 | { 28 | "name": "stdout", 29 | "output_type": "stream", 30 | "text": [ 31 | "Red: 7.0\n", 32 | "Green: 3.0\n", 33 | "Blue: 8.0\n", 34 | "Yellow: 1.0\n" 35 | ] 36 | } 37 | ], 38 | "source": [ 39 | "m = Model(GLPK.Optimizer)\n", 40 | "@variable(m, y, Int)\n", 41 | "@variable(m, b, Int)\n", 42 | "@variable(m, g, Int)\n", 43 | "@variable(m, r, Int)\n", 44 | "@constraint(m, \n", 45 | " (1000y + 100b + 10b + g) \n", 46 | " + (1000g + 100r + 10y + y) \n", 47 | " + (1000y + 100r + 10y + r) == (1000r + 100g + 10y + y))\n", 48 | "@constraint(m, 1 <= y <= 9)\n", 49 | "@constraint(m, 1 <= g <= 9)\n", 50 | "@constraint(m, 0 <= b <= 9)\n", 51 | "@constraint(m, 1 <= r <= 9)\n", 52 | "optimize!(m)\n", 53 | "println(\"Red: \", value(r))\n", 54 | "println(\"Green: \", value(g))\n", 55 | "println(\"Blue: \", value(b))\n", 56 | "println(\"Yellow: \", value(y))" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 23, 62 | "id": "9a28c0b8-bdc4-4141-b2f5-de4d701362cc", 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "Feasibility\n", 70 | "Subject to\n", 71 | " 2010 y + 110 b + 901 g - 799 r = 0.0\n", 72 | " y ∈ [1.0, 9.0]\n", 73 | " g ∈ [1.0, 9.0]\n", 74 | " b ∈ [0.0, 9.0]\n", 75 | " r ∈ [1.0, 9.0]\n", 76 | " y integer\n", 77 | " b integer\n", 78 | " g integer\n", 79 | " r integer\n", 80 | "\n" 81 | ] 82 | } 83 | ], 84 | "source": [ 85 | "println(m)" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 24, 91 | "id": "7d917d22-4d55-4201-9f2b-92201955e27d", 92 | "metadata": {}, 93 | "outputs": [ 94 | { 95 | "data": { 96 | "text/plain": [ 97 | "4-element Vector{Float64}:\n", 98 | " 7.0\n", 99 | " 3.0\n", 100 | " 1.0\n", 101 | " 1.0" 102 | ] 103 | }, 104 | "execution_count": 24, 105 | "metadata": {}, 106 | "output_type": "execute_result" 107 | } 108 | ], 109 | "source": [ 110 | "value.([r, g, y, y])" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": null, 116 | "id": "64530fac-cb62-4a37-bf29-f8ff414720e5", 117 | "metadata": {}, 118 | "outputs": [], 119 | "source": [] 120 | } 121 | ], 122 | "metadata": { 123 | "kernelspec": { 124 | "display_name": "Julia 1.9.0-DEV", 125 | "language": "julia", 126 | "name": "julia-1.9" 127 | }, 128 | "language_info": { 129 | "file_extension": ".jl", 130 | "mimetype": "application/julia", 131 | "name": "julia", 132 | "version": "1.9.0" 133 | } 134 | }, 135 | "nbformat": 4, 136 | "nbformat_minor": 5 137 | } 138 | -------------------------------------------------------------------------------- /digit-finding-problem.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 4, 6 | "id": "6f442e7a-f48b-49ae-bb32-18216505acd6", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "using JuMP\n", 11 | "using GLPK" 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "id": "91c8fc2e-2de9-4d11-9dd0-d1ca1c36c689", 17 | "metadata": {}, 18 | "source": [ 19 | "# Goal Programming Approach" 20 | ] 21 | }, 22 | { 23 | "cell_type": "markdown", 24 | "id": "165b435e-df16-4e6c-a83c-9e79b7eb5833", 25 | "metadata": {}, 26 | "source": [ 27 | "- I am 3 digit number between 400 and 800\n", 28 | "- My digits add up to 15\n", 29 | "- My ten's digit is twice hundereds' digit\n", 30 | "- What number am I?" 31 | ] 32 | }, 33 | { 34 | "cell_type": "markdown", 35 | "id": "9ff85896-cf01-4b9c-9d67-d36af47f5cfa", 36 | "metadata": {}, 37 | "source": [ 38 | "$$\n", 39 | "\\begin{aligned}\n", 40 | "\\min z = & d_1^- + d_1^+ \\\\\n", 41 | "\\text{Subject to:} \\\\\n", 42 | "& x_1 + x_2 + x_3 + d_1^- - d_1^+ = 15 \\\\\n", 43 | "& x_2 - 2x_1 = 0 \\\\\n", 44 | "& 100x_1 + 10x_2 + x_3 \\le 800 \\\\\n", 45 | "& 100x_1 + 10x_2 + x_3 \\ge 400 \\\\\n", 46 | "& x_1, x_2, x_3 \\ge 0 \\\\\n", 47 | "& x_1, x_2, x_3 \\le 9 \\\\\n", 48 | "& d_1^-, d_1^+ \\ge 0 \\\\\n", 49 | "& x_1, x_2, x_3 \\in \\{0, Z^+\\}\n", 50 | "\\end{aligned}\n", 51 | "$$" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 5, 57 | "id": "cd2b34cb-b61d-4d2b-acf5-daed586c9c93", 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [ 61 | "using JuMP, GLPK\n", 62 | "\n", 63 | "m = Model(GLPK.Optimizer)\n", 64 | "\n", 65 | "@variable(m, x[1:3], Int)\n", 66 | "@variable(m, d[1:2])\n", 67 | "\n", 68 | "@objective(m, Min, sum(d[1:2]))\n", 69 | "@constraint(m, x[1] + x[2] + x[3] + d[1] - d[2] == 15)\n", 70 | "@constraint(m, x[2] == 2 * x[1])\n", 71 | "@constraint(m, x[1] * 100 + x[2] *10 + x[3] <= 800)\n", 72 | "@constraint(m, x[1] * 100 + x[2] *10 + x[3] >= 400)\n", 73 | "@constraint(m, x[1:3] .>= 0)\n", 74 | "@constraint(m, x[1:3] .<= 9)\n", 75 | "@constraint(m, d[1:2] .>= 0)\n", 76 | "\n", 77 | "optimize!(m)" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 6, 83 | "id": "6e79611a-e5da-4357-ba04-373ec15b11c4", 84 | "metadata": {}, 85 | "outputs": [ 86 | { 87 | "data": { 88 | "text/plain": [ 89 | "3-element Vector{Float64}:\n", 90 | " 4.0\n", 91 | " 8.0\n", 92 | " 3.0" 93 | ] 94 | }, 95 | "execution_count": 6, 96 | "metadata": {}, 97 | "output_type": "execute_result" 98 | } 99 | ], 100 | "source": [ 101 | "value.(x[1:3])" 102 | ] 103 | }, 104 | { 105 | "cell_type": "markdown", 106 | "id": "8162fddb-f853-411c-b417-7058ff7eaa3b", 107 | "metadata": {}, 108 | "source": [ 109 | "# Constraint Satisfaction Approach" 110 | ] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "id": "2d4b5158-39ad-4273-ae2b-0eb4d377d3a4", 115 | "metadata": {}, 116 | "source": [ 117 | "- I am 3 digit number between 400 and 800\n", 118 | "- My digits add up to 15\n", 119 | "- My ten's digit is twice hundereds' digit\n", 120 | "- What number am I?" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 7, 126 | "id": "4d8602fe-4d03-4f7e-a2c7-e8c6317c7194", 127 | "metadata": {}, 128 | "outputs": [ 129 | { 130 | "data": { 131 | "text/plain": [ 132 | "3-element Vector{Float64}:\n", 133 | " 4.0\n", 134 | " 8.0\n", 135 | " 3.0" 136 | ] 137 | }, 138 | "execution_count": 7, 139 | "metadata": {}, 140 | "output_type": "execute_result" 141 | } 142 | ], 143 | "source": [ 144 | "m = Model(GLPK.Optimizer)\n", 145 | "@variable(m, a, Int)\n", 146 | "@variable(m, b, Int)\n", 147 | "@variable(m, c, Int)\n", 148 | "\n", 149 | "@constraint(m, 400 <= 100a + 10b + c <= 800)\n", 150 | "@constraint(m, a + b + c == 15)\n", 151 | "@constraint(m, b == 2a)\n", 152 | "@constraint(m, 0 .<= [a, b, c] .<= 9)\n", 153 | "\n", 154 | "optimize!(m)\n", 155 | "value.([a, b, c])" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": null, 161 | "id": "236d3d2d-b5db-40d1-a1f2-b137783213af", 162 | "metadata": {}, 163 | "outputs": [], 164 | "source": [] 165 | } 166 | ], 167 | "metadata": { 168 | "kernelspec": { 169 | "display_name": "Julia 1.9.0-DEV", 170 | "language": "julia", 171 | "name": "julia-1.9" 172 | }, 173 | "language_info": { 174 | "file_extension": ".jl", 175 | "mimetype": "application/julia", 176 | "name": "julia", 177 | "version": "1.9.0" 178 | } 179 | }, 180 | "nbformat": 4, 181 | "nbformat_minor": 5 182 | } 183 | -------------------------------------------------------------------------------- /entropy-dice.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 202, 6 | "id": "c313f401-bf96-46d3-91bc-175ec823ac59", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "using Ipopt" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 203, 16 | "id": "1b56f01b-429d-4e2b-8264-868ce7e3cc03", 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "using JuMP" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 204, 26 | "id": "f65514e1-a9c4-44cc-ac48-a820fbce97c7", 27 | "metadata": {}, 28 | "outputs": [ 29 | { 30 | "data": { 31 | "text/plain": [ 32 | "A JuMP Model\n", 33 | "Feasibility problem with:\n", 34 | "Variables: 0\n", 35 | "Model mode: AUTOMATIC\n", 36 | "CachingOptimizer state: EMPTY_OPTIMIZER\n", 37 | "Solver name: Ipopt" 38 | ] 39 | }, 40 | "execution_count": 204, 41 | "metadata": {}, 42 | "output_type": "execute_result" 43 | } 44 | ], 45 | "source": [ 46 | "m = Model(Ipopt.Optimizer)" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 205, 52 | "id": "ce681950-0ea9-4a60-aa91-cce9998ac72b", 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "data": { 57 | "text/plain": [ 58 | "6-element Vector{VariableRef}:\n", 59 | " p[1]\n", 60 | " p[2]\n", 61 | " p[3]\n", 62 | " p[4]\n", 63 | " p[5]\n", 64 | " p[6]" 65 | ] 66 | }, 67 | "execution_count": 205, 68 | "metadata": {}, 69 | "output_type": "execute_result" 70 | } 71 | ], 72 | "source": [ 73 | "@variable(m, p[1:6])" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 206, 79 | "id": "9274f3a2-144f-4c35-9440-cf439847f839", 80 | "metadata": {}, 81 | "outputs": [], 82 | "source": [ 83 | "function mylog(x)\n", 84 | " if x == 0.0\n", 85 | " return 0.0\n", 86 | " else\n", 87 | " return log(x)\n", 88 | " end\n", 89 | "end\n", 90 | "register(m, :mylog, 1, mylog; autodiff = true)" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 207, 96 | "id": "cbb79288-f5cc-40dc-8d30-045530f2c9f8", 97 | "metadata": {}, 98 | "outputs": [], 99 | "source": [ 100 | "@NLobjective(m, Max, -sum(p[i] * mylog(p[i]) for i in 1:6))" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 208, 106 | "id": "8a0277a7-dd89-4e25-b585-7bc5615b8919", 107 | "metadata": {}, 108 | "outputs": [ 109 | { 110 | "data": { 111 | "text/latex": [ 112 | "$$ p_{1} + p_{2} + p_{3} + p_{4} + p_{5} + p_{6} = 1.0 $$" 113 | ], 114 | "text/plain": [ 115 | "p[1] + p[2] + p[3] + p[4] + p[5] + p[6] = 1.0" 116 | ] 117 | }, 118 | "execution_count": 208, 119 | "metadata": {}, 120 | "output_type": "execute_result" 121 | } 122 | ], 123 | "source": [ 124 | "@constraint(m, sum(p[i] for i in 1:6) == 1.0)" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 209, 130 | "id": "3b0eab02-1ec2-4592-9918-f119fed75a95", 131 | "metadata": {}, 132 | "outputs": [], 133 | "source": [ 134 | "for i in 1:6\n", 135 | " @NLconstraint(m, p[i] >= 0.0)\n", 136 | "end" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 210, 142 | "id": "4436c864-d3e9-4269-989c-59844d6e02d1", 143 | "metadata": {}, 144 | "outputs": [ 145 | { 146 | "name": "stdout", 147 | "output_type": "stream", 148 | "text": [ 149 | "Max -((p[1] * mylog(p[1]) + p[2] * mylog(p[2]) + p[3] * mylog(p[3]) + p[4] * mylog(p[4]) + p[5] * mylog(p[5]) + p[6] * mylog(p[6])))\n", 150 | "Subject to\n", 151 | " p[1] + p[2] + p[3] + p[4] + p[5] + p[6] = 1.0\n", 152 | " p[1] - 0.0 ≥ 0\n", 153 | " p[2] - 0.0 ≥ 0\n", 154 | " p[3] - 0.0 ≥ 0\n", 155 | " p[4] - 0.0 ≥ 0\n", 156 | " p[5] - 0.0 ≥ 0\n", 157 | " p[6] - 0.0 ≥ 0\n", 158 | "\n" 159 | ] 160 | } 161 | ], 162 | "source": [ 163 | "println(m)" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": 211, 169 | "id": "9e8e4aaf-9178-4a34-bb23-2fdff81b7d15", 170 | "metadata": {}, 171 | "outputs": [ 172 | { 173 | "name": "stdout", 174 | "output_type": "stream", 175 | "text": [ 176 | "This is Ipopt version 3.14.4, running with linear solver MUMPS 5.4.1.\n", 177 | "\n", 178 | "Number of nonzeros in equality constraint Jacobian...: 6\n", 179 | "Number of nonzeros in inequality constraint Jacobian.: 6\n", 180 | "Number of nonzeros in Lagrangian Hessian.............: 6\n", 181 | "\n", 182 | "Total number of variables............................: 6\n", 183 | " variables with only lower bounds: 0\n", 184 | " variables with lower and upper bounds: 0\n", 185 | " variables with only upper bounds: 0\n", 186 | "Total number of equality constraints.................: 1\n", 187 | "Total number of inequality constraints...............: 6\n", 188 | " inequality constraints with only lower bounds: 6\n", 189 | " inequality constraints with lower and upper bounds: 0\n", 190 | " inequality constraints with only upper bounds: 0\n", 191 | "\n", 192 | "iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls\n", 193 | " 0 -0.0000000e+00 1.00e+00 0.00e+00 -1.0 0.00e+00 - 0.00e+00 0.00e+00 0\n", 194 | " 1 1.7917595e+00 1.11e-16 1.37e+01 -1.7 1.67e-01 - 6.75e-02 1.00e+00f 1\n", 195 | " 2 1.7917595e+00 0.00e+00 2.00e-07 -1.7 7.06e-17 - 1.00e+00 1.00e+00 0\n", 196 | " 3 1.7917595e+00 2.22e-16 1.50e-09 -3.8 7.20e-17 - 1.00e+00 1.00e+00 0\n", 197 | " 4 1.7917595e+00 1.11e-16 1.84e-11 -5.7 5.93e-17 - 1.00e+00 1.00e+00 0\n", 198 | " 5 1.7917595e+00 2.22e-16 2.51e-14 -8.6 1.85e-17 - 1.00e+00 1.00e+00T 0\n", 199 | "\n", 200 | "Number of Iterations....: 5\n", 201 | "\n", 202 | " (scaled) (unscaled)\n", 203 | "Objective...............: -1.7917594692280550e+00 1.7917594692280550e+00\n", 204 | "Dual infeasibility......: 2.5059036063649481e-14 2.5059036063649481e-14\n", 205 | "Constraint violation....: 2.2204460492503131e-16 2.2204460492503131e-16\n", 206 | "Variable bound violation: 0.0000000000000000e+00 0.0000000000000000e+00\n", 207 | "Complementarity.........: 2.5059035596800899e-09 2.5059035596800899e-09\n", 208 | "Overall NLP error.......: 2.5059035596800899e-09 2.5059035596800899e-09\n", 209 | "\n", 210 | "\n", 211 | "Number of objective function evaluations = 6\n", 212 | "Number of objective gradient evaluations = 6\n", 213 | "Number of equality constraint evaluations = 6\n", 214 | "Number of inequality constraint evaluations = 6\n", 215 | "Number of equality constraint Jacobian evaluations = 6\n", 216 | "Number of inequality constraint Jacobian evaluations = 6\n", 217 | "Number of Lagrangian Hessian evaluations = 5\n", 218 | "Total seconds in IPOPT = 0.053\n", 219 | "\n", 220 | "EXIT: Optimal Solution Found.\n" 221 | ] 222 | } 223 | ], 224 | "source": [ 225 | "optimize!(m)" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": 212, 231 | "id": "de4c7066-f42b-4a08-b418-122b68542f62", 232 | "metadata": {}, 233 | "outputs": [ 234 | { 235 | "data": { 236 | "text/plain": [ 237 | "6-element Vector{Float64}:\n", 238 | " 0.16666666666666669\n", 239 | " 0.16666666666666669\n", 240 | " 0.16666666666666669\n", 241 | " 0.16666666666666669\n", 242 | " 0.16666666666666669\n", 243 | " 0.16666666666666669" 244 | ] 245 | }, 246 | "execution_count": 212, 247 | "metadata": {}, 248 | "output_type": "execute_result" 249 | } 250 | ], 251 | "source": [ 252 | "value.(p)" 253 | ] 254 | }, 255 | { 256 | "cell_type": "markdown", 257 | "id": "a1f67070-0020-4111-a192-a81d97c01bbf", 258 | "metadata": {}, 259 | "source": [ 260 | "### Maximum Entropy For a Single Dice" 261 | ] 262 | }, 263 | { 264 | "cell_type": "markdown", 265 | "id": "b974fd9c-f06b-4bf4-bf71-aec34303158b", 266 | "metadata": {}, 267 | "source": [ 268 | "$$\n", 269 | "\\begin{aligned}\n", 270 | "\\max z & = - \\sum_{i=1}^{n} p_i \\times \\log p_i \\\\\n", 271 | "\\text{Subject to:} \\\\\n", 272 | "& \\sum_{i=1}^{n} p_i = 1 \\\\\n", 273 | "& 0 \\le p_i \\le 1 \\\\\n", 274 | "& i = 1, 2, \\ldots, 6 \\\\\n", 275 | "\\end{aligned}\n", 276 | "$$" 277 | ] 278 | }, 279 | { 280 | "cell_type": "markdown", 281 | "id": "1544429e-8a06-4ce4-a8a5-9be55537744a", 282 | "metadata": {}, 283 | "source": [ 284 | "### Solution" 285 | ] 286 | }, 287 | { 288 | "cell_type": "markdown", 289 | "id": "4b310fef-e46a-4597-bf6c-969fde1ed1d0", 290 | "metadata": {}, 291 | "source": [ 292 | "Entropy is maximum when $p_i = \\frac{1}{6}$ for $i = 1,2, \\ldots, 6$." 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": null, 298 | "id": "3919e1c7-349a-4794-882a-65be27086310", 299 | "metadata": {}, 300 | "outputs": [], 301 | "source": [] 302 | } 303 | ], 304 | "metadata": { 305 | "kernelspec": { 306 | "display_name": "Julia 1.7.2", 307 | "language": "julia", 308 | "name": "julia-1.7" 309 | }, 310 | "language_info": { 311 | "file_extension": ".jl", 312 | "mimetype": "application/julia", 313 | "name": "julia", 314 | "version": "1.7.2" 315 | } 316 | }, 317 | "nbformat": 4, 318 | "nbformat_minor": 5 319 | } 320 | -------------------------------------------------------------------------------- /flux-xor-nn.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "ee83fc74", 6 | "metadata": {}, 7 | "source": [ 8 | "Example has been taken:\n", 9 | "- https://discourse.julialang.org/t/julia-flux-how-to-build-a-simple-multilayer-perception-to-solve-xor-problem/48901/5" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "id": "8c908243-404c-4f3c-8262-3caaa57b5eba", 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "using Flux" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 2, 25 | "id": "ee185cb3-71ab-48dc-a2bc-b6f9b5e40b5d", 26 | "metadata": {}, 27 | "outputs": [ 28 | { 29 | "data": { 30 | "text/plain": [ 31 | "1×4 Matrix{Float32}:\n", 32 | " 0.0 1.0 1.0 0.0" 33 | ] 34 | }, 35 | "execution_count": 2, 36 | "metadata": {}, 37 | "output_type": "execute_result" 38 | } 39 | ], 40 | "source": [ 41 | "X = Float32[\n", 42 | " 0.0 0 1 1; \n", 43 | " 0.0 1 0 1\n", 44 | " ]\n", 45 | "y = Float32[0.0 1 1 0]" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 3, 51 | "id": "44187413-927b-42f7-a2b4-87f5801b90c5", 52 | "metadata": {}, 53 | "outputs": [ 54 | { 55 | "data": { 56 | "text/plain": [ 57 | "Chain(Dense(2, 3, σ), Dense(3, 1, σ))" 58 | ] 59 | }, 60 | "execution_count": 3, 61 | "metadata": {}, 62 | "output_type": "execute_result" 63 | } 64 | ], 65 | "source": [ 66 | "model = Chain(\n", 67 | " Dense(2, 3, Flux.sigmoid),\n", 68 | " Dense(3, 1, Flux.sigmoid)\n", 69 | ")" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 4, 75 | "id": "e438a06e-9ebc-40e3-9288-c6544fadbf49", 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "data": { 80 | "text/plain": [ 81 | "loss_fn (generic function with 1 method)" 82 | ] 83 | }, 84 | "execution_count": 4, 85 | "metadata": {}, 86 | "output_type": "execute_result" 87 | } 88 | ], 89 | "source": [ 90 | "loss_fn(x, y) = Flux.mse(model(x), y)" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 5, 96 | "id": "978188ed-4d4a-4aa4-9adf-7f731cb2dc35", 97 | "metadata": {}, 98 | "outputs": [ 99 | { 100 | "data": { 101 | "text/plain": [ 102 | "ADAM(0.1, (0.9, 0.999), IdDict{Any, Any}())" 103 | ] 104 | }, 105 | "execution_count": 5, 106 | "metadata": {}, 107 | "output_type": "execute_result" 108 | } 109 | ], 110 | "source": [ 111 | "opt = Flux.ADAM(0.1)" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 7, 117 | "id": "cd798cb8-b7a7-40af-8b41-e7af44358160", 118 | "metadata": {}, 119 | "outputs": [ 120 | { 121 | "name": "stdout", 122 | "output_type": "stream", 123 | "text": [ 124 | "0.00011575997\n", 125 | "3.301706e-5\n", 126 | "1.4382855e-5\n", 127 | "7.329355e-6\n", 128 | "4.027235e-6\n", 129 | "2.3041612e-6\n", 130 | "1.3489207e-6\n", 131 | "8.0038114e-7\n", 132 | "4.788012e-7\n", 133 | "2.8784052e-7\n" 134 | ] 135 | } 136 | ], 137 | "source": [ 138 | "N = 10000\n", 139 | "loss = zeros(Float32, N)\n", 140 | "for i in 1:N\n", 141 | " Flux.train!(loss_fn, params(model), [(X, y)], opt)\n", 142 | " loss[i] = loss_fn(X, y).data\n", 143 | " if i % 1000 == 0\n", 144 | " println(loss[i])\n", 145 | " end\n", 146 | "end " 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 17, 152 | "id": "710ec401-5338-4d5a-8050-730b885baac8", 153 | "metadata": {}, 154 | "outputs": [ 155 | { 156 | "data": { 157 | "text/plain": [ 158 | "1×4 Matrix{Float32}:\n", 159 | " 0.000296875 0.999411 0.999405 0.000601562" 160 | ] 161 | }, 162 | "execution_count": 17, 163 | "metadata": {}, 164 | "output_type": "execute_result" 165 | } 166 | ], 167 | "source": [ 168 | "model(X).data" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": 9, 174 | "id": "1b6e53c5-c1ad-4461-bd66-af1b0c49913d", 175 | "metadata": {}, 176 | "outputs": [ 177 | { 178 | "data": { 179 | "text/plain": [ 180 | "1×4 Matrix{Float32}:\n", 181 | " 0.0 1.0 1.0 0.0" 182 | ] 183 | }, 184 | "execution_count": 9, 185 | "metadata": {}, 186 | "output_type": "execute_result" 187 | } 188 | ], 189 | "source": [ 190 | "y" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": null, 196 | "id": "eee1e3b8-6657-4fea-966d-abbc98a4ac7f", 197 | "metadata": {}, 198 | "outputs": [], 199 | "source": [] 200 | } 201 | ], 202 | "metadata": { 203 | "kernelspec": { 204 | "display_name": "Julia 1.7.1", 205 | "language": "julia", 206 | "name": "julia-1.7" 207 | }, 208 | "language_info": { 209 | "file_extension": ".jl", 210 | "mimetype": "application/julia", 211 | "name": "julia", 212 | "version": "1.7.1" 213 | } 214 | }, 215 | "nbformat": 4, 216 | "nbformat_minor": 5 217 | } 218 | -------------------------------------------------------------------------------- /game-stone-paper-scissors.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "e386818e-0314-4111-9de9-06d35e98a8b3", 6 | "metadata": {}, 7 | "source": [ 8 | "## Stone-Paper-Scissors Game" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "2d2e6b45-a01a-4f38-a044-9ced5a2fe18b", 14 | "metadata": {}, 15 | "source": [ 16 | "### Payoff matrix" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "id": "eb62df96-a8d2-4593-b3ea-72799b9b864a", 22 | "metadata": {}, 23 | "source": [ 24 | "| | Stone | Paper | Scissors |\n", 25 | "| :----: | :---------: | :---------: | :--------: |\n", 26 | "| Stone | 0 | -1 | 1 |\n", 27 | "| Paper | 1 | 0 | -1 |\n", 28 | "| Scissors| -1 | 1 | 0 |" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "id": "6b3ff495-b7bb-4c64-89a7-2f34ae0e01c8", 34 | "metadata": {}, 35 | "source": [ 36 | "### Payoff matrix with non-negative values " 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "id": "8aa31010-f280-4c22-969a-eb28e02fddae", 42 | "metadata": {}, 43 | "source": [ 44 | "| | Stone | Paper | Scissors |\n", 45 | "| :----: | :---------: | :---------: | :--------: |\n", 46 | "| Stone | 1 | 0 | 2 |\n", 47 | "| Paper | 2 | 1 | 0 |\n", 48 | "| Scissors| 0 | 2 | 1 |" 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "id": "d81a5e14-c217-4e6c-9614-7941c1490a28", 54 | "metadata": {}, 55 | "source": [ 56 | "### The model" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "id": "d8c4c758-d321-4bce-b54f-3234ca5199e1", 62 | "metadata": {}, 63 | "source": [ 64 | "$$\n", 65 | "\\begin{aligned}\n", 66 | "\\max z & = g \\\\\n", 67 | "\\text{Subject to:} \\\\ \n", 68 | "& y_1 + 2 y_2 \\ge g \\\\\n", 69 | "& y_2 + 2 y_3 \\ge g \\\\\n", 70 | "& 2y_1 + y_3 \\ge g \\\\\n", 71 | "& y_1 + y_2 + y_3 = 1 \\\\\n", 72 | "& y_1, y_2, y_3, g \\ge 0\n", 73 | "\\end{aligned}\n", 74 | "$$" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 1, 80 | "id": "7d7f4bab-3bdb-4595-8169-1e60b636e47b", 81 | "metadata": {}, 82 | "outputs": [], 83 | "source": [ 84 | "using JuMP" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": 2, 90 | "id": "c4737897-a5f3-4c99-b3fc-5a8c3df1b07c", 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [ 94 | "using GLPK" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 4, 100 | "id": "ad4fd56d-1ed5-4dde-9a3a-e090a007f2d7", 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "data": { 105 | "text/plain": [ 106 | "A JuMP Model\n", 107 | "Feasibility problem with:\n", 108 | "Variables: 0\n", 109 | "Model mode: AUTOMATIC\n", 110 | "CachingOptimizer state: EMPTY_OPTIMIZER\n", 111 | "Solver name: GLPK" 112 | ] 113 | }, 114 | "execution_count": 4, 115 | "metadata": {}, 116 | "output_type": "execute_result" 117 | } 118 | ], 119 | "source": [ 120 | "model = Model(GLPK.Optimizer)" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 5, 126 | "id": "957467d7-fda2-4e0d-b95e-985de9102c4a", 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [ 130 | "@variable(model, y1);\n", 131 | "@variable(model, y2);\n", 132 | "@variable(model, y3);\n", 133 | "@variable(model, g);" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 7, 139 | "id": "9ee4d085-127b-41d4-aa3e-c99d16ed58e1", 140 | "metadata": {}, 141 | "outputs": [], 142 | "source": [ 143 | "@objective(model, Max, g);" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 8, 149 | "id": "5a242473-b8c0-4fde-9fbf-b2a806e7ba72", 150 | "metadata": {}, 151 | "outputs": [], 152 | "source": [ 153 | "@constraint(model, y1 + 2y2 >= g);\n", 154 | "@constraint(model, y2 + 2y3 >= g);\n", 155 | "@constraint(model, 2y1 + y3 >= g);\n", 156 | "@constraint(model, y1 + y2 + y3 == 1);" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 10, 162 | "id": "4360587a-03a4-4b8f-947f-90c2cf5969fc", 163 | "metadata": {}, 164 | "outputs": [], 165 | "source": [ 166 | "@constraint(model, y1 >= 0);\n", 167 | "@constraint(model, y2 >= 0);\n", 168 | "@constraint(model, y3 >= 0);\n", 169 | "@constraint(model, g >= 0);" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 12, 175 | "id": "a0136f8a-6aad-4c7e-9946-dd582d6d462f", 176 | "metadata": {}, 177 | "outputs": [], 178 | "source": [ 179 | "optimize!(model)" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": 15, 185 | "id": "02352703-f385-4395-9982-19da63270def", 186 | "metadata": {}, 187 | "outputs": [ 188 | { 189 | "data": { 190 | "text/plain": [ 191 | "0.3333333333333334" 192 | ] 193 | }, 194 | "execution_count": 15, 195 | "metadata": {}, 196 | "output_type": "execute_result" 197 | } 198 | ], 199 | "source": [ 200 | "value(y1)" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": 16, 206 | "id": "3005ecfd-9325-498d-8c2e-12834d08fd9e", 207 | "metadata": {}, 208 | "outputs": [ 209 | { 210 | "data": { 211 | "text/plain": [ 212 | "0.3333333333333333" 213 | ] 214 | }, 215 | "execution_count": 16, 216 | "metadata": {}, 217 | "output_type": "execute_result" 218 | } 219 | ], 220 | "source": [ 221 | "value(y2)" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": 17, 227 | "id": "f437caaf-d72b-4673-a01b-d69378bd23f0", 228 | "metadata": {}, 229 | "outputs": [ 230 | { 231 | "data": { 232 | "text/plain": [ 233 | "0.3333333333333333" 234 | ] 235 | }, 236 | "execution_count": 17, 237 | "metadata": {}, 238 | "output_type": "execute_result" 239 | } 240 | ], 241 | "source": [ 242 | "value(y3)" 243 | ] 244 | }, 245 | { 246 | "cell_type": "code", 247 | "execution_count": 19, 248 | "id": "27f8958c-bf1d-4e8d-8b55-d76f9d6b8fbe", 249 | "metadata": {}, 250 | "outputs": [ 251 | { 252 | "data": { 253 | "text/plain": [ 254 | "1.0" 255 | ] 256 | }, 257 | "execution_count": 19, 258 | "metadata": {}, 259 | "output_type": "execute_result" 260 | } 261 | ], 262 | "source": [ 263 | "value(g)" 264 | ] 265 | }, 266 | { 267 | "cell_type": "markdown", 268 | "id": "8bee9be1-7fc4-47d7-a7e7-6499b3c8322d", 269 | "metadata": {}, 270 | "source": [ 271 | "Since we have added 1 to each single element of the payoff matrix, the value of the game is" 272 | ] 273 | }, 274 | { 275 | "cell_type": "code", 276 | "execution_count": 20, 277 | "id": "f1f14adc-7f58-4645-8e90-2c6da6bd40c2", 278 | "metadata": {}, 279 | "outputs": [ 280 | { 281 | "data": { 282 | "text/plain": [ 283 | "0.0" 284 | ] 285 | }, 286 | "execution_count": 20, 287 | "metadata": {}, 288 | "output_type": "execute_result" 289 | } 290 | ], 291 | "source": [ 292 | "value(g) - 1" 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": null, 298 | "id": "15c4f066-1e44-47fc-ab0d-937de71c1e49", 299 | "metadata": {}, 300 | "outputs": [], 301 | "source": [] 302 | } 303 | ], 304 | "metadata": { 305 | "kernelspec": { 306 | "display_name": "Julia 1.7.2", 307 | "language": "julia", 308 | "name": "julia-1.7" 309 | }, 310 | "language_info": { 311 | "file_extension": ".jl", 312 | "mimetype": "application/julia", 313 | "name": "julia", 314 | "version": "1.7.2" 315 | } 316 | }, 317 | "nbformat": 4, 318 | "nbformat_minor": 5 319 | } 320 | -------------------------------------------------------------------------------- /german-tank-problem.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 6, 6 | "id": "94c6dd9e", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "data": { 11 | "text/plain": [ 12 | "nestimate (generic function with 1 method)" 13 | ] 14 | }, 15 | "execution_count": 6, 16 | "metadata": {}, 17 | "output_type": "execute_result" 18 | } 19 | ], 20 | "source": [ 21 | "using StatsBase\n", 22 | "function nestimate(sampl::AbstractArray; method = \"mvue\")::Float64\n", 23 | "\n", 24 | " function mvue(sampl::AbstractArray)::Float64\n", 25 | " mx = maximum(sampl)\n", 26 | " estimate = mx + mx / length(sampl) - 1.0\n", 27 | " return estimate\n", 28 | " end\n", 29 | "\n", 30 | " function mom(sampl::AbstractArray)::Float64\n", 31 | " return 2.0 * mean(sampl) - 1.0\n", 32 | " end\n", 33 | "\n", 34 | " if method == \"mvue\"\n", 35 | " return mvue(sampl)\n", 36 | " elseif method == \"mom\"\n", 37 | " return mom(sampl)\n", 38 | " else\n", 39 | " throw(ArgumentError(\"Unknown method: $method\"))\n", 40 | " end\n", 41 | "end" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "id": "45acce99", 47 | "metadata": {}, 48 | "source": [ 49 | "### Minimum Variance Estimator" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "id": "4ca038d4", 55 | "metadata": {}, 56 | "source": [ 57 | "$$\n", 58 | "\\hat{n} = \\max{x} + \\frac{\\max{x}}{n} - 1\n", 59 | "$$" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "id": "8026063e", 65 | "metadata": {}, 66 | "source": [ 67 | "### Method of Moments Estimator" 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "id": "22049072", 73 | "metadata": {}, 74 | "source": [ 75 | "$$\n", 76 | "\\hat{n} = 2 \\frac{\\Sigma{x}}{n} - 1\n", 77 | "$$" 78 | ] 79 | }, 80 | { 81 | "cell_type": "markdown", 82 | "id": "54f19e41", 83 | "metadata": {}, 84 | "source": [ 85 | "### Example" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 12, 91 | "id": "6efc0132", 92 | "metadata": {}, 93 | "outputs": [ 94 | { 95 | "data": { 96 | "text/plain": [ 97 | "10-element Vector{Int64}:\n", 98 | " 34\n", 99 | " 59\n", 100 | " 22\n", 101 | " 39\n", 102 | " 77\n", 103 | " 41\n", 104 | " 16\n", 105 | " 10\n", 106 | " 54\n", 107 | " 11" 108 | ] 109 | }, 110 | "execution_count": 12, 111 | "metadata": {}, 112 | "output_type": "execute_result" 113 | } 114 | ], 115 | "source": [ 116 | "x = [34, 59, 22, 39, 77, 41, 16, 10, 54, 11]" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 13, 122 | "id": "fcdc18ac", 123 | "metadata": {}, 124 | "outputs": [ 125 | { 126 | "data": { 127 | "text/plain": [ 128 | "83.7" 129 | ] 130 | }, 131 | "execution_count": 13, 132 | "metadata": {}, 133 | "output_type": "execute_result" 134 | } 135 | ], 136 | "source": [ 137 | "nestimate(x, method = \"mvue\")" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 14, 143 | "id": "ab29172e", 144 | "metadata": {}, 145 | "outputs": [ 146 | { 147 | "data": { 148 | "text/plain": [ 149 | "71.6" 150 | ] 151 | }, 152 | "execution_count": 14, 153 | "metadata": {}, 154 | "output_type": "execute_result" 155 | } 156 | ], 157 | "source": [ 158 | "nestimate(x, method = \"mom\")" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": null, 164 | "id": "498c00ab", 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [] 168 | } 169 | ], 170 | "metadata": { 171 | "kernelspec": { 172 | "display_name": "Julia 1.9.0-rc2", 173 | "language": "julia", 174 | "name": "julia-1.9" 175 | }, 176 | "language_info": { 177 | "file_extension": ".jl", 178 | "mimetype": "application/julia", 179 | "name": "julia", 180 | "version": "1.9.0" 181 | } 182 | }, 183 | "nbformat": 4, 184 | "nbformat_minor": 5 185 | } 186 | -------------------------------------------------------------------------------- /gradient-descent.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "2e9d7e29", 6 | "metadata": {}, 7 | "source": [ 8 | "# Gradient Descent" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "b56fea23", 14 | "metadata": {}, 15 | "source": [ 16 | "$$\n", 17 | "\\min z = f(x, y) = (x - \\pi)^2 + (y - e)^2\n", 18 | "$$" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 1, 24 | "id": "158cd9c7", 25 | "metadata": {}, 26 | "outputs": [ 27 | { 28 | "data": { 29 | "text/plain": [ 30 | "f (generic function with 1 method)" 31 | ] 32 | }, 33 | "execution_count": 1, 34 | "metadata": {}, 35 | "output_type": "execute_result" 36 | } 37 | ], 38 | "source": [ 39 | "function f(w)\n", 40 | " return (w[1] - 3.14159265)^2 + (w[2] - exp(1))^2\n", 41 | "end " 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "id": "beebe076", 47 | "metadata": {}, 48 | "source": [ 49 | "### Load the required package for gradients" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 2, 55 | "id": "4d31b405", 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "using ForwardDiff" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "id": "e47c56db", 65 | "metadata": {}, 66 | "source": [ 67 | "### Playground" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 3, 73 | "id": "b132299e", 74 | "metadata": {}, 75 | "outputs": [ 76 | { 77 | "name": "stdout", 78 | "output_type": "stream", 79 | "text": [ 80 | "Convergen in 74 iters. Breaking\n", 81 | "[3.141592385321343, 2.718281599444254]\n" 82 | ] 83 | } 84 | ], 85 | "source": [ 86 | "eta = 0.1\n", 87 | "epsilon = 10^(-7)\n", 88 | "initial = [0.0, 0.0]\n", 89 | "for i in 1:100\n", 90 | " newinitial = initial - ForwardDiff.gradient(f, initial) * eta\n", 91 | " if sum(abs.(newinitial - initial)) < epsilon\n", 92 | " println(\"Convergen in $i iters. Breaking\")\n", 93 | " break\n", 94 | " end \n", 95 | " initial = newinitial\n", 96 | "end \n", 97 | "println(initial)" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "id": "e7dde07f", 103 | "metadata": {}, 104 | "source": [ 105 | "### Packing calculations using a function" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 4, 111 | "id": "f3ad96c6", 112 | "metadata": {}, 113 | "outputs": [ 114 | { 115 | "data": { 116 | "text/plain": [ 117 | "gradientdescent (generic function with 1 method)" 118 | ] 119 | }, 120 | "execution_count": 4, 121 | "metadata": {}, 122 | "output_type": "execute_result" 123 | } 124 | ], 125 | "source": [ 126 | "function gradientdescent(f, initialpoint; eta = 0.1, epsilon = 10^(-7), maxiter = 100)\n", 127 | " for i in 1:maxiter\n", 128 | " newinitial = initialpoint - ForwardDiff.gradient(f, initialpoint) * eta\n", 129 | " if sum(abs.(newinitial - initialpoint)) < epsilon\n", 130 | " println(\"Convergen in $i iters. Breaking\")\n", 131 | " break\n", 132 | " end \n", 133 | " initialpoint = newinitial\n", 134 | " end\n", 135 | " return initialpoint\n", 136 | "end " 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 5, 142 | "id": "4663a4c9", 143 | "metadata": {}, 144 | "outputs": [ 145 | { 146 | "name": "stdout", 147 | "output_type": "stream", 148 | "text": [ 149 | "Convergen in 74 iters. Breaking\n" 150 | ] 151 | }, 152 | { 153 | "data": { 154 | "text/plain": [ 155 | "2-element Vector{Float64}:\n", 156 | " 3.141592385321343\n", 157 | " 2.718281599444254" 158 | ] 159 | }, 160 | "execution_count": 5, 161 | "metadata": {}, 162 | "output_type": "execute_result" 163 | } 164 | ], 165 | "source": [ 166 | "gradientdescent(f, [0.0, 0.0])" 167 | ] 168 | }, 169 | { 170 | "cell_type": "markdown", 171 | "id": "d0d27273", 172 | "metadata": {}, 173 | "source": [ 174 | "### A more complex function" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": 6, 180 | "id": "ca982df3", 181 | "metadata": {}, 182 | "outputs": [ 183 | { 184 | "data": { 185 | "text/plain": [ 186 | "myf (generic function with 1 method)" 187 | ] 188 | }, 189 | "execution_count": 6, 190 | "metadata": {}, 191 | "output_type": "execute_result" 192 | } 193 | ], 194 | "source": [ 195 | "myf(w) = (w[1] - 7.0)^2 + (w[2] - 77.0)^2 + (w[3] - 777.0)^2 + (w[4] - 7777.0)^2" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": 7, 201 | "id": "cbb8e427", 202 | "metadata": {}, 203 | "outputs": [ 204 | { 205 | "data": { 206 | "text/plain": [ 207 | "4-element Vector{Float64}:\n", 208 | " 100.0\n", 209 | " 100.0\n", 210 | " 100.0\n", 211 | " 100.0" 212 | ] 213 | }, 214 | "execution_count": 7, 215 | "metadata": {}, 216 | "output_type": "execute_result" 217 | } 218 | ], 219 | "source": [ 220 | "initial = [100.0 for i in 1:4]" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 8, 226 | "id": "caee73f9", 227 | "metadata": {}, 228 | "outputs": [ 229 | { 230 | "name": "stdout", 231 | "output_type": "stream", 232 | "text": [ 233 | "Convergen in 107 iters. Breaking\n" 234 | ] 235 | }, 236 | { 237 | "data": { 238 | "text/plain": [ 239 | "4-element Vector{Float64}:\n", 240 | " 7.00000000496617\n", 241 | " 77.0000000012282\n", 242 | " 776.9999999638484\n", 243 | " 7776.999999590051" 244 | ] 245 | }, 246 | "execution_count": 8, 247 | "metadata": {}, 248 | "output_type": "execute_result" 249 | } 250 | ], 251 | "source": [ 252 | "gradientdescent(myf, initial, maxiter = 1000, eta = 0.1)" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": null, 258 | "id": "f893c890", 259 | "metadata": {}, 260 | "outputs": [], 261 | "source": [] 262 | } 263 | ], 264 | "metadata": { 265 | "kernelspec": { 266 | "display_name": "Julia 1.9Beta4 1.9.0-beta4", 267 | "language": "julia", 268 | "name": "julia-1.9beta4-1.9" 269 | }, 270 | "language_info": { 271 | "file_extension": ".jl", 272 | "mimetype": "application/julia", 273 | "name": "julia", 274 | "version": "1.9.0" 275 | } 276 | }, 277 | "nbformat": 4, 278 | "nbformat_minor": 5 279 | } 280 | -------------------------------------------------------------------------------- /hooke-jeeves.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "9b2e406a-8298-4c1c-adb9-4be0aeee0f36", 6 | "metadata": {}, 7 | "source": [ 8 | "# Hooke-Jeeves Pattern Search" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 3, 14 | "id": "17c58f05-04a0-4734-bfd5-8e3e10beec88", 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "text/plain": [ 20 | "mutate (generic function with 1 method)" 21 | ] 22 | }, 23 | "execution_count": 3, 24 | "metadata": {}, 25 | "output_type": "execute_result" 26 | } 27 | ], 28 | "source": [ 29 | "function mutate(par, p, d)\n", 30 | " newpar = copy(par)\n", 31 | " newpar[p] += d\n", 32 | " return newpar\n", 33 | "end" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 2, 39 | "id": "6e73000e-2b45-49cf-9728-2f2470b074ff", 40 | "metadata": {}, 41 | "outputs": [ 42 | { 43 | "data": { 44 | "text/plain": [ 45 | "hj (generic function with 1 method)" 46 | ] 47 | }, 48 | "execution_count": 2, 49 | "metadata": {}, 50 | "output_type": "execute_result" 51 | } 52 | ], 53 | "source": [ 54 | "function hj(\n", 55 | " f::Function,\n", 56 | " par::Vector{Float64};\n", 57 | " maxiter = 1000,\n", 58 | " startstep = 5.0,\n", 59 | " endstep = 0.0001,\n", 60 | ")\n", 61 | " p = length(par)\n", 62 | " currentstep = startstep\n", 63 | " iter::Int64 = 0\n", 64 | " while iter < maxiter\n", 65 | " fold = f(par)\n", 66 | " fnow = fold\n", 67 | " for currentp = 1:p\n", 68 | " mutateleft = mutate(par, currentp, -currentstep)\n", 69 | " fleft = f(mutateleft)\n", 70 | " mutateright = mutate(par, currentp, currentstep)\n", 71 | " fright = f(mutateright)\n", 72 | " if fleft < fold\n", 73 | " par = mutateleft\n", 74 | " fnow = fleft\n", 75 | " elseif fright < fold\n", 76 | " par = mutateright\n", 77 | " fnow = fright\n", 78 | " end\n", 79 | " end\n", 80 | " if fold <= fnow\n", 81 | " currentstep /= 2\n", 82 | " end\n", 83 | " if currentstep < endstep\n", 84 | " break\n", 85 | " end\n", 86 | " iter += 1\n", 87 | " end\n", 88 | "\n", 89 | " return Dict(\"par\" => par, \"iter\" => iter, \"step\" => currentstep)\n", 90 | "end" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 4, 96 | "id": "b26843cd-f84e-43bd-8289-7b2d7c325341", 97 | "metadata": {}, 98 | "outputs": [ 99 | { 100 | "data": { 101 | "text/plain": [ 102 | "f (generic function with 1 method)" 103 | ] 104 | }, 105 | "execution_count": 4, 106 | "metadata": {}, 107 | "output_type": "execute_result" 108 | } 109 | ], 110 | "source": [ 111 | "function f(x::Vector{Float64})::Float64\n", 112 | " return (x[1] - 3.14159265)^2 + (x[2] - 2.71828183)^2\n", 113 | "end" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": 5, 119 | "id": "d3d5c649-392f-499d-806f-34234425b2d2", 120 | "metadata": {}, 121 | "outputs": [ 122 | { 123 | "data": { 124 | "text/plain": [ 125 | "Dict{String, Any} with 3 entries:\n", 126 | " \"par\" => [3.14163, 2.71835]\n", 127 | " \"step\" => 7.62939e-5\n", 128 | " \"iter\" => 31" 129 | ] 130 | }, 131 | "execution_count": 5, 132 | "metadata": {}, 133 | "output_type": "execute_result" 134 | } 135 | ], 136 | "source": [ 137 | "hj(f, [0.0, 0.0])" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 6, 143 | "id": "6d581118-c3a0-4f8c-a038-b2a7dbc96ee5", 144 | "metadata": {}, 145 | "outputs": [ 146 | { 147 | "data": { 148 | "text/plain": [ 149 | "f10 (generic function with 1 method)" 150 | ] 151 | }, 152 | "execution_count": 6, 153 | "metadata": {}, 154 | "output_type": "execute_result" 155 | } 156 | ], 157 | "source": [ 158 | "function f10(x::Vector{Float64})::Float64\n", 159 | " s = 0.0\n", 160 | " for i in x\n", 161 | " s += (i - 10.0)^2\n", 162 | " end\n", 163 | " return s\n", 164 | "end" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 13, 170 | "id": "054fbe4d-42d8-439d-8948-8704d93208c4", 171 | "metadata": {}, 172 | "outputs": [ 173 | { 174 | "data": { 175 | "text/plain": [ 176 | "Dict{String, Any} with 3 entries:\n", 177 | " \"par\" => [10.0, 10.0, 10.0, 10.0, 10.0]\n", 178 | " \"step\" => 7.45058e-8\n", 179 | " \"iter\" => 79" 180 | ] 181 | }, 182 | "execution_count": 13, 183 | "metadata": {}, 184 | "output_type": "execute_result" 185 | } 186 | ], 187 | "source": [ 188 | "hj(f10, [rand() for i in 1:5], endstep = 0.0000001)" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": null, 194 | "id": "ee723c7a-fd94-4f56-8c7a-73c91c505a14", 195 | "metadata": {}, 196 | "outputs": [], 197 | "source": [] 198 | } 199 | ], 200 | "metadata": { 201 | "kernelspec": { 202 | "display_name": "Julia 1.9.0-DEV", 203 | "language": "julia", 204 | "name": "julia-1.9" 205 | }, 206 | "language_info": { 207 | "file_extension": ".jl", 208 | "mimetype": "application/julia", 209 | "name": "julia", 210 | "version": "1.9.0" 211 | } 212 | }, 213 | "nbformat": 4, 214 | "nbformat_minor": 5 215 | } 216 | -------------------------------------------------------------------------------- /kuhn-tucker-for-linear-problem.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "7335dfe3-574e-4df1-978e-531f49c1aa32", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "using NLsolve" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "id": "ae605b46-a2d8-4470-b569-5202ff0cdba7", 16 | "metadata": {}, 17 | "source": [ 18 | "$$\n", 19 | "\\begin{aligned}\n", 20 | "\\max z & = 3x + 5y \\\\\n", 21 | "\\text{Subject to:} \\\\\n", 22 | "& 5x + 10y \\le 100 \\\\\n", 23 | "& x \\ge 0 \\\\\n", 24 | "& y \\ge 0\n", 25 | "\\end{aligned}\n", 26 | "$$" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "id": "ec754c02-6698-4a12-adf6-eaeffad7330f", 32 | "metadata": {}, 33 | "source": [ 34 | "$$\n", 35 | "\\begin{aligned}\n", 36 | "\\max z & = 3x + 5y \\\\\n", 37 | "\\text{Subject to:} \\\\\n", 38 | "& 5x + 10y \\le 100 \\\\\n", 39 | "& -x \\le 0 \\\\\n", 40 | "& -y \\ge 0\n", 41 | "\\end{aligned}\n", 42 | "$$" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "id": "152bea7c-2a2f-47c4-a7a4-7f893f99c1f4", 48 | "metadata": {}, 49 | "source": [ 50 | "_________________" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "id": "6b814aa8-7561-437d-9d78-3e9dbb79f98f", 56 | "metadata": {}, 57 | "source": [ 58 | "- $3 - \\lambda_1 5 - \\lambda_2 (-1) = 0$\n", 59 | "- $5 - \\lambda_1 10 - \\lambda_3 (-1) = 0$\n", 60 | "- $\\lambda_1 (100 - 5x - 10y) = 0$\n", 61 | "- $\\lambda_2 (0 + x) = 0$\n", 62 | "- $\\lambda_3 (0 + y) = 0$" 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "id": "adb13242-bb7c-48d1-b96e-e4748036b67d", 68 | "metadata": {}, 69 | "source": [ 70 | "_____________________" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 22, 76 | "id": "02b5b411-ef19-4963-b275-662cd94605d3", 77 | "metadata": {}, 78 | "outputs": [ 79 | { 80 | "data": { 81 | "text/plain": [ 82 | "f! (generic function with 1 method)" 83 | ] 84 | }, 85 | "execution_count": 22, 86 | "metadata": {}, 87 | "output_type": "execute_result" 88 | } 89 | ], 90 | "source": [ 91 | "# x, y, l1, l2, l3\n", 92 | "function f!(F, x)\n", 93 | " F[1] = 3 - x[3] * 5 - x[4] * (-1)\n", 94 | " F[2] = 5 - x[3] * 10 - x[5] * (-1)\n", 95 | " F[3] = x[3] * (100 - 5 * x[1] - 10 * x[2])\n", 96 | " F[4] = x[4] * x[1]\n", 97 | " F[5] = x[5] * x[2]\n", 98 | "end" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 35, 104 | "id": "82dbbd86-73f3-469a-8f70-927bdec79dac", 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "data": { 109 | "text/plain": [ 110 | "Results of Nonlinear Solver Algorithm\n", 111 | " * Algorithm: Trust-region with dogleg and autoscaling\n", 112 | " * Starting Point: [1.0, 1.0, 1.0, 1.0, 1.0]\n", 113 | " * Zero: [20.000000000018446, -9.22409941498867e-12, 0.6000000000000922, 4.612599222024691e-13, 1.0000000000009226]\n", 114 | " * Inf-norm of residuals: 0.000000\n", 115 | " * Iterations: 10\n", 116 | " * Convergence: true\n", 117 | " * |x - x'| < 0.0e+00: false\n", 118 | " * |f(x)| < 1.0e-08: true\n", 119 | " * Function Calls (f): 11\n", 120 | " * Jacobian Calls (df/dx): 9" 121 | ] 122 | }, 123 | "execution_count": 35, 124 | "metadata": {}, 125 | "output_type": "execute_result" 126 | } 127 | ], 128 | "source": [ 129 | "initial_x = [1.0, 1.0, 1.0, 1.0, 1.0]\n", 130 | "\n", 131 | "nlsolve(f!, initial_x, autodiff = :forward)" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": 28, 137 | "id": "9e63f198-7806-41ab-bb3b-7650b44f8a17", 138 | "metadata": {}, 139 | "outputs": [], 140 | "source": [ 141 | "using JuMP" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 29, 147 | "id": "904af74f-43c7-49e9-8289-36e680537b85", 148 | "metadata": {}, 149 | "outputs": [], 150 | "source": [ 151 | "using GLPK" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": 33, 157 | "id": "863acc18-3d03-4372-8eee-5f26dece54c3", 158 | "metadata": {}, 159 | "outputs": [ 160 | { 161 | "name": "stdout", 162 | "output_type": "stream", 163 | "text": [ 164 | "20.0\n", 165 | "0.0\n" 166 | ] 167 | } 168 | ], 169 | "source": [ 170 | "m = Model(GLPK.Optimizer)\n", 171 | "@variable(m, x)\n", 172 | "@variable(m, y)\n", 173 | "@objective(m, Max, 3x + 5y)\n", 174 | "@constraint(m, 5x + 10y <= 100)\n", 175 | "@constraint(m, x >= 0)\n", 176 | "@constraint(m, y >= 0)\n", 177 | "optimize!(m)\n", 178 | "println(value(x))\n", 179 | "println(value(y))" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": null, 185 | "id": "effcb62f-3b01-4c32-8487-9d8bc1e2cf8c", 186 | "metadata": {}, 187 | "outputs": [], 188 | "source": [] 189 | } 190 | ], 191 | "metadata": { 192 | "kernelspec": { 193 | "display_name": "Julia 1.7.1", 194 | "language": "julia", 195 | "name": "julia-1.7" 196 | }, 197 | "language_info": { 198 | "file_extension": ".jl", 199 | "mimetype": "application/julia", 200 | "name": "julia", 201 | "version": "1.7.1" 202 | } 203 | }, 204 | "nbformat": 4, 205 | "nbformat_minor": 5 206 | } 207 | -------------------------------------------------------------------------------- /kuhn-tucker1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "b42748d1-0aef-48ee-a650-4537ff578672", 6 | "metadata": { 7 | "tags": [] 8 | }, 9 | "source": [ 10 | "# Kuhn-Tucker Example" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "id": "909a6034-d6d4-4bb9-a195-8f6e2c6a49cd", 16 | "metadata": {}, 17 | "source": [ 18 | "$$\n", 19 | "\\begin{aligned}\n", 20 | "\\max z & = x_1 - x_2 \\\\\n", 21 | "\\text{Subject to:} \\\\\n", 22 | "& x_1^2 + x_2^2 \\le 1 \n", 23 | "\\end{aligned}\n", 24 | "$$" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "id": "04968fb5-b2bb-4b95-8efb-9fab90ffaaae", 30 | "metadata": {}, 31 | "source": [ 32 | "_____________________" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "id": "c0a440d1-8fa2-46aa-a5e8-9ff12b54a4a1", 38 | "metadata": {}, 39 | "source": [ 40 | "- $1 - \\lambda (2x_1) = 0$\n", 41 | "- $-1 -\\lambda (2x_2) = 0$\n", 42 | "- $\\lambda(1 - x_1^2 - x_2^2) = 0$\n", 43 | "- $\\lambda \\ge 0$" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "id": "28135c45-ab7e-475c-90c5-1f7f2e912458", 49 | "metadata": {}, 50 | "source": [ 51 | "__________________________" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": null, 57 | "id": "e0c36ab6-fc90-45dd-8b2f-49656d8e728f", 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [ 61 | "using NLsolve" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 16, 67 | "id": "6fad32b9-e7db-412b-8522-cccc047f4a57", 68 | "metadata": {}, 69 | "outputs": [ 70 | { 71 | "data": { 72 | "text/plain": [ 73 | "f! (generic function with 1 method)" 74 | ] 75 | }, 76 | "execution_count": 16, 77 | "metadata": {}, 78 | "output_type": "execute_result" 79 | } 80 | ], 81 | "source": [ 82 | "function f!(F, x)\n", 83 | " F[1] = 1.0 - x[3] * 2 * x[1]\n", 84 | " F[2] = -1.0 - x[3] * 2 * x[2]\n", 85 | " F[3] = x[3] * (1 - x[1]^2 - x[2]^2)\n", 86 | "end " 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "id": "d56c3e1f-1bf6-40d7-9230-2cdf90f14694", 92 | "metadata": {}, 93 | "source": [ 94 | "If $\\lambda = 0$ then" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 18, 100 | "id": "1ea4b901-28f3-49e6-b14b-8aafadf3c36b", 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "data": { 105 | "text/plain": [ 106 | "Results of Nonlinear Solver Algorithm\n", 107 | " * Algorithm: Trust-region with dogleg and autoscaling\n", 108 | " * Starting Point: [0.0, 0.0, 0.0]\n", 109 | " * Zero: [NaN, NaN, NaN]\n", 110 | " * Inf-norm of residuals: 1.000000\n", 111 | " * Iterations: 1\n", 112 | " * Convergence: false\n", 113 | " * |x - x'| < 0.0e+00: false\n", 114 | " * |f(x)| < 1.0e-08: false\n", 115 | " * Function Calls (f): 2\n", 116 | " * Jacobian Calls (df/dx): 1" 117 | ] 118 | }, 119 | "execution_count": 18, 120 | "metadata": {}, 121 | "output_type": "execute_result" 122 | } 123 | ], 124 | "source": [ 125 | "initial_x = [0.0, 0.0, 0.0]\n", 126 | "\n", 127 | "nlsolve(f!, initial_x, autodiff = :forward)" 128 | ] 129 | }, 130 | { 131 | "cell_type": "markdown", 132 | "id": "340f32b9-8fb2-405f-9b45-956c34e3ff3e", 133 | "metadata": {}, 134 | "source": [ 135 | "no solutions! If $\\lambda \\ge 0$ then " 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 19, 141 | "id": "f289571e-04b3-40fd-abf0-7474324c41b3", 142 | "metadata": {}, 143 | "outputs": [ 144 | { 145 | "data": { 146 | "text/plain": [ 147 | "Results of Nonlinear Solver Algorithm\n", 148 | " * Algorithm: Trust-region with dogleg and autoscaling\n", 149 | " * Starting Point: [0.0, 0.0, 5.0]\n", 150 | " * Zero: [0.7071067811865467, -0.7071067811865467, 0.7071067811865471]\n", 151 | " * Inf-norm of residuals: 0.000000\n", 152 | " * Iterations: 9\n", 153 | " * Convergence: true\n", 154 | " * |x - x'| < 0.0e+00: false\n", 155 | " * |f(x)| < 1.0e-08: true\n", 156 | " * Function Calls (f): 9\n", 157 | " * Jacobian Calls (df/dx): 7" 158 | ] 159 | }, 160 | "execution_count": 19, 161 | "metadata": {}, 162 | "output_type": "execute_result" 163 | } 164 | ], 165 | "source": [ 166 | "initial_x = [0.0, 0.0, 5.0]\n", 167 | "\n", 168 | "nlsolve(f!, initial_x, autodiff = :forward)" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": null, 174 | "id": "89e98ab8-e3bc-4a9e-94fb-8b200fb4f207", 175 | "metadata": {}, 176 | "outputs": [], 177 | "source": [] 178 | } 179 | ], 180 | "metadata": { 181 | "kernelspec": { 182 | "display_name": "Julia 1.7.2", 183 | "language": "julia", 184 | "name": "julia-1.7" 185 | }, 186 | "language_info": { 187 | "file_extension": ".jl", 188 | "mimetype": "application/julia", 189 | "name": "julia", 190 | "version": "1.7.2" 191 | } 192 | }, 193 | "nbformat": 4, 194 | "nbformat_minor": 5 195 | } 196 | -------------------------------------------------------------------------------- /linear-equation-system.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "9b2827d7-7691-4e0f-bf3b-be72476ede7b", 6 | "metadata": {}, 7 | "source": [ 8 | "$$\n", 9 | "\\begin{align}\n", 10 | "w + 2y = 10 \\\\\n", 11 | "y + 2z = 14 \\\\\n", 12 | "z + 2w = 9\n", 13 | "\\end{align}\n", 14 | "$$" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "id": "41e5683d-2091-4a2a-a833-fb1acdaf9f58", 21 | "metadata": {}, 22 | "outputs": [ 23 | { 24 | "name": "stderr", 25 | "output_type": "stream", 26 | "text": [ 27 | "┌ Info: [2.0, 4.0, 5.0]\n", 28 | "└ @ Main In[1]:11\n", 29 | "┌ Info: 11.0\n", 30 | "└ @ Main In[1]:12\n" 31 | ] 32 | } 33 | ], 34 | "source": [ 35 | "A = [\n", 36 | " 1 2 0;\n", 37 | " 0 1 2;\n", 38 | " 2 0 1;\n", 39 | "]\n", 40 | "\n", 41 | "b = [10, 14, 9]\n", 42 | "\n", 43 | "wyz = A \\ b \n", 44 | "\n", 45 | "@info wyz\n", 46 | "@info sum(wyz)\n" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "id": "e68edca1-6689-4cee-b06d-46476fe78dc4", 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [] 56 | } 57 | ], 58 | "metadata": { 59 | "kernelspec": { 60 | "display_name": "Julia 1.7.3", 61 | "language": "julia", 62 | "name": "julia-1.7" 63 | }, 64 | "language_info": { 65 | "file_extension": ".jl", 66 | "mimetype": "application/julia", 67 | "name": "julia", 68 | "version": "1.7.3" 69 | } 70 | }, 71 | "nbformat": 4, 72 | "nbformat_minor": 5 73 | } 74 | -------------------------------------------------------------------------------- /maximum-flow.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "8ccdbef4-5835-4fe2-8660-3990f58f68ac", 6 | "metadata": {}, 7 | "source": [ 8 | "" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 27, 14 | "id": "f21f5e35-bd9a-482e-86d2-73b969f52f1a", 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "using JuMP" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 28, 24 | "id": "d7d7a481-1cb0-49aa-a681-d3f3a16e996e", 25 | "metadata": {}, 26 | "outputs": [], 27 | "source": [ 28 | "using GLPK" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 29, 34 | "id": "25d24a61-05ec-464d-b7e5-0f8705f80922", 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "m = Model(GLPK.Optimizer)\n", 39 | "@variable(m, f)\n", 40 | "@variable(m, x12)\n", 41 | "@variable(m, x13)\n", 42 | "@variable(m, x14)\n", 43 | "@variable(m, x23)\n", 44 | "@variable(m, x25)\n", 45 | "@variable(m, x34)\n", 46 | "@variable(m, x35)\n", 47 | "@variable(m, x43)\n", 48 | "@variable(m, x45)\n", 49 | "\n", 50 | "@objective(m, Max, f)\n", 51 | "\n", 52 | "@constraint(m, x12 + x13 + x14 == f)\n", 53 | "@constraint(m, x23 + x25 - x12 == 0)\n", 54 | "@constraint(m, x34 + x35 - x43 - x13 - x23 == 0)\n", 55 | "@constraint(m, x45 + x43 - x14 - x34 == 0)\n", 56 | "@constraint(m, x25 + x35 + x45 == f)\n", 57 | "\n", 58 | "@constraint(m, x14 <= 10)\n", 59 | "@constraint(m, x12 <= 20)\n", 60 | "@constraint(m, x13 <= 30)\n", 61 | "@constraint(m, x45 <= 20)\n", 62 | "@constraint(m, x23 <= 40)\n", 63 | "@constraint(m, x25 <= 30)\n", 64 | "@constraint(m, x35 <= 20)\n", 65 | "@constraint(m, x34 <= 10)\n", 66 | "@constraint(m, x43 <= 5)\n", 67 | "\n", 68 | "@constraint(m, x14 >= 0)\n", 69 | "@constraint(m, x12 >= 0)\n", 70 | "@constraint(m, x13 >= 0)\n", 71 | "@constraint(m, x45 >= 0)\n", 72 | "@constraint(m, x23 >= 0)\n", 73 | "@constraint(m, x25 >= 0)\n", 74 | "@constraint(m, x35 >= 0)\n", 75 | "@constraint(m, x34 >= 0)\n", 76 | "@constraint(m, x43 >= 0);" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 30, 82 | "id": "030813b9-5df0-4c91-aa28-ba7e78fe4848", 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "optimize!(m)" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 31, 92 | "id": "7dad42a5-c5dd-4c1e-8ab5-05434ff717bc", 93 | "metadata": {}, 94 | "outputs": [ 95 | { 96 | "data": { 97 | "text/latex": [ 98 | "$$ \\begin{aligned}\n", 99 | "\\max\\quad & f\\\\\n", 100 | "\\text{Subject to} \\quad & -f + x12 + x13 + x14 = 0.0\\\\\n", 101 | " & -x12 + x23 + x25 = 0.0\\\\\n", 102 | " & -x13 - x23 + x34 + x35 - x43 = 0.0\\\\\n", 103 | " & -x14 - x34 + x43 + x45 = 0.0\\\\\n", 104 | " & -f + x25 + x35 + x45 = 0.0\\\\\n", 105 | " & x14 \\geq 0.0\\\\\n", 106 | " & x12 \\geq 0.0\\\\\n", 107 | " & x13 \\geq 0.0\\\\\n", 108 | " & x45 \\geq 0.0\\\\\n", 109 | " & x23 \\geq 0.0\\\\\n", 110 | " & x25 \\geq 0.0\\\\\n", 111 | " & x35 \\geq 0.0\\\\\n", 112 | " & x34 \\geq 0.0\\\\\n", 113 | " & x43 \\geq 0.0\\\\\n", 114 | " & x14 \\leq 10.0\\\\\n", 115 | " & x12 \\leq 20.0\\\\\n", 116 | " & x13 \\leq 30.0\\\\\n", 117 | " & x45 \\leq 20.0\\\\\n", 118 | " & x23 \\leq 40.0\\\\\n", 119 | " & x25 \\leq 30.0\\\\\n", 120 | " & x35 \\leq 20.0\\\\\n", 121 | " & x34 \\leq 10.0\\\\\n", 122 | " & x43 \\leq 5.0\\\\\n", 123 | "\\end{aligned} $$" 124 | ] 125 | }, 126 | "metadata": {}, 127 | "output_type": "display_data" 128 | } 129 | ], 130 | "source": [ 131 | "print(m)" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": 32, 137 | "id": "e1766fee-f878-4944-bc91-28dd54d2b7fc", 138 | "metadata": {}, 139 | "outputs": [ 140 | { 141 | "data": { 142 | "text/plain": [ 143 | "* Solver : GLPK\n", 144 | "\n", 145 | "* Status\n", 146 | " Termination status : OPTIMAL\n", 147 | " Primal status : FEASIBLE_POINT\n", 148 | " Dual status : FEASIBLE_POINT\n", 149 | " Message from the solver:\n", 150 | " \"Solution is optimal\"\n", 151 | "\n", 152 | "* Candidate solution\n", 153 | " Objective value : 60.0\n", 154 | " Objective bound : Inf\n", 155 | " Dual objective value : 60.0\n", 156 | "\n", 157 | "* Work counters\n", 158 | " Solve time (sec) : 0.00006\n" 159 | ] 160 | }, 161 | "execution_count": 32, 162 | "metadata": {}, 163 | "output_type": "execute_result" 164 | } 165 | ], 166 | "source": [ 167 | "JuMP.solution_summary(m)" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 37, 173 | "id": "3e76db6f-b080-4145-bfb4-6f4c27c81d3c", 174 | "metadata": {}, 175 | "outputs": [ 176 | { 177 | "data": { 178 | "text/plain": [ 179 | "9-element Vector{VariableRef}:\n", 180 | " x12\n", 181 | " x13\n", 182 | " x14\n", 183 | " x23\n", 184 | " x25\n", 185 | " x34\n", 186 | " x35\n", 187 | " x43\n", 188 | " x45" 189 | ] 190 | }, 191 | "execution_count": 37, 192 | "metadata": {}, 193 | "output_type": "execute_result" 194 | } 195 | ], 196 | "source": [ 197 | "varnames = [x12, x13, x14, x23, x25, x34, x35, x43, x45]" 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": 38, 203 | "id": "5545f811-4f4c-4a9d-8ce2-5bfbeeb75b05", 204 | "metadata": {}, 205 | "outputs": [ 206 | { 207 | "data": { 208 | "text/plain": [ 209 | "9×2 Matrix{AffExpr}:\n", 210 | " x12 20\n", 211 | " x13 30\n", 212 | " x14 10\n", 213 | " x23 0\n", 214 | " x25 20\n", 215 | " x34 10\n", 216 | " x35 20\n", 217 | " x43 0\n", 218 | " x45 20" 219 | ] 220 | }, 221 | "execution_count": 38, 222 | "metadata": {}, 223 | "output_type": "execute_result" 224 | } 225 | ], 226 | "source": [ 227 | "hcat(varnames, value.(varnames))" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": null, 233 | "id": "8b5d89b2-2a8a-4f35-99eb-5e5ddc4b59a2", 234 | "metadata": {}, 235 | "outputs": [], 236 | "source": [] 237 | } 238 | ], 239 | "metadata": { 240 | "kernelspec": { 241 | "display_name": "Julia 1.7.1", 242 | "language": "julia", 243 | "name": "julia-1.7" 244 | }, 245 | "language_info": { 246 | "file_extension": ".jl", 247 | "mimetype": "application/julia", 248 | "name": "julia", 249 | "version": "1.7.1" 250 | } 251 | }, 252 | "nbformat": 4, 253 | "nbformat_minor": 5 254 | } 255 | -------------------------------------------------------------------------------- /maximum-flow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jbytecode/notebooks/0bb925e5599c6bcf130255d586b9c1d7f0e2595b/maximum-flow.png -------------------------------------------------------------------------------- /newton-method-examples/generator.jl: -------------------------------------------------------------------------------- 1 | using Symbolics, Latexify 2 | 3 | 4 | function doit(iters::Int) 5 | @variables x y w 6 | 7 | z = (x-1) * y^2 + (y-2) * w^2 + (w-3) * x^2 8 | 9 | myio = open("/home/hako/Desktop/latex/newton4.tex", "w") 10 | 11 | grad = Symbolics.gradient(z, [x, y, w]) 12 | hess = Symbolics.hessian(z, [x, y, w]) 13 | 14 | println(myio, "\\documentclass{article}") 15 | println(myio, "\\usepackage{geometry}") 16 | println(myio, "\\geometry{margin=1.5cm}") 17 | 18 | println(myio, "\\begin{document}") 19 | 20 | println(myio, "\\subsubsection{Initials}") 21 | println(myio, "Function:") 22 | println(myio, latexify(z)) 23 | println(myio, "Gradient Vector:") 24 | println(myio, latexify(grad)) 25 | println(myio, "Hession Matrix:") 26 | println(myio, latexify(hess)) 27 | 28 | xval = 1.0 29 | yval = 0.0 30 | wval = 1.0 31 | 32 | valdict = Dict(x => xval, y => yval, w => wval) 33 | println(myio, "Start Value:") 34 | println(myio, valdict |> Tuple) 35 | 36 | println(myio, "Function at point:") 37 | myf = substitute(z, valdict) 38 | println(myio, myf) 39 | 40 | for i in 1:iters 41 | println(myio, "\\subsubsection{Iteration $i}") 42 | println(myio, "Gradient at $(valdict |> Tuple)") 43 | mygrad = substitute(grad, valdict) 44 | println(myio, latexify(mygrad)) 45 | 46 | println(myio, "Hessian at $(valdict |> Tuple)") 47 | myhess = substitute(hess, valdict) 48 | println(myio, latexify(myhess)) 49 | 50 | println(myio, "Inverse of Hessian") 51 | invhess = inv(myhess) 52 | println(myio, latexify(invhess)) 53 | 54 | myresult = [xval, yval, wval] - invhess * mygrad 55 | xval = myresult[1] 56 | yval = myresult[2] 57 | wval = myresult[3] 58 | valdict = Dict(x => xval, y => yval, w => wval) 59 | println(myio, valdict |> Tuple) 60 | 61 | println(myio, "Function at point:") 62 | mynewf = substitute(z, valdict) 63 | println(myio, latexify(mynewf)) 64 | 65 | println(myio, "Diff of function values between two iterations:") 66 | println(myio, latexify(abs(mynewf - myf))) 67 | myf = mynewf 68 | end 69 | 70 | 71 | println(myio, "\\end{document}") 72 | close(myio) 73 | 74 | end 75 | 76 | doit(10) 77 | 78 | -------------------------------------------------------------------------------- /newton-method-examples/newton1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jbytecode/notebooks/0bb925e5599c6bcf130255d586b9c1d7f0e2595b/newton-method-examples/newton1.pdf -------------------------------------------------------------------------------- /newton-method-examples/newton1.tex: -------------------------------------------------------------------------------- 1 | \documentclass{article} 2 | \usepackage{geometry} 3 | \geometry{margin=1.5cm} 4 | \begin{document} 5 | \subsubsection{Initials} 6 | Function: 7 | \begin{equation} 8 | \left( -3.1416 + x \right)^{2} + \left( -2.7183 + y \right)^{2} 9 | \end{equation} 10 | 11 | Gradient Vector: 12 | \begin{equation} 13 | \left[ 14 | \begin{array}{c} 15 | 2 \left( -3.1416 + x \right) \\ 16 | 2 \left( -2.7183 + y \right) \\ 17 | \end{array} 18 | \right] 19 | \end{equation} 20 | 21 | Hession Matrix: 22 | \begin{equation} 23 | \left[ 24 | \begin{array}{cc} 25 | 2 & 0 \\ 26 | 0 & 2 \\ 27 | \end{array} 28 | \right] 29 | \end{equation} 30 | 31 | Start Value: 32 | (y => 5.0, x => -1.0) 33 | Function at point: 34 | 22.359035754102166 35 | \subsubsection{Iteration 1} 36 | Gradient at (y => 5.0, x => -1.0) 37 | \begin{equation} 38 | \left[ 39 | \begin{array}{c} 40 | -8.2832 \\ 41 | 4.5634 \\ 42 | \end{array} 43 | \right] 44 | \end{equation} 45 | 46 | Hessian at (y => 5.0, x => -1.0) 47 | \begin{equation} 48 | \left[ 49 | \begin{array}{cc} 50 | 2 & 0 \\ 51 | 0 & 2 \\ 52 | \end{array} 53 | \right] 54 | \end{equation} 55 | 56 | Inverse of Hessian 57 | \begin{equation} 58 | \left[ 59 | \begin{array}{cc} 60 | 0.5 & 0 \\ 61 | 0 & 0.5 \\ 62 | \end{array} 63 | \right] 64 | \end{equation} 65 | 66 | (y => 2.71828, x => 3.14159264) 67 | Function at point: 68 | \begin{equation} 69 | 0 70 | \end{equation} 71 | 72 | Diff of function values between two iterations: 73 | \begin{equation} 74 | 22.359 75 | \end{equation} 76 | 77 | \subsubsection{Iteration 2} 78 | Gradient at (y => 2.71828, x => 3.14159264) 79 | \begin{equation} 80 | \left[ 81 | \begin{array}{c} 82 | 0 \\ 83 | 0 \\ 84 | \end{array} 85 | \right] 86 | \end{equation} 87 | 88 | Hessian at (y => 2.71828, x => 3.14159264) 89 | \begin{equation} 90 | \left[ 91 | \begin{array}{cc} 92 | 2 & 0 \\ 93 | 0 & 2 \\ 94 | \end{array} 95 | \right] 96 | \end{equation} 97 | 98 | Inverse of Hessian 99 | \begin{equation} 100 | \left[ 101 | \begin{array}{cc} 102 | 0.5 & 0 \\ 103 | 0 & 0.5 \\ 104 | \end{array} 105 | \right] 106 | \end{equation} 107 | 108 | (y => 2.71828, x => 3.14159264) 109 | Function at point: 110 | \begin{equation} 111 | 0 112 | \end{equation} 113 | 114 | Diff of function values between two iterations: 115 | \begin{equation} 116 | 0 117 | \end{equation} 118 | 119 | \end{document} 120 | -------------------------------------------------------------------------------- /newton-method-examples/newton2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jbytecode/notebooks/0bb925e5599c6bcf130255d586b9c1d7f0e2595b/newton-method-examples/newton2.pdf -------------------------------------------------------------------------------- /newton-method-examples/newton2.tex: -------------------------------------------------------------------------------- 1 | \documentclass{article} 2 | \usepackage{geometry} 3 | \geometry{margin=1.5cm} 4 | \begin{document} 5 | \subsubsection{Initials} 6 | Function: 7 | \begin{equation} 8 | \left( -3.1416 + x \right)^{2} + \left( -2.7183 + y \right)^{2} + x^{2} y 9 | \end{equation} 10 | 11 | Gradient Vector: 12 | \begin{equation} 13 | \left[ 14 | \begin{array}{c} 15 | 2 \left( -3.1416 + x \right) + 2 x y \\ 16 | 2 \left( -2.7183 + y \right) + x^{2} \\ 17 | \end{array} 18 | \right] 19 | \end{equation} 20 | 21 | Hession Matrix: 22 | \begin{equation} 23 | \left[ 24 | \begin{array}{cc} 25 | 2 + 2 y & 2 x \\ 26 | 2 x & 2 \\ 27 | \end{array} 28 | \right] 29 | \end{equation} 30 | 31 | Start Value: 32 | (y => 5.0, x => -1.0) 33 | Function at point: 34 | 27.359035754102166 35 | \subsubsection{Iteration 1} 36 | Gradient at (y => 5.0, x => -1.0) 37 | \begin{equation} 38 | \left[ 39 | \begin{array}{c} 40 | -18.283 \\ 41 | 5.5634 \\ 42 | \end{array} 43 | \right] 44 | \end{equation} 45 | 46 | Hessian at (y => 5.0, x => -1.0) 47 | \begin{equation} 48 | \left[ 49 | \begin{array}{cc} 50 | 12 & -2 \\ 51 | -2 & 2 \\ 52 | \end{array} 53 | \right] 54 | \end{equation} 55 | 56 | Inverse of Hessian 57 | \begin{equation} 58 | \left[ 59 | \begin{array}{cc} 60 | 0.1 & 0.1 \\ 61 | 0.1 & 0.6 \\ 62 | \end{array} 63 | \right] 64 | \end{equation} 65 | 66 | (y => 3.4902545279999995, x => 0.2719745279999999) 67 | Function at point: 68 | \begin{equation} 69 | 9.0888 70 | \end{equation} 71 | 72 | Diff of function values between two iterations: 73 | \begin{equation} 74 | 18.27 75 | \end{equation} 76 | 77 | \subsubsection{Iteration 2} 78 | Gradient at (y => 3.4902545279999995, x => 0.2719745279999999) 79 | \begin{equation} 80 | \left[ 81 | \begin{array}{c} 82 | -3.8407 \\ 83 | 1.6179 \\ 84 | \end{array} 85 | \right] 86 | \end{equation} 87 | 88 | Hessian at (y => 3.4902545279999995, x => 0.2719745279999999) 89 | \begin{equation} 90 | \left[ 91 | \begin{array}{cc} 92 | 8.9805 & 0.54395 \\ 93 | 0.54395 & 2 \\ 94 | \end{array} 95 | \right] 96 | \end{equation} 97 | 98 | Inverse of Hessian 99 | \begin{equation} 100 | \left[ 101 | \begin{array}{cc} 102 | 0.11322 & -0.030792 \\ 103 | -0.030792 & 0.50837 \\ 104 | \end{array} 105 | \right] 106 | \end{equation} 107 | 108 | (y => 2.5494811205001673, x => 0.7566295011282975) 109 | Function at point: 110 | \begin{equation} 111 | 7.1761 112 | \end{equation} 113 | 114 | Diff of function values between two iterations: 115 | \begin{equation} 116 | 1.9127 117 | \end{equation} 118 | 119 | \subsubsection{Iteration 3} 120 | Gradient at (y => 2.5494811205001673, x => 0.7566295011282975) 121 | \begin{equation} 122 | \left[ 123 | \begin{array}{c} 124 | -0.9119 \\ 125 | 0.23489 \\ 126 | \end{array} 127 | \right] 128 | \end{equation} 129 | 130 | Hessian at (y => 2.5494811205001673, x => 0.7566295011282975) 131 | \begin{equation} 132 | \left[ 133 | \begin{array}{cc} 134 | 7.099 & 1.5133 \\ 135 | 1.5133 & 2 \\ 136 | \end{array} 137 | \right] 138 | \end{equation} 139 | 140 | Inverse of Hessian 141 | \begin{equation} 142 | \left[ 143 | \begin{array}{cc} 144 | 0.16795 & -0.12708 \\ 145 | -0.12708 & 0.59615 \\ 146 | \end{array} 147 | \right] 148 | \end{equation} 149 | 150 | (y => 2.2935667718462867, x => 0.9396373364802073) 151 | Function at point: 152 | \begin{equation} 153 | 7.054 154 | \end{equation} 155 | 156 | Diff of function values between two iterations: 157 | \begin{equation} 158 | 0.12207 159 | \end{equation} 160 | 161 | \subsubsection{Iteration 4} 162 | Gradient at (y => 2.2935667718462867, x => 0.9396373364802073) 163 | \begin{equation} 164 | \left[ 165 | \begin{array}{c} 166 | -0.093669 \\ 167 | 0.033492 \\ 168 | \end{array} 169 | \right] 170 | \end{equation} 171 | 172 | Hessian at (y => 2.2935667718462867, x => 0.9396373364802073) 173 | \begin{equation} 174 | \left[ 175 | \begin{array}{cc} 176 | 6.5871 & 1.8793 \\ 177 | 1.8793 & 2 \\ 178 | \end{array} 179 | \right] 180 | \end{equation} 181 | 182 | Inverse of Hessian 183 | \begin{equation} 184 | \left[ 185 | \begin{array}{cc} 186 | 0.20741 & -0.19489 \\ 187 | -0.19489 & 0.68313 \\ 188 | \end{array} 189 | \right] 190 | \end{equation} 191 | 192 | (y => 2.252432139556676, x => 0.9655927742248082) 193 | Function at point: 194 | \begin{equation} 195 | 7.0521 196 | \end{equation} 197 | 198 | Diff of function values between two iterations: 199 | \begin{equation} 200 | 0.0019322 201 | \end{equation} 202 | 203 | \subsubsection{Iteration 5} 204 | Gradient at (y => 2.252432139556676, x => 0.9655927742248082) 205 | \begin{equation} 206 | \left[ 207 | \begin{array}{c} 208 | -0.0021353 \\ 209 | 0.00067368 \\ 210 | \end{array} 211 | \right] 212 | \end{equation} 213 | 214 | Hessian at (y => 2.252432139556676, x => 0.9655927742248082) 215 | \begin{equation} 216 | \left[ 217 | \begin{array}{cc} 218 | 6.5049 & 1.9312 \\ 219 | 1.9312 & 2 \\ 220 | \end{array} 221 | \right] 222 | \end{equation} 223 | 224 | Inverse of Hessian 225 | \begin{equation} 226 | \left[ 227 | \begin{array}{cc} 228 | 0.21551 & -0.2081 \\ 229 | -0.2081 & 0.70094 \\ 230 | \end{array} 231 | \right] 232 | \end{equation} 233 | 234 | (y => 2.251515574307129, x => 0.9661931545204827) 235 | Function at point: 236 | \begin{equation} 237 | 7.0521 238 | \end{equation} 239 | 240 | Diff of function values between two iterations: 241 | \begin{equation} 242 | 9.5007 \cdot 10^{-7} 243 | \end{equation} 244 | 245 | \end{document} 246 | -------------------------------------------------------------------------------- /newton-method-examples/newton3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jbytecode/notebooks/0bb925e5599c6bcf130255d586b9c1d7f0e2595b/newton-method-examples/newton3.pdf -------------------------------------------------------------------------------- /newton-method-examples/newton3.tex: -------------------------------------------------------------------------------- 1 | \documentclass{article} 2 | \usepackage{geometry} 3 | \geometry{margin=1.5cm} 4 | \begin{document} 5 | \subsubsection{Initials} 6 | Function: 7 | \begin{equation} 8 | \left( 3 + w \right)^{2} + \left( 9 + x \right)^{2} + \left( -2 + y \right)^{2} 9 | \end{equation} 10 | 11 | Gradient Vector: 12 | \begin{equation} 13 | \left[ 14 | \begin{array}{c} 15 | 2 \left( 9 + x \right) \\ 16 | 2 \left( -2 + y \right) \\ 17 | 2 \left( 3 + w \right) \\ 18 | \end{array} 19 | \right] 20 | \end{equation} 21 | 22 | Hession Matrix: 23 | \begin{equation} 24 | \left[ 25 | \begin{array}{ccc} 26 | 2 & 0 & 0 \\ 27 | 0 & 2 & 0 \\ 28 | 0 & 0 & 2 \\ 29 | \end{array} 30 | \right] 31 | \end{equation} 32 | 33 | Start Value: 34 | (w => 0.0, y => 0.0, x => 1.0) 35 | Function at point: 36 | 113.0 37 | \subsubsection{Iteration 1} 38 | Gradient at (w => 0.0, y => 0.0, x => 1.0) 39 | \begin{equation} 40 | \left[ 41 | \begin{array}{c} 42 | 20 \\ 43 | -4 \\ 44 | 6 \\ 45 | \end{array} 46 | \right] 47 | \end{equation} 48 | 49 | Hessian at (w => 0.0, y => 0.0, x => 1.0) 50 | \begin{equation} 51 | \left[ 52 | \begin{array}{ccc} 53 | 2 & 0 & 0 \\ 54 | 0 & 2 & 0 \\ 55 | 0 & 0 & 2 \\ 56 | \end{array} 57 | \right] 58 | \end{equation} 59 | 60 | Inverse of Hessian 61 | \begin{equation} 62 | \left[ 63 | \begin{array}{ccc} 64 | 0.5 & 0 & 0 \\ 65 | 0 & 0.5 & 0 \\ 66 | 0 & 0 & 0.5 \\ 67 | \end{array} 68 | \right] 69 | \end{equation} 70 | 71 | (w => -3.0, y => 2.0, x => -9.0) 72 | Function at point: 73 | \begin{equation} 74 | 0 75 | \end{equation} 76 | 77 | Diff of function values between two iterations: 78 | \begin{equation} 79 | 113 80 | \end{equation} 81 | 82 | \subsubsection{Iteration 2} 83 | Gradient at (w => -3.0, y => 2.0, x => -9.0) 84 | \begin{equation} 85 | \left[ 86 | \begin{array}{c} 87 | 0 \\ 88 | 0 \\ 89 | 0 \\ 90 | \end{array} 91 | \right] 92 | \end{equation} 93 | 94 | Hessian at (w => -3.0, y => 2.0, x => -9.0) 95 | \begin{equation} 96 | \left[ 97 | \begin{array}{ccc} 98 | 2 & 0 & 0 \\ 99 | 0 & 2 & 0 \\ 100 | 0 & 0 & 2 \\ 101 | \end{array} 102 | \right] 103 | \end{equation} 104 | 105 | Inverse of Hessian 106 | \begin{equation} 107 | \left[ 108 | \begin{array}{ccc} 109 | 0.5 & 0 & 0 \\ 110 | 0 & 0.5 & 0 \\ 111 | 0 & 0 & 0.5 \\ 112 | \end{array} 113 | \right] 114 | \end{equation} 115 | 116 | (w => -3.0, y => 2.0, x => -9.0) 117 | Function at point: 118 | \begin{equation} 119 | 0 120 | \end{equation} 121 | 122 | Diff of function values between two iterations: 123 | \begin{equation} 124 | 0 125 | \end{equation} 126 | 127 | \end{document} 128 | -------------------------------------------------------------------------------- /newton-method-examples/newton4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jbytecode/notebooks/0bb925e5599c6bcf130255d586b9c1d7f0e2595b/newton-method-examples/newton4.pdf -------------------------------------------------------------------------------- /newton-method-examples/newton4.tex: -------------------------------------------------------------------------------- 1 | \documentclass{article} 2 | \usepackage{geometry} 3 | \geometry{margin=1.5cm} 4 | \begin{document} 5 | \subsubsection{Initials} 6 | Function: 7 | \begin{equation} 8 | w^{2} \left( -2 + y \right) + x^{2} \left( -3 + w \right) + y^{2} \left( -1 + x \right) 9 | \end{equation} 10 | 11 | Gradient Vector: 12 | \begin{equation} 13 | \left[ 14 | \begin{array}{c} 15 | 2 \left( -3 + w \right) x + y^{2} \\ 16 | w^{2} + 2 \left( -1 + x \right) y \\ 17 | 2 w \left( -2 + y \right) + x^{2} \\ 18 | \end{array} 19 | \right] 20 | \end{equation} 21 | 22 | Hession Matrix: 23 | \begin{equation} 24 | \left[ 25 | \begin{array}{ccc} 26 | 2 \left( -3 + w \right) & 2 y & 2 x \\ 27 | 2 y & 2 \left( -1 + x \right) & 2 w \\ 28 | 2 x & 2 w & 2 \left( -2 + y \right) \\ 29 | \end{array} 30 | \right] 31 | \end{equation} 32 | 33 | Start Value: 34 | (w => 1.0, y => 0.0, x => 1.0) 35 | Function at point: 36 | -4.0 37 | \subsubsection{Iteration 1} 38 | Gradient at (w => 1.0, y => 0.0, x => 1.0) 39 | \begin{equation} 40 | \left[ 41 | \begin{array}{c} 42 | -4 \\ 43 | 1 \\ 44 | -3 \\ 45 | \end{array} 46 | \right] 47 | \end{equation} 48 | 49 | Hessian at (w => 1.0, y => 0.0, x => 1.0) 50 | \begin{equation} 51 | \left[ 52 | \begin{array}{ccc} 53 | -4 & 0 & 2 \\ 54 | 0 & 0 & 2 \\ 55 | 2 & 2 & -4 \\ 56 | \end{array} 57 | \right] 58 | \end{equation} 59 | 60 | Inverse of Hessian 61 | \begin{equation} 62 | \left[ 63 | \begin{array}{ccc} 64 | -0.25 & 0.25 & 0 \\ 65 | 0.25 & 0.75 & 0.5 \\ 66 | 0 & 0.5 & -0 \\ 67 | \end{array} 68 | \right] 69 | \end{equation} 70 | 71 | (w => 0.5, y => 1.75, x => -0.25) 72 | Function at point: 73 | \begin{equation} 74 | -4.0469 75 | \end{equation} 76 | 77 | Diff of function values between two iterations: 78 | \begin{equation} 79 | 0.046875 80 | \end{equation} 81 | 82 | \subsubsection{Iteration 2} 83 | Gradient at (w => 0.5, y => 1.75, x => -0.25) 84 | \begin{equation} 85 | \left[ 86 | \begin{array}{c} 87 | 4.3125 \\ 88 | -4.125 \\ 89 | -0.1875 \\ 90 | \end{array} 91 | \right] 92 | \end{equation} 93 | 94 | Hessian at (w => 0.5, y => 1.75, x => -0.25) 95 | \begin{equation} 96 | \left[ 97 | \begin{array}{ccc} 98 | -5 & 3.5 & -0.5 \\ 99 | 3.5 & -2.5 & 1 \\ 100 | -0.5 & 1 & -0.5 \\ 101 | \end{array} 102 | \right] 103 | \end{equation} 104 | 105 | Inverse of Hessian 106 | \begin{equation} 107 | \left[ 108 | \begin{array}{ccc} 109 | 0.125 & 0.625 & 1.125 \\ 110 | 0.625 & 1.125 & 1.625 \\ 111 | 1.125 & 1.625 & 0.125 \\ 112 | \end{array} 113 | \right] 114 | \end{equation} 115 | 116 | (w => 2.375, y => 4.0, x => 2.0) 117 | Function at point: 118 | \begin{equation} 119 | 24.781 120 | \end{equation} 121 | 122 | Diff of function values between two iterations: 123 | \begin{equation} 124 | 28.828 125 | \end{equation} 126 | 127 | \subsubsection{Iteration 3} 128 | Gradient at (w => 2.375, y => 4.0, x => 2.0) 129 | \begin{equation} 130 | \left[ 131 | \begin{array}{c} 132 | 13.5 \\ 133 | 13.641 \\ 134 | 13.5 \\ 135 | \end{array} 136 | \right] 137 | \end{equation} 138 | 139 | Hessian at (w => 2.375, y => 4.0, x => 2.0) 140 | \begin{equation} 141 | \left[ 142 | \begin{array}{ccc} 143 | -1.25 & 8 & 4 \\ 144 | 8 & 2 & 4.75 \\ 145 | 4 & 4.75 & 4 \\ 146 | \end{array} 147 | \right] 148 | \end{equation} 149 | 150 | Inverse of Hessian 151 | \begin{equation} 152 | \left[ 153 | \begin{array}{ccc} 154 | -0.42577 & -0.38008 & 0.87711 \\ 155 | -0.38008 & -0.61398 & 1.1092 \\ 156 | 0.87711 & 1.1092 & -1.9443 \\ 157 | \end{array} 158 | \right] 159 | \end{equation} 160 | 161 | (w => 1.6516388761991792, y => 2.5322064869803587, x => 1.0913659205116488) 162 | Function at point: 163 | \begin{equation} 164 | 0.43165 165 | \end{equation} 166 | 167 | Diff of function values between two iterations: 168 | \begin{equation} 169 | 24.35 170 | \end{equation} 171 | 172 | \subsubsection{Iteration 4} 173 | Gradient at (w => 1.6516388761991792, y => 2.5322064869803587, x => 1.0913659205116488) 174 | \begin{equation} 175 | \left[ 176 | \begin{array}{c} 177 | 3.469 \\ 178 | 3.1906 \\ 179 | 2.9491 \\ 180 | \end{array} 181 | \right] 182 | \end{equation} 183 | 184 | Hessian at (w => 1.6516388761991792, y => 2.5322064869803587, x => 1.0913659205116488) 185 | \begin{equation} 186 | \left[ 187 | \begin{array}{ccc} 188 | -2.6967 & 5.0644 & 2.1827 \\ 189 | 5.0644 & 0.18273 & 3.3033 \\ 190 | 2.1827 & 3.3033 & 1.0644 \\ 191 | \end{array} 192 | \right] 193 | \end{equation} 194 | 195 | Inverse of Hessian 196 | \begin{equation} 197 | \left[ 198 | \begin{array}{ccc} 199 | -0.1453 & 0.024668 & 0.2214 \\ 200 | 0.024668 & -0.10351 & 0.27064 \\ 201 | 0.2214 & 0.27064 & -0.3544 \\ 202 | \end{array} 203 | \right] 204 | \end{equation} 205 | 206 | (w => 1.0653027177675614, y => 1.9787526896164094, x => 0.8637658180657077) 207 | Function at point: 208 | \begin{equation} 209 | -2.001 210 | \end{equation} 211 | 212 | Diff of function values between two iterations: 213 | \begin{equation} 214 | 2.4326 215 | \end{equation} 216 | 217 | \subsubsection{Iteration 5} 218 | Gradient at (w => 1.0653027177675614, y => 1.9787526896164094, x => 0.8637658180657077) 219 | \begin{equation} 220 | \left[ 221 | \begin{array}{c} 222 | 0.57321 \\ 223 | 0.59572 \\ 224 | 0.70082 \\ 225 | \end{array} 226 | \right] 227 | \end{equation} 228 | 229 | Hessian at (w => 1.0653027177675614, y => 1.9787526896164094, x => 0.8637658180657077) 230 | \begin{equation} 231 | \left[ 232 | \begin{array}{ccc} 233 | -3.8694 & 3.9575 & 1.7275 \\ 234 | 3.9575 & -0.27247 & 2.1306 \\ 235 | 1.7275 & 2.1306 & -0.042495 \\ 236 | \end{array} 237 | \right] 238 | \end{equation} 239 | 240 | Inverse of Hessian 241 | \begin{equation} 242 | \left[ 243 | \begin{array}{ccc} 244 | -0.094073 & 0.079965 & 0.18496 \\ 245 | 0.079965 & -0.058588 & 0.31333 \\ 246 | 0.18496 & 0.31333 & -0.30349 \\ 247 | \end{array} 248 | \right] 249 | \end{equation} 250 | 251 | (w => 0.9853183804875232, y => 1.7482324173687027, x => 0.7404263040235104) 252 | Function at point: 253 | \begin{equation} 254 | -2.1423 255 | \end{equation} 256 | 257 | Diff of function values between two iterations: 258 | \begin{equation} 259 | 0.14129 260 | \end{equation} 261 | 262 | \subsubsection{Iteration 6} 263 | Gradient at (w => 0.9853183804875232, y => 1.7482324173687027, x => 0.7404263040235104) 264 | \begin{equation} 265 | \left[ 266 | \begin{array}{c} 267 | 0.07287 \\ 268 | 0.063262 \\ 269 | 0.052089 \\ 270 | \end{array} 271 | \right] 272 | \end{equation} 273 | 274 | Hessian at (w => 0.9853183804875232, y => 1.7482324173687027, x => 0.7404263040235104) 275 | \begin{equation} 276 | \left[ 277 | \begin{array}{ccc} 278 | -4.0294 & 3.4965 & 1.4809 \\ 279 | 3.4965 & -0.51915 & 1.9706 \\ 280 | 1.4809 & 1.9706 & -0.50354 \\ 281 | \end{array} 282 | \right] 283 | \end{equation} 284 | 285 | Inverse of Hessian 286 | \begin{equation} 287 | \left[ 288 | \begin{array}{ccc} 289 | -0.085635 & 0.11062 & 0.18108 \\ 290 | 0.11062 & -0.0038774 & 0.31015 \\ 291 | 0.18108 & 0.31015 & -0.23959 \\ 292 | \end{array} 293 | \right] 294 | \end{equation} 295 | 296 | (w => 0.9649815248632138, y => 1.7242611657519602, x => 0.730235991788498) 297 | Function at point: 298 | \begin{equation} 299 | -2.144 300 | \end{equation} 301 | 302 | Diff of function values between two iterations: 303 | \begin{equation} 304 | 0.0016771 305 | \end{equation} 306 | 307 | \subsubsection{Iteration 7} 308 | Gradient at (w => 0.9649815248632138, y => 1.7242611657519602, x => 0.730235991788498) 309 | \begin{equation} 310 | \left[ 311 | \begin{array}{c} 312 | 0.0009891 \\ 313 | 0.00090214 \\ 314 | 0.0010788 \\ 315 | \end{array} 316 | \right] 317 | \end{equation} 318 | 319 | Hessian at (w => 0.9649815248632138, y => 1.7242611657519602, x => 0.730235991788498) 320 | \begin{equation} 321 | \left[ 322 | \begin{array}{ccc} 323 | -4.07 & 3.4485 & 1.4605 \\ 324 | 3.4485 & -0.53953 & 1.93 \\ 325 | 1.4605 & 1.93 & -0.55148 \\ 326 | \end{array} 327 | \right] 328 | \end{equation} 329 | 330 | Inverse of Hessian 331 | \begin{equation} 332 | \left[ 333 | \begin{array}{ccc} 334 | -0.08339 & 0.11486 & 0.18111 \\ 335 | 0.11486 & 0.0027144 & 0.31367 \\ 336 | 0.18111 & 0.31367 & -0.23593 \\ 337 | \end{array} 338 | \right] 339 | \end{equation} 340 | 341 | (w => 0.9647739419525763, y => 1.7238067081747834, x => 0.7300194636787573) 342 | Function at point: 343 | \begin{equation} 344 | -2.144 345 | \end{equation} 346 | 347 | Diff of function values between two iterations: 348 | \begin{equation} 349 | 4.2412 \cdot 10^{-7} 350 | \end{equation} 351 | 352 | \subsubsection{Iteration 8} 353 | Gradient at (w => 0.9647739419525763, y => 1.7238067081747834, x => 0.7300194636787573) 354 | \begin{equation} 355 | \left[ 356 | \begin{array}{c} 357 | 2.9643 \cdot 10^{-7} \\ 358 | 2.399 \cdot 10^{-7} \\ 359 | 2.3556 \cdot 10^{-7} \\ 360 | \end{array} 361 | \right] 362 | \end{equation} 363 | 364 | Hessian at (w => 0.9647739419525763, y => 1.7238067081747834, x => 0.7300194636787573) 365 | \begin{equation} 366 | \left[ 367 | \begin{array}{ccc} 368 | -4.0705 & 3.4476 & 1.46 \\ 369 | 3.4476 & -0.53996 & 1.9295 \\ 370 | 1.46 & 1.9295 & -0.55239 \\ 371 | \end{array} 372 | \right] 373 | \end{equation} 374 | 375 | Inverse of Hessian 376 | \begin{equation} 377 | \left[ 378 | \begin{array}{ccc} 379 | -0.083365 & 0.11493 & 0.18111 \\ 380 | 0.11493 & 0.0028418 & 0.3137 \\ 381 | 0.18111 & 0.3137 & -0.23582 \\ 382 | \end{array} 383 | \right] 384 | \end{equation} 385 | 386 | (w => 0.9647738685591329, y => 1.7238065995294303, x => 0.7300194181561234) 387 | Function at point: 388 | \begin{equation} 389 | -2.144 390 | \end{equation} 391 | 392 | Diff of function values between two iterations: 393 | \begin{equation} 394 | 2.8866 \cdot 10^{-14} 395 | \end{equation} 396 | 397 | \subsubsection{Iteration 9} 398 | Gradient at (w => 0.9647738685591329, y => 1.7238065995294303, x => 0.7300194181561234) 399 | \begin{equation} 400 | \left[ 401 | \begin{array}{c} 402 | 1.9096 \cdot 10^{-14} \\ 403 | 1.5099 \cdot 10^{-14} \\ 404 | 1.8097 \cdot 10^{-14} \\ 405 | \end{array} 406 | \right] 407 | \end{equation} 408 | 409 | Hessian at (w => 0.9647738685591329, y => 1.7238065995294303, x => 0.7300194181561234) 410 | \begin{equation} 411 | \left[ 412 | \begin{array}{ccc} 413 | -4.0705 & 3.4476 & 1.46 \\ 414 | 3.4476 & -0.53996 & 1.9295 \\ 415 | 1.46 & 1.9295 & -0.55239 \\ 416 | \end{array} 417 | \right] 418 | \end{equation} 419 | 420 | Inverse of Hessian 421 | \begin{equation} 422 | \left[ 423 | \begin{array}{ccc} 424 | -0.083365 & 0.11493 & 0.18111 \\ 425 | 0.11493 & 0.0028418 & 0.3137 \\ 426 | 0.18111 & 0.3137 & -0.23582 \\ 427 | \end{array} 428 | \right] 429 | \end{equation} 430 | 431 | (w => 0.964773868559129, y => 1.7238065995294223, x => 0.7300194181561199) 432 | Function at point: 433 | \begin{equation} 434 | -2.144 435 | \end{equation} 436 | 437 | Diff of function values between two iterations: 438 | \begin{equation} 439 | 4.4409 \cdot 10^{-16} 440 | \end{equation} 441 | 442 | \subsubsection{Iteration 10} 443 | Gradient at (w => 0.964773868559129, y => 1.7238065995294223, x => 0.7300194181561199) 444 | \begin{equation} 445 | \left[ 446 | \begin{array}{c} 447 | -8.8818 \cdot 10^{-16} \\ 448 | 1.1102 \cdot 10^{-16} \\ 449 | -2.2204 \cdot 10^{-16} \\ 450 | \end{array} 451 | \right] 452 | \end{equation} 453 | 454 | Hessian at (w => 0.964773868559129, y => 1.7238065995294223, x => 0.7300194181561199) 455 | \begin{equation} 456 | \left[ 457 | \begin{array}{ccc} 458 | -4.0705 & 3.4476 & 1.46 \\ 459 | 3.4476 & -0.53996 & 1.9295 \\ 460 | 1.46 & 1.9295 & -0.55239 \\ 461 | \end{array} 462 | \right] 463 | \end{equation} 464 | 465 | Inverse of Hessian 466 | \begin{equation} 467 | \left[ 468 | \begin{array}{ccc} 469 | -0.083365 & 0.11493 & 0.18111 \\ 470 | 0.11493 & 0.0028418 & 0.3137 \\ 471 | 0.18111 & 0.3137 & -0.23582 \\ 472 | \end{array} 473 | \right] 474 | \end{equation} 475 | 476 | (w => 0.9647738685591292, y => 1.7238065995294225, x => 0.7300194181561199) 477 | Function at point: 478 | \begin{equation} 479 | -2.144 480 | \end{equation} 481 | 482 | Diff of function values between two iterations: 483 | \begin{equation} 484 | 4.4409 \cdot 10^{-16} 485 | \end{equation} 486 | 487 | \end{document} 488 | -------------------------------------------------------------------------------- /newton-multiple.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 44, 6 | "id": "c90cc5e0-9298-462f-910f-f72964f07736", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "using ForwardDiff" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "id": "cb4d781e-3ea2-4d42-afd9-a0bb684bff41", 16 | "metadata": {}, 17 | "source": [ 18 | "# Newton's method " 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "id": "a8702e57-809d-4b99-b9f1-10e30506edaf", 24 | "metadata": {}, 25 | "source": [ 26 | "- $f(x_1, x_2, \\ldots, x_n)$: Function to minimize\n", 27 | "- $\\nabla{f}$ is gradient vector where\n", 28 | "\n", 29 | "\n", 30 | "$$\n", 31 | "\\nabla{f} = \\Big[\\frac{\\partial f}{\\partial x_1}, \\frac{\\partial f}{\\partial x_2}, \\ldots, \\frac{\\partial f}{\\partial x_n} \\Big] \n", 32 | "$$\n", 33 | "\n", 34 | "- $H$ is Hessian matrix\n", 35 | "- $x$ is the initial solution" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "id": "4f67f75d-451d-40a0-ae05-d8a3ba4af12b", 41 | "metadata": {}, 42 | "source": [ 43 | "- General algorithm:\n", 44 | "- iterate until convergence\n", 45 | "- $x := x - H^{-1}|_{x} \\nabla f|_{x}$" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 23, 51 | "id": "5eddb9d2-c4df-4c3f-81d7-b3185800ee3d", 52 | "metadata": {}, 53 | "outputs": [ 54 | { 55 | "data": { 56 | "text/plain": [ 57 | "f (generic function with 1 method)" 58 | ] 59 | }, 60 | "execution_count": 23, 61 | "metadata": {}, 62 | "output_type": "execute_result" 63 | } 64 | ], 65 | "source": [ 66 | "f(x) = (x[1] - 3.14159265)^2 + (x[2] - exp(1))^2 + (x[1] * x[2] - 8.539734)^2" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 45, 72 | "id": "3d30532e-eb0e-42f0-aad6-c4270da3e1aa", 73 | "metadata": {}, 74 | "outputs": [ 75 | { 76 | "name": "stderr", 77 | "output_type": "stream", 78 | "text": [ 79 | "┌ Info: Result\n", 80 | "│ i = 8\n", 81 | "│ x = [3.1415848245146822, 2.7182952709785266]\n", 82 | "└ @ Main In[45]:8\n" 83 | ] 84 | } 85 | ], 86 | "source": [ 87 | "epsilon = 0.0001\n", 88 | "x = [3.0, 4.0]\n", 89 | "for i in 1:100\n", 90 | " df = ForwardDiff.gradient(f, x)\n", 91 | " hs = ForwardDiff.hessian(f, x)\n", 92 | " newx = x - inv(hs) * df\n", 93 | " if sum((x .- newx) .|> abs) < 0.0001\n", 94 | " @info \"Result\" i x\n", 95 | " break\n", 96 | " end\n", 97 | " x = newx\n", 98 | "end" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": null, 104 | "id": "7d3ba4d3-b31c-4fc9-9ed9-a2ebe1642ed0", 105 | "metadata": {}, 106 | "outputs": [], 107 | "source": [] 108 | } 109 | ], 110 | "metadata": { 111 | "kernelspec": { 112 | "display_name": "Julia 1.7.1", 113 | "language": "julia", 114 | "name": "julia-1.7" 115 | }, 116 | "language_info": { 117 | "file_extension": ".jl", 118 | "mimetype": "application/julia", 119 | "name": "julia", 120 | "version": "1.7.1" 121 | } 122 | }, 123 | "nbformat": 4, 124 | "nbformat_minor": 5 125 | } 126 | -------------------------------------------------------------------------------- /nlsolve-example.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "8f5b26d4-49a4-4271-9078-94f3fedc9de6", 6 | "metadata": {}, 7 | "source": [ 8 | "$$\n", 9 | "\\begin{align}\n", 10 | "x^2 + xy = 28 \\\\\n", 11 | "y^2 + xy = 21\n", 12 | "\\end{align}\n", 13 | "$$" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 1, 19 | "id": "399d1d0e-47a3-4f37-afed-cd7eaeb85667", 20 | "metadata": {}, 21 | "outputs": [ 22 | { 23 | "name": "stderr", 24 | "output_type": "stream", 25 | "text": [ 26 | "┌ Info: Results of Nonlinear Solver Algorithm\n", 27 | "│ * Algorithm: Trust-region with dogleg and autoscaling\n", 28 | "│ * Starting Point: [1.0, 1.0]\n", 29 | "│ * Zero: [4.0, 3.0]\n", 30 | "│ * Inf-norm of residuals: 0.000000\n", 31 | "│ * Iterations: 6\n", 32 | "│ * Convergence: true\n", 33 | "│ * |x - x'| < 0.0e+00: false\n", 34 | "│ * |f(x)| < 1.0e-08: true\n", 35 | "│ * Function Calls (f): 7\n", 36 | "│ * Jacobian Calls (df/dx): 7\n", 37 | "└ @ Main In[1]:11\n" 38 | ] 39 | } 40 | ], 41 | "source": [ 42 | "using NLsolve\n", 43 | "\n", 44 | "function f!(F, x)\n", 45 | " F[1] = 28.0 - x[1]^2 - x[1] * x[2]\n", 46 | " F[2] = 21.0 - x[2]^2 - x[1] * x[2]\n", 47 | "end\n", 48 | "\n", 49 | "initial_x = [1.0, 1.0]\n", 50 | "result = nlsolve(f!, initial_x, autodiff = :forward)\n", 51 | "\n", 52 | "@info result" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": null, 58 | "id": "f5129ab9-f478-4f0a-ae2e-dfc379308a1e", 59 | "metadata": {}, 60 | "outputs": [], 61 | "source": [] 62 | } 63 | ], 64 | "metadata": { 65 | "kernelspec": { 66 | "display_name": "Julia 1.7.3", 67 | "language": "julia", 68 | "name": "julia-1.7" 69 | }, 70 | "language_info": { 71 | "file_extension": ".jl", 72 | "mimetype": "application/julia", 73 | "name": "julia", 74 | "version": "1.7.3" 75 | } 76 | }, 77 | "nbformat": 4, 78 | "nbformat_minor": 5 79 | } 80 | -------------------------------------------------------------------------------- /notes/or/assignment/assignment.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jbytecode/notebooks/0bb925e5599c6bcf130255d586b9c1d7f0e2595b/notes/or/assignment/assignment.pdf -------------------------------------------------------------------------------- /notes/or/assignment/assignment.tex: -------------------------------------------------------------------------------- 1 | \documentclass{article} 2 | \usepackage{float} 3 | \usepackage{geometry} 4 | \usepackage{amsmath} 5 | \usepackage{amssymb} 6 | 7 | \geometry{ 8 | left=2cm, 9 | top=2cm, 10 | right=2cm, 11 | bottom=2cm 12 | } 13 | 14 | 15 | \begin{document} 16 | 17 | \title{Assigment Problem Examples and Solutions} 18 | \author{Mehmet Hakan Satman (Ph.D.)} 19 | \maketitle 20 | \tableofcontents 21 | 22 | \section{Assignment Problem 1 and the Solution} 23 | \subsection{The Problem} 24 | \begin{table}[H] 25 | \centering 26 | \begin{tabular}{{|c|c|c|c|}} 27 | \hline 28 | & Task 1 & Task 2 & Task 3 \\ 29 | \hline 30 | Worker 1 & 10 & 17 & 9 \\ 31 | \hline 32 | Worker 2 & 11 & 19 & 14 \\ 33 | \hline 34 | Worker 3 & 12 & 15 & 12 \\ 35 | \hline 36 | \end{tabular} 37 | \label{} 38 | \caption{Times required to perform tasks by workers} 39 | \end{table} 40 | 41 | \subsection{The Mathematical Model} 42 | \begin{align*} 43 | \min z & = 10 x_{11} + 17 x_{12} + 9 x_{13} + \ldots + 12 x_{31} + 15 x_{32} + 12 x_{33} \\ 44 | \text{Subject to:} & \\ 45 | & x_{11} + x_{12} + x_{13} = 1 \\ 46 | & x_{21} + x_{22} + x_{23} = 1 \\ 47 | & x_{31} + x_{32} + x_{33} = 1 \\ 48 | & x_{11} + x_{21} + x_{31} = 1 \\ 49 | & x_{12} + x_{22} + x_{32} = 1 \\ 50 | & x_{13} + x_{23} + x_{33} = 1 \\ 51 | & x_{ij} \in \{0, 1\} \\ 52 | & i = 1, 2, 3 \\ 53 | & j = 1, 2, 3 \\ 54 | \end{align*} 55 | 56 | \subsection{The Solution} 57 | \begin{table}[H] 58 | \centering 59 | \begin{tabular}{{|c|c|c|c|}} 60 | \hline 61 | & Task 1 & Task 2 & Task 3 \\ 62 | \hline 63 | Worker 1 & & & \checkmark \\ 64 | \hline 65 | Worker 2 & \checkmark & & \\ 66 | \hline 67 | Worker 3 & & \checkmark & \\ 68 | \hline 69 | \end{tabular} 70 | \label{} 71 | \caption{Cost is 35} 72 | \end{table} 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 81 | 82 | \section{Assignment Problem 2 and the Solution} 83 | \subsection{The Problem} 84 | \begin{table}[H] 85 | \centering 86 | \begin{tabular}{{|c|c|c|c|c|}} 87 | \hline 88 | & Task 1 & Task 2 & Task 3 & Task 4 \\ 89 | \hline 90 | Worker 1 & 10 & 17 & 9 & 16\\ 91 | \hline 92 | Worker 2 & 11 & 19 & 14 & 8\\ 93 | \hline 94 | Worker 3 & 12 & 15 & 12 & 7 \\ 95 | \hline 96 | Worker 4 & 9 & 16 & 19 & 19 \\ 97 | \hline 98 | \end{tabular} 99 | \label{} 100 | \caption{Times required to perform tasks by workers} 101 | \end{table} 102 | 103 | 104 | 105 | 106 | 107 | \subsection{The Solution} 108 | \begin{table}[H] 109 | \centering 110 | \begin{tabular}{{|c|c|c|c|c|}} 111 | \hline 112 | & Task 1 & Task 2 & Task 3 & Task 4 \\ 113 | \hline 114 | Worker 1 & & & \checkmark & \\ 115 | \hline 116 | Worker 2 & & & & \checkmark \\ 117 | \hline 118 | Worker 3 & & \checkmark & & \\ 119 | \hline 120 | Worker 4 & \checkmark & & & \\ 121 | \hline 122 | \end{tabular} 123 | \label{} 124 | \caption{Cost is 41} 125 | \end{table} 126 | 127 | 128 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 129 | 130 | \section{Assignment Problem 3 and the Solution} 131 | 132 | \subsection{The Problem} 133 | \begin{table}[H] 134 | \centering 135 | \begin{tabular}{{|c|c|c|c|c|c|}} 136 | \hline 137 | & Task 1 & Task 2 & Task 3 & Task 4 & Task 5\\ 138 | \hline 139 | Worker 1 & 17 & 19 & 12 & 16 & 21\\ 140 | \hline 141 | Worker 2 & 19 & 11 & 15 & 8 & 10\\ 142 | \hline 143 | Worker 3 & 9 & 15 & 12 & 7 & 14\\ 144 | \hline 145 | Worker 4 & 16 & 13 & 22 & 19 & 22\\ 146 | \hline 147 | Worker 5 & 8 & 14 & 19 & 17 & 10\\ 148 | \hline 149 | \end{tabular} 150 | \label{} 151 | \caption{Times required to perform tasks by workers} 152 | \end{table} 153 | 154 | 155 | \subsection{The Solution} 156 | \begin{table}[H] 157 | \centering 158 | \begin{tabular}{{|c|c|c|c|c|c|}} 159 | \hline 160 | & Task 1 & Task 2 & Task 3 & Task 4 & Task 5\\ 161 | \hline 162 | Worker 1 & & & \checkmark & & \\ 163 | \hline 164 | Worker 2 & & & & & \checkmark\\ 165 | \hline 166 | Worker 3 & & & & \checkmark & \\ 167 | \hline 168 | Worker 4 & & \checkmark & & & \\ 169 | \hline 170 | Worker 5 & \checkmark & & & & \\ 171 | \hline 172 | \end{tabular} 173 | \label{} 174 | \caption{Cost is 50} 175 | \end{table} 176 | \end{document} -------------------------------------------------------------------------------- /notes/or/minimum-spanning-tree/mst-example.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jbytecode/notebooks/0bb925e5599c6bcf130255d586b9c1d7f0e2595b/notes/or/minimum-spanning-tree/mst-example.pdf -------------------------------------------------------------------------------- /notes/or/shortestpath.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jbytecode/notebooks/0bb925e5599c6bcf130255d586b9c1d7f0e2595b/notes/or/shortestpath.pdf -------------------------------------------------------------------------------- /notes/or/simplex/simplex.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jbytecode/notebooks/0bb925e5599c6bcf130255d586b9c1d7f0e2595b/notes/or/simplex/simplex.pdf -------------------------------------------------------------------------------- /notes/or/simplex/simplex.tex: -------------------------------------------------------------------------------- 1 | \documentclass{article} 2 | \usepackage{float} 3 | \usepackage{geometry} 4 | \usepackage{amsmath} 5 | 6 | \geometry{ 7 | left=2cm, 8 | top=2cm, 9 | right=2cm, 10 | bottom=2cm 11 | } 12 | 13 | 14 | \begin{document} 15 | 16 | \title{Simplex Examples and Solutions} 17 | \author{Mehmet Hakan Satman (Ph.D.)} 18 | \maketitle 19 | \tableofcontents 20 | 21 | \section{Linear Programming Problem 1 and the Simplex Solution} 22 | \subsection{The Problem} 23 | \begin{align*} 24 | \max z & = 5 x_1 + 10 x_2 \\ 25 | \text{Subject to:} & \\ 26 | & 2 x_1 + 4 x_2 \le 640 \\ 27 | & 6 x_1 + 2 x_2 \le 480 \\ 28 | & x_1, x_2 \ge 0 29 | \end{align*} 30 | 31 | \subsection{Initial Table} 32 | \begin{table}[H] 33 | \centering 34 | \begin{tabular}{{|c|c|c|c|c|c|}} 35 | \hline 36 | & x1 & x2 & x3 & x4 & Solution \\ 37 | \hline 38 | z & -5.0 & -10.0 & -0.0 & -0.0 & 0.0\\ 39 | \hline 40 | x3 & 2.0 & 4.0 & 1.0 & 0.0 & 640.0\\ 41 | \hline 42 | x4 & 6.0 & 2.0 & 0.0 & 1.0 & 480.0\\ 43 | \hline 44 | \end{tabular} 45 | \label{} 46 | \caption{$x_3$ and $x_4$ are slack variables} 47 | \end{table} 48 | 49 | \subsection{Iteration 1} 50 | \begin{table}[H] 51 | \centering 52 | \begin{tabular}{{|c|c|c|c|c|c|}} 53 | \hline 54 | & x1 & x2 & x3 & x4 & Solution \\ 55 | \hline 56 | z & -0.0 & -0.0 & 2.5 & -0.0 & 1600.0\\ 57 | \hline 58 | x2 & 0.5 & 1.0 & 0.25 & 0.0 & 160.0\\ 59 | \hline 60 | x4 & 5.0 & 0.0 & -0.5 & 1.0 & 160.0\\ 61 | \hline 62 | \end{tabular} 63 | \label{} 64 | \caption{No entering variables, algorithm terminates} 65 | \end{table} 66 | 67 | \subsection{Solution Set} 68 | $$ 69 | \text{Solution} = 70 | \begin{Bmatrix} 71 | x_1 & = & 0 \\ 72 | x_2 & = & 160 \\ 73 | x_3 & = & 0 \\ 74 | x_4 & = & 160 \\ 75 | z_{\max} & = & 1600 76 | \end{Bmatrix} 77 | $$ 78 | 79 | \section{Linear Programming Problem 2 and the Simplex Solution} 80 | \subsection{The Problem} 81 | \begin{align*} 82 | \max z & = 5 x_1 + 6 x_2 + 4 x_3 \\ 83 | \text{Subject to:} & \\ 84 | & 2 x_1 + 4 x_2 + 6 x_3 \le 640 \\ 85 | & 2 x_1 + 3 x_2 + x_3 \le 960 \\ 86 | & x_1, x_2, x_3 \ge 0 87 | \end{align*} 88 | 89 | \subsection{Initial Table} 90 | \begin{table}[H] 91 | \centering 92 | \begin{tabular}{{|c|c|c|c|c|c|c|}} 93 | \hline 94 | & x1 & x2 & x3 & x4 & x5 & Solution \\ 95 | \hline 96 | z & -5.0 & -6.0 & -4.0 & -0.0 & -0.0 & 0.0\\ 97 | \hline 98 | x4 & 2.0 & 4.0 & 6.0 & 1.0 & 0.0 & 640.0\\ 99 | \hline 100 | x5 & 2.0 & 3.0 & 1.0 & 0.0 & 1.0 & 960.0\\ 101 | \hline 102 | \end{tabular} 103 | \label{} 104 | \caption{$x_4$ and $x_5$ are slack variables} 105 | \end{table} 106 | 107 | 108 | 109 | \subsection{Iteration 1} 110 | \begin{table}[H] 111 | \centering 112 | \begin{tabular}{{|c|c|c|c|c|c|c|}} 113 | \hline 114 | & x1 & x2 & x3 & x4 & x5 & Solution \\ 115 | \hline 116 | z & -2.0 & -0.0 & 5.0 & 1.5 & -0.0 & 960.0\\ 117 | \hline 118 | x2 & 0.5 & 1.0 & 1.5 & 0.25 & 0.0 & 160.0\\ 119 | \hline 120 | x5 & 0.5 & 0.0 & -3.5 & -0.75 & 1.0 & 480.0\\ 121 | \hline 122 | \end{tabular} 123 | \label{} 124 | \caption{} 125 | \end{table} 126 | 127 | 128 | 129 | \subsection{iteration 2} 130 | \begin{table}[H] 131 | \centering 132 | \begin{tabular}{{|c|c|c|c|c|c|c|}} 133 | \hline 134 | & x1 & x2 & x3 & x4 & x5 & Solution \\ 135 | \hline 136 | z & -0.0 & 4.0 & 11.0 & 2.5 & -0.0 & 1600.0\\ 137 | \hline 138 | x1 & 1.0 & 2.0 & 3.0 & 0.5 & 0.0 & 320.0\\ 139 | \hline 140 | x5 & 0.0 & -1.0 & -5.0 & -1.0 & 1.0 & 320.0\\ 141 | \hline 142 | \end{tabular} 143 | \label{} 144 | \caption{No entering variables, algorithm terminates} 145 | \end{table} 146 | 147 | \subsection{Solution Set} 148 | $$ 149 | \text{Solution} = 150 | \begin{Bmatrix} 151 | x_1 & = & 320 \\ 152 | x_2 & = & 0 \\ 153 | x_3 & = & 0 \\ 154 | x_4 & = & 0 \\ 155 | x_5 & = & 320 \\ 156 | z_{\max} & = & 1600 157 | \end{Bmatrix} 158 | $$ 159 | 160 | 161 | 162 | \section{Linear Programming Problem 3 and the Simplex Solution} 163 | \subsection{The Problem} 164 | 165 | \begin{align*} 166 | \max z & = 3 x_1 + 5 x_2 + 4 x_3 \\ 167 | \text{Subject to:} & \\ 168 | & x_1 + 2 x_2 + 5 x_3 \le 120 \\ 169 | & 3 x_1 + x_2 + 4 x_3 \le 240 \\ 170 | & 2 x_1 + 2 x_2 + x_3 \le 360 \\ 171 | & x_1, x_2, x_3 \ge 0\\ 172 | \end{align*} 173 | 174 | \subsection{Initial Table} 175 | \begin{table}[H] 176 | \centering 177 | \begin{tabular}{{|c|c|c|c|c|c|c|c|}} 178 | \hline 179 | & x1 & x2 & x3 & x4 & x5 & x6 & Solution \\ 180 | \hline 181 | z & -3.0 & -5.0 & -4.0 & -0.0 & -0.0 & -0.0 & 0.0\\ 182 | \hline 183 | x4 & 1.0 & 2.0 & 5.0 & 1.0 & 0.0 & 0.0 & 120.0\\ 184 | \hline 185 | x5 & 3.0 & 1.0 & 4.0 & 0.0 & 1.0 & 0.0 & 240.0\\ 186 | \hline 187 | x6 & 2.0 & 2.0 & 1.0 & 0.0 & 0.0 & 1.0 & 360.0\\ 188 | \hline 189 | \end{tabular} 190 | \label{} 191 | \caption{$x_4$, $x_5$, $x_6$ are slack variables} 192 | \end{table} 193 | 194 | 195 | \subsection{Iteration 1} 196 | \begin{table}[H] 197 | \centering 198 | \begin{tabular}{{|c|c|c|c|c|c|c|c|}} 199 | \hline 200 | & x1 & x2 & x3 & x4 & x5 & x6 & Solution \\ 201 | \hline 202 | z & -0.5 & -0.0 & 8.5 & 2.5 & -0.0 & -0.0 & 300.0\\ 203 | \hline 204 | x2 & 0.5 & 1.0 & 2.5 & 0.5 & 0.0 & 0.0 & 60.0\\ 205 | \hline 206 | x5 & 2.5 & 0.0 & 1.5 & -0.5 & 1.0 & 0.0 & 180.0\\ 207 | \hline 208 | x6 & 1.0 & 0.0 & -4.0 & -1.0 & 0.0 & 1.0 & 240.0\\ 209 | \hline 210 | \end{tabular} 211 | \label{} 212 | \caption{} 213 | \end{table} 214 | 215 | \subsection{Iteration 2} 216 | \begin{table}[H] 217 | \centering 218 | \begin{tabular}{{|c|c|c|c|c|c|c|c|}} 219 | \hline 220 | & x1 & x2 & x3 & x4 & x5 & x6 & Solution \\ 221 | \hline 222 | z & -0.0 & -0.0 & 8.8 & 2.4 & 0.2 & -0.0 & 336.0\\ 223 | \hline 224 | x2 & 0.0 & 1.0 & 2.2 & 0.6 & -0.2 & 0.0 & 24.0\\ 225 | \hline 226 | x1 & 1.0 & 0.0 & 0.6 & -0.2 & 0.4 & 0.0 & 72.0\\ 227 | \hline 228 | x6 & 0.0 & 0.0 & -4.6 & -0.8 & -0.4 & 1.0 & 168.0\\ 229 | \hline 230 | \end{tabular} 231 | \label{} 232 | \caption{Final Table} 233 | \end{table} 234 | 235 | \subsection{Solution Set} 236 | $$ 237 | \text{Solution} = 238 | \begin{Bmatrix} 239 | x_1 & = & 72 \\ 240 | x_2 & = & 24 \\ 241 | x_3 & = & 0 \\ 242 | x_4 & = & 0 \\ 243 | x_5 & = & 0 \\ 244 | x_6 & = & 168 \\ 245 | z_{\max} & = & 336 246 | \end{Bmatrix} 247 | $$ 248 | 249 | 250 | \end{document} -------------------------------------------------------------------------------- /notes/or/transportation/transportation-problems.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jbytecode/notebooks/0bb925e5599c6bcf130255d586b9c1d7f0e2595b/notes/or/transportation/transportation-problems.pdf -------------------------------------------------------------------------------- /notes/or/transportation/transportation-problems.tex: -------------------------------------------------------------------------------- 1 | \documentclass{article} 2 | \usepackage{float} 3 | \usepackage{geometry} 4 | 5 | \geometry{ 6 | left=2cm, 7 | top=2cm, 8 | right=2cm, 9 | bottom=2cm 10 | } 11 | 12 | 13 | \begin{document} 14 | 15 | \title{Transportation Problem Examples} 16 | \author{Mehmet Hakan Satman (Ph.D.)} 17 | \maketitle 18 | \tableofcontents 19 | 20 | \section{Transportation Problem 1} 21 | \subsection{The Problem} 22 | \begin{table}[H] 23 | \begin{tabular}{|c|c|c|c|c|} 24 | \hline 25 | & $D_1$ & $D_2$ & $D_3$ & Supply \\ 26 | \hline 27 | $S_1$ & 10.0 & 20.0 & 15.0 & 60.0\\ 28 | $S_2$ & 22.0 & 9.0 & 21.0 & 60.0\\ 29 | \hline 30 | Demand & 30.0 & 40.0 & 50.0 & \\ 31 | \hline 32 | \end{tabular} 33 | \label{tbl:} 34 | \caption{} 35 | \end{table} 36 | 37 | \subsection{The Solution} 38 | \begin{table}[H] 39 | \begin{tabular}{|c|c|c|c|c|} 40 | \hline 41 | & $D_1$ & $D_2$ & $D_3$ & Supply \\ 42 | \hline 43 | $S_1$ & 10.0 \tiny{(30.0)} & 20.0 & 15.0 \tiny{(30.0)} & 60.0\\ 44 | $S_2$ & 22.0 & 9.0 \tiny{(40.0)} & 21.0 \tiny{(20.0)} & 60.0\\ 45 | \hline 46 | Demand & 30.0 & 40.0 & 50.0 & \\ 47 | \hline 48 | \end{tabular} 49 | \label{tbl:} 50 | \caption{Total cost: 1530.0} 51 | \end{table} 52 | 53 | 54 | \section{Transportation Problem 2} 55 | 56 | \subsection{The Problem} 57 | 58 | \begin{table}[H] 59 | \begin{tabular}{|c|c|c|c|c|c|} 60 | \hline 61 | & $D_1$ & $D_2$ & $D_3$ & $D_4$ & Supply \\ 62 | \hline 63 | $S_1$ & 94.0 & 23.0 & 57.0 & 66.0 & 88.0\\ 64 | $S_2$ & 77.0 & 12.0 & 59.0 & 27.0 & 68.0\\ 65 | \hline 66 | Demand & 13.0 & 81.0 & 70.0 & 48.0 & \\ 67 | \hline 68 | \end{tabular} 69 | \label{tbl:} 70 | \caption{Problem 1} 71 | \end{table} 72 | 73 | \subsection{The Solution} 74 | 75 | \begin{table}[H] 76 | \begin{tabular}{|c|c|c|c|c|c|} 77 | \hline 78 | & $D_1$ & $D_2$ & $D_3$ & $D_4$ & Supply \\ 79 | \hline 80 | $S_1$ & 94.0 & 23.0 \tiny{(61.0)} & 57.0 \tiny{(27.0)} & 66.0 & 88.0\\ 81 | $S_2$ & 77.0 & 12.0 \tiny{(20.0)} & 59.0 & 27.0 \tiny{(48.0)} & 68.0\\ 82 | $S_3$ & 0.0 \tiny{(13.0)} & 0.0 & 0.0 \tiny{(43.0)} & 0.0 & 56.0\\ 83 | \hline 84 | Demand & 13.0 & 81.0 & 70.0 & 48.0 & \\ 85 | \hline 86 | \end{tabular} 87 | \label{tbl:} 88 | \caption{Total cost: 4478.0} 89 | \end{table} 90 | 91 | 92 | \section{Transportation Problem 3} 93 | \subsection{The Problem} 94 | \begin{table}[H] 95 | \begin{tabular}{|c|c|c|c|} 96 | \hline 97 | & $D_1$ & $D_2$ & Supply \\ 98 | \hline 99 | $S_1$ & 10.0 & 20.0 & 60.0\\ 100 | $S_2$ & 22.0 & 9.0 & 60.0\\ 101 | $S_3$ & 10.0 & 30.0 & 30.0\\ 102 | \hline 103 | Demand & 30.0 & 40.0 & \\ 104 | \hline 105 | \end{tabular} 106 | \label{tbl:} 107 | \caption{Problem} 108 | \end{table} 109 | 110 | \subsection{Initial Feasible Solution with North-West Corner} 111 | \begin{table}[H] 112 | \begin{tabular}{|c|c|c|c|c|} 113 | \hline 114 | & $D_1$ & $D_2$ & $D_3$ & Supply \\ 115 | \hline 116 | $S_1$ & 10.0 \tiny{(30.0)} & 20.0 \tiny{(30.0)} & 0.0 & 60.0\\ 117 | $S_2$ & 22.0 & 9.0 \tiny{(10.0)} & 0.0 \tiny{(50.0)} & 60.0\\ 118 | $S_3$ & 10.0 & 30.0 & 0.0 \tiny{(30.0)} & 30.0\\ 119 | \hline 120 | Demand & 30.0 & 40.0 & 80.0 & \\ 121 | \hline 122 | \end{tabular} 123 | \label{tbl:} 124 | \caption{Total cost: 990.0} 125 | \end{table} 126 | 127 | 128 | \subsection{The Solution} 129 | \begin{table}[H] 130 | \begin{tabular}{|c|c|c|c|c|} 131 | \hline 132 | & $D_1$ & $D_2$ & $D_3$ & Supply \\ 133 | \hline 134 | $S_1$ & 10.0 & 20.0 & 0.0 \tiny{(60.0)} & 60.0\\ 135 | $S_2$ & 22.0 & 9.0 \tiny{(40.0)} & 0.0 \tiny{(20.0)} & 60.0\\ 136 | $S_3$ & 10.0 \tiny{(30.0)} & 30.0 & 0.0 & 30.0\\ 137 | \hline 138 | Demand & 30.0 & 40.0 & 80.0 & \\ 139 | \hline 140 | \end{tabular} 141 | \label{tbl:} 142 | \caption{Total cost: 660.0} 143 | \end{table} 144 | 145 | 146 | 147 | \end{document} 148 | -------------------------------------------------------------------------------- /notes/or/yoneylem-rassal-sayi-tablosu.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jbytecode/notebooks/0bb925e5599c6bcf130255d586b9c1d7f0e2595b/notes/or/yoneylem-rassal-sayi-tablosu.pdf -------------------------------------------------------------------------------- /nsga2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 108, 6 | "id": "ef8b29f0", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "using Metaheuristics" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "id": "0c3b3628", 16 | "metadata": {}, 17 | "source": [ 18 | "Suppose the model is \n", 19 | "\n", 20 | "$$\n", 21 | "y = 5 + 5x + \\varepsilon\n", 22 | "$$\n", 23 | "\n", 24 | "The first objective is to find $\\hat{\\beta_0}$ and $\\hat{\\beta_1}$ that minimize the sum of squared residuals:\n", 25 | "\n", 26 | "\n", 27 | "$$\n", 28 | "\\sum e^2\n", 29 | "$$\n", 30 | "\n", 31 | "where $e = y - \\hat{y}$. The second objective is to minimize \n", 32 | "\n", 33 | "$$\n", 34 | "\\Big| 4 - \\sum \\hat{\\beta} \\Big|\n", 35 | "$$" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 109, 41 | "id": "9e79d76c", 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "data": { 46 | "text/plain": [ 47 | "2×2 Matrix{Float64}:\n", 48 | " -10.0 -10.0\n", 49 | " 10.0 10.0" 50 | ] 51 | }, 52 | "execution_count": 109, 53 | "metadata": {}, 54 | "output_type": "execute_result" 55 | } 56 | ], 57 | "source": [ 58 | "n = 30\n", 59 | "x = rand(n)\n", 60 | "e = rand(n)\n", 61 | "y = 5 .+ 5 * x .+ e\n", 62 | "\n", 63 | "function f1(betas)::Float64\n", 64 | " res = y .- betas[1] - betas[2] * x \n", 65 | " return sum(res .^ 2.0)\n", 66 | "end \n", 67 | "\n", 68 | "function f2(betas)::Float64\n", 69 | " return abs(sum(betas) - 4.0)\n", 70 | "end \n", 71 | "\n", 72 | "function f(x)\n", 73 | " return ([f1(x), f2(x)], [0.0], [0.0])\n", 74 | "end\n", 75 | "\n", 76 | "bounds = [-10.0 -10.0; 10.0 10.0]" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 110, 82 | "id": "e5e6d2c9", 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "data": { 87 | "text/html": [ 88 | "
\n", 89 | "Optimization Result\n", 90 | "===================\n", 91 | " Iteration: 251\n", 92 | " Non-dominated: 100 / 100\n", 93 | " Function calls: 50100\n", 94 | " Feasibles: 100 / 100 in final population\n", 95 | " Total time: 0.6327 s\n", 96 | " Stop reason: Maximum objective function calls exceeded.\n", 97 | "\n" 98 | ], 99 | "text/plain": [ 100 | "\u001b[34m\u001b[1mOptimization Result\u001b[22m\u001b[39m\n", 101 | "\u001b[39m===================\u001b[39m\n", 102 | " Iteration: 251\n", 103 | " Non-dominated: 100 / 100\n", 104 | " Function calls: 50100\n", 105 | " Feasibles: 100 / 100 in final population\n", 106 | " Total time: 0.6327 s\n", 107 | " Stop reason: Maximum objective function calls exceeded.\n" 108 | ] 109 | }, 110 | "execution_count": 110, 111 | "metadata": {}, 112 | "output_type": "execute_result" 113 | } 114 | ], 115 | "source": [ 116 | "result = optimize(f, bounds, NSGA2(N = 100, p_cr = 0.90, p_m=0.01))" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 111, 122 | "id": "36089189", 123 | "metadata": {}, 124 | "outputs": [ 125 | { 126 | "data": { 127 | "text/plain": [ 128 | "(f = [720.367, 0.403679], g = [0.0], h = [0.0], x = [2.03569, 2.36799])" 129 | ] 130 | }, 131 | "execution_count": 111, 132 | "metadata": {}, 133 | "output_type": "execute_result" 134 | } 135 | ], 136 | "source": [ 137 | "result.best_sol" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 112, 143 | "id": "96b2b70b", 144 | "metadata": {}, 145 | "outputs": [ 146 | { 147 | "data": { 148 | "text/plain": [ 149 | "720.3669771927136" 150 | ] 151 | }, 152 | "execution_count": 112, 153 | "metadata": {}, 154 | "output_type": "execute_result" 155 | } 156 | ], 157 | "source": [ 158 | "f1(result.best_sol.x)" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 113, 164 | "id": "af60142d", 165 | "metadata": {}, 166 | "outputs": [ 167 | { 168 | "data": { 169 | "text/plain": [ 170 | "0.40367923763018787" 171 | ] 172 | }, 173 | "execution_count": 113, 174 | "metadata": {}, 175 | "output_type": "execute_result" 176 | } 177 | ], 178 | "source": [ 179 | "f2(result.best_sol.x)" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": null, 185 | "id": "3cc3cab6", 186 | "metadata": {}, 187 | "outputs": [], 188 | "source": [] 189 | } 190 | ], 191 | "metadata": { 192 | "kernelspec": { 193 | "display_name": "Julia 1.9.0-rc3", 194 | "language": "julia", 195 | "name": "julia-1.9" 196 | }, 197 | "language_info": { 198 | "file_extension": ".jl", 199 | "mimetype": "application/julia", 200 | "name": "julia", 201 | "version": "1.9.0" 202 | } 203 | }, 204 | "nbformat": 4, 205 | "nbformat_minor": 5 206 | } 207 | -------------------------------------------------------------------------------- /optimal system design.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 7, 6 | "id": "8045ddf4", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "name": "stderr", 11 | "output_type": "stream", 12 | "text": [ 13 | "\u001b[32m\u001b[1m Activating\u001b[22m\u001b[39m project at `~/code/julia/notebooks`\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "using Pkg\n", 19 | "Pkg.activate(\".\")" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 8, 25 | "id": "92ab1f4a", 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "using JuMP, HiGHS" 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "id": "b39e77a9", 35 | "metadata": {}, 36 | "source": [ 37 | "| | Product 1 | Product 2 | Capacity |\n", 38 | "| :----: | :---------: | :----------: | :-------------: |\n", 39 | "| Labour | 2 | 3 | a |\n", 40 | "| Machine | 4 | 2 | b |\n", 41 | "| Profit | 3 | 3 | |" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "id": "1aa1793d", 47 | "metadata": {}, 48 | "source": [ 49 | "- The total capacity of sources is limited to 500 hours" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "id": "8e273140", 55 | "metadata": {}, 56 | "source": [ 57 | "$$\n", 58 | "\\begin{aligned}\n", 59 | "\\max z = & 3x + 3y \\\\\n", 60 | "\\text{Subject to:} & \\\\\n", 61 | "& 2x + 3y \\le a \\\\\n", 62 | "& 4x + 2y \\le b \\\\\n", 63 | "& a + b \\le 500 \\\\\n", 64 | "& x, y, a, b \\ge 0 \\\\\n", 65 | "\\end{aligned}\n", 66 | "$$" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 9, 72 | "id": "514f2668", 73 | "metadata": {}, 74 | "outputs": [ 75 | { 76 | "name": "stdout", 77 | "output_type": "stream", 78 | "text": [ 79 | "Running HiGHS 1.10.0 (git hash: fd8665394e): Copyright (c) 2025 HiGHS under MIT licence terms\n", 80 | "LP has 3 rows; 4 cols; 8 nonzeros\n", 81 | "Coefficient ranges:\n", 82 | " Matrix [1e+00, 4e+00]\n", 83 | " Cost [3e+00, 3e+00]\n", 84 | " Bound [0e+00, 0e+00]\n", 85 | " RHS [5e+02, 5e+02]\n", 86 | "Presolving model\n", 87 | "3 rows, 4 cols, 8 nonzeros 0s\n", 88 | "2 rows, 3 cols, 6 nonzeros 0s\n", 89 | "1 rows, 2 cols, 2 nonzeros 0s\n", 90 | "0 rows, 0 cols, 0 nonzeros 0s\n", 91 | "Presolve : Reductions: rows 0(-3); columns 0(-4); elements 0(-8) - Reduced to empty\n", 92 | "Solving the original LP from the solution after postsolve\n", 93 | "Model status : Optimal\n", 94 | "Objective value : 3.0000000000e+02\n", 95 | "Relative P-D gap : 0.0000000000e+00\n", 96 | "HiGHS run time : 0.00\n" 97 | ] 98 | } 99 | ], 100 | "source": [ 101 | "m = Model(HiGHS.Optimizer)\n", 102 | "@variable(m, x >= 0)\n", 103 | "@variable(m, y >= 0)\n", 104 | "@variable(m, a >= 0)\n", 105 | "@variable(m, b >= 0)\n", 106 | "@objective(m, Max, 3x + 3y)\n", 107 | "@constraint(m, 2x + 3y <= a)\n", 108 | "@constraint(m, 4x + 2y <= b)\n", 109 | "@constraint(m, a + b <= 500)\n", 110 | "optimize!(m)" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 10, 116 | "id": "cd644760", 117 | "metadata": {}, 118 | "outputs": [ 119 | { 120 | "data": { 121 | "text/plain": [ 122 | "true" 123 | ] 124 | }, 125 | "execution_count": 10, 126 | "metadata": {}, 127 | "output_type": "execute_result" 128 | } 129 | ], 130 | "source": [ 131 | "JuMP.is_solved_and_feasible(m)" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": 11, 137 | "id": "b3d169ef", 138 | "metadata": {}, 139 | "outputs": [ 140 | { 141 | "data": { 142 | "text/plain": [ 143 | "4-element Vector{Float64}:\n", 144 | " 0.0\n", 145 | " 100.0\n", 146 | " 300.0\n", 147 | " 200.0" 148 | ] 149 | }, 150 | "execution_count": 11, 151 | "metadata": {}, 152 | "output_type": "execute_result" 153 | } 154 | ], 155 | "source": [ 156 | "value.([x, y, a, b])" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 12, 162 | "id": "1b58ffbc", 163 | "metadata": {}, 164 | "outputs": [ 165 | { 166 | "data": { 167 | "text/plain": [ 168 | "300.0" 169 | ] 170 | }, 171 | "execution_count": 12, 172 | "metadata": {}, 173 | "output_type": "execute_result" 174 | } 175 | ], 176 | "source": [ 177 | "JuMP.objective_value(m)" 178 | ] 179 | }, 180 | { 181 | "cell_type": "markdown", 182 | "id": "4d185630", 183 | "metadata": {}, 184 | "source": [ 185 | "- $x = 0$\n", 186 | "- $y = 100$\n", 187 | "- $a = 300$\n", 188 | "- $b = 200$\n", 189 | "- $z = 300$" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": null, 195 | "id": "f89ecc84", 196 | "metadata": {}, 197 | "outputs": [], 198 | "source": [] 199 | } 200 | ], 201 | "metadata": { 202 | "kernelspec": { 203 | "display_name": "Julia 1.11.4", 204 | "language": "julia", 205 | "name": "julia-1.11" 206 | }, 207 | "language_info": { 208 | "file_extension": ".jl", 209 | "mimetype": "application/julia", 210 | "name": "julia", 211 | "version": "1.11.4" 212 | } 213 | }, 214 | "nbformat": 4, 215 | "nbformat_minor": 5 216 | } 217 | -------------------------------------------------------------------------------- /p-median.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "a136407c", 6 | "metadata": {}, 7 | "source": [ 8 | "$$\n", 9 | "z_{ij} = \n", 10 | "\\begin{Bmatrix}\n", 11 | "1, \\text{if the ith facility is assigned to the jth center} \\\\\n", 12 | "0, \\text{otherwise}\n", 13 | "\\end{Bmatrix}\n", 14 | "$$" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "id": "66d9156d", 20 | "metadata": {}, 21 | "source": [ 22 | "$$\n", 23 | "y_j \n", 24 | "= \n", 25 | "\\begin{Bmatrix}\n", 26 | "1, \\text{if jth location is selected as the location for a depot} \\\\\n", 27 | "0, \\text{otherwise}\n", 28 | "\\end{Bmatrix}\n", 29 | "$$" 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "id": "d230caf4", 35 | "metadata": {}, 36 | "source": [ 37 | "$$\n", 38 | "d_{ij} = \\text{Distance between ith and jth location}\n", 39 | "$$" 40 | ] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "id": "79fa518c", 45 | "metadata": {}, 46 | "source": [ 47 | "$$\n", 48 | "a_{i} = \\text{Weight (demand) of the ith location}\n", 49 | "$$" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "id": "7f73ac33", 55 | "metadata": {}, 56 | "source": [ 57 | "$$\n", 58 | "\\begin{aligned}\n", 59 | "\\min z = & \\sum_{i=1}^{n} \\sum_{j=1}^n a_i d_{ij} z_{ij} \\\\\n", 60 | "\\text{Subject to:} & \\\\\n", 61 | "& \\sum_{j = 1}^{n} z_{ij = 1} \\;\\;\\;, \\forall_i, \\;\\;\\; i, j = 1,2,\\ldots, n \\\\\n", 62 | "& z_{ij} \\le y_j \\;\\;\\;, \\forall_{i, j}\\;\\;\\; i, j = 1,2,\\ldots, n \\\\\n", 63 | "& \\sum_{j=1}^n y_j = \\text{number_of_depots} \\\\\n", 64 | "& z_{ij}, y_j \\in \\{0,1\\} \\;\\;\\; i, j = 1,2,\\ldots, n \\\\\n", 65 | "& a_{ij} \\ge 0 \\\\\n", 66 | "\\end{aligned}\n", 67 | "$$" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 2, 73 | "id": "55a59845", 74 | "metadata": {}, 75 | "outputs": [], 76 | "source": [ 77 | "using JuMP, HiGHS" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 32, 83 | "id": "843eae19", 84 | "metadata": {}, 85 | "outputs": [ 86 | { 87 | "data": { 88 | "text/plain": [ 89 | "2" 90 | ] 91 | }, 92 | "execution_count": 32, 93 | "metadata": {}, 94 | "output_type": "execute_result" 95 | } 96 | ], 97 | "source": [ 98 | "number_of_depots = 2" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 3, 104 | "id": "f0b94343", 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "data": { 109 | "text/plain": [ 110 | "6×2 Matrix{Float64}:\n", 111 | " 1.0 5.0\n", 112 | " 2.0 4.0\n", 113 | " 4.0 3.0\n", 114 | " 2.0 1.0\n", 115 | " 7.0 8.0\n", 116 | " 8.0 7.0" 117 | ] 118 | }, 119 | "execution_count": 3, 120 | "metadata": {}, 121 | "output_type": "execute_result" 122 | } 123 | ], 124 | "source": [ 125 | "coords = Float64[\n", 126 | " 1 5;\n", 127 | " 2 4;\n", 128 | " 4 3;\n", 129 | " 2 1;\n", 130 | " 7 8;\n", 131 | " 8 7\n", 132 | "]" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 4, 138 | "id": "9c87f422", 139 | "metadata": {}, 140 | "outputs": [ 141 | { 142 | "data": { 143 | "text/plain": [ 144 | "euclidean (generic function with 1 method)" 145 | ] 146 | }, 147 | "execution_count": 4, 148 | "metadata": {}, 149 | "output_type": "execute_result" 150 | } 151 | ], 152 | "source": [ 153 | "function euclidean(u, v)\n", 154 | " return ((u .- v) .* (u .- v)) |> sum |> sqrt\n", 155 | "end" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 5, 161 | "id": "f7182d72", 162 | "metadata": {}, 163 | "outputs": [ 164 | { 165 | "data": { 166 | "text/plain": [ 167 | "(6, 2)" 168 | ] 169 | }, 170 | "execution_count": 5, 171 | "metadata": {}, 172 | "output_type": "execute_result" 173 | } 174 | ], 175 | "source": [ 176 | "n, p = size(coords)" 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": 6, 182 | "id": "3da2d1ff", 183 | "metadata": {}, 184 | "outputs": [ 185 | { 186 | "data": { 187 | "text/plain": [ 188 | "6×6 Matrix{Float64}:\n", 189 | " 0.0 0.0 0.0 0.0 0.0 0.0\n", 190 | " 0.0 0.0 0.0 0.0 0.0 0.0\n", 191 | " 0.0 0.0 0.0 0.0 0.0 0.0\n", 192 | " 0.0 0.0 0.0 0.0 0.0 0.0\n", 193 | " 0.0 0.0 0.0 0.0 0.0 0.0\n", 194 | " 0.0 0.0 0.0 0.0 0.0 0.0" 195 | ] 196 | }, 197 | "execution_count": 6, 198 | "metadata": {}, 199 | "output_type": "execute_result" 200 | } 201 | ], 202 | "source": [ 203 | "distances = zeros(Float64, n, n)" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": 8, 209 | "id": "bb0bf35e", 210 | "metadata": {}, 211 | "outputs": [], 212 | "source": [ 213 | "for i in 1:n\n", 214 | " for j in i:n\n", 215 | " d = euclidean(coords[i, :], coords[j, :])\n", 216 | " distances[i, j] = d\n", 217 | " distances[j, i] = d\n", 218 | " end \n", 219 | "end " 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": 9, 225 | "id": "e4c53f77", 226 | "metadata": {}, 227 | "outputs": [ 228 | { 229 | "data": { 230 | "text/plain": [ 231 | "6×6 Matrix{Float64}:\n", 232 | " 0.0 1.41421 3.60555 4.12311 6.7082 7.28011\n", 233 | " 1.41421 0.0 2.23607 3.0 6.40312 6.7082\n", 234 | " 3.60555 2.23607 0.0 2.82843 5.83095 5.65685\n", 235 | " 4.12311 3.0 2.82843 0.0 8.60233 8.48528\n", 236 | " 6.7082 6.40312 5.83095 8.60233 0.0 1.41421\n", 237 | " 7.28011 6.7082 5.65685 8.48528 1.41421 0.0" 238 | ] 239 | }, 240 | "execution_count": 9, 241 | "metadata": {}, 242 | "output_type": "execute_result" 243 | } 244 | ], 245 | "source": [ 246 | "distances" 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": 33, 252 | "id": "7bc95e7c", 253 | "metadata": {}, 254 | "outputs": [ 255 | { 256 | "name": "stdout", 257 | "output_type": "stream", 258 | "text": [ 259 | "Running HiGHS 1.5.1 [date: 1970-01-01, git hash: 93f1876e4]\n", 260 | "Copyright (c) 2023 HiGHS under MIT licence terms\n", 261 | "Presolving model\n", 262 | "43 rows, 42 cols, 114 nonzeros\n", 263 | "43 rows, 42 cols, 114 nonzeros\n", 264 | "\n", 265 | "Solving MIP model with:\n", 266 | " 43 rows\n", 267 | " 42 cols (42 binary, 0 integer, 0 implied int., 0 continuous)\n", 268 | " 114 nonzeros\n", 269 | "\n", 270 | " Nodes | B&B Tree | Objective Bounds | Dynamic Constraints | Work \n", 271 | " Proc. InQueue | Leaves Expl. | BestBound BestSol Gap | Cuts InLp Confl. | LpIters Time\n", 272 | "\n", 273 | " 0 0 0 0.00% 0 inf inf 0 0 0 0 0.0s\n", 274 | " T 0 0 0 0.00% 0 8.064495102 100.00% 0 0 0 17 0.0s\n", 275 | "\n", 276 | "Solving report\n", 277 | " Status Optimal\n", 278 | " Primal bound 8.06449510225\n", 279 | " Dual bound 8.06449510225\n", 280 | " Gap 0% (tolerance: 0.01%)\n", 281 | " Solution status feasible\n", 282 | " 8.06449510225 (objective)\n", 283 | " 0 (bound viol.)\n", 284 | " 0 (int. viol.)\n", 285 | " 0 (row viol.)\n", 286 | " Timing 0.00 (total)\n", 287 | " 0.00 (presolve)\n", 288 | " 0.00 (postsolve)\n", 289 | " Nodes 1\n", 290 | " LP iterations 17 (total)\n", 291 | " 0 (strong br.)\n", 292 | " 0 (separation)\n", 293 | " 0 (heuristics)\n" 294 | ] 295 | }, 296 | { 297 | "data": { 298 | "text/plain": [ 299 | "6-element Vector{Float64}:\n", 300 | " -0.0\n", 301 | " 1.0\n", 302 | " 0.0\n", 303 | " -0.0\n", 304 | " 0.0\n", 305 | " 1.0" 306 | ] 307 | }, 308 | "execution_count": 33, 309 | "metadata": {}, 310 | "output_type": "execute_result" 311 | } 312 | ], 313 | "source": [ 314 | "m = Model(HiGHS.Optimizer)\n", 315 | "\n", 316 | "@variable(m, z[1:n, 1:n], Bin)\n", 317 | "@variable(m, y[1:n], Bin)\n", 318 | "\n", 319 | "@constraint(m, sum(y) == number_of_depots)\n", 320 | "\n", 321 | "for i in 1:n\n", 322 | " for j in 1:n\n", 323 | " @constraint(m, z[i, j] .<= y[j])\n", 324 | " end \n", 325 | "end\n", 326 | "\n", 327 | "for i in 1:n\n", 328 | " @constraint(m, sum(z[i, :]) == 1)\n", 329 | "end \n", 330 | "\n", 331 | "@objective(m, Min, sum(distances[1:n, 1:n] .* z[1:n, 1:n]))\n", 332 | "\n", 333 | "optimize!(m)\n", 334 | "\n", 335 | "value.(y)" 336 | ] 337 | }, 338 | { 339 | "cell_type": "code", 340 | "execution_count": 34, 341 | "id": "f76c3643", 342 | "metadata": {}, 343 | "outputs": [ 344 | { 345 | "data": { 346 | "text/plain": [ 347 | "6×6 Matrix{Float64}:\n", 348 | " -0.0 1.0 0.0 0.0 0.0 0.0\n", 349 | " -0.0 1.0 0.0 0.0 0.0 0.0\n", 350 | " 0.0 1.0 -0.0 -0.0 0.0 0.0\n", 351 | " 0.0 1.0 -0.0 -0.0 0.0 0.0\n", 352 | " 0.0 0.0 0.0 0.0 -0.0 1.0\n", 353 | " 0.0 0.0 0.0 0.0 -0.0 1.0" 354 | ] 355 | }, 356 | "execution_count": 34, 357 | "metadata": {}, 358 | "output_type": "execute_result" 359 | } 360 | ], 361 | "source": [ 362 | "value.(z)" 363 | ] 364 | }, 365 | { 366 | "cell_type": "code", 367 | "execution_count": 37, 368 | "id": "aad18146", 369 | "metadata": {}, 370 | "outputs": [ 371 | { 372 | "data": { 373 | "text/plain": [ 374 | "2-element Vector{Int64}:\n", 375 | " 2\n", 376 | " 6" 377 | ] 378 | }, 379 | "execution_count": 37, 380 | "metadata": {}, 381 | "output_type": "execute_result" 382 | } 383 | ], 384 | "source": [ 385 | "location_of_depots = findall(x -> x == 1, value.(y))" 386 | ] 387 | }, 388 | { 389 | "cell_type": "code", 390 | "execution_count": null, 391 | "id": "97b8edaf", 392 | "metadata": {}, 393 | "outputs": [], 394 | "source": [] 395 | } 396 | ], 397 | "metadata": { 398 | "kernelspec": { 399 | "display_name": "Julia 1.10.0-rc1", 400 | "language": "julia", 401 | "name": "julia-1.10" 402 | }, 403 | "language_info": { 404 | "file_extension": ".jl", 405 | "mimetype": "application/julia", 406 | "name": "julia", 407 | "version": "1.9.0" 408 | } 409 | }, 410 | "nbformat": 4, 411 | "nbformat_minor": 5 412 | } 413 | -------------------------------------------------------------------------------- /project-selection.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "c2e7c49d", 6 | "metadata": {}, 7 | "source": [ 8 | "## Project Selection Problem " 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "bc390c95", 14 | "metadata": {}, 15 | "source": [ 16 | "| Project | Year 1 | Year 2 | Year 3 | Return |\n", 17 | "| :------------: | :-------: | :---------: | :-------: | :--------: |\n", 18 | "| 1 | 10 | 12 | 15 | 50 |\n", 19 | "| 2 | 15 | 17 | 19 | 75 |\n", 20 | "| 3 | 20 | 22 | 23 | 80 |\n", 21 | "| 4 | 22 | 25 | 25 | 90 |\n", 22 | "| 5 | 20 | 25 | 30 | 100 |\n", 23 | "| Budget | 40 | 50 | 60 | |" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "id": "1c13b414", 29 | "metadata": {}, 30 | "source": [ 31 | "- There are 5 alternative projects.\n", 32 | "- In the first, second, and third years, the budget is 40 million, 50 million, and 60 million respectively.\n", 33 | "- Once the first project is completed, there will be a return of 50 million, whereas, once the second project is completed, there will be a return of 75 million\n", 34 | "- If **Project 1** is selected, it costs 10, 12, and 15 million dollars in years 1st, 2nd, and 3rd, respectively. \n", 35 | "- Maximize the total return under the budget constraints" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "id": "bd4fcbff", 41 | "metadata": {}, 42 | "source": [ 43 | "$$\n", 44 | "\\begin{aligned}\n", 45 | "\\max z = & 50x_1 + 75x_2 + 80x_3 + 90x_4 + 100x_5 \\\\\n", 46 | "\\text{Subject to:} & \\\\\n", 47 | "& 10x_1 + 15x_2 + 20x_3 + 22x_4 + 20x_5 \\le 40 \\\\\n", 48 | "& 12x_1 + 17x_2 + 22x_3 + 25x_4 + 25x_5 \\le 50 \\\\\n", 49 | "& 15x_1 + 19x_2 + 23x_3 + 25x_4 + 30x_5 \\le 60 \\\\\n", 50 | "& x_1, x_2, \\ldots, x_5 \\in \\{0, 1\\}\n", 51 | "\\end{aligned}\n", 52 | "$$" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 5, 58 | "id": "ae821ac9", 59 | "metadata": {}, 60 | "outputs": [], 61 | "source": [ 62 | "using JuMP, HiGHS\n", 63 | "\n", 64 | "m = Model(HiGHS.Optimizer)\n", 65 | "\n", 66 | "MOI.set(m, MOI.Silent(), true) \n", 67 | "\n", 68 | "@variable(m, x[1:5], Bin)\n", 69 | "\n", 70 | "@objective(m, Max, sum([50.0, 75, 80, 90, 100] .* x[1:5]))\n", 71 | "\n", 72 | "@constraint(m, sum([10.0, 15, 20, 22, 20] .* x[1:5]) <= 40)\n", 73 | "@constraint(m, sum([12.0, 17, 22, 25, 25] .* x[1:5]) <= 50)\n", 74 | "@constraint(m, sum([15.0, 19, 23, 25, 30] .* x[1:5]) <= 60)\n", 75 | "\n", 76 | "optimize!(m)" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 7, 82 | "id": "f2f12538", 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "data": { 87 | "text/plain": [ 88 | "180.0" 89 | ] 90 | }, 91 | "execution_count": 7, 92 | "metadata": {}, 93 | "output_type": "execute_result" 94 | } 95 | ], 96 | "source": [ 97 | "objective_value(m)" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 8, 103 | "id": "c47cbe73", 104 | "metadata": {}, 105 | "outputs": [ 106 | { 107 | "data": { 108 | "text/plain": [ 109 | "5×2 Matrix{Float64}:\n", 110 | " 1.0 0.0\n", 111 | " 2.0 -0.0\n", 112 | " 3.0 1.0\n", 113 | " 4.0 -0.0\n", 114 | " 5.0 1.0" 115 | ] 116 | }, 117 | "execution_count": 8, 118 | "metadata": {}, 119 | "output_type": "execute_result" 120 | } 121 | ], 122 | "source": [ 123 | "hcat(1:5, value.(x))" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": null, 129 | "id": "3cf1755a", 130 | "metadata": {}, 131 | "outputs": [], 132 | "source": [] 133 | } 134 | ], 135 | "metadata": { 136 | "kernelspec": { 137 | "display_name": "Julia 1.10.0-rc1", 138 | "language": "julia", 139 | "name": "julia-1.10" 140 | }, 141 | "language_info": { 142 | "file_extension": ".jl", 143 | "mimetype": "application/julia", 144 | "name": "julia", 145 | "version": "1.10.0" 146 | } 147 | }, 148 | "nbformat": 4, 149 | "nbformat_minor": 5 150 | } 151 | -------------------------------------------------------------------------------- /sensitivity.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "0fc69a2f-0a31-42f8-9cb5-dcbe2e524883", 6 | "metadata": {}, 7 | "source": [ 8 | "### Problem 1" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "320b40f0-6e51-47e7-a9b5-ff6f61460cac", 14 | "metadata": {}, 15 | "source": [ 16 | "\\begin{aligned}\n", 17 | "\\max z = & 3x + 4y \\\\\n", 18 | "\\text{Subject to:} & \\\\\n", 19 | "& 2x + 4y \\le 24 \\\\\n", 20 | "& 6x + 3y \\le 36 \\\\\n", 21 | "& x, y \\ge 0\n", 22 | "\\end{aligned}" 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "id": "70d36f95-7be7-493c-9a20-b64c36b12210", 28 | "metadata": {}, 29 | "source": [ 30 | "| - | x | y | s1 | s2 | Solution |\n", 31 | "|:---:| :---: | :---: | :---: | :---: | :----: |\n", 32 | "| z | -3 | -4 | 0 | 0 | 0 |\n", 33 | "| s1 | 2 | 4 | 1 | 0 | 24 |\n", 34 | "| s2 | 6 | 3 | 0 | 1 | 36 |" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "id": "548e296c-fcdd-4123-8b8d-2a971907edf9", 40 | "metadata": {}, 41 | "source": [ 42 | "- $y$ enters\n", 43 | "- $s_1$ leaves the solution" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": null, 49 | "id": "09e36d32-2bd1-4180-a89e-a36309484110", 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "s1 = [2, 4, 1, 0, 24]\n", 54 | "y = s1 / 4" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": null, 60 | "id": "b9d92a9e-41d9-4679-9508-8e076fdda92e", 61 | "metadata": {}, 62 | "outputs": [], 63 | "source": [ 64 | "z = [-3, -4, 0, 0, 0]\n", 65 | "newz = z + y * 4" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": null, 71 | "id": "6bc89cd0-78a5-4ac3-8e31-a5b96b91ece6", 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [ 75 | "s2 = [6, 3, 0, 1, 36]\n", 76 | "news2 = s2 + y * (-3)" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "id": "a6b88601-5ccf-407f-861c-90693c79853b", 82 | "metadata": {}, 83 | "source": [ 84 | "| - | x | y | s1 | s2 | Solution |\n", 85 | "|:---:| :---: | :---: | :---: | :---: | :----: |\n", 86 | "| z | -1 | 0 | 1 | 0 | 24 |\n", 87 | "| y | 0.5 | 1 | 0.25 | 0 | 6 |\n", 88 | "| s2 | 4.5 | 0 | -0.75 | 1 | 18 |" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "id": "9fb3546b-fee8-4ae5-bb5f-56f0b69c9e10", 94 | "metadata": {}, 95 | "source": [ 96 | "- $x_1$ enters\n", 97 | "- $s_2$ leaves the solution " 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": null, 103 | "id": "c0100b97-3b73-4ba2-9523-7f50d375916b", 104 | "metadata": {}, 105 | "outputs": [], 106 | "source": [ 107 | "x = news2 / 4.5" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": null, 113 | "id": "e7cb99b9-4cc2-4070-932b-17d312768683", 114 | "metadata": {}, 115 | "outputs": [], 116 | "source": [ 117 | "newy = y + (-0.5) * x" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": null, 123 | "id": "b4b9bfd0-6b50-4df5-b794-bdc05522226d", 124 | "metadata": {}, 125 | "outputs": [], 126 | "source": [ 127 | "newnewz = newz + 1 * x" 128 | ] 129 | }, 130 | { 131 | "cell_type": "markdown", 132 | "id": "51ca9bb7-6118-4139-8f07-4b67b8055f1c", 133 | "metadata": {}, 134 | "source": [ 135 | "| - | x | y | s1 | s2 | Solution |\n", 136 | "|:---:| :---: | :---: | :---: | :---: | :----: |\n", 137 | "| z | 0 | 0 | **0.83** | **0.22** | 28 |\n", 138 | "| y | 0 | 1 | 0.33 | -0.11| 4 |\n", 139 | "| x | 1 | 0 | -0.16 | 0.22 | 4 |" 140 | ] 141 | }, 142 | { 143 | "cell_type": "markdown", 144 | "id": "c0ecc8ae-747d-4ba4-bf68-3299de49d561", 145 | "metadata": {}, 146 | "source": [ 147 | "The optimum solution is $\\{x = 4 , y = 4\\}$ and $z = 24$." 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": null, 153 | "id": "c02b56b9-e6ed-4e2e-8a36-2f0be8a5bad6", 154 | "metadata": {}, 155 | "outputs": [], 156 | "source": [ 157 | "Binv = [0.33333333 -0.11111111; -0.16666666 0.22222222]" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": null, 163 | "id": "ea9d6fc4-2071-4f88-9bb2-69aa4ba38748", 164 | "metadata": {}, 165 | "outputs": [], 166 | "source": [ 167 | "rhs = [24, 36]" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": null, 173 | "id": "c0d0669f-1b55-423e-8b68-0dab6fbf4d6d", 174 | "metadata": {}, 175 | "outputs": [], 176 | "source": [ 177 | "Binv * rhs" 178 | ] 179 | }, 180 | { 181 | "cell_type": "markdown", 182 | "id": "89050d36-f4cc-4bab-9f47-83ca10f66707", 183 | "metadata": {}, 184 | "source": [ 185 | "- Increasing RHS value from 36 to 37" 186 | ] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "id": "9b041e55-d6cf-4ce4-be0b-068be836968e", 191 | "metadata": {}, 192 | "source": [ 193 | "### Julia Solution with JuMP" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": null, 199 | "id": "c2561fee-7c37-4ed2-853f-0a1ae2f42617", 200 | "metadata": {}, 201 | "outputs": [], 202 | "source": [ 203 | "using JuMP, GLPK" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": null, 209 | "id": "8de767fe-6366-454c-aabe-0ffe38735060", 210 | "metadata": {}, 211 | "outputs": [], 212 | "source": [ 213 | "m = Model(GLPK.Optimizer)\n", 214 | "@variable(m, x)\n", 215 | "@variable(m, y)\n", 216 | "@objective(m, Max, 3x + 4y)\n", 217 | "@constraint(m, c1, 2x + 4y <= 24)\n", 218 | "@constraint(m, c2, 6x + 3y <= 36)\n", 219 | "@constraint(m, x >= 0)\n", 220 | "@constraint(m, y >= 0)\n", 221 | "optimize!(m)\n", 222 | "println(value.([x, y]))\n", 223 | "println(JuMP.shadow_price(c1))\n", 224 | "println(JuMP.shadow_price(c2))" 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "execution_count": null, 230 | "id": "33a81afb-4c48-48b9-8dc6-7008824669db", 231 | "metadata": {}, 232 | "outputs": [], 233 | "source": [] 234 | } 235 | ], 236 | "metadata": { 237 | "kernelspec": { 238 | "display_name": "Julia 1.9 1.9.0-DEV", 239 | "language": "julia", 240 | "name": "julia-1.9-1.9" 241 | }, 242 | "language_info": { 243 | "file_extension": ".jl", 244 | "mimetype": "application/julia", 245 | "name": "julia", 246 | "version": "1.9.0" 247 | } 248 | }, 249 | "nbformat": 4, 250 | "nbformat_minor": 5 251 | } 252 | -------------------------------------------------------------------------------- /set-covering.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "07b7c7f0", 6 | "metadata": {}, 7 | "source": [ 8 | "## Set Covering Problem" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "1337d766", 14 | "metadata": {}, 15 | "source": [ 16 | "" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "id": "f25a3c2a", 22 | "metadata": {}, 23 | "source": [ 24 | "- There are 14 street lamps.\n", 25 | "- A street is considered lit if at least **two** lamps are turned on.\n", 26 | "- What is the minimum number of lamps that need to be turned on?" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "id": "561a4105", 32 | "metadata": {}, 33 | "source": [ 34 | "$$\n", 35 | "\\begin{aligned}\n", 36 | "\\min z = & x_1 + x_2 + x_3 + \\ldots + x_{11} + x_{12} + x_{13} + x_{14} \\\\\n", 37 | "\\text{Subject to:} & \\\\\n", 38 | "& x_1 + x_2 + x_3 + x_4 \\ge 2 \\\\\n", 39 | "& x_5 + x_6 + x_7 + x_8 + x_9 \\ge 2\\\\\n", 40 | "& x_{11} + x_{12} + x_{13} + x_{14} \\ge 2 \\\\\n", 41 | "& x_{1} + x_{5} + x_{11} \\ge 2 \\\\\n", 42 | "& x_{2} + x_{6} \\ge 2 \\\\\n", 43 | "& x_{3} + x_{7} + x_{12} \\ge 2 \\\\\n", 44 | "& x_{8} + x_{13} \\ge 2 \\\\\n", 45 | "& x_{4} + x_{9} + x_{14} \\ge 2 \\\\\n", 46 | "& x_i \\in \\{0, 1\\} , \\;\\; i = 1, 2, \\ldots , 14 \\\\\n", 47 | "\\end{aligned}\n", 48 | "$$" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 19, 54 | "id": "38897ba6", 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [ 58 | "using JuMP, HiGHS\n", 59 | "\n", 60 | "m = Model(HiGHS.Optimizer)\n", 61 | "\n", 62 | "MOI.set(m, MOI.Silent(), true) \n", 63 | "\n", 64 | "@variable(m, x[1:14], Bin)\n", 65 | "\n", 66 | "@objective(m, Min, sum(x))\n", 67 | "\n", 68 | "@constraint(m, sum(x[1:4]) >= 2)\n", 69 | "@constraint(m, sum(x[5:9]) >= 2)\n", 70 | "@constraint(m, sum(x[11:14]) >= 2)\n", 71 | "@constraint(m, x[1] + x[5] + x[11] >= 2)\n", 72 | "@constraint(m, x[2] + x[6] >= 2)\n", 73 | "@constraint(m, x[3] + x[7] + x[12] >= 2)\n", 74 | "@constraint(m, x[8] + x[13] >= 2)\n", 75 | "@constraint(m, x[4] + x[9] + x[14] >= 2)\n", 76 | "\n", 77 | "optimize!(m)" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 20, 83 | "id": "630654cb", 84 | "metadata": {}, 85 | "outputs": [ 86 | { 87 | "data": { 88 | "text/plain": [ 89 | "10.0" 90 | ] 91 | }, 92 | "execution_count": 20, 93 | "metadata": {}, 94 | "output_type": "execute_result" 95 | } 96 | ], 97 | "source": [ 98 | "objective_value(m)" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 21, 104 | "id": "c2a38d92", 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "data": { 109 | "text/plain": [ 110 | "14×2 Matrix{Float64}:\n", 111 | " 1.0 1.0\n", 112 | " 2.0 1.0\n", 113 | " 3.0 1.0\n", 114 | " 4.0 1.0\n", 115 | " 5.0 0.0\n", 116 | " 6.0 1.0\n", 117 | " 7.0 0.0\n", 118 | " 8.0 1.0\n", 119 | " 9.0 0.0\n", 120 | " 10.0 0.0\n", 121 | " 11.0 1.0\n", 122 | " 12.0 1.0\n", 123 | " 13.0 1.0\n", 124 | " 14.0 1.0" 125 | ] 126 | }, 127 | "execution_count": 21, 128 | "metadata": {}, 129 | "output_type": "execute_result" 130 | } 131 | ], 132 | "source": [ 133 | "hcat(1:14, value.(x))" 134 | ] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "id": "72f44c3b", 139 | "metadata": {}, 140 | "source": [ 141 | "### Modified Problem" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "id": "c042f690", 147 | "metadata": {}, 148 | "source": [ 149 | "- There are 14 street lamps.\n", 150 | "- A street is considered lit if at least **one** lamp is turned on.\n", 151 | "- What is the minimum number of lamps that need to be turned on?" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": 22, 157 | "id": "e5f1472f", 158 | "metadata": {}, 159 | "outputs": [], 160 | "source": [ 161 | "using JuMP, HiGHS\n", 162 | "\n", 163 | "m = Model(HiGHS.Optimizer)\n", 164 | "\n", 165 | "MOI.set(m, MOI.Silent(), true) \n", 166 | "\n", 167 | "@variable(m, x[1:14], Bin)\n", 168 | "\n", 169 | "@objective(m, Min, sum(x))\n", 170 | "\n", 171 | "@constraint(m, sum(x[1:4]) >= 1)\n", 172 | "@constraint(m, sum(x[5:9]) >= 1)\n", 173 | "@constraint(m, sum(x[11:14]) >= 1)\n", 174 | "@constraint(m, x[1] + x[5] + x[11] >= 1)\n", 175 | "@constraint(m, x[2] + x[6] >= 1)\n", 176 | "@constraint(m, x[3] + x[7] + x[12] >= 1)\n", 177 | "@constraint(m, x[8] + x[13] >= 1)\n", 178 | "@constraint(m, x[4] + x[9] + x[14] >= 1)\n", 179 | "\n", 180 | "optimize!(m)" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": 23, 186 | "id": "0ea28f41", 187 | "metadata": {}, 188 | "outputs": [ 189 | { 190 | "data": { 191 | "text/plain": [ 192 | "5.0" 193 | ] 194 | }, 195 | "execution_count": 23, 196 | "metadata": {}, 197 | "output_type": "execute_result" 198 | } 199 | ], 200 | "source": [ 201 | "objective_value(m)" 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": 24, 207 | "id": "4116aeed", 208 | "metadata": {}, 209 | "outputs": [ 210 | { 211 | "data": { 212 | "text/plain": [ 213 | "14×2 Matrix{Float64}:\n", 214 | " 1.0 0.0\n", 215 | " 2.0 1.0\n", 216 | " 3.0 0.0\n", 217 | " 4.0 1.0\n", 218 | " 5.0 0.0\n", 219 | " 6.0 0.0\n", 220 | " 7.0 0.0\n", 221 | " 8.0 1.0\n", 222 | " 9.0 0.0\n", 223 | " 10.0 0.0\n", 224 | " 11.0 1.0\n", 225 | " 12.0 1.0\n", 226 | " 13.0 0.0\n", 227 | " 14.0 0.0" 228 | ] 229 | }, 230 | "execution_count": 24, 231 | "metadata": {}, 232 | "output_type": "execute_result" 233 | } 234 | ], 235 | "source": [ 236 | "hcat(1:14, value.(x))" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": null, 242 | "id": "600ff9f2", 243 | "metadata": {}, 244 | "outputs": [], 245 | "source": [] 246 | } 247 | ], 248 | "metadata": { 249 | "kernelspec": { 250 | "display_name": "Julia 1.10.0-rc1", 251 | "language": "julia", 252 | "name": "julia-1.10" 253 | }, 254 | "language_info": { 255 | "file_extension": ".jl", 256 | "mimetype": "application/julia", 257 | "name": "julia", 258 | "version": "1.10.0" 259 | } 260 | }, 261 | "nbformat": 4, 262 | "nbformat_minor": 5 263 | } 264 | -------------------------------------------------------------------------------- /setcoveringmap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jbytecode/notebooks/0bb925e5599c6bcf130255d586b9c1d7f0e2595b/setcoveringmap.png -------------------------------------------------------------------------------- /shadow_price.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "a1547727-e53c-41e1-af9b-c16f12bbe6a4", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "using JuMP" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 2, 16 | "id": "f14c4c70-171f-4e8a-b05e-9385399fdf83", 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "using GLPK" 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "96611709-e5db-4d8f-92bf-fdfb6035d3f0", 26 | "metadata": {}, 27 | "source": [ 28 | "\\begin{aligned}\n", 29 | "\\max z & = 3x + 5y \\\\\n", 30 | "\\text{Subject to:} \\\\\n", 31 | "& x + y \\le 100 \\\\\n", 32 | "& x, y \\ge 0\n", 33 | "\\end{aligned}" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "id": "7c4814fa-8416-473a-baed-347868bc5ccc", 39 | "metadata": {}, 40 | "source": [ 41 | "### RHS = 100" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 4, 47 | "id": "25d60d9d-40d3-40ab-97fb-5492dcc0b846", 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "m = Model(GLPK.Optimizer)\n", 52 | "@variable(m, x)\n", 53 | "@variable(m, y)\n", 54 | "@objective(m, Max, 3x + 5y)\n", 55 | "@constraint(m, c1, x + y <= 100)\n", 56 | "@constraint(m, x >= 0)\n", 57 | "@constraint(m, y >= 0)\n", 58 | "optimize!(m)" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 5, 64 | "id": "6be32bc8-acfd-48fe-8742-1eeb6f85898b", 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "data": { 69 | "text/plain": [ 70 | "0.0" 71 | ] 72 | }, 73 | "execution_count": 5, 74 | "metadata": {}, 75 | "output_type": "execute_result" 76 | } 77 | ], 78 | "source": [ 79 | "value(x)" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 6, 85 | "id": "0fd6cb25-93d5-49ea-9bf9-11ddccd913ea", 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "data": { 90 | "text/plain": [ 91 | "100.0" 92 | ] 93 | }, 94 | "execution_count": 6, 95 | "metadata": {}, 96 | "output_type": "execute_result" 97 | } 98 | ], 99 | "source": [ 100 | "value(y)" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 7, 106 | "id": "acbe4e01-d065-4f65-89fc-542e315bb8bd", 107 | "metadata": {}, 108 | "outputs": [ 109 | { 110 | "data": { 111 | "text/plain": [ 112 | "500.0" 113 | ] 114 | }, 115 | "execution_count": 7, 116 | "metadata": {}, 117 | "output_type": "execute_result" 118 | } 119 | ], 120 | "source": [ 121 | "objective_value(m)" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 8, 127 | "id": "ab02a810-198f-4711-84cc-3d3d238a398a", 128 | "metadata": {}, 129 | "outputs": [ 130 | { 131 | "data": { 132 | "text/plain": [ 133 | "5.0" 134 | ] 135 | }, 136 | "execution_count": 8, 137 | "metadata": {}, 138 | "output_type": "execute_result" 139 | } 140 | ], 141 | "source": [ 142 | "shadow_price(c1)" 143 | ] 144 | }, 145 | { 146 | "cell_type": "markdown", 147 | "id": "3bd8fe3b-10ad-486e-a293-0fbc12703cff", 148 | "metadata": {}, 149 | "source": [ 150 | "### RHS = 101" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 9, 156 | "id": "1ba1a0c7-87c6-42e8-a19b-8b190753cfd2", 157 | "metadata": {}, 158 | "outputs": [], 159 | "source": [ 160 | "m = Model(GLPK.Optimizer)\n", 161 | "@variable(m, x)\n", 162 | "@variable(m, y)\n", 163 | "@objective(m, Max, 3x + 5y)\n", 164 | "@constraint(m, c1, x + y <= 101)\n", 165 | "@constraint(m, x >= 0)\n", 166 | "@constraint(m, y >= 0)\n", 167 | "optimize!(m)" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 10, 173 | "id": "866c438c-189f-43d0-a009-291f6a5efd00", 174 | "metadata": {}, 175 | "outputs": [ 176 | { 177 | "data": { 178 | "text/plain": [ 179 | "0.0" 180 | ] 181 | }, 182 | "execution_count": 10, 183 | "metadata": {}, 184 | "output_type": "execute_result" 185 | } 186 | ], 187 | "source": [ 188 | "value(x)" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 11, 194 | "id": "8773109d-07c7-4026-a03b-ea7b770eefcc", 195 | "metadata": {}, 196 | "outputs": [ 197 | { 198 | "data": { 199 | "text/plain": [ 200 | "101.0" 201 | ] 202 | }, 203 | "execution_count": 11, 204 | "metadata": {}, 205 | "output_type": "execute_result" 206 | } 207 | ], 208 | "source": [ 209 | "value(y)" 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": 12, 215 | "id": "7f120100-ec23-42e1-a412-49747f4b5cb9", 216 | "metadata": {}, 217 | "outputs": [ 218 | { 219 | "data": { 220 | "text/plain": [ 221 | "505.0" 222 | ] 223 | }, 224 | "execution_count": 12, 225 | "metadata": {}, 226 | "output_type": "execute_result" 227 | } 228 | ], 229 | "source": [ 230 | "objective_value(m)" 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": 13, 236 | "id": "e5bc84fd-1c91-4032-b1ea-59f921fb3451", 237 | "metadata": {}, 238 | "outputs": [ 239 | { 240 | "data": { 241 | "text/plain": [ 242 | "5.0" 243 | ] 244 | }, 245 | "execution_count": 13, 246 | "metadata": {}, 247 | "output_type": "execute_result" 248 | } 249 | ], 250 | "source": [ 251 | "shadow_price(c1)" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": null, 257 | "id": "c0136482-6bc3-4411-960d-3dd10c9e9373", 258 | "metadata": {}, 259 | "outputs": [], 260 | "source": [] 261 | } 262 | ], 263 | "metadata": { 264 | "kernelspec": { 265 | "display_name": "Julia 1.9.0-DEV", 266 | "language": "julia", 267 | "name": "julia-1.9" 268 | }, 269 | "language_info": { 270 | "file_extension": ".jl", 271 | "mimetype": "application/julia", 272 | "name": "julia", 273 | "version": "1.9.0" 274 | } 275 | }, 276 | "nbformat": 4, 277 | "nbformat_minor": 5 278 | } 279 | -------------------------------------------------------------------------------- /shortestpath-solution.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jbytecode/notebooks/0bb925e5599c6bcf130255d586b9c1d7f0e2595b/shortestpath-solution.png -------------------------------------------------------------------------------- /shortestpath.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "c2a870a3-86d3-4775-a3a2-45014538060c", 6 | "metadata": {}, 7 | "source": [ 8 | "" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "5abebc65-2528-4b3a-ab20-75df47a31f4b", 14 | "metadata": {}, 15 | "source": [ 16 | "$$\n", 17 | "\\begin{aligned}\n", 18 | "\\min z & = 3x_{12} + 2x_{13} + 4x_{14} + 3x_{25} + x_{35} + x_{36} + 2x_{46} + 6x_{57} + 5x_{67} \\\\\n", 19 | "\\text{Subject to:} \\\\\n", 20 | "& x_{12} + x_{13} + x_{14} = 1 \\\\\n", 21 | "& x_{12} - x_{25} = 0 \\\\\n", 22 | "& x_{13} - x_{35} - x_{36} = 0 \\\\\n", 23 | "& x_{14} - x_{46} = 0 \\\\\n", 24 | "& x_{25} + x_{35} - x_{57} = 0 \\\\\n", 25 | "& x_{36} + x_{46} - x_{67} = 0 \\\\\n", 26 | "& x_{57} + x_{67} = 1 \\\\\n", 27 | "& x_{ij} \\in \\{0,1\\} \n", 28 | "\\end{aligned}\n", 29 | "$$" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 58, 35 | "id": "fc9e8b24-b5d2-4f41-90a0-a36155eb11ed", 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "using JuMP" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 59, 45 | "id": "570c84aa-7e77-4ae5-acd3-81c62e0c6082", 46 | "metadata": {}, 47 | "outputs": [], 48 | "source": [ 49 | "using GLPK" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 60, 55 | "id": "dc61f7a0-519c-4a22-8742-cccfd1757bd4", 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "data": { 60 | "text/plain": [ 61 | "A JuMP Model\n", 62 | "Feasibility problem with:\n", 63 | "Variables: 0\n", 64 | "Model mode: AUTOMATIC\n", 65 | "CachingOptimizer state: EMPTY_OPTIMIZER\n", 66 | "Solver name: GLPK" 67 | ] 68 | }, 69 | "execution_count": 60, 70 | "metadata": {}, 71 | "output_type": "execute_result" 72 | } 73 | ], 74 | "source": [ 75 | "m = Model(GLPK.Optimizer)" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 61, 81 | "id": "6142f199-151a-4263-be7f-6a22c967eebd", 82 | "metadata": {}, 83 | "outputs": [], 84 | "source": [ 85 | "@variable(m, x12, Bin)\n", 86 | "@variable(m, x13, Bin)\n", 87 | "@variable(m, x14, Bin)\n", 88 | "@variable(m, x25, Bin)\n", 89 | "@variable(m, x35, Bin)\n", 90 | "@variable(m, x36, Bin)\n", 91 | "@variable(m, x46, Bin)\n", 92 | "@variable(m, x57, Bin)\n", 93 | "@variable(m, x67, Bin);" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 62, 99 | "id": "ce78f165-69e3-45f2-99ad-112b9f2438e5", 100 | "metadata": {}, 101 | "outputs": [ 102 | { 103 | "data": { 104 | "text/latex": [ 105 | "$$ 3 x12 + 2 x13 + 4 x14 + 3 x25 + x35 + x36 + 2 x46 + 6 x57 + 5 x67 $$" 106 | ], 107 | "text/plain": [ 108 | "3 x12 + 2 x13 + 4 x14 + 3 x25 + x35 + x36 + 2 x46 + 6 x57 + 5 x67" 109 | ] 110 | }, 111 | "execution_count": 62, 112 | "metadata": {}, 113 | "output_type": "execute_result" 114 | } 115 | ], 116 | "source": [ 117 | "@objective(m, Min, 3x12 + 2x13 + 4x14 + 3x25 + x35 + x36 + 2x46 + 6x57 + 5x67)" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 63, 123 | "id": "9a08f531-863d-41ca-ae89-de5532752b0b", 124 | "metadata": {}, 125 | "outputs": [ 126 | { 127 | "data": { 128 | "text/latex": [ 129 | "$$ x12 + x13 + x14 = 1.0 $$" 130 | ], 131 | "text/plain": [ 132 | "x12 + x13 + x14 = 1.0" 133 | ] 134 | }, 135 | "execution_count": 63, 136 | "metadata": {}, 137 | "output_type": "execute_result" 138 | } 139 | ], 140 | "source": [ 141 | "@constraint(m, x12 + x13 + x14 == 1)" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 64, 147 | "id": "dd747f11-5c38-44d4-b867-0842421263cf", 148 | "metadata": {}, 149 | "outputs": [ 150 | { 151 | "data": { 152 | "text/latex": [ 153 | "$$ x12 - x25 = 0.0 $$" 154 | ], 155 | "text/plain": [ 156 | "x12 - x25 = 0.0" 157 | ] 158 | }, 159 | "execution_count": 64, 160 | "metadata": {}, 161 | "output_type": "execute_result" 162 | } 163 | ], 164 | "source": [ 165 | "@constraint(m, x12 - x25 == 0)" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 65, 171 | "id": "32e0cc19-ea6f-409b-b74c-7e540aab8cf5", 172 | "metadata": {}, 173 | "outputs": [ 174 | { 175 | "data": { 176 | "text/latex": [ 177 | "$$ x13 - x35 - x36 = 0.0 $$" 178 | ], 179 | "text/plain": [ 180 | "x13 - x35 - x36 = 0.0" 181 | ] 182 | }, 183 | "execution_count": 65, 184 | "metadata": {}, 185 | "output_type": "execute_result" 186 | } 187 | ], 188 | "source": [ 189 | "@constraint(m, x13 - x35 - x36 == 0)" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 66, 195 | "id": "d80758db-cf55-4bf7-b2d6-9999aff2c3b5", 196 | "metadata": {}, 197 | "outputs": [ 198 | { 199 | "data": { 200 | "text/latex": [ 201 | "$$ x14 - x46 = 0.0 $$" 202 | ], 203 | "text/plain": [ 204 | "x14 - x46 = 0.0" 205 | ] 206 | }, 207 | "execution_count": 66, 208 | "metadata": {}, 209 | "output_type": "execute_result" 210 | } 211 | ], 212 | "source": [ 213 | "@constraint(m, x14 - x46 == 0)" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": 67, 219 | "id": "e0d216bb-f5e8-4a48-8ca7-e9fdb280e733", 220 | "metadata": {}, 221 | "outputs": [ 222 | { 223 | "data": { 224 | "text/latex": [ 225 | "$$ x25 + x35 - x57 = 0.0 $$" 226 | ], 227 | "text/plain": [ 228 | "x25 + x35 - x57 = 0.0" 229 | ] 230 | }, 231 | "execution_count": 67, 232 | "metadata": {}, 233 | "output_type": "execute_result" 234 | } 235 | ], 236 | "source": [ 237 | "@constraint(m, x25 + x35 - x57 == 0)" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": 68, 243 | "id": "c8439e46-ba07-404b-ac3a-b8e074d6aa9f", 244 | "metadata": {}, 245 | "outputs": [ 246 | { 247 | "data": { 248 | "text/latex": [ 249 | "$$ x36 + x46 - x67 = 0.0 $$" 250 | ], 251 | "text/plain": [ 252 | "x36 + x46 - x67 = 0.0" 253 | ] 254 | }, 255 | "execution_count": 68, 256 | "metadata": {}, 257 | "output_type": "execute_result" 258 | } 259 | ], 260 | "source": [ 261 | "@constraint(m, x36 + x46 - x67 == 0)" 262 | ] 263 | }, 264 | { 265 | "cell_type": "code", 266 | "execution_count": 69, 267 | "id": "74664159-974e-4c32-aad6-a4e580ceb99f", 268 | "metadata": {}, 269 | "outputs": [ 270 | { 271 | "data": { 272 | "text/latex": [ 273 | "$$ x57 + x67 = 1.0 $$" 274 | ], 275 | "text/plain": [ 276 | "x57 + x67 = 1.0" 277 | ] 278 | }, 279 | "execution_count": 69, 280 | "metadata": {}, 281 | "output_type": "execute_result" 282 | } 283 | ], 284 | "source": [ 285 | "@constraint(m, x57 + x67 == 1)" 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": 70, 291 | "id": "fcab5f12-388f-4965-b691-ba08544fe6a9", 292 | "metadata": {}, 293 | "outputs": [], 294 | "source": [ 295 | "optimize!(m)" 296 | ] 297 | }, 298 | { 299 | "cell_type": "code", 300 | "execution_count": 71, 301 | "id": "5d8936a7-8611-4449-b36f-8391fdec3504", 302 | "metadata": {}, 303 | "outputs": [ 304 | { 305 | "name": "stdout", 306 | "output_type": "stream", 307 | "text": [ 308 | "Min 3 x12 + 2 x13 + 4 x14 + 3 x25 + x35 + x36 + 2 x46 + 6 x57 + 5 x67\n", 309 | "Subject to\n", 310 | " x12 + x13 + x14 = 1.0\n", 311 | " x12 - x25 = 0.0\n", 312 | " x13 - x35 - x36 = 0.0\n", 313 | " x14 - x46 = 0.0\n", 314 | " x25 + x35 - x57 = 0.0\n", 315 | " x36 + x46 - x67 = 0.0\n", 316 | " x57 + x67 = 1.0\n", 317 | " x12 binary\n", 318 | " x13 binary\n", 319 | " x14 binary\n", 320 | " x25 binary\n", 321 | " x35 binary\n", 322 | " x36 binary\n", 323 | " x46 binary\n", 324 | " x57 binary\n", 325 | " x67 binary\n", 326 | "\n" 327 | ] 328 | } 329 | ], 330 | "source": [ 331 | "println(m)" 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": 72, 337 | "id": "8721481d-2332-47f7-b1dc-f5f7f66a5fc4", 338 | "metadata": {}, 339 | "outputs": [ 340 | { 341 | "data": { 342 | "text/plain": [ 343 | "* Solver : GLPK\n", 344 | "\n", 345 | "* Status\n", 346 | " Termination status : OPTIMAL\n", 347 | " Primal status : FEASIBLE_POINT\n", 348 | " Dual status : NO_SOLUTION\n", 349 | " Message from the solver:\n", 350 | " \"Solution is optimal\"\n", 351 | "\n", 352 | "* Candidate solution\n", 353 | " Objective value : 8.0\n", 354 | " Objective bound : 8.0\n", 355 | "\n", 356 | "* Work counters\n", 357 | " Solve time (sec) : 0.00011\n" 358 | ] 359 | }, 360 | "execution_count": 72, 361 | "metadata": {}, 362 | "output_type": "execute_result" 363 | } 364 | ], 365 | "source": [ 366 | "solution_summary(m)" 367 | ] 368 | }, 369 | { 370 | "cell_type": "code", 371 | "execution_count": 73, 372 | "id": "3aa894a0-68f6-4a68-b6ce-f51ebd6470cf", 373 | "metadata": {}, 374 | "outputs": [ 375 | { 376 | "data": { 377 | "text/plain": [ 378 | "A JuMP Model\n", 379 | "Minimization problem with:\n", 380 | "Variables: 9\n", 381 | "Objective function type: AffExpr\n", 382 | "`AffExpr`-in-`MathOptInterface.EqualTo{Float64}`: 7 constraints\n", 383 | "`VariableRef`-in-`MathOptInterface.ZeroOne`: 9 constraints\n", 384 | "Model mode: AUTOMATIC\n", 385 | "CachingOptimizer state: ATTACHED_OPTIMIZER\n", 386 | "Solver name: GLPK\n", 387 | "Names registered in the model: x12, x13, x14, x25, x35, x36, x46, x57, x67" 388 | ] 389 | }, 390 | "execution_count": 73, 391 | "metadata": {}, 392 | "output_type": "execute_result" 393 | } 394 | ], 395 | "source": [ 396 | "values(m)" 397 | ] 398 | }, 399 | { 400 | "cell_type": "code", 401 | "execution_count": 74, 402 | "id": "4132b7cd-1c12-421e-b4c1-9c3cb663160a", 403 | "metadata": {}, 404 | "outputs": [ 405 | { 406 | "data": { 407 | "text/plain": [ 408 | "9-element Vector{Tuple{VariableRef, Float64}}:\n", 409 | " (x12, 0.0)\n", 410 | " (x13, 1.0)\n", 411 | " (x14, 0.0)\n", 412 | " (x25, 0.0)\n", 413 | " (x35, 0.0)\n", 414 | " (x36, 1.0)\n", 415 | " (x46, 0.0)\n", 416 | " (x57, 0.0)\n", 417 | " (x67, 1.0)" 418 | ] 419 | }, 420 | "execution_count": 74, 421 | "metadata": {}, 422 | "output_type": "execute_result" 423 | } 424 | ], 425 | "source": [ 426 | "map(x -> (x, value(x)), [x12, x13, x14, x25, x35, x36, x46, x57, x67])" 427 | ] 428 | }, 429 | { 430 | "cell_type": "markdown", 431 | "id": "5d7b2afb-dad1-4169-a50e-1b46cbe65234", 432 | "metadata": {}, 433 | "source": [ 434 | "" 435 | ] 436 | }, 437 | { 438 | "cell_type": "markdown", 439 | "id": "d2d8f18d-1b46-4ad4-825f-712bf137a84a", 440 | "metadata": {}, 441 | "source": [ 442 | "$$\n", 443 | "1 \\rightarrow 3 \\rightarrow 6 \\rightarrow 7\n", 444 | "$$" 445 | ] 446 | }, 447 | { 448 | "cell_type": "code", 449 | "execution_count": null, 450 | "id": "ec5b8245-55f7-49a8-8da8-4c95b68870c0", 451 | "metadata": {}, 452 | "outputs": [], 453 | "source": [] 454 | } 455 | ], 456 | "metadata": { 457 | "kernelspec": { 458 | "display_name": "Julia 1.11.4", 459 | "language": "julia", 460 | "name": "julia-1.11" 461 | }, 462 | "language_info": { 463 | "file_extension": ".jl", 464 | "mimetype": "application/julia", 465 | "name": "julia", 466 | "version": "1.11.4" 467 | } 468 | }, 469 | "nbformat": 4, 470 | "nbformat_minor": 5 471 | } 472 | -------------------------------------------------------------------------------- /shortestpath.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jbytecode/notebooks/0bb925e5599c6bcf130255d586b9c1d7f0e2595b/shortestpath.png -------------------------------------------------------------------------------- /topsis.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "14693e81-dd92-43a4-ba5a-b472719b10a6", 6 | "metadata": {}, 7 | "source": [ 8 | "## House selection using TOPSIS method" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "id": "59fcd487-a322-4c93-8978-6db9c60b7aa0", 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "using JMcDM" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 2, 24 | "id": "b033f3c6-0d06-4bda-87bf-c5b276792e45", 25 | "metadata": {}, 26 | "outputs": [], 27 | "source": [ 28 | "using DataFrames" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 3, 34 | "id": "b5899645-9d49-4bcc-89f3-1c7b8b85580f", 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "text/html": [ 40 | "
Row | price | rooms | size | distance_to_transportation | years |
---|---|---|---|---|---|
Float64 | Float64 | Float64 | Float64 | Float64 | |
1 | 2500.0 | 2.0 | 50.0 | 1000.0 | 3.0 |
2 | 3000.0 | 2.0 | 75.0 | 1200.0 | 4.0 |
3 | 4000.0 | 3.0 | 100.0 | 800.0 | 8.0 |
4 | 5000.0 | 3.0 | 100.0 | 750.0 | 7.0 |