What is fmin?
The fmin npm package is a JavaScript library for function minimization. It provides tools to find the minimum value of a function, which is useful in various optimization problems.
What are fmin's main functionalities?
Unconstrained Minimization
This feature allows you to find the minimum of an unconstrained function using the Nelder-Mead algorithm. In this example, the function (x - 3)^2 is minimized, and the result should be close to x = 3.
const fmin = require('fmin');
function quadratic(x) {
return (x - 3) * (x - 3);
}
const result = fmin.nelderMead(quadratic, [0]);
console.log(result);
Multidimensional Minimization
This feature allows you to minimize functions of multiple variables. The example uses the Rosenbrock function, a common test problem for optimization algorithms, and finds the minimum in a 2D space.
const fmin = require('fmin');
function rosenbrock(x) {
return 100 * (x[1] - x[0] * x[0]) * (x[1] - x[0] * x[0]) + (1 - x[0]) * (1 - x[0]);
}
const result = fmin.nelderMead(rosenbrock, [-1, 1]);
console.log(result);
Other packages similar to fmin
mathjs
Math.js is an extensive math library for JavaScript and Node.js. It provides a wide range of mathematical functions, including optimization routines. Compared to fmin, math.js offers a broader set of mathematical tools but may be more complex to use for simple minimization tasks.
numeric
Numeric.js is a library for numerical computations in JavaScript. It includes functions for linear algebra, solving systems of equations, and optimization. While it offers similar minimization capabilities as fmin, it also provides additional numerical methods that can be useful for more complex problems.
optimize-js
Optimize-js is a library focused on optimization algorithms, including gradient descent and other methods. It provides more advanced optimization techniques compared to fmin, making it suitable for more complex or large-scale optimization problems.
fmin
Unconstrained function minimization in javascript.
This package implements some basic numerical optimization algorithms: Nelder-Mead, Gradient
Descent, Wolf Line Search and Non-Linear Conjugate Gradient methods are all provided.
Interactive visualizations with D3 explaining how these algorithms work are also included in this package.
Descriptions of the algorithms as well as most of the visualizations are available on my blog post
An Interactive Tutorial on Numerical
Optimization.
Installing
If you use NPM, npm install fmin
. Otherwise, download the latest release.
API Reference
# nelderMead(f, initial)
Uses the Nelder-Mead method to
minimize a function f starting at location initial.
Example usage minimizing the function f(x, y) = x2 + y2 + x sin y + y
sin x is:
function loss(X) {
var x = X[0], y = X[1];
return Math.sin(y) * x + Math.sin(x) * y + x * x + y *y;
}
var solution = fmin.nelderMead(loss, [-3.5, 3.5]);
console.log("solution is at " + solution.x);
# conjugateGradient(f, initial)
Minimizes a function using the Polak–Ribière non-linear conjugate gradient method
. The function f should
compute both the loss and the gradient.
An example minimizing Rosenbrock's Banana
function is:
function banana(X, fxprime) {
fxprime = fxprime || [0, 0];
var x = X[0], y = X[1];
fxprime[0] = 400 * x * x * x - 400 * y * x + 2 * x - 2;
fxprime[1] = 200 * y - 200 * x * x;
return (1 - x) * (1 - x) + 100 * (y - x * x) * (y - x * x);
}
var solution = fmin.conjugateGradient(banana, [-1, 1]);
console.log("solution is at " + solution.x);