void print_vector( const ROL::Vector<Real> &x ) { typedef ROL::Vector<Real> V; typedef ROL::StdVector<Real> SV; typedef ROL::PartitionedVector<Real> PV; typedef typename PV::size_type size_type; const PV eb = Teuchos::dyn_cast<const PV>(x); size_type n = eb.numVectors(); for(size_type k=0; k<n; ++k) { std::cout << "[subvector " << k << "]" << std::endl; Teuchos::RCP<const V> vec = eb.get(k); Teuchos::RCP<const std::vector<Real> > vp = Teuchos::dyn_cast<const SV>(*vec).getVector(); for(size_type i=0; i<vp->size(); ++i) { std::cout << (*vp)[i] << std::endl; } } }
int main(int argc, char *argv[]) { using namespace ROL; using Teuchos::RCP; using Teuchos::rcp; typedef Teuchos::ParameterList PL; typedef Vector<RealT> V; typedef PartitionedVector<RealT> PV; typedef OptimizationProblem<RealT> OPT; typedef NonlinearProgram<RealT> NLP; typedef AlgorithmState<RealT> STATE; Teuchos::GlobalMPISession mpiSession(&argc, &argv); // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. int iprint = argc - 1; RCP<std::ostream> outStream; Teuchos::oblackholestream bhs; // outputs nothing if (iprint > 0) outStream = rcp(&std::cout, false); else outStream = rcp(&bhs, false); int errorFlag = 0; int numProblems = 40; RealT errtol = 1e-5; // Require norm of computed solution error be less than this std::vector<int> passedTests; std::vector<int> failedTests; std::vector<std::string> converged; std::vector<std::string> stepname; HS::ProblemFactory<RealT> factory; RCP<NLP> nlp; RCP<OPT> opt; // Get two copies so we can validate without setting defaults RCP<PL> parlist = rcp( new PL() ); RCP<PL> parlistCopy = rcp( new PL() ); RCP<PL> test = rcp( new PL() ); Teuchos::updateParametersFromXmlFile(std::string("hs_parameters.xml"),parlist.ptr()); Teuchos::updateParametersFromXmlFile(std::string("hs_parameters.xml"),parlistCopy.ptr()); Teuchos::updateParametersFromXmlFile(std::string("test_parameters.xml"),test.ptr()); RCP<const PL> validParameters = getValidROLParameters(); parlistCopy->validateParametersAndSetDefaults(*validParameters); RCP<V> x; RCP<const STATE> algo_state; bool problemSolved; // *** Test body. try { for(int n=1; n<=numProblems; ++n) { *outStream << "\nHock & Schittkowski Problem " << std::to_string(n) << std::endl; nlp = factory.getProblem(n); opt = nlp->getOptimizationProblem(); EProblem problemType = opt->getProblemType(); std::string str; switch( problemType ) { case TYPE_U: str = test->get("Type-U Step","Trust Region"); break; case TYPE_B: str = test->get("Type-B Step","Trust Region"); break; case TYPE_E: str = test->get("Type-E Step","Composite Step"); break; case TYPE_EB: str = test->get("Type-EB Step","Augmented Lagrangian"); break; case TYPE_LAST: TEUCHOS_TEST_FOR_EXCEPTION(true,std::invalid_argument,"Error: Unsupported problem type!"); break; } stepname.push_back(str); parlist->sublist("Step").set("Type",str); OptimizationSolver<RealT> solver( *opt, *parlist ); solver.solve(*outStream); algo_state = solver.getAlgorithmState(); x = opt->getSolutionVector(); if( nlp->dimension_ci() > 0 ) { // Has slack variables, extract optimization vector PV xpv = Teuchos::dyn_cast<PV>(*x); RCP<V> sol = xpv.get(0); problemSolved = nlp->foundAcceptableSolution( *sol, errtol ); } else { problemSolved = nlp->foundAcceptableSolution( *x, errtol ); } RealT tol = std::sqrt(ROL_EPSILON<RealT>()); *outStream << "Target objective value = " << nlp->getSolutionObjectiveValue() << std::endl; *outStream << "Attained objective value = " << opt->getObjective()->value(*x,tol) << std::endl; if( problemSolved ) { *outStream << "Computed an acceptable solution" << std::endl; passedTests.push_back(n); converged.push_back(std::to_string(algo_state->nfval)); } else { *outStream << "Failed to converge" << std::endl; failedTests.push_back(n); converged.push_back("Failed"); } } // end for loop over HS problems *outStream << "\nTests passed: "; for( auto const& value : passedTests ) { *outStream << value << " "; } *outStream << "\nTests failed: "; for( auto const& value : failedTests ) { *outStream << value << " "; } *outStream << std::endl; if( passedTests.size() > failedTests.size() ) { *outStream << "Most tests passed." << std::endl; } else { *outStream << "Most tests failed." << std::endl; errorFlag++; } *outStream << "\n\nPERFORMANCE SUMMARY:\n\n"; *outStream << std::setw(16) << "H&S Problem #" << std::setw(24) << "Step Used" << std::setw(12) << "#fval" << std::endl; *outStream << std::string(52,'-') << std::endl; for(int n=0; n<numProblems; ++n) { *outStream << std::setw(16) << n+1 << std::setw(24) << stepname[n] << std::setw(12) << converged[n] << std::endl; } } catch (std::logic_error err) { *outStream << err.what() << "\n"; errorFlag = -1000; }; // end try if (errorFlag != 0) std::cout << "End Result: TEST FAILED\n"; else std::cout << "End Result: TEST PASSED\n"; return 0; }