12 #ifndef PLSSVM_PARAMETER_HPP_
13 #define PLSSVM_PARAMETER_HPP_
22 #include "fmt/ostream.h"
23 #include "igor/igor.hpp"
26 #include <string_view>
27 #include <type_traits>
35 IGOR_MAKE_NAMED_ARGUMENT(kernel_type);
37 IGOR_MAKE_NAMED_ARGUMENT(gamma);
39 IGOR_MAKE_NAMED_ARGUMENT(degree);
41 IGOR_MAKE_NAMED_ARGUMENT(coef0);
43 IGOR_MAKE_NAMED_ARGUMENT(cost);
45 IGOR_MAKE_NAMED_ARGUMENT(epsilon);
47 IGOR_MAKE_NAMED_ARGUMENT(max_iter);
49 IGOR_MAKE_NAMED_ARGUMENT(sycl_implementation_type);
51 IGOR_MAKE_NAMED_ARGUMENT(sycl_kernel_invocation_type);
59 template <
typename... Args>
65 template <
typename... Args>
71 template <
typename... Args>
72 constexpr
bool has_only_sycl_parameter_named_args_v = !igor::has_other_than<Args...>(plssvm::kernel_type, plssvm::gamma, plssvm::degree, plssvm::coef0, plssvm::cost, plssvm::sycl_implementation_type, plssvm::sycl_kernel_invocation_type);
83 template <
typename ExpectedType,
typename IgorParser,
typename NamedArgType>
87 if constexpr (is_default_value_v<parsed_named_arg_type>) {
88 static_assert(std::is_convertible_v<typename parsed_named_arg_type::value_type, ExpectedType>,
"Cannot convert the wrapped default value to the expected type!");
90 return static_cast<ExpectedType
>(parser(named_arg).value());
91 }
else if constexpr (std::is_convertible_v<parsed_named_arg_type, ExpectedType>) {
93 return static_cast<ExpectedType
>(parser(named_arg));
95 static_assert(detail::always_false_v<ExpectedType>,
"The named parameter must be of type plssvm::default_value or a fundamental type!");
105 template <
typename T>
108 static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>,
"The template type can only be 'float' or 'double'!");
140 template <
typename... Args,
PLSSVM_REQUIRES(has_only_named_args_v<Args...>)>
151 template <
typename... Args,
PLSSVM_REQUIRES(has_only_named_args_v<Args...>)>
152 constexpr
explicit parameter(Args &&...named_args) noexcept {
172 template <
typename U>
174 if constexpr (std::is_same_v<U, real_type>) {
200 return cost == other.cost;
202 return degree == other.degree &&
gamma == other.gamma &&
coef0 == other.coef0 &&
cost == other.cost;
204 return gamma == other.gamma &&
cost == other.cost;
215 template <
typename... Args>
217 igor::parser parser{ std::forward<Args>(named_args)... };
220 static_assert(!parser.has_unnamed_arguments(),
"Can only use named parameter!");
222 static_assert(!parser.has_duplicates(),
"Can only use each named parameter once!");
224 static_assert(!parser.has_other_than(plssvm::kernel_type, plssvm::gamma, plssvm::degree, plssvm::coef0, plssvm::cost, plssvm::sycl_implementation_type, plssvm::sycl_kernel_invocation_type),
225 "An illegal named parameter has been passed!");
228 [[maybe_unused]]
const auto print_warning = [](
const std::string_view param_name,
const kernel_function_type kernel) {
233 if constexpr (parser.has(plssvm::kernel_type)) {
235 kernel_type = get_value_from_named_parameter<typename decltype(kernel_type)::value_type>(parser, plssvm::kernel_type);
237 if constexpr (parser.has(plssvm::gamma)) {
239 gamma = get_value_from_named_parameter<typename decltype(gamma)::value_type>(parser, plssvm::gamma);
245 if constexpr (parser.has(plssvm::degree)) {
247 degree = get_value_from_named_parameter<typename decltype(degree)::value_type>(parser, plssvm::degree);
253 if constexpr (parser.has(plssvm::coef0)) {
255 coef0 = get_value_from_named_parameter<typename decltype(coef0)::value_type>(parser, plssvm::coef0);
261 if constexpr (parser.has(plssvm::cost)) {
263 cost = get_value_from_named_parameter<typename decltype(cost)::value_type>(parser, plssvm::cost);
279 template <
typename T>
281 return lhs.kernel_type == rhs.kernel_type && lhs.degree == rhs.degree && lhs.gamma == rhs.gamma && lhs.coef0 == rhs.coef0 && lhs.cost == rhs.cost;
291 template <
typename T>
293 return !(lhs == rhs);
305 template <
typename T>
307 return lhs.equivalent(rhs);
317 template <
typename T>
constexpr const value_type & value() const noexcept
Get the currently active value: the user provided value if provided, otherwise the default value is r...
Definition: default_value.hpp:150
Implements a class used to be able to distinguish between the default value of a variable and an assi...
Defines universal utility functions.
Defines an enumeration holding all possible kernel function types.
std::remove_cv_t< std::remove_reference_t< T > > remove_cvref_t
Remove the topmost reference- and cv-qualifiers.
Definition: type_traits.hpp:46
constexpr bool has_only_named_args_v
Trait to check whether Args only contains named-parameter.
Definition: parameter.hpp:60
constexpr bool operator!=(const parameter< T > &lhs, const parameter< T > &rhs) noexcept
Compares the two parameter sets lhs and rhs for inequality.
Definition: parameter.hpp:292
ExpectedType get_value_from_named_parameter(const IgorParser &parser, const NamedArgType &named_arg)
Parse the value hold be named_arg and return it converted to the ExpectedType.
Definition: parameter.hpp:84
void unreachable()
Invokes undefined behavior. Used to mark code paths that may never be reachable.
Definition: utility.hpp:32
constexpr bool equivalent(const parameter< T > &lhs, const parameter< T > &rhs) noexcept
Checks whether the two parameter sets lhs and rhs are equivalent.
Definition: parameter.hpp:306
constexpr bool has_only_parameter_named_args_v
Trait to check whether Args only contains named-parameter that can be used to initialize a plssvm::pa...
Definition: parameter.hpp:66
std::ostream & operator<<(std::ostream &out, const execution_range &range)
Output the execution range to the given output-stream out.
constexpr bool operator==(const parameter< T > &lhs, const parameter< T > &rhs) noexcept
Compares the two parameter sets lhs and rhs for equality.
Definition: parameter.hpp:280
constexpr bool has_only_sycl_parameter_named_args_v
Trait to check whether Args only contains named-parameter that can be used to initialize a plssvm::pa...
Definition: parameter.hpp:72
The main namespace containing all public API functions.
Definition: backend_types.hpp:24
kernel_function_type
Enum class for all implemented kernel functions.
Definition: kernel_function_types.hpp:31
constexpr bool equivalent(const parameter &lhs, const parameter &rhs) noexcept
Checks whether the two parameter sets lhs and rhs are equivalent.
Definition: parameter.hpp:333
std::string_view kernel_function_type_to_math_string(kernel_function_type kernel) noexcept
Return the mathematical representation of the kernel_type kernel.
This class denotes an explicit default value initialization used to distinguish between the default v...
Definition: default_value.hpp:37
Class for encapsulating all important C-SVM parameters.
Definition: parameter.hpp:106
default_value< real_type > cost
The cost parameter in the C-SVM.
Definition: parameter.hpp:165
constexpr parameter(const parameter ¶ms, Args &&...named_args)
Construct a parameter by using the values in params and overwriting all values using the provided nam...
Definition: parameter.hpp:141
default_value< real_type > coef0
The coef0 parameter used in the polynomial kernel function.
Definition: parameter.hpp:163
constexpr parameter(Args &&...named_args) noexcept
Construct a parameter set by overwriting the SVM parameters' default values that are provided using n...
Definition: parameter.hpp:152
default_value< int > degree
The degree parameter used in the polynomial kernel function.
Definition: parameter.hpp:159
void set_named_arguments(Args &&...named_args)
Overwrite the default values of this parameter object with the potential provided named-parameters na...
Definition: parameter.hpp:216
T real_type
The type of the data. Must be either float or double.
Definition: parameter.hpp:111
constexpr bool equivalent(const parameter &other) const noexcept
Checks whether the current parameter set is equivalent to the one given by other.
Definition: parameter.hpp:191
default_value< kernel_function_type > kernel_type
The used kernel function: linear, polynomial, or radial basis functions (rbf).
Definition: parameter.hpp:157
constexpr parameter() noexcept=default
Default construct a parameter set, i.e., each SVM parameter has its default value.
default_value< real_type > gamma
The gamma parameter used in the polynomial and rbf kernel functions.
Definition: parameter.hpp:161
Defines some generic type traits used in the PLSSVM library.
#define PLSSVM_REQUIRES(...)
A shorthand macro for the std::enable_if_t type trait.
Definition: type_traits.hpp:33