npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsfuzzymind

v1.0.1

Published

A JavaScript library for fuzzy logic operations including fuzzy sets, fuzzy rules, and an inference engine.

Downloads

10

Readme

jsFuzzyMind

This library provides a set of classes for implementing fuzzy logic systems in JavaScript. It includes support for defining fuzzy sets, fuzzy rules, and performing inference using an inference engine. It also provides methods for defuzzification using different techniques. For example, you can use this library to build a fuzzy logic-based risk assessment system for financial investments. In this system, fuzzy sets could represent risk levels (such as "Low Risk", "Medium Risk", "High Risk"), and rules could determine investment strategies based on market indicators. The inference engine would evaluate these rules to suggest an appropriate investment strategy, and defuzzification methods like the Mean of Maximum (MOM) could be used to provide a precise risk score for decision-making.

FuzzySet

Represents a fuzzy set with a membership function. Provides methods for operations on fuzzy sets.

new FuzzySet(name, membershipFunction)
// name: The name of the fuzzy set.
// membershipFunction: A function that defines the membership degree of the set.
Methods
  • membershipDegree(x): Returns the membership degree of x.
  • union(otherSet): Returns a new fuzzy set representing the union of this set and otherSet.
  • intersection(otherSet): Returns a new fuzzy set representing the intersection of this set and otherSet.
  • complement(): Returns a new fuzzy set representing the complement of this set.
  • normalize(): Returns a new fuzzy set with normalized membership function.
  • centroid(min, max, step): Performs defuzzification using the centroid method.

FuzzyRule

Represents a fuzzy rule consisting of a condition and a consequence.

new FuzzyRule(condition, consequence, weight)
// condition: A function that takes inputs and returns a boolean indicating if the rule condition is satisfied.
// consequence: The result of the rule if the condition is true. Can be a fuzzy set or a function.
// weight: An optional weight for the rule (default is 1).
Methods
  • evaluate(inputs): Evaluates the rule against the given inputs and returns the result and weight if the condition is satisfied.

InferenceEngine

Uses a set of fuzzy rules to perform inference and defuzzification.

new InferenceEngine(rules) //rules: An array of FuzzyRule instances.
Methods
  • infer(inputs): Performs inference based on the input values and returns the aggregated result.
  • aggregateResults(results): Aggregates results from the fuzzy rules.
  • defuzzifyCentroid(min, max, step): Performs defuzzification using the centroid method.
  • defuzzifyMOM(min, max, step): Performs defuzzification using the Mean of Maxima (MOM) method.
  • defuzzifyBisector(min, max, step): Performs defuzzification using the Bisector method.
  • getFuzzySetConsequences(): Returns a list of fuzzy sets as consequences of the rules.
Example Usage CommonJS
const { FuzzySet, FuzzyRule, InferenceEngine } = require('jsfuzzymind');

// Define fuzzy sets for urgency and complexity
const urgencySet = new FuzzySet('Urgency', urgency => {
    if (urgency < 3) return 0;
    if (urgency < 7) return (urgency - 3) / 4;
    return 1;
});
const complexitySet = new FuzzySet('Complexity', complexity => {
    if (complexity < 2) return 0;
    if (complexity < 5) return (complexity - 2) / 3;
    return 1;
});

// Define fuzzy rules
const rules = [
    new FuzzyRule(
        inputs => urgencySet.membershipDegree(inputs.urgency) > 0.7 && complexitySet.membershipDegree(inputs.complexity) > 0.7,
        new FuzzySet('Urgent', x => x >= 7 ? 1 : x / 7)
    ),
    new FuzzyRule(
        inputs => urgencySet.membershipDegree(inputs.urgency) > 0.5,
        () => 'High Priority'
    ),
    new FuzzyRule(
        inputs => complexitySet.membershipDegree(inputs.complexity) > 0.5,
        () => 'Medium Priority'
    ),
    new FuzzyRule(
        inputs => urgencySet.membershipDegree(inputs.urgency) <= 0.5 && complexitySet.membershipDegree(inputs.complexity) <= 0.5,
        () => 'Low Priority'
    )
];

const engine = new InferenceEngine(rules);

// Example ticket
const ticket = { urgency: 8, complexity: 6 };
const priority = engine.infer(ticket);
console.log(`Ticket Priority: ${priority}`);

// Defuzzification examples
const centroid = urgencySet.centroid(0, 10);
console.log(`Centroid defuzzification: ${centroid}`);

const defuzzifiedCentroid = engine.defuzzifyCentroid(0, 10);
console.log(`Defuzzified Centroid: ${defuzzifiedCentroid}`);

const defuzzifiedMOM = engine.defuzzifyMOM(0, 10);
console.log(`Defuzzified MOM: ${defuzzifiedMOM}`);

const defuzzifiedBisector = engine.defuzzifyBisector(0, 10);
console.log(`Defuzzified Bisector: ${defuzzifiedBisector}`);
Example Usage ES Modules
import { FuzzySet, FuzzyRule, InferenceEngine } from 'jsfuzzymind';

// Define fuzzy sets for urgency and complexity
const urgencySet = new FuzzySet('Urgency', urgency => {
    if (urgency < 3) return 0;
    if (urgency < 7) return (urgency - 3) / 4;
    return 1;
});

const complexitySet = new FuzzySet('Complexity', complexity => {
    if (complexity < 2) return 0;
    if (complexity < 5) return (complexity - 2) / 3;
    return 1;
});

// Define fuzzy rules
const rules = [
    new FuzzyRule(
        inputs => urgencySet.membershipDegree(inputs.urgency) > 0.7 && complexitySet.membershipDegree(inputs.complexity) > 0.7,
        new FuzzySet('Urgent', x => x >= 7 ? 1 : x / 7)
    ),
    new FuzzyRule(
        inputs => urgencySet.membershipDegree(inputs.urgency) > 0.5,
        () => 'High Priority'
    ),
    new FuzzyRule(
        inputs => complexitySet.membershipDegree(inputs.complexity) > 0.5,
        () => 'Medium Priority'
    ),
    new FuzzyRule(
        inputs => urgencySet.membershipDegree(inputs.urgency) <= 0.5 && complexitySet.membershipDegree(inputs.complexity) <= 0.5,
        () => 'Low Priority'
    )
];

const engine = new InferenceEngine(rules);

// Example ticket
const ticket = { urgency: 8, complexity: 6 };
const priority = engine.infer(ticket);
console.log(`Ticket Priority: ${priority}`);

// Defuzzification examples
const centroid = urgencySet.centroid(0, 10);
console.log(`Centroid defuzzification: ${centroid}`);

const defuzzifiedCentroid = engine.defuzzifyCentroid(0, 10);
console.log(`Defuzzified Centroid: ${defuzzifiedCentroid}`);

const defuzzifiedMOM = engine.defuzzifyMOM(0, 10);
console.log(`Defuzzified MOM: ${defuzzifiedMOM}`);

const defuzzifiedBisector = engine.defuzzifyBisector(0, 10);
console.log(`Defuzzified Bisector: ${defuzzifiedBisector}`);