OptStatus DefaultOptConvergenceTest::test(const OptState& state) const
{
  Tabs tab(0);
  int i = state.iter();
  int conv = 0;

  PLAYA_MSG1(verb(), tab << "DefaultOptConvergenceTest testing iter #" << i);
  Tabs tab1;

  if (i < std::max(1, minIters_)) 
  {
    PLAYA_MSG2(verb(), tab1 << "iter #" << i 
      << " below minimum, skipping test");
    return Opt_Continue;
  }

  /* Get problem scale */
  double fCur = state.fCur();
  double fPrev = state.fPrev();
  double fScale = std::max( std::fabs(fPrev), std::fabs(fTyp_) );
  double xScale = std::max( state.xCur().normInf(), std::fabs(xTyp_) );

  /* check function value change */
  double objConv = std::fabs(fCur - fPrev)/fScale;
  if (objConv <= objTol_) conv++;
  PLAYA_MSG2(verb(), tab1 << "obj test: " << objConv << " tol=" << objTol_);

  /* check gradient */
  double gradConv = state.gradCur().normInf() * xScale / fScale ;
  PLAYA_MSG2(verb(), tab1 << "|grad|: " << gradConv << " tol=" << gradTol_);
  if (gradConv <= gradTol_) conv++;

  /* compute |xPrev_k - xCur_k| / xScale */
  double stepConv = (state.xCur() - state.xPrev()).normInf()/xScale;
  if (stepConv <= stepTol_) conv++;
  PLAYA_MSG2(verb(), tab1 << "step test " << stepConv << " tol=" << stepTol_);
  
  PLAYA_MSG2(verb(), tab1 << conv << " of " << requiredPasses_ << " criteria "
    "passed");

  if (conv >= requiredPasses_) 
  {
    PLAYA_MSG2(verb(), tab1 << "convergence detected!");
    return Opt_Converged;
  }

  if (i >= maxIters_) 
  {
    PLAYA_MSG2(verb(), "iter #" << i << " above maxiters, giving up");
    return Opt_ExceededMaxiters;
  }

  PLAYA_MSG2(verb(), tab1 << "not yet converged");
  return Opt_Continue;
}
Example #2
0
int main(int argc, char** argv)
{
  try
		{
			Sundance::init(&argc, &argv);
      int np = MPIComm::world().getNProc();
      
      int nx = 48;
      int ny = 48;
      int npx = -1;
      int npy = -1;
      PartitionedRectangleMesher::balanceXY(np, &npx, &npy);
      TEUCHOS_TEST_FOR_EXCEPT(npx < 1);
      TEUCHOS_TEST_FOR_EXCEPT(npy < 1);
      TEUCHOS_TEST_FOR_EXCEPT(npx * npy != np);
      MeshType meshType = new BasicSimplicialMeshType();
      MeshSource mesher = new PartitionedRectangleMesher(0.0, 1.0, nx, npx, 
        0.0,  1.0, ny, npy, meshType);

      Mesh mesh = mesher.getMesh();
      CellFilter interior = new MaximalCellFilter();
      CellFilter bdry = new BoundaryCellFilter();
      
      /* Create a vector space factory, used to 
       * specify the low-level linear algebra representation */
      VectorType<double> vecType = new EpetraVectorType();
  
      /* create a discrete space on the mesh */
      DiscreteSpace discreteSpace(mesh, new Lagrange(1), vecType);

      /* initialize the design, state, and multiplier vectors */
      Expr alpha0 = new DiscreteFunction(discreteSpace, 1.0, "alpha0");
      Expr u0 = new DiscreteFunction(discreteSpace, 1.0, "u0");
      Expr lambda0 = new DiscreteFunction(discreteSpace, 1.0, "lambda0");

      /* create symbolic objects for test and unknown functions */
      Expr u = new UnknownFunction(new Lagrange(1), "u");
      Expr lambda = new UnknownFunction(new Lagrange(1), "lambda");
      Expr alpha = new UnknownFunction(new Lagrange(1), "alpha");

      /* create symbolic differential operators */
      Expr dx = new Derivative(0);
      Expr dy = new Derivative(1);
      Expr grad = List(dx, dy);

      /* create symbolic coordinate functions */
      Expr x = new CoordExpr(0);
      Expr y = new CoordExpr(1);

      /* create target function */
      const double pi = 4.0*atan(1.0);
      Expr uStar = sin(pi*x)*sin(pi*y);
      
      /* create quadrature rules of different orders */
      QuadratureFamily q1 = new GaussianQuadrature(1);
      QuadratureFamily q2 = new GaussianQuadrature(2);
      QuadratureFamily q4 = new GaussianQuadrature(4);

      /* Regularization weight */
      double R = 0.001;
      double U0 = 1.0/(1.0 + 4.0*pow(pi,4.0)*R);
      double A0 = -2.0*pi*pi*U0;

      /* Form objective function */
      Expr reg = Integral(interior, 0.5 * R * alpha*alpha, q2);
      Expr fit = Integral(interior, 0.5 * pow(u-uStar, 2.0), q4);

      Expr constraintEqn = Integral(interior, 
        (grad*lambda)*(grad*u) + lambda*alpha, q2);
      Expr L = reg + fit + constraintEqn;

      Expr constraintBC = EssentialBC(bdry, lambda*u, q2);
      Functional Lagrangian(mesh, L, constraintBC, vecType);
      
      LinearSolver<double> solver 
        = LinearSolverBuilder::createSolver("amesos.xml");

      RCP<ObjectiveBase> obj = rcp(new LinearPDEConstrainedObj(
        Lagrangian, u, u0, lambda, lambda0, alpha, alpha0,
        solver));

      Vector<double> xInit = obj->getInit();

      bool doFDCheck = false;
      if (doFDCheck)
      {
        Out::root() << "Doing FD check of gradient..." << endl;
        bool fdOK = obj->fdCheck(xInit, 1.0e-6, 2);
        if (fdOK) 
        {
          Out::root() << "FD check OK" << endl;
        }
        else
        {
          Out::root() << "FD check FAILED" << endl;
        }
      }

      RCP<UnconstrainedOptimizerBase> opt 
          = OptBuilder::createOptimizer("basicLMBFGS.xml");
      opt->setVerb(2);

      OptState state = opt->run(obj, xInit);

      bool ok = true;
      if (state.status() != Opt_Converged)
      {
        Out::root()<< "optimization failed: " << state.status() << endl;
        TEUCHOS_TEST_FOR_EXCEPT(state.status() != Opt_Converged);
      }

      Out::root() << "opt converged: " << state.iter() << " iterations"
                  << endl;
      Out::root() << "exact solution: U0=" << U0 << " A0=" << A0 << endl;
      FieldWriter w = new VTKWriter("PoissonSourceInversion");
      w.addMesh(mesh);
      w.addField("u", new ExprFieldWrapper(u0));
      w.addField("alpha", new ExprFieldWrapper(alpha0));
      w.addField("lambda", new ExprFieldWrapper(lambda0));
      w.write();
      
      
      double uErr = L2Norm(mesh, interior, u0-U0*uStar, q4);
      double aErr = L2Norm(mesh, interior, alpha0-A0*uStar, q4);
      Out::root() << "error in u = " << uErr << endl;
      Out::root() << "error in alpha = " << aErr << endl;

      double tol = 0.01;
      Sundance::passFailTest(uErr + aErr, tol);
    }
	catch(std::exception& e)
		{
      cerr << "main() caught exception: " << e.what() << endl;
		}
	Sundance::finalize();
  return Sundance::testStatus(); 
}
int main(int argc, char *argv[])
{
  int rtn = 0;

  try
  {
    /* Initialize MPI */
    GlobalMPISession session(&argc, &argv);


    /* The VectorType object will be used when we create vector spaces, 
     * specifying what type of low-level linear algebra implementation
     * will be used. */
    VectorType<double> vecType = new EpetraVectorType();

    /* Construct the objective function */
    int M = 6;
    double alpha = 40.0;
    RCP<ObjectiveBase> obj = rcp(new Rosenbrock(M, alpha, vecType));

    Out::root() << "Objective function is " << obj->description()
                << endl;

    /* Get the starting point for the optimization run */
    Vector<double> xInit = obj->getInit();
    
    /* Run a finite-difference calculation of the function's gradient. This
     * will be expensive, but is a valuable test when developing new objective
     * functions */
    Out::root() << "Doing FD check of gradient..." << endl;
    bool fdOK = obj->fdCheck(xInit, 1.0e-6, 0);
    TEUCHOS_TEST_FOR_EXCEPTION(!fdOK, std::runtime_error,
      "finite difference test of Rosenbrock function gradient FAILED");
    Out::root() << "FD check OK!" << endl << endl;


    /* Create an optimizer object. */
    RCP<UnconstrainedOptimizerBase> opt 
      = OptBuilder::createOptimizer("basicLMBFGS.xml");

    /* Set the optimizer's verbosity to a desired volume of output */
    opt->setVerb(1);

    /* Run the optimizer with xInit s the initial guess. 
     * The results (location and value of min) and 
     * diagnostic information about the run are stored in the OptState
     * object returned by the run() function. */
    OptState state = opt->run(obj, xInit);

    /* Check the output */
    if (state.status() != Opt_Converged)
    {
      Out::root() << "optimization failed: " << state.status() << endl;
      rtn = -1;
    }
    else /* We converged, so let's make sure we got the right solution */
    {
      Out::root() << "optimization converged!" << endl; 
      Out::root() << "Iterations taken: " << state.iter() << endl;
      Out::root() << "Approximate minimum value: " << state.fCur() << endl;

      /* The exact solution is [1,1,\cdots, 1, 1]. */
      Vector<double> exactAns = xInit.space().createMember();
      exactAns.setToConstant(1.0);
      
      /* Compute the norm of the error in the location of the minimum */
      double locErr = (exactAns - state.xCur()).norm2();
      Out::root() << "||x-x^*||=" << locErr << endl;
      
      /* Compare the error to a desired tolerance */
      double testTol = 1.0e-4;
      if (locErr > testTol) 
      {
        rtn = -1;
      }
    }
     
    if (rtn == 0)
    {
      Out::root() << "test PASSED" << endl;
    }
    else
    {
      Out::root() << "test FAILED" << endl;
    }
  }
  catch(std::exception& e)
  {
    std::cerr << "Caught exception: " << e.what() << endl;
    rtn = -1;
  }
  return rtn;
}
int main(int argc, char** argv)
{
  try
		{
			Sundance::init(&argc, &argv);
      int np = MPIComm::world().getNProc();
      
      int nx = 64;
      const double pi = 4.0*atan(1.0);
      MeshType meshType = new BasicSimplicialMeshType();
      MeshSource mesher = new PartitionedLineMesher(0.0, pi, nx, meshType);

      Mesh mesh = mesher.getMesh();
      CellFilter interior = new MaximalCellFilter();
      CellFilter bdry = new BoundaryCellFilter();
      
      /* Create a vector space factory, used to 
       * specify the low-level linear algebra representation */
      VectorType<double> vecType = new EpetraVectorType();

      /* create symbolic coordinate functions */
      Expr x = new CoordExpr(0);

      /* create target function */
      double R = 0.01;
      Expr sx = sin(x);
      Expr cx = cos(x);
      Expr ssx = sin(sx);
      Expr sx2 = sx*sx;
      Expr cx2 = cx*cx;
      Expr f = sx2 - sx - ssx;
      Expr uStar = 2.0*R*(sx2-cx2) + R*sx2*ssx + sx;

      /* Form exact solution */
      Expr uEx = sx;
      Expr lambdaEx = R*sx2;
      Expr alphaEx = -lambdaEx/R;
      

      /* create a discrete space on the mesh */
      BasisFamily bas = new Lagrange(1);
      DiscreteSpace discreteSpace(mesh, bas, vecType);

      /* initialize the design, state, and multiplier vectors to constants */
      Expr alpha0 = new DiscreteFunction(discreteSpace, 0.25, "alpha0");
      Expr u0 = new DiscreteFunction(discreteSpace, 0.5, "u0");
      Expr lambda0 = new DiscreteFunction(discreteSpace, 0.25, "lambda0");

      /* create symbolic objects for test and unknown functions */
      Expr u = new UnknownFunction(bas, "u");
      Expr lambda = new UnknownFunction(bas, "lambda");
      Expr alpha = new UnknownFunction(bas, "alpha");

      /* create symbolic differential operators */
      Expr dx = new Derivative(0);
      Expr grad = dx;

      /* create quadrature rules of different orders */
      QuadratureFamily q1 = new GaussianQuadrature(1);
      QuadratureFamily q2 = new GaussianQuadrature(2);
      QuadratureFamily q4 = new GaussianQuadrature(4);

      /* Form objective function */
      Expr reg = Integral(interior, 0.5 * R * alpha*alpha, q2);
      Expr fit = Integral(interior, 0.5 * pow(u-uStar, 2.0), q4);

      Expr constraintEqn = Integral(interior, 
        (grad*lambda)*(grad*u) + lambda*(alpha + sin(u) + f), q4);
      Expr L = reg + fit + constraintEqn;

      Expr constraintBC = EssentialBC(bdry, lambda*u, q2);

      Functional Lagrangian(mesh, L, constraintBC, vecType);
      
      LinearSolver<double> adjSolver 
        = LinearSolverBuilder::createSolver("amesos.xml");
      ParameterXMLFileReader reader("nox-amesos.xml");
      ParameterList noxParams = reader.getParameters();
      NOXSolver nonlinSolver(noxParams);

      RCP<PDEConstrainedObjBase> obj = rcp(new NonlinearPDEConstrainedObj(
        Lagrangian, u, u0, lambda, lambda0, alpha, alpha0,
        nonlinSolver, adjSolver));

      Vector<double> xInit = obj->getInit();

      bool doFDCheck = true;
      if (doFDCheck)
      {
        Out::root() << "Doing FD check of gradient..." << endl;
        bool fdOK = obj->fdCheck(xInit, 1.0e-6, 0);
        if (fdOK) 
        {
          Out::root() << "FD check OK" << endl;
        }
        else
        {
          Out::root() << "FD check FAILED" << endl;
          TEUCHOS_TEST_FOR_EXCEPT(!fdOK);
        }
      }

      RCP<UnconstrainedOptimizerBase> opt 
          = OptBuilder::createOptimizer("basicLMBFGS.xml");
      opt->setVerb(3);

      OptState state = opt->run(obj, xInit);

      if (state.status() != Opt_Converged)
      {
        Out::root()<< "optimization failed: " << state.status() << endl;
        TEUCHOS_TEST_FOR_EXCEPT(state.status() != Opt_Converged);
      }
      else
      {
        Out::root() << "opt converged: " << state.iter() << " iterations"
                    << endl;
      }
      FieldWriter w = new MatlabWriter("NonlinControl1D");
      w.addMesh(mesh);
      w.addField("u", new ExprFieldWrapper(u0));
      w.addField("alpha", new ExprFieldWrapper(alpha0));
      w.addField("lambda", new ExprFieldWrapper(lambda0));
      w.write();

      double uErr = L2Norm(mesh, interior, u0-uEx, q4);
      double lamErr = L2Norm(mesh, interior, lambda0-lambdaEx, q4);
      double aErr = L2Norm(mesh, interior, alpha0-alphaEx, q4);
      Out::root() << "error in u = " << uErr << endl;
      Out::root() << "error in lambda = " << lamErr << endl;
      Out::root() << "error in alpha = " << aErr << endl;

      double tol = 0.05;
      Sundance::passFailTest(uErr + lamErr + aErr, tol);
    }
	catch(exception& e)
		{
      cerr << "main() caught exception: " << e.what() << endl;
		}
	Sundance::finalize();
  return Sundance::testStatus(); 
}
int main(int argc, char *argv[])
{
  int stat = 0;
  try
  {
    GlobalMPISession session(&argc, &argv);
    Tabs::showDepth() = false;
    VectorType<double> vecType = new EpetraVectorType();
    double testTol = 1.0e-4;

    
    int n = 200;
    RCP<UncTestProb> ell = rcp(new Ellipsoid(n, vecType));
    RCP<UncTestProb> rosen = rcp(new Rosenbrock(2, 1.0, vecType));

    Array<RCP<UncTestProb> > probs = tuple(ell, rosen);
    Array<string> algs 
      = tuple<string>("basicLMBFGS", "steepestDescent");

    int numFail = 0;
    int testCount = 0;

    for (int i=0; i<probs.size(); i++)
    {
      RCP<UncTestProb> obj = probs[i];
      Out::root() << "====================================================="
                  << endl
                  << "  running test case: " << obj->description() << endl
                  << "====================================================="
                  << endl << endl;
      Tabs tab1;
      
      
      Vector<double> xInit = obj->getInit();
      Out::root() << tab1 << "Doing FD check of gradient..." << endl;
      bool fdOK = obj->fdCheck(xInit, 1.0e-6, 0);
      if (!fdOK)
      {
        Tabs tab2;
        Out::root() << tab2 << "FD check FAILED!" << endl;
        obj->fdCheck(xInit, 1.0e-6, 2);
        numFail++;
        continue;
      }
      else
      {
        Tabs tab2;
        Out::root() << tab2 << "FD check OK" << endl;
      }
      Out::root() << endl;

      for (int j=0; j<algs.size(); j++)
      {
        Tabs tab2;
        RCP<UnconstrainedOptimizerBase> opt 
          = OptBuilder::createOptimizer(algs[j] + ".xml");

        Out::root() << tab1 << "Running opt algorithm [" 
                    << algs[j] << "]" << endl;
        RCP<ConvergenceMonitor> mon = rcp(new ConvergenceMonitor());

        opt->setVerb(0);

        OptState state = opt->run(obj, xInit, mon);

        bool ok = true;

        if (state.status() != Opt_Converged)
        {
          Out::root() << tab2 << "[" << algs[j] 
                      << "] optimization failed: " << state.status() << endl;
          ok = false;
        }
        else
        {
          Out::root() << tab2 
                      << "[" << algs[j] 
                      << "] optimization converged!" << endl ;
          
          Vector<double> exactAns = obj->exactSoln();
          
          double locErr = (exactAns - state.xCur()).norm2();
          Out::root() << tab2 << "||x-x^*||=" << locErr << endl;
          if (locErr > testTol) 
          {
            ok = false;
          }
          
          string monName = algs[j] + ".dat";
          ofstream os(monName.c_str());
          mon->write(os);
        }
        
        if (ok) 
        {
          Out::root() << tab2 << " test  PASSED " << endl;
        }
        else
        {
          numFail++;
          Out::root() << tab2 << " ****** test FAILED ****** " << endl;
        }
        Out::root() << endl;
        testCount++;
      }
    }
    
    if (numFail > 0)
    {
      Out::root() << "detected " << numFail 
                  << " FAILURES out of " << testCount << " tests" << endl;
      stat = -1;
    }
    else
    {
      Out::root() << "all tests PASSED" << endl;
    }
  }
  catch(std::exception& e)
  {
    std::cerr << "Caught exception: " << e.what() << endl;
    stat = -1;
  }
  return stat;
}