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; }
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[]) { 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; }