#include <functional>
#include <random>
#include <vector>
#ifdef USE_CGAL
#include <CGAL/MP_Float.h>
#include <CGAL/QP_functions.h>
#include <CGAL/QP_models.h>
#include <CGAL/QP_solution.h>
#include <CGAL/basic.h>
typedef CGAL::MP_Float ET;
typedef CGAL::Quadratic_program_solution<ET> Solution;
typedef CGAL::Quadratic_program_from_iterators<
double**,
double*,
CGAL::Const_oneset_iterator<CGAL::Comparison_result>,
bool*,
double*,
bool*,
double*,
double**,
double*>
Program;
#endif
void calc_residual(Grid& grid, DataMatrix& samples, double lambda, DataVector& alpha,
DataVector& result) {
size_t numSamples = samples.getNrows();
size_t storage_size = grid.getStorage().getSize();
DensitySystemMatrix AlambC(A_op, B_op, C_op, lambda, numSamples);
DataVector
b(storage_size);
AlambC.mult(alpha, result);
}
void randu(DataVector& rvar, std::mt19937& generator) {
std::normal_distribution<double> distribution(0.5, 0.1);
for (
size_t j = 0;
j < rvar.getSize(); ++
j) {
rvar[
j] = distribution(generator);
while (rvar[
j] < 0 || rvar[
j] > 1) rvar[
j] = distribution(generator);
}
}
void createSamples(DataMatrix& rvar, std::uint64_t seedValue = std::mt19937_64::default_seed) {
size_t nsamples = rvar.getNrows(), ndim = rvar.getNcols();
std::mt19937 generator(seedValue);
DataVector sample(ndim);
for (
size_t i = 0; i < nsamples; ++
i) {
randu(sample, generator);
rvar.setRow(i, sample);
}
}
void checkPositive(Grid& grid, const DataVector& alpha) {
size_t storage_size = gridStorage->
getSize();
size_t dims = grid.getDimension();
DataVector coords(dims);
for (size_t i = 0; i < storage_size; i++) {
gp = gridStorage->getPoint(i);
gp.getStandardCoordinates(coords);
if (op_eval->eval(alpha, coords) < 0) {
std::cout << "f(" << coords.toString() << ")=" << op_eval->eval(alpha, coords) << std::endl;
}
}
}
#ifdef USE_CGAL
void solve_cgal(Grid& grid, DataMatrix& samples, double lambda, DataVector& result) {
size_t storage_size = gridStorage->
getSize();
std::cout << "storage size:" << storage_size << std::endl;
size_t numSamples = samples.getNrows();
size_t dims = samples.getNcols();
DataMatrix M(storage_size, storage_size);
DataMatrix
C(storage_size, storage_size);
DataVector
b(storage_size);
DataVector q(storage_size);
DensitySystemMatrix AlambC(A_op, B_op, C_op, lambda, numSamples);
q.mult(-1);
double** P_it = new double*[storage_size];
for (size_t i = 0; i < storage_size; i++) {
P_it[
i] =
new double[storage_size];
}
for (size_t i = 0; i < storage_size; i++) {
DataVector col(storage_size);
DataVector
tmp(storage_size);
M.getColumn(i, col);
M.mult(col, tmp);
P_it[
i] =
new double[storage_size];
for (
size_t j = 0;
j <=
i;
j++) {
}
}
for (size_t i = 0; i < storage_size; i++) {
for (
size_t j = 0;
j < storage_size;
j++) {
std::cout << P_it[
j][
i] <<
" ";
}
std::cout << std::endl;
}
std::cout << "---------------" << std::endl;
DataMatrix gridPoints(storage_size, dims);
for (size_t i = 0; i < storage_size; i++) {
gp = gridStorage->getPoint(i);
DataVector coords(dims);
gp.getStandardCoordinates(coords);
gridPoints.setRow(i, coords);
}
double** G_it = new double*[storage_size];
for (size_t i = 0; i < storage_size; i++) {
G_it[
i] =
new double[storage_size];
}
for (size_t i = 0; i < storage_size; i++) {
DataVector tmp_result(storage_size);
DataVector
alpha(storage_size, 0.0);
alpha.set(i, 1.0);
G_op->mult(alpha, tmp_result);
for (
size_t j = 0;
j < storage_size;
j++) {
G_it[
i][
j] = tmp_result.get(
j);
}
}
for (size_t i = 0; i < storage_size; i++) {
for (
size_t j = 0;
j < storage_size;
j++) {
std::cout << G_it[
j][
i] <<
" ";
}
std::cout << std::endl;
}
std::cout << q.toString() << std::endl;
CGAL::Const_oneset_iterator<CGAL::Comparison_result>
r(CGAL::LARGER);
bool* bounded = new bool[storage_size];
for (size_t i = 0; i < storage_size; i++) {
}
DataVector bounds(storage_size, 0.0);
Program qp(static_cast<int>(storage_size), static_cast<int>(storage_size),
P_it, q.getPointer());
Solution
s = CGAL::solve_quadratic_program(qp, ET());
Solution::Variable_value_iterator it = s.variable_values_begin();
for (size_t i = 0; i < storage_size; i++) {
result.set(i, to_double(*it));
it++;
}
std::cout << "objective function:" << to_double(s.objective_value()) << std::endl;
for (size_t i = 0; i < storage_size; i++) {
}
delete P_it;
delete G_it;
delete bounded;
}
#endif
int main(
int argc,
char** argv) {
size_t d = 2;
int level = 3;
gridConfig.
type_ = gridType;
DataMatrix trainSamples(1000, d);
createSamples(trainSamples, 1234567);
grid->getGenerator().regular(gridConfig.
level_);
size_t storage_size = grid->getStorage().getSize();
DataVector result(storage_size);
#ifdef USE_CGAL
double lambda = 0.0;
solve_cgal(*grid, trainSamples, lambda, result);
std::cout << "Best alpha:" << result.toString() << std::endl;
DataVector residual(storage_size);
calc_residual(*grid, trainSamples, lambda, result, residual);
std::cout << "residual:" << residual.l2Norm() << std::endl;
double al[17] = {18.90769281, -9.09911171, -9.0176581, 0.10785147, -1.18044529, -0.28708884,
0.15533845, -9.38380473, -9.00915268, -0.02802101, -2.24950203, -1.58041974,
-0.09580067, 5.12791209, 4.54942521, 4.66378412, 4.32550775};
DataVector cmp_alpha(al, 17);
std::cout << "compare residual:" << residual.l2Norm() << std::endl;
std::cout << "Non positive best_alpha:" << std::endl;
std::cout << "Non positive cmp_alpha:" << std::endl;
#endif
return 0;
}