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