#include <cmath>
#include <iostream>
#include <memory>
#include <vector>
double f_2D(DataVector v) { return 4.0 * v[0] * v[0] * (v[1] - v[1] * v[1]); }
void simpleInterpolation() {
size_t numDimensions = 2;
f_2D);
auto operation =
CombigridOperation::createLinearLejaPolynomialInterpolation(numDimensions, wrapper);
size_t maxLevelSum = 2;
double result = operation->evaluate(
maxLevelSum,
DataVector(std::vector<double>{
0.5, 0.7}));
std::cout << result << "\n";
}
void adaptiveInterpolation() {
size_t numDimensions = 2;
f_2D);
auto operation =
CombigridOperation::createLinearLejaPolynomialInterpolation(numDimensions, wrapper);
DataVector param(std::vector<double>{0.5, 0.7});
auto levelManager = std::make_shared<WeightedRatioLevelManager>();
operation->setParameters(param);
operation->setLevelManager(levelManager);
operation->getLevelManager()->addLevelsAdaptive(300);
double result = operation->getResult();
std::cout << result << "\n";
}
void multistageInterpolation() {
size_t numDimensions = 2;
std::vector<DataVector> gridPoints;
auto dummyFunc = [&gridPoints](DataVector const ¶m) -> double {
gridPoints.push_back(param);
return 0.0;
};
std::vector<std::shared_ptr<AbstractPointHierarchy>> pointHierarchies(numDimensions);
size_t growthFactor = 2;
pointHierarchies[0] = std::make_shared<NestedPointHierarchy>(
std::make_shared<LejaPointDistribution>(),
std::make_shared<IdentityPointOrdering>(std::make_shared<LinearGrowthStrategy>(growthFactor),
false));
pointHierarchies[1] = pointHierarchies[0];
std::vector<std::shared_ptr<AbstractLinearEvaluator<FloatArrayVector>>> evaluators(numDimensions);
evaluators[0] = std::make_shared<ArrayEvaluator<PolynomialInterpolationEvaluator>>(
true);
evaluators[1] = evaluators[0];
auto storage = std::make_shared<CombigridTreeStorage>(pointHierarchies,
MultiFunction(dummyFunc));
auto fullGridEval =
std::make_shared<sgpp::combigrid::FullGridCallbackEvaluator<FloatArrayVector>>(
storage, evaluators, pointHierarchies);
auto combiGridEval =
std::make_shared<CombigridEvaluator<FloatArrayVector>>(numDimensions, fullGridEval);
size_t maxLevelSum = 2;
std::vector<FloatArrayVector> parameters(2);
parameters[0] = FloatArrayVector(
std::vector<FloatScalarVector>{FloatScalarVector(0.5), FloatScalarVector(0.3)});
parameters[1] = FloatArrayVector(
std::vector<FloatScalarVector>{FloatScalarVector(0.7), FloatScalarVector(0.27)});
fullGridEval->setParameters(parameters);
auto levelManager = std::make_shared<sgpp::combigrid::AveragingLevelManager>(combiGridEval);
levelManager->addRegularLevels(maxLevelSum);
std::cout << "Grid points:\n";
for (auto &gridPoint : gridPoints) {
std::cout << gridPoint[0] << ", " << gridPoint[1] << "\n";
funcLookup(gridPoint);
}
auto realStorage = std::make_shared<CombigridTreeStorage>(
pointHierarchies,
MultiFunction([&funcLookup](DataVector
const ¶m) {
return funcLookup(param); }));
auto realFullGridEval =
std::make_shared<sgpp::combigrid::FullGridCallbackEvaluator<FloatArrayVector>>(
storage, evaluators, pointHierarchies);
auto realCombiGridEval =
std::make_shared<CombigridEvaluator<FloatArrayVector>>(numDimensions, realFullGridEval);
realFullGridEval->setParameters(parameters);
auto realLevelManager =
std::make_shared<sgpp::combigrid::AveragingLevelManager>(realCombiGridEval);
realLevelManager->addRegularLevels(maxLevelSum);
FloatArrayVector result = realCombiGridEval->getValue();
std::cout << "Results:\n";
for (
size_t i = 0;
i < result.size(); ++
i) {
std::cout << result.get(
i).getValue() <<
"\n";
}
}
simpleInterpolation();
adaptiveInterpolation();
multistageInterpolation();
return 0;
}