compass_search, a Python code which seeks the minimizer of a scalar function of several variables using compass search, a direct search algorithm that does not use derivatives.

The algorithm, which goes back to Fermi and Metropolis, is easy to describe. The algorithm begins with a starting point X, and a step size DELTA.

For each dimension I, the algorithm considers perturbing X(I) by adding or subtracting DELTA.

If a perturbation is found which decreases the function, this becomes the new X. Otherwise DELTA is halved.

The iteration halts when DELTA reaches a minimal value.

The algorithm is not guaranteed to find a global minimum. It can, for instance, easily be attracted to a local minimum. Moreover, the algorithm can diverge if, for instance, the function decreases as the argument goes to infinity.


[ x, fx, k ] = compass_search ( f, m, x, delta_tol, delta, k_max )


The computer code and data files described and made available on this web page are distributed under the MIT license


compass_search is available in a C version and a C++ version and a FORTRAN90 version and a MATLAB version and a Python version.

Related Data and Programs:

asa047, a Python code which minimizes a scalar function of several variables using the Nelder-Mead algorithm.

toms178, a Python code which optimizes a scalar functional of multiple variables using the Hooke-Jeeves method, by Arthur Kaupe. This is a version of ACM TOMS algorithm 178.


John Burkardt


  1. Tamara Kolda, Robert Michael Lewis, Virginia Torczon,
    Optimization by Direct Search: New Perspectives on Some Classical and Modern Methods,
    SIAM Review,
    Volume 45, Number 3, 2003, pages 385-482.

Source Code:

Last revised on 20 January 2020.