Sparse Grids Example 9: local polynomial refinement.
Example 8 showed the difference between the local polynomial rules and how the basis can be tuned to a specific model. This example shows refinement in the local polynomial context using hierarchical coefficients (surpluses). A sharp model is selected so that TasGrid::rule_localp is the natural choice, and two different refinement strategies are tested.
#endif
cout << "\n---------------------------------------------------------------------------------------------------\n";
cout << std::scientific; cout.precision(4);
cout << "Example 9: comparison between local polynomial refinement strategies\n\n";
int const num_inputs = 2;
int const num_test_points = 1000;
std::vector<double> test_points(num_test_points * num_inputs);
std::minstd_rand park_miller(42);
std::uniform_real_distribution<double> domain(-1.0, 1.0);
for(auto &t : test_points) t = domain(park_miller);
std::function<void(double const x[], double y[], size_t)> model)->
double{
std::vector<double> grid_result;
double err = 0.0;
for(int i=0; i<num_test_points; i++){
double model_result;
model(&test_points[i*num_inputs], &model_result, 0);
err = std::max(err, std::abs(grid_result[i] - model_result));
}
return err;
};
auto sharp_model = [](double const x[], double y[], size_t)->
void{ y[0] = exp(-x[0]) / (1.0 + 100.0 * exp(-10.0 * x[1])); };
int const order = -1;
cout << "Using batch refinement:\n"
<< setw(22) << "classic" << setw(22) << "fds\n"
<< setw(8) << "points" << setw(14) << "error"
<< setw(8) << "points" << setw(14) << "error\n";
double const tolerance = 1.E-5;
while((grid_classic.getNumNeeded() > 0) || (grid_fds.getNumNeeded() > 0)){
cout << setw(8) << grid_classic.getNumLoaded()
<< setw(14) << get_error(grid_classic, sharp_model)
<< setw(8) << grid_fds.getNumLoaded()
<< setw(14) << get_error(grid_fds, sharp_model) << "\n";
}
auto vector_model = [=](std::vector<double> const &x,
std::vector<double> &y,
size_t tid)->
void{
y.resize(1);
sharp_model(x.data(), y.data(), tid);
};
cout << "\nUsing construction:\n"
<< setw(22) << "classic" << setw(22) << "fds\n"
<< setw(8) << "points" << setw(14) << "error"
<< setw(8) << "points" << setw(14) << "error\n";
for(size_t budget = 50; budget < 800; budget += 100){
cout << setw(8) << grid_classic.getNumLoaded()
<< setw(14) << get_error(grid_classic, sharp_model)
<< setw(8) << grid_fds.getNumLoaded()
<< setw(14) << get_error(grid_fds, sharp_model) << "\n";
}
#ifndef __TASMANIAN_DOXYGEN_SKIP
The master-class that represents an instance of a Tasmanian sparse grid.
Definition: TasmanianSparseGrid.hpp:293
void evaluateBatch(std::vector< FloatType > const &x, std::vector< FloatType > &y) const
Computes the value of the interpolant (or point-wise approximation) for a batch of points.
@ rule_localp
Nested rule with a hierarchy of uniformly distributed nodes and functions with compact support.
Definition: tsgEnumerates.hpp:362
@ refine_classic
Isotropic refinement using only the children and disregarding missing parents.
Definition: tsgEnumerates.hpp:427
@ refine_fds
Anisotropic refinement adding children only if the parents are already included.
Definition: tsgEnumerates.hpp:433
void constructSurrogate(ModelSignature model, size_t max_num_points, size_t num_parallel_jobs, size_t max_samples_per_job, TasmanianSparseGrid &grid, double tolerance, TypeRefinement criteria, int output=-1, std::vector< int > const &level_limits=std::vector< int >(), std::string const &checkpoint_filename=std::string())
Construct a sparse grid surrogate to the model defined by the lambda.
Definition: tsgConstructSurrogate.hpp:464
void loadNeededValues(std::function< void(double const x[], double y[], size_t thread_id)> model, TasmanianSparseGrid &grid, size_t num_threads)
Loads the current grid with model values, does not perform any refinement.
Definition: tsgLoadNeededValues.hpp:104
TasmanianSparseGrid makeLocalPolynomialGrid(int dimensions, int outputs, int depth, int order=1, TypeOneDRule rule=rule_localp, std::vector< int > const &level_limits=std::vector< int >())
Factory method, creates a new grid and calls TasmanianSparseGrid::makeLocalPolynomialGrid().
Definition: TasmanianSparseGrid.hpp:2301
TasmanianSparseGrid copyGrid(TasmanianSparseGrid const &source, int outputs_begin=0, int outputs_end=-1)
Returns a grid that is a copy of the source.
Definition: TasmanianSparseGrid.hpp:2367