|
IterationPack: General framework for building iterative algorithms
Version of the Day
|
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 #ifndef ALGORITHM_STATE_H 00043 #define ALGORITHM_STATE_H 00044 00045 #include <limits> 00046 #include <vector> 00047 #include <map> 00048 #include <string> 00049 #include <sstream> 00050 #include <iosfwd> 00051 00052 #include "IterationPack_IterQuantity.hpp" 00053 #include "Teuchos_RCP.hpp" 00054 00055 namespace IterationPack { 00056 00093 class AlgorithmState { 00094 public: 00095 00098 00100 typedef size_t iq_id_type; 00102 typedef Teuchos::RCP<IterQuantity> IQ_ptr; 00104 enum { DOES_NOT_EXIST = INT_MAX }; // should not ever be this many insertions. 00105 00107 class DoesNotExist : public std::logic_error 00108 {public: DoesNotExist(const std::string& what_arg) : std::logic_error(what_arg) {}}; 00109 00111 class AlreadyExists : public std::logic_error 00112 {public: AlreadyExists(const std::string& what_arg) : std::logic_error(what_arg) {}}; 00113 00115 00117 virtual ~AlgorithmState() {} 00118 00121 00126 explicit AlgorithmState(size_t reserve_size = 0); 00127 00129 00132 00134 void k(int k); 00136 int k() const; 00138 int incr_k(); 00139 00141 00144 00146 virtual size_t num_iter_quant() const; 00147 00161 virtual iq_id_type set_iter_quant(const std::string& iq_name, const IQ_ptr& iq); 00162 00175 virtual void erase_iter_quant(const std::string& iq_name); 00176 00184 virtual iq_id_type get_iter_quant_id(const std::string& iq_name) const; 00185 00196 virtual IQ_ptr& get_iter_quant(iq_id_type iq_id); 00197 00199 virtual const IQ_ptr& get_iter_quant(iq_id_type iq_id) const; 00200 00202 00205 00214 virtual IterQuantity& iter_quant(const std::string& iq_name ); 00216 virtual const IterQuantity& iter_quant(const std::string& iq_name ) const; 00224 virtual IterQuantity& iter_quant(iq_id_type iq_id); 00226 virtual const IterQuantity& iter_quant(iq_id_type iq_id) const; 00227 00229 00232 00236 virtual void next_iteration(bool incr_k = true); 00237 00239 00242 00251 virtual void dump_iter_quant(std::ostream& out) const; 00252 00254 00255 private: 00256 // /////////////////////////////////////////////////////////// 00257 // Private types 00258 00259 typedef std::vector<IQ_ptr> iq_t; 00260 typedef std::map<std::string,iq_id_type> iq_name_to_id_t; 00261 00262 // /////////////////////////////////////////////////////////// 00263 // Private data members 00264 00265 int k_; // Iteration counter. 00266 00267 iq_t iq_; 00268 // Array of RCP objects that point to set iteration quantities. 00269 // The index into this array is the iq_id for an IQ object. This array 00270 // is filled sequantially from the beginning using push_back(...). 00271 // When erase_iter_quant(...) is called the iq_[iq_id] is set to null which 00272 // reduces the reference count of the IQ object (possible deleing it if 00273 // there are no other references). Then if the user tries to access and 00274 // IQ object with this abandonded iq_id, the dereferencing operator for 00275 // RCP<...> will throw an exception. 00276 #ifdef DOXYGEN_COMPILE 00277 IterQuantity *iteration_quantities; 00278 #endif 00279 00280 iq_name_to_id_t iq_name_to_id_; 00281 // Mapping of an IQ name to its id. 00282 00283 // /////////////////////////////////////////////////////////// 00284 // Private member functions 00285 00287 iq_name_to_id_t::iterator find_and_assert(const std::string& iq_name); 00289 iq_name_to_id_t::const_iterator find_and_assert(const std::string& iq_name) const; 00290 00291 }; // end class AlgorithmState 00292 00293 // ///////////////////////////////////////////////////////////////////////////////// 00294 // Inline member definitions for AlgorithmState 00295 00296 inline 00297 AlgorithmState::AlgorithmState(size_t reserve_size) 00298 : k_(0) 00299 { iq_.reserve(reserve_size); } 00300 00301 // iteration counter 00302 00303 inline 00304 void AlgorithmState::k(int k) 00305 { k_ = k; } 00306 00307 inline 00308 int AlgorithmState::k() const 00309 { return k_; } 00310 00311 inline 00312 int AlgorithmState::incr_k() 00313 { return ++k_; } 00314 00315 // 00316 00317 inline 00318 size_t AlgorithmState::num_iter_quant() const { 00319 return iq_name_to_id_.size(); 00320 } 00321 00322 inline 00323 AlgorithmState::iq_id_type AlgorithmState::get_iter_quant_id( 00324 const std::string& iq_name) const 00325 { 00326 const iq_name_to_id_t::const_iterator itr = iq_name_to_id_.find(iq_name); 00327 return itr == iq_name_to_id_.end() ? DOES_NOT_EXIST : (*itr).second; 00328 } 00329 00330 inline 00331 AlgorithmState::IQ_ptr& AlgorithmState::get_iter_quant(iq_id_type iq_id) { 00332 return iq_.at(iq_id); 00333 } 00334 00335 inline 00336 const AlgorithmState::IQ_ptr& AlgorithmState::get_iter_quant( 00337 iq_id_type iq_id) const 00338 { 00339 return iq_.at(iq_id); 00340 } 00341 00342 inline 00343 IterQuantity& AlgorithmState::iter_quant(const std::string& iq_name ) { 00344 return *iq_[(*find_and_assert(iq_name)).second]; 00345 } 00346 00347 inline 00348 const IterQuantity& AlgorithmState::iter_quant(const std::string& iq_name ) const { 00349 return *iq_[(*find_and_assert(iq_name)).second]; 00350 } 00351 00352 } // end namespace IterationPack 00353 00354 #endif // ALGORITHM_STATE_H
1.7.6.1