AIToolbox
A library that offers tools for AI problem solving.
TypeTraits.hpp
Go to the documentation of this file.
1 #ifndef AI_TOOLBOX_TYPE_TRAITS_HEADER_FILE
2 #define AI_TOOLBOX_TYPE_TRAITS_HEADER_FILE
3 
4 #include <type_traits>
5 #include <utility>
6 #include <concepts>
7 #include <cstddef>
8 
9 #include <Eigen/Core>
10 
11 namespace AIToolbox {
15  template <typename CopiedType, typename ConstReference>
16  struct copy_const {
17  using type = typename std::conditional_t<std::is_const_v<ConstReference>,
18  std::add_const_t<CopiedType>,
19  std::remove_const_t<CopiedType>>;
20  };
21  template <typename CopiedType, typename ConstReference>
23 
27  template <typename A, typename B>
28  concept different_from = !std::same_as<A, B>;
29 
33  template <typename M>
34  concept IsNaive2DMatrix = requires (M m) {
35  { m[0][0] } -> std::convertible_to<double>;
36  };
37 
41  template <typename M>
42  concept IsNaive3DMatrix = requires (M m) {
43  { m[0][0][0] } -> std::convertible_to<double>;
44  };
45 
49  template <typename T>
50  concept IsNaive2DTable = requires (T t) {
51  { t[0][0] } -> std::convertible_to<long unsigned>;
52  };
53 
57  template <typename T>
58  concept IsNaive3DTable = requires (T t) {
59  { t[0][0][0] } -> std::convertible_to<long unsigned>;
60  };
61 
65  template <typename T>
66  concept IsDerivedFromEigen = std::derived_from<T, Eigen::EigenBase<T>>;
67 
68  // #############################################
69  // ######## Generic modeling concepts ##########
70  // #############################################
71 
75  template <typename M>
76  concept HasStateSpace = requires(const M m) {
77  { m.getS() } -> different_from<void>;
78  };
79 
83  template <typename M>
84  concept HasFixedActionSpace = requires(const M m) {
85  { m.getA() } -> different_from<void>;
86  };
87 
91  template <typename M>
92  concept HasVariableActionSpace = requires(const M m) {
93  { m.getA(m.getS()) } -> different_from<void>;
94  };
95 
99  template <typename M>
100  concept HasActionSpace = HasFixedActionSpace<M> || HasVariableActionSpace<M>;
101 
105  template <typename M>
106  concept HasObservationSpace = requires(const M m) {
107  { m.getO() } -> different_from<void>;
108  };
109 
113  template <typename M>
114  concept HasIntegralStateSpace = requires(const M m) {
115  { m.getS() } -> std::same_as<size_t>;
116  };
117 
121  template <typename M>
122  concept HasIntegralActionSpace = requires(const M m) {
123  requires
124  (HasFixedActionSpace<M> && requires {{ m.getA() } -> std::same_as<size_t>; }) ||
125  (HasVariableActionSpace<M> && requires {{ m.getA(m.getS()) } -> std::same_as<size_t>; });
126  };
127 
131  template <typename M>
132  concept HasIntegralObservationSpace = requires(const M m) {
133  { m.getO() } -> std::same_as<size_t>;
134  };
135 
158  template <typename M>
159  concept IsGenerativeModel = HasStateSpace<M> && HasActionSpace<M> && requires(const M m) {
160  { m.getDiscount() } -> std::convertible_to<double>;
161  { m.isTerminal(m.getS()) } -> std::convertible_to<bool>;
162 
163  requires
164  (HasFixedActionSpace<M> && requires {{ m.sampleSR(m.getS(), m.getA()) } -> std::convertible_to<std::tuple<std::remove_cvref_t<decltype(m.getS())>, double>>; }) ||
165  (HasVariableActionSpace<M> && requires {{ m.sampleSR(m.getS(), m.getA(m.getS())) } -> std::convertible_to<std::tuple<std::remove_cvref_t<decltype(m.getS())>, double>>; });
166  };
167 }
168 
169 #endif
AIToolbox::copy_const
This struct is used to copy constness from one type to another.
Definition: TypeTraits.hpp:16
AIToolbox::HasIntegralActionSpace
concept HasIntegralActionSpace
This concept checks that getA returns size_t.
Definition: TypeTraits.hpp:122
AIToolbox::different_from
concept different_from
This concept simplifies checking for non-void.
Definition: TypeTraits.hpp:28
AIToolbox::IsNaive2DTable
concept IsNaive2DTable
This concept checks for a simple 2D accessible table.
Definition: TypeTraits.hpp:50
AIToolbox::IsNaive3DTable
concept IsNaive3DTable
This concept checks for a simple 3D accessible table.
Definition: TypeTraits.hpp:58
AIToolbox::copy_const_t
typename copy_const< CopiedType, ConstReference >::type copy_const_t
Definition: TypeTraits.hpp:22
AIToolbox::HasActionSpace
concept HasActionSpace
This concept checks that some form of getA exists.
Definition: TypeTraits.hpp:100
AIToolbox::IsNaive2DMatrix
concept IsNaive2DMatrix
This concept checks for a simple 2D accessible matrix.
Definition: TypeTraits.hpp:34
AIToolbox
Definition: Experience.hpp:6
AIToolbox::IsDerivedFromEigen
concept IsDerivedFromEigen
This concept simplifies checking for non-void.
Definition: TypeTraits.hpp:66
AIToolbox::HasObservationSpace
concept HasObservationSpace
This concept checks that getO() exists.
Definition: TypeTraits.hpp:106
AIToolbox::HasFixedActionSpace
concept HasFixedActionSpace
This concept checks that getA() exists.
Definition: TypeTraits.hpp:84
AIToolbox::HasStateSpace
concept HasStateSpace
This concept checks that getS() exists.
Definition: TypeTraits.hpp:76
AIToolbox::IsNaive3DMatrix
concept IsNaive3DMatrix
This concept checks for a simple 3D accessible matrix.
Definition: TypeTraits.hpp:42
AIToolbox::IsGenerativeModel
concept IsGenerativeModel
This concept checks the minimum requirements for a generative model.
Definition: TypeTraits.hpp:159
AIToolbox::HasIntegralStateSpace
concept HasIntegralStateSpace
This concept checks that getS() returns size_t.
Definition: TypeTraits.hpp:114
AIToolbox::HasIntegralObservationSpace
concept HasIntegralObservationSpace
This concept checks that getO() returns size_t.
Definition: TypeTraits.hpp:132
AIToolbox::copy_const::type
typename std::conditional_t< std::is_const_v< ConstReference >, std::add_const_t< CopiedType >, std::remove_const_t< CopiedType > > type
Definition: TypeTraits.hpp:19
AIToolbox::HasVariableActionSpace
concept HasVariableActionSpace
This concept checks that getA(state) exists.
Definition: TypeTraits.hpp:92