FuzzySet.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace FuzzyLogicPCL.FuzzySets
  5. {
  6. /// <summary>
  7. /// Fuzzy set class : general methods
  8. /// </summary>
  9. public class FuzzySet
  10. {
  11. /// <summary>
  12. /// List of points describing the fuzzy set
  13. /// </summary>
  14. protected List<Point2D> Points;
  15. /// <summary>
  16. /// Minimal value of the fuzzy set
  17. /// </summary>
  18. protected double Min {get;set;}
  19. /// <summary>
  20. /// Maximal value of the fuzzy set
  21. /// </summary>
  22. protected double Max { get; set; }
  23. /// <summary>
  24. /// Simple constructor
  25. /// </summary>
  26. /// <param name="min">Min. value</param>
  27. /// <param name="max">Max. value</param>
  28. public FuzzySet(double min, double max)
  29. {
  30. this.Points = new List<Point2D>();
  31. this.Min = min;
  32. this.Max = max;
  33. }
  34. /// <summary>
  35. /// Add a point to the fuzzy set. It will be sorted to be at the good place
  36. /// </summary>
  37. /// <param name="pt">New point to add</param>
  38. public void Add(Point2D pt)
  39. {
  40. Points.Add(pt);
  41. Points.Sort();
  42. }
  43. /// <summary>
  44. /// Add a point to the fuzzy set. It will be sorted to be at the good place
  45. /// </summary>
  46. /// <param name="x">New point x coordinate</param>
  47. /// <param name="y">New point y coordinate (degree)</param>
  48. public void Add(double x, double y)
  49. {
  50. Point2D pt = new Point2D(x, y);
  51. Add(pt);
  52. }
  53. /// <summary>
  54. /// Truth degree at a special x value. It is a simple line interpolation.
  55. /// </summary>
  56. /// <param name="XValue">The x coordinate</param>
  57. /// <returns>The truth degree at this point</returns>
  58. public double DegreeAtValue(double XValue)
  59. {
  60. // Check value in range (else, degree is null)
  61. if (XValue < Min || XValue > Max)
  62. {
  63. return 0;
  64. }
  65. // Compute value from interpolation : we locate the before and after point
  66. Point2D before = Points.LastOrDefault(pt => pt.X <= XValue);
  67. Point2D after = Points.FirstOrDefault(pt => pt.X >= XValue);
  68. if (before.Equals(after))
  69. {
  70. // x is the coordinate of a defined point
  71. return before.Y;
  72. }
  73. else
  74. {
  75. // x is between two points, we compute the interpolated value
  76. return (((before.Y - after.Y) * (after.X - XValue) / (after.X - before.X)) + after.Y);
  77. }
  78. }
  79. /// <summary>
  80. /// Compute the centroid (or x coordinate of the center of gravity) of a fuzzy set. Used for defuzzification
  81. /// </summary>
  82. /// <returns>The centroid x value</returns>
  83. public double Centroid()
  84. {
  85. // Less than two points : no area, so no centroid
  86. if (Points.Count < 2)
  87. {
  88. return 0;
  89. }
  90. else
  91. {
  92. // We compute the total area, and the ponderated one
  93. double ponderatedArea = 0;
  94. double totalArea = 0;
  95. double localArea;
  96. Point2D oldPt = null;
  97. foreach (Point2D newPt in Points)
  98. {
  99. if (oldPt != null)
  100. {
  101. // Centroids computation
  102. if (oldPt.Y == newPt.Y)
  103. {
  104. // For a rectangle : at the center value
  105. localArea = oldPt.Y * (newPt.X - oldPt.X);
  106. totalArea += localArea;
  107. ponderatedArea += ((newPt.X - oldPt.X) / 2 + oldPt.X) * localArea;
  108. }
  109. else
  110. {
  111. // We have two geometric shapes : a rectangle (at half) and a triangle (at 1/3 or 2/3 depending on the slope)
  112. // For the rectangle
  113. localArea = Math.Min(oldPt.Y, newPt.Y) * (newPt.X - oldPt.X);
  114. totalArea += localArea;
  115. ponderatedArea += ((newPt.X - oldPt.X) / 2 + oldPt.X) * localArea;
  116. // For the triangle
  117. localArea = (newPt.X - oldPt.X) * (Math.Abs(newPt.Y - oldPt.Y)) / 2;
  118. totalArea += localArea;
  119. if (newPt.Y > oldPt.Y)
  120. {
  121. ponderatedArea += (2.0 / 3.0 * (newPt.X - oldPt.X) + oldPt.X) * localArea;
  122. }
  123. else
  124. {
  125. ponderatedArea += (1.0 / 3.0 * (newPt.X - oldPt.X) + oldPt.X) * localArea;
  126. }
  127. }
  128. }
  129. oldPt = newPt;
  130. }
  131. // Return the centroid, that is the division of the two areas
  132. return ponderatedArea / totalArea;
  133. }
  134. }
  135. /// <summary>
  136. /// ToString method, returns the fuzzy set with the format : [min-max] : list of points
  137. /// </summary>
  138. /// <returns>The fuzzy set returns as a string</returns>
  139. public override String ToString()
  140. {
  141. String result = "[" + Min + "-" + Max + "]:";
  142. foreach (Point2D pt in Points)
  143. {
  144. result += pt.ToString(); //"(" + pt.X + ";" + pt.Y + ")";
  145. }
  146. return result;
  147. }
  148. /// <summary>
  149. /// ! Operator (NOT) : inverse the fuzzy set (1- original value)
  150. /// </summary>
  151. /// <param name="fs">The original fuzzy set</param>
  152. /// <returns>A new fuzzy set</returns>
  153. public static FuzzySet operator !(FuzzySet fs) {
  154. FuzzySet result = new FuzzySet(fs.Min, fs.Max);
  155. foreach (Point2D pt in fs.Points)
  156. {
  157. result.Add(new Point2D(pt.X, 1 - pt.Y));
  158. }
  159. return result;
  160. }
  161. /// <summary>
  162. /// Compute the multiplication of a fuzzy set : all y values are "value" times
  163. /// </summary>
  164. /// <param name="fs">Original fuzzy set</param>
  165. /// <param name="value">Number</param>
  166. /// <returns>New fuzzy set (fuzzy set * value)</returns>
  167. public static FuzzySet operator *(FuzzySet fs, double value)
  168. {
  169. FuzzySet result = new FuzzySet(fs.Min, fs.Max);
  170. foreach (Point2D pt in fs.Points)
  171. {
  172. result.Add(new Point2D(pt.X, pt.Y * value));
  173. }
  174. return result;
  175. }
  176. /// <summary>
  177. /// Equality operator
  178. /// </summary>
  179. /// <param name="fs1">First fuzzy set</param>
  180. /// <param name="fs2">Second fuzzy set</param>
  181. /// <returns>True if the two fuzzy sets have the same range and the same points, false otherwise</returns>
  182. public static Boolean operator == (FuzzySet fs1, FuzzySet fs2)
  183. {
  184. return fs1.ToString().Equals(fs2.ToString());
  185. }
  186. /// <summary>
  187. /// Difference operator
  188. /// </summary>
  189. /// <param name="fs1">First fuzzy set</param>
  190. /// <param name="fs2">Second fuzzy set</param>
  191. /// <returns>True if the two fuzzy sets have not the same range or not the same points</returns>
  192. public static Boolean operator != (FuzzySet fs1, FuzzySet fs2)
  193. {
  194. return !(fs1 == fs2);
  195. }
  196. /// <summary>
  197. /// Intersection operator : min(fs1, fs2)
  198. /// </summary>
  199. /// <param name="fs1">First fuzzy set</param>
  200. /// <param name="fs2">Second fuzzy set</param>
  201. /// <returns>New fuzzy set representing the intersection</returns>
  202. public static FuzzySet operator &(FuzzySet fs1, FuzzySet fs2)
  203. {
  204. return Merge(fs1, fs2, Math.Min);
  205. }
  206. /// <summary>
  207. /// Union operator : max(fs1, fs2)
  208. /// </summary>
  209. /// <param name="fs1">First fuzzy set</param>
  210. /// <param name="fs2">Second fuzzy set</param>
  211. /// <returns>New fuzzy set representing the union</returns>
  212. public static FuzzySet operator |(FuzzySet fs1, FuzzySet fs2)
  213. {
  214. return Merge(fs1, fs2, Math.Max);
  215. }
  216. /// <summary>
  217. /// Private method computing the merging of two fuzzy sets
  218. /// </summary>
  219. /// <param name="fs1">First fuzzy set</param>
  220. /// <param name="fs2">Second fuzzy set</param>
  221. /// <param name="MergeFt">Function for merging (eg min, or max), with two parameters (two double values) and the one to keep (double)</param>
  222. /// <returns>The resulting fuzzy set</returns>
  223. private static FuzzySet Merge(FuzzySet fs1, FuzzySet fs2, Func<double, double, double> MergeFt)
  224. {
  225. // New Fuzzy set
  226. FuzzySet result = new FuzzySet(Math.Min(fs1.Min, fs2.Min), Math.Max(fs1.Max, fs2.Max));
  227. // Creation of iterators on lists + initialization
  228. List<Point2D>.Enumerator enum1 = fs1.Points.GetEnumerator();
  229. List<Point2D>.Enumerator enum2 = fs2.Points.GetEnumerator();
  230. enum1.MoveNext();
  231. enum2.MoveNext();
  232. Point2D oldPt1 = enum1.Current;
  233. // Relative positions of fuzzy sets (to know when they intersect)
  234. int relativePosition = 0;
  235. int newRelativePosition = Math.Sign(enum1.Current.Y - enum2.Current.Y);
  236. // Loop while there are points in the two collections
  237. Boolean endOfList1 = false;
  238. Boolean endOfList2 = false;
  239. while (!endOfList1 && !endOfList2)
  240. {
  241. // New x values
  242. double x1 = enum1.Current.X;
  243. double x2 = enum2.Current.X;
  244. // New current position
  245. relativePosition = newRelativePosition;
  246. newRelativePosition = Math.Sign(enum1.Current.Y - enum2.Current.Y);
  247. if (relativePosition != newRelativePosition && relativePosition != 0 && newRelativePosition != 0)
  248. {
  249. // Positions have changed, so we have to compute the intersection and add it
  250. // Compute the points coordinates
  251. double x = (x1 == x2 ? oldPt1.X : Math.Min(x1, x2));
  252. double xPrime = Math.Max(x1, x2);
  253. // Intersection
  254. double slope1 = (fs1.DegreeAtValue(xPrime) - fs1.DegreeAtValue(x)) / (xPrime - x);
  255. double slope2 = (fs2.DegreeAtValue(xPrime) - fs2.DegreeAtValue(x)) / (xPrime - x);
  256. double delta = (fs2.DegreeAtValue(x) - fs1.DegreeAtValue(x)) / (slope1 - slope2);
  257. // Add point
  258. result.Add(x + delta, fs1.DegreeAtValue(x + delta));
  259. // Go on
  260. if (x1 < x2)
  261. {
  262. oldPt1 = enum1.Current;
  263. endOfList1 = !(enum1.MoveNext());
  264. }
  265. else if (x1 > x2)
  266. {
  267. endOfList2 = !(enum2.MoveNext());
  268. }
  269. }
  270. else if (x1 == x2)
  271. {
  272. // The two points are on the same X, so we take the good value (eg min or max)
  273. result.Add(x1, MergeFt(enum1.Current.Y, enum2.Current.Y));
  274. oldPt1 = enum1.Current;
  275. endOfList1 = !(enum1.MoveNext());
  276. endOfList2 = !(enum2.MoveNext());
  277. }
  278. else if (x1 < x2)
  279. {
  280. // Fs1 point is first, we add the value (eg min or max) between the enum1 point and the degree for fs2
  281. result.Add(x1, MergeFt(enum1.Current.Y, fs2.DegreeAtValue(x1)));
  282. oldPt1 = enum1.Current;
  283. endOfList1 = !(enum1.MoveNext());
  284. }
  285. else
  286. {
  287. // This time, it's fs2 first
  288. result.Add(x2, MergeFt(fs1.DegreeAtValue(x2), enum2.Current.Y));
  289. endOfList2 = !(enum2.MoveNext());
  290. }
  291. }
  292. // Add end points
  293. if (!endOfList1)
  294. {
  295. while (!endOfList1)
  296. {
  297. result.Add(enum1.Current.X, MergeFt(0, enum1.Current.Y));
  298. endOfList1 = !enum1.MoveNext();
  299. }
  300. }
  301. else if (!endOfList2)
  302. {
  303. while (!endOfList2)
  304. {
  305. result.Add(enum2.Current.X, MergeFt(0, enum2.Current.Y));
  306. endOfList2 = !enum2.MoveNext();
  307. }
  308. }
  309. return result;
  310. }
  311. }
  312. }