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