1 /*  _______________________________________________________________________
2 
3     DAKOTA: Design Analysis Kit for Optimization and Terascale Applications
4     Copyright 2014-2020 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
5     This software is distributed under the GNU Lesser General Public License.
6     For more information, see the README file in the top Dakota directory.
7     _______________________________________________________________________ */
8 
9 //- Class:	 NonD
10 //- Description: Base class for NonDeterministic branch
11 //- Owner:	 Mike Eldred
12 //- Checked by:
13 //- Version:
14 
15 #ifndef DAKOTA_NOND_H
16 #define DAKOTA_NOND_H
17 
18 #include "DakotaAnalyzer.hpp"
19 
20 //#define DERIV_DEBUG
21 
22 
23 namespace Dakota {
24 
25 
26 /// Base class for all nondetermistic iterators (the DAKOTA/UQ branch).
27 
28 /** The base class for nondeterministic iterators consolidates
29     uncertain variable data and probabilistic utilities for inherited
30     classes. */
31 
32 class NonD: public Analyzer
33 {
34 public:
35 
36   //
37   //- Heading: Member functions
38   //
39 
40   /// set requestedRespLevels, requestedProbLevels, requestedRelLevels,
41   /// requestedGenRelLevels, respLevelTarget, cdfFlag, and pdfOutput
42   /// (used in combination with alternate ctors)
43   void requested_levels(const RealVectorArray& req_resp_levels,
44 			const RealVectorArray& req_prob_levels,
45 			const RealVectorArray& req_rel_levels,
46 			const RealVectorArray& req_gen_rel_levels,
47 			short resp_lev_tgt, short resp_lev_tgt_reduce,
48 			bool cdf_flag, bool pdf_output);
49 
50   /// prints the z/p/beta/beta* mappings reflected in
51   /// {requested,computed}{Resp,Prob,Rel,GenRel}Levels for default
52   /// qoi_type and qoi_labels
53   void print_level_mappings(std::ostream& s) const;
54   /// prints the z/p/beta/beta* mappings reflected in
55   /// {requested,computed}{Resp,Prob,Rel,GenRel}Levels
56   void print_level_mappings(std::ostream& s, String qoi_type,
57 			    const StringArray& qoi_labels) const;
58   /// print level mapping statistics using optional pre-pend
59   void print_level_mappings(std::ostream& s, const RealVector& level_maps,
60 			    bool moment_offset, const String& prepend = "");
61 
62   //
63   //- Heading: Virtual member function redefinitions
64   //
65 
66   bool resize();
67 
68   //
69   //- Heading: Set/get routines
70   //
71 
72   /// get pdfOutput
73   bool pdf_output() const;
74   /// set pdfOutput
75   void pdf_output(bool output);
76 
77   /// get finalMomentsType
78   short final_moments_type() const;
79   /// set finalMomentsType
80   void final_moments_type(short type);
81 
82 protected:
83 
84   //
85   //- Heading: Constructors and destructor
86   //
87 
88   /// constructor
89   NonD(ProblemDescDB& problem_db, Model& model);
90   /// alternate constructor for sample generation and evaluation "on the fly"
91   NonD(unsigned short method_name, Model& model);
92   /// alternate constructor for sample generation "on the fly"
93   NonD(unsigned short method_name, const RealVector& lower_bnds,
94        const RealVector& upper_bnds);
95 
96   ~NonD(); ///< destructor
97 
98   //
99   //- Heading: Virtual member function redefinitions
100   //
101 
102   void derived_set_communicators(ParLevLIter pl_iter);
103 
104   void initialize_run();
105   void finalize_run();
106 
107   // return the final uncertain variables from the nondeterministic iteration
108   //const Variables& variables_results() const;
109   /// return the final statistics from the nondeterministic iteration
110   const Response& response_results() const;
111   /// set the active set within finalStatistics
112   void response_results_active_set(const ActiveSet& set);
113 
114   //
115   //- Heading: New virtual member functions
116   //
117 
118   /// initializes respCovariance
119   virtual void initialize_response_covariance();
120   /// initializes finalStatistics for storing NonD final results
121   virtual void initialize_final_statistics();
122   /// update finalStatistics::functionValues
123   virtual void update_final_statistics();
124 
125   //
126   //- Heading: Utility routines
127   //
128 
129   /// concatenate computed{Resp,Prob,Rel,GenRel}Levels into level_maps
130   void pull_level_mappings(RealVector& level_maps, size_t offset);
131   /// update computed{Resp,Prob,Rel,GenRel}Levels from level_maps
132   void push_level_mappings(const RealVector& level_maps, size_t offset);
133 
134   /// distribute pilot sample specification across model levels
135   void load_pilot_sample(const SizetArray& pilot_spec, SizetArray& delta_N_l);
136   /// distribute pilot sample specification across model forms or levels
137   void load_pilot_sample(const SizetArray& pilot_spec, const Sizet3DArray& N_l,
138 			 SizetArray& delta_N_l);
139   /// distribute pilot sample specification across model forms and levels
140   void load_pilot_sample(const SizetArray& pilot_spec, const Sizet3DArray& N_l,
141 			 Sizet2DArray& delta_N_l);
142 
143   /// resizes finalStatistics::functionGradients based on finalStatistics ASV
144   void resize_final_statistics_gradients();
145   /// update finalStatistics::functionValues from momentStats and
146   /// computed{Prob,Rel,GenRel,Resp}Levels
147   void update_aleatory_final_statistics();
148   /// update system metrics from component metrics within finalStatistics
149   void update_system_final_statistics();
150   /// update finalStatistics::functionGradients
151   void update_system_final_statistics_gradients();
152 
153   /// size computed{Resp,Prob,Rel,GenRel}Levels
154   void initialize_level_mappings();
155   /// compute the PDF bins from the CDF/CCDF values and store in
156   /// computedPDF{Abscissas,Ordinates}
157   void compute_densities(const RealRealPairArray& min_max_fns,
158 			 bool prob_refinement = false,
159 			 bool all_levels_computed = false);
160   /// output the PDFs reflected in computedPDF{Abscissas,Ordinates} using
161   /// default qoi_type and pdf_labels
162   void print_densities(std::ostream& s) const;
163   /// output the PDFs reflected in computedPDF{Abscissas,Ordinates}
164   void print_densities(std::ostream& s, String qoi_type,
165 		       const StringArray& pdf_labels) const;
166   /// print system series/parallel mappings for response levels
167   void print_system_mappings(std::ostream& s) const;
168 
169   /// print evaluation summary for multilevel sampling across 1D profile
170   void print_multilevel_evaluation_summary(std::ostream& s,
171 					   const SizetArray& N_samp);
172   /// print evaluation summary for multilevel sampling across 2D profile
173   void print_multilevel_evaluation_summary(std::ostream& s,
174 					   const Sizet2DArray& N_samp);
175   /// print evaluation summary for multilevel sampling across 3D profile
176   void print_multilevel_evaluation_summary(std::ostream& s,
177 					   const Sizet3DArray& N_samp);
178 
179   /// assign a NonDLHSSampling instance within u_space_sampler
180   void construct_lhs(Iterator& u_space_sampler, Model& u_model,
181 		     unsigned short sample_type, int num_samples, int seed,
182 		     const String& rng, bool vary_pattern,
183 		     short sampling_vars_mode = ACTIVE);
184 
185   /// compute a one-sided sample increment for multilevel methods to
186   /// move current sampling level to a new target
187   size_t one_sided_delta(Real current, Real target);
188 
189   /// allocate results array storage for distribution mappings
190   void archive_allocate_mappings();
191   /// archive the mappings from specified response levels for specified fn
192   void archive_from_resp(size_t fn_index, size_t inc_id = 0);
193   /// archive the mappings to computed response levels for specified fn and
194   /// (optional) increment id.
195   void archive_to_resp(size_t fn_index, size_t inc_id = 0);
196   /// allocate results array storage for pdf histograms
197   void archive_allocate_pdf();
198   /// archive a single pdf histogram for specified function
199   void archive_pdf(size_t fn_index, size_t inc_id = 0);
200   /// archive the equivalent number of HF evals (used by ML/MF methods)
201   void archive_equiv_hf_evals(const Real equiv_hf_evals);
202 
203   //
204   //- Heading: Data members
205   //
206 
207   /// pointer to the active object instance used within static evaluator
208   /// functions in order to avoid the need for static data
209   static NonD* nondInstance;
210   /// pointer containing previous value of nondInstance
211   NonD* prevNondInstance;
212 
213   /// starting index of continuous aleatory uncertain variables within
214   /// active continuous variables (convenience for managing offsets)
215   size_t startCAUV;
216   /// number of active continuous aleatory uncertain variables
217   size_t numCAUV;
218 
219   /// flag for computing interval-type metrics instead of integrated metrics
220   /// If any epistemic vars are active in a metric evaluation, then flag is set.
221   bool epistemicStats;
222 
223   /// standardized or central resp moments, as determined by finalMomentsType.
224   /// Calculated in compute_moments()) and indexed as (moment,fn).
225   RealMatrix momentStats;
226 
227   // map response level z -> probability level p, reliability level beta,
228   // or generalized reliability level beta*
229 
230   /// requested response levels for all response functions
231   RealVectorArray requestedRespLevels;
232   /// output probability levels for all response functions resulting
233   /// from requestedRespLevels
234   RealVectorArray computedProbLevels;
235   /// output reliability levels for all response functions resulting
236   /// from requestedRespLevels
237   RealVectorArray computedRelLevels;
238   /// output generalized reliability levels for all response functions
239   /// resulting from requestedRespLevels
240   RealVectorArray computedGenRelLevels;
241   /// indicates mapping of z->p (PROBABILITIES), z->beta (RELIABILITIES),
242   /// or z->beta* (GEN_RELIABILITIES)
243   short respLevelTarget;
244   /// indicates component or system series/parallel failure metrics
245   short respLevelTargetReduce;
246 
247   // map probability level p, reliability level beta, or generalized
248   // reliability level beta* -> response level z
249 
250   /// requested probability levels for all response functions
251   RealVectorArray requestedProbLevels;
252   /// requested reliability levels for all response functions
253   RealVectorArray requestedRelLevels;
254   /// requested generalized reliability levels for all response functions
255   RealVectorArray requestedGenRelLevels;
256   /// output response levels for all response functions resulting from
257   /// requestedProbLevels, requestedRelLevels, or requestedGenRelLevels
258   RealVectorArray computedRespLevels;
259 
260   /// total number of levels specified within requestedRespLevels,
261   /// requestedProbLevels, and requestedRelLevels
262   size_t totalLevelRequests;
263 
264   /// flag for type of probabilities/reliabilities used in mappings:
265   /// cumulative/CDF (true) or complementary/CCDF (false)
266   bool cdfFlag;
267 
268   /// flag for managing output of response probability density functions (PDFs)
269   bool pdfOutput;
270   /// sorted response PDF intervals bounds extracted from min/max sample
271   /// and requested/computedRespLevels (vector lengths = num bins + 1)
272   RealVectorArray computedPDFAbscissas;
273   /// response PDF densities computed from bin counts divided by
274   /// (unequal) bin widths (vector lengths = num bins)
275   RealVectorArray computedPDFOrdinates;
276 
277   /// final statistics from the uncertainty propagation used in strategies:
278   /// response means, standard deviations, and probabilities of failure
279   Response finalStatistics;
280   /// type of moments logged within finalStatistics: none, central, standard
281   short finalMomentsType;
282 
283   /// index for the active ParallelLevel within ParallelConfiguration::miPLIters
284   size_t miPLIndex;
285 
286   /// Whether PDF was computed for function i; used to determine whether
287   /// a pdf should be archived
288   BitArray pdfComputed;
289 
290 private:
291 
292   /// initialize data based on variable counts
293   void initialize_counts();
294 
295   /// convenience function for distributing a vector of levels among multiple
296   /// response functions if a short-hand specification is employed.
297   void distribute_levels(RealVectorArray& levels, bool ascending = true);
298 
299   /// Write level mappings to a file for a single response
300   void level_mappings_file(size_t fn_index, const String& qoi_label) const;
301 
302   /// Print level mapping for a single response function to ostream
303   void print_level_map(std::ostream& s, size_t fn_index,
304 		       const String& qoi_label) const;
305 
306   /// return true if N_l has consistent values
307   bool homogeneous(const SizetArray& N_l) const;
308 
309   //
310   //- Heading: Data members
311   //
312 
313 };
314 
315 
~NonD()316 inline NonD::~NonD()
317 { }
318 
319 
pdf_output() const320 inline bool NonD::pdf_output() const
321 { return pdfOutput; }
322 
323 
pdf_output(bool output)324 inline void NonD::pdf_output(bool output)
325 { pdfOutput = output; }
326 
327 
final_moments_type() const328 inline short NonD::final_moments_type() const
329 { return finalMomentsType; }
330 
331 
final_moments_type(short type)332 inline void NonD::final_moments_type(short type)
333 { finalMomentsType = type; }
334 
335 
initialize_run()336 inline void NonD::initialize_run()
337 {
338   Analyzer::initialize_run();
339   prevNondInstance = nondInstance;
340   nondInstance = this;
341 }
342 
343 
finalize_run()344 inline void NonD::finalize_run()
345 {
346   nondInstance = prevNondInstance;
347   Analyzer::finalize_run();
348 }
349 
350 
response_results() const351 inline const Response& NonD::response_results() const
352 { return finalStatistics; }
353 
354 
response_results_active_set(const ActiveSet & set)355 inline void NonD::response_results_active_set(const ActiveSet& set)
356 { finalStatistics.active_set(set); }
357 
358 
print_level_mappings(std::ostream & s) const359 inline void NonD::print_level_mappings(std::ostream& s) const
360 {
361   print_level_mappings(s, "response function", iteratedModel.response_labels());
362 }
363 
364 
print_densities(std::ostream & s) const365 inline void NonD::print_densities(std::ostream& s) const
366 { print_densities(s, "response function", iteratedModel.response_labels()); }
367 
368 
homogeneous(const SizetArray & N_l) const369 inline bool NonD::homogeneous(const SizetArray& N_l) const
370 {
371   size_t N0 = N_l[0], i, len = N_l.size();
372   for (i=1; i<len; ++i)
373     if (N_l[i] != N0)
374       return false;
375   return true;
376 }
377 
378 
one_sided_delta(Real current,Real target)379 inline size_t NonD::one_sided_delta(Real current, Real target)
380 { return (target > current) ? (size_t)std::floor(target - current + .5) : 0; }
381 
382 } // namespace Dakota
383 
384 #endif
385