bitrl & cuberl Documentation
Simulation engine for reinforcement learning agents
Loading...
Searching...
No Matches
lp_metric_impl.h
Go to the documentation of this file.
1#ifndef LP_METRIC_IMPL_H
2#define LP_METRIC_IMPL_H
3
4#include <cmath>
5
6namespace cuberl{
7namespace maths {
8
9template<int P, bool TTakeRoot>
11
12//the general implementation
13template<int P, bool TTakeRoot>
16
17 real_t sum = 0.0;
18 for (uint_t i = 0; i < v1.size(); i++){
19 sum += std::pow(std::fabs(v1[i] - v2[i]), P);
20 }
21
22 if (!TTakeRoot) // The compiler should optimize this correctly at compile-time.
23 return sum;
24
25 return std::pow(sum, (1.0 / P));
26}
27
28//the general implementation
29template<int P, bool TTakeRoot>
31LpMetric<P,TTakeRoot>::evaluate(const std::vector<real_t>& v1, const std::vector<real_t>& v2){
32
33 real_t sum = 0.0;
34 for (uint_t i = 0; i < v1.size(); i++){
35 sum += std::pow(std::fabs(v1[i] - v2[i]), P);
36 }
37
38 if (!TTakeRoot) // The compiler should optimize this correctly at compile-time.
39 return sum;
40
41 return std::pow(sum, (1.0 / P));
42}
43
44// L1-metric specializations; the root doesn't matter.
45template<>
48 return blaze::l1Norm(v1 - v2);
49}
50
51template<>
54 return blaze::l1Norm(v1 - v2);
55}
56
57// L2-metric specializations.
58template<>
61 return std::sqrt(blaze::sqrNorm(v1 - v2));
62}
63
64template<>
67 return blaze::sqrNorm(v1 - v2);
68}
69
70// L3-metric specialization (not very likely to be used, but just in case).
71template<>
74 return std::pow(blaze::reduce(blaze::pow(blaze::abs(v1 - v2), 3.0), blaze::Add()), 1.0 / 3.0);
75}
76
77template<>
80 return std::pow(blaze::reduce(blaze::pow(blaze::abs(v1 - v2), 3.0), blaze::Add()), 1.0 / 3.0);
81}
82
83
84template<int P, bool TTakeRoot>
89
90template<>
92LpMetric<1, true>::evaluate(const std::vector<real_t>& v1, const std::vector<real_t>& v2){
93
94 real_t sum = 0.0;
95
96 for(uint_t i=0; i<v1.size(); ++i){
97 sum += std::fabs(v1[i] - v2[i]);
98 }
99
100 return sum;
101
102}
103
104template<>
105real_t
106LpMetric<1, false>::evaluate(const std::vector<real_t>& v1, const std::vector<real_t>& v2){
107
108 return LpMetric<1, true>::evaluate(v1 , v2);
109}
110
111template<>
112real_t
113LpMetric<2, true>::evaluate(const std::vector<real_t>& v1, const std::vector<real_t>& v2){
114
115 real_t sum = 0.0;
116
117 for(uint_t i=0; i<v1.size(); ++i){
118 sum += (v1[i] - v2[i]) * (v1[i] - v2[i]);
119 }
120
121 return std::sqrt(sum);
122
123}
124
125template<>
126real_t
127LpMetric<2, false>::evaluate(const std::vector<real_t>& v1, const std::vector<real_t>& v2){
128
129 real_t sum = 0.0;
130
131 for(uint_t i=0; i<v1.size(); ++i){
132 sum += (v1[i] - v2[i]) * (v1[i] - v2[i]);
133 }
134
135 return sum;
136}
137
138}
139}
140#endif // LP_METRIC_IMPL_H
static real_t tolerance_value
Definition lp_metric.h:22
real_t operator()(const DynVec< real_t > &v1, const DynVec< real_t > &v2) const
Overload operator()
Definition lp_metric_impl.h:86
static real_t evaluate(const DynVec< real_t > &v1, const DynVec< real_t > &v2)
evaluate
Definition lp_metric_impl.h:15
double real_t
real_t
Definition bitrl_types.h:23
Eigen::RowVectorX< T > DynVec
Dynamically sized row vector.
Definition bitrl_types.h:74
std::size_t uint_t
uint_t
Definition bitrl_types.h:43
std::iterator_traits< IteratorType >::value_type sum(IteratorType begin, IteratorType end, bool parallel=true)
Definition vector_math.h:98
Various utilities used when working with RL problems.
Definition cuberl_types.h:16