Doxygen 1.9.1
Toolkit for Adaptive Stochastic Modeling and Non-Intrusive ApproximatioN: Tasmanian v8.1
Tasmanian Sparse Grids module, example 9
Collaboration diagram for Tasmanian Sparse Grids module, example 9:

Functions

void sparse_grids_example_09 ()
 Sparse Grids Example 9: local polynomial refinement. More...
 

Detailed Description

Example 9
Different local polynomial refinement.

Function Documentation

◆ sparse_grids_example_09()

void sparse_grids_example_09 ( )

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.

Note: the refinement process can be used in the context of construction, e.g., similar to Example 6 and TasGrid::constructSurrogate().

#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;
// using random points to test the error
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);
// computes the error between the gird surrogate model and the actual model
// using the test points, finds the largest absolute error
auto get_error = [&](TasGrid::TasmanianSparseGrid const &grid,
std::function<void(double const x[], double y[], size_t)> model)->
double{
std::vector<double> grid_result;
grid.evaluateBatch(test_points, grid_result);
double err = 0.0;
for(int i=0; i<num_test_points; i++){
double model_result; // using only one output
model(&test_points[i*num_inputs], &model_result, 0);
err = std::max(err, std::abs(grid_result[i] - model_result));
}
return err;
};
// using a model with sharp transition, the modified rules are not
// suitable for this model, using rule_localp
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])); };
// using maximal order
int const order = -1;
auto grid_classic = TasGrid::makeLocalPolynomialGrid(num_inputs, 1, 2, order,
auto grid_fds = TasGrid::copyGrid(grid_classic);
// setup the top rows of the output table
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)){
TasGrid::loadNeededValues(sharp_model, grid_classic, 4);
TasGrid::loadNeededValues(sharp_model, grid_fds, 4);
// output the grids and results at the current stage
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";
// setting refinement for each grid
grid_classic.setSurplusRefinement(tolerance, TasGrid::refine_classic);
grid_fds.setSurplusRefinement(tolerance, TasGrid::refine_fds);
}
// Repeat the example using construction
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);
};
// reset the grids
grid_classic = TasGrid::makeLocalPolynomialGrid(num_inputs, 1, 2, order,
grid_fds = TasGrid::copyGrid(grid_classic);
// setup the top rows of the output table
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){
TasGrid::constructSurrogate(vector_model, budget, 1, 1, grid_classic,
TasGrid::constructSurrogate(vector_model, budget, 1, 1, grid_fds,
tolerance, TasGrid::refine_fds);
// output the grids and results at the current stage
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";
}
// The FDS strategy does not win at every step of the way, but at the final
// stage the FDS approach results in fewer nodes while keeping the error
// at near the same magnitude
#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