|
| 1 | +import { |
| 2 | + conjugateGradient, |
| 3 | + conjugateGradientSolve, |
| 4 | + gradientDescent, |
| 5 | + gradientDescentLineSearch, |
| 6 | + nelderMead, |
| 7 | +} from '../../../../../src/data/utils/venn/fmin'; |
| 8 | + |
| 9 | +const SMALL = 1e-5; |
| 10 | + |
| 11 | +function nearlyEqual( |
| 12 | + left, |
| 13 | + right, |
| 14 | + tolerance = SMALL, |
| 15 | + message = 'assertNearlyEqual', |
| 16 | +) { |
| 17 | + expect(Math.abs(left - right)).toBeLessThan(tolerance); |
| 18 | + console.log(`${message}: ${left} ~== ${right}`); |
| 19 | +} |
| 20 | + |
| 21 | +function lessThan(test, left, right, message) { |
| 22 | + message = message || 'lessThan'; |
| 23 | + test.ok(left < right, `${message}: ${left} < ${right}`); |
| 24 | +} |
| 25 | + |
| 26 | +const optimizers = [ |
| 27 | + nelderMead, |
| 28 | + gradientDescent, |
| 29 | + gradientDescentLineSearch, |
| 30 | + conjugateGradient, |
| 31 | +]; |
| 32 | + |
| 33 | +const optimizerNames = [ |
| 34 | + 'Nelder Mead', |
| 35 | + 'Gradient Descent', |
| 36 | + 'Gradient Descent w/ Line Search', |
| 37 | + 'Conjugate Gradient', |
| 38 | +]; |
| 39 | + |
| 40 | +describe('fmin', () => { |
| 41 | + test('himmelblau', () => { |
| 42 | + // due to a bug, this used to not converge to the minimum |
| 43 | + const x = 4.9515014216303825; |
| 44 | + const y = 0.07301421370357275; |
| 45 | + |
| 46 | + const params = { learnRate: 0.1 }; |
| 47 | + |
| 48 | + const himmelblau = (X, fxprime = [0, 0]) => { |
| 49 | + const [x, y] = X; |
| 50 | + fxprime[0] = 2 * (x + 2 * y - 7) + 4 * (2 * x + y - 5); |
| 51 | + fxprime[1] = 4 * (x + 2 * y - 7) + 2 * (2 * x + y - 5); |
| 52 | + // biome-ignore lint/style/useExponentiationOperator: TODO: use ** |
| 53 | + return Math.pow(x + 2 * y - 7, 2) + Math.pow(2 * x + y - 5, 2); |
| 54 | + }; |
| 55 | + |
| 56 | + optimizers.forEach((optimizer, index) => { |
| 57 | + const solution = optimizer(himmelblau, [x, y], params); |
| 58 | + nearlyEqual(solution.fx, 0, SMALL, `himmelblau:${optimizerNames[index]}`); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + test('banana', () => { |
| 63 | + const x = 1.6084564160555601; |
| 64 | + const y = -1.5980748860165477; |
| 65 | + |
| 66 | + const banana = (X, fxprime) => { |
| 67 | + fxprime = fxprime || [0, 0]; |
| 68 | + const x = X[0]; |
| 69 | + const y = X[1]; |
| 70 | + fxprime[0] = 400 * x * x * x - 400 * y * x + 2 * x - 2; |
| 71 | + fxprime[1] = 200 * y - 200 * x * x; |
| 72 | + return (1 - x) * (1 - x) + 100 * (y - x * x) * (y - x * x); |
| 73 | + }; |
| 74 | + |
| 75 | + const params = { learnRate: 0.0003, maxIterations: 50000 }; |
| 76 | + for (let i = 0; i < optimizers.length; ++i) { |
| 77 | + const solution = optimizers[i](banana, [x, y], params); |
| 78 | + nearlyEqual(solution.fx, 0, 1e-3, `banana:${optimizerNames[i]}`); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + test('quadratic1D', () => { |
| 83 | + const loss = (x, xprime) => { |
| 84 | + xprime = xprime || [0, 0]; |
| 85 | + xprime[0] = 2 * (x[0] - 10); |
| 86 | + return (x[0] - 10) * (x[0] - 10); |
| 87 | + }; |
| 88 | + |
| 89 | + const params = { learnRate: 0.5 }; |
| 90 | + |
| 91 | + for (let i = 0; i < optimizers.length; ++i) { |
| 92 | + const solution = optimizers[i](loss, [0], params); |
| 93 | + nearlyEqual(solution.fx, 0, SMALL, `quadratic_1d:${optimizerNames[i]}`); |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + test('nelderMead', () => { |
| 98 | + const loss = (X) => { |
| 99 | + const x = X[0]; |
| 100 | + const y = X[1]; |
| 101 | + return Math.sin(y) * x + Math.sin(x) * y + x * x + y * y; |
| 102 | + }; |
| 103 | + |
| 104 | + const solution = nelderMead(loss, [-3.5, 3.5]); |
| 105 | + nearlyEqual(solution.fx, 0, SMALL, 'nelderMead'); |
| 106 | + }); |
| 107 | + |
| 108 | + test('conjugateGradientSolve', () => { |
| 109 | + // matyas function |
| 110 | + let A = [ |
| 111 | + [0.52, -0.48], |
| 112 | + [-0.48, 0.52], |
| 113 | + ]; |
| 114 | + let b = [0, 0]; |
| 115 | + const initial = [-9.08, -7.83]; |
| 116 | + let x = conjugateGradientSolve(A, b, initial); |
| 117 | + nearlyEqual(x[0], 0, SMALL, 'matyas.x'); |
| 118 | + nearlyEqual(x[1], 0, SMALL, 'matyas.y'); |
| 119 | + |
| 120 | + // booth's function |
| 121 | + const history = []; |
| 122 | + A = [ |
| 123 | + [10, 8], |
| 124 | + [8, 10], |
| 125 | + ]; |
| 126 | + b = [34, 38]; |
| 127 | + x = conjugateGradientSolve(A, b, initial, history); |
| 128 | + nearlyEqual(x[0], 1, SMALL, 'booth.x'); |
| 129 | + nearlyEqual(x[1], 3, SMALL, 'booth.y'); |
| 130 | + console.log(history); |
| 131 | + }); |
| 132 | +}); |
0 commit comments