SG++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
constrainedOptimization.cpp

This example can be found under optimization/examples/constrainedOptimization.cpp.

// Copyright (C) 2008-today The SG++ project
// This file is part of the SG++ project. For conditions of distribution and
// use, please see the copyright notice provided with SG++ or at
// sgpp.sparsegrids.org
#include <sgpp_base.hpp>
#include <iostream>
int main() {
std::cout << "Hello World!\n";
const size_t d = problem.getObjectiveFunction().getNumberOfParameters();
/*const size_t d = 6;
sgpp::optimization::test_problems::ProductOnSphere problem(d);*/
/*sgpp::optimization::WrapperScalarFunctionGradient fGradient(
d, [d](const sgpp::base::DataVector & x,
sgpp::base::DataVector & gradient) {
double fx = -1.0;
gradient.setAll(-1.0);
for (size_t t = 0; t < d; t++) {
for (size_t t2 = 0; t2 < d; t2++) {
if (t2 != t) {
gradient[t2] *= x[t];
} else {
fx *= x[t];
}
}
}
return fx;
});
sgpp::optimization::WrapperVectorFunctionGradient gGradient(
d, 1, [d](const sgpp::base::DataVector & x,
sgpp::base::DataVector & value,
sgpp::base::DataMatrix & gradient) {
double gx = -1.0;
for (size_t t = 0; t < d; t++) {
gx += x[t] * x[t];
gradient(0, t) = 2.0 * x[t];
}
value[0] = gx;
});
sgpp::optimization::VectorFunctionGradient& hGradient =
sgpp::optimization::EmptyVectorFunctionGradient::getInstance();*/
const size_t mG = g.getNumberOfComponents();
const size_t mH = h.getNumberOfComponents();
const size_t p = 5;
std::unique_ptr<sgpp::base::Grid> grid(
const size_t l = 5;
grid->getGenerator().regular(l);
sgpp::base::GridStorage& gridStorage = grid->getStorage();
const size_t N = grid->getSize();
std::unique_ptr<sgpp::optimization::OperationMultipleHierarchisation> hierOp(
sgpp::base::DataVector x(d), gx(mG), hx(mH);
sgpp::base::DataMatrix gAlpha(N, mG);
sgpp::base::DataMatrix hAlpha(N, mH);
for (size_t i = 0; i < N; i++) {
x = gridStorage.getCoordinates(gridStorage[i]);
fAlpha[i] = f.eval(x);
g.eval(x, gx);
gAlpha.setRow(i, gx);
h.eval(x, hx);
hAlpha.setRow(i, hx);
}
hierOp->doHierarchisation(fAlpha);
hierOp->doHierarchisation(gAlpha);
hierOp->doHierarchisation(hAlpha);
*grid, fAlpha);
*grid, gAlpha);
*grid, hAlpha);
ft, ftGradient, gt, gtGradient, ht, htGradient);
/*sgpp::optimization::optimizer::AugmentedLagrangian optimizer(
f, fGradient, g, gGradient, h, hGradient);*/
optimizer.setN(10000);
const sgpp::base::DataVector x0(optimizer.findFeasiblePoint());
// const sgpp::base::DataVector x0(d, 0.2);
optimizer.setStartingPoint(x0);
optimizer.optimize();
problem.getOptimalPoint(xOpt);
const sgpp::base::DataVector xOptStar(optimizer.getOptimalPoint());
for (const sgpp::base::DataVector& x : {
xOpt, x0, xOptStar
}) {
const double fx = f.eval(x);
g.eval(x, gx);
h.eval(x, hx);
const double ftx = ft.eval(x);
gt.eval(x, gtx);
ht.eval(x, htx);
std::cout << "x = " << x.toString() << "\n";
std::cout << "f = " << fx << ", g = " << gx.toString() << ", h = " <<
hx.toString() << "\n";
std::cout << "ft = " << ftx << ", gt = " << gtx.toString() << ", ht = " <<
htx.toString() << "\n";
std::cout << "\n";
}
return 0;
}