tiny-essentials 1.25.5 → 1.25.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/changelog/1/25/5.md +5 -0
- package/changelog/1/25/6.md +7 -0
- package/dist/v1/TinyEssentials.min.js +1 -1
- package/dist/v1/TinyMaInSys.min.js +1 -1
- package/dist/v1/libs/TinyMamdaniInferenceSystem.cjs +25 -9
- package/dist/v1/libs/TinyMamdaniInferenceSystem.d.mts +7 -3
- package/dist/v1/libs/TinyMamdaniInferenceSystem.mjs +26 -11
- package/docs/v1/libs/TinyMamdaniInferenceSystem.md +6 -4
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{"use strict";var e={d:(t,r)=>{for(var a in r)e.o(r,a)&&!e.o(t,a)&&Object.defineProperty(t,a,{enumerable:!0,get:r[a]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{FuzzySet:()=>s,MamdaniInferenceSystem:()=>
|
|
1
|
+
(()=>{"use strict";var e={d:(t,r)=>{for(var a in r)e.o(r,a)&&!e.o(t,a)&&Object.defineProperty(t,a,{enumerable:!0,get:r[a]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{FuzzySet:()=>s,MamdaniInferenceSystem:()=>o,defuzzifyCentroid:()=>a,trapezoid:()=>i});const r=e=>{if(!Array.isArray(e))throw new TypeError("Parameter 'sets' must be an array.");if(!e.every(e=>e instanceof s))throw new TypeError("All elements in the array must be instances of FuzzySet.")},a=(e,t,a=.5)=>{n(e,"object","defuzzifyCentroid.fuzzyOutput");for(const t in e)n(e[t],"number",`fuzzyOutput['${t}']`);r(t),n(a,"number","defuzzifyCentroid.step");let i=0,s=0;for(let r=0;r<=100;r+=a){let a=0;t.forEach(t=>{const i=e[t.name]||0,n=Math.min(i,t.calculate(r));a=Math.max(a,n)}),i+=r*a,s+=a}return 0===s?0:i/s},i=(e,t,r,a,i,n=!1)=>{if(n){if(e<=t||e>=i)return 0;if(e>=r&&e<=a)return 1}const s=t===r?1:(e-t)/(r-t),o=a===i?1:(i-e)/(i-a),u=Math.max(0,Math.min(s,1,o));return isNaN(u)?0:u},n=(e,t,r)=>{if(typeof e!==t)throw new TypeError(`Parameter '${r}' must be a ${t}.`)};class s{static trapezoid(e,t,r,a,n,s=!1){return i(e,t,r,a,n,s)}#e="";#t=0;#r=0;#a=0;#i=0;#n=!1;get name(){return this.#e}set name(e){n(e,"string","FuzzySet.name"),this.#e=e}get a(){return this.#t}set a(e){n(e,"number","FuzzySet.a"),this.#t=e}get b(){return this.#r}set b(e){n(e,"number","FuzzySet.b"),this.#r=e}get c(){return this.#a}set c(e){n(e,"number","FuzzySet.c"),this.#a=e}get d(){return this.#i}set d(e){n(e,"number","FuzzySet.d"),this.#i=e}get optimize(){return this.#n}set optimize(e){n(e,"boolean","FuzzySet.optimize"),this.#n=e}constructor(e,t,r,a,i,n=!1){this.name=e,this.a=t,this.b=r,this.c=a,this.d=i,this.optimize=n}calculate(e){return n(e,"number","calculate.x"),s.trapezoid(e,this.#t,this.#r,this.#a,this.#i,this.#n)}}class o{#s=new Map;addVariable(e,t){n(e,"string","addVariable.name"),r(t),this.#s.set(e,t)}removeVariable(e){return n(e,"string","removeVariable.name"),this.#s.delete(e)}getVariable(e){n(e,"string","getVariable.name");const t=this.#s.get(e);if(!t)throw new Error(`Linguistic variable '${e}' not found in the inference system.`);return[...t]}hasVariable(e){return n(e,"string","hasVariable.name"),this.#s.has(e)}fuzzify(e,t){n(e,"string","fuzzify.varName"),n(t,"number","fuzzify.value");const r=this.#s.get(e)||[],a={};return r.forEach(e=>{a[e.name]=e.calculate(t)}),a}}window.TinyMaInSys=t})();
|
|
@@ -63,13 +63,16 @@ const defuzzifyCentroid = (fuzzyOutput, outputSets, step = 0.5) => {
|
|
|
63
63
|
* @param {number} b - End of the rise (start of plateau).
|
|
64
64
|
* @param {number} c - Start of the fall (end of plateau).
|
|
65
65
|
* @param {number} d - End of the fall.
|
|
66
|
+
* @param {boolean} [optimize=false] - Optimizes performance by skipping math for absolute bounds.
|
|
66
67
|
* @returns {number} Degree of membership [0, 1].
|
|
67
68
|
*/
|
|
68
|
-
const trapezoid = (value, a, b, c, d) => {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
69
|
+
const trapezoid = (value, a, b, c, d, optimize = false) => {
|
|
70
|
+
if (optimize) {
|
|
71
|
+
// If the value is completely outside the outer bounds, return 0 immediately
|
|
72
|
+
if (value <= a || value >= d) return 0;
|
|
73
|
+
// If the value is entirely within the plateau, return 1 immediately
|
|
74
|
+
if (value >= b && value <= c) return 1;
|
|
75
|
+
}
|
|
73
76
|
|
|
74
77
|
/** @type {number} - Safely calculate rising slope */
|
|
75
78
|
const rise = a === b ? 1 : (value - a) / (b - a);
|
|
@@ -106,10 +109,11 @@ class FuzzySet {
|
|
|
106
109
|
* @param {number} b - End of the rise (start of plateau).
|
|
107
110
|
* @param {number} c - Start of the fall (end of plateau).
|
|
108
111
|
* @param {number} d - End of the fall.
|
|
112
|
+
* @param {boolean} [optimize=false] - Performance optimization flag.
|
|
109
113
|
* @returns {number} Degree of membership [0, 1].
|
|
110
114
|
*/
|
|
111
|
-
static trapezoid(value, a, b, c, d) {
|
|
112
|
-
return trapezoid(value, a, b, c, d);
|
|
115
|
+
static trapezoid(value, a, b, c, d, optimize = false) {
|
|
116
|
+
return trapezoid(value, a, b, c, d, optimize);
|
|
113
117
|
}
|
|
114
118
|
|
|
115
119
|
/** @type {string} - Internal name of the fuzzy set */
|
|
@@ -122,6 +126,8 @@ class FuzzySet {
|
|
|
122
126
|
#c = 0;
|
|
123
127
|
/** @type {number} - Internal right foot coordinate */
|
|
124
128
|
#d = 0;
|
|
129
|
+
/** @type {boolean} - Internal flag to enable calculation optimization */
|
|
130
|
+
#optimize = false;
|
|
125
131
|
|
|
126
132
|
get name() {
|
|
127
133
|
return this.#name;
|
|
@@ -163,19 +169,29 @@ class FuzzySet {
|
|
|
163
169
|
this.#d = value;
|
|
164
170
|
}
|
|
165
171
|
|
|
172
|
+
get optimize() {
|
|
173
|
+
return this.#optimize;
|
|
174
|
+
}
|
|
175
|
+
set optimize(value) {
|
|
176
|
+
validateType(value, 'boolean', 'FuzzySet.optimize');
|
|
177
|
+
this.#optimize = value;
|
|
178
|
+
}
|
|
179
|
+
|
|
166
180
|
/**
|
|
167
181
|
* @param {string} name - Name of the set (e.g., "Hot").
|
|
168
182
|
* @param {number} a - Left foot.
|
|
169
183
|
* @param {number} b - Left shoulder.
|
|
170
184
|
* @param {number} c - Right shoulder.
|
|
171
185
|
* @param {number} d - Right feet.
|
|
186
|
+
* @param {boolean} [optimize=false] - Enables performance optimization.
|
|
172
187
|
*/
|
|
173
|
-
constructor(name, a, b, c, d) {
|
|
188
|
+
constructor(name, a, b, c, d, optimize = false) {
|
|
174
189
|
this.name = name;
|
|
175
190
|
this.a = a;
|
|
176
191
|
this.b = b;
|
|
177
192
|
this.c = c;
|
|
178
193
|
this.d = d;
|
|
194
|
+
this.optimize = optimize;
|
|
179
195
|
}
|
|
180
196
|
|
|
181
197
|
/**
|
|
@@ -185,7 +201,7 @@ class FuzzySet {
|
|
|
185
201
|
*/
|
|
186
202
|
calculate(x) {
|
|
187
203
|
validateType(x, 'number', 'calculate.x');
|
|
188
|
-
return FuzzySet.trapezoid(x, this.#a, this.#b, this.#c, this.#d);
|
|
204
|
+
return FuzzySet.trapezoid(x, this.#a, this.#b, this.#c, this.#d, this.#optimize);
|
|
189
205
|
}
|
|
190
206
|
}
|
|
191
207
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export function defuzzifyCentroid(fuzzyOutput: {
|
|
2
2
|
[x: string]: number;
|
|
3
3
|
}, outputSets: FuzzySet[], step?: number): number;
|
|
4
|
-
export function trapezoid(value: number, a: number, b: number, c: number, d: number): number;
|
|
4
|
+
export function trapezoid(value: number, a: number, b: number, c: number, d: number, optimize?: boolean): number;
|
|
5
5
|
/**
|
|
6
6
|
* Represents a single Membership Function (Trapezoidal).
|
|
7
7
|
*/
|
|
@@ -13,17 +13,19 @@ export class FuzzySet {
|
|
|
13
13
|
* @param {number} b - End of the rise (start of plateau).
|
|
14
14
|
* @param {number} c - Start of the fall (end of plateau).
|
|
15
15
|
* @param {number} d - End of the fall.
|
|
16
|
+
* @param {boolean} [optimize=false] - Performance optimization flag.
|
|
16
17
|
* @returns {number} Degree of membership [0, 1].
|
|
17
18
|
*/
|
|
18
|
-
static trapezoid(value: number, a: number, b: number, c: number, d: number): number;
|
|
19
|
+
static trapezoid(value: number, a: number, b: number, c: number, d: number, optimize?: boolean): number;
|
|
19
20
|
/**
|
|
20
21
|
* @param {string} name - Name of the set (e.g., "Hot").
|
|
21
22
|
* @param {number} a - Left foot.
|
|
22
23
|
* @param {number} b - Left shoulder.
|
|
23
24
|
* @param {number} c - Right shoulder.
|
|
24
25
|
* @param {number} d - Right feet.
|
|
26
|
+
* @param {boolean} [optimize=false] - Enables performance optimization.
|
|
25
27
|
*/
|
|
26
|
-
constructor(name: string, a: number, b: number, c: number, d: number);
|
|
28
|
+
constructor(name: string, a: number, b: number, c: number, d: number, optimize?: boolean);
|
|
27
29
|
set name(value: string);
|
|
28
30
|
get name(): string;
|
|
29
31
|
set a(value: number);
|
|
@@ -34,6 +36,8 @@ export class FuzzySet {
|
|
|
34
36
|
get c(): number;
|
|
35
37
|
set d(value: number);
|
|
36
38
|
get d(): number;
|
|
39
|
+
set optimize(value: boolean);
|
|
40
|
+
get optimize(): boolean;
|
|
37
41
|
/**
|
|
38
42
|
* Calculates the membership degree.
|
|
39
43
|
* @param {number} x - Crisp input.
|
|
@@ -51,15 +51,18 @@ export const defuzzifyCentroid = (fuzzyOutput, outputSets, step = 0.5) => {
|
|
|
51
51
|
* @param {number} b - End of the rise (start of plateau).
|
|
52
52
|
* @param {number} c - Start of the fall (end of plateau).
|
|
53
53
|
* @param {number} d - End of the fall.
|
|
54
|
+
* @param {boolean} [optimize=false] - Optimizes performance by skipping math for absolute bounds.
|
|
54
55
|
* @returns {number} Degree of membership [0, 1].
|
|
55
56
|
*/
|
|
56
|
-
export const trapezoid = (value, a, b, c, d) => {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
57
|
+
export const trapezoid = (value, a, b, c, d, optimize = false) => {
|
|
58
|
+
if (optimize) {
|
|
59
|
+
// If the value is completely outside the outer bounds, return 0 immediately
|
|
60
|
+
if (value <= a || value >= d)
|
|
61
|
+
return 0;
|
|
62
|
+
// If the value is entirely within the plateau, return 1 immediately
|
|
63
|
+
if (value >= b && value <= c)
|
|
64
|
+
return 1;
|
|
65
|
+
}
|
|
63
66
|
/** @type {number} - Safely calculate rising slope */
|
|
64
67
|
const rise = a === b ? 1 : (value - a) / (b - a);
|
|
65
68
|
/** @type {number} - Safely calculate falling slope */
|
|
@@ -90,10 +93,11 @@ class FuzzySet {
|
|
|
90
93
|
* @param {number} b - End of the rise (start of plateau).
|
|
91
94
|
* @param {number} c - Start of the fall (end of plateau).
|
|
92
95
|
* @param {number} d - End of the fall.
|
|
96
|
+
* @param {boolean} [optimize=false] - Performance optimization flag.
|
|
93
97
|
* @returns {number} Degree of membership [0, 1].
|
|
94
98
|
*/
|
|
95
|
-
static trapezoid(value, a, b, c, d) {
|
|
96
|
-
return trapezoid(value, a, b, c, d);
|
|
99
|
+
static trapezoid(value, a, b, c, d, optimize = false) {
|
|
100
|
+
return trapezoid(value, a, b, c, d, optimize);
|
|
97
101
|
}
|
|
98
102
|
/** @type {string} - Internal name of the fuzzy set */
|
|
99
103
|
#name = '';
|
|
@@ -105,6 +109,8 @@ class FuzzySet {
|
|
|
105
109
|
#c = 0;
|
|
106
110
|
/** @type {number} - Internal right foot coordinate */
|
|
107
111
|
#d = 0;
|
|
112
|
+
/** @type {boolean} - Internal flag to enable calculation optimization */
|
|
113
|
+
#optimize = false;
|
|
108
114
|
get name() {
|
|
109
115
|
return this.#name;
|
|
110
116
|
}
|
|
@@ -140,19 +146,28 @@ class FuzzySet {
|
|
|
140
146
|
validateType(value, 'number', 'FuzzySet.d');
|
|
141
147
|
this.#d = value;
|
|
142
148
|
}
|
|
149
|
+
get optimize() {
|
|
150
|
+
return this.#optimize;
|
|
151
|
+
}
|
|
152
|
+
set optimize(value) {
|
|
153
|
+
validateType(value, 'boolean', 'FuzzySet.optimize');
|
|
154
|
+
this.#optimize = value;
|
|
155
|
+
}
|
|
143
156
|
/**
|
|
144
157
|
* @param {string} name - Name of the set (e.g., "Hot").
|
|
145
158
|
* @param {number} a - Left foot.
|
|
146
159
|
* @param {number} b - Left shoulder.
|
|
147
160
|
* @param {number} c - Right shoulder.
|
|
148
161
|
* @param {number} d - Right feet.
|
|
162
|
+
* @param {boolean} [optimize=false] - Enables performance optimization.
|
|
149
163
|
*/
|
|
150
|
-
constructor(name, a, b, c, d) {
|
|
164
|
+
constructor(name, a, b, c, d, optimize = false) {
|
|
151
165
|
this.name = name;
|
|
152
166
|
this.a = a;
|
|
153
167
|
this.b = b;
|
|
154
168
|
this.c = c;
|
|
155
169
|
this.d = d;
|
|
170
|
+
this.optimize = optimize;
|
|
156
171
|
}
|
|
157
172
|
/**
|
|
158
173
|
* Calculates the membership degree.
|
|
@@ -161,7 +176,7 @@ class FuzzySet {
|
|
|
161
176
|
*/
|
|
162
177
|
calculate(x) {
|
|
163
178
|
validateType(x, 'number', 'calculate.x');
|
|
164
|
-
return FuzzySet.trapezoid(x, this.#a, this.#b, this.#c, this.#d);
|
|
179
|
+
return FuzzySet.trapezoid(x, this.#a, this.#b, this.#c, this.#d, this.#optimize);
|
|
165
180
|
}
|
|
166
181
|
}
|
|
167
182
|
/**
|
|
@@ -6,7 +6,7 @@ Welcome to the official documentation for the **Fuzzy Logic Engine**! 🚀 This
|
|
|
6
6
|
|
|
7
7
|
## 🛠️ Core Utilities
|
|
8
8
|
|
|
9
|
-
### 📐 `trapezoid(value, a, b, c, d)`
|
|
9
|
+
### 📐 `trapezoid(value, a, b, c, d, optimize = false)`
|
|
10
10
|
A high-performance utility to safely calculate the fuzzy membership degree using a trapezoidal shape. It includes built-in protections against division by zero and short-circuit optimizations.
|
|
11
11
|
|
|
12
12
|
**Parameters:**
|
|
@@ -17,6 +17,7 @@ A high-performance utility to safely calculate the fuzzy membership degree using
|
|
|
17
17
|
| `b` | `number` | End of the rise / start of plateau (membership = 1). |
|
|
18
18
|
| `c` | `number` | Start of the fall / end of plateau (membership = 1). |
|
|
19
19
|
| `d` | `number` | End of the fall (membership = 0). |
|
|
20
|
+
| `optimize` | `boolean`, optional | Enables performance optimization by skipping math for absolute bounds. Default is `false`. |
|
|
20
21
|
|
|
21
22
|
**Returns:** * `number` - The degree of membership, safely clamped between `[0, 1]`.
|
|
22
23
|
|
|
@@ -44,20 +45,21 @@ Represents a single linguistic term (e.g., "Cold", "High", "Severe") defined by
|
|
|
44
45
|
|
|
45
46
|
#### ⚙️ Constructor
|
|
46
47
|
```javascript
|
|
47
|
-
new FuzzySet(name, a, b, c, d)
|
|
48
|
+
new FuzzySet(name, a, b, c, d, optimize = false)
|
|
48
49
|
```
|
|
49
50
|
|
|
50
51
|
#### 📦 Properties
|
|
51
52
|
* **`name`** (`string`): The name of the fuzzy set.
|
|
52
53
|
* **`a`, `b`, `c`, `d`** (`number`): The coordinates defining the trapezoidal shape.
|
|
54
|
+
* **`optimize`** (`boolean`): Internal flag to enable calculation optimization for absolute bounds.
|
|
53
55
|
|
|
54
56
|
#### 🧮 Methods
|
|
55
57
|
* **`calculate(x)`**
|
|
56
|
-
Calculates the membership degree for a specific input using the set's coordinates.
|
|
58
|
+
Calculates the membership degree for a specific input using the set's coordinates and optimization flag.
|
|
57
59
|
* **Parameters:** `x` (`number`) - The crisp input value.
|
|
58
60
|
* **Returns:** `number` - Degree of membership `[0, 1]`.
|
|
59
61
|
|
|
60
|
-
* **`static trapezoid(value, a, b, c, d)`**
|
|
62
|
+
* **`static trapezoid(value, a, b, c, d, optimize = false)`**
|
|
61
63
|
Static wrapper for the global `trapezoid` utility.
|
|
62
64
|
|
|
63
65
|
---
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tiny-essentials",
|
|
3
|
-
"version": "1.25.
|
|
3
|
+
"version": "1.25.6",
|
|
4
4
|
"description": "Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "npm run test:mjs && npm run test:cjs && npm run test:js",
|