libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNFuzzyLogic.cpp
Go to the documentation of this file.
1 /* Copyright 2008-2016 INSA-Lyon, ENS-Lyon
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: FuzzyLogic.cpp
19  * \author Jean DUONG, Yann LEYDIER
20  */
21 
22 #include <CRNMath/CRNFuzzyLogic.h>
24 
25 using namespace crn;
26 
37 {
39  c.SetImageSize(nbBins);
40 
41  double delta = 1.0 / double(nbBins - 1);
42  double x = 0.0;
43 
44  for (size_t k = 0; k < nbBins; k++)
45  {
46  double sup = 0.0;
47  try
48  {
49  MatrixDouble preF = f.MakePreimage(x);
50  for (size_t i = 0; i < preF.GetCols(); i++)
51  {
52  double y = g.Evaluate(preF.At(0, i));
53 
54  if (y > sup)
55  {
56  sup = y;
57  }
58  }
59  }
60  catch (ExceptionNotFound&) { }
61  c.SetElement(k, sup);
62  x += delta;
63  }
64 
65  c.SetLeftEndpoint(0.0);
66  c.SetRightEndpoint(1.0);
67 
68  return c;
69 }
70 
79 double FuzzyLogic::DefuzzificationCOG(double min, double max, const RealToRealFunction& f)
80 {
81  double denominator = f.TrapezoidIntegral();
82 
83  if (denominator != 0.0)
84  {
85  URealToRealFunction newF = CloneAs<RealToRealFunction>(f);
86 
87  size_t n = newF->GetImageSize();
88  double x = 0.0;
89  double delta = 1.0 / double(n - 1);
90 
91  for (size_t k = 0; k < n; k++)
92  {
93  newF->SetElement(k, newF->At(k) * x);
94  x += delta;
95  }
96 
97  return newF->TrapezoidIntegral() / denominator;
98  }
99  else
100  {
101  return 0.0;
102  }
103 }
104 
105 
RealToRealFunction MakeCompatibility(const RealToRealFunction &f, const RealToRealFunction &g, size_t nbBins)
Computes the compatibility function between a regular function and a approximated function...
MatrixDouble MakePreimage(double y) const
Finds the possible preimages of a given image value.
size_t GetCols() const noexcept
Returns the number of columns.
Definition: CRNMatrix.h:163
double TrapezoidIntegral() const
Evaluates the integral with the trapezoid method.
Numerical integration toolbox class.
double DefuzzificationCOG(double min, double max, const RealToRealFunction &f)
Defuzzification over an interval using center of gravity method.
double matrix class
void SetRightEndpoint(double d) noexcept
Sets the end of the definition interval.
const T & At(size_t pos) const noexcept
Definition: CRNMatrix.h:165
void SetImageSize(size_t s)
Sets the number of elements to be modelized if the image.
void SetLeftEndpoint(double d) noexcept
Sets the beginning of the definition interval.
double Evaluate(double x) const
Evaluates the image a given point.
An item was not found in a container.
Definition: CRNException.h:95
void SetElement(size_t k, double v)
Sets a given image.