Skip to content

Commit

Permalink
trunk:
Browse files Browse the repository at this point in the history
* Only add internal library dependencies to SCons's dependency graph. [all]
* Reformatting. [base, datadriven, optimization]

git-svn-id: https://ipvs.informatik.uni-stuttgart.de/SGpp/repos/trunk@5214 4eea3252-f0fb-4393-894d-40516dce545b
  • Loading branch information
valentjn committed Nov 9, 2015
1 parent f71c834 commit e8e4d84
Show file tree
Hide file tree
Showing 20 changed files with 234 additions and 203 deletions.
142 changes: 72 additions & 70 deletions base/examples/c++_simple_predictive_ANOVArefinement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,91 +25,93 @@ using namespace SGPP::base;

// function to interpolate - nonsymmetric(!)
SGPP::float_t f(SGPP::float_t x0, SGPP::float_t x1) {
return sin(x0 * 10);
return sin(x0 * 10);
}

DataVector& calculateError(const DataMatrix& dataSet, Grid& grid, const DataVector& alpha, DataVector& error) {
cout << "calculating error" << endl;

//traverse dataSet
DataVector vec(2);
OperationEval* opEval = SGPP::op_factory::createOperationEval(grid);
for (unsigned int i = 0; i < dataSet.getNrows(); i++) {
dataSet.getRow(i, vec);
error[i] = pow(f(dataSet.get(i, 0), dataSet.get(i, 1)) - opEval->eval(alpha, vec), 2);
}
return error;
cout << "calculating error" << endl;

//traverse dataSet
DataVector vec(2);
OperationEval* opEval = SGPP::op_factory::createOperationEval(grid);

for (unsigned int i = 0; i < dataSet.getNrows(); i++) {
dataSet.getRow(i, vec);
error[i] = pow(f(dataSet.get(i, 0), dataSet.get(i, 1)) - opEval->eval(alpha, vec), 2);
}

return error;
}

int main() {
// create a two-dimensional piecewise bilinear grid
size_t dim = 2;
Grid* grid = Grid::createModLinearGrid(dim);
GridStorage* hashGridStorage = grid->getStorage();
cout << "dimensionality: " << hashGridStorage->dim() << endl;

// create regular grid, level 3
size_t level = 3;
GridGenerator* gridGen = grid->createGridGenerator();
gridGen->regular(level);
cout << "number of grid points: " << hashGridStorage->size() << endl;

// create coefficient vector
DataVector alpha(hashGridStorage->size());
alpha.setAll(0.0);
cout << "length of alpha vector: " << alpha.getSize() << endl;

int rows = 100;
int cols = 100;

DataMatrix dataSet(rows * cols, dim);
DataVector vals(rows * cols);

// Create a "List" of points where the error should be calculated.
// This represents a regular 2d grid with a step size of 1 / rows and 1 / cols.
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
//xcoord
dataSet.set(i * cols + j, 0, i * 1.0 / rows);
//ycoord
dataSet.set(i * cols + j, 1, j * 1.0 / cols);
vals[i * cols + j] = f(i * 1.0 / rows, j * 1.0 / cols);
}
// create a two-dimensional piecewise bilinear grid
size_t dim = 2;
Grid* grid = Grid::createModLinearGrid(dim);
GridStorage* hashGridStorage = grid->getStorage();
cout << "dimensionality: " << hashGridStorage->dim() << endl;

// create regular grid, level 3
size_t level = 3;
GridGenerator* gridGen = grid->createGridGenerator();
gridGen->regular(level);
cout << "number of grid points: " << hashGridStorage->size() << endl;

// create coefficient vector
DataVector alpha(hashGridStorage->size());
alpha.setAll(0.0);
cout << "length of alpha vector: " << alpha.getSize() << endl;

int rows = 100;
int cols = 100;

DataMatrix dataSet(rows * cols, dim);
DataVector vals(rows * cols);

// Create a "List" of points where the error should be calculated.
// This represents a regular 2d grid with a step size of 1 / rows and 1 / cols.
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
//xcoord
dataSet.set(i * cols + j, 0, i * 1.0 / rows);
//ycoord
dataSet.set(i * cols + j, 1, j * 1.0 / cols);
vals[i * cols + j] = f(i * 1.0 / rows, j * 1.0 / cols);
}
}

//refine adaptively 20 times
for (int step = 0; step < 20; step++) {
// set function values in alpha
DataVector gridPointCoordinates(dim);
//refine adaptively 20 times
for (int step = 0; step < 20; step++) {
// set function values in alpha
DataVector gridPointCoordinates(dim);

for (size_t i = 0; i < hashGridStorage->size(); i++) {
hashGridStorage->get(i)->getCoords(gridPointCoordinates);
alpha[i] = f(gridPointCoordinates[0], gridPointCoordinates[1]);
}
for (size_t i = 0; i < hashGridStorage->size(); i++) {
hashGridStorage->get(i)->getCoords(gridPointCoordinates);
alpha[i] = f(gridPointCoordinates[0], gridPointCoordinates[1]);
}

// hierarchize
SGPP::op_factory::createOperationHierarchisation(*grid)->doHierarchisation(alpha);
// hierarchize
SGPP::op_factory::createOperationHierarchisation(*grid)->doHierarchisation(alpha);

// calculate squared offset
DataVector errorVector(dataSet.getNrows());
calculateError(dataSet, *grid, alpha, errorVector);
// calculate squared offset
DataVector errorVector(dataSet.getNrows());
calculateError(dataSet, *grid, alpha, errorVector);

// refinement stuff
HashRefinement refinement;
PredictiveANOVARefinement decorator(&refinement);
// refinement stuff
HashRefinement refinement;
PredictiveANOVARefinement decorator(&refinement);

// refine a single grid point each time
cout << "Error over all = " << errorVector.sum() << endl;
PredictiveRefinementIndicator indicator(grid, &dataSet, &errorVector, 1);
decorator.free_refine(hashGridStorage, &indicator);
// refine a single grid point each time
cout << "Error over all = " << errorVector.sum() << endl;
PredictiveRefinementIndicator indicator(grid, &dataSet, &errorVector, 1);
decorator.free_refine(hashGridStorage, &indicator);

cout << "Refinement step " << step + 1 << ", new grid size: " << hashGridStorage->size() << endl;
cout << "Refinement step " << step + 1 << ", new grid size: " << hashGridStorage->size() << endl;

// plot grid
// plot grid

// extend alpha vector (new entries uninitialized)
alpha.resize(hashGridStorage->size());
}
// extend alpha vector (new entries uninitialized)
alpha.resize(hashGridStorage->size());
}

delete grid;
delete grid;
}
142 changes: 72 additions & 70 deletions base/examples/c++_simple_predictive_refinement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,91 +25,93 @@ using namespace SGPP::base;

// function to interpolate
SGPP::float_t f(SGPP::float_t x0, SGPP::float_t x1) {
return sin(x0 * M_PI);
return sin(x0 * M_PI);
}

DataVector& calculateError(const DataMatrix& dataSet, Grid& grid, const DataVector& alpha, DataVector& error) {
cout << "calculating error" << endl;

//traverse dataSet
DataVector vec(2);
OperationEval* opEval = SGPP::op_factory::createOperationEval(grid);
for (unsigned int i = 0; i < dataSet.getNrows(); i++) {
dataSet.getRow(i, vec);
error[i] = pow(f(dataSet.get(i, 0), dataSet.get(i, 1)) - opEval->eval(alpha, vec), 2);
}
return error;
cout << "calculating error" << endl;

//traverse dataSet
DataVector vec(2);
OperationEval* opEval = SGPP::op_factory::createOperationEval(grid);

for (unsigned int i = 0; i < dataSet.getNrows(); i++) {
dataSet.getRow(i, vec);
error[i] = pow(f(dataSet.get(i, 0), dataSet.get(i, 1)) - opEval->eval(alpha, vec), 2);
}

return error;
}

int main() {
// create a two-dimensional piecewise bilinear grid
size_t dim = 2;
Grid* grid = Grid::createModLinearGrid(dim);
GridStorage* hashGridStorage = grid->getStorage();
cout << "dimensionality: " << hashGridStorage->dim() << endl;

// create regular grid, level 3
size_t level = 1;
GridGenerator* gridGen = grid->createGridGenerator();
gridGen->regular(level);
cout << "number of initial grid points: " << hashGridStorage->size() << endl;

// create coefficient vector
DataVector alpha(hashGridStorage->size());
alpha.setAll(0.0);
cout << "length of alpha vector: " << alpha.getSize() << endl;

int rows = 100;
int cols = 100;

DataMatrix dataSet(rows * cols, dim);
DataVector vals(rows * cols);

// Create a "List" of points where the error should be calculated.
// This represents a regular 2d grid with a step size of 1 / rows and 1 / cols.
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
//xcoord
dataSet.set(i * cols + j, 0, i * 1.0 / rows);
//ycoord
dataSet.set(i * cols + j, 1, j * 1.0 / cols);
vals[i * cols + j] = f(i * 1.0 / rows, j * 1.0 / cols);
}
// create a two-dimensional piecewise bilinear grid
size_t dim = 2;
Grid* grid = Grid::createModLinearGrid(dim);
GridStorage* hashGridStorage = grid->getStorage();
cout << "dimensionality: " << hashGridStorage->dim() << endl;

// create regular grid, level 3
size_t level = 1;
GridGenerator* gridGen = grid->createGridGenerator();
gridGen->regular(level);
cout << "number of initial grid points: " << hashGridStorage->size() << endl;

// create coefficient vector
DataVector alpha(hashGridStorage->size());
alpha.setAll(0.0);
cout << "length of alpha vector: " << alpha.getSize() << endl;

int rows = 100;
int cols = 100;

DataMatrix dataSet(rows * cols, dim);
DataVector vals(rows * cols);

// Create a "List" of points where the error should be calculated.
// This represents a regular 2d grid with a step size of 1 / rows and 1 / cols.
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
//xcoord
dataSet.set(i * cols + j, 0, i * 1.0 / rows);
//ycoord
dataSet.set(i * cols + j, 1, j * 1.0 / cols);
vals[i * cols + j] = f(i * 1.0 / rows, j * 1.0 / cols);
}
}

//refine adaptively 20 times
for (int step = 0; step < 20; step++) {
// set function values in alpha
DataVector gridPointCoordinates(dim);
//refine adaptively 20 times
for (int step = 0; step < 20; step++) {
// set function values in alpha
DataVector gridPointCoordinates(dim);

for (size_t i = 0; i < hashGridStorage->size(); i++) {
hashGridStorage->get(i)->getCoords(gridPointCoordinates);
alpha[i] = f(gridPointCoordinates[0], gridPointCoordinates[1]);
}
for (size_t i = 0; i < hashGridStorage->size(); i++) {
hashGridStorage->get(i)->getCoords(gridPointCoordinates);
alpha[i] = f(gridPointCoordinates[0], gridPointCoordinates[1]);
}

// hierarchize
SGPP::op_factory::createOperationHierarchisation(*grid)->doHierarchisation(alpha);
// hierarchize
SGPP::op_factory::createOperationHierarchisation(*grid)->doHierarchisation(alpha);

// calculate squared offset
DataVector errorVector(dataSet.getNrows());
calculateError(dataSet, *grid, alpha, errorVector);
// calculate squared offset
DataVector errorVector(dataSet.getNrows());
calculateError(dataSet, *grid, alpha, errorVector);

// refinement stuff
HashRefinement refinement;
PredictiveRefinement decorator(&refinement);
// refinement stuff
HashRefinement refinement;
PredictiveRefinement decorator(&refinement);

// refine a single grid point each time
cout << "Error over all = " << errorVector.sum() << endl;
PredictiveRefinementIndicator indicator(grid, &dataSet, &errorVector, 1);
decorator.free_refine(hashGridStorage, &indicator);
// refine a single grid point each time
cout << "Error over all = " << errorVector.sum() << endl;
PredictiveRefinementIndicator indicator(grid, &dataSet, &errorVector, 1);
decorator.free_refine(hashGridStorage, &indicator);

cout << "Refinement step " << step + 1 << ", new grid size: " << hashGridStorage->size() << endl;
cout << "Refinement step " << step + 1 << ", new grid size: " << hashGridStorage->size() << endl;

// plot grid
// plot grid

// extend alpha vector (new entries uninitialized)
alpha.resize(hashGridStorage->size());
}
// extend alpha vector (new entries uninitialized)
alpha.resize(hashGridStorage->size());
}

delete grid;
delete grid;
}
Loading

0 comments on commit e8e4d84

Please sign in to comment.