コード例 #1
0
ファイル: _demo_mincg_1.cpp プロジェクト: gilso/Packages
int main(int argc, char **argv)
{
    int n;
    mincgstate state;
    mincgreport rep;
    ap::real_1d_array s;
    double x;
    double y;

    
    //
    // Function minimized:
    //     F = (x-1)^4 + (y-x)^2
    // N = 2 - task dimension.
    //
    n = 2;
    s.setlength(2);
    s(0) = 10;
    s(1) = 11;
    mincgcreate(n, s, state);
    mincgsetcond(state, 0.0, 0.0, 0.00001, 0);
    mincgsetxrep(state, true);
    printf("\n\nF = (x-1)^4 + (y-x)^2\n");
    printf("OPTIMIZATION STARTED\n");
    while(mincgiteration(state))
    {
        if( state.needfg )
        {
            x = state.x(0);
            y = state.x(1);
            state.f = ap::sqr(ap::sqr(x-1))+ap::sqr(y-x);
            state.g(0) = 4*ap::sqr(x-1)*(x-1)+2*(x-y);
            state.g(1) = 2*(y-x);
        }
        if( state.xupdated )
        {
            printf("    F(%8.5lf,%8.5lf)=%0.5lf\n",
                double(state.x(0)),
                double(state.x(1)),
                double(state.f));
        }
    }
    printf("OPTIMIZATION STOPPED\n");
    mincgresults(state, s, rep);
    
    //
    // output results
    //
    printf("X = %4.2lf (should be 1.00)\n",
        double(s(0)));
    printf("Y = %4.2lf (should be 1.00)\n\n\n",
        double(s(1)));
    return 0;
}
コード例 #2
0
ファイル: MeshOpt.cpp プロジェクト: iyer-arvind/gmsh
void MeshOpt::runOptim(alglib::real_1d_array &x,
                        const alglib::real_1d_array &initGradObj, int itMax)
{
  static const double EPSG = 0.;
  static const double EPSF = 0.;
  static const double EPSX = 0.;

  _iter = 0;

  alglib::real_1d_array scale;
  calcScale(scale);

  int iterationscount = 0, nfev = 0, terminationtype = -1;
  alglib::mincgstate state;
  alglib::mincgreport rep;
  try {
    mincgcreate(x, state);
    mincgsetscale(state,scale);
    mincgsetprecscale(state);
    mincgsetcond(state, EPSG, EPSF, EPSX, itMax);
    mincgsetxrep(state, true);
    alglib::mincgoptimize(state, evalObjGradFunc, printProgressFunc, this);
    mincgresults(state, x, rep);
  }
  catch(alglib::ap_error &e) {
    Msg::Error("%s", e.msg.c_str());
  }
  iterationscount = rep.iterationscount;
  nfev = rep.nfev;
  terminationtype = rep.terminationtype;

  if (_verbose > 2) {
    Msg::Info("Optimization finalized after %d iterations (%d function evaluations),",
              iterationscount, nfev);
    switch(int(terminationtype)) {
    case 1: Msg::Info("because relative function improvement is no more than EpsF"); break;
    case 2: Msg::Info("because relative step is no more than EpsX"); break;
    case 4: Msg::Info("because gradient norm is no more than EpsG"); break;
    case 5: Msg::Info("because the maximum number of steps was taken"); break;
    default: Msg::Info("with code %d", int(terminationtype)); break;
    }
  }
}
コード例 #3
0
ファイル: OptHOM.cpp プロジェクト: iyer-arvind/gmsh
void OptHOM::OptimPass(alglib::real_1d_array &x, int itMax)
{

  static const double EPSG = 0.;
  static const double EPSF = 0.;
  static const double EPSX = 0.;
  static int OPTMETHOD = 1;

  Msg::Info("--- Optimization pass with initial jac. range (%g, %g), jacBar = %g",
            minJac, maxJac, jacBar);

  iter = 0;

  alglib::real_1d_array scale;
  calcScale(scale);

  int iterationscount = 0, nfev = 0, terminationtype = -1;
  if (OPTMETHOD == 1) {
    alglib::mincgstate state;
    alglib::mincgreport rep;
    try{
      mincgcreate(x, state);
      mincgsetscale(state,scale);
      mincgsetprecscale(state);
      mincgsetcond(state, EPSG, EPSF, EPSX, itMax);
      mincgsetxrep(state, true);
      alglib::mincgoptimize(state, evalObjGradFunc, printProgressFunc, this);
      mincgresults(state, x, rep);
    }
    catch(alglib::ap_error e){
      Msg::Error("%s", e.msg.c_str());
    }
    iterationscount = rep.iterationscount;
    nfev = rep.nfev;
    terminationtype = rep.terminationtype;
  }
  else {
    alglib::minlbfgsstate state;
    alglib::minlbfgsreport rep;
    try{
      minlbfgscreate(3, x, state);
      minlbfgssetscale(state,scale);
      minlbfgssetprecscale(state);
      minlbfgssetcond(state, EPSG, EPSF, EPSX, itMax);
      minlbfgssetxrep(state, true);
      alglib::minlbfgsoptimize(state, evalObjGradFunc, printProgressFunc, this);
      minlbfgsresults(state, x, rep);
    }
    catch(alglib::ap_error e){
      Msg::Error("%s", e.msg.c_str());
    }
    iterationscount = rep.iterationscount;
    nfev = rep.nfev;
    terminationtype = rep.terminationtype;
  }

  Msg::Info("Optimization finalized after %d iterations (%d function evaluations),",
            iterationscount, nfev);
  switch(int(terminationtype)) {
  case 1: Msg::Info("because relative function improvement is no more than EpsF"); break;
  case 2: Msg::Info("because relative step is no more than EpsX"); break;
  case 4: Msg::Info("because gradient norm is no more than EpsG"); break;
  case 5: Msg::Info("because the maximum number of steps was taken"); break;
  default: Msg::Info("with code %d", int(terminationtype)); break;
  }
}