1. 1 : import { FuzzyTerm } from './FuzzyTerm.js';
  2. 2 : import { MathUtils } from '../math/MathUtils.js';
  3. 3 :
  4. 4 : /**
  5. 5 : * Base class for fuzzy sets. This type of sets are defined by a membership function
  6. 6 : * which can be any arbitrary shape but are typically triangular or trapezoidal. They define
  7. 7 : * a gradual transition from regions completely outside the set to regions completely
  8. 8 : * within the set, thereby enabling a value to have partial membership to a set.
  9. 9 : *
  10. 10 : * This class is derived from {@link FuzzyTerm} so it can be directly used in fuzzy rules.
  11. 11 : * According to the composite design pattern, a fuzzy set can be considered as an atomic fuzzy term.
  12. 12 : *
  13. 13 : * @author {@link https://github.com/Mugen87|Mugen87}
  14. 14 : * @augments FuzzyTerm
  15. 15 : */
  16. 16 : class FuzzySet extends FuzzyTerm {
  17. 17 :
  18. 18 : /**
  19. 19 : * Constructs a new fuzzy set with the given values.
  20. 20 : *
  21. 21 : * @param {Number} representativeValue - The maximum of the set's membership function.
  22. 22 : */
  23. 23 : constructor( representativeValue = 0 ) {
  24. 24 :
  25. 25 : super();
  26. 26 :
  27. 27 : /**
  28. 28 : * Represents the degree of membership to this fuzzy set.
  29. 29 : * @type {Number}
  30. 30 : * @default 0
  31. 31 : */
  32. 32 : this.degreeOfMembership = 0;
  33. 33 :
  34. 34 : /**
  35. 35 : * The maximum of the set's membership function. For instance, if
  36. 36 : * the set is triangular then this will be the peak point of the triangular.
  37. 37 : * If the set has a plateau then this value will be the mid point of the
  38. 38 : * plateau. Used to avoid runtime calculations.
  39. 39 : * @type {Number}
  40. 40 : * @default 0
  41. 41 : */
  42. 42 : this.representativeValue = representativeValue;
  43. 43 :
  44. 44 : /**
  45. 45 : * Represents the left border of this fuzzy set.
  46. 46 : * @type {Number}
  47. 47 : * @default 0
  48. 48 : */
  49. 49 : this.left = 0;
  50. 50 :
  51. 51 : /**
  52. 52 : * Represents the right border of this fuzzy set.
  53. 53 : * @type {Number}
  54. 54 : * @default 0
  55. 55 : */
  56. 56 : this.right = 0;
  57. 57 :
  58. 58 : //
  59. 59 :
  60. 60 : this._uuid = null;
  61. 61 :
  62. 62 : }
  63. 63 :
  64. 64 : /**
  65. 65 : * Unique ID, primarily used in context of serialization/deserialization.
  66. 66 : * @type {String}
  67. 67 : * @readonly
  68. 68 : */
  69. 69 : get uuid() {
  70. 70 :
  71. 71 : if ( this._uuid === null ) {
  72. 72 :
  73. 73 : this._uuid = MathUtils.generateUUID();
  74. 74 :
  75. 75 : }
  76. 76 :
  77. 77 : return this._uuid;
  78. 78 :
  79. 79 : }
  80. 80 :
  81. 81 : /**
  82. 82 : * Computes the degree of membership for the given value. Notice that this method
  83. 83 : * does not set {@link FuzzySet#degreeOfMembership} since other classes use it in
  84. 84 : * order to calculate intermediate degree of membership values. This method be
  85. 85 : * implemented by all concrete fuzzy set classes.
  86. 86 : *
  87. 87 : * @param {Number} value - The value used to calculate the degree of membership.
  88. 88 : * @return {Number} The degree of membership.
  89. 89 : */
  90. 90 : computeDegreeOfMembership( /* value */ ) {}
  91. 91 :
  92. 92 : // FuzzyTerm API
  93. 93 :
  94. 94 : /**
  95. 95 : * Clears the degree of membership value.
  96. 96 : *
  97. 97 : * @return {FuzzySet} A reference to this fuzzy set.
  98. 98 : */
  99. 99 : clearDegreeOfMembership() {
  100. 100 :
  101. 101 : this.degreeOfMembership = 0;
  102. 102 :
  103. 103 : return this;
  104. 104 :
  105. 105 : }
  106. 106 :
  107. 107 : /**
  108. 108 : * Returns the degree of membership.
  109. 109 : *
  110. 110 : * @return {Number} Degree of membership.
  111. 111 : */
  112. 112 : getDegreeOfMembership() {
  113. 113 :
  114. 114 : return this.degreeOfMembership;
  115. 115 :
  116. 116 : }
  117. 117 :
  118. 118 : /**
  119. 119 : * Updates the degree of membership by the given value. This method is used when
  120. 120 : * the set is part of a fuzzy rule's consequent.
  121. 121 : *
  122. 122 : * @return {FuzzySet} A reference to this fuzzy set.
  123. 123 : */
  124. 124 : updateDegreeOfMembership( value ) {
  125. 125 :
  126. 126 : // update the degree of membership if the given value is greater than the
  127. 127 : // existing one
  128. 128 :
  129. 129 : if ( value > this.degreeOfMembership ) this.degreeOfMembership = value;
  130. 130 :
  131. 131 : return this;
  132. 132 :
  133. 133 : }
  134. 134 :
  135. 135 : /**
  136. 136 : * Transforms this instance into a JSON object.
  137. 137 : *
  138. 138 : * @return {Object} The JSON object.
  139. 139 : */
  140. 140 : toJSON() {
  141. 141 :
  142. 142 : const json = super.toJSON();
  143. 143 :
  144. 144 : json.degreeOfMembership = this.degreeOfMembership;
  145. 145 : json.representativeValue = this.representativeValue;
  146. 146 : json.left = this.left;
  147. 147 : json.right = this.right;
  148. 148 : json.uuid = this.uuid;
  149. 149 :
  150. 150 : return json;
  151. 151 :
  152. 152 : }
  153. 153 :
  154. 154 : /**
  155. 155 : * Restores this instance from the given JSON object.
  156. 156 : *
  157. 157 : * @param {Object} json - The JSON object.
  158. 158 : * @return {FuzzySet} A reference to this fuzzy set.
  159. 159 : */
  160. 160 : fromJSON( json ) {
  161. 161 :
  162. 162 : this.degreeOfMembership = json.degreeOfMembership;
  163. 163 : this.representativeValue = json.representativeValue;
  164. 164 : this.left = json.left;
  165. 165 : this.right = json.right;
  166. 166 :
  167. 167 : this._uuid = json.uuid;
  168. 168 :
  169. 169 : return this;
  170. 170 :
  171. 171 : }
  172. 172 :
  173. 173 : }
  174. 174 :
  175. 175 : export { FuzzySet };