summaryrefslogtreecommitdiff
path: root/training/liblbfgs/ll_test.cc
blob: 48bc0366c538cfeeddb638b971d58191496169fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <liblbfgs/lbfgs++.h>

using namespace std;

// Function must be lbfgsfloatval_t f(x.begin, x.end, g.begin)
lbfgsfloatval_t func(const vector<lbfgsfloatval_t>& x, lbfgsfloatval_t* g) {
    unsigned i;
    lbfgsfloatval_t fx = 0.0;

    for (i = 0;i < x.size();i += 2) {
        lbfgsfloatval_t t1 = 1.0 - x[i];
        lbfgsfloatval_t t2 = 10.0 * (x[i+1] - x[i] * x[i]);
        g[i+1] = 20.0 * t2;
        g[i] = -2.0 * (x[i] * g[i+1] + t1);
        fx += t1 * t1 + t2 * t2;
    }
    return fx;
}

template<typename F>
void Opt(F& f) {
  LBFGS<F> lbfgs(4, f);
  lbfgs.MinimizeFunction();
}

int main() {
  Opt(func);
  return 0;
}