2.1.81 • Published 2 days ago

@thi.ng/fuzzy v2.1.81

Weekly downloads
9
License
Apache-2.0
Repository
github
Last release
2 days ago

fuzzy

npm version npm downloads Twitter Follow

This project is part of the @thi.ng/umbrella monorepo.

About

Fuzzy logic operators & configurable rule inferencing engine.

Features

  • Entirely declarative & functional approach
  • Fully type checked
  • Fuzzy set domain shaping & composition functions (incl. negated / inverse)
  • Various T-norms & S-norms, incl. parametric versions
  • Rules with multiple inputs/outputs and arbitrary term combinators (i.e. T-norms). Syntax sugar for common and/or rules.
  • Defuzzification via customizable strategies and options to balance precision vs. performance
    • Maxima: First, Last, Mean
    • Center-of-Gravity (COG)
  • Linguistic variable creation and term/set classification for given domain values
  • Fuzzy set visualization (via @thi.ng/fuzzy-viz)

References / Further reading

Status

STABLE - used in production

Search or submit any issues for this package

Support packages

Installation

yarn add @thi.ng/fuzzy

ES module import:

<script type="module" src="https://cdn.skypack.dev/@thi.ng/fuzzy"></script>

Skypack documentation

For Node.js REPL:

# with flag only for < v16
node --experimental-repl-await

> const fuzzy = await import("@thi.ng/fuzzy");

Package sizes (gzipped, pre-treeshake): ESM: 1.66 KB

Dependencies

API

Generated API docs

(See tests for more usage examples).

Fuzzy set generators & combinators

Generators:

  • constant()
  • point()
  • ramp() / invRamp()
  • triangle()
  • trapezoid()
  • sigmoid() / invSigmoid()
  • gaussian()

Combinators:

  • negate()
  • weighted()
  • alphaCut() / invAlphaCut()
  • compose() / intersect() / union()

Linguistic variables

Linguistic variables (short: L-vars) are groupings of named (and possibly overlapping) fuzzy sets within a given value domain. The can be used standalone or as inputs/outputs in rules (further below).

The @thi.ng/fuzzy-viz package provides utilities to visualize the fuzzy sets of an L-var.

fuzzy set visualization of the example
l-var

// temperature sets (in celsius)
const temp = variable(
  // value domain
  [-20, 40],
  {
    freezing: invSigmoid(0, 2),
    cold: trapezoid(-1, 2, 16, 20),
    warm: trapezoid(15, 20, 30, 34),
    hot: sigmoid(32, 2)
  }
);

// evaluate all fuzzy sets for given domain value
evaluate(temp, 18)
// {
//   freezing: 2.220446049250313e-16,
//   cold: 0.5,
//   warm: 0.6,
//   hot: 6.914400106935423e-13
// }

evaluate(temp, 28)
// {
//   freezing: 0,
//   cold: 0,
//   warm: 1,
//   hot: 0.0003353501304664781
// }

// classify temperature (min confidence 33%, default: 50%)
classify(temp, 28, 0.33)
// "warm"

Rule creation & inferencing

Example taken from Franck Dernoncourt's Introduction to Fuzzy Logic:

fuzzy set illustration from F.Dernoncourt's
tutorial

// define fuzzy input variables
const inputs = {
  food: variable([0, 10], {
    awful: invRamp(1, 3),
    delicious: ramp(7, 9),
  }),
  service: variable([0, 10], {
    poor: gaussian(0, 1.5),
    good: gaussian(5, 1.5),
    excellent: gaussian(10, 1.5),
  }),
};

const outputs = {
  tip: variable([0, 30], {
    low: triangle(0, 5, 10),
    medium: triangle(10, 15, 20),
    high: triangle(20, 25, 30),
  }),
};

// l-vars, rules and defuzzification are using generics for type safety
// we define these 2 type aliases for brevity
type I = typeof inputs;
type O = typeof outputs;

// rule definitions:
// if service is poor OR food is awful -> tip is low
// if service is normal -> tip is medium
// if service is excellent OR food is delicious -> tip is high
const rules = [
  or<I, O>({ food: "awful", service: "poor" }, { tip: "low" }),
  or<I, O>({ service: "good" }, { tip: "medium" }),
  or<I, O>({ food: "delicious", service: "excellent" }, { tip: "high" }),
];

// defuzzification using default center-of-gravity strategy
defuzz(
  inputs,
  outputs,
  rules,
  // input values
  { food: 7.32, service: 7.83 },
);
// { tip: 22.650000000000034 }

// defuzz with custom strategy (note: each has further config options)
defuzz(
  inputs,
  outputs,
  rules,
  // input values
  { food: 7.32, service: 7.83 },
  // custom defuzz strategy
  meanOfMaximaStrategy()
);
// { tip: 25.050000000000043 }

Note: The results are slightly different than those in the textbook example, due to different gaussian fuzzy sets used for the service L-var.

Using instrumentStrategy() from the upcoming @thi.ng/fuzzy-viz package, we can also visualize the final, transformed fuzzy sets used to compute crisp results and highlight the position of the crisp result value.

Here is the ASCII art output for the centroidStrategy and using tnormMin (the default) to transform each rule's output set(s):

// wrap existing strategy
const strat = instrumentStrategy(centroidStrategy(), fuzzySetToAscii());

// call defuzz as normal
defuzz(inputs, outputs, rules, strat);

// obtain secondary results via deref()
console.log(strat.deref()[0]);
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅.....
.......................................................................▁|██████████████████████▇....
......................................................................▁█|███████████████████████▇...
....................................▅▆▆▆▆▆▆▆▆▆▆▆▆▆▆▆▆▆▆▆▆▆▆▆▆▆▆▆▅....▂██|████████████████████████▇..
...................................▅█████████████████████████████▅..▂███|██████████████████████████.
..................................▅███████████████████████████████▅▂████|███████████████████████████
                                                                        ^ 21.52

Different results can be obtained by adjusting the T-norm used to transform each rule's output sets, here using tnormHamacher(2).

.........................................................................|..........................
.........................................................................|..........................
.........................................................................|..........................
.........................................................................|..........................
.........................................................................|..........................
.........................................................................|..........................
.........................................................................|..........................
.........................................................................|..........................
.........................................................................|..........................
.........................................................................|..........................
.........................................................................|.........▃▂...............
.........................................................................|.......▃███▆▂.............
.........................................................................|....▁▅███████▇▃...........
................................................▁▃▆▃▁....................|.▁▄▇████████████▆▃........
...........................................▁▂▄▆███████▆▄▂▁..............▂|▇██████████████████▆▃▁....
...................................▁▂▃▄▅▆▇█████████████████▇▆▅▄▃▂▁..▂▄▆██|███████████████████████▅▃▁
                                                                         ^ 21.84

...or using tnormAczelAlsina(2) (there're many more available):

........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|.....▂▃▄▅▅▅▅▅▄▄▃▁..........
........................................................................|.▂▅▇██████████████▆▄.......
........................................................................|▇████████████████████▆▂....
........................................▁▂▃▄▄▅▅▆▆▆▆▆▆▆▅▅▄▄▃▂▁.........▃█|███████████████████████▆▁..
....................................▂▄▇███████████████████████▇▄▂....▆██|█████████████████████████▄.
..................................▃▇█████████████████████████████▇▃▂████|██████████████████████████▇
                                                                        ^ 21.58

Just for illustration purposes (and using a different example), SVG output can be obtained by merely switching to another instrumentation function (here fuzzySetToSvg()):

fuzzySetToSvg() visualization example

Authors

Karsten Schmidt

If this project contributes to an academic publication, please cite it as:

@misc{thing-fuzzy,
  title = "@thi.ng/fuzzy",
  author = "Karsten Schmidt",
  note = "https://thi.ng/fuzzy",
  year = 2020
}

License

© 2020 - 2021 Karsten Schmidt // Apache Software License 2.0

2.1.81

2 days ago

2.1.80

17 days ago

2.1.79

20 days ago

2.1.78

29 days ago

2.1.77

1 month ago

2.1.76

1 month ago

2.1.75

2 months ago

2.1.74

2 months ago

2.1.73

2 months ago

2.1.72

2 months ago

2.1.71

2 months ago

2.1.70

2 months ago

2.1.69

3 months ago

2.1.68

3 months ago

2.1.67

3 months ago

2.1.66

3 months ago

2.1.65

3 months ago

2.1.64

3 months ago

2.1.63

3 months ago

2.1.61

3 months ago

2.1.62

3 months ago

2.1.60

3 months ago

2.1.59

3 months ago

2.1.58

4 months ago

2.1.56

4 months ago

2.1.57

4 months ago

2.1.54

5 months ago

2.1.55

5 months ago

2.1.53

5 months ago

2.1.52

5 months ago

2.1.51

5 months ago

2.1.49

6 months ago

2.1.47

6 months ago

2.1.45

7 months ago

2.1.46

7 months ago

2.1.43

9 months ago

2.1.44

7 months ago

2.1.41

9 months ago

2.1.42

9 months ago

2.1.40

9 months ago

2.1.50

6 months ago

2.1.39

9 months ago

2.1.36

10 months ago

2.1.37

9 months ago

2.1.35

11 months ago

2.1.34

1 year ago

2.1.32

1 year ago

2.1.33

1 year ago

2.1.28

1 year ago

2.1.29

1 year ago

2.1.30

1 year ago

2.1.31

1 year ago

2.1.27

1 year ago

2.1.26

1 year ago

2.1.24

1 year ago

2.1.23

1 year ago

2.1.21

1 year ago

2.1.22

1 year ago

2.1.20

1 year ago

2.1.18

1 year ago

2.1.19

1 year ago

2.1.16

2 years ago

2.1.17

1 year ago

2.1.14

2 years ago

2.1.15

2 years ago

2.1.12

2 years ago

2.1.13

2 years ago

2.1.11

2 years ago

2.1.8

2 years ago

2.1.9

2 years ago

2.1.10

2 years ago

2.1.6

2 years ago

2.1.7

2 years ago

2.1.5

2 years ago

2.1.4

2 years ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.3

2 years ago

2.0.8

2 years ago

2.1.0

2 years ago

2.0.7

3 years ago

2.0.4

3 years ago

2.0.6

3 years ago

2.0.3

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.4

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.3

3 years ago

0.1.14

3 years ago

0.1.13

3 years ago

0.1.12

3 years ago

0.1.11

3 years ago

0.1.10

3 years ago

0.1.9

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago