NLPInterfacePack_NLPWBCounterExample.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 // 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 <math.h>
00030 
00031 #include "NLPInterfacePack_NLPWBCounterExample.hpp"
00032 #include "DenseLinAlgPack_PermVecMat.hpp"
00033 #include "DenseLinAlgPack_LinAlgOpPack.hpp"
00034 #include "Teuchos_TestForException.hpp"
00035 
00036 namespace NLPInterfacePack {
00037 
00038 // Constructors / initializers
00039 
00040 NLPWBCounterExample::NLPWBCounterExample(
00041   value_type xinit[3], value_type a, value_type b, bool nlp_selects_basis, bool linear_obj
00042   )
00043   :is_initialized_(false),nlp_selects_basis_(nlp_selects_basis),basis_selection_was_given_(false)
00044   ,linear_obj_(linear_obj),n_orig_(3),m_orig_(2),Gc_orig_nz_(4),a_(a),b_(b)
00045 {
00046 #ifdef TEUCHOS_DEBUG  
00047   const char msg_err_head[] = "NLPWBCounterExample::NLPWBCounterExample(...) : Error";
00048   TEST_FOR_EXCEPTION( !(b >= 0),        std::invalid_argument, msg_err_head<<"!" );
00049   TEST_FOR_EXCEPTION( !(a + b*b != 0),  std::invalid_argument, msg_err_head<<"!" );
00050   TEST_FOR_EXCEPTION( !(xinit[1] >= 0), std::invalid_argument, msg_err_head<<"!" );
00051   TEST_FOR_EXCEPTION( !(xinit[2] >= 0), std::invalid_argument, msg_err_head<<"!" );
00052 #endif
00053   // Set the inital guess and the variable bounds
00054   xinit_orig_.resize(n_orig_); xl_orig_.resize(n_orig_); xu_orig_.resize(n_orig_);
00055   const value_type inf = NLP::infinite_bound();
00056   xinit_orig_(1) = xinit[0]; xl_orig_(1) = -inf; xu_orig_(1) = +inf;
00057   xinit_orig_(2) = xinit[1]; xl_orig_(2) =  0.0; xu_orig_(2) = +inf;
00058   xinit_orig_(3) = xinit[2]; xl_orig_(3) =  0.0; xu_orig_(3) = +inf;
00059 }
00060 
00061 // Overridden public members from NLP
00062 
00063 void NLPWBCounterExample::initialize(bool test_setup)
00064 {
00065   // Nothing to initialize locally so just pass it on
00066   NLPSerialPreprocessExplJac::initialize(test_setup);
00067   is_initialized_ = true;
00068 }
00069 
00070 bool NLPWBCounterExample::is_initialized() const
00071 {
00072   return is_initialized_;
00073 }
00074 
00075 value_type NLPWBCounterExample::max_var_bounds_viol() const
00076 {
00077   return +1e+20; // Functions defined everywhere!
00078 }
00079 
00080 // Overridden protected methods from NLPSerialPreprocess
00081 
00082 bool NLPWBCounterExample::imp_nlp_has_changed() const
00083 {
00084   return !is_initialized_;
00085 }
00086 
00087 size_type NLPWBCounterExample::imp_n_orig() const
00088 {
00089   return n_orig_;
00090 }
00091 
00092 size_type NLPWBCounterExample::imp_m_orig() const
00093 {
00094   return m_orig_;
00095 }
00096 
00097 size_type NLPWBCounterExample::imp_mI_orig() const
00098 {
00099   return 0;
00100 }
00101 
00102 const DVectorSlice NLPWBCounterExample::imp_xinit_orig() const
00103 {
00104   return xinit_orig_();
00105 }
00106 
00107 bool NLPWBCounterExample::imp_has_var_bounds() const
00108 {
00109   return true;
00110 }
00111 
00112 const DVectorSlice NLPWBCounterExample::imp_xl_orig() const
00113 {
00114   return xl_orig_();
00115 }
00116 
00117 const DVectorSlice NLPWBCounterExample::imp_xu_orig() const
00118 {
00119   return xu_orig_();
00120 }
00121 
00122 const DVectorSlice NLPWBCounterExample::imp_hl_orig() const
00123 {
00124   TEST_FOR_EXCEPT(true);  // Should never be called
00125    return xinit_orig_();
00126 }
00127 
00128 const DVectorSlice NLPWBCounterExample::imp_hu_orig() const
00129 {
00130   TEST_FOR_EXCEPT(true);  // Should never be called
00131    return xinit_orig_();
00132 }
00133 
00134 void NLPWBCounterExample::imp_calc_f_orig(
00135   const DVectorSlice &x_full, bool newx, const ZeroOrderInfoSerial &zero_order_info ) const
00136 {
00137   DVectorSlice x = x_full(1,n_orig_);
00138   *zero_order_info.f = ( linear_obj_ ? x(1) : 0.5*x(1)*x(1) );
00139 }
00140 
00141 void NLPWBCounterExample::imp_calc_c_orig(
00142   const DVectorSlice &x_full, bool newx, const ZeroOrderInfoSerial &zero_order_info ) const
00143 {
00144   DVectorSlice x = x_full(1,n_orig_);
00145   DVector &c = *zero_order_info.c;
00146   c(1) = x(1)*x(1) - x(2) + a_;
00147   c(2) = x(1)      - x(3) - b_;
00148 }
00149 
00150 void NLPWBCounterExample::imp_calc_h_orig(
00151   const DVectorSlice &x_full, bool newx, const ZeroOrderInfoSerial &zero_order_info ) const
00152 {
00153   TEST_FOR_EXCEPT(true); // Should never be called
00154 }
00155 
00156 void NLPWBCounterExample::imp_calc_Gf_orig(
00157   const DVectorSlice &x_full, bool newx, const ObjGradInfoSerial &obj_grad_info ) const
00158 {
00159   DVectorSlice x = x_full(1,n_orig_); 
00160   DVector &Gf = *obj_grad_info.Gf;
00161   Gf(1) = (linear_obj_ ? 1.0 : x(1) ); Gf(2) = 0.0; Gf(3) = 0.0;
00162 }
00163 
00164 bool NLPWBCounterExample::imp_get_next_basis(
00165   IVector      *var_perm_full
00166   ,IVector     *equ_perm_full
00167   ,size_type   *rank_full
00168   ,size_type   *rank
00169   )
00170 {
00171 #ifdef TEUCHOS_DEBUG
00172   TEST_FOR_EXCEPT( !( var_perm_full) );
00173   TEST_FOR_EXCEPT( !( equ_perm_full ) );
00174   TEST_FOR_EXCEPT( !( rank_full ) );
00175   TEST_FOR_EXCEPT( !( rank  ) );
00176 #endif
00177   if(basis_selection_was_given_) return false; // Already gave this basis selection
00178   // Select x(2) ans x(3) as the basic variables (sorted!)
00179   var_perm_full->resize(n_orig_);
00180   (*var_perm_full)(1) = 2;  // The basis variables
00181   (*var_perm_full)(2) = 3;  // ""
00182   (*var_perm_full)(3) = 1;  // The nonbasis variable
00183   equ_perm_full->resize(m_orig_);
00184   DenseLinAlgPack::identity_perm( equ_perm_full ); // Gc_orig is full rank
00185   *rank_full                 = m_orig_;
00186   *rank                      = m_orig_;
00187   basis_selection_was_given_ = true;
00188   return true;
00189 }
00190 
00191 void NLPWBCounterExample::imp_report_orig_final_solution(
00192   const DVectorSlice      &x_orig
00193   ,const DVectorSlice     *lambda_orig
00194   ,const DVectorSlice     *lambdaI_orig
00195   ,const DVectorSlice     *nu_orig
00196   ,bool                   is_optimal
00197   ) const
00198 {
00199   // ToDo: Do something with the final soltuion?
00200 }
00201 
00202 bool NLPWBCounterExample::nlp_selects_basis() const
00203 {
00204   return nlp_selects_basis_;
00205 }
00206 
00207 // Overridden protected methods from NLPSerialPreprocessExplJac
00208 
00209 size_type NLPWBCounterExample::imp_Gc_nz_orig() const
00210 {
00211   return Gc_orig_nz_;
00212 }
00213 
00214 size_type NLPWBCounterExample::imp_Gh_nz_orig() const
00215 {
00216   return 0;
00217 }
00218 
00219 void NLPWBCounterExample::imp_calc_Gc_orig(
00220   const DVectorSlice& x_full, bool newx, const FirstOrderExplInfo& first_order_expl_info ) const
00221 {
00222   DVectorSlice x = x_full(1,n_orig_); 
00223   // Get references/pointers to data for Gc to be computed/updated.
00224   index_type  &Gc_nz = *first_order_expl_info.Gc_nz;
00225   value_type  *Gc_v = &(*first_order_expl_info.Gc_val)[0];
00226   index_type  *Gc_i = ( first_order_expl_info.Gc_ivect ? &(*first_order_expl_info.Gc_ivect)[0] : NULL );
00227   index_type  *Gc_j = ( first_order_expl_info.Gc_jvect ? &(*first_order_expl_info.Gc_jvect)[0] : NULL );
00228   // Set up the nonzero structure of Gc_orig (sorted by constraint and then by variable)
00229   if( Gc_i ) {
00230     Gc_j[0] = 1; Gc_i[0] = 1; // d(c(1))/d(x(1))
00231     Gc_j[1] = 1; Gc_i[1] = 2; // d(c(1))/d(x(2))
00232     Gc_j[2] = 2; Gc_i[2] = 1; // d(c(2))/d(x(1))
00233     Gc_j[3] = 2; Gc_i[3] = 3; // d(c(2))/d(x(3))
00234   }
00235   // Fill in the nonzero values of Gc_orig (must have the same order as structure!)
00236   Gc_v[0] = 2*x(1); // d(c(1))/d(x(1))
00237   Gc_v[1] =   -1.0; // d(c(1))/d(x(2))
00238   Gc_v[2] =   +1.0; // d(c(2))/d(x(1))
00239   Gc_v[3] =   -1.0; // d(c(2))/d(x(3))
00240   // Set the actual number of nonzeros
00241   Gc_nz = Gc_orig_nz_;
00242 }
00243 
00244 void NLPWBCounterExample::imp_calc_Gh_orig(
00245   const DVectorSlice& x_full, bool newx, const FirstOrderExplInfo& first_order_expl_info ) const
00246 {
00247   TEST_FOR_EXCEPT(true); // Should never be called
00248 }
00249 
00250 } // end namespace NLPInterfacePack

Generated on Tue Jul 13 09:36:22 2010 for MOOCHO by  doxygen 1.4.7