AutoPas  3.0.0
Loading...
Searching...
No Matches
FuzzySet.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <functional>
10#include <map>
11#include <memory>
12#include <optional>
13#include <string>
14#include <variant>
15
16#include "CrispSet.h"
19
20namespace autopas::FuzzyLogic {
21
25class DefuzzificationMethodOption : public Option<DefuzzificationMethodOption> {
26 public:
30 enum Value {
38 MoM
39 };
40
45
50 constexpr DefuzzificationMethodOption(Value option) : _value(option) {}
51
56 constexpr operator Value() const { return _value; }
57
62 static std::map<DefuzzificationMethodOption, std::string> getOptionNames() {
63 return {
64 {DefuzzificationMethodOption::CoG, "centerOfGravity"},
65 {DefuzzificationMethodOption::MoM, "meanOfMaximum"},
66 };
67 };
68
69 private:
70 Value _value{Value(-1)};
71};
72
76class FuzzySet {
77 public:
81 using Data = std::map<std::string, double>;
82
89 using ComposedMembershipFunction = std::function<double(const Data &)>;
90
96 using BaseMembershipFunction = std::tuple<std::string, std::vector<double>, std::function<double(double)>>;
97
103 FuzzySet(std::string linguisticTerm, BaseMembershipFunction &&baseMembershipFunction);
104
111 FuzzySet(std::string linguisticTerm, ComposedMembershipFunction &&membershipFunction,
112 const std::shared_ptr<CrispSet> &crispSet);
113
119 [[nodiscard]] double evaluate_membership(const Data &data) const;
120
127 [[nodiscard]] double defuzzify(DefuzzificationMethodOption method, size_t numSamples) const;
128
133 [[nodiscard]] std::string printBaseMembershipFunction() const;
134
138 explicit operator std::string() const;
139
144 [[nodiscard]] const std::string &getLinguisticTerm() const;
145
150 [[nodiscard]] const std::shared_ptr<CrispSet> &getCrispSet() const;
151
156 void setCrispSet(const std::shared_ptr<CrispSet> &crispSet);
157
161 friend std::shared_ptr<FuzzySet> operator||(const std::shared_ptr<FuzzySet> &lhs,
162 const std::shared_ptr<FuzzySet> &rhs);
163
167 friend std::shared_ptr<FuzzySet> operator&&(const std::shared_ptr<FuzzySet> &lhs,
168 const std::shared_ptr<FuzzySet> &rhs);
169
173 friend std::shared_ptr<FuzzySet> operator!(const std::shared_ptr<FuzzySet> &fuzzySet);
174
175 private:
179 std::string _linguisticTerm;
180
184 std::variant<BaseMembershipFunction, ComposedMembershipFunction> _membershipFunction;
185
189 std::shared_ptr<CrispSet> _crispSet;
190
196 [[nodiscard]] double centerOfGravity(size_t numSamples) const;
197
205 [[nodiscard]] double meanOfMaximum(size_t numSamples) const;
206};
207
214std::shared_ptr<FuzzySet> operator||(const std::shared_ptr<FuzzySet> &lhs, const std::shared_ptr<FuzzySet> &rhs);
215
222std::shared_ptr<FuzzySet> operator&&(const std::shared_ptr<FuzzySet> &lhs, const std::shared_ptr<FuzzySet> &rhs);
223
229std::shared_ptr<FuzzySet> operator!(const std::shared_ptr<FuzzySet> &fuzzySet);
230
231} // namespace autopas::FuzzyLogic
Used to represent the different defuzzification methods.
Definition: FuzzySet.h:25
static std::map< DefuzzificationMethodOption, std::string > getOptionNames()
Provides a way to iterate over the possible choices of DefuzzificationMethod.
Definition: FuzzySet.h:62
Value
Enum for the different defuzzification methods.
Definition: FuzzySet.h:30
@ MoM
Mean of Maximum.
Definition: FuzzySet.h:38
@ CoG
Center of Gravity.
Definition: FuzzySet.h:34
constexpr DefuzzificationMethodOption(Value option)
Constructor from value.
Definition: FuzzySet.h:50
Used to represent a mathematical Fuzzy-Set.
Definition: FuzzySet.h:76
std::tuple< std::string, std::vector< double >, std::function< double(double)> > BaseMembershipFunction
A base membership function calculates the membership value based on a direct function evaluation.
Definition: FuzzySet.h:96
void setCrispSet(const std::shared_ptr< CrispSet > &crispSet)
Sets the crisp set of the FuzzySet.
Definition: FuzzySet.cpp:146
const std::string & getLinguisticTerm() const
Returns the linguistic term of the FuzzySet.
Definition: FuzzySet.cpp:142
friend std::shared_ptr< FuzzySet > operator&&(const std::shared_ptr< FuzzySet > &lhs, const std::shared_ptr< FuzzySet > &rhs)
Make the Intersection Operator a friend of the class.
Definition: FuzzySet.cpp:157
std::string printBaseMembershipFunction() const
Returns a string representation of the BaseMembershipFunction.
Definition: FuzzySet.cpp:121
friend std::shared_ptr< FuzzySet > operator!(const std::shared_ptr< FuzzySet > &fuzzySet)
Make the Complement Operator a friend of the class.
Definition: FuzzySet.cpp:166
friend std::shared_ptr< FuzzySet > operator||(const std::shared_ptr< FuzzySet > &lhs, const std::shared_ptr< FuzzySet > &rhs)
Make the Union Operator a friend of the class.
Definition: FuzzySet.cpp:148
const std::shared_ptr< CrispSet > & getCrispSet() const
Returns the crisp set of the FuzzySet.
Definition: FuzzySet.cpp:144
double evaluate_membership(const Data &data) const
Evaluates the membership function of this FuzzySet at the given value.
Definition: FuzzySet.cpp:27
std::map< std::string, double > Data
A map of the form {dimension_name: value}.
Definition: FuzzySet.h:81
std::function< double(const Data &)> ComposedMembershipFunction
A composed membership function is a membership function that relies on different FuzzySets to calcula...
Definition: FuzzySet.h:89
double defuzzify(DefuzzificationMethodOption method, size_t numSamples) const
Defuzzifies the FuzzySet using the given data and method.
Definition: FuzzySet.cpp:51
Namespace that contains the fuzzy logic framework used by the FuzzyTuning-strategy.
Definition: namespaces.h:112
std::shared_ptr< FuzzySet > operator!(const std::shared_ptr< FuzzySet > &fuzzySet)
Returns the complement of a FuzzySet.
Definition: FuzzySet.cpp:166
std::shared_ptr< FuzzySet > operator&&(const std::shared_ptr< FuzzySet > &lhs, const std::shared_ptr< FuzzySet > &rhs)
Returns the intersection of two FuzzySets.
Definition: FuzzySet.cpp:157
std::shared_ptr< FuzzySet > operator||(const std::shared_ptr< FuzzySet > &lhs, const std::shared_ptr< FuzzySet > &rhs)
Returns the union of two FuzzySets.
Definition: FuzzySet.cpp:148