nebular provides basic fuzzy membership functions, that can be used with Java collections to create fuzzy sets.
In the example below, we will see how to create a sigmoidal membership function and store its elements and their respective membership values in a HashMap.
SigmoidalMembershipFunction mf = new SigmoidalMembershipFunction(0.5, 1.5); Map<Double, Double> map = new LinkedHashMap<Double, Double>(); for(double i = 0.0; i <= 2.0 ; i+=0.01) { map.put(i, mf.evaluate(i)); } NumberFormat nf = NumberFormat.getInstance(); nf.setMinimumFractionDigits(2); nf.setMaximumFractionDigits(2); for(Map.Entry<Double, Double> entry : map.entrySet()) { System.out.println("i="+nf.format(entry.getKey())+", degree="+nf.format(entry.getValue())); }
The output is a map containing the initial values and its membership degrees, calculated using the sigmoidal membership function.
i=0.00, degree=0.32 i=0.01, degree=0.32 i=0.02, degree=0.32 i=0.03, degree=0.32 i=0.04, degree=0.33 ... ... ... i=1.95, degree=0.56 i=1.96, degree=0.56 i=1.97, degree=0.56 i=1.98, degree=0.56 i=1.99, degree=0.56
nebular has also functions for defuzzification, used to extract a crisp value from a fuzzy set. Below example demonstrates how to use the centroid defuzzification function, applied over a range of doubles and a trapezoidal membership function.
NumericRange<Double> x = new DoubleRange(-10.0, 10.0, 0.1); MembershipFunction<Double> mf = new TrapezoidalMembershipFunction(-5.0, -3.0, 0.0, 4.0); DefuzzificationFunction<Double> df = new CentroidDefuzzificationFunction<Double>(); double out = df.evaluate(x, mf); System.out.printf("The defuzzified output is %.4f", out);
Running the example above will produce the following output.
The defuzzified output is -0.9167