ConstrainedOptPack_QPInitFixedFreeStd.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 <assert.h>
00030 
00031 #include <sstream>
00032 
00033 #include "ConstrainedOptPack_QPInitFixedFreeStd.hpp"
00034 #include "ConstrainedOptPack_initialize_Q_R_Q_X.hpp"
00035 
00036 namespace ConstrainedOptPack {
00037 namespace QPSchurPack {
00038 
00039 QPInitFixedFreeStd::QPInitFixedFreeStd()
00040   :n_(0)
00041   ,n_R_(0)
00042   ,m_(0)
00043   ,G_(NULL)
00044   ,A_(NULL)
00045   ,Ko_(NULL)
00046   ,constraints_(NULL)
00047 {}
00048 
00049 void QPInitFixedFreeStd::initialize(
00050   const DVectorSlice                   &g
00051   ,const MatrixSymOp              &G
00052   ,const MatrixOp                 *A
00053   ,size_type                          n_R
00054   ,const size_type                    i_x_free[]
00055   ,const size_type                    i_x_fixed[]
00056   ,const EBounds                      bnd_fixed[]
00057   ,const DVectorSlice                  &b_X
00058   ,const MatrixSymOpNonsing    &Ko
00059   ,const DVectorSlice                  &fo
00060   ,Constraints                        *constraints
00061   ,std::ostream                       *out
00062   ,bool                               test_setup
00063   ,value_type                         warning_tol
00064   ,value_type                         error_tol
00065   ,bool                               print_all_warnings
00066   )
00067 {
00068   namespace GPMSTP = AbstractLinAlgPack::GenPermMatrixSliceIteratorPack;
00069 
00070   if(!constraints)
00071     throw std::invalid_argument( "QPInitFixedFreeStd::initialize(...) : Error, "
00072       "constraints == NULL is not allowed." );
00073 
00074   // Validate the sizes of the input arguments
00075   const size_type
00076     n = constraints->n(),
00077     n_X = n - n_R;
00078   if( n_R > n )
00079     throw std::invalid_argument( "QPInitFixedFreeStd::initialize(...) : Error, "
00080       "n_R > constraints->n() is not allowed." );
00081   if(g.dim() !=n)
00082     throw std::invalid_argument( "QPInitFixedFreeStd::initialize(...) : Error, "
00083       "g.dim() != constraints->n()." );
00084   if(G.rows() != n || G.cols() !=  n)
00085     throw std::invalid_argument( "QPInitFixedFreeStd::initialize(...) : Error, "
00086       "G.rows() != constraints->n() or G.cols() !=  constraints->n()." );
00087   size_type
00088     m = 0;
00089   if(A) {
00090     m = A->cols();
00091     if( A->rows() != n )
00092       throw std::invalid_argument( "QPInitFixedFreeStd::initialize(...) : Error, "
00093         "A->rows() != constraints->n()." );
00094   }
00095   if(b_X.dim() != n_X)
00096     throw std::invalid_argument( "QPInitFixedFreeStd::initialize(...) : Error, "
00097       "b_X.dim() != constraints->n() - n_R." );
00098   if(Ko.rows() != n_R+m || Ko.cols() !=  n_R+m)
00099     throw std::invalid_argument( "QPInitFixedFreeStd::initialize(...) : Error, "
00100       "Ko.rows() != n_R+A->cols() or Ko.cols() !=  n_R+A->cols()." );
00101   if(fo.dim() != n_R+m)
00102     throw std::invalid_argument( "QPInitFixedFreeStd::initialize(...) : Error, "
00103       "fo.dim() != n_R+A->cols()." );
00104 
00105   // Setup x_init, l_x_X_map, i_x_X_map
00106 
00107   const int NOT_SET_YET = -9999; // Can't be FREE, LOWER, UPPER, or EQUALITY
00108   if(test_setup)
00109     x_init_.assign( n, (EBounds)NOT_SET_YET );
00110   else
00111     x_init_.resize(n);
00112   l_x_X_map_.assign(n,0);
00113   i_x_X_map_.assign(n_X,0);
00114 
00115   // Set free portion of x_init
00116   if( i_x_free == NULL ) {
00117     for( size_type i = 0; i < n_R; ++i )
00118       x_init_[i] = FREE;
00119   }
00120   else {
00121     if(test_setup) {
00122       for( const size_type *i_x_R = i_x_free; i_x_R != i_x_free + n_R; ++i_x_R ) {
00123         if( *i_x_R < 1 || *i_x_R > n ) {
00124           std::ostringstream omsg;
00125           omsg
00126             << "QPInitFixedFreeStd::initialize(...) : Error, "
00127             << "i_x_free[" << i_x_R-i_x_free << "] = "
00128             << (*i_x_R) << " is out of bounds";
00129           throw std::invalid_argument( omsg.str() );
00130         }
00131         if( x_init_(*i_x_R) != NOT_SET_YET ) {
00132           std::ostringstream omsg;
00133           omsg
00134             << "QPInitFixedFreeStd::initialize(...) : Error, "
00135             << "Duplicate entries for i_x_free[i] = "
00136             << (*i_x_R);
00137           throw std::invalid_argument( omsg.str() );
00138         }
00139         x_init_(*i_x_R) = FREE;
00140       }
00141     }
00142     else {
00143       for( const size_type *i_x_R = i_x_free; i_x_R != i_x_free + n_R; ++i_x_R ) {
00144         x_init_(*i_x_R) = FREE;
00145       }
00146     }
00147   }
00148 
00149   // Setup the fixed portion of x_init and l_x_X_map and i_x_X_map
00150   {
00151     const size_type
00152       *i_x_X = i_x_fixed;
00153     const EBounds
00154       *bnd = bnd_fixed;
00155     i_x_X_map_t::iterator
00156       i_x_X_map_itr = i_x_X_map_.begin();
00157     if(test_setup) {
00158       for( size_type l = 1; l <= n_X; ++l, ++i_x_X, ++bnd, ++i_x_X_map_itr ) {
00159         if( *i_x_X < 1 || *i_x_X > n ) {
00160           std::ostringstream omsg;
00161           omsg
00162             << "QPInitFixedFreeStd::initialize(...) : Error, "
00163             << "i_x_fixed[" << i_x_X-i_x_fixed << "] = "
00164             << (*i_x_X) << " is out of bounds";
00165           throw std::invalid_argument( omsg.str() );
00166         }
00167         if( *bnd == FREE ) {
00168           std::ostringstream omsg;
00169           omsg
00170             << "QPInitFixedFreeStd::initialize(...) : Error, "
00171             << "bnd_fixed[" << l-1 << "] can not equal FREE";
00172           throw std::invalid_argument( omsg.str() );
00173         }
00174         if( x_init_(*i_x_X) != NOT_SET_YET ) {
00175           std::ostringstream omsg;
00176           omsg
00177             << "QPInitFixedFreeStd::initialize(...) : Error, "
00178             << "Duplicate entries for i_x_fixed[i] = "
00179             << (*i_x_X);
00180           throw std::invalid_argument( omsg.str() );
00181         }
00182         x_init_(*i_x_X) = *bnd;
00183         l_x_X_map_(*i_x_X) = l;
00184         *i_x_X_map_itr = *i_x_X;
00185       }
00186       // Check that x_init was filled up properly
00187       for( size_type i = 1; i <= n; ++i ) {
00188         if( x_init_(i) == NOT_SET_YET ) {
00189           std::ostringstream omsg;
00190           omsg
00191             << "QPInitFixedFreeStd::initialize(...) : Error, "
00192             << "x_init(" << i << ") has not been set by"
00193                " i_x_free[] or i_x_fixed[].";
00194           throw std::invalid_argument( omsg.str() );
00195         }
00196       }
00197     }
00198     else {
00199       for( size_type l = 1; l <= n_X; ++l, ++i_x_X, ++bnd, ++i_x_X_map_itr ) {
00200         x_init_(*i_x_X) = *bnd;
00201         l_x_X_map_(*i_x_X) = l;
00202         *i_x_X_map_itr = *i_x_X;
00203       }
00204     }
00205   }
00206 
00207   // Setup Q_R and Q_X
00208   Q_R_row_i_.resize( i_x_free ? n_R : 0 );
00209   Q_R_col_j_.resize( i_x_free ? n_R : 0 );
00210   Q_X_row_i_.resize(n_X);
00211   Q_X_col_j_.resize(n_X);
00212   initialize_Q_R_Q_X(
00213     n_R,n_X,i_x_free,i_x_fixed,test_setup
00214     ,n_R && i_x_free ? &Q_R_row_i_[0] : NULL
00215     ,n_R && i_x_free ? &Q_R_col_j_[0] : NULL
00216     ,&Q_R_
00217     ,n_X ? &Q_X_row_i_[0] : NULL
00218     ,n_X ? &Q_X_col_j_[0] : NULL
00219     ,&Q_X_
00220   );
00221 
00222   // Setup other arguments
00223   n_        = n;
00224   n_R_      = n_R;
00225   m_        = m;
00226   g_.bind(const_cast<DVectorSlice&>(g));
00227   G_        = &G;
00228   A_        = A;
00229   b_X_.bind(const_cast<DVectorSlice&>(b_X));
00230   Ko_       = &Ko;
00231   fo_.bind(const_cast<DVectorSlice&>(fo));
00232   constraints_  = constraints;
00233 }
00234 
00235 // Overridden from QP 
00236 
00237 size_type QPInitFixedFreeStd::n() const
00238 {
00239   assert_initialized();
00240   return n_;
00241 }
00242 
00243 size_type QPInitFixedFreeStd::m() const
00244 {
00245   assert_initialized();
00246   return m_;
00247 }
00248 
00249 const DVectorSlice QPInitFixedFreeStd::g() const
00250 {
00251   assert_initialized();
00252   return g_;
00253 }
00254 
00255 const MatrixSymOp& QPInitFixedFreeStd::G() const
00256 {
00257   assert_initialized();
00258   return *G_;
00259 }
00260 
00261 const MatrixOp& QPInitFixedFreeStd::A() const
00262 {
00263   assert(A_); // ToDo: make this throw an exception
00264   return *A_;
00265 }
00266 
00267 size_type QPInitFixedFreeStd::n_R() const
00268 {
00269   assert_initialized();
00270   return n_R_;
00271 }
00272 
00273 const QP::x_init_t& QPInitFixedFreeStd::x_init() const
00274 {
00275   assert_initialized();
00276   return x_init_;
00277 }
00278 
00279 const QP::l_x_X_map_t& QPInitFixedFreeStd::l_x_X_map() const
00280 {
00281   assert_initialized();
00282   return l_x_X_map_;
00283 }
00284 
00285 const QP::i_x_X_map_t& QPInitFixedFreeStd::i_x_X_map() const
00286 {
00287   assert_initialized();
00288   return i_x_X_map_;
00289 }
00290 
00291 const DVectorSlice QPInitFixedFreeStd::b_X() const
00292 {
00293   assert_initialized();
00294   return b_X_;
00295 }
00296 
00297 const GenPermMatrixSlice& QPInitFixedFreeStd::Q_R() const
00298 {
00299   assert_initialized();
00300   return Q_R_;
00301 }
00302 
00303 const GenPermMatrixSlice& QPInitFixedFreeStd::Q_X() const
00304 {
00305   assert_initialized();
00306   return Q_X_;
00307 }
00308 
00309 const MatrixSymOpNonsing& QPInitFixedFreeStd::Ko() const
00310 {
00311   assert_initialized();
00312   return *Ko_;
00313 }
00314 
00315 const DVectorSlice QPInitFixedFreeStd::fo() const
00316 {
00317   assert_initialized();
00318   return fo_;
00319 }
00320 
00321 Constraints& QPInitFixedFreeStd::constraints()
00322 {
00323   assert_initialized();
00324   return *constraints_;
00325 }
00326 
00327 const Constraints& QPInitFixedFreeStd::constraints() const
00328 {
00329   assert_initialized();
00330   return *constraints_;
00331 }
00332 
00333 // private member functions
00334 
00335 void QPInitFixedFreeStd::assert_initialized() const
00336 {
00337   if( !n_ )
00338     throw std::logic_error( "QPInitFixedFreeStd::assert_initialized(), Error "
00339       "object not initialized\n" );
00340 }
00341 
00342 } // end namespace QPSchurPack
00343 } // end namespace ConstrainedOptPack 

Generated on Thu Sep 18 12:35:14 2008 for MOOCHO (Single Doxygen Collection) by doxygen 1.3.9.1