| 66 | return 0; |
| 67 | } |
| 68 | VectorXf minimizeLBFGS( EnergyFunction & efun, int restart, bool verbose ) { |
| 69 | VectorXf x0 = efun.initialValue(); |
| 70 | const int n = x0.rows(); |
| 71 | |
| 72 | lbfgsfloatval_t *x = lbfgs_malloc(n); |
| 73 | if (x == NULL) { |
| 74 | printf("ERROR: Failed to allocate a memory block for variables.\n"); |
| 75 | return x0; |
| 76 | } |
| 77 | std::copy( x0.data(), x0.data()+n, x ); |
| 78 | |
| 79 | lbfgs_parameter_t param; |
| 80 | lbfgs_parameter_init(¶m); |
| 81 | // You might want to adjust the parameters to your problem |
| 82 | param.epsilon = 1e-6; |
| 83 | param.max_iterations = 50; |
| 84 | |
| 85 | double last_f = 1e100; |
| 86 | int ret; |
| 87 | for( int i=0; i<=restart; i++ ) { |
| 88 | lbfgsfloatval_t fx; |
| 89 | ret = lbfgs(n, x, &fx, evaluate, verbose?progress:NULL, &efun, ¶m); |
| 90 | if( last_f > fx ) |
| 91 | last_f = fx; |
| 92 | else |
| 93 | break; |
| 94 | } |
| 95 | |
| 96 | if ( verbose ) { |
| 97 | printf("L-BFGS optimization terminated with status code = %d\n", ret); |
| 98 | } |
| 99 | |
| 100 | std::copy( x, x+n, x0.data() ); |
| 101 | lbfgs_free(x); |
| 102 | return x0; |
| 103 | } |
| 104 | VectorXf numericGradient( EnergyFunction & efun, const VectorXf & x, float EPS ) { |
| 105 | VectorXf g( x.rows() ), tmp; |
| 106 | for( int i=0; i<x.rows(); i++ ) { |