Exemplo n.º 1
0
int main(int argc, char **argv)
{
  const double alpha = (argc == 2) ? atof(argv[1]) : 0.01;

  int i;
  const int dim = f_dimension();

  double *x = malloc(dim * sizeof(double));
  for (i = 0; i < dim; i++) {
    x[i] = 0;
  }

  const int iter = optimize(alpha, f_dimension(), x, f_gradient, f_value);

  printf("number of iterations = %d\n", iter);

  return 0;
}
Exemplo n.º 2
0
/**
 *  BFGS公式により擬似ヘッシアンを作る
 *  H[k+1] = A H[k] A + B
 *  ただし,A = (I - y[(s)^T] / [(y)^T]s), B = y[(y)^T] / [(y)^T]s
 */
void f_quasi_hessian(const double y[], const double s[], double hes[])
{
    int dim = f_dimension();    // 次元
    double *mat[3];
    const int mat_num = 3;
    double *tmp = (double *)malloc(dim * dim * sizeof(double)); // 次のヘッシアン
    double c = 0.0;
    int i, j;
    
    for (i = 0; i < mat_num; i++)
        mat[i] = (double *)malloc(dim * dim * sizeof(double));
    
    // 定数c = [(y)^T]s
    for (i = 0; i < dim; i++)
        c += y[i] * s[i];
    
    // mat[0] = I
    // mat[1] = y[(s)^T] / [(y)^T]s
    // mat[2] = y[(y)^T] / [(y)^T]s
    for (i = 0; i < dim; i++)
    {
        for (j = 0; j < dim; j++)
        {
            // -- 単位行列を作る --
            if (i == j)
                mat[0][i * dim + j] =  1.0;
            else
                mat[0][i * dim + j] =  0.0;
            // ------------------
            
            // 計算用の行列
            mat[1][i * dim + j] = y[i] * s[j] / c;
            mat[2][i * dim + j] = y[i] * y[j] / c;
        }
    }
     
    // mat[0] = mat[0] - mat[1] = I - y[(s)^T] / [(y)^T]s
    for (i = 0; i < dim; i++)
    {
        for (j = 0; j < dim; j++)
            mat[0][i * dim + j] -= mat[1][i * dim + j];
    }
    
    // tmp = mat[0] * hes
    mal_matrix(dim, mat[0], hes, tmp);
    // hes = tmp * mat[0]
    mal_matrix(dim, tmp, mat[0], hes);
    
    // hes = hes + mat[2]
    for (i = 0; i < dim * dim; i++)
        hes[i] += mat[2][i];
    
    free(tmp);
    for (i = 0; i < mat_num; i++)
        free(mat[i]);
}
Exemplo n.º 3
0
int main(int argc, char **argv)
{
  const double alpha = (argc == 2) ? atof(argv[1]) : 0.01;

  int i;
  const int dim = f_dimension(); // return 2

  double *x = malloc(dim * sizeof(double));
  for (i = 0; i < dim; i++) {
    x[i] = 0; // initializing an array w 2 elems
  }

  const int iter = optimize(alpha, f_dimension(), x, f_gradient);

  printf("number of iterations = %d\n", iter);

  free(x);

  return 0;
}