This example shows how grids with more interaction terms differ from simpler grids.
#include <cstdlib>
#include <tuple>
#include <unordered_set>
#include <vector>
decodeCoords
- Parameters
-
coords | are the coordinates of a grid point |
result | is the boolean version of the vector, each entry is true if the corresponding dimension is used, i.e. not equal to 0.5 result[ i] = coords[ i] != 0.5; } } |
main creates a grid with dimension three and level five and adds more and more interaction points.
This is done by using interaction-term aware sparse grids. First, we create a grid with interaction terms, that model only one feature, then we add the pairwise interactions and finally all interactions. The differences between the grids are printed.
int main(
int argc,
char** argv) {
auto dimensions = 3;
auto terms = std::unordered_set<std::vector<bool>>();
auto realCoords = std::unordered_set<std::vector<bool>>();
auto boolCoords = std::vector<bool>(dimensions);
bool isInserted = false;
terms.insert(std::vector<bool>(dimensions, false));
for (
auto dim = 0;
dim < dimensions; ++
dim) {
auto vec = std::vector<bool>(dimensions, false);
terms.insert(vec);
}
{
Create our first grid.
auto& storage =
grid->getStorage();
generator.regular_inter(storage,
level, terms);
std::cout <<
"Grid size with one level terms is " <<
grid->getSize() << std::endl;
for (
size_t i = 0;
i <
grid->getSize(); ++
i) {
decodeCoords(coords, boolCoords);
std::tie(std::ignore, isInserted) = realCoords.insert(boolCoords);
if (isInserted) {
std::cout << "New point" << coords.toString() << std::endl;
}
}
}
Add all two-level-interactions
for (
auto i = 0;
i < dimensions; ++
i) {
for (
auto j = 0;
j < dimensions; ++
j) {
auto vec = std::vector<bool>(dimensions, false);
terms.insert(vec);
}
}
{
auto& storage =
grid->getStorage();
generator.regular_inter(storage,
level, terms);
std::cout <<
"Grid size with two level terms is " <<
grid->getSize() << std::endl;
for (
size_t i = 0;
i <
grid->getSize(); ++
i) {
decodeCoords(coords, boolCoords);
std::tie(std::ignore, isInserted) = realCoords.insert(boolCoords);
if (isInserted) {
std::cout << "New point" << coords.toString() << std::endl;
}
}
}
Add all three-level-interactions
auto vec = std::vector<bool>(dimensions, true);
terms.insert(vec);
{
auto& storage =
grid->getStorage();
generator.regular_inter(storage,
level, terms);
std::cout <<
"Grid size with three level terms is " <<
grid->getSize() << std::endl;
for (
size_t i = 0;
i <
grid->getSize(); ++
i) {
decodeCoords(coords, boolCoords);
std::tie(std::ignore, isInserted) = realCoords.insert(boolCoords);
if (isInserted) {
std::cout << "New point" << coords.toString() << std::endl;
}
}
}
}