NLPInterfacePack: C++ Interfaces and Implementation for Non-Linear Programs Version of the Day
NLPInterfacePack_NLP.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 <limits>
00043 
00044 #include "NLPInterfacePack_NLP.hpp"
00045 #include "AbstractLinAlgPack_VectorMutable.hpp"
00046 #include "AbstractLinAlgPack_VectorSpace.hpp"
00047 #include "Teuchos_Assert.hpp"
00048 
00049 namespace {
00050 const char name_f[] = "f";
00051 const char name_c[] = "c";
00052 const char name_c_breve[] = "c_breve";
00053 const char name_h_breve[] = "h_breve";
00054 NLPInterfacePack::NLP::options_ptr_t  null_options = Teuchos::null;
00055 } // end namespace
00056 
00057 namespace NLPInterfacePack {
00058 
00059 // static
00060 
00061 value_type NLP::infinite_bound()
00062 {
00063 //  return std::numeric_limits<value_type>::max();
00064   return 1e+50;
00065 }
00066 
00067 // constructors
00068 
00069 NLP::NLP()
00070 {}
00071 
00072 // destructor
00073 
00074 NLP::~NLP()
00075 {}
00076 
00077 void NLP::set_options( const options_ptr_t& options )
00078 {}
00079 
00080 const NLP::options_ptr_t&
00081 NLP::get_options() const
00082 {
00083   return null_options;
00084 }
00085 
00086 void NLP::initialize(bool test_setup)
00087 {
00088   num_f_evals_ = num_c_evals_ = 0;
00089 }
00090 
00091 // dimensionality
00092 
00093 size_type
00094 NLP::n() const
00095 {
00096   return this->space_x()->dim();
00097 }
00098 
00099 size_type 
00100 NLP::m() const
00101 {
00102   VectorSpace::space_ptr_t spc = this->space_c();
00103   return spc.get() ? spc->dim() : 0;
00104 }
00105 
00106 // initial guess
00107 
00108 void NLP::get_init_lagrange_mult(
00109   VectorMutable*   lambda
00110   ,VectorMutable*  nu
00111   ) const
00112 {
00113 #ifdef TEUCHOS_DEBUG
00114   TEUCHOS_TEST_FOR_EXCEPTION( lambda  && this->m()  == 0,            std::logic_error, "" );
00115   TEUCHOS_TEST_FOR_EXCEPTION( nu      && this->num_bounded_x() == 0, std::logic_error, "" );
00116 #endif
00117   if(lambda) {
00118 #ifdef TEUCHOS_DEBUG
00119     TEUCHOS_TEST_FOR_EXCEPTION( !this->space_c()->is_compatible(lambda->space()), VectorSpace::IncompatibleVectorSpaces, "" );
00120 #endif
00121     *lambda = 0.0;
00122   }
00123   if(nu) {
00124 #ifdef TEUCHOS_DEBUG
00125     TEUCHOS_TEST_FOR_EXCEPTION( !this->space_x()->is_compatible(nu->space()), VectorSpace::IncompatibleVectorSpaces, "" );
00126 #endif
00127     *nu = 0.0;
00128   }
00129 }
00130 
00131 // <<std comp>> members for f
00132 
00133 void NLP::set_f(value_type* f)
00134 {
00135   first_order_info_.f = f;
00136 }
00137 
00138 value_type* NLP::get_f()
00139 {
00140   return StandardCompositionRelationshipsPack::get_role_name(first_order_info_.f, false, name_f);
00141 }
00142 
00143 value_type& NLP::f()
00144 {
00145   return StandardCompositionRelationshipsPack::role_name(first_order_info_.f, false, name_f);
00146 }
00147 
00148 const value_type& NLP::f() const
00149 {
00150   return StandardCompositionRelationshipsPack::role_name(first_order_info_.f, false, name_f);
00151 }
00152 
00153 // <<std comp>> members for c
00154 
00155 void NLP::set_c(VectorMutable* c)
00156 {
00157 #ifdef TEUCHOS_DEBUG
00158   TEUCHOS_TEST_FOR_EXCEPTION( this->m() == 0, std::logic_error, "" );
00159   TEUCHOS_TEST_FOR_EXCEPTION( c && !this->space_c()->is_compatible(c->space()), VectorSpace::IncompatibleVectorSpaces, "" );
00160 #endif
00161   first_order_info_.c = c;
00162 }
00163 
00164 VectorMutable* NLP::get_c()
00165 {
00166 #ifdef TEUCHOS_DEBUG
00167   TEUCHOS_TEST_FOR_EXCEPTION( this->m() == 0, std::logic_error, "" );
00168 #endif
00169   return StandardCompositionRelationshipsPack::get_role_name(first_order_info_.c, false, name_c);
00170 }
00171 
00172 VectorMutable& NLP::c()
00173 {
00174 #ifdef TEUCHOS_DEBUG
00175   TEUCHOS_TEST_FOR_EXCEPTION( this->m() == 0, std::logic_error, "" );
00176 #endif
00177   return StandardCompositionRelationshipsPack::role_name(first_order_info_.c, false, name_c);
00178 }
00179 
00180 const Vector& NLP::c() const
00181 {
00182 #ifdef TEUCHOS_DEBUG
00183   TEUCHOS_TEST_FOR_EXCEPTION( this->m() == 0, std::logic_error, "" );
00184 #endif
00185   return StandardCompositionRelationshipsPack::role_name(first_order_info_.c, false, name_c);
00186 }
00187 
00188 void NLP::unset_quantities()
00189 {
00190   set_f(NULL);
00191   if(m()) set_c(NULL);
00192   if(m()-ns()) set_c_breve(NULL);
00193   if(m()-ns()) set_h_breve(NULL);
00194 }
00195 
00196 // calculations
00197 
00198 void NLP::calc_f(const Vector& x, bool newx) const
00199 {
00200   StandardCompositionRelationshipsPack::assert_role_name_set(first_order_info_.f, "NLP::calc_f()", name_f);
00201   imp_calc_f(x,newx,zero_order_info());
00202   num_f_evals_++;
00203 }
00204 
00205 void NLP::calc_c(const Vector& x, bool newx) const
00206 {
00207 #ifdef TEUCHOS_DEBUG
00208   TEUCHOS_TEST_FOR_EXCEPTION( this->m() == 0, std::logic_error, "" );
00209 #endif
00210   StandardCompositionRelationshipsPack::assert_role_name_set(first_order_info_.c, "NLP::calc_c()", name_c);
00211   imp_calc_c(x,newx,zero_order_info());
00212   num_c_evals_++;
00213 }
00214 
00215 void NLP::report_final_solution(
00216   const Vector&    x
00217   ,const Vector*   lambda
00218   ,const Vector*   nu
00219   ,bool            optimal
00220   )
00221 {
00222   // The default behavior is just to ignore the solution!
00223 }
00224 
00225 size_type NLP::num_f_evals() const
00226 {
00227   return num_f_evals_;
00228 }
00229 
00230 size_type NLP::num_c_evals() const
00231 {
00232 #ifdef TEUCHOS_DEBUG
00233   TEUCHOS_TEST_FOR_EXCEPTION( this->m() == 0, std::logic_error, "" );
00234 #endif
00235   return num_c_evals_;
00236 }
00237 
00238 // General inequalities and slack variables
00239 
00240 size_type NLP::ns() const
00241 {
00242   vec_space_ptr_t space_h_breve = this->space_h_breve();
00243   return space_h_breve.get() ? space_h_breve->dim() : 0;
00244 }
00245 
00246 NLP::vec_space_ptr_t NLP::space_c_breve() const
00247 {
00248   return this->space_c();
00249 }
00250 
00251 NLP::vec_space_ptr_t NLP::space_h_breve() const
00252 {
00253   return Teuchos::null;
00254 }
00255 
00256 const Vector& NLP::hl_breve() const
00257 {
00258   TEUCHOS_TEST_FOR_EXCEPTION(
00259     true, std::logic_error
00260     ,"NLP::hl_breve(): Error, this method must be overridden if space_h_breve is defined" );
00261 
00262   //execution should never reach this point, but compilers expect a non-void
00263   //function to return something, so we'll create a dummy value to use in a
00264   //return statement.
00265   //(a better design would not require function bodies for unimplemented
00266   //functions like this...)
00267   Vector* dummy = NULL;
00268   return(*dummy);
00269 }
00270 
00271 const Vector& NLP::hu_breve() const
00272 {
00273   TEUCHOS_TEST_FOR_EXCEPTION(
00274     true, std::logic_error
00275     ,"NLP::hl_breve(): Error, this method must be overridden if space_h_breve is defined" );
00276 
00277   //execution should never reach this point, but compilers expect a non-void
00278   //function to return something, so we'll create a dummy value to use in a
00279   //return statement.
00280   //(a better design would not require function bodies for unimplemented
00281   //functions like this...)
00282   Vector* dummy = NULL;
00283   return(*dummy);
00284 }
00285 
00286 void NLP::set_c_breve(VectorMutable* c_breve)
00287 {
00288 #ifdef TEUCHOS_DEBUG
00289   TEUCHOS_TEST_FOR_EXCEPTION( this->m() - this->ns() == 0, std::logic_error, "" );
00290   TEUCHOS_TEST_FOR_EXCEPTION( c_breve && !this->space_c_breve()->is_compatible(c_breve->space()), VectorSpace::IncompatibleVectorSpaces, "" );
00291 #endif
00292   first_order_info_breve_.c = c_breve;
00293 }
00294 
00295 VectorMutable* NLP::get_c_breve()
00296 {
00297 #ifdef TEUCHOS_DEBUG
00298   TEUCHOS_TEST_FOR_EXCEPTION( this->m() - this->ns() == 0, std::logic_error, "" );
00299 #endif
00300   return first_order_info_breve_.c;
00301 }
00302 
00303 VectorMutable& NLP::c_breve()
00304 {
00305 #ifdef TEUCHOS_DEBUG
00306   TEUCHOS_TEST_FOR_EXCEPTION( this->m() - this->ns() == 0, std::logic_error, "" );
00307 #endif
00308   return StandardCompositionRelationshipsPack::role_name(first_order_info_breve_.c, false, name_c_breve);
00309 }
00310 
00311 const Vector& NLP::c_breve() const
00312 {
00313 #ifdef TEUCHOS_DEBUG
00314   TEUCHOS_TEST_FOR_EXCEPTION( this->m() - this->ns() == 0, std::logic_error, "" );
00315 #endif
00316   return StandardCompositionRelationshipsPack::role_name(first_order_info_breve_.c, false, name_c_breve);
00317 }
00318 
00319 void NLP::set_h_breve(VectorMutable* h_breve)
00320 {
00321 #ifdef TEUCHOS_DEBUG
00322   TEUCHOS_TEST_FOR_EXCEPTION( this->m() - this->ns() == 0, std::logic_error, "" );
00323   TEUCHOS_TEST_FOR_EXCEPTION( h_breve && !this->space_h_breve()->is_compatible(h_breve->space()), VectorSpace::IncompatibleVectorSpaces, "" );
00324 #endif
00325   first_order_info_breve_.c = h_breve;
00326 }
00327 
00328 VectorMutable* NLP::get_h_breve()
00329 {
00330 #ifdef TEUCHOS_DEBUG
00331   TEUCHOS_TEST_FOR_EXCEPTION( this->m() - this->ns() == 0, std::logic_error, "" );
00332 #endif
00333   return first_order_info_breve_.h;
00334 }
00335 
00336 VectorMutable& NLP::h_breve()
00337 {
00338 #ifdef TEUCHOS_DEBUG
00339   TEUCHOS_TEST_FOR_EXCEPTION( this->m() - this->ns() == 0, std::logic_error, "" );
00340 #endif
00341   return StandardCompositionRelationshipsPack::role_name(first_order_info_breve_.c, false, name_h_breve);
00342 }
00343 
00344 const Vector& NLP::h_breve() const
00345 {
00346 #ifdef TEUCHOS_DEBUG
00347   TEUCHOS_TEST_FOR_EXCEPTION( this->m() - this->ns() == 0, std::logic_error, "" );
00348 #endif
00349   return StandardCompositionRelationshipsPack::role_name(first_order_info_breve_.c, false, name_h_breve);
00350 }
00351 
00352 const Permutation& NLP::P_var() const
00353 {
00354   TEUCHOS_TEST_FOR_EXCEPT(true);
00355 //  if(!P_var_.get()) P_var_ = Teuchos::rcp(new PermutationSerial(this->space_x());
00356   return *P_var_;
00357 }
00358 
00359 const Permutation& NLP::P_equ() const
00360 {
00361   TEUCHOS_TEST_FOR_EXCEPT(true);
00362 //  if(!P_equ_.get()) P_equ = Teuchos::rcp(new PermutationSerial(this->space_c());
00363   return *P_equ_;
00364 }
00365 
00366 void NLP::calc_c_breve(const Vector& x, bool newx) const
00367 {
00368 #ifdef TEUCHOS_DEBUG
00369   TEUCHOS_TEST_FOR_EXCEPTION( this->m() == 0 || this->ns() > 0, std::logic_error, "" );
00370 #endif
00371   StandardCompositionRelationshipsPack::assert_role_name_set(first_order_info_breve_.c, "NLP::calc_c_breve()", name_c_breve);
00372   imp_calc_c_breve(x,newx,zero_order_info_breve());
00373   num_c_evals_++;
00374 }
00375 
00376 void NLP::calc_h_breve(const Vector& x, bool newx) const
00377 {
00378 #ifdef TEUCHOS_DEBUG
00379   TEUCHOS_TEST_FOR_EXCEPTION( this->ns() == 0, std::logic_error, "" );
00380 #endif
00381   StandardCompositionRelationshipsPack::assert_role_name_set(first_order_info_breve_.h, "NLP::calc_h_breve()", name_h_breve);
00382   imp_calc_c_breve(x,newx,zero_order_info_breve());
00383   num_c_evals_++;
00384 }
00385 
00386 // protected
00387 
00388 void NLP::imp_calc_c_breve(
00389   const Vector           &x
00390   ,bool                  newx
00391   ,const ZeroOrderInfo   &zero_order_info_breve
00392   ) const
00393 {
00394   imp_calc_c(x,newx,zero_order_info_breve);
00395 }
00396 
00397 void NLP::imp_calc_h_breve(
00398   const Vector           &x
00399   ,bool                  newx
00400   ,const ZeroOrderInfo   &zero_order_info_breve
00401   ) const
00402 {
00403   TEUCHOS_TEST_FOR_EXCEPTION(
00404     true, std::logic_error
00405     ,"NLP::hl_breve(): Error, this method must be overridden if space_h_breve is defined" );
00406 }
00407 
00408 } // namespace NLPInterfacePack
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends