OSDN Git Service

Initial commit
[wordring-tm/wordring-tm.git] / third_party / mecab-0.996 / src / lbfgs.h
1 //  MeCab -- Yet Another Part-of-Speech and Morphological Analyzer
2 //
3 //
4 //  Copyright(C) 2001-2006 Taku Kudo <taku@chasen.org>
5 //  Copyright(C) 2004-2006 Nippon Telegraph and Telephone Corporation
6 #ifndef MECAB_LBFGS_H_
7 #define MECAB_LBFGS_H_
8
9 #include <vector>
10 #include <iostream>
11
12 namespace MeCab {
13
14 class LBFGS {
15  public:
16   explicit LBFGS(): iflag_(0), iscn(0), nfev(0), iycn(0),
17                     point(0), npt(0), iter(0), info(0),
18                     ispt(0), isyt(0), iypt(0), maxfev(0),
19                     stp(0.0), stp1(0.0), mcsrch_(0) {}
20   virtual ~LBFGS() { clear(); }
21
22   void clear();
23
24   int optimize(size_t size, double *x, double f, double *g,
25                bool orthant, double C) {
26     static const int msize = 5;
27     if (w_.empty()) {
28       iflag_ = 0;
29       w_.resize(size * (2 * msize + 1) + 2 * msize);
30       diag_.resize(size);
31     } else if (diag_.size() != size) {
32       std::cerr << "size of array is different" << std::endl;
33       return -1;
34     }
35
36     lbfgs_optimize(static_cast<int>(size),
37                    msize, x, f, g, &diag_[0], &w_[0], orthant, C, &iflag_);
38
39     if (iflag_ < 0) {
40       std::cerr << "routine stops with unexpected error" << std::endl;
41       return -1;
42     }
43
44     if (iflag_ == 0) {
45       clear();
46       return 0;   // terminate
47     }
48
49     return 1;   // evaluate next f and g
50   }
51
52  private:
53   class Mcsrch;
54   int iflag_, iscn, nfev, iycn, point, npt;
55   int iter, info, ispt, isyt, iypt, maxfev;
56   double stp, stp1;
57   std::vector <double> diag_;
58   std::vector <double> w_;
59   Mcsrch *mcsrch_;
60
61   void lbfgs_optimize(int size,
62                       int msize,
63                       double *x,
64                       double f,
65                       const double *g,
66                       double *diag,
67                       double *w, bool orthant, double C, int *iflag);
68 };
69 }
70
71 #endif