From 08045c62a42bdba5ed7548b778360d8340c74b03 Mon Sep 17 00:00:00 2001 From: Daniel Knittl-Frank Date: Sun, 26 Jun 2011 11:25:49 +0200 Subject: [PATCH] initial commit of fuzzy functions in javascript --- linguistics.js | 12 ++++++++++++ membership.js | 37 +++++++++++++++++++++++++++++++++++++ modifiers.js | 15 +++++++++++++++ operators.js | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+) create mode 100644 linguistics.js create mode 100644 membership.js create mode 100644 modifiers.js create mode 100644 operators.js diff --git a/linguistics.js b/linguistics.js new file mode 100644 index 0000000..1b7db6f --- /dev/null +++ b/linguistics.js @@ -0,0 +1,12 @@ +/* simple linguistic terms to modify fuzzy sets */ + +function very(a) { return Con(a); } +function more_or_less(a) { return Dil(a); } +function plus(a) { return PoweredHedge(a, 1.25); } +function slightly(a) { return Int(and(plus(a), not(very(a)))); } +function sort_of(a) { + return and(Int(more_or_less(a)), Int(more_or_less(not(a)))); +} +function pretty(a) { return and(Int(a), not(Int(very(a)))); } +function rather(a) { return Int(very(a)); } +function minus(a) { return PoweredHedge(a, 0.75); } diff --git a/membership.js b/membership.js new file mode 100644 index 0000000..d413bb6 --- /dev/null +++ b/membership.js @@ -0,0 +1,37 @@ +/* membership functions for fuzzy sets */ + +// Γ function +function T(x, a, b) { + if(x < a) return 0; + if(x > b) return 1; + return (x-a)/(b-a); +} + +// S function +function S(x, a, b, g) { + if(x < a) return 0; + if(x > g) return 1; + if(x <= b) return _S(x-a, a, g); + else return 1-_S(x-g, a, g); +} +function _S(q, a, g) { + return 2*Math.pow(q/(g-a),2); +} + +// L function +function L(x, a, b) { + return 1-T(x, a, b); +} + +// Δ function +function D(x, a, b, g) { + if(x < a || x > g) return 0; + if(x <= b) return (x-a)/(b-a); + return (b-x)/(g-b); +} + +// Π function +function P(x, a, g) { + if(x <= g) return S(x, g-a, g-a/2, g); + return 1-S(x, g, g+a/2, g+a); +} diff --git a/modifiers.js b/modifiers.js new file mode 100644 index 0000000..1b211d0 --- /dev/null +++ b/modifiers.js @@ -0,0 +1,15 @@ +/* modifier functions for fuzzy sets */ + +function Dilation(mu) { return PoweredHedge(mu, 0.5); } +var Dil = Dilation; + +function Concentration(mu) { return PoweredHedge(mu, 2); } +var Con = Concentration; + +function PoweredHedge(mu, exponent){ return Math.pow(mu, exponent); } + +function ContrastIntensification(mu){ + if(mu < 0.5) return 2*Math.pow(mu, 2); + return 1-2*Math.pow((1-mu), 2); +} +var Int = ContrastIntensification; diff --git a/operators.js b/operators.js new file mode 100644 index 0000000..9cfbe19 --- /dev/null +++ b/operators.js @@ -0,0 +1,37 @@ +/* simple operators for fuzzy sets: + * complement (not), intersection (and) and union (and) + */ + +function not_zadeh(x) { return 1-x; } +function not_sugeno(x, l) { return (1-x)/(1+l*x); } +function not_yager(x, l) { return Math.pow(1-Math.pow(x, l), 1/l); } + +function not(x) { + return not_zadeh(x); + //return not_sugeno(x, -0.5); + //return not_yager(x, 2); +} + +function and_zadeh(a, b) { return Math.min(a, b); } +function and_zadeh2(a, b) { return a*b; } +function and_tnorm(a, b, l) { + return 1 - Math.min(1, Math.pow(Math.pow(1-a, l) + Math.pow(1-b, l), 1/l)); +} + +function and(a, b) { + return and_zadeh(a, b); + //return and_zadeh2(a, b); + //return and_tnorm(a, b, 2); +} + +function or_zadeh(a, b) { return Math.max(a, b); } +function or_zadeh2(a, b) { return a+b - a*b; } +function or_snorm(a, b, l) { + return Math.min(1, Math.pow(Math.pow(a, l) + Math.pow(b, l), 1/l)); +} + +function or(a, b) { + return or_zadeh(a, b); + //return or_zadeh(a, b); + //return or_snorm(a, b, 2); +} -- 2.11.4.GIT