MOOCHO (Single Doxygen Collection) Version of the Day
NLPInterfacePack_NLP.cpp
Go to the documentation of this file.
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 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #include <limits>
00030 
00031 #include "NLPInterfacePack_NLP.hpp"
00032 #include "AbstractLinAlgPack_VectorMutable.hpp"
00033 #include "AbstractLinAlgPack_VectorSpace.hpp"
00034 #include "Teuchos_TestForException.hpp"
00035 
00036 namespace {
00037 const char name_f[] = "f";
00038 const char name_c[] = "c";
00039 const char name_c_breve[] = "c_breve";
00040 const char name_h_breve[] = "h_breve";
00041 NLPInterfacePack::NLP::options_ptr_t  null_options = Teuchos::null;
00042 } // end namespace
00043 
00044 namespace NLPInterfacePack {
00045 
00046 // static
00047 
00048 value_type NLP::infinite_bound()
00049 {
00050 //  return std::numeric_limits<value_type>::max();
00051   return 1e+50;
00052 }
00053 
00054 // constructors
00055 
00056 NLP::NLP()
00057 {}
00058 
00059 // destructor
00060 
00061 NLP::~NLP()
00062 {}
00063 
00064 void NLP::set_options( const options_ptr_t& options )
00065 {}
00066 
00067 const NLP::options_ptr_t&
00068 NLP::get_options() const
00069 {
00070   return null_options;
00071 }
00072 
00073 void NLP::initialize(bool test_setup)
00074 {
00075   num_f_evals_ = num_c_evals_ = 0;
00076 }
00077 
00078 // dimensionality
00079 
00080 size_type
00081 NLP::n() const
00082 {
00083   return this->space_x()->dim();
00084 }
00085 
00086 size_type 
00087 NLP::m() const
00088 {
00089   VectorSpace::space_ptr_t spc = this->space_c();
00090   return spc.get() ? spc->dim() : 0;
00091 }
00092 
00093 // initial guess
00094 
00095 void NLP::get_init_lagrange_mult(
00096   VectorMutable*   lambda
00097   ,VectorMutable*  nu
00098   ) const
00099 {
00100 #ifdef TEUCHOS_DEBUG
00101   TEST_FOR_EXCEPTION( lambda  && this->m()  == 0,            std::logic_error, "" );
00102   TEST_FOR_EXCEPTION( nu      && this->num_bounded_x() == 0, std::logic_error, "" );
00103 #endif
00104   if(lambda) {
00105 #ifdef TEUCHOS_DEBUG
00106     TEST_FOR_EXCEPTION( !this->space_c()->is_compatible(lambda->space()), VectorSpace::IncompatibleVectorSpaces, "" );
00107 #endif
00108     *lambda = 0.0;
00109   }
00110   if(nu) {
00111 #ifdef TEUCHOS_DEBUG
00112     TEST_FOR_EXCEPTION( !this->space_x()->is_compatible(nu->space()), VectorSpace::IncompatibleVectorSpaces, "" );
00113 #endif
00114     *nu = 0.0;
00115   }
00116 }
00117 
00118 // <<std comp>> members for f
00119 
00120 void NLP::set_f(value_type* f)
00121 {
00122   first_order_info_.f = f;
00123 }
00124 
00125 value_type* NLP::get_f()
00126 {
00127   return StandardCompositionRelationshipsPack::get_role_name(first_order_info_.f, false, name_f);
00128 }
00129 
00130 value_type& NLP::f()
00131 {
00132   return StandardCompositionRelationshipsPack::role_name(first_order_info_.f, false, name_f);
00133 }
00134 
00135 const value_type& NLP::f() const
00136 {
00137   return StandardCompositionRelationshipsPack::role_name(first_order_info_.f, false, name_f);
00138 }
00139 
00140 // <<std comp>> members for c
00141 
00142 void NLP::set_c(VectorMutable* c)
00143 {
00144 #ifdef TEUCHOS_DEBUG
00145   TEST_FOR_EXCEPTION( this->m() == 0, std::logic_error, "" );
00146   TEST_FOR_EXCEPTION( c && !this->space_c()->is_compatible(c->space()), VectorSpace::IncompatibleVectorSpaces, "" );
00147 #endif
00148   first_order_info_.c = c;
00149 }
00150 
00151 VectorMutable* NLP::get_c()
00152 {
00153 #ifdef TEUCHOS_DEBUG
00154   TEST_FOR_EXCEPTION( this->m() == 0, std::logic_error, "" );
00155 #endif
00156   return StandardCompositionRelationshipsPack::get_role_name(first_order_info_.c, false, name_c);
00157 }
00158 
00159 VectorMutable& NLP::c()
00160 {
00161 #ifdef TEUCHOS_DEBUG
00162   TEST_FOR_EXCEPTION( this->m() == 0, std::logic_error, "" );
00163 #endif
00164   return StandardCompositionRelationshipsPack::role_name(first_order_info_.c, false, name_c);
00165 }
00166 
00167 const Vector& NLP::c() const
00168 {
00169 #ifdef TEUCHOS_DEBUG
00170   TEST_FOR_EXCEPTION( this->m() == 0, std::logic_error, "" );
00171 #endif
00172   return StandardCompositionRelationshipsPack::role_name(first_order_info_.c, false, name_c);
00173 }
00174 
00175 void NLP::unset_quantities()
00176 {
00177   set_f(NULL);
00178   if(m()) set_c(NULL);
00179   if(m()-ns()) set_c_breve(NULL);
00180   if(m()-ns()) set_h_breve(NULL);
00181 }
00182 
00183 // calculations
00184 
00185 void NLP::calc_f(const Vector& x, bool newx) const
00186 {
00187   StandardCompositionRelationshipsPack::assert_role_name_set(first_order_info_.f, "NLP::calc_f()", name_f);
00188   imp_calc_f(x,newx,zero_order_info());
00189   num_f_evals_++;
00190 }
00191 
00192 void NLP::calc_c(const Vector& x, bool newx) const
00193 {
00194 #ifdef TEUCHOS_DEBUG
00195   TEST_FOR_EXCEPTION( this->m() == 0, std::logic_error, "" );
00196 #endif
00197   StandardCompositionRelationshipsPack::assert_role_name_set(first_order_info_.c, "NLP::calc_c()", name_c);
00198   imp_calc_c(x,newx,zero_order_info());
00199   num_c_evals_++;
00200 }
00201 
00202 void NLP::report_final_solution(
00203   const Vector&    x
00204   ,const Vector*   lambda
00205   ,const Vector*   nu
00206   ,bool            optimal
00207   )
00208 {
00209   // The default behavior is just to ignore the solution!
00210 }
00211 
00212 size_type NLP::num_f_evals() const
00213 {
00214   return num_f_evals_;
00215 }
00216 
00217 size_type NLP::num_c_evals() const
00218 {
00219 #ifdef TEUCHOS_DEBUG
00220   TEST_FOR_EXCEPTION( this->m() == 0, std::logic_error, "" );
00221 #endif
00222   return num_c_evals_;
00223 }
00224 
00225 // General inequalities and slack variables
00226 
00227 size_type NLP::ns() const
00228 {
00229   vec_space_ptr_t space_h_breve = this->space_h_breve();
00230   return space_h_breve.get() ? space_h_breve->dim() : 0;
00231 }
00232 
00233 NLP::vec_space_ptr_t NLP::space_c_breve() const
00234 {
00235   return this->space_c();
00236 }
00237 
00238 NLP::vec_space_ptr_t NLP::space_h_breve() const
00239 {
00240   return Teuchos::null;
00241 }
00242 
00243 const Vector& NLP::hl_breve() const
00244 {
00245   TEST_FOR_EXCEPTION(
00246     true, std::logic_error
00247     ,"NLP::hl_breve(): Error, this method must be overridden if space_h_breve is defined" );
00248 
00249   //execution should never reach this point, but compilers expect a non-void
00250   //function to return something, so we'll create a dummy value to use in a
00251   //return statement.
00252   //(a better design would not require function bodies for unimplemented
00253   //functions like this...)
00254   Vector* dummy = NULL;
00255   return(*dummy);
00256 }
00257 
00258 const Vector& NLP::hu_breve() const
00259 {
00260   TEST_FOR_EXCEPTION(
00261     true, std::logic_error
00262     ,"NLP::hl_breve(): Error, this method must be overridden if space_h_breve is defined" );
00263 
00264   //execution should never reach this point, but compilers expect a non-void
00265   //function to return something, so we'll create a dummy value to use in a
00266   //return statement.
00267   //(a better design would not require function bodies for unimplemented
00268   //functions like this...)
00269   Vector* dummy = NULL;
00270   return(*dummy);
00271 }
00272 
00273 void NLP::set_c_breve(VectorMutable* c_breve)
00274 {
00275 #ifdef TEUCHOS_DEBUG
00276   TEST_FOR_EXCEPTION( this->m() - this->ns() == 0, std::logic_error, "" );
00277   TEST_FOR_EXCEPTION( c_breve && !this->space_c_breve()->is_compatible(c_breve->space()), VectorSpace::IncompatibleVectorSpaces, "" );
00278 #endif
00279   first_order_info_breve_.c = c_breve;
00280 }
00281 
00282 VectorMutable* NLP::get_c_breve()
00283 {
00284 #ifdef TEUCHOS_DEBUG
00285   TEST_FOR_EXCEPTION( this->m() - this->ns() == 0, std::logic_error, "" );
00286 #endif
00287   return first_order_info_breve_.c;
00288 }
00289 
00290 VectorMutable& NLP::c_breve()
00291 {
00292 #ifdef TEUCHOS_DEBUG
00293   TEST_FOR_EXCEPTION( this->m() - this->ns() == 0, std::logic_error, "" );
00294 #endif
00295   return StandardCompositionRelationshipsPack::role_name(first_order_info_breve_.c, false, name_c_breve);
00296 }
00297 
00298 const Vector& NLP::c_breve() const
00299 {
00300 #ifdef TEUCHOS_DEBUG
00301   TEST_FOR_EXCEPTION( this->m() - this->ns() == 0, std::logic_error, "" );
00302 #endif
00303   return StandardCompositionRelationshipsPack::role_name(first_order_info_breve_.c, false, name_c_breve);
00304 }
00305 
00306 void NLP::set_h_breve(VectorMutable* h_breve)
00307 {
00308 #ifdef TEUCHOS_DEBUG
00309   TEST_FOR_EXCEPTION( this->m() - this->ns() == 0, std::logic_error, "" );
00310   TEST_FOR_EXCEPTION( h_breve && !this->space_h_breve()->is_compatible(h_breve->space()), VectorSpace::IncompatibleVectorSpaces, "" );
00311 #endif
00312   first_order_info_breve_.c = h_breve;
00313 }
00314 
00315 VectorMutable* NLP::get_h_breve()
00316 {
00317 #ifdef TEUCHOS_DEBUG
00318   TEST_FOR_EXCEPTION( this->m() - this->ns() == 0, std::logic_error, "" );
00319 #endif
00320   return first_order_info_breve_.h;
00321 }
00322 
00323 VectorMutable& NLP::h_breve()
00324 {
00325 #ifdef TEUCHOS_DEBUG
00326   TEST_FOR_EXCEPTION( this->m() - this->ns() == 0, std::logic_error, "" );
00327 #endif
00328   return StandardCompositionRelationshipsPack::role_name(first_order_info_breve_.c, false, name_h_breve);
00329 }
00330 
00331 const Vector& NLP::h_breve() const
00332 {
00333 #ifdef TEUCHOS_DEBUG
00334   TEST_FOR_EXCEPTION( this->m() - this->ns() == 0, std::logic_error, "" );
00335 #endif
00336   return StandardCompositionRelationshipsPack::role_name(first_order_info_breve_.c, false, name_h_breve);
00337 }
00338 
00339 const Permutation& NLP::P_var() const
00340 {
00341   TEST_FOR_EXCEPT(true);
00342 //  if(!P_var_.get()) P_var_ = Teuchos::rcp(new PermutationSerial(this->space_x());
00343   return *P_var_;
00344 }
00345 
00346 const Permutation& NLP::P_equ() const
00347 {
00348   TEST_FOR_EXCEPT(true);
00349 //  if(!P_equ_.get()) P_equ = Teuchos::rcp(new PermutationSerial(this->space_c());
00350   return *P_equ_;
00351 }
00352 
00353 void NLP::calc_c_breve(const Vector& x, bool newx) const
00354 {
00355 #ifdef TEUCHOS_DEBUG
00356   TEST_FOR_EXCEPTION( this->m() == 0 || this->ns() > 0, std::logic_error, "" );
00357 #endif
00358   StandardCompositionRelationshipsPack::assert_role_name_set(first_order_info_breve_.c, "NLP::calc_c_breve()", name_c_breve);
00359   imp_calc_c_breve(x,newx,zero_order_info_breve());
00360   num_c_evals_++;
00361 }
00362 
00363 void NLP::calc_h_breve(const Vector& x, bool newx) const
00364 {
00365 #ifdef TEUCHOS_DEBUG
00366   TEST_FOR_EXCEPTION( this->ns() == 0, std::logic_error, "" );
00367 #endif
00368   StandardCompositionRelationshipsPack::assert_role_name_set(first_order_info_breve_.h, "NLP::calc_h_breve()", name_h_breve);
00369   imp_calc_c_breve(x,newx,zero_order_info_breve());
00370   num_c_evals_++;
00371 }
00372 
00373 // protected
00374 
00375 void NLP::imp_calc_c_breve(
00376   const Vector           &x
00377   ,bool                  newx
00378   ,const ZeroOrderInfo   &zero_order_info_breve
00379   ) const
00380 {
00381   imp_calc_c(x,newx,zero_order_info_breve);
00382 }
00383 
00384 void NLP::imp_calc_h_breve(
00385   const Vector           &x
00386   ,bool                  newx
00387   ,const ZeroOrderInfo   &zero_order_info_breve
00388   ) const
00389 {
00390   TEST_FOR_EXCEPTION(
00391     true, std::logic_error
00392     ,"NLP::hl_breve(): Error, this method must be overridden if space_h_breve is defined" );
00393 }
00394 
00395 } // namespace NLPInterfacePack
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines