NLPInterfacePack: C++ Interfaces and Implementation for Non-Linear Programs Version of the Day
NLPInterfacePack_test_nlp_first_order.cpp
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
00005 //                  Copyright (2003) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00038 // 
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #include <assert.h>
00043 
00044 #include "NLPInterfacePack_test_nlp_first_order.hpp"
00045 #include "NLPInterfacePack_CalcFiniteDiffProd.hpp"
00046 #include "NLPInterfacePack_CalcFiniteDiffProdSetOptions.hpp"
00047 #include "NLPInterfacePack_NLPTester.hpp"
00048 #include "NLPInterfacePack_NLPTesterSetOptions.hpp"
00049 #include "NLPInterfacePack_NLPFirstDerivTester.hpp"
00050 #include "NLPInterfacePack_NLPFirstDerivTesterSetOptions.hpp"
00051 #include "NLPInterfacePack_NLPFirstOrder.hpp"
00052 #include "AbstractLinAlgPack_VectorSpace.hpp"
00053 #include "AbstractLinAlgPack_VectorSpaceTester.hpp"
00054 #include "AbstractLinAlgPack_VectorSpaceTesterSetOptions.hpp"
00055 #include "AbstractLinAlgPack_VectorMutable.hpp"
00056 #include "AbstractLinAlgPack_VectorOut.hpp"
00057 #include "AbstractLinAlgPack_MatrixOp.hpp"
00058 #include "AbstractLinAlgPack_MatrixOpOut.hpp"
00059 #include "TestingHelperPack_update_success.hpp"
00060 #include "Teuchos_FancyOStream.hpp"
00061 
00062 bool NLPInterfacePack::test_nlp_first_order(
00063   NLPFirstOrder                                 *nlp
00064   ,OptionsFromStreamPack::OptionsFromStream     *options
00065   ,std::ostream                                 *out
00066   )
00067 {
00068   namespace rcp = MemMngPack;
00069   using TestingHelperPack::update_success;
00070 
00071   bool result;
00072   bool success = true;
00073   
00074   Teuchos::VerboseObjectTempState<NLP>
00075     nlpOutputTempState(Teuchos::rcp(nlp,false),Teuchos::getFancyOStream(Teuchos::rcp(out,false)),Teuchos::VERB_LOW);
00076 
00077   if(out)
00078     *out << "\n*********************************"
00079          << "\n*** test_nlp_first_order(...) ***"
00080          << "\n*********************************\n";
00081   
00082   nlp->initialize(true);
00083 
00084   // Test the DVector spaces
00085   if(out)
00086     *out << "\nTesting the vector spaces ...\n";
00087   
00088   VectorSpaceTester vec_space_tester;
00089   if(options) {
00090     VectorSpaceTesterSetOptions
00091       opt_setter(&vec_space_tester);
00092     opt_setter.set_options(*options);
00093   }
00094 
00095   if(out)
00096     *out << "\nTesting nlp->space_x() ...\n";
00097   result = vec_space_tester.check_vector_space(*nlp->space_x(),out);
00098   if(out) {
00099     if(result)
00100       *out << "nlp->space_x() checks out!\n";
00101     else
00102       *out << "nlp->space_x() check failed!\n";
00103   }
00104   update_success( result, &success );
00105 
00106   if( nlp->m() ) {
00107     if(out)
00108       *out << "\nTesting nlp->space_c() ...\n";
00109     result = vec_space_tester.check_vector_space(*nlp->space_c(),out);
00110     if(out) {
00111       if(result)
00112         *out << "nlp->space_c() checks out!\n";
00113       else
00114         *out << "nlp->space_c() check failed!\n";
00115     }
00116     update_success( result, &success );
00117   }
00118 
00119   // Test the NLP interface first!
00120 
00121   NLPTester nlp_tester;
00122   if(options) {
00123     NLPTesterSetOptions
00124       nlp_tester_opt_setter(&nlp_tester);
00125     nlp_tester_opt_setter.set_options(*options);
00126   }
00127   const bool print_all_warnings = nlp_tester.print_all();
00128 
00129   result = nlp_tester.test_interface(
00130     nlp, nlp->xinit(), print_all_warnings, out );
00131   update_success( result, &success );
00132   
00133   // Test the NLPFirstOrder interface now!
00134 
00135   const size_type
00136     n  = nlp->n(),
00137     m  = nlp->m();
00138   VectorSpace::vec_mut_ptr_t
00139     c   = m  ? nlp->space_c()->create_member() : Teuchos::null,
00140     Gf  =      nlp->space_x()->create_member();
00141   NLPFirstOrder::mat_fcty_ptr_t::element_type::obj_ptr_t
00142     Gc  = m  ? nlp->factory_Gc()->create() : Teuchos::null;
00143 
00144   if(m) {
00145     if(out)
00146       *out << "\nCalling nlp->calc_Gc(...) at nlp->xinit() ...\n";
00147     nlp->set_Gc( Gc.get() );
00148     nlp->calc_Gc( nlp->xinit(), true );
00149     if(nlp_tester.print_all()) {
00150       *out << "\nGc =\n" << *Gc;
00151     }
00152   }
00153 
00154   if(out)
00155     *out << "\nCalling nlp->calc_Gf(...) at nlp->xinit() ...\n";
00156   nlp->set_Gf( Gf.get() );
00157   nlp->calc_Gf( nlp->xinit(), m == 0 );
00158   if(nlp_tester.print_all())
00159     *out << "\nGf =\n" << *Gf;
00160 
00161   CalcFiniteDiffProd
00162     calc_fd_prod;
00163   if(options) {
00164     CalcFiniteDiffProdSetOptions
00165       options_setter( &calc_fd_prod );
00166     options_setter.set_options(*options);
00167   }
00168   NLPFirstDerivTester
00169     nlp_first_derivatives_tester(Teuchos::rcp(&calc_fd_prod,false));
00170   if(options) {
00171     NLPFirstDerivTesterSetOptions
00172       nlp_tester_opt_setter(&nlp_first_derivatives_tester);
00173     nlp_tester_opt_setter.set_options(*options);
00174   }
00175   result = nlp_first_derivatives_tester.finite_diff_check(
00176     nlp, nlp->xinit()
00177     ,nlp->num_bounded_x() ? &nlp->xl() : NULL
00178     ,nlp->num_bounded_x() ? &nlp->xu() : NULL
00179     ,Gc.get(), Gf.get()
00180     ,print_all_warnings, out
00181     );
00182   update_success( result, &success );
00183 
00184   return success;
00185 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends