Fuzzy Logic in JavaScript
- Constant - Fixed membership value
- Grade - Linear membership function
- Reverse Grade - Inverted linear membership
- Sigmoid - S-shaped membership curve
- Trapezoid - Trapezoidal membership function
- Triangle - Triangular membership functionve
npm install es6-fuzzconst { Logic, Triangle, Trapezoid, Grade } = require('es6-fuzz');
const logic = new Logic();
const result = logic
.init('noAttack', new Triangle(0, 20, 40))
.or('normalAttack', new Trapezoid(20, 30, 90, 100))
.or('enragedAttack', new Grade(90, 100))
.defuzzify(99);
console.log(result.toString()); // 'enragedAttack'const { Logic, Trapezoid } = require('es6-fuzz');
const logic = new Logic();
const result = logic
.init('cold', new Trapezoid(0, 12, 18, 20))
.or('hot', new Trapezoid(12, 14, 16, 100))
.defuzzify(20);
console.log(result.toString()); // 'hot'Combine multiple fuzzy functions with boolean logic using the boon-js compatibility layer.
A monster that bites when it's cold AND you're close to it:
const { Logic, Triangle } = require('es6-fuzz');
const { getEvaluator } = require('boon-js');
// Temperature logic
const logicHeat = new Logic();
logicHeat.init('cold', new Triangle(0, 10, 15))
.or('optimal', new Triangle(10, 20, 30))
.or('hot', new Triangle(25, 40, 60));
// Distance logic
const logicDistance = new Logic();
logicDistance.init('close', new Triangle(0, 10, 20))
.or('far', new Triangle(5, 50, 100));
// Combine with boolean logic
const monsterBiteTest = getEvaluator('heat.cold AND distance.close');
const resHeat = logicHeat.defuzzify(2, 'heat');
const resClose = logicDistance.defuzzify(2, 'distance');
const jsBoonInput = { ...resHeat.boonJsInputs, ...resClose.boonJsInputs };
console.log(monsterBiteTest(jsBoonInput)); // truenpm test # build + run the test suite
npm run test:coverage # same, with a coverage reportnpm run docs # Generate the TypeDoc API site into ./outThe site is deployed to GitHub Pages automatically on every push to master
by the Docs workflow; npm run docs is for local
preview.
The render-examples script renders every fuzzifier and a few composite
systems (temperature/speed controllers) to SVG, plus an index.html viewer:
npm run build # render-examples runs from the compiled output
npm run render-examples # writes SVGs + index.html into ./examplesOpen examples/index.html in a browser to see the membership curves.
- Node.js 22+
- gaussian - Gaussian distribution functions