MOOCHO (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IterationPack_TestAlgorithm.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
5 // Copyright (2003) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #include <ostream>
43 #include <iomanip>
44 
50 
51 namespace IterationPack {
52 
53 // Implement minor loop 1
55 public:
56 
58  {}
59 
60  bool do_step(Algorithm& algo, poss_type step_poss, EDoStepType type
61  , poss_type assoc_step_poss)
62  {
63  if(called_) {
64  algo.terminate(false);
65  return false;
66  }
67  AlgorithmStepTesting::do_step(algo,step_poss,type,assoc_step_poss);
68  algo.track().journal_out() << "\n* Jump to \"Step_2\"\n";
69  algo.do_step_next("Step_2");
70  called_ = true;
71  return false;
72  }
73 
74 private:
75  bool called_;
76 };
77 
78 // Implement controled loop 1
80 public:
81 
83  {}
84 
85  bool do_step(Algorithm& algo, poss_type step_poss, EDoStepType type
86  , poss_type assoc_step_poss)
87  {
88  if(called_) {
89  algo.terminate(false);
90  return false;
91  }
92  AlgorithmStepTesting::do_step(algo,step_poss,type,assoc_step_poss);
93  algo.track().journal_out() << "\n* Do step \"Step_2_p1\" then Jump to \"Step_1\"\n";
94  algo.get_assoc_step(2,POST_STEP,1)->do_step(algo,2,DO_POST_STEP,1);
95  algo.do_step_next("Step_1");
96  called_ = true;
97  return false;
98  }
99 
100 private:
101  bool called_;
102 };
103 
104 // Runtime Config change
106 public:
107 
109  {}
110 
111  bool do_step(Algorithm& algo, poss_type step_poss, EDoStepType type
112  , poss_type assoc_step_poss)
113  {
114  AlgorithmStepTesting::do_step(algo,step_poss,type,assoc_step_poss);
115  if(!called_) {
116  algo.track().journal_out() << "\n* Remove step \"Step_1\" then put it back then print steps\n";
117  algo.begin_config_update();
118  Algorithm::step_ptr_t step = algo.get_step(1);
119  algo.remove_step(1);
120  algo.insert_step(1,"Step_1",step);
121  algo.end_config_update();
122  called_ = true;
123  }
124  else {
125  algo.track().journal_out() << "\n* Remove step 1, step_name = "
126  << algo.get_step_name(1) << " but don't put it back then print steps\n";
127  algo.begin_config_update();
128  algo.remove_step(1);
129  algo.end_config_update();
130  called_ = true;
131  }
132  algo.print_steps(algo.track().journal_out());
133  return true;
134  }
135 
136 private:
137  bool called_;
138 };
139 
140 } // end namespace IterationPack
141 
143 
144  using std::endl;
145  using std::setw;
146 
147  std::ostream& _out = *out;
148  // ToDo: RAB: 7/1/99: Modify for optional output when out == 0;
149 
150  try {
151 
152  bool success = true;
153 // const int w = 15;
154  _out << std::boolalpha;
155 
156  _out << "\n\n*************************\n"
157  << "*** Testing Algorithm ***\n"
158  << "*************************\n";
159 
160  Algorithm algo;
161 
164 
165  algo.set_state( state );
166  algo.set_track( track );
167 
170 
171  algo.insert_step( 1, "Step_1", step );
172 
173  algo.insert_step( 2, "Step_2", step );
174 
175  algo.insert_assoc_step( 2, PRE_STEP, 1, "Step_2_m1", assoc_step );
176 
177  algo.insert_assoc_step( 2, POST_STEP, 1, "Step_2_p1", assoc_step );
178 
179  algo.insert_step( 3, "Step_3", step );
180 
181  algo.insert_step( 4, "Step_4", step );
182 
183  algo.insert_assoc_step( 4, PRE_STEP, 1, "Step_4_m2", assoc_step );
184 
185  algo.insert_assoc_step( 4, PRE_STEP, 2, "Step_4_m1", assoc_step );
186 
187  _out << "\n\n*** algo.print_algorithm(_out)\n\n";
188  algo.print_algorithm(_out);
189 
190  // Test the major loop
191 
192  _out << "\n\n*** Test the major loop for two iterations ***\n";
193 
194  _out << "\nalgo.set_algo_timing(true); algo.max_iter(2); algo.do_algorithm();\n\n";
195  algo.set_algo_timing(true);
196  algo.max_iter(2);
197  algo.do_algorithm();
198 
199  // Test Minor Loop 1
200 
201  _out << "\n\n*** Test Minor Loop 1 ***\n";
202 
203  _out << "\nalgo.remove_assoc_step( 4, PRE_STEP, 2 );\n";
204  algo.remove_assoc_step( 4, PRE_STEP, 2 );
205 
206  _out << "\nalgo.insert_assoc_step( 4, PRE_STEP, 2, \"Step_4_m1\", new MinorLoop1Step );\n";
207  algo.insert_assoc_step( 4, PRE_STEP, 2, "Step_4_m1", Teuchos::rcp(new MinorLoop1Step) );
208 
209  _out << "\nalgo.state().k(0);\n";
210  algo.state().k(0);
211 
212  _out << "\nalgo.max_iter(1);\n";
213  algo.max_iter(1);
214 
215  _out << "\n\nalgo.print_steps(_out)\n\n";
216  algo.print_steps(_out);
217 
218  _out << "\nalgo.do_algorithm();\n";
219  algo.do_algorithm();
220 
221  // Test Controlled Loop 1
222 
223  _out << "\n\n*** Test Controlled Loop 1 ***\n";
224 
225  _out << "\nalgo.remove_assoc_step( 4, PRE_STEP, 2 );\n";
226  algo.remove_assoc_step( 4, PRE_STEP, 2 );
227 
228  _out << "\nalgo.insert_assoc_step( 4, PRE_STEP, 2, \"Step_4_m1\", assoc_step );\n";
229  algo.insert_assoc_step( 4, PRE_STEP, 2, "Step_4_m1", assoc_step );
230 
231  _out << "\nalgo.remove_assoc_step( 4, PRE_STEP, 1 );\n";
232  algo.remove_assoc_step( 4, PRE_STEP, 1 );
233 
234  _out << "\nalgo.insert_assoc_step( 4, PRE_STEP, 1 , \"Step_4_m2\", new ControledLoop1Step );\n";
235  algo.insert_assoc_step( 4, PRE_STEP, 1 , "Step_4_m2", Teuchos::rcp(new ControledLoop1Step) );
236 
237  _out << "\n\nalgo.print_steps(_out)\n\n";
238  algo.print_steps(_out);
239 
240  _out << "\nalgo.state.k(0);\n";
241  algo.state().k(0);
242 
243  _out << "\nalgo.do_algorithm();\n";
244  algo.do_algorithm();
245 
246  // Test runtime configuration change.
247 
248  _out << "\n\n*** Test runtime configuration change ***\n";
249 
250  _out << "\nalgo.remove_assoc_step( 4, PRE_STEP, 1 );\n";
251  algo.remove_assoc_step( 4, PRE_STEP, 1 );
252 
253  _out << "\nalgo.insert_assoc_step( 4, PRE_STEP, 1, \"Step_4_m2\", new RuntimeConfigChangeStep );\n";
254  algo.insert_assoc_step( 4, PRE_STEP, 1, "Step_4_m2", Teuchos::rcp(new RuntimeConfigChangeStep) );
255 
256  _out << "\n\nalgo.print_steps(_out)\n\n";
257  algo.print_steps(_out);
258 
259  _out << "\nalgo.state.k(0);\n";
260  algo.state().k(0);
261 
262  _out << "\nalgo.max_iter(5);\n";
263  algo.max_iter(5);
264 
265  try {
266  _out << "In the 5th (k= 4) iteration an Algorithm::InvalidConfigChange exception should be thrown.";
267  _out << "\nalgo.do_algorithm();\n";
268  algo.do_algorithm();
269  success = false;
270  _out
271  << "Algorithm threw exception : false\n";
272  }
273  catch(Algorithm::InvalidConfigChange& excpt) {
274  _out << "\nAs expected! Caught a Algorithm::InvalidConfigChange&: " << excpt.what() << endl
275  << "Algorithm threw exception : true\n";
276  }
277 
278  _out << "\n*** Congradulations, If you read this the tests for Algorithm"
279  " seem to have been successful\n";
280 
281  return success;
282 
283  } // end try
284  catch(const std::exception& excpt) {
285  _out << "\nCaught a std::exception: " << excpt.what() << endl;
286  }
287  catch(...) {
288  _out << "\nCaught an unknown exception\n";
289  }
290 
291  _out << "\n*** Oops, If you read this some function throw an unexpected exception and the tests have failed!\n";
292 
293  return false;
294 }
virtual EAlgoReturn do_algorithm(poss_type step_poss=1)
Called by clients to begin an algorithm.
virtual void max_iter(size_t max_iter)
virtual void print_steps(std::ostream &out) const
Print out just a listing of the steps, their positions in the algorithm and the subclasses.
bool do_step(Algorithm &algo, poss_type step_poss, EDoStepType type, poss_type assoc_step_poss)
virtual void begin_config_update()
Changes from running_state() == RUNNING to running_state() == RUNNING_BEING_CONFIGURED.
virtual void insert_assoc_step(poss_type step_poss, EAssocStepType type, poss_type assoc_step_poss, const std::string &assoc_step_name, const step_ptr_t &assoc_step)
Insert an pre or post step into for the main step step_poss into the possition assoc_step_poss.
bool do_step(Algorithm &algo, poss_type step_poss, EDoStepType type, poss_type assoc_step_poss)
virtual void insert_step(poss_type step_poss, const std::string &step_name, const step_ptr_t &step)
Insert a step object with the name step_name into the possition step_poss.
virtual void print_algorithm(std::ostream &out) const
Print out the entire algorithm by calling print_step(...) on the step objects.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual void terminate(bool success)
Called by step objects to terminate the algorithm.
virtual std::ostream & journal_out() const
Return a reference to a std::ostream to be used to output debug information and the like...
void set_state(const state_ptr_t &state)
std::ostream * out
virtual void do_step_next(const std::string &step_name)
Called by step objects to set the step (given its name) that this will envoke the next time this call...
Abstacts a set of iteration quantities for an iterative algorithm.
virtual step_ptr_t & get_assoc_step(poss_type step_poss, EAssocStepType type, poss_type assoc_step_poss)
Return the RCP<...> object for the associated step object at step_poss and assoc_step_poss.
virtual step_ptr_t & get_step(poss_type step_poss)
Return the RCP<...> object for the step object at step_poss.
virtual void end_config_update()
Changes from running_state() == RUNNING_BEING_CONFIGURED to running_state() == RUNNING.
virtual const std::string & get_step_name(poss_type step_poss) const
Return the name of a step given its possition.
Acts as the central hub for an iterative algorithm.
virtual void remove_assoc_step(poss_type step_poss, EAssocStepType type, poss_type assoc_step_poss)
Remove an pre or post step for the main step step_poss in the possition assoc_step_poss.
Thrown if a member function is called while this is in an invalid running state.. ...
virtual void remove_step(poss_type step_poss)
Remove an existing step object and all of its pre and post steps.
bool do_step(Algorithm &algo, poss_type step_poss, EDoStepType type, poss_type assoc_step_poss)
virtual void set_algo_timing(bool algo_timing)
Causes algorithm to be timed.
void set_track(const track_ptr_t &track)
bool do_step(Algorithm &algo, poss_type step_poss, EDoStepType type, poss_type assoc_step_poss)