UNPKG

@thi.ng/fuzzy

Version:

Fuzzy logic operators & configurable rule inferencing engine

47 lines (46 loc) 1.71 kB
import { norm } from "@thi.ng/math/fit"; import { clamp0 } from "@thi.ng/math/interval"; const tnormMin = (x, y) => Math.min(x, y); const tnormProduct = (x, y) => x * y; const tnormLukasiewicz = (x, y) => clamp0(x + y - 1); const tnormDrastic = (x, y) => x === 1 ? y : y === 1 ? x : 0; const tnormNilpotent = (x, y) => x + y > 1 ? Math.min(x, y) : 0; const tnormHamacher = (p = 2) => (x, y) => x === 0 && y === 0 ? 0 : x * y / (p + (1 - p) * (x + y - x * y)); const tnormYager = (p = 2) => p === 0 ? () => 0 : (x, y) => clamp0(1 - ((1 - x) ** p + (1 - y) ** p) ** (1 / p)); const tnormDombi = (p = 2) => p === 0 ? () => 0 : (x, y) => x === 0 || y === 0 ? 0 : 1 / (1 + (((1 - x) / x) ** p + ((1 - y) / y) ** p) ** (1 / p)); const tnormAczelAlsina = (p = 2) => (x, y) => Math.exp( -((Math.abs(Math.log(x)) ** p + Math.abs(Math.log(y)) ** p) ** (1 / p)) ); const snormMax = (x, y) => Math.max(x, y); const snormProbabilistic = (x, y) => x + y - x * y; const snormBoundedSum = (x, y) => Math.min(x + y, 1); const snormDrastic = (x, y) => x === 0 ? y : y === 0 ? x : 1; const snormNilpotent = (x, y) => x + y < 1 ? Math.max(x, y) : 1; const snormEinstein = (x, y) => (x + y) / (1 + x * y); const ordinalSum = (specs) => (x, y) => { for (let s of specs) { const [a, b] = s.domain; if (x >= a && x <= b && y >= a && y <= b) { return a + (b - a) * s.tnorm(norm(x, a, b), norm(y, a, b)); } } return Math.min(x, y); }; export { ordinalSum, snormBoundedSum, snormDrastic, snormEinstein, snormMax, snormNilpotent, snormProbabilistic, tnormAczelAlsina, tnormDombi, tnormDrastic, tnormHamacher, tnormLukasiewicz, tnormMin, tnormNilpotent, tnormProduct, tnormYager };