fis-tsukamoto
v1.0.5
Published
Tsukamoto Fuzzy Inference System written in TypeScript
Readme
Tsukamoto Fuzzy Inference System (FIS)
A lightweight TypeScript library for implementing the Tsukamoto Fuzzy Inference System.
Features
- Built-in Membership Functions: Supports
DOWN,UP,UPDOWN(triangular),TRAPEZE(trapezoidal), andGAUSS(Gaussian) terms. - Automatic Defuzzification: Computes crisp outputs using weighted average defuzzification.
- Visualization Support: Helper method
getChartData()to export membership function curves for easy plotting. - Fully Typed: Built with TypeScript with exports for all classes and enums.
Installation
npm install fis-tsukamotoQuick Start Example
Here is a classic example modeling a Tipping System based on Service Quality and Food Quality.
import { TsukamotoFis, Variable, Term, Rule, TermType } from "fis-tsukamoto";
// 1. Define Input Variables & Membership Functions (Terms)
const service = new Variable({
name: "Service",
terms: [
new Term({ name: "poor", type: TermType.DOWN, x: [2, 6] }),
new Term({ name: "good", type: TermType.UP, x: [4, 8] }),
],
});
const food = new Variable({
name: "Food",
terms: [
new Term({ name: "rancid", type: TermType.DOWN, x: [3, 7] }),
new Term({ name: "delicious", type: TermType.UP, x: [5, 9] }),
],
});
// 2. Define Output Variable
// NOTE: Tsukamoto output terms MUST use either TermType.UP or TermType.DOWN
const tip = new Variable({
name: "Tip",
terms: [
new Term({ name: "cheap", type: TermType.DOWN, x: [5, 15] }),
new Term({ name: "generous", type: TermType.UP, x: [10, 25] }),
],
});
// 3. Define Rules
// Rule inputs map order-wise to the input variables array
const rules = [
new Rule({ input: ["poor", "rancid"], output: "cheap" }),
new Rule({ input: ["good", "delicious"], output: "generous" }),
];
// 4. Initialize the Tsukamoto System
const fis = new TsukamotoFis({
name: "Tip Calculator",
inputvar: [service, food],
outputvar: tip,
rules: rules,
});
// 5. Calculate Crisp Output
// Input values map to [service, food]
const result = fis.calculate([3, 8]);
console.log(`Calculated Tip: ${result}`);Membership Function Types (TermType)
| Type | Array x Parameters | Description | Valid for Output Term? |
| :----------------- | :-------------------- | :--------------------------------------- | :--------------------- |
| TermType.DOWN | [start, end] | Monotonically decreasing linear function | Yes |
| TermType.UP | [start, end] | Monotonically increasing linear function | Yes |
| TermType.UPDOWN | [left, peak, right] | Triangular function | No |
| TermType.TRAPEZE | [a, b, c, d] | Trapezoidal function | No |
| TermType.GAUSS | [mean, stdev] | Gaussian curve function | No |
⚠️ Important: Tsukamoto inference requires monotonous output membership functions. Therefore, your outputvar terms must use either TermType.UP or TermType.DOWN.
API Reference
Term
Represents a membership function for a variable.
calculate(input: number): number: Returns the membership value ($\alpha$) between $0$ and $1$.output(alpha: number): number: Inverts the membership function to solve for crisp value $z$. (Only for UP / DOWN types).
Variable
Groups terms together into a named variable.
getTerm(name: string): Term: Retrieves a Term instance by name.getChartData(minX: number, maxX: number, resolution?: number): { term: string, x: number, y: number }[]: Generates dynamic coordinates [{ term, x, y }] useful for plotting graphs.
TsukamotoFis
The core engine.
calculate(input: number[]): number: Accepts an array of numeric inputs matching the order of inputvar and returns the final defuzzified output value.
License
MIT
