tiny-essentials
Version:
Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.
1 lines • 2.75 kB
JavaScript
(()=>{"use strict";const e={d:(t,r)=>{if(Array.isArray(r))for(var a=0;a<r.length;){var i=r[a++],n=r[a++];e.o(t,i)?0===n&&a++:0===n?Object.defineProperty(t,i,{enumerable:!0,value:r[a++]}):Object.defineProperty(t,i,{enumerable:!0,get:n})}else for(var i in r)e.o(r,i)&&!e.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:r[i]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}};let t={};e.r(t),e.d(t,{FuzzySet:()=>n,MamdaniInferenceSystem:()=>s});const r=e=>{if(!Array.isArray(e))throw new TypeError("Parameter 'sets' must be an array.");if(!e.every(e=>e instanceof n))throw new TypeError("All elements in the array must be instances of FuzzySet.")},a=(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},i=(e,t,r)=>{if(typeof e!==t)throw new TypeError(`Parameter '${r}' must be a ${t}.`)};class n{static trapezoid(e,t,r,i,n,s=!1){return a(e,t,r,i,n,s)}#e="";#t=0;#r=0;#a=0;#i=0;#n=!1;get name(){return this.#e}set name(e){i(e,"string","FuzzySet.name"),this.#e=e}get a(){return this.#t}set a(e){i(e,"number","FuzzySet.a"),this.#t=e}get b(){return this.#r}set b(e){i(e,"number","FuzzySet.b"),this.#r=e}get c(){return this.#a}set c(e){i(e,"number","FuzzySet.c"),this.#a=e}get d(){return this.#i}set d(e){i(e,"number","FuzzySet.d"),this.#i=e}get optimize(){return this.#n}set optimize(e){i(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 i(e,"number","calculate.x"),n.trapezoid(e,this.#t,this.#r,this.#a,this.#i,this.#n)}}class s{#s=new Map;addVariable(e,t){i(e,"string","addVariable.name"),r(t),this.#s.set(e,t)}removeVariable(e){return i(e,"string","removeVariable.name"),this.#s.delete(e)}getVariable(e){i(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 i(e,"string","hasVariable.name"),this.#s.has(e)}fuzzify(e,t){i(e,"string","fuzzify.varName"),i(t,"number","fuzzify.value");const r=this.#s.get(e)||[],a={};return r.forEach(e=>{a[e.name]=e.calculate(t)}),a}}e.d(t,["defuzzifyCentroid",0,(e,t,a=.5)=>{i(e,"object","defuzzifyCentroid.fuzzyOutput");for(const t in e)i(e[t],"number",`fuzzyOutput['${t}']`);r(t),i(a,"number","defuzzifyCentroid.step");let n=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)}),n+=r*a,s+=a}return 0===s?0:n/s},"trapezoid",0,a]),window.TinyMaInSys=t})();