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:	 NonDGlobalEvidence
10 //- Description: Implementation code for NonDEvidence class
11 //- Owner:       Laura Swiler
12 //- Checked by:
13 //- Version:
14 
15 #include "NonDGlobalEvidence.hpp"
16 #include "dakota_data_types.hpp"
17 #include "dakota_system_defs.hpp"
18 #include "DakotaApproximation.hpp"
19 
20 
21 namespace Dakota {
22 
NonDGlobalEvidence(ProblemDescDB & problem_db,Model & model)23 NonDGlobalEvidence::NonDGlobalEvidence(ProblemDescDB& problem_db, Model& model):
24   NonDGlobalInterval(problem_db, model)
25 {
26   // if the user does not specify the number of samples,
27   // try to get at least one function evaluation in each cell
28   //if (!numSamples)
29   //  numSamples = numCells;
30 }
31 
32 
~NonDGlobalEvidence()33 NonDGlobalEvidence::~NonDGlobalEvidence()
34 { }
35 
36 
initialize()37 void NonDGlobalEvidence::initialize()
38 { calculate_cells_and_bpas(); }
39 
40 
set_cell_bounds()41 void NonDGlobalEvidence::set_cell_bounds()
42 {
43   size_t j;
44   for (j=0; j<numContIntervalVars; ++j) {
45     intervalOptModel.continuous_lower_bound(cellContLowerBounds[cellCntr][j],j);
46     intervalOptModel.continuous_upper_bound(cellContUpperBounds[cellCntr][j],j);
47   }
48 
49   for (j=0; j<numDiscIntervalVars; ++j) {
50     intervalOptModel.discrete_int_lower_bound(
51       cellIntRangeLowerBounds[cellCntr][j], j);
52     intervalOptModel.discrete_int_upper_bound(
53       cellIntRangeUpperBounds[cellCntr][j], j);
54   }
55 
56   for (j=0; j<numDiscSetIntUncVars; ++j)
57     intervalOptModel.discrete_int_variable(cellIntSetBounds[cellCntr][j],
58 					   j+numDiscIntervalVars);
59 
60   for (j=0; j<numDiscSetRealUncVars; ++j)
61     intervalOptModel.discrete_real_variable(cellRealSetBounds[cellCntr][j],j);
62 }
63 
64 
post_process_cell_results(bool maximize)65 void NonDGlobalEvidence::post_process_cell_results(bool maximize)
66 {
67   if (maximize)
68     cellFnUpperBounds[respFnCntr][cellCntr] = truthFnStar;
69   else
70     cellFnLowerBounds[respFnCntr][cellCntr] = truthFnStar;
71 }
72 
73 
post_process_response_fn_results()74 void NonDGlobalEvidence::post_process_response_fn_results()
75 { calculate_cbf_cpf(); }
76 
77 
post_process_final_results()78 void NonDGlobalEvidence::post_process_final_results()
79 {
80 #ifdef DEBUG
81   size_t i, j;
82   for (i=0; i<numCells; ++i) {
83     Cout << "Cell " << i << "\nBPA: " << cellBPA[i] << std::endl;;
84     for (j=0, j<numContIntervalVars; ++j)
85       Cout << "Cell bounds for continuous variable " << j << ": ("
86 	   << cellContLowerBounds[i][j] << ", "
87 	   << cellContUpperBounds[i][j] << ")\n";
88     for (j=0; j<numDiscIntervalVars; ++j)
89       Cout << "Cell bounds for discrete int range variable " << j << ": ("
90 	   << cellIntRangeLowerBounds[i][j] << ", "
91 	   << cellIntRangeUpperBounds[i][j] << ")\n";
92     for (j=0; j<numDiscSetIntUncVars; ++j)
93       Cout << "Cell value for discrete int set variable " << j << ": "
94 	   << cellIntSetBounds[i][j] << '\n';
95     for (j=0; j<numDiscSetRealUncVars; ++j)
96       Cout << "Cell value for discrete real set variable " << j << ": "
97 	   << cellRealSetBounds[i][j] << '\n';
98     for (j=0; j<numFunctions; ++j)
99       Cout << "Response fn " << j << " (min,max) for cell " << i << ": ("
100 	   << cellFnLowerBounds[j][i] << ", " << cellFnUpperBounds[j][i]
101 	   << ")\n";
102   }
103 #endif
104   // compute statistics
105   compute_evidence_statistics();
106 }
107 
108 
get_best_sample(bool maximize,bool eval_approx)109 void NonDGlobalEvidence::get_best_sample(bool maximize, bool eval_approx)
110 {
111   // Pull the samples and responses from data used to build latest GP
112   // to determine fnStar for use in the expected improvement function
113 
114   const Pecos::SurrogateData& gp_data
115     = fHatModel.approximation_data(respFnCntr);
116   const Pecos::SDVArray& sdv_array = gp_data.variables_data();
117   const Pecos::SDRArray& sdr_array = gp_data.response_data();
118 
119   // GT:
120   // We want to make sure that we pick a data point that lies inside the cell
121   size_t i, j, index_star, num_data_pts = gp_data.points();
122   truthFnStar = (maximize) ? -DBL_MAX : DBL_MAX;
123   for (i=0; i<num_data_pts; ++i) {
124     const Real&      truth_fn = sdr_array[i].response_function();
125     const Pecos::SurrogateDataVars& sdv = sdv_array[i];
126     const RealVector&  c_vars = sdv.continuous_variables();
127     const IntVector&  di_vars = sdv.discrete_int_variables();
128     const RealVector& dr_vars = sdv.discrete_real_variables();
129     bool in_bounds = true;
130     for (j=0; j<numContIntervalVars; ++j)
131       if (c_vars[j] < cellContLowerBounds[cellCntr][j] ||
132 	  c_vars[j] > cellContUpperBounds[cellCntr][j])
133 	{ in_bounds = false; break; }
134     if (in_bounds)
135       for (j=0; j<numDiscIntervalVars; ++j)
136 	if (di_vars[j] < cellIntRangeLowerBounds[cellCntr][j] ||
137 	    di_vars[j] > cellIntRangeUpperBounds[cellCntr][j])
138 	  { in_bounds = false; break; }
139     if (in_bounds)
140       for (j=0; j<numDiscSetIntUncVars; ++j)
141 	if (di_vars[j+numDiscIntervalVars] != cellIntSetBounds[cellCntr][j])
142 	  { in_bounds = false; break; }
143     if (in_bounds)
144       for (j=0; j<numDiscSetRealUncVars; ++j)
145 	if (dr_vars[j] != cellRealSetBounds[cellCntr][j])
146 	  { in_bounds = false; break; }
147 
148     if ( in_bounds && ( (  maximize && truth_fn > truthFnStar ) ||
149 			( !maximize && truth_fn < truthFnStar ) ) ) {
150       index_star  = i;
151       truthFnStar = truth_fn;
152     }
153   }
154 
155   if (eval_approx) {
156     if ( ( !maximize && truthFnStar ==  DBL_MAX ) ||
157 	 (  maximize && truthFnStar == -DBL_MAX ) ) {
158       Cout << "No function evaluations were found in cell. Truth function is "
159 	   << "set to DBL_MAX and approxFnStar is evaluated at midpoint.\n";
160       for (i=0; i<numContIntervalVars; ++i)
161 	fHatModel.continuous_variable(
162 	  ( cellContLowerBounds[cellCntr][i] +
163 	    cellContUpperBounds[cellCntr][i] ) / 2., i);
164       for (i=0; i<numDiscIntervalVars; ++i)
165 	fHatModel.discrete_int_variable(
166 	  ( cellIntRangeLowerBounds[cellCntr][i] +
167 	    cellIntRangeUpperBounds[cellCntr][i] ) / 2, i);
168       for (i=0; i<numDiscSetIntUncVars; ++i)
169 	fHatModel.discrete_int_variable(
170 	  cellIntSetBounds[cellCntr][i], i+numDiscIntervalVars);
171       for (i=0; i<numDiscSetRealUncVars; ++i)
172 	fHatModel.discrete_real_variable(
173 	  cellRealSetBounds[cellCntr][i], i);
174     }
175     else {
176       const Pecos::SurrogateDataVars& sdv = sdv_array[index_star];
177       if (numContIntervalVars)
178 	fHatModel.continuous_variables(sdv.continuous_variables());
179       if (numDiscIntervalVars || numDiscSetIntUncVars)
180 	fHatModel.discrete_int_variables(sdv.discrete_int_variables());
181       if (numDiscSetRealUncVars)
182 	fHatModel.discrete_real_variables(sdv.discrete_real_variables());
183     }
184 
185     ActiveSet set = fHatModel.current_response().active_set();
186     set.request_values(0); set.request_value(1, respFnCntr);
187     fHatModel.evaluate(set);
188     approxFnStar = fHatModel.current_response().function_value(respFnCntr);
189   }
190 #ifdef DEBUG
191   Cout << "truthFnStar: " << truthFnStar << "\napproxFnStar: " << approxFnStar
192        << std::endl;
193 #endif
194 }
195 
196 } // namespace Dakota
197