IterationPack_TestAlgorithm.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 <ostream>
00030 #include <iomanip>
00031 
00032 #include "IterationPack_TestIterationPack.hpp"
00033 #include "IterationPack_AlgorithmStepTesting.hpp"
00034 #include "IterationPack_AlgorithmTrackTesting.hpp"
00035 #include "IterationPack_Algorithm.hpp"
00036 #include "IterationPack_AlgorithmState.hpp"
00037 
00038 namespace IterationPack {
00039 
00040 // Implement minor loop 1
00041 class MinorLoop1Step : public AlgorithmStepTesting {
00042 public:
00043 
00044   MinorLoop1Step() : called_(false)
00045   {}
00046 
00047   bool do_step(Algorithm& algo, poss_type step_poss, EDoStepType type
00048     , poss_type assoc_step_poss)
00049   {
00050     if(called_) {
00051       algo.terminate(false);
00052       return false;
00053     }
00054     AlgorithmStepTesting::do_step(algo,step_poss,type,assoc_step_poss);
00055     algo.track().journal_out() << "\n* Jump to \"Step_2\"\n";
00056     algo.do_step_next("Step_2");
00057     called_ = true;
00058     return false;
00059   }
00060 
00061 private:
00062   bool called_;
00063 };
00064 
00065 // Implement controled loop 1
00066 class ControledLoop1Step : public AlgorithmStepTesting {
00067 public:
00068 
00069   ControledLoop1Step() : called_(false)
00070   {}
00071 
00072   bool do_step(Algorithm& algo, poss_type step_poss, EDoStepType type
00073     , poss_type assoc_step_poss)
00074   {
00075     if(called_) {
00076       algo.terminate(false);
00077       return false;
00078     }
00079     AlgorithmStepTesting::do_step(algo,step_poss,type,assoc_step_poss);
00080     algo.track().journal_out() << "\n* Do step \"Step_2_p1\" then Jump to \"Step_1\"\n";
00081     algo.get_assoc_step(2,POST_STEP,1)->do_step(algo,2,DO_POST_STEP,1);
00082     algo.do_step_next("Step_1");
00083     called_ = true;
00084     return false;
00085   }
00086 
00087 private:
00088   bool called_;
00089 };
00090 
00091 // Runtime Config change
00092 class RuntimeConfigChangeStep : public AlgorithmStepTesting {
00093 public:
00094 
00095   RuntimeConfigChangeStep() : called_(false)
00096   {}
00097 
00098   bool do_step(Algorithm& algo, poss_type step_poss, EDoStepType type
00099     , poss_type assoc_step_poss)
00100   {
00101     AlgorithmStepTesting::do_step(algo,step_poss,type,assoc_step_poss);
00102     if(!called_) {
00103       algo.track().journal_out() << "\n* Remove step \"Step_1\" then put it back then print steps\n";
00104       algo.begin_config_update();
00105       Algorithm::step_ptr_t step = algo.get_step(1);
00106       algo.remove_step(1);
00107       algo.insert_step(1,"Step_1",step);
00108       algo.end_config_update();
00109       called_ = true;
00110     }
00111     else {
00112       algo.track().journal_out() << "\n* Remove step 1, step_name = "
00113         << algo.get_step_name(1) << " but don't put it back then print steps\n";
00114       algo.begin_config_update();
00115       algo.remove_step(1);
00116       algo.end_config_update();
00117       called_ = true;
00118     }
00119     algo.print_steps(algo.track().journal_out());
00120     return true;
00121   }
00122 
00123 private:
00124   bool called_;
00125 };
00126 
00127 } // end namespace IterationPack
00128 
00129 bool IterationPack::TestingPack::TestAlgorithm(std::ostream* out) {
00130 
00131   using std::endl;
00132   using std::setw;
00133   
00134   std::ostream& _out = *out;
00135   // ToDo: RAB: 7/1/99: Modify for optional output when out == 0;
00136 
00137   try {
00138 
00139   bool success = true;
00140 //  const int w = 15;
00141   _out << std::boolalpha;
00142     
00143   _out  << "\n\n*************************\n"
00144       << "*** Testing Algorithm ***\n"
00145       << "*************************\n";
00146 
00147   Algorithm algo;
00148   
00149   Algorithm::state_ptr_t      state   = Teuchos::rcp(new AlgorithmState);
00150   Algorithm::track_ptr_t      track   = Teuchos::rcp(new AlgorithmTrackTesting(Teuchos::rcp(&_out,false)));
00151 
00152   algo.set_state( state );
00153   algo.set_track( track );
00154 
00155   Algorithm::step_ptr_t     step    = Teuchos::rcp(new AlgorithmStepTesting);
00156   Algorithm::step_ptr_t     assoc_step  = Teuchos::rcp(new AlgorithmStepTesting);
00157 
00158   algo.insert_step( 1, "Step_1", step );
00159 
00160   algo.insert_step( 2, "Step_2", step );
00161 
00162   algo.insert_assoc_step( 2, PRE_STEP, 1, "Step_2_m1", assoc_step );
00163 
00164   algo.insert_assoc_step( 2, POST_STEP, 1, "Step_2_p1", assoc_step );
00165 
00166   algo.insert_step( 3, "Step_3", step );
00167 
00168   algo.insert_step( 4, "Step_4", step );
00169 
00170   algo.insert_assoc_step( 4, PRE_STEP, 1, "Step_4_m2", assoc_step );
00171 
00172   algo.insert_assoc_step( 4, PRE_STEP, 2, "Step_4_m1", assoc_step );
00173 
00174   _out  << "\n\n*** algo.print_algorithm(_out)\n\n";
00175   algo.print_algorithm(_out);
00176 
00177   // Test the major loop
00178 
00179   _out  << "\n\n*** Test the major loop for two iterations ***\n";
00180 
00181   _out  << "\nalgo.set_algo_timing(true); algo.max_iter(2); algo.do_algorithm();\n\n";
00182   algo.set_algo_timing(true);
00183   algo.max_iter(2);
00184   algo.do_algorithm();
00185 
00186   // Test Minor Loop 1
00187 
00188   _out  << "\n\n*** Test Minor Loop 1 ***\n";
00189 
00190   _out  << "\nalgo.remove_assoc_step( 4, PRE_STEP, 2 );\n";
00191   algo.remove_assoc_step( 4, PRE_STEP, 2 );
00192 
00193   _out  << "\nalgo.insert_assoc_step( 4, PRE_STEP, 2, \"Step_4_m1\", new MinorLoop1Step );\n";
00194   algo.insert_assoc_step( 4, PRE_STEP, 2, "Step_4_m1", Teuchos::rcp(new MinorLoop1Step) );
00195 
00196   _out  << "\nalgo.state().k(0);\n";
00197   algo.state().k(0);
00198 
00199   _out  << "\nalgo.max_iter(1);\n";
00200   algo.max_iter(1);
00201 
00202   _out  << "\n\nalgo.print_steps(_out)\n\n";
00203   algo.print_steps(_out);
00204 
00205   _out  << "\nalgo.do_algorithm();\n";
00206   algo.do_algorithm();
00207 
00208   // Test Controlled Loop 1
00209 
00210   _out  << "\n\n*** Test Controlled Loop 1 ***\n";
00211 
00212   _out  << "\nalgo.remove_assoc_step( 4, PRE_STEP, 2 );\n";
00213   algo.remove_assoc_step( 4, PRE_STEP, 2 );
00214 
00215   _out  << "\nalgo.insert_assoc_step( 4, PRE_STEP, 2, \"Step_4_m1\", assoc_step );\n";
00216   algo.insert_assoc_step( 4, PRE_STEP, 2, "Step_4_m1", assoc_step );
00217 
00218   _out  << "\nalgo.remove_assoc_step( 4, PRE_STEP, 1 );\n";
00219   algo.remove_assoc_step( 4, PRE_STEP, 1 );
00220 
00221   _out  << "\nalgo.insert_assoc_step( 4, PRE_STEP, 1 , \"Step_4_m2\", new ControledLoop1Step );\n";
00222   algo.insert_assoc_step( 4, PRE_STEP, 1 , "Step_4_m2", Teuchos::rcp(new ControledLoop1Step) );
00223 
00224   _out  << "\n\nalgo.print_steps(_out)\n\n";
00225   algo.print_steps(_out);
00226 
00227   _out  << "\nalgo.state.k(0);\n";
00228   algo.state().k(0);
00229 
00230   _out  << "\nalgo.do_algorithm();\n";
00231   algo.do_algorithm();
00232 
00233   // Test runtime configuration change.
00234 
00235   _out  << "\n\n*** Test runtime configuration change ***\n";
00236 
00237   _out  << "\nalgo.remove_assoc_step( 4, PRE_STEP, 1 );\n";
00238   algo.remove_assoc_step( 4, PRE_STEP, 1 );
00239 
00240   _out  << "\nalgo.insert_assoc_step( 4, PRE_STEP, 1, \"Step_4_m2\", new  RuntimeConfigChangeStep );\n";
00241   algo.insert_assoc_step( 4, PRE_STEP, 1, "Step_4_m2", Teuchos::rcp(new  RuntimeConfigChangeStep) );
00242 
00243   _out  << "\n\nalgo.print_steps(_out)\n\n";
00244   algo.print_steps(_out);
00245 
00246   _out  << "\nalgo.state.k(0);\n";
00247   algo.state().k(0);
00248 
00249   _out  << "\nalgo.max_iter(5);\n";
00250   algo.max_iter(5);
00251 
00252   try {
00253     _out  << "In the 5th (k= 4) iteration an Algorithm::InvalidConfigChange exception should be thrown.";
00254     _out  << "\nalgo.do_algorithm();\n";
00255     algo.do_algorithm();
00256     success = false;
00257     _out
00258       << "Algorithm threw exception : false\n";
00259   }
00260   catch(Algorithm::InvalidConfigChange& excpt) {
00261     _out << "\nAs expected!  Caught a Algorithm::InvalidConfigChange&: " << excpt.what() << endl
00262        << "Algorithm threw exception : true\n";
00263   }
00264 
00265   _out << "\n*** Congradulations, If you read this the tests for Algorithm"
00266       " seem to have been successful\n";
00267 
00268   return success;
00269 
00270   } // end try
00271   catch(const std::exception& excpt) {
00272     _out << "\nCaught a std::exception: " << excpt.what() << endl;
00273   }
00274   catch(...) {
00275     _out << "\nCaught an unknown exception\n";
00276   }
00277 
00278   _out << "\n*** Oops, If you read this some function throw an unexpected exception and the tests have failed!\n";
00279 
00280   return false;
00281 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Apr 13 10:10:59 2011 for MOOCHO (Single Doxygen Collection) by  doxygen 1.6.3