MOOCHO Version of the Day
NLPThyraEpetraModelEval4DOptMain.cpp
00001 /*
00002 // @HEADER
00003 // ***********************************************************************
00004 // 
00005 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
00006 //                  Copyright (2003) Sandia Corporation
00007 // 
00008 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00009 // license for use of this work by or on behalf of the U.S. Government.
00010 // 
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00039 // 
00040 // ***********************************************************************
00041 // @HEADER
00042 */
00043 
00044 #include "NLPInterfacePack_NLPFirstOrderThyraModelEvaluator.hpp"
00045 #include "EpetraModelEval4DOpt.hpp"
00046 #include "MoochoPack_MoochoThyraSolver.hpp"
00047 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
00048 #include "Thyra_EpetraModelEvaluator.hpp"
00049 #include "Teuchos_GlobalMPISession.hpp"
00050 #include "Teuchos_VerboseObject.hpp"
00051 #include "Teuchos_CommandLineProcessor.hpp"
00052 #include "Teuchos_StandardCatchMacros.hpp"
00053 
00054 int main( int argc, char* argv[] )
00055 {
00056   using Teuchos::CommandLineProcessor;
00057   typedef AbstractLinAlgPack::value_type  Scalar;
00058   using MoochoPack::MoochoSolver;
00059   using MoochoPack::MoochoThyraSolver;
00060 
00061   bool dummySuccess = true;
00062 
00063   Teuchos::GlobalMPISession mpiSession(&argc,&argv);
00064 
00065   Teuchos::RCP<Teuchos::FancyOStream>
00066     out = Teuchos::VerboseObjectBase::getDefaultOStream();
00067 
00068   try {
00069 
00070     Stratimikos::DefaultLinearSolverBuilder lowsfCreator;
00071     MoochoThyraSolver                     solver;
00072   
00073     //
00074     // Get options from the command line
00075     //
00076     
00077     Scalar       xt0         = 1.0;
00078     Scalar       xt1         = 1.0;
00079     Scalar       pt0         = 2.0;
00080     Scalar       pt1         = 0.0;
00081     Scalar       d           = 10.0;
00082     Scalar       x00         = 1.0;
00083     Scalar       x01         = 1.0;
00084     Scalar       p00         = 2.0;
00085     Scalar       p01         = 0.0;
00086     Scalar       pL0         = -1e+50;
00087     Scalar       pL1         = -1e+50;
00088     Scalar       pU0         = +1e+50;
00089     Scalar       pU1         = +1e+50;
00090 
00091     Scalar       xL0         = -1e+50;
00092     Scalar       xL1         = -1e+50;
00093     Scalar       xU0         = +1e+50;
00094     Scalar       xU1         = +1e+50;
00095 
00096     bool supportDerivs = true;
00097 
00098     std::string extraXmlFile = "";
00099 
00100     CommandLineProcessor  clp(false); // Don't throw exceptions
00101 
00102     lowsfCreator.setupCLP(&clp);
00103     solver.setupCLP(&clp);
00104 
00105     clp.setOption( "xt0", &xt0 );
00106     clp.setOption( "xt1", &xt1 );
00107     clp.setOption( "pt0", &pt0 );
00108     clp.setOption( "pt1", &pt1 );
00109     clp.setOption( "d", &d );
00110     clp.setOption( "x00", &x00 );
00111     clp.setOption( "x01", &x01 );
00112     clp.setOption( "p00", &p00 );
00113     clp.setOption( "p01", &p01 );
00114     clp.setOption( "pL0", &pL0 );
00115     clp.setOption( "pL1", &pL1 );
00116     clp.setOption( "pU0", &pU0 );
00117     clp.setOption( "pU1", &pU1 );
00118     clp.setOption( "xL0", &xL0 );
00119     clp.setOption( "xL1", &xL1 );
00120     clp.setOption( "xU0", &xU0 );
00121     clp.setOption( "xU1", &xU1 );
00122     clp.setOption( "support-derivs", "no-support-derivs", &supportDerivs );
00123     clp.setOption("extra-xml-file",&extraXmlFile,"File with extra XML text that will modify the initial XML read in");
00124  
00125     std::string line("");
00126     if(extraXmlFile.length()) {
00127       std::ifstream myfile(extraXmlFile.c_str());
00128       if (myfile.is_open())
00129       {
00130         getline (myfile,line);
00131         solver.extraParamsXmlStringOption(line);
00132         std::cout << line << "\n";
00133         myfile.close();
00134       }
00135     }
00136 
00137     CommandLineProcessor::EParseCommandLineReturn
00138       parse_return = clp.parse(argc,argv,&std::cerr);
00139 
00140     if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL )
00141       return parse_return;
00142 
00143     lowsfCreator.readParameters(out.get());
00144     solver.readParameters(out.get());
00145 
00146     //
00147     // Create the NLP
00148     //
00149     
00150     // Create the EpetraExt::ModelEvaluator object
00151 
00152     Teuchos::RCP<EpetraModelEval4DOpt>
00153       epetraModel = Teuchos::rcp(new EpetraModelEval4DOpt(xt0,xt1,pt0,pt1,d,x00,x01,p00,p01));
00154     epetraModel->setSupportDerivs(supportDerivs);
00155     epetraModel->set_p_bounds(pL0,pL1,pU0,pU1);
00156     epetraModel->set_x_bounds(xL0,xL1,xU0,xU1);
00157 
00158     // Create the Thyra::EpetraModelEvaluator object
00159 
00160     Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<double> >
00161       lowsFactory = lowsfCreator.createLinearSolveStrategy("");
00162 
00163     Teuchos::RCP<Thyra::EpetraModelEvaluator>
00164       epetraThyraModel = Teuchos::rcp(new Thyra::EpetraModelEvaluator());
00165     
00166     epetraThyraModel->initialize(epetraModel,lowsFactory);
00167     
00168     //
00169     // Solve the NLP
00170     //
00171     
00172     // Set the model
00173     solver.setModel(epetraThyraModel);
00174 
00175     // Read the initial guess if one exists
00176     solver.readInitialGuess(out.get());
00177 
00178     // Solve the NLP
00179     const MoochoSolver::ESolutionStatus solution_status = solver.solve();
00180 
00181     // Write the final solution if requested
00182     solver.writeFinalSolution(out.get());
00183 
00184     // Write the parameters that where read
00185     lowsfCreator.writeParamsFile(*lowsFactory);
00186     solver.writeParamsFile();
00187     
00188     //
00189     // Return the solution status (0 if sucessfull)
00190     //
00191 
00192     return solution_status;
00193 
00194   }
00195   TEUCHOS_STANDARD_CATCH_STATEMENTS(true,*out,dummySuccess)
00196 
00197   return MoochoSolver::SOLVE_RETURN_EXCEPTION;
00198 
00199 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends