Last active
July 24, 2021 22:05
-
-
Save hugmanrique/4f19ef559bbd6864e540f1fa84e074a2 to your computer and use it in GitHub Desktop.
Multivariate (ℝ^2) relative minimum brute-force finder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // The function we want to find the minimums of | |
| // https://i.imgur.com/GG6uXvD.png | |
| const getValue = (m, n) => m * m + n * n; | |
| // The sampling interval (in ℝ^2) | |
| let mStart = 0; | |
| let nStart = 0; | |
| let mEnd = 1; | |
| let nEnd = 1; | |
| // On every iteration we decrease the sampling interval by this factor | |
| const reductionFactor = 100; | |
| // We compute `4 * reductionFactor` points (per dimension) inside the sampling interval | |
| const samplingPoints = reductionFactor * 4; | |
| // The offset between these points | |
| let delta = (mEnd - mStart) / samplingPoints; | |
| const requiredPrecision = 2e-15; | |
| function findMinimum() { | |
| let bestValue = Infinity; | |
| let bestM, bestN; | |
| // Find best parameters | |
| for (let m = mStart; m < mEnd; m += delta) { | |
| for (let n = nStart; n < nEnd; n += delta) { | |
| const value = getValue(m, n); | |
| if (value < bestValue) { | |
| bestValue = value; | |
| bestM = m; | |
| bestN = n; | |
| } | |
| } | |
| } | |
| // Zoom around the found parameters | |
| const newSize = (mEnd - mStart) / reductionFactor; | |
| mStart = bestM - newSize / 2; | |
| mEnd = mStart + newSize; | |
| nStart = bestN - newSize / 2; | |
| nEnd = nStart + newSize; | |
| delta = newSize / samplingPoints; | |
| } | |
| function start() { | |
| let i = 0; | |
| while (delta > requiredPrecision) { | |
| findMinimum(); | |
| console.log( | |
| `Iteration #${i++} -> m ∈ [${mStart}, ${mEnd}], n ∈ [${nStart}, ${nEnd}]` | |
| ); | |
| } | |
| } | |
| start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment