/* The MIT License

   Copyright (c) 2008, by Attractive Chaos <attractivechaos@aol.co.uk>

   Permission is hereby granted, free of charge, to any person obtaining
   a copy of this software and associated documentation files (the
   "Software"), to deal in the Software without restriction, including
   without limitation the rights to use, copy, modify, merge, publish,
   distribute, sublicense, and/or sell copies of the Software, and to
   permit persons to whom the Software is furnished to do so, subject to
   the following conditions:

   The above copyright notice and this permission notice shall be
   included in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   SOFTWARE.
*/

/* Hooke-Jeeves algorithm for nonlinear minimization
   Attractive Chaos, Februay 3, 2008
 
   Based on the pseudocodes by Bell and Pike (CACM 9(9):684-685), and
   the revision by Tomlin and Smith (CACM 12(11):637-638). Both of the
   papers are comments on Kaupe's Algorithm 178 "Direct Search" (ACM
   6(6):313-314). The original algorithm was designed by Hooke and
   Jeeves (ACM 8:212-229). This program is further revised according to
   Johnson's implementation at Netlib (opt/hooke.c).
 
   Hooke-Jeeves algorithm is very simple and it works quite well on a
   few examples. However, it might fail to converge due to its heuristic
   nature. A possible improvement, as is suggested by Johnson, may be to
   choose a small r at the beginning to quickly approach to the minimum
   and a large r at later step to hit the minimum.
 */

#ifndef AC_MIN_HH_
#define AC_MIN_HH_

#include <stdlib.h>
#include <math.h>
#include <string.h>

template<class TYPE, class Func>
TYPE min_hj(int n, TYPE *x, Func &f, TYPE r=0.5, TYPE eps=1e-7, int max_calls=50000);

template<class TYPE, class Func>
static TYPE min_hj_aux(int n, TYPE *x1, Func &f, TYPE fx1, TYPE *dx, int *n_calls)
{
    int k, j = *n_calls;
    TYPE ftmp;
    for (k = 0; k != n; ++k) {
        x1[k] += dx[k];
        ftmp = f(n, x1); ++j;
        if (ftmp < fx1) fx1 = ftmp;
        else { // search the opposite direction
            dx[k] = 0.0 - dx[k];
            x1[k] += dx[k] + dx[k];
            ftmp = f(n, x1); ++j;
            if (ftmp < fx1) fx1 = ftmp;
            else x1[k] -= dx[k]; // back to the original x[k]
        }
    }
    *n_calls = j;
    return fx1; // here: fx1=f(n,x1)
}
/**
   Hooke-Jeeve's method for multi-variable function minimization.
   @param n Dimension.
   @param x[0..n-1] Initial value, an array of size n.
   @param f(n,x) Function. f(n,x) retures the function value at x.
   @param r Radius for initial search.
   @param eps Accuracy.
   @param max_calls Maximum allowed function calls.
 */
template<class TYPE, class Func>
TYPE min_hj(int n, TYPE *x, Func &f, TYPE r, TYPE eps, int max_calls)
{
    TYPE fx, fx1, *x1, *dx, radius;
    int k, n_calls = 0;
    x1 = (TYPE*)calloc(n, sizeof(TYPE));
    dx = (TYPE*)calloc(n, sizeof(TYPE));
    for (k = 0; k != n; ++k) { // initial directions, based on MGJ
        dx[k] = fabs(x[k]) * r;
        if (dx[k] == 0) dx[k] = r;
    }
    radius = r;
    fx1 = fx = f(n, x); ++n_calls;
    for (;;) {
        memcpy(x1, x, n * sizeof(TYPE)); // x1 = x
        fx1 = min_hj_aux(n, x1, f, fx, dx, &n_calls);
        while (fx1 < fx) {
            for (k = 0; k != n; ++k) {
                TYPE t = x[k];
                dx[k] = x1[k] > x[k]? fabs(dx[k]) : 0.0 - fabs(dx[k]);
                x[k] = x1[k];
                x1[k] = x1[k] + x1[k] - t;
            }
            fx = fx1;
            if (n_calls >= max_calls) goto END_MIN_HJ;
            fx1 = f(n, x1); ++n_calls;
            fx1 = min_hj_aux(n, x1, f, fx1, dx, &n_calls);
            if (fx1 >= fx) break;
            for (k = 0; k != n; ++k)
                if (fabs(x1[k] - x[k]) > .5 * fabs(dx[k])) break;
            if (k == n) break;
        }
        if (radius >= eps) {
            if (n_calls >= max_calls) goto END_MIN_HJ;
            radius *= r;
            for (k = 0; k != n; ++k) dx[k] *= r;
        } else break; // converge
    }
END_MIN_HJ:
    free(x1); free(dx);
    return fx1;
}

#endif