forked from GeomScale/volesti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoracle_autodiff_functors.hpp
94 lines (75 loc) · 3.23 KB
/
oracle_autodiff_functors.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// VolEsti (volume computation and sampling library)
// Copyright (c) 2012-2024 Vissarion Fisikopoulos
// Copyright (c) 2018-2020 Apostolos Chalkis
// Licensed under GNU LGPL.3, see LICENCE file
#ifndef ODE_SOLVERS_ORACLE_AUTODIFF_FUNCTORS_HPP
#define ODE_SOLVERS_ORACLE_AUTODIFF_FUNCTORS_HPP
#include "Eigen/Eigen"
#include <autodiff/forward/real.hpp>
#include <autodiff/forward/real/eigen.hpp>
#include "cartesian_geom/cartesian_kernel.h"
#include "cartesian_geom/autopoint.h"
struct AutoDiffFunctor {
template <typename NT>
struct parameters {
unsigned int order;
NT L; // Lipschitz constant for gradient
NT m; // Strong convexity constant
NT kappa; // Condition number
Eigen::Matrix<NT, Eigen::Dynamic, Eigen::Dynamic> data;
parameters() : order(2), L(4), m(4), kappa(1){};
};
template <typename NT>
struct FunctionFunctor_internal {
using Autopoint = autopoint<NT>;
using Coeff = typename autopoint<NT>::Coeff;
using FT = typename autopoint<NT>::FT;
using Point = typename Cartesian<NT>::Point;
static std::function<FT(const Autopoint &, const Eigen::Matrix<NT, Eigen::Dynamic, Eigen::Dynamic> &)> pdf;
FT static result_internal(const Coeff &x, const Eigen::Matrix<NT, Eigen::Dynamic, Eigen::Dynamic> &data){
return pdf(x, data); //
}
// external interface
Point static differentiate(Point const &x0, const Eigen::Matrix<NT, Eigen::Dynamic, Eigen::Dynamic> &data) {
Autopoint x = Autopoint(x0.getCoefficients()); // cast into autopoint
auto x1 = x.getCoefficients();
Coeff y = autodiff::gradient(result_internal, autodiff::wrt(x1), autodiff::at(x1, data));
auto result = y.template cast<NT>();
return -1 * Point(result);
}
NT static result(Point const &x0, const Eigen::Matrix<NT, Eigen::Dynamic, Eigen::Dynamic> &data) {
Autopoint x = Autopoint(x0.getCoefficients()); // cast to autopoint
auto x1 = x.getCoefficients();
return result_internal(x1, data).val();
}
};
template <typename Point>
struct GradientFunctor {
using NT = typename Point::FT;
FunctionFunctor_internal<NT> F;
parameters<NT> ¶ms;
GradientFunctor(parameters<NT> ¶ms_) : params(params_){};
// The index i represents the state vector index
Point operator()(unsigned int const &i, std::vector<Point> const &xs, NT const &t) const {
// std::cout<<"calling gradient functor"<<std::flush;
if (i == params.order - 1) {
return F.differentiate(xs[0], params.data);
}
else {
return xs[i + 1]; // returns derivative
}
}
};
template <typename Point>
struct FunctionFunctor {
using NT = typename Point::FT;
parameters<NT> ¶ms;
FunctionFunctor(parameters<NT> ¶ms_) : params(params_){};
// The index i represents the state vector index
FunctionFunctor_internal<NT> F;
NT operator()(Point const &x) const {
return F.result(x, params.data);
}
};
};
#endif //ODE_SOLVERS_ORACLE_AUTODIFF_FUNCTORS_HPP