PlayaOptRosenbrock.cpp
00001
00002
00003
00004
00005 #include "PlayaOptBuilder.hpp"
00006 #include "PlayaEpetraVectorType.hpp"
00007 #include "PlayaVectorType.hpp"
00008 #include "PlayaOut.hpp"
00009 #include "PlayaLinearCombinationImpl.hpp"
00010
00011 #include "PlayaRosenbrock.hpp"
00012
00013 #include "Teuchos_GlobalMPISession.hpp"
00014 #include "PlayaMPIComm.hpp"
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 using namespace Playa;
00030
00031 int main(int argc, char *argv[])
00032 {
00033 int rtn = 0;
00034
00035 try
00036 {
00037
00038 GlobalMPISession session(&argc, &argv);
00039
00040
00041
00042
00043
00044 VectorType<double> vecType = new EpetraVectorType();
00045
00046
00047 int M = 6;
00048 double alpha = 40.0;
00049 RCP<ObjectiveBase> obj = rcp(new Rosenbrock(M, alpha, vecType));
00050
00051 Out::root() << "Objective function is " << obj->description()
00052 << endl;
00053
00054
00055 Vector<double> xInit = obj->getInit();
00056
00057
00058
00059
00060 Out::root() << "Doing FD check of gradient..." << endl;
00061 bool fdOK = obj->fdCheck(xInit, 1.0e-6, 0);
00062 TEUCHOS_TEST_FOR_EXCEPTION(!fdOK, std::runtime_error,
00063 "finite difference test of Rosenbrock function gradient FAILED");
00064 Out::root() << "FD check OK!" << endl << endl;
00065
00066
00067
00068 RCP<UnconstrainedOptimizerBase> opt
00069 = OptBuilder::createOptimizer("basicLMBFGS.xml");
00070
00071
00072 opt->setVerb(1);
00073
00074
00075
00076
00077
00078 OptState state = opt->run(obj, xInit);
00079
00080
00081 if (state.status() != Opt_Converged)
00082 {
00083 Out::root() << "optimization failed: " << state.status() << endl;
00084 rtn = -1;
00085 }
00086 else
00087 {
00088 Out::root() << "optimization converged!" << endl;
00089 Out::root() << "Iterations taken: " << state.iter() << endl;
00090 Out::root() << "Approximate minimum value: " << state.fCur() << endl;
00091
00092
00093 Vector<double> exactAns = xInit.space().createMember();
00094 exactAns.setToConstant(1.0);
00095
00096
00097 double locErr = (exactAns - state.xCur()).norm2();
00098 Out::root() << "||x-x^*||=" << locErr << endl;
00099
00100
00101 double testTol = 1.0e-4;
00102 if (locErr > testTol)
00103 {
00104 rtn = -1;
00105 }
00106 }
00107
00108 if (rtn == 0)
00109 {
00110 Out::root() << "test PASSED" << endl;
00111 }
00112 else
00113 {
00114 Out::root() << "test FAILED" << endl;
00115 }
00116 }
00117 catch(std::exception& e)
00118 {
00119 std::cerr << "Caught exception: " << e.what() << endl;
00120 rtn = -1;
00121 }
00122 return rtn;
00123 }